Skip to main content
summaryrefslogtreecommitdiffstats
blob: c31536e7fe4a0a2fd98bb7c12f2c1e9c4cc2d493 (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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
/*******************************************************************************
 * Copyright (c) 2004, 2008 IBM Corporation and others.
 * 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:
 *     IBM - Initial API and implementation
 *     Markus Schorn (Wind River Systems)
 *     Gerhard Schaber (Wind River Systems)
 *******************************************************************************/
package org.eclipse.cdt.make.internal.core.scannerconfig.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

/**
 * Class that represents a compiler command and related scanner configuration 
 * 
 * @author vhirsl
 */
public class CCommandDSC {
    private final static String SINGLE_SPACE = " "; //$NON-NLS-1$
    private final static String CMD_DESCRIPTION_ELEM = "commandDescription"; //$NON-NLS-1$
    private final static String CMD_SI_ELEM = "commandScannerInfo"; //$NON-NLS-1$
    private final static String OPTION_ELEM = "option"; //$NON-NLS-1$
    private final static String SI_ITEM_ELEM = "siItem"; //$NON-NLS-1$
    private final static String KEY_ATTR = "key"; //$NON-NLS-1$
    private final static String VALUE_ATTR = "value"; //$NON-NLS-1$
    private final static String QUOTE_INCLUDE_ATTR = "quote"; //$NON-NLS-1$
    private final static String KIND_ATTR = "kind"; //$NON-NLS-1$
    
	private int commandId;
	private List compilerCommand;	// members are KVStringPair objects
	private boolean discovered;
	private boolean cppFileType;	// C or C++ file type
	private IProject project;

    private List symbols;
    private List includes;
    private List quoteIncludes;
    
    /**
	 * @param cppFileType2 
	 */
	public CCommandDSC(boolean cppFileType) {
		this(cppFileType, null);
	}
	
	public CCommandDSC(boolean cppFileType, IProject project) {
		compilerCommand = new ArrayList();
		discovered = false;
		this.cppFileType = cppFileType;
        
        symbols = new ArrayList();
        includes = new ArrayList();
        quoteIncludes = new ArrayList();
        this.project = project;
	}

    public boolean appliesToCPPFileType() {
        return cppFileType;
    }
    
	public void addSCOption(KVStringPair option) {
		if (project != null &&
			(option.getKey().equals(SCDOptionsEnum.INCLUDE_FILE.toString()) ||
			 option.getKey().equals(SCDOptionsEnum.INCLUDE.toString()) ||
			 option.getKey().equals(SCDOptionsEnum.ISYSTEM.toString()) ||
			 option.getKey().equals(SCDOptionsEnum.IMACROS_FILE.toString()) ||
			 option.getKey().equals(SCDOptionsEnum.IQUOTE.toString())))
		{
			String value = option.getValue();
			value = (String)CygpathTranslator.translateIncludePaths(project, Collections.singletonList(value)).get(0);
			value = makeRelative(project, new Path(value)).toOSString();
			option = new KVStringPair(option.getKey(), value);
		}
		compilerCommand.add(option);
	}
	
    /**
     * @return
     */
    public Integer getCommandIdAsInteger() {
        return new Integer(getCommandId());
    }
    /**
     * @return Returns the commandId.
     */
    public int getCommandId() {
        return commandId;
    }
    /**
     * @param commandId The commandId to set.
     */
    public void setCommandId(int commandId) {
        this.commandId = commandId;
    }
    
	public String toString() {
		String commandAsString = new String();
		for (Iterator i = compilerCommand.iterator(); i.hasNext(); ) {
			KVStringPair optionPair = (KVStringPair)i.next();
			String value = optionPair.getValue();
			commandAsString += optionPair.getKey() + SINGLE_SPACE + 
                               value + SINGLE_SPACE;
		}
		return commandAsString.trim();
	}
	
	public int getId() {
		return commandId;
	}
	
	/**
	 * Returns a command where -imacros and -include options have been removed
	 * @param quoteIncludePaths whether or not paths for includes must be put inside double quotes.
	 * @return the command line to run the scanner discovery.
	 */
	public String getSCDRunnableCommand(boolean quoteIncludePaths, boolean quoteDefines) {
		String commandAsString = new String();
		for (Iterator i = compilerCommand.iterator(); i.hasNext(); ) {
			KVStringPair optionPair = (KVStringPair)i.next();
            if (optionPair.getKey().equals(SCDOptionsEnum.COMMAND.toString())) {
                commandAsString += optionPair.getValue() + SINGLE_SPACE;
            }
            else {
                // skip -include and -imacros options
    			if (optionPair.getKey().equals(SCDOptionsEnum.IMACROS_FILE.toString()) ||
    					optionPair.getKey().equals(SCDOptionsEnum.INCLUDE_FILE.toString()))
    				continue;
    			String value = optionPair.getValue();
    			if (optionPair.getKey().equals(SCDOptionsEnum.INCLUDE.toString()) ||
    				optionPair.getKey().equals(SCDOptionsEnum.ISYSTEM.toString()) || 
    				optionPair.getKey().equals(SCDOptionsEnum.IQUOTE.toString())) {
    				value = makeAbsolute(project, value);
    				if (quoteIncludePaths) {
    					value= "\"" + value + "\""; //$NON-NLS-1$ //$NON-NLS-2$
    				}
    			}
    			else if (quoteDefines && optionPair.getKey().equals(SCDOptionsEnum.DEFINE.toString())) {
    				if (value.indexOf('\'') == -1) {
    					value= "'" + value + "'";  //$NON-NLS-1$//$NON-NLS-2$
    				}
    				else {
    					value= value.replaceAll("\"", "\\\\\"");  //$NON-NLS-1$//$NON-NLS-2$
    					value= "\"" + value + "\""; //$NON-NLS-1$ //$NON-NLS-2$
    				}
    			}
    			commandAsString += optionPair.getKey() + SINGLE_SPACE + value + SINGLE_SPACE;
            }
		}
		return commandAsString.trim();
	}
	
	/**
	 * Returns the compiler command
	 * @return
	 */
	public String getCompilerName() {
		String compiler = new String();
		for (Iterator i = compilerCommand.iterator(); i.hasNext(); ) {
			KVStringPair optionPair = (KVStringPair)i.next();
            if (optionPair.getKey().equals(SCDOptionsEnum.COMMAND.toString())) {
            	compiler = optionPair.getValue();
            	break;
            }
		}
		return compiler.trim();
	}
	
	/**
	 * @return list of strings
	 */
	public List getImacrosFile() {
		List imacrosFiles = new ArrayList();
		for (Iterator i = compilerCommand.iterator(); i.hasNext(); ) {
			KVStringPair optionPair = (KVStringPair)i.next();
			if (optionPair.getKey().equals(SCDOptionsEnum.IMACROS_FILE.toString())) {
				imacrosFiles.add(makeAbsolute(project,optionPair.getValue()));
			}
		}
		return imacrosFiles;
	}
	
	/**
	 * @return list of strings
	 */
	public List getIncludeFile() {
		List includeFiles = new ArrayList();
		for (Iterator i = compilerCommand.iterator(); i.hasNext(); ) {
			KVStringPair optionPair = (KVStringPair)i.next();
			if (optionPair.getKey().equals(SCDOptionsEnum.INCLUDE_FILE.toString())) {
				includeFiles.add(makeAbsolute(project,optionPair.getValue()));
			}
		}
		return includeFiles;
	}
	
//	public List getFilesList() {
//		return files;
//	}
	
	/* (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	public boolean equals(Object arg0) {
		if (arg0 != null && arg0.getClass().equals(this.getClass())) {
            CCommandDSC other = (CCommandDSC)arg0;
			return (compilerCommand.equals(other.compilerCommand) &&
                cppFileType == other.cppFileType);
		}
		return false;
	}
	/* (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	public int hashCode() {
		return compilerCommand.hashCode();
	}

    /**
     * @return Returns the includes as strings.
     */
    public List getIncludes() {
        return makeAbsolute(project, includes);
    }
    /**
     * @param includes The includes to set.
     */
    public void setIncludes(List includes) {
        this.includes = includes;
    }
    /**
     * @return Returns the quote include paths as strings (for #include "...")
     */
    public List getQuoteIncludes() {
        return makeAbsolute(project, quoteIncludes);
    }
    /**
     * @param includes. Quote include paths (for #include "...")
     */
    public void setQuoteIncludes(List includes) {
        quoteIncludes = includes;
    }
    /**
     * @return Returns the symbols.
     */
    public List getSymbols() {
        return symbols;
    }
    /**
     * @param symbols The symbols to set.
     */
    public void setSymbols(List symbols) {
        this.symbols = symbols;
    }
    /**
     * @return Returns the discovered.
     */
    public boolean isDiscovered() {
        return discovered;
    }
    /**
     * @param discovered The discovered to set.
     */
    public void setDiscovered(boolean discovered) {
        this.discovered = discovered;
    }

    /**
     * @param cmdElem
     */
    public void serialize(Element cmdElem) {
        Document doc = cmdElem.getOwnerDocument();
        // serialize the command
        Element cmdDescElem = doc.createElement(CMD_DESCRIPTION_ELEM);
        for (Iterator i = compilerCommand.iterator(); i.hasNext(); ) {
            Element optionElem = doc.createElement(OPTION_ELEM); 
            KVStringPair option = (KVStringPair) i.next();
            optionElem.setAttribute(KEY_ATTR, option.getKey()); 
            optionElem.setAttribute(VALUE_ATTR, option.getValue()); 
            cmdDescElem.appendChild(optionElem);
        }
        cmdElem.appendChild(cmdDescElem);
        // serialize includes and symbols
        Element siElem = doc.createElement(CMD_SI_ELEM);
        for (Iterator j = quoteIncludes.iterator(); j.hasNext(); ) {
            Element siItem = doc.createElement(SI_ITEM_ELEM); 
            siItem.setAttribute(KIND_ATTR, "INCLUDE_PATH");  //$NON-NLS-1$
            siItem.setAttribute(VALUE_ATTR, (String) j.next());
            siItem.setAttribute(QUOTE_INCLUDE_ATTR, "true"); //$NON-NLS-1$
            siElem.appendChild(siItem);
        }
        for (Iterator j = includes.iterator(); j.hasNext(); ) {
            Element siItem = doc.createElement(SI_ITEM_ELEM); 
            siItem.setAttribute(KIND_ATTR, "INCLUDE_PATH");  //$NON-NLS-1$
            siItem.setAttribute(VALUE_ATTR, (String) j.next());
            siElem.appendChild(siItem);
        }
        for (Iterator j = symbols.iterator(); j.hasNext(); ) {
            Element siItem = doc.createElement(SI_ITEM_ELEM);
            siItem.setAttribute(KIND_ATTR, "SYMBOL_DEFINITION"); //$NON-NLS-1$
            siItem.setAttribute(VALUE_ATTR, (String) j.next()); 
            siElem.appendChild(siItem);
        }
        cmdElem.appendChild(siElem);
    }

    /**
     * @param cmdElem
     */
    public void deserialize(Element cmdElem) {
        // read command options
        NodeList descList = cmdElem.getElementsByTagName(CMD_DESCRIPTION_ELEM);
        if (descList.getLength() > 0) {
            Element descElem = (Element) descList.item(0);
            NodeList optionList = descElem.getElementsByTagName(OPTION_ELEM);
            for (int i = 0; i < optionList.getLength(); ++i) {
                Element optionElem = (Element) optionList.item(i);
                String key = optionElem.getAttribute(KEY_ATTR);
                String value = optionElem.getAttribute(VALUE_ATTR);
                KVStringPair option = new KVStringPair(key, value);
                addSCOption(option);
            }
        }
        // read associated scanner info
        NodeList siList = cmdElem.getElementsByTagName(CMD_SI_ELEM);
        if (siList.getLength() > 0) {
            Element siElem = (Element) siList.item(0);
            NodeList siItemList = siElem.getElementsByTagName(SI_ITEM_ELEM);
            for (int i = 0; i < siItemList.getLength(); ++i) {
                Element siItemElem = (Element) siItemList.item(i);
                String kind = siItemElem.getAttribute(KIND_ATTR);
                String value = siItemElem.getAttribute(VALUE_ATTR);
                String quote = siItemElem.getAttribute(QUOTE_INCLUDE_ATTR);
                if (kind.equals("INCLUDE_PATH")) { //$NON-NLS-1$
                    if (quote.equals("true")) { //$NON-NLS-1$
                        quoteIncludes.add(value);
                    }
                    else {
                        includes.add(value);
                    }
                }
                else if (kind.equals("SYMBOL_DEFINITION")) { //$NON-NLS-1$
                    symbols.add(value);
                }
            }
            setDiscovered(true);
        }
    }
    
    public void resolveOptions(IProject project) {
    	if (!isDiscovered()) {
    		// that's wrong for sure, options cannot be resolved fron the optionPairs??
    		ArrayList symbols = new ArrayList();
    		ArrayList includes = new ArrayList();
    		ArrayList quoteincludes = new ArrayList();
    		for (Iterator options = compilerCommand.iterator(); options.hasNext(); ) {
    			KVStringPair optionPair = (KVStringPair)options.next();
    			String key = optionPair.getKey();
    			String value = optionPair.getValue();
    			if (key.equals(SCDOptionsEnum.INCLUDE.toString()) || key.equals(SCDOptionsEnum.ISYSTEM.toString())) {
    				includes.add(value);
    			}
    			else if (key.equals(SCDOptionsEnum.IQUOTE.toString())) {
    				quoteincludes.add(value);
    			}
    			else if (key.equals(SCDOptionsEnum.DEFINE.toString())) {
    				symbols.add(value);
    			}
    		}
    		setIncludes(includes);
    		setQuoteIncludes(quoteincludes);		                
    		setSymbols(symbols);
    	}
		setDiscovered(true);    	
    }
    
	public static IPath makeRelative(IProject project, IPath path) {
		IResource resource = findResource(project, path);
		if (resource != null) {
			if (resource.getProject() == project) {
				path = resource.getProjectRelativePath();
			}
//			else {
//				path = resource.getFullPath();
//			}
		}
		return path;
	}

	private static IResource findResource(IProject project, IPath path) {
		IResource resource = project.findMember(path, false);
		if (resource == null) {
			IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
			resource = root.findMember(path, false);
			if (resource == null) {
				IResource[] resources = root.findFilesForLocation(path);
				if (project != null) {
					for (int i = 0; i < resources.length; i++) {
						final IProject myProject = resources[i].getProject();
						// resource could be root, then myProject is null.
						if (myProject != null && myProject.equals(project)) {
							resource = resources[i];
							break;
						}
					}
				}
				// make a relative path to another project (better than an absolute path)
				if (resource == null && resources.length > 0) {
					resource = resources[0];
				}
			}
		}
		return resource;
	}

	public static List makeRelative(IProject project, List paths) {
		List list = new ArrayList(paths.size());
		for (Iterator iter=paths.iterator(); iter.hasNext(); ) {
			String path = (String)iter.next();
			path = makeRelative(project, new Path(path)).toOSString();
			list.add(path);
		}
		return list;
	}


	public static final String makeAbsolute(IProject project, String path) {
		IPath ppath = new Path(path);
		if (project != null && !ppath.isAbsolute()) {
			IResource res = project.findMember(ppath);
			if (res != null) {
				ppath = res.getLocation();
				if (ppath != null) {
					path = ppath.toOSString();
				}
			}
//			path = new File(project.getLocation().toOSString(), path).getAbsolutePath();
		}
		return path;
	}

	public static List makeAbsolute(IProject project, List paths) {
		List list = new ArrayList(paths.size());
		for (Iterator iter=paths.iterator(); iter.hasNext(); ) {
			String path = (String)iter.next();
			path = makeAbsolute(project, path);
			list.add(path);
		}
		return list;
	}
}

Back to the top