Skip to content

Commit 898cb90

Browse files
authored
Changed directory field to reflect PWD. (#1372)
Directory field in JSON is not the working build directory. Fixes #1371
1 parent 444c931 commit 898cb90

File tree

2 files changed

+74
-2
lines changed

2 files changed

+74
-2
lines changed

build/org.eclipse.cdt.managedbuilder.core.tests/tests/org/eclipse/cdt/managedbuilder/core/tests/CompilationDatabaseGenerationTest.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@
3333
import org.eclipse.cdt.utils.CommandLineUtil;
3434
import org.eclipse.core.resources.IFile;
3535
import org.eclipse.core.resources.IProject;
36+
import org.eclipse.core.resources.IResource;
3637
import org.eclipse.core.resources.IncrementalProjectBuilder;
38+
import org.eclipse.core.resources.ResourcesPlugin;
3739
import org.eclipse.core.runtime.IPath;
3840
import org.eclipse.core.runtime.Path;
3941
import org.eclipse.core.runtime.preferences.InstanceScope;
@@ -246,4 +248,47 @@ private static String escapeArgsForCompileCommand(List<String> args) {
246248
}).collect(Collectors.joining(" "));
247249
}
248250

251+
@Test
252+
public void testCompileCommandsDirectory() throws Exception {
253+
setWorkspace("regressions");
254+
final IProject app = loadProject("helloworldC");
255+
IManagedBuildInfo info = ManagedBuildManager.getBuildInfo(app);
256+
IManagedProject mProj = info.getManagedProject();
257+
IConfiguration cfg = mProj.getConfigurations()[0];
258+
IToolChain toolChain = cfg.getToolChain();
259+
IBuilder builder = toolChain.getBuilder();
260+
setGenerateFileOptionEnabled(true);
261+
app.build(IncrementalProjectBuilder.FULL_BUILD, null);
262+
263+
IFile commandsFile = app.getFile("Debug/compile_commands.json");
264+
265+
if (commandsFile.exists()) {
266+
try (FileReader reader = new FileReader(commandsFile.getLocation().toFile())) {
267+
Gson gson = new Gson();
268+
JsonArray jsonArray = gson.fromJson(reader, JsonArray.class);
269+
IPath buildDir = ManagedBuildManager.getBuildFullPath(cfg, builder);
270+
271+
IResource rc = ResourcesPlugin.getWorkspace().getRoot().findMember(buildDir);
272+
String workspacePath = rc != null ? rc.getLocation().toOSString() : buildDir.toOSString();
273+
274+
java.nio.file.Path workspaceNormalized = java.nio.file.Paths.get(workspacePath).normalize();
275+
276+
for (JsonElement element : jsonArray) {
277+
CompilationDatabaseInformation compileCommand = gson.fromJson(element,
278+
CompilationDatabaseInformation.class);
279+
280+
String directory = compileCommand.directory();
281+
assertNotNull("Directory field should not be null", directory);
282+
assertFalse(directory.isEmpty(), "Directory field should not be empty");
283+
284+
java.nio.file.Path directoryNormalized = java.nio.file.Paths.get(directory).normalize();
285+
286+
assertTrue(directoryNormalized.startsWith(workspaceNormalized),
287+
"Directory should start with workspace path.\nExpected prefix: " + workspaceNormalized
288+
+ "\nBut got: " + directoryNormalized);
289+
}
290+
}
291+
}
292+
}
293+
249294
}

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

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.eclipse.cdt.core.settings.model.ICSourceEntry;
2929
import org.eclipse.cdt.core.settings.model.util.CDataUtil;
3030
import org.eclipse.cdt.managedbuilder.core.BuildException;
31+
import org.eclipse.cdt.managedbuilder.core.IBuilder;
3132
import org.eclipse.cdt.managedbuilder.core.IConfiguration;
3233
import org.eclipse.cdt.managedbuilder.core.IFileInfo;
3334
import org.eclipse.cdt.managedbuilder.core.IFolderInfo;
@@ -164,6 +165,10 @@ private List<CompilationDatabaseInformation> populateObjList(IProject project, I
164165
List<CompilationDatabaseInformation> objList = new ArrayList<>();
165166
for (IResource resource : getFileList()) {
166167

168+
IPath buildDir = getFullBuildDirectory(config);
169+
if (buildDir == null) {
170+
buildDir = resource.getProject().getLocation();
171+
}
167172
IPath moduleRelativePath = resource.getParent().getProjectRelativePath();
168173
String relativePath = moduleRelativePath.toString();
169174
IFolder folder = project.getFolder(config.getName());
@@ -249,8 +254,8 @@ private List<CompilationDatabaseInformation> populateObjList(IProject project, I
249254
new FileContextData(sourceLocation, outputLocation, null, tool));
250255

251256
}
252-
objList.add(new CompilationDatabaseInformation(project.getLocation().toString(),
253-
resolvedOptionFileContents, resource.getLocation().toString()));
257+
objList.add(new CompilationDatabaseInformation(buildDir.toString(), resolvedOptionFileContents,
258+
resource.getLocation().toString()));
254259
}
255260

256261
}
@@ -501,4 +506,26 @@ private static String escArgsForCompileCommand(final List<String> args) {
501506
}).collect(Collectors.joining(" ")); //$NON-NLS-1$
502507
}
503508

509+
public static IPath getFullBuildDirectory(IConfiguration config) {
510+
if (config == null) {
511+
throw new IllegalArgumentException("Configuration cannot be null"); //$NON-NLS-1$
512+
}
513+
514+
IBuilder builder = config.getBuilder();
515+
IPath buildDir = ManagedBuildManager.getBuildFullPath(config, builder);
516+
IPath fullBuildPath = null;
517+
518+
if (buildDir != null) {
519+
IResource rc = ResourcesPlugin.getWorkspace().getRoot().findMember(buildDir);
520+
521+
if (rc != null && rc.getLocation() != null) {
522+
fullBuildPath = rc.getLocation();
523+
} else if (buildDir.toFile().isAbsolute()) {
524+
fullBuildPath = buildDir;
525+
}
526+
}
527+
528+
return fullBuildPath;
529+
}
530+
504531
}

0 commit comments

Comments
 (0)