-
Notifications
You must be signed in to change notification settings - Fork 208
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created JSON Compilation Database Generator preference page and set file
generation on false by default. Added JUnit test to test the generation of the file
- Loading branch information
1 parent
e6fb475
commit 743ca47
Showing
8 changed files
with
333 additions
and
11 deletions.
There are no files selected for viewing
Binary file added
BIN
+3.91 KB
...clipse.cdt.managedbuilder.core.tests/resources/builderTests/regressions/helloworldCPP.zip
Binary file not shown.
187 changes: 187 additions & 0 deletions
187
...ts/tests/org/eclipse/cdt/managedbuilder/core/tests/CompilationDatabaseGenerationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
...org/eclipse/cdt/managedbuilder/ui/compilationdatabase/JsonCdbGeneratorPreferencePage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/******************************************************************************** | ||
* 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.ui.compilationdatabase; | ||
|
||
import org.eclipse.cdt.managedbuilder.ui.properties.ManagedBuilderUIPlugin; | ||
import org.eclipse.jface.preference.IPreferenceStore; | ||
import org.eclipse.jface.preference.PreferencePage; | ||
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.Control; | ||
import org.eclipse.swt.widgets.Group; | ||
import org.eclipse.ui.IWorkbench; | ||
import org.eclipse.ui.IWorkbenchPreferencePage; | ||
|
||
/** | ||
* Preference page for JSON Compilation Database Generator. | ||
*/ | ||
public class JsonCdbGeneratorPreferencePage extends PreferencePage implements IWorkbenchPreferencePage { | ||
private static final String ENABLE_FILE_GENERATION = "generateFile"; //$NON-NLS-1$ | ||
private Button generateFileCheckbox; | ||
private IPreferenceStore preferenceStore; | ||
|
||
public JsonCdbGeneratorPreferencePage() { | ||
preferenceStore = ManagedBuilderUIPlugin.getDefault().getPreferenceStore(); | ||
} | ||
|
||
@Override | ||
protected Control createContents(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)); | ||
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); | ||
}); | ||
return composite; | ||
} | ||
|
||
/** | ||
* Initializes the default values of this page in the preference bundle. | ||
*/ | ||
@Override | ||
protected void performDefaults() { | ||
super.performDefaults(); | ||
preferenceStore.setDefault(ENABLE_FILE_GENERATION, false); | ||
} | ||
|
||
@Override | ||
public void init(IWorkbench workbench) { | ||
} | ||
|
||
} |
Oops, something went wrong.