Skip to content

Commit e668dea

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 e668dea

File tree

9 files changed

+697
-1
lines changed

9 files changed

+697
-1
lines changed

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

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

0 commit comments

Comments
 (0)