Skip to content

Commit 2c4aa3f

Browse files
committed
Fix for the compiler path in compile_commands.json file
Compiler path was wrongly created when anything was set on the "Prefix" option from "Cross Settings". The code needs to check if anything is set there before creating the default path for the compiler.
1 parent 04105c2 commit 2c4aa3f

File tree

1 file changed

+24
-15
lines changed

1 file changed

+24
-15
lines changed

build/org.eclipse.cdt.managedbuilder.core/src/org/eclipse/cdt/managedbuilder/internal/core/jsoncdb/generator/CompilationDatabaseGenerator.java

+24-15
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import java.io.InputStream;
1515
import java.nio.charset.StandardCharsets;
1616
import java.util.ArrayList;
17+
import java.util.Arrays;
1718
import java.util.Collection;
1819
import java.util.HashMap;
1920
import java.util.LinkedHashSet;
@@ -70,7 +71,7 @@ public final class CompilationDatabaseGenerator {
7071
* Checked on each build
7172
* Used before we look up the environment
7273
*/
73-
private Map<ITool, String> toolMap = new HashMap<>();
74+
private Map<String, String> toolMap = new HashMap<>();
7475
private IProject project;
7576
private IConfiguration configuration;
7677
private ICSourceEntry[] srcEntries;
@@ -229,18 +230,19 @@ private List<CompilationDatabaseInformation> populateObjList(IProject project, I
229230
outputLocation + "", inputStrings, sourceLocation, outputLocation); //$NON-NLS-1$
230231

231232
IBuildMacroProvider provider = ManagedBuildManager.getBuildMacroProvider();
232-
String compilerName = CompilationDatabaseGenerator.getCompilerName(tool);
233233
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);
236237
String resolvedOptionFileContents;
237238
if (compilerPath != null && !compilerPath.isEmpty()) {
238-
resolvedOptionFileContents = provider.resolveValueToMakefileFormat(compilerPath + " " + commandLine, //$NON-NLS-1$
239+
resolvedOptionFileContents = provider.resolveValueToMakefileFormat(
240+
compilerPath + " " + compilerArguments, //$NON-NLS-1$
239241
"", " ", //$NON-NLS-1$//$NON-NLS-2$
240242
IBuildMacroProvider.CONTEXT_FILE,
241243
new FileContextData(sourceLocation, outputLocation, null, tool));
242244
} else {
243-
resolvedOptionFileContents = provider.resolveValueToMakefileFormat(commandLine, "", " ", //$NON-NLS-1$//$NON-NLS-2$
245+
resolvedOptionFileContents = provider.resolveValueToMakefileFormat(compilerArguments, "", " ", //$NON-NLS-1$//$NON-NLS-2$
244246
IBuildMacroProvider.CONTEXT_FILE,
245247
new FileContextData(sourceLocation, outputLocation, null, tool));
246248

@@ -438,15 +440,15 @@ public boolean visit(IResourceProxy proxy) throws CoreException {
438440

439441
}
440442

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$
444447
}
445-
String compilerName = CompilationDatabaseGenerator.getCompilerName(tool);
446448
File pathToCompiler = new File(compilerName);
447449

448450
if (pathToCompiler.isAbsolute()) {
449-
toolMap.put(tool, compilerName);
451+
toolMap.put(cacheKey, compilerName);
450452
return "\"" + compilerName + "\""; //$NON-NLS-1$ //$NON-NLS-2$
451453
}
452454
ICConfigurationDescription cfg = ManagedBuildManager.getDescriptionForConfiguration(config);
@@ -458,7 +460,7 @@ private String findCompilerInPath(ITool tool, IConfiguration config) {
458460
IPath resolvedPath = PathUtil.findProgramLocation(compilerName, variable.getValue());
459461
if (resolvedPath != null) {
460462
String path = resolvedPath.toString();
461-
toolMap.put(tool, path);
463+
toolMap.put(cacheKey, path);
462464
return "\"" + path + "\""; //$NON-NLS-1$ //$NON-NLS-2$
463465
} else {
464466
return null; // Only one PATH so can exit early
@@ -469,13 +471,20 @@ private String findCompilerInPath(ITool tool, IConfiguration config) {
469471
return null;
470472
}
471473

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);
475476
if (arguments.length == 0) {
476477
return ""; //$NON-NLS-1$
477478
}
478479
return arguments[0];
479480
}
480481

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+
481490
}

0 commit comments

Comments
 (0)