Skip to content

Commit 53d79e8

Browse files
committed
CMake built-ins detection successful when compile_commands.json compiler
path is relative Under certain conditions*, the compiler param in the command field of the compile_commands.json is a relative path rather than absolute. When this happens, the built-ins detection was not successful and the following exception was thrown: !ENTRY org.eclipse.cdt.core 4 0 2025-02-23 20:32:10.752 !MESSAGE Error: Cannot run program "gcc": Launching failed !STACK 0 java.io.IOException: Cannot run program "gcc": Launching failed at org.eclipse.cdt.utils.spawner.Spawner.exec(Spawner.java:450) at org.eclipse.cdt.utils.spawner.Spawner.<init>(Spawner.java:147) at org.eclipse.cdt.utils.spawner.Spawner.<init>(Spawner.java:134) at org.eclipse.cdt.utils.spawner.ProcessFactory$Builder.start(ProcessFactory.java:273) at org.eclipse.cdt.utils.spawner.ProcessFactory.exec(ProcessFactory.java:366) at org.eclipse.cdt.core.CommandLauncher.execute(CommandLauncher.java:189) at org.eclipse.cdt.jsoncdb.core.internal.builtins.CompilerBuiltinsDetector.detectBuiltins(CompilerBuiltinsDetector.java:111) at org.eclipse.cdt.jsoncdb.core.CompileCommandsJsonParser.detectBuiltins(CompileCommandsJsonParser.java:290) at org.eclipse.cdt.jsoncdb.core.CompileCommandsJsonParser.processJsonFile(CompileCommandsJsonParser.java:193) at org.eclipse.cdt.jsoncdb.core.CompileCommandsJsonParser.parse(CompileCommandsJsonParser.java:455) at org.eclipse.cdt.cmake.core.CMakeBuildConfiguration.processCompileCommandsFile(CMakeBuildConfiguration.java:361) at org.eclipse.cdt.cmake.core.CMakeBuildConfiguration.build(CMakeBuildConfiguration.java:241) This meant that source file includes were not indexed and could not be opened using Open Declaration (F3) and info markers of the following type appeared in the Problems view: gcc -E -P -dM -Wp,-v "...extCmakegcc\\build\\cmake.run.win32.x86_64.Local\\detect_compiler_builtins.c" Cannot run program "gcc": Launching failed Error: Program "gcc" not found in PATH PATH=[...] extCmakegcc Unknown Compiler Builtins Detector Problem gcc -E -P -dM -Wp,-v "...extCmakegcc\\build\\cmake.run.win32.x86_64.Local\\detect_compiler_builtins.c" Cannot run program "gcc": Launching failed This patch fixes the environment issue for Core Build projects, by calling ICBuildConfiguration.setBuildEnvironment(Map<String, String>) as part of the built-ins detection setup, thereby supporting absolute and relative compiler paths. Addresses Issue: CDT CMake Improvements eclipse-cdt#1000, IDE-82683-REQ-011 Source code navigation and Built-ins detection *: CMake produces relative compiler path When the CMAKE_<LANG>_COMPILER variable (eg CMAKE_C_COMPILER) is set in the CMakeLists.txt *after* the project() or language commands, it causes a relative path to be used. For example, in the CMakeLists.txt below, gcc is set after project() command: cmake_minimum_required(VERSION 3.10) project (track2) set(CMAKE_C_COMPILER gcc) add_executable(${PROJECT_NAME} track2.c) The above script creates a relative compiler path in compile_commands.json: "command": "gcc -O3 -DNDEBUG -o ... Normally the CMAKE_C_COMPILER variable should be set before the project() comannd. "command": "C:\\msys64\\mingw64\\bin\\c++.exe -IC...
1 parent 5135c9f commit 53d79e8

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

jsoncdb/org.eclipse.cdt.jsoncdb.core/src/org/eclipse/cdt/jsoncdb/core/internal/builtins/CompilerBuiltinsDetector.java

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import org.eclipse.cdt.core.CCorePlugin;
2222
import org.eclipse.cdt.core.ConsoleOutputStream;
2323
import org.eclipse.cdt.core.ICommandLauncher;
24+
import org.eclipse.cdt.core.build.ICBuildConfiguration;
25+
import org.eclipse.cdt.core.build.ICBuildConfigurationManager;
2426
import org.eclipse.cdt.core.resources.IConsole;
2527
import org.eclipse.cdt.jsoncdb.core.IParserPreferences;
2628
import org.eclipse.cdt.jsoncdb.core.IParserPreferencesAccess;
@@ -190,13 +192,28 @@ private String[] getEnvp(IProject project) {
190192
// e.g. 'C:\msys64\mingw64\bin' must be part of the PATH environment variable. That's why we need PATH here:
191193
// Fixes CDT #407
192194
try {
193-
final String path = "PATH"; //$NON-NLS-1$
194-
var variables = CCorePlugin.getDefault().getBuildEnvironmentManager()
195-
.getVariables(project.getActiveBuildConfig(), true);
196-
for (var variable : variables) {
197-
if (path.equalsIgnoreCase(variable.getName())) {
198-
map.put(path, variable.getValue());
199-
break;
195+
final ICBuildConfiguration cBuildConfiguration;
196+
var active = project.getActiveBuildConfig();
197+
if (active != null) {
198+
var manager = CCorePlugin.getService(ICBuildConfigurationManager.class);
199+
cBuildConfiguration = manager.getBuildConfiguration(active);
200+
} else {
201+
cBuildConfiguration = null;
202+
}
203+
204+
if (cBuildConfiguration != null) {
205+
// If this project is Core Build, use its specialisation of getBuildEnvironmentManager
206+
// to get the environment.
207+
cBuildConfiguration.setBuildEnvironment(map);
208+
} else {
209+
final String path = "PATH"; //$NON-NLS-1$
210+
var variables = CCorePlugin.getDefault().getBuildEnvironmentManager()
211+
.getVariables(project.getActiveBuildConfig(), true);
212+
for (var variable : variables) {
213+
if (path.equalsIgnoreCase(variable.getName())) {
214+
map.put(path, variable.getValue());
215+
break;
216+
}
200217
}
201218
}
202219
} catch (CoreException e) {

0 commit comments

Comments
 (0)