Skip to content

Commit afb8c41

Browse files
betamaxbanditjonahgraham
authored andcommitted
Add API to set CMake generator default (eg Ninja)
ISV can set their desired CMake generator default (eg Ninja). Addresses Issue: CDT CMake Improvements #1000, IDE-82683-REQ-012 CMake generator default
1 parent 5e62200 commit afb8c41

19 files changed

+428
-404
lines changed

NewAndNoteworthy/CHANGELOG-API.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ This section describes API removals that occurred in past releases, and upcoming
88

99
Below is the detailed descriptions of API changes and mitigation efforts API consumers need to take.
1010

11+
## API Changes in CDT 12.0.
12+
### org.eclipse.cdt.cmake.core.properties.ICMakePropertiesController removed
13+
14+
The interface ICMakePropertiesController provided load and save methods; only the load method was called in CDT and not the save method, because of a partially implemented feature.
15+
1116
## API Changes in CDT 11.5.
1217

1318
### org.eclipse.cdt.make.ui.dialogs.DiscoveredPathContainerPage removed

cmake/org.eclipse.cdt.cmake.core.tests/META-INF/MANIFEST.MF

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ Bundle-SymbolicName: org.eclipse.cdt.cmake.core.tests
55
Bundle-Version: 1.0.0.qualifier
66
Fragment-Host: org.eclipse.cdt.cmake.core;bundle-version="1.5.0"
77
Import-Package: org.assertj.core.api;version="[3.24.2,4.0.0)",
8-
org.junit.jupiter.api;version="[5.9.3,6.0.0)"
8+
org.junit.jupiter.api;version="[5.9.3,6.0.0)",
9+
org.mockito;version="[5.15.0,6.0.0)",
10+
org.mockito.stubbing;version="[5.15.0,6.0.0)"
911
Automatic-Module-Name: org.eclipse.cdt.cmake.core.tests
1012
Bundle-Vendor: %Bundle-Vendor
1113
Bundle-Copyright: %Bundle-Copyright
12-
Require-Bundle: org.junit
14+
Require-Bundle: org.junit,
15+
org.eclipse.cdt.core.tests;bundle-version="[5.4.0,6.0.0)"
1316

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2025 Renesas Electronics Europe.
3+
*
4+
* This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License 2.0
6+
* which accompanies this distribution, and is available at
7+
* https://www.eclipse.org/legal/epl-2.0/
8+
*
9+
* SPDX-License-Identifier: EPL-2.0
10+
*******************************************************************************/
11+
package org.eclipse.cdt.cmake.core;
12+
13+
import static org.hamcrest.MatcherAssert.assertThat;
14+
import static org.hamcrest.Matchers.contains;
15+
import static org.hamcrest.Matchers.is;
16+
import static org.mockito.Mockito.mock;
17+
import static org.mockito.Mockito.when;
18+
19+
import java.util.ArrayList;
20+
import java.util.List;
21+
22+
import org.eclipse.cdt.cmake.core.properties.CMakeGenerator;
23+
import org.eclipse.cdt.cmake.core.properties.ICMakeProperties;
24+
import org.eclipse.cdt.cmake.core.properties.IOsOverrides;
25+
import org.eclipse.cdt.core.CCProjectNature;
26+
import org.eclipse.cdt.core.CProjectNature;
27+
import org.eclipse.cdt.core.build.IToolChain;
28+
import org.eclipse.cdt.core.testplugin.ResourceHelper;
29+
import org.eclipse.cdt.core.testplugin.util.BaseTestCase5;
30+
import org.eclipse.core.resources.IBuildConfiguration;
31+
import org.eclipse.core.resources.IProject;
32+
import org.eclipse.core.resources.IProjectDescription;
33+
import org.junit.jupiter.api.BeforeEach;
34+
import org.junit.jupiter.api.Test;
35+
36+
/**
37+
* Tests a new API added to the CMake Build Configuration which allows default CMake properties to be set.
38+
* See the new interface {@link ICMakeBuildConfiguration}.
39+
*/
40+
public class CMakeBuildConfigurationTests extends BaseTestCase5 {
41+
private IBuildConfiguration buildConfig;
42+
private IToolChain mockToolchain;
43+
44+
@BeforeEach
45+
public void setup() throws Exception {
46+
// Create a CMake project
47+
IProject project = createCMakeProject();
48+
// Get the default build config from the project (it always has one)
49+
buildConfig = project.getBuildConfig(IBuildConfiguration.DEFAULT_CONFIG_NAME);
50+
// Setup a toolchain ready to use for creating the valid ICBuildConfiguration
51+
mockToolchain = mock(IToolChain.class);
52+
when(mockToolchain.getProperty(IToolChain.ATTR_OS)).thenReturn("osDummy");
53+
when(mockToolchain.getProperty(IToolChain.ATTR_ARCH)).thenReturn("archDummy");
54+
when(mockToolchain.getTypeId()).thenReturn("tc_typeId");
55+
when(mockToolchain.getId()).thenReturn("tcId");
56+
when(mockToolchain.getBuildConfigNameFragment()).thenReturn("buildConfigName");
57+
}
58+
59+
/**
60+
* Test for {@link IOsOverrides#setGenerator()}.
61+
*/
62+
@Test
63+
public void getCMakePropertiesTestSetGenerator() throws Exception {
64+
CMakeBuildConfigurationExtended cmBuildConfig = new CMakeBuildConfigurationExtended(buildConfig,
65+
"cmBuildConfigName", mockToolchain) {
66+
67+
@Override
68+
public ICMakeProperties getCMakeProperties() {
69+
ICMakeProperties properties = super.getCMakeProperties();
70+
71+
IOsOverrides windowsOverrides = properties.getWindowsOverrides();
72+
windowsOverrides.setGenerator(CMakeGenerator.NMakeMakefiles);
73+
IOsOverrides linuxOverrides = properties.getLinuxOverrides();
74+
linuxOverrides.setGenerator(CMakeGenerator.UnixMakefiles);
75+
return properties;
76+
}
77+
};
78+
79+
// Call the new method on ICMakeBuildConfiguration to get the default CMake properties.
80+
ICMakeProperties cMakeProperties = cmBuildConfig.getCMakeProperties();
81+
82+
// Get overrides for Windows host and check the default value for getGenerator.
83+
IOsOverrides windowsOverrides = cMakeProperties.getWindowsOverrides();
84+
CMakeGenerator winGenerator = windowsOverrides.getGenerator();
85+
assertThat(winGenerator, is(CMakeGenerator.NMakeMakefiles));
86+
87+
// Get overrides for Linux host and check the default value for getGenerator.
88+
IOsOverrides linuxOverrides = cMakeProperties.getLinuxOverrides();
89+
CMakeGenerator linuxGenerator = linuxOverrides.getGenerator();
90+
assertThat(linuxGenerator, is(CMakeGenerator.UnixMakefiles));
91+
}
92+
93+
/**
94+
* Test for {@link IOsOverrides#setDefaultGenerator()}. Also tests that {@link ICMakeProperties#reset(boolean)} works as expected.
95+
*/
96+
@Test
97+
public void getCMakePropertiesTestSetDefaultGenerator() throws Exception {
98+
CMakeBuildConfigurationExtended cmBuildConfig = new CMakeBuildConfigurationExtended(buildConfig,
99+
"cmBuildConfigName", mockToolchain) {
100+
101+
@Override
102+
public ICMakeProperties getCMakeProperties() {
103+
ICMakeProperties properties = super.getCMakeProperties();
104+
105+
IOsOverrides windowsOverrides = properties.getWindowsOverrides();
106+
windowsOverrides.setDefaultGenerator(CMakeGenerator.NMakeMakefiles);
107+
IOsOverrides linuxOverrides = properties.getLinuxOverrides();
108+
linuxOverrides.setDefaultGenerator(CMakeGenerator.UnixMakefiles);
109+
// reset(true) causes the CMake generator to be reset to it's default value.
110+
properties.reset(true);
111+
return properties;
112+
}
113+
};
114+
115+
// Call the new method on ICMakeBuildConfiguration to get the default CMake properties.
116+
ICMakeProperties cMakeProperties = cmBuildConfig.getCMakeProperties();
117+
118+
// Get overrides for Windows host and check the default value for getGenerator.
119+
IOsOverrides windowsOverrides = cMakeProperties.getWindowsOverrides();
120+
CMakeGenerator winGenerator = windowsOverrides.getGenerator();
121+
assertThat(winGenerator, is(CMakeGenerator.NMakeMakefiles));
122+
123+
// Get overrides for Linux host and check the default value for getGenerator.
124+
IOsOverrides linuxOverrides = cMakeProperties.getLinuxOverrides();
125+
CMakeGenerator linuxGenerator = linuxOverrides.getGenerator();
126+
assertThat(linuxGenerator, is(CMakeGenerator.UnixMakefiles));
127+
}
128+
129+
/**
130+
* Test for {@link ICMakeProperties#setExtraArguments()}
131+
* This is a different extraArguments to IOsOverrides#setExtraArguments().
132+
* Presumably ICMakeProperties#setExtraArguments() are platform agnostic extra arguments, where as
133+
* IOsOverrides#setExtraArguments() can be set different for Linux and Windows.
134+
*/
135+
@Test
136+
public void getCMakePropertiesTestSetExtraArguments() throws Exception {
137+
// Create a C Build Configuration using the default build config and an arbitrary name
138+
CMakeBuildConfigurationExtended cmBuildConfig = new CMakeBuildConfigurationExtended(buildConfig,
139+
"cmBuildConfigName", mockToolchain) {
140+
141+
@Override
142+
public ICMakeProperties getCMakeProperties() {
143+
ICMakeProperties properties = super.getCMakeProperties();
144+
properties.setExtraArguments(
145+
new ArrayList<>((List.of("-DplatformAgnosticArgsTest0=0", "-DplatformAgnosticArgsTest1=1"))));
146+
return properties;
147+
}
148+
};
149+
// Call the new method on ICMakeBuildConfiguration to get the default CMake properties.
150+
ICMakeProperties cMakeProperties = cmBuildConfig.getCMakeProperties();
151+
List<String> extraArguments = cMakeProperties.getExtraArguments();
152+
assertThat(extraArguments, contains("-DplatformAgnosticArgsTest0=0", "-DplatformAgnosticArgsTest1=1"));
153+
}
154+
155+
/**
156+
* Test for {@link IOsOverrides#setExtraArguments()}
157+
*/
158+
@Test
159+
public void getCMakePropertiesTestIOsOverridesSetExtraArguments() throws Exception {
160+
// Create a C Build Configuration using the default build config and an arbitrary name
161+
CMakeBuildConfigurationExtended cmBuildConfig = new CMakeBuildConfigurationExtended(buildConfig,
162+
"cmBuildConfigName", mockToolchain);
163+
// Call the new method on ICMakeBuildConfiguration to get the default CMake properties.
164+
ICMakeProperties cMakeProperties = cmBuildConfig.getCMakeProperties();
165+
166+
// Get overrides for Windows host and check the default value for getExtraArguments.
167+
IOsOverrides windowsOverrides = cMakeProperties.getWindowsOverrides();
168+
List<String> winExtraArguments = windowsOverrides.getExtraArguments();
169+
assertThat(winExtraArguments, contains("-Dtest0=0", "-Dtest1=1"));
170+
171+
// Get overrides for Linux host and check the default value for getExtraArguments.
172+
IOsOverrides linuxOverrides = cMakeProperties.getLinuxOverrides();
173+
List<String> linuxExtraArguments = linuxOverrides.getExtraArguments();
174+
assertThat(linuxExtraArguments, contains("-DLinuxtest0=0", "-DLinuxtest1=1"));
175+
}
176+
177+
private class CMakeBuildConfigurationExtended extends CMakeBuildConfiguration {
178+
179+
public CMakeBuildConfigurationExtended(IBuildConfiguration config, String name, IToolChain toolChain) {
180+
super(config, name, toolChain);
181+
}
182+
183+
@Override
184+
public ICMakeProperties getCMakeProperties() {
185+
// get the built-in CDT defaults
186+
ICMakeProperties properties = super.getCMakeProperties();
187+
188+
IOsOverrides windowsOverrides = properties.getWindowsOverrides();
189+
windowsOverrides.setExtraArguments(new ArrayList<>((List.of("-Dtest0=0", "-Dtest1=1"))));
190+
191+
IOsOverrides linuxOverrides = properties.getLinuxOverrides();
192+
linuxOverrides.setExtraArguments(new ArrayList<>((List.of("-DLinuxtest0=0", "-DLinuxtest1=1"))));
193+
194+
return properties;
195+
}
196+
}
197+
198+
private IProject createCMakeProject() throws Exception {
199+
// Create a plain Eclipse project
200+
IProject project = ResourceHelper.createProject(this.getName());
201+
// Add C/C++ and CMake natures to make it a CMake project
202+
IProjectDescription description = project.getDescription();
203+
description.setNatureIds(
204+
new String[] { CProjectNature.C_NATURE_ID, CCProjectNature.CC_NATURE_ID, CMakeNature.ID });
205+
project.setDescription(description, null);
206+
return project;
207+
}
208+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package org.eclipse.cdt.cmake.core;
2+
3+
import org.eclipse.cdt.cmake.core.properties.CMakeGenerator;
4+
import org.eclipse.cdt.cmake.core.properties.ICMakeProperties;
5+
import org.eclipse.cdt.cmake.core.properties.IOsOverrides;
6+
import org.eclipse.cdt.core.build.IToolChain;
7+
import org.eclipse.core.resources.IBuildConfiguration;
8+
import org.eclipse.core.runtime.CoreException;
9+
10+
public class ExtendedCMakeBuildConfiguration extends CMakeBuildConfiguration {
11+
12+
public ExtendedCMakeBuildConfiguration(IBuildConfiguration config, String name) throws CoreException {
13+
super(config, name);
14+
}
15+
16+
public ExtendedCMakeBuildConfiguration(IBuildConfiguration config, String name, IToolChain toolChain,
17+
ICMakeToolChainFile toolChainFile, String launchMode) {
18+
super(config, name, toolChain, toolChainFile, launchMode);
19+
}
20+
21+
public ExtendedCMakeBuildConfiguration(IBuildConfiguration config, String name, IToolChain toolChain) {
22+
super(config, name, toolChain);
23+
}
24+
25+
// TODO: Here goes the example extending that is being developed in #1046 https://github.com/eclipse-cdt/cdt/pull/1046
26+
@Override
27+
public ICMakeProperties getCMakeProperties() {
28+
ICMakeProperties properties = super.getCMakeProperties();
29+
30+
/*
31+
* To change the CMake generator (specified with -G <generator>) used on both Windows and Linux host platforms.
32+
*/
33+
IOsOverrides windowsOverrides = properties.getWindowsOverrides();
34+
windowsOverrides.setGenerator(CMakeGenerator.Ninja);
35+
IOsOverrides linuxOverrides = properties.getLinuxOverrides();
36+
linuxOverrides.setGenerator(CMakeGenerator.UnixMakefiles);
37+
return properties;
38+
}
39+
}

cmake/org.eclipse.cdt.cmake.core.tests/src/org/eclipse/cdt/cmake/core/internal/CMakePropertiesControllerTest.java

Lines changed: 0 additions & 103 deletions
This file was deleted.

cmake/org.eclipse.cdt.cmake.core/META-INF/MANIFEST.MF

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Manifest-Version: 1.0
22
Bundle-ManifestVersion: 2
33
Bundle-Name: %pluginName
44
Bundle-SymbolicName: org.eclipse.cdt.cmake.core;singleton:=true
5-
Bundle-Version: 1.6.0.qualifier
5+
Bundle-Version: 2.0.0.qualifier
66
Bundle-Activator: org.eclipse.cdt.cmake.core.internal.Activator
77
Bundle-Vendor: %providerName
88
Require-Bundle: org.eclipse.core.runtime,

0 commit comments

Comments
 (0)