Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@
import org.eclipse.cdt.utils.CommandLineUtil;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IncrementalProjectBuilder;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.preferences.InstanceScope;
Expand Down Expand Up @@ -246,4 +248,47 @@ private static String escapeArgsForCompileCommand(List<String> args) {
}).collect(Collectors.joining(" "));
}

@Test
public void testCompileCommandsDirectory() throws Exception {
setWorkspace("regressions");
final IProject app = loadProject("helloworldC");
IManagedBuildInfo info = ManagedBuildManager.getBuildInfo(app);
IManagedProject mProj = info.getManagedProject();
IConfiguration cfg = mProj.getConfigurations()[0];
IToolChain toolChain = cfg.getToolChain();
IBuilder builder = toolChain.getBuilder();
setGenerateFileOptionEnabled(true);
app.build(IncrementalProjectBuilder.FULL_BUILD, null);

IFile commandsFile = app.getFile("Debug/compile_commands.json");

if (commandsFile.exists()) {
try (FileReader reader = new FileReader(commandsFile.getLocation().toFile())) {
Gson gson = new Gson();
JsonArray jsonArray = gson.fromJson(reader, JsonArray.class);
IPath buildDir = ManagedBuildManager.getBuildFullPath(cfg, builder);

IResource rc = ResourcesPlugin.getWorkspace().getRoot().findMember(buildDir);
String workspacePath = rc != null ? rc.getLocation().toOSString() : buildDir.toOSString();

java.nio.file.Path workspaceNormalized = java.nio.file.Paths.get(workspacePath).normalize();

for (JsonElement element : jsonArray) {
CompilationDatabaseInformation compileCommand = gson.fromJson(element,
CompilationDatabaseInformation.class);

String directory = compileCommand.directory();
assertNotNull("Directory field should not be null", directory);
assertFalse(directory.isEmpty(), "Directory field should not be empty");

java.nio.file.Path directoryNormalized = java.nio.file.Paths.get(directory).normalize();

assertTrue(directoryNormalized.startsWith(workspaceNormalized),
"Directory should start with workspace path.\nExpected prefix: " + workspaceNormalized
+ "\nBut got: " + directoryNormalized);
}
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.eclipse.cdt.core.settings.model.ICSourceEntry;
import org.eclipse.cdt.core.settings.model.util.CDataUtil;
import org.eclipse.cdt.managedbuilder.core.BuildException;
import org.eclipse.cdt.managedbuilder.core.IBuilder;
import org.eclipse.cdt.managedbuilder.core.IConfiguration;
import org.eclipse.cdt.managedbuilder.core.IFileInfo;
import org.eclipse.cdt.managedbuilder.core.IFolderInfo;
Expand Down Expand Up @@ -164,6 +165,10 @@ private List<CompilationDatabaseInformation> populateObjList(IProject project, I
List<CompilationDatabaseInformation> objList = new ArrayList<>();
for (IResource resource : getFileList()) {

IPath buildDir = getFullBuildDirectory(config);
if (buildDir == null) {
buildDir = resource.getProject().getLocation();
}
IPath moduleRelativePath = resource.getParent().getProjectRelativePath();
String relativePath = moduleRelativePath.toString();
IFolder folder = project.getFolder(config.getName());
Expand Down Expand Up @@ -249,8 +254,8 @@ private List<CompilationDatabaseInformation> populateObjList(IProject project, I
new FileContextData(sourceLocation, outputLocation, null, tool));

}
objList.add(new CompilationDatabaseInformation(project.getLocation().toString(),
resolvedOptionFileContents, resource.getLocation().toString()));
objList.add(new CompilationDatabaseInformation(buildDir.toString(), resolvedOptionFileContents,
resource.getLocation().toString()));
}

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

public static IPath getFullBuildDirectory(IConfiguration config) {
if (config == null) {
throw new IllegalArgumentException("Configuration cannot be null"); //$NON-NLS-1$
}

IBuilder builder = config.getBuilder();
IPath buildDir = ManagedBuildManager.getBuildFullPath(config, builder);
IPath fullBuildPath = null;

if (buildDir != null) {
IResource rc = ResourcesPlugin.getWorkspace().getRoot().findMember(buildDir);

if (rc != null && rc.getLocation() != null) {
fullBuildPath = rc.getLocation();
} else if (buildDir.toFile().isAbsolute()) {
fullBuildPath = buildDir;
}
}

return fullBuildPath;
}

}
Loading