Skip to content

Commit beb9200

Browse files
committed
Added feature to generate compile_commands.json file, in the Common
Builder. Created a new extension point to provide the opportunity to incorporate additional information into the file
1 parent 2e38e74 commit beb9200

File tree

9 files changed

+705
-2
lines changed

9 files changed

+705
-2
lines changed

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

+4-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,6 +12,7 @@ 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",
@@ -39,7 +40,8 @@ Require-Bundle: org.eclipse.cdt.core;bundle-version="[8.3.0,9.0.0)",
3940
org.eclipse.ui;bundle-version="[3.2.0,4.0.0)",
4041
org.eclipse.core.variables;bundle-version="[3.1.100,4.0.0)",
4142
org.eclipse.cdt.make.core;visibility:=reexport,
42-
org.eclipse.core.filesystem;bundle-version="1.2.0"
43+
org.eclipse.core.filesystem;bundle-version="1.2.0",
44+
org.eclipse.jdt.annotation;bundle-version="[2.2.7,3.0.0)";resolution:=optional
4345
Bundle-ActivationPolicy: lazy
4446
Bundle-RequiredExecutionEnvironment: JavaSE-17
4547
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.generator.api.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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
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.generator;
11+
12+
import java.util.Collections;
13+
import java.util.HashMap;
14+
import java.util.List;
15+
import java.util.Map;
16+
17+
import org.eclipse.cdt.managedbuilder.core.IConfiguration;
18+
import org.eclipse.cdt.managedbuilder.core.IToolChain;
19+
import org.eclipse.cdt.managedbuilder.core.jsoncdb.CompilationDatabaseInformation;
20+
import org.eclipse.cdt.managedbuilder.core.jsoncdb.ICompilationDatabaseContributor;
21+
import org.eclipse.core.runtime.Assert;
22+
import org.eclipse.core.runtime.CoreException;
23+
import org.eclipse.core.runtime.IConfigurationElement;
24+
import org.eclipse.core.runtime.IExtension;
25+
import org.eclipse.core.runtime.IExtensionPoint;
26+
import org.eclipse.core.runtime.Platform;
27+
import org.eclipse.core.runtime.Status;
28+
import org.eclipse.jdt.annotation.NonNull;
29+
30+
final class CompilationDatabaseContributionManager {
31+
private static final String ATTRIB_RUNNER = "runner"; //$NON-NLS-1$
32+
private static final String ATTRIB_TOOLCHAIN_ID = "toolchainID"; //$NON-NLS-1$
33+
private static final String ID_COMPILATIONDATABASE = "compilationDatabase"; //$NON-NLS-1$
34+
private static final String EXTENSION_ID = "compilationDatabaseContributor"; //$NON-NLS-1$
35+
@NonNull
36+
private final Map<String, ICompilationDatabaseContributor> loadedInstances;
37+
38+
private class EmptyCompilationDatabaseContributor implements ICompilationDatabaseContributor {
39+
40+
@Override
41+
public final @NonNull List<CompilationDatabaseInformation> getAdditionalFiles(@NonNull IConfiguration config) {
42+
return Collections.emptyList();
43+
}
44+
}
45+
46+
private Map<String, IConfigurationElement> factoryExtensions;
47+
private static CompilationDatabaseContributionManager instance;
48+
49+
private CompilationDatabaseContributionManager() {
50+
factoryExtensions = new HashMap<>();
51+
loadedInstances = new HashMap<>();
52+
initalise();
53+
}
54+
55+
public static synchronized CompilationDatabaseContributionManager getInstance() {
56+
if (CompilationDatabaseContributionManager.instance == null) {
57+
CompilationDatabaseContributionManager.instance = new CompilationDatabaseContributionManager();
58+
}
59+
return CompilationDatabaseContributionManager.instance;
60+
}
61+
62+
private void initalise() {
63+
Map<String, IConfigurationElement> loadedExtension = new HashMap<>();
64+
65+
IExtensionPoint extension = Platform.getExtensionRegistry().getExtensionPoint(
66+
"org.eclipse.cdt.managedbuilder.core", CompilationDatabaseContributionManager.EXTENSION_ID); //$NON-NLS-1$
67+
if (extension != null) {
68+
IExtension[] extensions = extension.getExtensions();
69+
for (IExtension extension2 : extensions) {
70+
IConfigurationElement[] configElements = extension2.getConfigurationElements();
71+
for (IConfigurationElement configElement : configElements) {
72+
if (CompilationDatabaseContributionManager.ID_COMPILATIONDATABASE.equals(configElement.getName())) { // $NON-NLS-1$
73+
String toolchainId = configElement
74+
.getAttribute(CompilationDatabaseContributionManager.ATTRIB_TOOLCHAIN_ID);
75+
String className = configElement
76+
.getAttribute(CompilationDatabaseContributionManager.ATTRIB_RUNNER);
77+
if (toolchainId != null && className != null) {
78+
loadedExtension.put(toolchainId, configElement);
79+
}
80+
}
81+
}
82+
}
83+
}
84+
85+
factoryExtensions = loadedExtension;
86+
}
87+
88+
/**
89+
* Get the Code Assist tool with the specified id
90+
*
91+
* @param id
92+
* @return Tool extension or a list with no element if not found.
93+
* @throws CoreException
94+
*/
95+
96+
@NonNull
97+
public synchronized ICompilationDatabaseContributor getCompilationDatabaseContributor(
98+
@NonNull IConfiguration config) {
99+
IToolChain toolChain = config.getToolChain();
100+
while (toolChain != null) {
101+
String toolchainId = toolChain.getBaseId();
102+
if (loadedInstances.containsKey(toolchainId)) {
103+
ICompilationDatabaseContributor contributor = loadedInstances.get(toolchainId);
104+
Assert.isNotNull(contributor);
105+
return contributor;
106+
} else if (factoryExtensions.containsKey(toolchainId)) {
107+
return createCdbInstance(toolchainId);
108+
} else {
109+
toolChain = toolChain.getSuperClass();
110+
}
111+
}
112+
return new EmptyCompilationDatabaseContributor();
113+
}
114+
115+
@NonNull
116+
private ICompilationDatabaseContributor createCdbInstance(String toolchainId) {
117+
IConfigurationElement ele = factoryExtensions.get(toolchainId);
118+
if (ele != null) {
119+
ICompilationDatabaseContributor loaded = null;
120+
try {
121+
loaded = (ICompilationDatabaseContributor) ele
122+
.createExecutableExtension(CompilationDatabaseContributionManager.ATTRIB_RUNNER);
123+
124+
} catch (CoreException e) {
125+
Platform.getLog(getClass()).log(Status.error("Not able to create instance", e)); //$NON-NLS-1$
126+
}
127+
if (loaded == null) {
128+
loaded = new EmptyCompilationDatabaseContributor();
129+
}
130+
loadedInstances.put(toolchainId, loaded);
131+
return loaded;
132+
}
133+
134+
return new EmptyCompilationDatabaseContributor();
135+
}
136+
137+
}

0 commit comments

Comments
 (0)