forked from eclipse-cdt/cdt
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add API to set CMake generator default (eg Ninja)
ISV can set their desired CMake generator default (eg Ninja). Addresses Issue: CDT CMake Improvements eclipse-cdt#1000, IDE-82683-REQ-012 CMake generator default
- Loading branch information
1 parent
5e62200
commit afb8c41
Showing
19 changed files
with
428 additions
and
404 deletions.
There are no files selected for viewing
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
208 changes: 208 additions & 0 deletions
208
...pse.cdt.cmake.core.tests/src/org/eclipse/cdt/cmake/core/CMakeBuildConfigurationTests.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,208 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2025 Renesas Electronics Europe. | ||
* | ||
* 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.cmake.core; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.contains; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.eclipse.cdt.cmake.core.properties.CMakeGenerator; | ||
import org.eclipse.cdt.cmake.core.properties.ICMakeProperties; | ||
import org.eclipse.cdt.cmake.core.properties.IOsOverrides; | ||
import org.eclipse.cdt.core.CCProjectNature; | ||
import org.eclipse.cdt.core.CProjectNature; | ||
import org.eclipse.cdt.core.build.IToolChain; | ||
import org.eclipse.cdt.core.testplugin.ResourceHelper; | ||
import org.eclipse.cdt.core.testplugin.util.BaseTestCase5; | ||
import org.eclipse.core.resources.IBuildConfiguration; | ||
import org.eclipse.core.resources.IProject; | ||
import org.eclipse.core.resources.IProjectDescription; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
/** | ||
* Tests a new API added to the CMake Build Configuration which allows default CMake properties to be set. | ||
* See the new interface {@link ICMakeBuildConfiguration}. | ||
*/ | ||
public class CMakeBuildConfigurationTests extends BaseTestCase5 { | ||
private IBuildConfiguration buildConfig; | ||
private IToolChain mockToolchain; | ||
|
||
@BeforeEach | ||
public void setup() throws Exception { | ||
// Create a CMake project | ||
IProject project = createCMakeProject(); | ||
// Get the default build config from the project (it always has one) | ||
buildConfig = project.getBuildConfig(IBuildConfiguration.DEFAULT_CONFIG_NAME); | ||
// Setup a toolchain ready to use for creating the valid ICBuildConfiguration | ||
mockToolchain = mock(IToolChain.class); | ||
when(mockToolchain.getProperty(IToolChain.ATTR_OS)).thenReturn("osDummy"); | ||
when(mockToolchain.getProperty(IToolChain.ATTR_ARCH)).thenReturn("archDummy"); | ||
when(mockToolchain.getTypeId()).thenReturn("tc_typeId"); | ||
when(mockToolchain.getId()).thenReturn("tcId"); | ||
when(mockToolchain.getBuildConfigNameFragment()).thenReturn("buildConfigName"); | ||
} | ||
|
||
/** | ||
* Test for {@link IOsOverrides#setGenerator()}. | ||
*/ | ||
@Test | ||
public void getCMakePropertiesTestSetGenerator() throws Exception { | ||
CMakeBuildConfigurationExtended cmBuildConfig = new CMakeBuildConfigurationExtended(buildConfig, | ||
"cmBuildConfigName", mockToolchain) { | ||
|
||
@Override | ||
public ICMakeProperties getCMakeProperties() { | ||
ICMakeProperties properties = super.getCMakeProperties(); | ||
|
||
IOsOverrides windowsOverrides = properties.getWindowsOverrides(); | ||
windowsOverrides.setGenerator(CMakeGenerator.NMakeMakefiles); | ||
IOsOverrides linuxOverrides = properties.getLinuxOverrides(); | ||
linuxOverrides.setGenerator(CMakeGenerator.UnixMakefiles); | ||
return properties; | ||
} | ||
}; | ||
|
||
// Call the new method on ICMakeBuildConfiguration to get the default CMake properties. | ||
ICMakeProperties cMakeProperties = cmBuildConfig.getCMakeProperties(); | ||
|
||
// Get overrides for Windows host and check the default value for getGenerator. | ||
IOsOverrides windowsOverrides = cMakeProperties.getWindowsOverrides(); | ||
CMakeGenerator winGenerator = windowsOverrides.getGenerator(); | ||
assertThat(winGenerator, is(CMakeGenerator.NMakeMakefiles)); | ||
|
||
// Get overrides for Linux host and check the default value for getGenerator. | ||
IOsOverrides linuxOverrides = cMakeProperties.getLinuxOverrides(); | ||
CMakeGenerator linuxGenerator = linuxOverrides.getGenerator(); | ||
assertThat(linuxGenerator, is(CMakeGenerator.UnixMakefiles)); | ||
} | ||
|
||
/** | ||
* Test for {@link IOsOverrides#setDefaultGenerator()}. Also tests that {@link ICMakeProperties#reset(boolean)} works as expected. | ||
*/ | ||
@Test | ||
public void getCMakePropertiesTestSetDefaultGenerator() throws Exception { | ||
CMakeBuildConfigurationExtended cmBuildConfig = new CMakeBuildConfigurationExtended(buildConfig, | ||
"cmBuildConfigName", mockToolchain) { | ||
|
||
@Override | ||
public ICMakeProperties getCMakeProperties() { | ||
ICMakeProperties properties = super.getCMakeProperties(); | ||
|
||
IOsOverrides windowsOverrides = properties.getWindowsOverrides(); | ||
windowsOverrides.setDefaultGenerator(CMakeGenerator.NMakeMakefiles); | ||
IOsOverrides linuxOverrides = properties.getLinuxOverrides(); | ||
linuxOverrides.setDefaultGenerator(CMakeGenerator.UnixMakefiles); | ||
// reset(true) causes the CMake generator to be reset to it's default value. | ||
properties.reset(true); | ||
return properties; | ||
} | ||
}; | ||
|
||
// Call the new method on ICMakeBuildConfiguration to get the default CMake properties. | ||
ICMakeProperties cMakeProperties = cmBuildConfig.getCMakeProperties(); | ||
|
||
// Get overrides for Windows host and check the default value for getGenerator. | ||
IOsOverrides windowsOverrides = cMakeProperties.getWindowsOverrides(); | ||
CMakeGenerator winGenerator = windowsOverrides.getGenerator(); | ||
assertThat(winGenerator, is(CMakeGenerator.NMakeMakefiles)); | ||
|
||
// Get overrides for Linux host and check the default value for getGenerator. | ||
IOsOverrides linuxOverrides = cMakeProperties.getLinuxOverrides(); | ||
CMakeGenerator linuxGenerator = linuxOverrides.getGenerator(); | ||
assertThat(linuxGenerator, is(CMakeGenerator.UnixMakefiles)); | ||
} | ||
|
||
/** | ||
* Test for {@link ICMakeProperties#setExtraArguments()} | ||
* This is a different extraArguments to IOsOverrides#setExtraArguments(). | ||
* Presumably ICMakeProperties#setExtraArguments() are platform agnostic extra arguments, where as | ||
* IOsOverrides#setExtraArguments() can be set different for Linux and Windows. | ||
*/ | ||
@Test | ||
public void getCMakePropertiesTestSetExtraArguments() throws Exception { | ||
// Create a C Build Configuration using the default build config and an arbitrary name | ||
CMakeBuildConfigurationExtended cmBuildConfig = new CMakeBuildConfigurationExtended(buildConfig, | ||
"cmBuildConfigName", mockToolchain) { | ||
|
||
@Override | ||
public ICMakeProperties getCMakeProperties() { | ||
ICMakeProperties properties = super.getCMakeProperties(); | ||
properties.setExtraArguments( | ||
new ArrayList<>((List.of("-DplatformAgnosticArgsTest0=0", "-DplatformAgnosticArgsTest1=1")))); | ||
return properties; | ||
} | ||
}; | ||
// Call the new method on ICMakeBuildConfiguration to get the default CMake properties. | ||
ICMakeProperties cMakeProperties = cmBuildConfig.getCMakeProperties(); | ||
List<String> extraArguments = cMakeProperties.getExtraArguments(); | ||
assertThat(extraArguments, contains("-DplatformAgnosticArgsTest0=0", "-DplatformAgnosticArgsTest1=1")); | ||
} | ||
|
||
/** | ||
* Test for {@link IOsOverrides#setExtraArguments()} | ||
*/ | ||
@Test | ||
public void getCMakePropertiesTestIOsOverridesSetExtraArguments() throws Exception { | ||
// Create a C Build Configuration using the default build config and an arbitrary name | ||
CMakeBuildConfigurationExtended cmBuildConfig = new CMakeBuildConfigurationExtended(buildConfig, | ||
"cmBuildConfigName", mockToolchain); | ||
// Call the new method on ICMakeBuildConfiguration to get the default CMake properties. | ||
ICMakeProperties cMakeProperties = cmBuildConfig.getCMakeProperties(); | ||
|
||
// Get overrides for Windows host and check the default value for getExtraArguments. | ||
IOsOverrides windowsOverrides = cMakeProperties.getWindowsOverrides(); | ||
List<String> winExtraArguments = windowsOverrides.getExtraArguments(); | ||
assertThat(winExtraArguments, contains("-Dtest0=0", "-Dtest1=1")); | ||
|
||
// Get overrides for Linux host and check the default value for getExtraArguments. | ||
IOsOverrides linuxOverrides = cMakeProperties.getLinuxOverrides(); | ||
List<String> linuxExtraArguments = linuxOverrides.getExtraArguments(); | ||
assertThat(linuxExtraArguments, contains("-DLinuxtest0=0", "-DLinuxtest1=1")); | ||
} | ||
|
||
private class CMakeBuildConfigurationExtended extends CMakeBuildConfiguration { | ||
|
||
public CMakeBuildConfigurationExtended(IBuildConfiguration config, String name, IToolChain toolChain) { | ||
super(config, name, toolChain); | ||
} | ||
|
||
@Override | ||
public ICMakeProperties getCMakeProperties() { | ||
// get the built-in CDT defaults | ||
ICMakeProperties properties = super.getCMakeProperties(); | ||
|
||
IOsOverrides windowsOverrides = properties.getWindowsOverrides(); | ||
windowsOverrides.setExtraArguments(new ArrayList<>((List.of("-Dtest0=0", "-Dtest1=1")))); | ||
|
||
IOsOverrides linuxOverrides = properties.getLinuxOverrides(); | ||
linuxOverrides.setExtraArguments(new ArrayList<>((List.of("-DLinuxtest0=0", "-DLinuxtest1=1")))); | ||
|
||
return properties; | ||
} | ||
} | ||
|
||
private IProject createCMakeProject() throws Exception { | ||
// Create a plain Eclipse project | ||
IProject project = ResourceHelper.createProject(this.getName()); | ||
// Add C/C++ and CMake natures to make it a CMake project | ||
IProjectDescription description = project.getDescription(); | ||
description.setNatureIds( | ||
new String[] { CProjectNature.C_NATURE_ID, CCProjectNature.CC_NATURE_ID, CMakeNature.ID }); | ||
project.setDescription(description, null); | ||
return project; | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
....cdt.cmake.core.tests/src/org/eclipse/cdt/cmake/core/ExtendedCMakeBuildConfiguration.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,39 @@ | ||
package org.eclipse.cdt.cmake.core; | ||
|
||
import org.eclipse.cdt.cmake.core.properties.CMakeGenerator; | ||
import org.eclipse.cdt.cmake.core.properties.ICMakeProperties; | ||
import org.eclipse.cdt.cmake.core.properties.IOsOverrides; | ||
import org.eclipse.cdt.core.build.IToolChain; | ||
import org.eclipse.core.resources.IBuildConfiguration; | ||
import org.eclipse.core.runtime.CoreException; | ||
|
||
public class ExtendedCMakeBuildConfiguration extends CMakeBuildConfiguration { | ||
|
||
public ExtendedCMakeBuildConfiguration(IBuildConfiguration config, String name) throws CoreException { | ||
super(config, name); | ||
} | ||
|
||
public ExtendedCMakeBuildConfiguration(IBuildConfiguration config, String name, IToolChain toolChain, | ||
ICMakeToolChainFile toolChainFile, String launchMode) { | ||
super(config, name, toolChain, toolChainFile, launchMode); | ||
} | ||
|
||
public ExtendedCMakeBuildConfiguration(IBuildConfiguration config, String name, IToolChain toolChain) { | ||
super(config, name, toolChain); | ||
} | ||
|
||
// TODO: Here goes the example extending that is being developed in #1046 https://github.com/eclipse-cdt/cdt/pull/1046 | ||
@Override | ||
public ICMakeProperties getCMakeProperties() { | ||
ICMakeProperties properties = super.getCMakeProperties(); | ||
|
||
/* | ||
* To change the CMake generator (specified with -G <generator>) used on both Windows and Linux host platforms. | ||
*/ | ||
IOsOverrides windowsOverrides = properties.getWindowsOverrides(); | ||
windowsOverrides.setGenerator(CMakeGenerator.Ninja); | ||
IOsOverrides linuxOverrides = properties.getLinuxOverrides(); | ||
linuxOverrides.setGenerator(CMakeGenerator.UnixMakefiles); | ||
return properties; | ||
} | ||
} |
103 changes: 0 additions & 103 deletions
103
...ake.core.tests/src/org/eclipse/cdt/cmake/core/internal/CMakePropertiesControllerTest.java
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.