diff --git a/build/org.eclipse.cdt.managedbuilder.core.tests/resources/builderTests/regressions/helloworldCPP.zip b/build/org.eclipse.cdt.managedbuilder.core.tests/resources/builderTests/regressions/helloworldCPP.zip new file mode 100644 index 00000000000..1b7db76c4f1 Binary files /dev/null and b/build/org.eclipse.cdt.managedbuilder.core.tests/resources/builderTests/regressions/helloworldCPP.zip differ diff --git a/build/org.eclipse.cdt.managedbuilder.core.tests/tests/org/eclipse/cdt/managedbuilder/core/tests/CompilationDatabaseGenerationTest.java b/build/org.eclipse.cdt.managedbuilder.core.tests/tests/org/eclipse/cdt/managedbuilder/core/tests/CompilationDatabaseGenerationTest.java new file mode 100644 index 00000000000..c723e9668c4 --- /dev/null +++ b/build/org.eclipse.cdt.managedbuilder.core.tests/tests/org/eclipse/cdt/managedbuilder/core/tests/CompilationDatabaseGenerationTest.java @@ -0,0 +1,187 @@ +/******************************************************************************* + * 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 java.io.IOException; + +import org.eclipse.cdt.managedbuilder.core.ManagedBuilderCorePlugin; +import org.eclipse.cdt.managedbuilder.core.jsoncdb.CompilationDatabaseInformation; +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.CoreException; +import org.eclipse.core.runtime.preferences.InstanceScope; +import org.eclipse.jface.preference.IPreferenceStore; +import org.eclipse.ui.preferences.ScopedPreferenceStore; +import org.junit.Test; + +import com.google.gson.Gson; +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import com.google.gson.JsonIOException; + +public class CompilationDatabaseGenerationTest extends AbstractBuilderTest { + + /** + * Tests generation of compile_commands.json in "build" folder + * @throws CoreException + */ + @Test + public void testCompilationDatabaseGeneration() throws CoreException { + setWorkspace("regressions"); + final IProject app = loadProject("helloworldC"); + isGenerateFileOptionEnabled(true); + app.build(IncrementalProjectBuilder.FULL_BUILD, null); + IFile compilationDatabase = app.getFile("build/compile_commands.json"); + assertTrue(compilationDatabase.exists()); + } + + /** + * Tests format for compile_commands.json. JSON array is expected, containing an element for the c file + * @throws JsonIOException + * @throws CoreException + */ + @Test + public void testJsonFormat() throws JsonIOException, CoreException { + setWorkspace("regressions"); + final IProject app = loadProject("helloworldC"); + isGenerateFileOptionEnabled(true); + app.build(IncrementalProjectBuilder.FULL_BUILD, null); + IFile commandsFile = app.getFile("build/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); + System.out.println(jsonArray); + 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")); + } + + } catch (IOException e) { + assertTrue(false); + } + + } + } + + /** + * Test that compile_commands.json is correctly generated when more than one .c file is present as a source file + * @throws CoreException + */ + @Test + public void testMultipleFiles() throws CoreException { + setWorkspace("regressions"); + final IProject app = loadProject("helloworldC"); + IFile aFile = ManagedBuildTestHelper.createFile(app, "src/newFile.c"); + isGenerateFileOptionEnabled(true); + app.build(IncrementalProjectBuilder.FULL_BUILD, null); + IFile commandsFile = app.getFile("build/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); + } catch (IOException e) { + assertTrue(false); + } + + } + + /** + * Tests that cpp files are handled by compile_commands.json file generator + * @throws CoreException + */ + @Test + public void isCPPFileAllowed() throws CoreException { + setWorkspace("regressions"); + final IProject app = loadProject("helloworldCPP"); + isGenerateFileOptionEnabled(true); + app.build(IncrementalProjectBuilder.FULL_BUILD, null); + System.out.println(app.getLocation()); + IFile commandsFile = app.getFile("build/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); + System.out.println(jsonArray); + 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")); + } + + } catch (IOException e) { + assertTrue(false); + } + } + } + + /** + * Tests that compilation database is not generated when feature is disabled + * @throws CoreException + */ + @Test + public void testCompilationDatabaseGenerationNotEnabled() throws CoreException { + setWorkspace("regressions"); + final IProject app = loadProject("helloworldC"); + isGenerateFileOptionEnabled(false); + app.build(IncrementalProjectBuilder.FULL_BUILD, null); + IFile compilationDatabase = app.getFile("build/compile_commands.json"); + assertFalse(compilationDatabase.exists()); + } + + public static boolean isGenerateFileOptionEnabled(boolean value) { + try { + IPreferenceStore preferenceStore = new ScopedPreferenceStore(InstanceScope.INSTANCE, + "org.eclipse.cdt.managedbuilder.ui"); //$NON-NLS-1$ + preferenceStore.setDefault("generateFile", value); + return preferenceStore.getBoolean("generateFile"); + } catch (Exception e) { + ManagedBuilderCorePlugin.log(e); + } + return false; + } +} diff --git a/build/org.eclipse.cdt.managedbuilder.core/schema/compilationDatabaseContributor.exsd b/build/org.eclipse.cdt.managedbuilder.core/schema/compilationDatabaseContributor.exsd index 5453c5de637..ebe86d50583 100644 --- a/build/org.eclipse.cdt.managedbuilder.core/schema/compilationDatabaseContributor.exsd +++ b/build/org.eclipse.cdt.managedbuilder.core/schema/compilationDatabaseContributor.exsd @@ -69,7 +69,7 @@ - + diff --git a/build/org.eclipse.cdt.managedbuilder.core/src/org/eclipse/cdt/managedbuilder/internal/core/CommonBuilder.java b/build/org.eclipse.cdt.managedbuilder.core/src/org/eclipse/cdt/managedbuilder/internal/core/CommonBuilder.java index e470b70ab08..e0dab6f4188 100644 --- a/build/org.eclipse.cdt.managedbuilder.core/src/org/eclipse/cdt/managedbuilder/internal/core/CommonBuilder.java +++ b/build/org.eclipse.cdt.managedbuilder.core/src/org/eclipse/cdt/managedbuilder/internal/core/CommonBuilder.java @@ -51,13 +51,13 @@ import org.eclipse.cdt.managedbuilder.core.IManagedBuildInfo; import org.eclipse.cdt.managedbuilder.core.ManagedBuildManager; import org.eclipse.cdt.managedbuilder.core.ManagedBuilderCorePlugin; -import org.eclipse.cdt.managedbuilder.core.jsoncdb.generator.CompilationDatabaseGenerator; import org.eclipse.cdt.managedbuilder.internal.buildmodel.BuildDescription; import org.eclipse.cdt.managedbuilder.internal.buildmodel.BuildStateManager; import org.eclipse.cdt.managedbuilder.internal.buildmodel.IBuildModelBuilder; import org.eclipse.cdt.managedbuilder.internal.buildmodel.IConfigurationBuildState; import org.eclipse.cdt.managedbuilder.internal.buildmodel.IProjectBuildState; import org.eclipse.cdt.managedbuilder.internal.buildmodel.StepBuilder; +import org.eclipse.cdt.managedbuilder.internal.core.jsoncdb.generator.CompilationDatabaseGenerator; import org.eclipse.cdt.managedbuilder.macros.BuildMacroException; import org.eclipse.cdt.managedbuilder.macros.IBuildMacroProvider; import org.eclipse.cdt.managedbuilder.makegen.IManagedBuilderMakefileGenerator; @@ -86,6 +86,9 @@ import org.eclipse.core.runtime.SubProgressMonitor; import org.eclipse.core.runtime.jobs.ISchedulingRule; import org.eclipse.core.runtime.jobs.Job; +import org.eclipse.core.runtime.preferences.InstanceScope; +import org.eclipse.jface.preference.IPreferenceStore; +import org.eclipse.ui.preferences.ScopedPreferenceStore; public class CommonBuilder extends ACBuilder implements IIncrementalProjectBuilder2 { @@ -94,6 +97,7 @@ public class CommonBuilder extends ACBuilder implements IIncrementalProjectBuild private static final String NEWLINE = System.getProperty("line.separator"); //$NON-NLS-1$ private static final String TRACE_FOOTER = "]: "; //$NON-NLS-1$ private static final String TRACE_HEADER = "GeneratedmakefileBuilder trace ["; //$NON-NLS-1$ + private static final String COMPILATION_DATABASE_ENABLEMENT = "generateFile"; //$NON-NLS-1$ public static boolean VERBOSE = false; private static final int PROGRESS_MONITOR_SCALE = 100; @@ -506,8 +510,10 @@ private IProject[] build(int kind, IProject project, IBuilder[] builders, boolea } for (int i = 0; i < num; i++) { - CompilationDatabaseGenerator generator = new CompilationDatabaseGenerator(getProject(), activeCfg); - generator.generate(); + if (isGenerateFileOptionEnabled()) { + CompilationDatabaseGenerator generator = new CompilationDatabaseGenerator(getProject(), activeCfg); + generator.generate(); + } //bug 219337 if (kind == INCREMENTAL_BUILD || kind == AUTO_BUILD) { if (buildConfigResourceChanges()) { //only build projects with project resource changes @@ -1378,4 +1384,15 @@ public ISchedulingRule getRule(int trigger, Map args) { // Success! return null; } + + public static boolean isGenerateFileOptionEnabled() { + try { + IPreferenceStore preferenceStore = new ScopedPreferenceStore(InstanceScope.INSTANCE, + "org.eclipse.cdt.managedbuilder.ui"); //$NON-NLS-1$ + return preferenceStore.getBoolean(COMPILATION_DATABASE_ENABLEMENT); + } catch (Exception e) { + ManagedBuilderCorePlugin.log(e); + } + return false; + } } diff --git a/build/org.eclipse.cdt.managedbuilder.core/src/org/eclipse/cdt/managedbuilder/core/jsoncdb/generator/CompilationDatabaseContributionManager.java b/build/org.eclipse.cdt.managedbuilder.core/src/org/eclipse/cdt/managedbuilder/internal/core/jsoncdb/generator/CompilationDatabaseContributionManager.java similarity index 91% rename from build/org.eclipse.cdt.managedbuilder.core/src/org/eclipse/cdt/managedbuilder/core/jsoncdb/generator/CompilationDatabaseContributionManager.java rename to build/org.eclipse.cdt.managedbuilder.core/src/org/eclipse/cdt/managedbuilder/internal/core/jsoncdb/generator/CompilationDatabaseContributionManager.java index aa53f3a3961..b6792ab56a9 100644 --- a/build/org.eclipse.cdt.managedbuilder.core/src/org/eclipse/cdt/managedbuilder/core/jsoncdb/generator/CompilationDatabaseContributionManager.java +++ b/build/org.eclipse.cdt.managedbuilder.core/src/org/eclipse/cdt/managedbuilder/internal/core/jsoncdb/generator/CompilationDatabaseContributionManager.java @@ -7,7 +7,7 @@ * * SPDX-License-Identifier: EPL-2.0 ********************************************************************************/ -package org.eclipse.cdt.managedbuilder.core.jsoncdb.generator; +package org.eclipse.cdt.managedbuilder.internal.core.jsoncdb.generator; import java.util.Collections; import java.util.HashMap; @@ -32,9 +32,17 @@ 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$ + /** + * Map of tool chain IDs (see {@link IToolChain#getId()} to + * loaded instances of {@link ICompilationDatabaseContributor} + */ @NonNull - private final Map loadedInstances; - private final Map factoryExtensions; + private final Map loadedInstances = new HashMap<>(); + /** + * Map of tool chain IDs (see {@link IToolChain#getId()} to + * extension point information for the compilationDatabaseContributor extension. + */ + private final Map factoryExtensions = new HashMap<>(); private class EmptyCompilationDatabaseContributor implements ICompilationDatabaseContributor { @@ -47,8 +55,6 @@ private class EmptyCompilationDatabaseContributor implements ICompilationDatabas private static CompilationDatabaseContributionManager instance; private CompilationDatabaseContributionManager() { - this.factoryExtensions = new HashMap<>(); - this.loadedInstances = new HashMap<>(); initalise(); } @@ -60,8 +66,6 @@ public static synchronized CompilationDatabaseContributionManager getInstance() } private void initalise() { - Map loadedExtension = new HashMap<>(); - IExtensionPoint extension = Platform.getExtensionRegistry().getExtensionPoint( "org.eclipse.cdt.managedbuilder.core", CompilationDatabaseContributionManager.EXTENSION_ID); //$NON-NLS-1$ if (extension != null) { @@ -75,14 +79,12 @@ private void initalise() { String className = configElement .getAttribute(CompilationDatabaseContributionManager.ATTRIB_RUNNER); if (toolchainId != null && className != null) { - loadedExtension.put(toolchainId, configElement); + factoryExtensions.put(toolchainId, configElement); } } } } } - - this.factoryExtensions.putAll(loadedExtension); } /** diff --git a/build/org.eclipse.cdt.managedbuilder.core/src/org/eclipse/cdt/managedbuilder/core/jsoncdb/generator/CompilationDatabaseGenerator.java b/build/org.eclipse.cdt.managedbuilder.core/src/org/eclipse/cdt/managedbuilder/internal/core/jsoncdb/generator/CompilationDatabaseGenerator.java similarity index 99% rename from build/org.eclipse.cdt.managedbuilder.core/src/org/eclipse/cdt/managedbuilder/core/jsoncdb/generator/CompilationDatabaseGenerator.java rename to build/org.eclipse.cdt.managedbuilder.core/src/org/eclipse/cdt/managedbuilder/internal/core/jsoncdb/generator/CompilationDatabaseGenerator.java index 916adddf719..d7d3c5d3b9f 100644 --- a/build/org.eclipse.cdt.managedbuilder.core/src/org/eclipse/cdt/managedbuilder/core/jsoncdb/generator/CompilationDatabaseGenerator.java +++ b/build/org.eclipse.cdt.managedbuilder.core/src/org/eclipse/cdt/managedbuilder/internal/core/jsoncdb/generator/CompilationDatabaseGenerator.java @@ -7,7 +7,7 @@ * * SPDX-License-Identifier: EPL-2.0 ********************************************************************************/ -package org.eclipse.cdt.managedbuilder.core.jsoncdb.generator; +package org.eclipse.cdt.managedbuilder.internal.core.jsoncdb.generator; import java.io.ByteArrayInputStream; import java.io.InputStream; diff --git a/build/org.eclipse.cdt.managedbuilder.ui/META-INF/MANIFEST.MF b/build/org.eclipse.cdt.managedbuilder.ui/META-INF/MANIFEST.MF index 8759e6dc2e2..211ddabea56 100644 --- a/build/org.eclipse.cdt.managedbuilder.ui/META-INF/MANIFEST.MF +++ b/build/org.eclipse.cdt.managedbuilder.ui/META-INF/MANIFEST.MF @@ -2,7 +2,7 @@ Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: %pluginName Bundle-SymbolicName: org.eclipse.cdt.managedbuilder.ui; singleton:=true -Bundle-Version: 9.4.0.qualifier +Bundle-Version: 9.4.100.qualifier Bundle-Activator: org.eclipse.cdt.managedbuilder.ui.properties.ManagedBuilderUIPlugin Bundle-Vendor: %providerName Bundle-Localization: plugin diff --git a/build/org.eclipse.cdt.managedbuilder.ui/plugin.properties b/build/org.eclipse.cdt.managedbuilder.ui/plugin.properties index 14a4812b3e0..1ab240aa55e 100644 --- a/build/org.eclipse.cdt.managedbuilder.ui/plugin.properties +++ b/build/org.eclipse.cdt.managedbuilder.ui/plugin.properties @@ -119,3 +119,5 @@ Configurations.menu=Build Configurations buildDefinitionsUI.ep.name = Build Definitions UI extension-point.name = Custom MBS New Wizard Pages + +JSONCompilatioDatabaseGeneratorPage.name = JSON Compilation Database Generator \ No newline at end of file diff --git a/build/org.eclipse.cdt.managedbuilder.ui/plugin.xml b/build/org.eclipse.cdt.managedbuilder.ui/plugin.xml index 198bcea8041..d1e0a8459a8 100644 --- a/build/org.eclipse.cdt.managedbuilder.ui/plugin.xml +++ b/build/org.eclipse.cdt.managedbuilder.ui/plugin.xml @@ -340,6 +340,12 @@ id="org.eclipse.cdt.managedbuilder.ui.preferences.PrefPage_MultiConfig" name="%multicfg"> + + @@ -809,6 +815,12 @@ + + @@ -917,5 +929,4 @@ label="%CDTToolchainProperty.keyword.toolchain2"> - diff --git a/build/org.eclipse.cdt.managedbuilder.ui/src/org/eclipse/cdt/managedbuilder/internal/ui/compilationdatabase/CompilationDatabaseGeneratorBlock.java b/build/org.eclipse.cdt.managedbuilder.ui/src/org/eclipse/cdt/managedbuilder/internal/ui/compilationdatabase/CompilationDatabaseGeneratorBlock.java new file mode 100644 index 00000000000..d100730449e --- /dev/null +++ b/build/org.eclipse.cdt.managedbuilder.ui/src/org/eclipse/cdt/managedbuilder/internal/ui/compilationdatabase/CompilationDatabaseGeneratorBlock.java @@ -0,0 +1,100 @@ +/******************************************************************************** + * 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.internal.ui.compilationdatabase; + +import org.eclipse.cdt.managedbuilder.ui.properties.ManagedBuilderUIPlugin; +import org.eclipse.cdt.ui.dialogs.AbstractCOptionPage; +import org.eclipse.cdt.ui.dialogs.ICOptionContainer; +import org.eclipse.cdt.ui.dialogs.ICOptionContainerExtension; +import org.eclipse.cdt.ui.dialogs.PreferenceScopeBlock; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.jface.preference.IPreferenceStore; +import org.eclipse.swt.SWT; +import org.eclipse.swt.layout.FillLayout; +import org.eclipse.swt.layout.GridData; +import org.eclipse.swt.layout.GridLayout; +import org.eclipse.swt.widgets.Button; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Group; + +/** + + * @noextend This class is not intended to be subclasses by clients. + * @since 9.5 + */ +public class CompilationDatabaseGeneratorBlock extends AbstractCOptionPage { + + private static final String PREF_PAGE_ID = "org.eclipse.cdt.managedbuilder.internal.ui.compilationdatabase.JsonCdbGeneratorPreferencePage"; //$NON-NLS-1$ + private final String ENABLE_FILE_GENERATION = "generateFile"; //$NON-NLS-1$ + private Button generateFileCheckbox; + private IPreferenceStore preferenceStore; + private PreferenceScopeBlock fPrefScopeBlock; + + protected CompilationDatabaseGeneratorBlock() { + preferenceStore = ManagedBuilderUIPlugin.getDefault().getPreferenceStore(); + performDefaults(); + } + + @Override + public void createControl(Composite parent) { + final Composite composite = new Composite(parent, SWT.NONE); + composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); + composite.setLayout(new GridLayout(1, false)); + if (getProject() != null || getContainer() instanceof ICOptionContainerExtension) { + fPrefScopeBlock = new PreferenceScopeBlock(PREF_PAGE_ID) { + @Override + protected void onPreferenceScopeChange() { + generateFileCheckbox.setEnabled(preferenceStore.getBoolean(ENABLE_FILE_GENERATION)); + } + }; + fPrefScopeBlock.createControl(composite); + fPrefScopeBlock.setInstanceScope(); + } + Group cdbGeneratorOptions = new Group(composite, SWT.NONE); + cdbGeneratorOptions.setLayout(new FillLayout(SWT.HORIZONTAL)); + cdbGeneratorOptions.setText(Messages.JsonCdbGeneratorPreferencePage_description); + cdbGeneratorOptions.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false)); + generateFileCheckbox = new Button(cdbGeneratorOptions, SWT.CHECK); + generateFileCheckbox.setSelection(preferenceStore.getBoolean(ENABLE_FILE_GENERATION)); + generateFileCheckbox.setText(Messages.JsonCdbGeneratorPreferencePage_generateCompilationdatabase); + generateFileCheckbox.addListener(SWT.Selection, e -> { + boolean newValue = generateFileCheckbox.getSelection(); + preferenceStore.setValue(ENABLE_FILE_GENERATION, newValue); + }); + + } + + @Override + public void performDefaults() { + preferenceStore.setDefault(ENABLE_FILE_GENERATION, false); + } + + @Override + public void performApply(IProgressMonitor monitor) throws CoreException { + this.performApply(monitor); + } + + private IProject getProject() { + ICOptionContainer container = getContainer(); + if (container != null) { + if (container instanceof ICOptionContainerExtension) { + try { + return ((ICOptionContainerExtension) container).getProjectHandle(); + } catch (Exception e) { + ManagedBuilderUIPlugin.log(e); + } + } + return container.getProject(); + } + return null; + } +} diff --git a/build/org.eclipse.cdt.managedbuilder.ui/src/org/eclipse/cdt/managedbuilder/internal/ui/compilationdatabase/JsonCdbGeneratorPreferencePage.java b/build/org.eclipse.cdt.managedbuilder.ui/src/org/eclipse/cdt/managedbuilder/internal/ui/compilationdatabase/JsonCdbGeneratorPreferencePage.java new file mode 100644 index 00000000000..88c67a21db9 --- /dev/null +++ b/build/org.eclipse.cdt.managedbuilder.ui/src/org/eclipse/cdt/managedbuilder/internal/ui/compilationdatabase/JsonCdbGeneratorPreferencePage.java @@ -0,0 +1,89 @@ +/******************************************************************************** + * 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.internal.ui.compilationdatabase; + +import org.eclipse.cdt.ui.dialogs.ICOptionContainer; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.NullProgressMonitor; +import org.eclipse.jface.preference.PreferencePage; +import org.eclipse.swt.SWT; +import org.eclipse.swt.layout.GridData; +import org.eclipse.swt.layout.GridLayout; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Control; +import org.eclipse.ui.IWorkbench; +import org.eclipse.ui.IWorkbenchPreferencePage; + +/** + * Preference page for JSON Compilation Database Generator. + */ +public class JsonCdbGeneratorPreferencePage extends PreferencePage + implements IWorkbenchPreferencePage, ICOptionContainer { + + private final CompilationDatabaseGeneratorBlock fOptionBlock; + + public JsonCdbGeneratorPreferencePage() { + fOptionBlock = new CompilationDatabaseGeneratorBlock(); + fOptionBlock.setContainer(this); + } + + @Override + protected Control createContents(Composite parent) { + GridLayout gl; + Composite composite = new Composite(parent, SWT.NONE); + composite.setLayout(gl = new GridLayout()); + composite.setLayoutData(new GridData()); + gl.verticalSpacing = 0; + fOptionBlock.createControl(composite); + return composite; + } + + @Override + public void init(IWorkbench workbench) { + } + + @Override + public void updateContainer() { + if (!fOptionBlock.isValid()) { + setErrorMessage(fOptionBlock.getErrorMessage()); + setValid(false); + } else { + setErrorMessage(null); + setValid(true); + } + } + + @Override + @SuppressWarnings("deprecation") + public org.eclipse.core.runtime.Preferences getPreferences() { + throw new UnsupportedOperationException(); + } + + @Override + public boolean performOk() { + try { + fOptionBlock.performApply(new NullProgressMonitor()); + } catch (CoreException e) { + } + return true; + } + + @Override + public void performDefaults() { + fOptionBlock.performDefaults(); + } + + @Override + public IProject getProject() { + return null; + } + +} \ No newline at end of file diff --git a/build/org.eclipse.cdt.managedbuilder.ui/src/org/eclipse/cdt/managedbuilder/internal/ui/compilationdatabase/JsonCdbGeneratorPropertyPage.java b/build/org.eclipse.cdt.managedbuilder.ui/src/org/eclipse/cdt/managedbuilder/internal/ui/compilationdatabase/JsonCdbGeneratorPropertyPage.java new file mode 100644 index 00000000000..f4c55ec52a8 --- /dev/null +++ b/build/org.eclipse.cdt.managedbuilder.ui/src/org/eclipse/cdt/managedbuilder/internal/ui/compilationdatabase/JsonCdbGeneratorPropertyPage.java @@ -0,0 +1,83 @@ +/******************************************************************************** + * 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.internal.ui.compilationdatabase; + +import org.eclipse.cdt.managedbuilder.ui.properties.ManagedBuilderUIPlugin; +import org.eclipse.cdt.ui.dialogs.ICOptionContainer; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IAdaptable; +import org.eclipse.core.runtime.NullProgressMonitor; +import org.eclipse.core.runtime.Preferences; +import org.eclipse.swt.SWT; +import org.eclipse.swt.layout.GridLayout; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Control; +import org.eclipse.ui.dialogs.PropertyPage; + +/** + * Property page for JSON Compilation Database Generator. + */ + +public class JsonCdbGeneratorPropertyPage extends PropertyPage implements ICOptionContainer { + private CompilationDatabaseGeneratorBlock optionPage; + + public JsonCdbGeneratorPropertyPage() { + super(); + optionPage = new CompilationDatabaseGeneratorBlock(); + optionPage.setContainer(this); + } + + @Override + protected Control createContents(Composite parent) { + Composite composite = new Composite(parent, SWT.NONE); + composite.setLayout(new GridLayout()); + optionPage.createControl(composite); + return composite; + } + + @Override + protected void performDefaults() { + optionPage.performDefaults(); + } + + @Override + public boolean performOk() { + try { + optionPage.performApply(new NullProgressMonitor()); + } catch (CoreException e) { + ManagedBuilderUIPlugin.log(e); + } + return true; + } + + @Override + public IProject getProject() { + IProject project = null; + IAdaptable elem = getElement(); + if (elem instanceof IProject) { + project = (IProject) elem; + } else if (elem != null) { + project = elem.getAdapter(IProject.class); + } + return project; + } + + @Override + public void updateContainer() { + } + + @Override + public Preferences getPreferences() { + return null; + } + +} diff --git a/build/org.eclipse.cdt.managedbuilder.ui/src/org/eclipse/cdt/managedbuilder/internal/ui/compilationdatabase/Messages.java b/build/org.eclipse.cdt.managedbuilder.ui/src/org/eclipse/cdt/managedbuilder/internal/ui/compilationdatabase/Messages.java new file mode 100644 index 00000000000..884a7e5f672 --- /dev/null +++ b/build/org.eclipse.cdt.managedbuilder.ui/src/org/eclipse/cdt/managedbuilder/internal/ui/compilationdatabase/Messages.java @@ -0,0 +1,26 @@ +/******************************************************************************** + * 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.internal.ui.compilationdatabase; + +import org.eclipse.osgi.util.NLS; + +class Messages extends NLS { + private static final String BUNDLE_NAME = "org.eclipse.cdt.managedbuilder.internal.ui.compilationdatabase.messages"; //$NON-NLS-1$ + public static String JsonCdbGeneratorPreferencePage_description; + public static String JsonCdbGeneratorPreferencePage_generateCompilationdatabase; + static { + // initialize resource bundle + NLS.initializeMessages(BUNDLE_NAME, Messages.class); + } + + private Messages() { + } +} diff --git a/build/org.eclipse.cdt.managedbuilder.ui/src/org/eclipse/cdt/managedbuilder/internal/ui/compilationdatabase/messages.properties b/build/org.eclipse.cdt.managedbuilder.ui/src/org/eclipse/cdt/managedbuilder/internal/ui/compilationdatabase/messages.properties new file mode 100644 index 00000000000..e9d671ea720 --- /dev/null +++ b/build/org.eclipse.cdt.managedbuilder.ui/src/org/eclipse/cdt/managedbuilder/internal/ui/compilationdatabase/messages.properties @@ -0,0 +1,11 @@ +############################################################################### +# 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 +############################################################################### +JsonCdbGeneratorPreferencePage_description=Enables default generation of compile_commands.json file on build process +JsonCdbGeneratorPreferencePage_generateCompilationdatabase=Generate compile_commands.json file \ No newline at end of file