Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ffd1ef5713354f5b33931b9a799538ab4bc5c8a5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*******************************************************************************
 * Copyright (c) 2010 Red Hat Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Red Hat Inc. - initial API and implementation
 *     Sergey Prigogin (Google)
 *******************************************************************************/
package org.eclipse.cdt.internal.autotools.core;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
import java.lang.reflect.Method;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.cdt.autotools.core.AutotoolsPlugin;
import org.eclipse.cdt.core.IErrorParser;
import org.eclipse.cdt.core.ProblemMarkerInfo;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;

// This class would normally extend IErrorParser and use the CDT error parser
// extension.  However, we want an extended IMarker that contains library info and
// possibly other data in the future.  The standard CDT ErrorParserManager doesn't allow
// us to pass an extended ProblemMarkerInfo, so we are forced to have our own mechanism
// which is similar to the CDT one.
public class ErrorParser extends MarkerGenerator implements IErrorParser {
	public static final String ID = AutotoolsPlugin.PLUGIN_ID + ".errorParser"; //$NON-NLS-1$
	private Pattern pkgconfigError = 
			Pattern.compile(".*?(configure:\\s+error:\\s+Package requirements\\s+\\((.*?)\\)\\s+were not met).*"); //$NON-NLS-1$
	private Pattern genconfigError = 
			Pattern.compile(".*?configure:\\s+error:\\s+(.*)"); //$NON-NLS-1$
	private Pattern checkingFail = 
			Pattern.compile("checking for (.*)\\.\\.\\. no"); //$NON-NLS-1$

	private Pattern changingConfigDirectory = 
			Pattern.compile("Configuring in (.*)"); //$NON-NLS-1$

	private IPath buildDir;
	private IPath sourcePath;
	private IProject project;

	public ErrorParser(){
	}
	
	public ErrorParser(IPath sourcePath, IPath buildPath) {
		this.buildDir = buildPath;
		this.sourcePath = sourcePath;
	}

	@Override
	public boolean processLine(String line,
			org.eclipse.cdt.core.ErrorParserManager eoParser) {

		if (this.project == null)
			this.project = eoParser.getProject();

		if (this.buildDir == null)
			this.buildDir = new Path(eoParser.getWorkingDirectoryURI().getPath());
		
		if (this.sourcePath == null)
			this.sourcePath = eoParser.getProject().getLocation();

		AutotoolsProblemMarkerInfo marker = processLine(line);
		if ( marker != null){
			// Check to see if addProblemMarker exists.
			try {
				Method method = eoParser.getClass().getMethod("addProblemMarker", ProblemMarkerInfo.class);
				try {
					method.invoke(eoParser, marker);
					return true;
				} catch (Exception e) {
					throw new RuntimeException(e);
				}
			} catch (SecurityException e) {
				return false;
			} catch (NoSuchMethodException e) {
				return false;
			}
		}
		return false;
	}

	public boolean processLine(String line, ErrorParserManager eoParser) {
		if (this.project == null)
			this.project = eoParser.getProject();
		
		AutotoolsProblemMarkerInfo marker = processLine(line);
		if ( marker != null){
			eoParser.addProblemMarker(marker);
			return true;
		}
		return false;
	}

	public AutotoolsProblemMarkerInfo processLine(String line) {
		Matcher m;
		
		m = changingConfigDirectory.matcher(line);
		if(m.matches()){
			// set configuration directory.
			this.buildDir = this.buildDir.append(m.group(1));
			this.sourcePath = this.sourcePath.append(m.group(1));
			return null;
		}

		m = pkgconfigError.matcher(line);
		if (m.matches()) {
			return new AutotoolsProblemMarkerInfo(getProject(), -1, m.group(1), SEVERITY_ERROR_BUILD, null, null, m.group(2), AutotoolsProblemMarkerInfo.Type.PACKAGE);
		}
		
		m = genconfigError.matcher(line);
		if (m.matches()) {
			return new AutotoolsProblemMarkerInfo(getProject(), -1, m.group(1), SEVERITY_ERROR_BUILD, null,
					AutotoolsProblemMarkerInfo.Type.GENERIC);
		}
		
		m = checkingFail.matcher(line);
		if (m.matches()) {
			// We know that there is a 'checking for ...' fail.
			// Find the log file containing this check
			AutotoolsProblemMarkerInfo.Type type = getCheckType(m.group(1));
			if (type != null)
				return new AutotoolsProblemMarkerInfo(getProject(), "Missing " + type + " " + m.group(1), SEVERITY_INFO, m.group(1), type);
		}

		return null;
	}

	/**
	 * Given the name of the filed check object, look for it in the log file
	 * file and then examine the configure script to figure out what the type of
	 * the check was.
	 * 
	 * @param name
	 * @return
	 */
	private AutotoolsProblemMarkerInfo.Type getCheckType(String name) {
		int lineNumber = getErrorConfigLineNumber(name);

		// now open configure file.
		File file = new File(sourcePath + "/configure");
		// If the log file is not present there is nothing we can do.
		if (!file.exists())
			return null;

		LineNumberReader reader = null;
		try {
			reader = new LineNumberReader(new FileReader(file));

			// look for something like:
			// if test "${ac_cv_prog_WINDRES+set}" = set; then :
			Pattern errorPattern = Pattern.compile(".*ac_cv_([a-z]*)_.*"); //$NON-NLS-1$

			// skip to the line
			String line = reader.readLine();
			for (int i = 0; i < lineNumber + 10 && line != null; i++) {
				if (i < lineNumber) {
					line = reader.readLine();
					continue;
				}
				Matcher m = errorPattern.matcher(line);
				if (m.matches()) {
					String typeString = m.group(1);
					if (typeString.equals("prog"))
						return AutotoolsProblemMarkerInfo.Type.PROG;
					if (typeString.equals("header"))
						return AutotoolsProblemMarkerInfo.Type.HEADER;
					if (typeString.equals("file"))
						return AutotoolsProblemMarkerInfo.Type.FILE;
					if (typeString.equals("lib"))
						return AutotoolsProblemMarkerInfo.Type.LIB;

					return null;
				}
				line = reader.readLine();
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e) {
					// Ignore.
				}
			}
		}

		return null;
	}

	/**
	 * Check the log file for the check for the given name and return the line
	 * number in configure where the check occurs.
	 * 
	 * @param name
	 * @return
	 */
	private int getErrorConfigLineNumber(String name) {
		LineNumberReader reader = null;
		try {
			File file = new File(buildDir + "/config.log");
			// If the log file is not present there is nothing we can do.
			if (!file.exists())
				return -1;

			reader = new LineNumberReader(new FileReader(file));

			Pattern errorPattern =
					Pattern.compile("configure:(\\d+): checking for " + name); //$NON-NLS-1$
			String line;
			while ((line = reader.readLine()) != null) {
				Matcher m = errorPattern.matcher(line);
				if (m.matches()) {
					return Integer.parseInt(m.group(1));
				}
			}
		} catch (Exception e) {
			return -1;
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e) {
					// Ignore.
				}
			}
		}
		return -1;
	}
	
	@Override
	public IProject getProject() {
		return this.project;
	}

}

Back to the top