Skip to content

Commit d4bace4

Browse files
committed
Compiler path in compile_commands.json must be absolute
Added code to create the absolute path of the compiler on compile_commands.json file
1 parent 2fbb421 commit d4bace4

File tree

2 files changed

+53
-4
lines changed

2 files changed

+53
-4
lines changed

build/org.eclipse.cdt.managedbuilder.core/META-INF/MANIFEST.MF

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Manifest-Version: 1.0
22
Bundle-ManifestVersion: 2
33
Bundle-Name: %pluginName
44
Bundle-SymbolicName: org.eclipse.cdt.managedbuilder.core; singleton:=true
5-
Bundle-Version: 9.7.0.qualifier
5+
Bundle-Version: 9.7.100.qualifier
66
Bundle-Activator: org.eclipse.cdt.managedbuilder.core.ManagedBuilderCorePlugin
77
Bundle-Vendor: %providerName
88
Bundle-Localization: plugin

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

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,19 @@
1010
package org.eclipse.cdt.managedbuilder.internal.core.jsoncdb.generator;
1111

1212
import java.io.ByteArrayInputStream;
13+
import java.io.File;
1314
import java.io.InputStream;
1415
import java.nio.charset.StandardCharsets;
1516
import java.util.ArrayList;
1617
import java.util.Collection;
18+
import java.util.HashMap;
1719
import java.util.LinkedHashSet;
1820
import java.util.List;
21+
import java.util.Map;
1922

23+
import org.eclipse.cdt.core.CCorePlugin;
24+
import org.eclipse.cdt.core.envvar.IEnvironmentVariable;
25+
import org.eclipse.cdt.core.settings.model.ICConfigurationDescription;
2026
import org.eclipse.cdt.core.settings.model.ICSourceEntry;
2127
import org.eclipse.cdt.core.settings.model.util.CDataUtil;
2228
import org.eclipse.cdt.managedbuilder.core.BuildException;
@@ -34,6 +40,8 @@
3440
import org.eclipse.cdt.managedbuilder.internal.macros.FileContextData;
3541
import org.eclipse.cdt.managedbuilder.macros.BuildMacroException;
3642
import org.eclipse.cdt.managedbuilder.macros.IBuildMacroProvider;
43+
import org.eclipse.cdt.utils.CommandLineUtil;
44+
import org.eclipse.cdt.utils.PathUtil;
3745
import org.eclipse.core.resources.IFile;
3846
import org.eclipse.core.resources.IFolder;
3947
import org.eclipse.core.resources.IProject;
@@ -58,7 +66,7 @@ public final class CompilationDatabaseGenerator {
5866

5967
private static final String CDB_FILENAME = "compile_commands.json"; //$NON-NLS-1$
6068
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<>();
6270
private IProject project;
6371
private IConfiguration configuration;
6472
private ICSourceEntry[] srcEntries;
@@ -217,8 +225,12 @@ private List<CompilationDatabaseInformation> populateObjList(IProject project, I
217225
outputLocation + "", inputStrings, sourceLocation, outputLocation); //$NON-NLS-1$
218226

219227
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$
222234
new FileContextData(sourceLocation, outputLocation, null, tool));
223235

224236
objList.add(new CompilationDatabaseInformation(project.getLocation().toString(),
@@ -414,4 +426,41 @@ public boolean visit(IResourceProxy proxy) throws CoreException {
414426

415427
}
416428

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

0 commit comments

Comments
 (0)