-
Notifications
You must be signed in to change notification settings - Fork 215
Added feature to generate compile_commands.json file #692
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jonahgraham
merged 2 commits into
eclipse-cdt:main
from
alicetrifu:compilationDatabaseGenerator
May 16, 2024
+2,141
−5
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file added
BIN
+2.6 KB
...clipse.cdt.managedbuilder.core.tests/resources/builderTests/regressions/helloworldCPP.zip
Binary file not shown.
170 changes: 170 additions & 0 deletions
170
...ts/tests/org/eclipse/cdt/managedbuilder/core/tests/CompilationDatabaseGenerationTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2019, 2020 Marc-Andre Laperle. | ||
* | ||
* This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License 2.0 | ||
* which accompanies this distribution, and is available at | ||
* https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*******************************************************************************/ | ||
package org.eclipse.cdt.managedbuilder.core.tests; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.io.FileReader; | ||
|
||
import org.eclipse.cdt.managedbuilder.core.jsoncdb.CompilationDatabaseInformation; | ||
import org.eclipse.cdt.managedbuilder.internal.core.CommonBuilder; | ||
import org.eclipse.cdt.managedbuilder.testplugin.AbstractBuilderTest; | ||
import org.eclipse.cdt.managedbuilder.testplugin.ManagedBuildTestHelper; | ||
import org.eclipse.core.resources.IFile; | ||
import org.eclipse.core.resources.IProject; | ||
import org.eclipse.core.resources.IncrementalProjectBuilder; | ||
import org.eclipse.core.runtime.preferences.InstanceScope; | ||
import org.eclipse.jface.preference.IPreferenceStore; | ||
import org.eclipse.ui.preferences.ScopedPreferenceStore; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.JsonArray; | ||
import com.google.gson.JsonElement; | ||
|
||
public class CompilationDatabaseGenerationTest extends AbstractBuilderTest { | ||
|
||
/** | ||
* Tests generation of compile_commands.json in "build" folder | ||
*/ | ||
@Test | ||
public void testCompilationDatabaseGeneration() throws Exception { | ||
setWorkspace("regressions"); | ||
final IProject app = loadProject("helloworldC"); | ||
setGenerateFileOptionEnabled(true); | ||
app.build(IncrementalProjectBuilder.FULL_BUILD, null); | ||
IFile compilationDatabase = app.getFile("Debug/compile_commands.json"); | ||
assertTrue(compilationDatabase.exists()); | ||
} | ||
|
||
/** | ||
* Tests format for compile_commands.json. JSON array is expected, containing an element for the c file | ||
*/ | ||
@Test | ||
public void testJsonFormat() throws Exception { | ||
setWorkspace("regressions"); | ||
final IProject app = loadProject("helloworldC"); | ||
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); | ||
for (JsonElement element : jsonArray) { | ||
CompilationDatabaseInformation compileCommand = gson.fromJson(element, | ||
CompilationDatabaseInformation.class); | ||
|
||
assertTrue(compileCommand.directory() != null && !compileCommand.directory().isEmpty()); | ||
assertTrue(compileCommand.command() != null && !compileCommand.command().isEmpty()); | ||
assertTrue(compileCommand.file() != null && !compileCommand.file().isEmpty()); | ||
assertTrue(compileCommand.file().endsWith("src/helloworldC.c")); | ||
} | ||
|
||
} | ||
|
||
} | ||
} | ||
|
||
/** | ||
* Test that compile_commands.json is correctly generated when more than one .c file is present as a source file | ||
*/ | ||
@Test | ||
public void testMultipleFiles() throws Exception { | ||
setWorkspace("regressions"); | ||
final IProject app = loadProject("helloworldC"); | ||
IFile aFile = ManagedBuildTestHelper.createFile(app, "src/newFile.c"); | ||
setGenerateFileOptionEnabled(true); | ||
app.build(IncrementalProjectBuilder.FULL_BUILD, null); | ||
IFile commandsFile = app.getFile("Debug/compile_commands.json"); | ||
int numberOfElementsFound = 0; | ||
boolean helloworldCIsPresent = false; | ||
boolean newFileIsPresent = false; | ||
try (FileReader reader = new FileReader(commandsFile.getLocation().toFile())) { | ||
Gson gson = new Gson(); | ||
JsonArray jsonArray = gson.fromJson(reader, JsonArray.class); | ||
System.out.println(jsonArray); | ||
for (JsonElement element : jsonArray) { | ||
CompilationDatabaseInformation compileCommand = gson.fromJson(element, | ||
CompilationDatabaseInformation.class); | ||
numberOfElementsFound++; | ||
if (compileCommand.file().endsWith("helloworldC.c")) { | ||
helloworldCIsPresent = true; | ||
} | ||
if (compileCommand.file().endsWith("newFile.c")) { | ||
newFileIsPresent = true; | ||
} | ||
} | ||
assertEquals(2, numberOfElementsFound); | ||
assertTrue(helloworldCIsPresent); | ||
assertTrue(newFileIsPresent); | ||
} | ||
} | ||
|
||
/** | ||
* Tests that cpp files are handled by compile_commands.json file generator | ||
*/ | ||
@Test | ||
public void isCPPFileAllowed() throws Exception { | ||
setWorkspace("regressions"); | ||
final IProject app = loadProject("helloworldCPP"); | ||
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); | ||
for (JsonElement element : jsonArray) { | ||
CompilationDatabaseInformation compileCommand = gson.fromJson(element, | ||
CompilationDatabaseInformation.class); | ||
|
||
assertTrue(compileCommand.directory() != null && !compileCommand.directory().isEmpty()); | ||
assertTrue(compileCommand.command() != null && !compileCommand.command().isEmpty()); | ||
assertTrue(compileCommand.file() != null && !compileCommand.file().isEmpty()); | ||
assertTrue(compileCommand.file().endsWith("src/helloworldCPP.cpp")); | ||
} | ||
|
||
} | ||
} | ||
} | ||
|
||
/** | ||
* Tests that compilation database is not generated when feature is disabled | ||
*/ | ||
@Test | ||
public void testCompilationDatabaseGenerationNotEnabled() throws Exception { | ||
setWorkspace("regressions"); | ||
final IProject app = loadProject("helloworldC"); | ||
setGenerateFileOptionEnabled(false); | ||
app.build(IncrementalProjectBuilder.FULL_BUILD, null); | ||
IFile compilationDatabase = app.getFile("Debug/compile_commands.json"); | ||
assertFalse(compilationDatabase.exists()); | ||
} | ||
|
||
private static void setGenerateFileOptionEnabled(boolean value) { | ||
IPreferenceStore preferenceStore = new ScopedPreferenceStore(InstanceScope.INSTANCE, | ||
"org.eclipse.cdt.managedbuilder.ui"); | ||
preferenceStore.setValue(CommonBuilder.COMPILATION_DATABASE_ENABLEMENT, value); | ||
} | ||
|
||
@AfterEach | ||
public void restoreDefaultForGenerateFile() { | ||
IPreferenceStore preferenceStore = new ScopedPreferenceStore(InstanceScope.INSTANCE, | ||
"org.eclipse.cdt.managedbuilder.ui"); | ||
preferenceStore.setToDefault(CommonBuilder.COMPILATION_DATABASE_ENABLEMENT); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
build/org.eclipse.cdt.managedbuilder.core/schema/compilationDatabaseContributor.exsd
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?xml version='1.0' encoding='UTF-8'?> | ||
<!-- Schema file written by PDE --> | ||
<schema targetNamespace="org.eclipse.cdt.managedbuilder.core" xmlns="http://www.w3.org/2001/XMLSchema"> | ||
<annotation> | ||
<appInfo> | ||
<meta.schema plugin="org.eclipse.cdt.managedbuilder.core" id="compilationDatabaseContributor" name="Compilation Database Contributor"/> | ||
</appInfo> | ||
<documentation> | ||
This extension point allows to add specific information to compile_commands.json file. | ||
</documentation> | ||
</annotation> | ||
|
||
<element name="extension"> | ||
<annotation> | ||
<appInfo> | ||
<meta.element /> | ||
</appInfo> | ||
</annotation> | ||
<complexType> | ||
<sequence minOccurs="1" maxOccurs="unbounded"> | ||
<choice> | ||
<element ref="compilationDatabase"/> | ||
</choice> | ||
</sequence> | ||
<attribute name="point" type="string" use="required"> | ||
<annotation> | ||
<documentation> | ||
|
||
</documentation> | ||
</annotation> | ||
</attribute> | ||
<attribute name="id" type="string"> | ||
<annotation> | ||
<documentation> | ||
|
||
</documentation> | ||
</annotation> | ||
</attribute> | ||
<attribute name="name" type="string"> | ||
<annotation> | ||
<documentation> | ||
|
||
</documentation> | ||
<appInfo> | ||
<meta.attribute translatable="true"/> | ||
</appInfo> | ||
</annotation> | ||
</attribute> | ||
</complexType> | ||
</element> | ||
|
||
<element name="compilationDatabase"> | ||
<annotation> | ||
<documentation> | ||
Compilation Database for C/C++ projects | ||
</documentation> | ||
</annotation> | ||
<complexType> | ||
<attribute name="toolchainID" type="string" use="required"> | ||
<annotation> | ||
<documentation> | ||
Toolchain ID | ||
</documentation> | ||
</annotation> | ||
</attribute> | ||
<attribute name="runner" type="string" use="required"> | ||
<annotation> | ||
<documentation> | ||
|
||
</documentation> | ||
<appInfo> | ||
<meta.attribute kind="java" basedOn=":org.eclipse.cdt.managedbuilder.core.jsoncdb.ICompilationDatabaseContributor"/> | ||
</appInfo> | ||
</annotation> | ||
</attribute> | ||
</complexType> | ||
</element> | ||
|
||
|
||
|
||
|
||
|
||
|
||
</schema> |
22 changes: 22 additions & 0 deletions
22
....core/src/org/eclipse/cdt/managedbuilder/core/jsoncdb/CompilationDatabaseInformation.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/******************************************************************************** | ||
* Copyright (c) 2023, 2024 Renesas Electronics Corp. and others. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
********************************************************************************/ | ||
package org.eclipse.cdt.managedbuilder.core.jsoncdb; | ||
|
||
/** | ||
* The compilation database array of “command objects” members to be used for | ||
* the extension point | ||
* directory: The working directory of the compilation. | ||
* command: The compile command. file: The main translation unit source | ||
* processed by this compilation step. | ||
* @since 9.7 | ||
*/ | ||
|
||
public record CompilationDatabaseInformation(String directory, String command, String file) { | ||
} |
33 changes: 33 additions & 0 deletions
33
...core/src/org/eclipse/cdt/managedbuilder/core/jsoncdb/ICompilationDatabaseContributor.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/******************************************************************************** | ||
* Copyright (c) 2023, 2024 Renesas Electronics Europe. and others. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
********************************************************************************/ | ||
package org.eclipse.cdt.managedbuilder.core.jsoncdb; | ||
|
||
import java.util.List; | ||
|
||
import org.eclipse.cdt.managedbuilder.core.IConfiguration; | ||
import org.eclipse.jdt.annotation.NonNull; | ||
|
||
/** | ||
* @since 9.7 | ||
*/ | ||
public interface ICompilationDatabaseContributor { | ||
|
||
/** | ||
* @param config | ||
* Adds a new list of files to the compilation database | ||
* Implementors should provide concrete implementations of this | ||
* interface. IConfiguration will be taken as input, accessing the project and will generate a list of | ||
* additional files to be added to compile_commands.json | ||
* @return A non-null list of files that will be added to compilation database | ||
*/ | ||
@NonNull | ||
public List<CompilationDatabaseInformation> getAdditionalFiles(@NonNull IConfiguration config); | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.