Skip to content

Commit 502ca15

Browse files
alicetrifujonahgraham
authored andcommitted
Added feature to generate compile_commands.json file
Includes: - Created a new extension point to provide the opportunity to incorporate additional information into the file - New tests for the generator - Preference page to enable/disable generator Fixes #689
1 parent 271041b commit 502ca15

File tree

44 files changed

+2142
-5
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+2142
-5
lines changed

build/org.eclipse.cdt.managedbuilder.core.tests/resources/builderTests/regressions/README.txt

+4
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,9 @@ helloworldC.zip:
33
Basic standard Linux GCC C HelloWorld ManagedBuild Project template.
44
Contains Debug + Release configurations.
55

6+
helloworldCPP.zip:
7+
Minimal standard Linux GCC C++ HelloWorld ManagedBuild Project template
8+
Contains Debug + Release configurations.
9+
610
bug_335476.zip:
711
Test for noticing environment variable changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2019, 2020 Marc-Andre Laperle.
3+
*
4+
* This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License 2.0
6+
* which accompanies this distribution, and is available at
7+
* https://www.eclipse.org/legal/epl-2.0/
8+
*
9+
* SPDX-License-Identifier: EPL-2.0
10+
*******************************************************************************/
11+
package org.eclipse.cdt.managedbuilder.core.tests;
12+
13+
import static org.junit.jupiter.api.Assertions.assertEquals;
14+
import static org.junit.jupiter.api.Assertions.assertFalse;
15+
import static org.junit.jupiter.api.Assertions.assertTrue;
16+
17+
import java.io.FileReader;
18+
19+
import org.eclipse.cdt.managedbuilder.core.jsoncdb.CompilationDatabaseInformation;
20+
import org.eclipse.cdt.managedbuilder.internal.core.CommonBuilder;
21+
import org.eclipse.cdt.managedbuilder.testplugin.AbstractBuilderTest;
22+
import org.eclipse.cdt.managedbuilder.testplugin.ManagedBuildTestHelper;
23+
import org.eclipse.core.resources.IFile;
24+
import org.eclipse.core.resources.IProject;
25+
import org.eclipse.core.resources.IncrementalProjectBuilder;
26+
import org.eclipse.core.runtime.preferences.InstanceScope;
27+
import org.eclipse.jface.preference.IPreferenceStore;
28+
import org.eclipse.ui.preferences.ScopedPreferenceStore;
29+
import org.junit.jupiter.api.AfterEach;
30+
import org.junit.jupiter.api.Test;
31+
32+
import com.google.gson.Gson;
33+
import com.google.gson.JsonArray;
34+
import com.google.gson.JsonElement;
35+
36+
public class CompilationDatabaseGenerationTest extends AbstractBuilderTest {
37+
38+
/**
39+
* Tests generation of compile_commands.json in "build" folder
40+
*/
41+
@Test
42+
public void testCompilationDatabaseGeneration() throws Exception {
43+
setWorkspace("regressions");
44+
final IProject app = loadProject("helloworldC");
45+
setGenerateFileOptionEnabled(true);
46+
app.build(IncrementalProjectBuilder.FULL_BUILD, null);
47+
IFile compilationDatabase = app.getFile("build/compile_commands.json");
48+
assertTrue(compilationDatabase.exists());
49+
}
50+
51+
/**
52+
* Tests format for compile_commands.json. JSON array is expected, containing an element for the c file
53+
*/
54+
@Test
55+
public void testJsonFormat() throws Exception {
56+
setWorkspace("regressions");
57+
final IProject app = loadProject("helloworldC");
58+
setGenerateFileOptionEnabled(true);
59+
app.build(IncrementalProjectBuilder.FULL_BUILD, null);
60+
IFile commandsFile = app.getFile("build/compile_commands.json");
61+
if (commandsFile.exists()) {
62+
63+
try (FileReader reader = new FileReader(commandsFile.getLocation().toFile())) {
64+
Gson gson = new Gson();
65+
JsonArray jsonArray = gson.fromJson(reader, JsonArray.class);
66+
for (JsonElement element : jsonArray) {
67+
CompilationDatabaseInformation compileCommand = gson.fromJson(element,
68+
CompilationDatabaseInformation.class);
69+
70+
assertTrue(compileCommand.directory() != null && !compileCommand.directory().isEmpty());
71+
assertTrue(compileCommand.command() != null && !compileCommand.command().isEmpty());
72+
assertTrue(compileCommand.file() != null && !compileCommand.file().isEmpty());
73+
assertTrue(compileCommand.file().endsWith("src/helloworldC.c"));
74+
}
75+
76+
}
77+
78+
}
79+
}
80+
81+
/**
82+
* Test that compile_commands.json is correctly generated when more than one .c file is present as a source file
83+
*/
84+
@Test
85+
public void testMultipleFiles() throws Exception {
86+
setWorkspace("regressions");
87+
final IProject app = loadProject("helloworldC");
88+
IFile aFile = ManagedBuildTestHelper.createFile(app, "src/newFile.c");
89+
setGenerateFileOptionEnabled(true);
90+
app.build(IncrementalProjectBuilder.FULL_BUILD, null);
91+
IFile commandsFile = app.getFile("build/compile_commands.json");
92+
int numberOfElementsFound = 0;
93+
boolean helloworldCIsPresent = false;
94+
boolean newFileIsPresent = false;
95+
try (FileReader reader = new FileReader(commandsFile.getLocation().toFile())) {
96+
Gson gson = new Gson();
97+
JsonArray jsonArray = gson.fromJson(reader, JsonArray.class);
98+
System.out.println(jsonArray);
99+
for (JsonElement element : jsonArray) {
100+
CompilationDatabaseInformation compileCommand = gson.fromJson(element,
101+
CompilationDatabaseInformation.class);
102+
numberOfElementsFound++;
103+
if (compileCommand.file().endsWith("helloworldC.c")) {
104+
helloworldCIsPresent = true;
105+
}
106+
if (compileCommand.file().endsWith("newFile.c")) {
107+
newFileIsPresent = true;
108+
}
109+
}
110+
assertEquals(2, numberOfElementsFound);
111+
assertTrue(helloworldCIsPresent);
112+
assertTrue(newFileIsPresent);
113+
}
114+
}
115+
116+
/**
117+
* Tests that cpp files are handled by compile_commands.json file generator
118+
*/
119+
@Test
120+
public void isCPPFileAllowed() throws Exception {
121+
setWorkspace("regressions");
122+
final IProject app = loadProject("helloworldCPP");
123+
setGenerateFileOptionEnabled(true);
124+
app.build(IncrementalProjectBuilder.FULL_BUILD, null);
125+
IFile commandsFile = app.getFile("build/compile_commands.json");
126+
if (commandsFile.exists()) {
127+
128+
try (FileReader reader = new FileReader(commandsFile.getLocation().toFile())) {
129+
Gson gson = new Gson();
130+
JsonArray jsonArray = gson.fromJson(reader, JsonArray.class);
131+
for (JsonElement element : jsonArray) {
132+
CompilationDatabaseInformation compileCommand = gson.fromJson(element,
133+
CompilationDatabaseInformation.class);
134+
135+
assertTrue(compileCommand.directory() != null && !compileCommand.directory().isEmpty());
136+
assertTrue(compileCommand.command() != null && !compileCommand.command().isEmpty());
137+
assertTrue(compileCommand.file() != null && !compileCommand.file().isEmpty());
138+
assertTrue(compileCommand.file().endsWith("src/helloworldCPP.cpp"));
139+
}
140+
141+
}
142+
}
143+
}
144+
145+
/**
146+
* Tests that compilation database is not generated when feature is disabled
147+
*/
148+
@Test
149+
public void testCompilationDatabaseGenerationNotEnabled() throws Exception {
150+
setWorkspace("regressions");
151+
final IProject app = loadProject("helloworldC");
152+
setGenerateFileOptionEnabled(false);
153+
app.build(IncrementalProjectBuilder.FULL_BUILD, null);
154+
IFile compilationDatabase = app.getFile("build/compile_commands.json");
155+
assertFalse(compilationDatabase.exists());
156+
}
157+
158+
private static void setGenerateFileOptionEnabled(boolean value) {
159+
IPreferenceStore preferenceStore = new ScopedPreferenceStore(InstanceScope.INSTANCE,
160+
"org.eclipse.cdt.managedbuilder.ui");
161+
preferenceStore.setValue(CommonBuilder.COMPILATION_DATABASE_ENABLEMENT, value);
162+
}
163+
164+
@AfterEach
165+
public void restoreDefaultForGenerateFile() {
166+
IPreferenceStore preferenceStore = new ScopedPreferenceStore(InstanceScope.INSTANCE,
167+
"org.eclipse.cdt.managedbuilder.ui");
168+
preferenceStore.setToDefault(CommonBuilder.COMPILATION_DATABASE_ENABLEMENT);
169+
}
170+
}

build/org.eclipse.cdt.managedbuilder.core/.project

+5
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@
2525
<arguments>
2626
</arguments>
2727
</buildCommand>
28+
<buildCommand>
29+
<name>org.eclipse.pde.ds.core.builder</name>
30+
<arguments>
31+
</arguments>
32+
</buildCommand>
2833
</buildSpec>
2934
<natures>
3035
<nature>org.eclipse.jdt.core.javanature</nature>

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

+5-2
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.6.300.qualifier
5+
Bundle-Version: 9.7.0.qualifier
66
Bundle-Activator: org.eclipse.cdt.managedbuilder.core.ManagedBuilderCorePlugin
77
Bundle-Vendor: %providerName
88
Bundle-Localization: plugin
@@ -12,9 +12,11 @@ Export-Package: org.eclipse.cdt.build.core.scannerconfig,
1212
org.eclipse.cdt.managedbuilder.buildmodel,
1313
org.eclipse.cdt.managedbuilder.buildproperties,
1414
org.eclipse.cdt.managedbuilder.core,
15+
org.eclipse.cdt.managedbuilder.core.jsoncdb,
1516
org.eclipse.cdt.managedbuilder.envvar,
1617
org.eclipse.cdt.managedbuilder.internal.buildmodel;x-friends:="org.eclipse.cdt.managedbuilder.ui",
1718
org.eclipse.cdt.managedbuilder.internal.core;x-friends:="org.eclipse.cdt.managedbuilder.ui,org.eclipse.cdt.managedbuilder.headlessbuilderapp",
19+
org.eclipse.cdt.managedbuilder.internal.core.jsoncdb.generator;x-internal:=true,
1820
org.eclipse.cdt.managedbuilder.internal.dataprovider;x-internal:=true,
1921
org.eclipse.cdt.managedbuilder.internal.envvar;x-internal:=true,
2022
org.eclipse.cdt.managedbuilder.internal.language.settings.providers;x-friends:="org.eclipse.cdt.managedbuilder.ui",
@@ -39,7 +41,8 @@ Require-Bundle: org.eclipse.cdt.core;bundle-version="[8.3.0,9.0.0)",
3941
org.eclipse.ui;bundle-version="[3.2.0,4.0.0)",
4042
org.eclipse.core.variables;bundle-version="[3.1.100,4.0.0)",
4143
org.eclipse.cdt.make.core;visibility:=reexport,
42-
org.eclipse.core.filesystem;bundle-version="1.2.0"
44+
org.eclipse.core.filesystem;bundle-version="1.2.0",
45+
org.eclipse.jdt.annotation;bundle-version="[2.2.7,3.0.0)";resolution:=optional
4346
Bundle-ActivationPolicy: lazy
4447
Bundle-RequiredExecutionEnvironment: JavaSE-17
4548
Import-Package: com.google.gson;version="2.8.0"

build/org.eclipse.cdt.managedbuilder.core/plugin.properties

+1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ extension-point.name.0 = Managed Build Definitions
7878
extension-point.name.1 = Managed Build Project Converter
7979
extension-point.name.2 = Build Properties
8080
extension-point.name.3 = ToolChain Modification Info
81+
extension-point.name.4 = Compilation Database Contributor
8182

8283
GCCBuildOutputParser.name = CDT GCC Build Output Parser
8384
GCCBuiltinCompilerSettings.name = CDT GCC Built-in Compiler Settings

build/org.eclipse.cdt.managedbuilder.core/plugin.xml

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
<extension-point id="projectConverter" name="%extension-point.name.1" schema="schema/Converter.exsd"/>
88
<extension-point id="buildProperties" name="%extension-point.name.2" schema="schema/buildProperties.exsd"/>
99
<extension-point id="tcModificationInfo" name="%extension-point.name.3" schema="schema/tcModificationInfo.exsd"/>
10+
<extension-point id="compilationDatabaseContributor" name="%extension-point.name.4" schema="schema/compilationDatabaseContributor.exsd"/>
11+
1012

1113

1214
<!-- =================================================================================== -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?xml version='1.0' encoding='UTF-8'?>
2+
<!-- Schema file written by PDE -->
3+
<schema targetNamespace="org.eclipse.cdt.managedbuilder.core" xmlns="http://www.w3.org/2001/XMLSchema">
4+
<annotation>
5+
<appInfo>
6+
<meta.schema plugin="org.eclipse.cdt.managedbuilder.core" id="compilationDatabaseContributor" name="Compilation Database Contributor"/>
7+
</appInfo>
8+
<documentation>
9+
This extension point allows to add specific information to compile_commands.json file.
10+
</documentation>
11+
</annotation>
12+
13+
<element name="extension">
14+
<annotation>
15+
<appInfo>
16+
<meta.element />
17+
</appInfo>
18+
</annotation>
19+
<complexType>
20+
<sequence minOccurs="1" maxOccurs="unbounded">
21+
<choice>
22+
<element ref="compilationDatabase"/>
23+
</choice>
24+
</sequence>
25+
<attribute name="point" type="string" use="required">
26+
<annotation>
27+
<documentation>
28+
29+
</documentation>
30+
</annotation>
31+
</attribute>
32+
<attribute name="id" type="string">
33+
<annotation>
34+
<documentation>
35+
36+
</documentation>
37+
</annotation>
38+
</attribute>
39+
<attribute name="name" type="string">
40+
<annotation>
41+
<documentation>
42+
43+
</documentation>
44+
<appInfo>
45+
<meta.attribute translatable="true"/>
46+
</appInfo>
47+
</annotation>
48+
</attribute>
49+
</complexType>
50+
</element>
51+
52+
<element name="compilationDatabase">
53+
<annotation>
54+
<documentation>
55+
Compilation Database for C/C++ projects
56+
</documentation>
57+
</annotation>
58+
<complexType>
59+
<attribute name="toolchainID" type="string" use="required">
60+
<annotation>
61+
<documentation>
62+
Toolchain ID
63+
</documentation>
64+
</annotation>
65+
</attribute>
66+
<attribute name="runner" type="string" use="required">
67+
<annotation>
68+
<documentation>
69+
70+
</documentation>
71+
<appInfo>
72+
<meta.attribute kind="java" basedOn=":org.eclipse.cdt.managedbuilder.core.jsoncdb.ICompilationDatabaseContributor"/>
73+
</appInfo>
74+
</annotation>
75+
</attribute>
76+
</complexType>
77+
</element>
78+
79+
80+
81+
82+
83+
84+
</schema>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/********************************************************************************
2+
* Copyright (c) 2023, 2024 Renesas Electronics Corp. and others.
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Public License 2.0 which is available at
6+
* http://www.eclipse.org/legal/epl-2.0.
7+
*
8+
* SPDX-License-Identifier: EPL-2.0
9+
********************************************************************************/
10+
package org.eclipse.cdt.managedbuilder.core.jsoncdb;
11+
12+
/**
13+
* The compilation database array of “command objects” members to be used for
14+
* the extension point
15+
* directory: The working directory of the compilation.
16+
* command: The compile command. file: The main translation unit source
17+
* processed by this compilation step.
18+
* @since 9.7
19+
*/
20+
21+
public record CompilationDatabaseInformation(String directory, String command, String file) {
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/********************************************************************************
2+
* Copyright (c) 2023, 2024 Renesas Electronics Europe. and others.
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Public License 2.0 which is available at
6+
* http://www.eclipse.org/legal/epl-2.0.
7+
*
8+
* SPDX-License-Identifier: EPL-2.0
9+
********************************************************************************/
10+
package org.eclipse.cdt.managedbuilder.core.jsoncdb;
11+
12+
import java.util.List;
13+
14+
import org.eclipse.cdt.managedbuilder.core.IConfiguration;
15+
import org.eclipse.jdt.annotation.NonNull;
16+
17+
/**
18+
* @since 9.7
19+
*/
20+
public interface ICompilationDatabaseContributor {
21+
22+
/**
23+
* @param config
24+
* Adds a new list of files to the compilation database
25+
* Implementors should provide concrete implementations of this
26+
* interface. IConfiguration will be taken as input, accessing the project and will generate a list of
27+
* additional files to be added to compile_commands.json
28+
* @return A non-null list of files that will be added to compilation database
29+
*/
30+
@NonNull
31+
public List<CompilationDatabaseInformation> getAdditionalFiles(@NonNull IConfiguration config);
32+
33+
}

0 commit comments

Comments
 (0)