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,7 @@ 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
+ private Map < ITool , String > toolMap = new HashMap <>();
62
70
private IProject project ;
63
71
private IConfiguration configuration ;
64
72
private ICSourceEntry [] srcEntries ;
@@ -217,8 +225,12 @@ private List<CompilationDatabaseInformation> populateObjList(IProject project, I
217
225
outputLocation + "" , inputStrings , sourceLocation , outputLocation ); //$NON-NLS-1$
218
226
219
227
IBuildMacroProvider provider = ManagedBuildManager .getBuildMacroProvider ();
220
- String resolvedOptionFileContents = provider .resolveValueToMakefileFormat (cmdLInfo .getCommandLine (), "" , //$NON-NLS-1$
221
- " " , IBuildMacroProvider .CONTEXT_FILE , //$NON-NLS-1$
228
+ String compilerName = CompilationDatabaseGenerator .getCompilerName (tool );
229
+ String commandLine = cmdLInfo .getCommandLine ();
230
+ commandLine = commandLine .replace (compilerName , "" ).trim (); //$NON-NLS-1$
231
+ commandLine = commandLine .replaceAll ("\\ s+" , " " ).trim (); //$NON-NLS-1$ //$NON-NLS-2$
232
+ String resolvedOptionFileContents = provider .resolveValueToMakefileFormat (
233
+ findCompilerInPath (tool , config ) + " " + commandLine , "" , " " , IBuildMacroProvider .CONTEXT_FILE , //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
222
234
new FileContextData (sourceLocation , outputLocation , null , tool ));
223
235
224
236
objList .add (new CompilationDatabaseInformation (project .getLocation ().toString (),
@@ -414,4 +426,41 @@ public boolean visit(IResourceProxy proxy) throws CoreException {
414
426
415
427
}
416
428
429
+ private String findCompilerInPath (ITool tool , IConfiguration config ) {
430
+ if (toolMap .containsKey (tool )) {
431
+ return "\" " + toolMap .get (tool ) + "\" " ; //$NON-NLS-1$//$NON-NLS-2$
432
+ }
433
+ String compilerName = CompilationDatabaseGenerator .getCompilerName (tool );
434
+ File pathToCompiler = new File (compilerName );
435
+
436
+ if (pathToCompiler .isAbsolute ()) {
437
+ toolMap .put (tool , compilerName );
438
+ return "\" " + compilerName + "\" " ; //$NON-NLS-1$ //$NON-NLS-2$
439
+ }
440
+ ICConfigurationDescription cfg = ManagedBuildManager .getDescriptionForConfiguration (config );
441
+ IEnvironmentVariable [] variables = CCorePlugin .getDefault ().getBuildEnvironmentManager ().getVariables (cfg ,
442
+ true );
443
+
444
+ for (IEnvironmentVariable variable : variables ) {
445
+ if ("PATH" .equals (variable .getName ())) { //$NON-NLS-1$
446
+ IPath resolvedPath = PathUtil .findProgramLocation (compilerName , variable .getValue ());
447
+ if (resolvedPath != null ) {
448
+ String path = resolvedPath .toString ().replaceAll ("\\ s+" , " " ).trim (); //$NON-NLS-1$//$NON-NLS-2$
449
+ toolMap .put (tool , path );
450
+ return "\" " + path + "\" " ; //$NON-NLS-1$ //$NON-NLS-2$
451
+ } else {
452
+ return null ; // Only one PATH so can exit early
453
+ }
454
+ }
455
+ }
456
+
457
+ return null ;
458
+ }
459
+
460
+ private static String getCompilerName (ITool tool ) {
461
+ String compilerCommand = tool .getToolCommand ();
462
+ String [] arguments = CommandLineUtil .argumentsToArray (compilerCommand );
463
+ return arguments [0 ];
464
+ }
465
+
417
466
}
0 commit comments