Skip to content

Commit

Permalink
Added feature to generate compile_commands.json file, in the Common
Browse files Browse the repository at this point in the history
Builder. Created a new extension point to provide the opportunity to
incorporate additional information into the file
  • Loading branch information
alicetrifu committed Feb 9, 2024
1 parent 2e38e74 commit beb9200
Show file tree
Hide file tree
Showing 9 changed files with 705 additions and 2 deletions.
Expand Up @@ -2,7 +2,7 @@ Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: %pluginName
Bundle-SymbolicName: org.eclipse.cdt.managedbuilder.core; singleton:=true
Bundle-Version: 9.6.300.qualifier
Bundle-Version: 9.7.0.qualifier
Bundle-Activator: org.eclipse.cdt.managedbuilder.core.ManagedBuilderCorePlugin
Bundle-Vendor: %providerName
Bundle-Localization: plugin
Expand All @@ -12,6 +12,7 @@ Export-Package: org.eclipse.cdt.build.core.scannerconfig,
org.eclipse.cdt.managedbuilder.buildmodel,
org.eclipse.cdt.managedbuilder.buildproperties,
org.eclipse.cdt.managedbuilder.core,
org.eclipse.cdt.managedbuilder.core.jsoncdb,
org.eclipse.cdt.managedbuilder.envvar,
org.eclipse.cdt.managedbuilder.internal.buildmodel;x-friends:="org.eclipse.cdt.managedbuilder.ui",
org.eclipse.cdt.managedbuilder.internal.core;x-friends:="org.eclipse.cdt.managedbuilder.ui,org.eclipse.cdt.managedbuilder.headlessbuilderapp",
Expand Down Expand Up @@ -39,7 +40,8 @@ Require-Bundle: org.eclipse.cdt.core;bundle-version="[8.3.0,9.0.0)",
org.eclipse.ui;bundle-version="[3.2.0,4.0.0)",
org.eclipse.core.variables;bundle-version="[3.1.100,4.0.0)",
org.eclipse.cdt.make.core;visibility:=reexport,
org.eclipse.core.filesystem;bundle-version="1.2.0"
org.eclipse.core.filesystem;bundle-version="1.2.0",
org.eclipse.jdt.annotation;bundle-version="[2.2.7,3.0.0)";resolution:=optional
Bundle-ActivationPolicy: lazy
Bundle-RequiredExecutionEnvironment: JavaSE-17
Import-Package: com.google.gson;version="2.8.0"
Expand Down
Expand Up @@ -78,6 +78,7 @@ extension-point.name.0 = Managed Build Definitions
extension-point.name.1 = Managed Build Project Converter
extension-point.name.2 = Build Properties
extension-point.name.3 = ToolChain Modification Info
extension-point.name.4 = Compilation Database Contributor

GCCBuildOutputParser.name = CDT GCC Build Output Parser
GCCBuiltinCompilerSettings.name = CDT GCC Built-in Compiler Settings
Expand Down
2 changes: 2 additions & 0 deletions build/org.eclipse.cdt.managedbuilder.core/plugin.xml
Expand Up @@ -7,6 +7,8 @@
<extension-point id="projectConverter" name="%extension-point.name.1" schema="schema/Converter.exsd"/>
<extension-point id="buildProperties" name="%extension-point.name.2" schema="schema/buildProperties.exsd"/>
<extension-point id="tcModificationInfo" name="%extension-point.name.3" schema="schema/tcModificationInfo.exsd"/>
<extension-point id="compilationDatabaseContributor" name="%extension-point.name.4" schema="schema/compilationDatabaseContributor.exsd"/>



<!-- =================================================================================== -->
Expand Down
@@ -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.generator.api.ICompilationDatabaseContributor"/>
</appInfo>
</annotation>
</attribute>
</complexType>
</element>






</schema>
@@ -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) {
}
@@ -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);

}
@@ -0,0 +1,137 @@
/********************************************************************************
* 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.generator;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.cdt.managedbuilder.core.IConfiguration;
import org.eclipse.cdt.managedbuilder.core.IToolChain;
import org.eclipse.cdt.managedbuilder.core.jsoncdb.CompilationDatabaseInformation;
import org.eclipse.cdt.managedbuilder.core.jsoncdb.ICompilationDatabaseContributor;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.jdt.annotation.NonNull;

final class CompilationDatabaseContributionManager {
private static final String ATTRIB_RUNNER = "runner"; //$NON-NLS-1$
private static final String ATTRIB_TOOLCHAIN_ID = "toolchainID"; //$NON-NLS-1$
private static final String ID_COMPILATIONDATABASE = "compilationDatabase"; //$NON-NLS-1$
private static final String EXTENSION_ID = "compilationDatabaseContributor"; //$NON-NLS-1$
@NonNull
private final Map<String, ICompilationDatabaseContributor> loadedInstances;

private class EmptyCompilationDatabaseContributor implements ICompilationDatabaseContributor {

@Override
public final @NonNull List<CompilationDatabaseInformation> getAdditionalFiles(@NonNull IConfiguration config) {
return Collections.emptyList();
}
}

private Map<String, IConfigurationElement> factoryExtensions;
private static CompilationDatabaseContributionManager instance;

private CompilationDatabaseContributionManager() {
factoryExtensions = new HashMap<>();
loadedInstances = new HashMap<>();
initalise();
}

public static synchronized CompilationDatabaseContributionManager getInstance() {
if (CompilationDatabaseContributionManager.instance == null) {
CompilationDatabaseContributionManager.instance = new CompilationDatabaseContributionManager();
}
return CompilationDatabaseContributionManager.instance;
}

private void initalise() {
Map<String, IConfigurationElement> loadedExtension = new HashMap<>();

IExtensionPoint extension = Platform.getExtensionRegistry().getExtensionPoint(
"org.eclipse.cdt.managedbuilder.core", CompilationDatabaseContributionManager.EXTENSION_ID); //$NON-NLS-1$
if (extension != null) {
IExtension[] extensions = extension.getExtensions();
for (IExtension extension2 : extensions) {
IConfigurationElement[] configElements = extension2.getConfigurationElements();
for (IConfigurationElement configElement : configElements) {
if (CompilationDatabaseContributionManager.ID_COMPILATIONDATABASE.equals(configElement.getName())) { // $NON-NLS-1$
String toolchainId = configElement
.getAttribute(CompilationDatabaseContributionManager.ATTRIB_TOOLCHAIN_ID);
String className = configElement
.getAttribute(CompilationDatabaseContributionManager.ATTRIB_RUNNER);
if (toolchainId != null && className != null) {
loadedExtension.put(toolchainId, configElement);
}
}
}
}
}

factoryExtensions = loadedExtension;
}

/**
* Get the Code Assist tool with the specified id
*
* @param id
* @return Tool extension or a list with no element if not found.
* @throws CoreException
*/

@NonNull
public synchronized ICompilationDatabaseContributor getCompilationDatabaseContributor(
@NonNull IConfiguration config) {
IToolChain toolChain = config.getToolChain();
while (toolChain != null) {
String toolchainId = toolChain.getBaseId();
if (loadedInstances.containsKey(toolchainId)) {
ICompilationDatabaseContributor contributor = loadedInstances.get(toolchainId);
Assert.isNotNull(contributor);
return contributor;
} else if (factoryExtensions.containsKey(toolchainId)) {
return createCdbInstance(toolchainId);
} else {
toolChain = toolChain.getSuperClass();
}
}
return new EmptyCompilationDatabaseContributor();
}

@NonNull
private ICompilationDatabaseContributor createCdbInstance(String toolchainId) {
IConfigurationElement ele = factoryExtensions.get(toolchainId);
if (ele != null) {
ICompilationDatabaseContributor loaded = null;
try {
loaded = (ICompilationDatabaseContributor) ele
.createExecutableExtension(CompilationDatabaseContributionManager.ATTRIB_RUNNER);

} catch (CoreException e) {
Platform.getLog(getClass()).log(Status.error("Not able to create instance", e)); //$NON-NLS-1$
}
if (loaded == null) {
loaded = new EmptyCompilationDatabaseContributor();
}
loadedInstances.put(toolchainId, loaded);
return loaded;
}

return new EmptyCompilationDatabaseContributor();
}

}

0 comments on commit beb9200

Please sign in to comment.