10
10
package org .eclipse .cdt .managedbuilder .internal .core .jsoncdb .generator ;
11
11
12
12
import java .io .ByteArrayInputStream ;
13
+ import java .io .File ;
13
14
import java .io .InputStream ;
14
15
import java .nio .charset .StandardCharsets ;
15
16
import java .util .ArrayList ;
16
17
import java .util .Collection ;
18
+ import java .util .HashMap ;
17
19
import java .util .LinkedHashSet ;
18
20
import java .util .List ;
21
+ import java .util .Map ;
19
22
23
+ import org .eclipse .cdt .core .CCorePlugin ;
24
+ import org .eclipse .cdt .core .envvar .IEnvironmentVariable ;
25
+ import org .eclipse .cdt .core .settings .model .ICConfigurationDescription ;
20
26
import org .eclipse .cdt .core .settings .model .ICSourceEntry ;
21
27
import org .eclipse .cdt .core .settings .model .util .CDataUtil ;
22
28
import org .eclipse .cdt .managedbuilder .core .BuildException ;
34
40
import org .eclipse .cdt .managedbuilder .internal .macros .FileContextData ;
35
41
import org .eclipse .cdt .managedbuilder .macros .BuildMacroException ;
36
42
import org .eclipse .cdt .managedbuilder .macros .IBuildMacroProvider ;
43
+ import org .eclipse .cdt .utils .CommandLineUtil ;
44
+ import org .eclipse .cdt .utils .PathUtil ;
37
45
import org .eclipse .core .resources .IFile ;
38
46
import org .eclipse .core .resources .IFolder ;
39
47
import org .eclipse .core .resources .IProject ;
@@ -58,7 +66,11 @@ public final class CompilationDatabaseGenerator {
58
66
59
67
private static final String CDB_FILENAME = "compile_commands.json" ; //$NON-NLS-1$
60
68
private static final String ERROR_MESSAGE = "Can not set contents to compile_commands.json file" ; //$NON-NLS-1$
61
-
69
+ /**
70
+ * Checked on each build
71
+ * Used before we look up the environment
72
+ */
73
+ private Map <ITool , String > toolMap = new HashMap <>();
62
74
private IProject project ;
63
75
private IConfiguration configuration ;
64
76
private ICSourceEntry [] srcEntries ;
@@ -217,10 +229,22 @@ private List<CompilationDatabaseInformation> populateObjList(IProject project, I
217
229
outputLocation + "" , inputStrings , sourceLocation , outputLocation ); //$NON-NLS-1$
218
230
219
231
IBuildMacroProvider provider = ManagedBuildManager .getBuildMacroProvider ();
220
- String resolvedOptionFileContents = provider .resolveValueToMakefileFormat (cmdLInfo .getCommandLine (), "" , //$NON-NLS-1$
221
- " " , IBuildMacroProvider .CONTEXT_FILE , //$NON-NLS-1$
222
- new FileContextData (sourceLocation , outputLocation , null , tool ));
232
+ String compilerName = CompilationDatabaseGenerator .getCompilerName (tool );
233
+ String commandLine = cmdLInfo .getCommandLine ();
234
+ commandLine = commandLine .replace (compilerName , "" ).trim (); //$NON-NLS-1$
235
+ String compilerPath = findCompilerInPath (tool , config );
236
+ String resolvedOptionFileContents ;
237
+ if (compilerPath != null && !compilerPath .isEmpty ()) {
238
+ resolvedOptionFileContents = provider .resolveValueToMakefileFormat (compilerPath + " " + commandLine , //$NON-NLS-1$
239
+ "" , " " , //$NON-NLS-1$//$NON-NLS-2$
240
+ IBuildMacroProvider .CONTEXT_FILE ,
241
+ new FileContextData (sourceLocation , outputLocation , null , tool ));
242
+ } else {
243
+ resolvedOptionFileContents = provider .resolveValueToMakefileFormat (commandLine , "" , " " , //$NON-NLS-1$//$NON-NLS-2$
244
+ IBuildMacroProvider .CONTEXT_FILE ,
245
+ new FileContextData (sourceLocation , outputLocation , null , tool ));
223
246
247
+ }
224
248
objList .add (new CompilationDatabaseInformation (project .getLocation ().toString (),
225
249
resolvedOptionFileContents , resource .getLocation ().toString ()));
226
250
}
@@ -414,4 +438,44 @@ public boolean visit(IResourceProxy proxy) throws CoreException {
414
438
415
439
}
416
440
441
+ private String findCompilerInPath (ITool tool , IConfiguration config ) {
442
+ if (toolMap .containsKey (tool )) {
443
+ return "\" " + toolMap .get (tool ) + "\" " ; //$NON-NLS-1$//$NON-NLS-2$
444
+ }
445
+ String compilerName = CompilationDatabaseGenerator .getCompilerName (tool );
446
+ File pathToCompiler = new File (compilerName );
447
+
448
+ if (pathToCompiler .isAbsolute ()) {
449
+ toolMap .put (tool , compilerName );
450
+ return "\" " + compilerName + "\" " ; //$NON-NLS-1$ //$NON-NLS-2$
451
+ }
452
+ ICConfigurationDescription cfg = ManagedBuildManager .getDescriptionForConfiguration (config );
453
+ IEnvironmentVariable [] variables = CCorePlugin .getDefault ().getBuildEnvironmentManager ().getVariables (cfg ,
454
+ true );
455
+
456
+ for (IEnvironmentVariable variable : variables ) {
457
+ if ("PATH" .equalsIgnoreCase (variable .getName ())) { //$NON-NLS-1$
458
+ IPath resolvedPath = PathUtil .findProgramLocation (compilerName , variable .getValue ());
459
+ if (resolvedPath != null ) {
460
+ String path = resolvedPath .toString ();
461
+ toolMap .put (tool , path );
462
+ return "\" " + path + "\" " ; //$NON-NLS-1$ //$NON-NLS-2$
463
+ } else {
464
+ return null ; // Only one PATH so can exit early
465
+ }
466
+ }
467
+ }
468
+
469
+ return null ;
470
+ }
471
+
472
+ private static String getCompilerName (ITool tool ) {
473
+ String compilerCommand = tool .getToolCommand ();
474
+ String [] arguments = CommandLineUtil .argumentsToArray (compilerCommand );
475
+ if (arguments .length == 0 ) {
476
+ return "" ; //$NON-NLS-1$
477
+ }
478
+ return arguments [0 ];
479
+ }
480
+
417
481
}
0 commit comments