14
14
import java .io .InputStream ;
15
15
import java .nio .charset .StandardCharsets ;
16
16
import java .util .ArrayList ;
17
+ import java .util .Arrays ;
17
18
import java .util .Collection ;
18
19
import java .util .HashMap ;
19
20
import java .util .LinkedHashSet ;
@@ -70,7 +71,7 @@ public final class CompilationDatabaseGenerator {
70
71
* Checked on each build
71
72
* Used before we look up the environment
72
73
*/
73
- private Map <ITool , String > toolMap = new HashMap <>();
74
+ private Map <String , String > toolMap = new HashMap <>();
74
75
private IProject project ;
75
76
private IConfiguration configuration ;
76
77
private ICSourceEntry [] srcEntries ;
@@ -229,18 +230,19 @@ private List<CompilationDatabaseInformation> populateObjList(IProject project, I
229
230
outputLocation + "" , inputStrings , sourceLocation , outputLocation ); //$NON-NLS-1$
230
231
231
232
IBuildMacroProvider provider = ManagedBuildManager .getBuildMacroProvider ();
232
- String compilerName = CompilationDatabaseGenerator .getCompilerName (tool );
233
233
String commandLine = cmdLInfo .getCommandLine ();
234
- commandLine = commandLine .replace (compilerName , "" ).trim (); //$NON-NLS-1$
235
- String compilerPath = findCompilerInPath (tool , config );
234
+ String compilerName = CompilationDatabaseGenerator .getCompilerName (commandLine );
235
+ String compilerArguments = getCompilerArgs (commandLine );
236
+ String compilerPath = findCompilerInPath (tool , config , compilerName );
236
237
String resolvedOptionFileContents ;
237
238
if (compilerPath != null && !compilerPath .isEmpty ()) {
238
- resolvedOptionFileContents = provider .resolveValueToMakefileFormat (compilerPath + " " + commandLine , //$NON-NLS-1$
239
+ resolvedOptionFileContents = provider .resolveValueToMakefileFormat (
240
+ compilerPath + " " + compilerArguments , //$NON-NLS-1$
239
241
"" , " " , //$NON-NLS-1$//$NON-NLS-2$
240
242
IBuildMacroProvider .CONTEXT_FILE ,
241
243
new FileContextData (sourceLocation , outputLocation , null , tool ));
242
244
} else {
243
- resolvedOptionFileContents = provider .resolveValueToMakefileFormat (commandLine , "" , " " , //$NON-NLS-1$//$NON-NLS-2$
245
+ resolvedOptionFileContents = provider .resolveValueToMakefileFormat (compilerArguments , "" , " " , //$NON-NLS-1$//$NON-NLS-2$
244
246
IBuildMacroProvider .CONTEXT_FILE ,
245
247
new FileContextData (sourceLocation , outputLocation , null , tool ));
246
248
@@ -438,15 +440,15 @@ public boolean visit(IResourceProxy proxy) throws CoreException {
438
440
439
441
}
440
442
441
- private String findCompilerInPath (ITool tool , IConfiguration config ) {
442
- if (toolMap .containsKey (tool )) {
443
- return "\" " + toolMap .get (tool ) + "\" " ; //$NON-NLS-1$//$NON-NLS-2$
443
+ private String findCompilerInPath (ITool tool , IConfiguration config , String compilerName ) {
444
+ String cacheKey = compilerName ;
445
+ if (toolMap .containsKey (cacheKey )) {
446
+ return "\" " + toolMap .get (cacheKey ) + "\" " ; //$NON-NLS-1$//$NON-NLS-2$
444
447
}
445
- String compilerName = CompilationDatabaseGenerator .getCompilerName (tool );
446
448
File pathToCompiler = new File (compilerName );
447
449
448
450
if (pathToCompiler .isAbsolute ()) {
449
- toolMap .put (tool , compilerName );
451
+ toolMap .put (cacheKey , compilerName );
450
452
return "\" " + compilerName + "\" " ; //$NON-NLS-1$ //$NON-NLS-2$
451
453
}
452
454
ICConfigurationDescription cfg = ManagedBuildManager .getDescriptionForConfiguration (config );
@@ -458,7 +460,7 @@ private String findCompilerInPath(ITool tool, IConfiguration config) {
458
460
IPath resolvedPath = PathUtil .findProgramLocation (compilerName , variable .getValue ());
459
461
if (resolvedPath != null ) {
460
462
String path = resolvedPath .toString ();
461
- toolMap .put (tool , path );
463
+ toolMap .put (cacheKey , path );
462
464
return "\" " + path + "\" " ; //$NON-NLS-1$ //$NON-NLS-2$
463
465
} else {
464
466
return null ; // Only one PATH so can exit early
@@ -469,13 +471,20 @@ private String findCompilerInPath(ITool tool, IConfiguration config) {
469
471
return null ;
470
472
}
471
473
472
- private static String getCompilerName (ITool tool ) {
473
- String compilerCommand = tool .getToolCommand ();
474
- String [] arguments = CommandLineUtil .argumentsToArray (compilerCommand );
474
+ private static String getCompilerName (String commandLine ) {
475
+ String [] arguments = CommandLineUtil .argumentsToArray (commandLine );
475
476
if (arguments .length == 0 ) {
476
477
return "" ; //$NON-NLS-1$
477
478
}
478
479
return arguments [0 ];
479
480
}
480
481
482
+ private static String getCompilerArgs (String commandLine ) {
483
+ String [] arguments = CommandLineUtil .argumentsToArray (commandLine );
484
+ if (arguments .length <= 1 ) {
485
+ return "" ; //$NON-NLS-1$
486
+ }
487
+ return String .join (" " , Arrays .copyOfRange (arguments , 1 , arguments .length )); //$NON-NLS-1$
488
+ }
489
+
481
490
}
0 commit comments