|
| 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.hamcrest.Matchers.nullValue; |
| 17 | +import static org.mockito.Mockito.mock; |
| 18 | +import static org.mockito.Mockito.when; |
| 19 | + |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.HashMap; |
| 22 | +import java.util.List; |
| 23 | +import java.util.Map; |
| 24 | + |
| 25 | +import org.eclipse.cdt.cmake.core.properties.CMakeGenerator; |
| 26 | +import org.eclipse.cdt.cmake.core.properties.ICMakeGenerator; |
| 27 | +import org.eclipse.cdt.cmake.core.properties.ICMakeProperties; |
| 28 | +import org.eclipse.cdt.core.CCProjectNature; |
| 29 | +import org.eclipse.cdt.core.CProjectNature; |
| 30 | +import org.eclipse.cdt.core.build.IToolChain; |
| 31 | +import org.eclipse.cdt.core.testplugin.ResourceHelper; |
| 32 | +import org.eclipse.cdt.core.testplugin.util.BaseTestCase5; |
| 33 | +import org.eclipse.cdt.utils.CommandLineUtil; |
| 34 | +import org.eclipse.core.resources.IBuildConfiguration; |
| 35 | +import org.eclipse.core.resources.IProject; |
| 36 | +import org.eclipse.core.resources.IProjectDescription; |
| 37 | +import org.junit.jupiter.api.BeforeEach; |
| 38 | +import org.junit.jupiter.api.Test; |
| 39 | + |
| 40 | +/** |
| 41 | + * Tests a new API added to the CMake Build Configuration which allows default CMake properties to be set. |
| 42 | + * See the new interface {@link ICMakeBuildConfiguration}. |
| 43 | + */ |
| 44 | +public class CMakeBuildConfigurationTests extends BaseTestCase5 { |
| 45 | + private IBuildConfiguration buildConfig; |
| 46 | + private IToolChain mockToolchain; |
| 47 | + |
| 48 | + @BeforeEach |
| 49 | + public void setup() throws Exception { |
| 50 | + // Create a CMake project |
| 51 | + IProject project = createCMakeProject(); |
| 52 | + // Get the default build config from the project (it always has one) |
| 53 | + buildConfig = project.getBuildConfig(IBuildConfiguration.DEFAULT_CONFIG_NAME); |
| 54 | + // Setup a toolchain ready to use for creating the valid ICBuildConfiguration |
| 55 | + mockToolchain = mock(IToolChain.class); |
| 56 | + when(mockToolchain.getProperty(IToolChain.ATTR_OS)).thenReturn("osDummy"); |
| 57 | + when(mockToolchain.getProperty(IToolChain.ATTR_ARCH)).thenReturn("archDummy"); |
| 58 | + when(mockToolchain.getTypeId()).thenReturn("tc_typeId"); |
| 59 | + when(mockToolchain.getId()).thenReturn("tcId"); |
| 60 | + when(mockToolchain.getBuildConfigNameFragment()).thenReturn("buildConfigName"); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Test for {@link ICMakeProperties#setGenerator()}. |
| 65 | + * |
| 66 | + * This test also verifies that what the ISV overrides in getCMakeProperties is what takes effect. |
| 67 | + */ |
| 68 | + @Test |
| 69 | + public void getCMakePropertiesTestSetGenerator() throws Exception { |
| 70 | + CMakeBuildConfiguration cmBuildConfig = new CMakeBuildConfiguration(buildConfig, "cmBuildConfigName", |
| 71 | + mockToolchain) { |
| 72 | + |
| 73 | + @Override |
| 74 | + public ICMakeProperties getCMakeProperties() { |
| 75 | + ICMakeProperties properties = super.getCMakeProperties(); |
| 76 | + properties.setGenerator(CMakeGenerator.WatcomWMake); |
| 77 | + return properties; |
| 78 | + } |
| 79 | + }; |
| 80 | + |
| 81 | + // Call the new method on ICMakeBuildConfiguration to get the default CMake properties. |
| 82 | + ICMakeProperties cMakeProperties = cmBuildConfig.getCMakeProperties(); |
| 83 | + assertThat(cMakeProperties.getGenerator(), is(CMakeGenerator.WatcomWMake)); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Test for {@link ICMakeProperties#setExtraArguments()} |
| 88 | + * |
| 89 | + * This test also verifies that what the ISV overrides in getCMakeProperties is what takes effect. |
| 90 | + */ |
| 91 | + @Test |
| 92 | + public void getCMakePropertiesTestSetExtraArguments() throws Exception { |
| 93 | + // Create a C Build Configuration using the default build config and an arbitrary name |
| 94 | + CMakeBuildConfiguration cmBuildConfig = new CMakeBuildConfiguration(buildConfig, "cmBuildConfigName", |
| 95 | + mockToolchain) { |
| 96 | + |
| 97 | + @Override |
| 98 | + public ICMakeProperties getCMakeProperties() { |
| 99 | + ICMakeProperties properties = super.getCMakeProperties(); |
| 100 | + properties.setExtraArguments( |
| 101 | + new ArrayList<>((List.of("-DplatformAgnosticArgsTest0=0", "-DplatformAgnosticArgsTest1=1")))); |
| 102 | + return properties; |
| 103 | + } |
| 104 | + }; |
| 105 | + // Call the new method on ICMakeBuildConfiguration to get the default CMake properties. |
| 106 | + ICMakeProperties cMakeProperties = cmBuildConfig.getCMakeProperties(); |
| 107 | + List<String> extraArguments = cMakeProperties.getExtraArguments(); |
| 108 | + assertThat(extraArguments, contains("-DplatformAgnosticArgsTest0=0", "-DplatformAgnosticArgsTest1=1")); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Test for {@link CMakeBuildConfiguration#getDefaultProperties()} |
| 113 | + */ |
| 114 | + @Test |
| 115 | + public void getDefaultProperties() throws Exception { |
| 116 | + // Create a C Build Configuration using the default build config and an arbitrary name |
| 117 | + CMakeBuildConfiguration cmBuildConfig = new CMakeBuildConfiguration(buildConfig, "cmBuildConfigName", |
| 118 | + mockToolchain) { |
| 119 | + |
| 120 | + @Override |
| 121 | + public Map<String, String> getDefaultProperties() { |
| 122 | + var defs = new HashMap<>(super.getDefaultProperties()); |
| 123 | + defs.put(CMAKE_GENERATOR, CMakeGenerator.WatcomWMake.getCMakeName()); |
| 124 | + return defs; |
| 125 | + } |
| 126 | + }; |
| 127 | + // Call the new method on ICMakeBuildConfiguration to get the default CMake properties. |
| 128 | + ICMakeProperties cMakeProperties = cmBuildConfig.getCMakeProperties(); |
| 129 | + assertThat(cMakeProperties.getGenerator(), is(CMakeGenerator.WatcomWMake)); |
| 130 | + } |
| 131 | + |
| 132 | + @Test |
| 133 | + public void getDefaultPropertiesTestExtraArgs() throws Exception { |
| 134 | + // Create a C Build Configuration using the default build config and an arbitrary name |
| 135 | + CMakeBuildConfiguration cmBuildConfig = new CMakeBuildConfiguration(buildConfig, "cmBuildConfigName", |
| 136 | + mockToolchain) { |
| 137 | + @Override |
| 138 | + public Map<String, String> getDefaultProperties() { |
| 139 | + var defs = new HashMap<>(super.getDefaultProperties()); |
| 140 | + defs.put(CMAKE_ARGUMENTS, "-Dtest0=0 -Dtest1=1"); |
| 141 | + return defs; |
| 142 | + } |
| 143 | + }; |
| 144 | + // Call the new method on ICMakeBuildConfiguration to get the default CMake properties. |
| 145 | + ICMakeProperties cMakeProperties = cmBuildConfig.getCMakeProperties(); |
| 146 | + List<String> extraArguments = cMakeProperties.getExtraArguments(); |
| 147 | + assertThat(extraArguments, contains("-Dtest0=0", "-Dtest1=1")); |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * Test that a custom cmake generator can be entered and auto-created |
| 152 | + */ |
| 153 | + @Test |
| 154 | + public void customCMakeGeneratorEntryAuto() throws Exception { |
| 155 | + // Create a C Build Configuration using the default build config and an arbitrary name |
| 156 | + CMakeBuildConfiguration cmBuildConfig = new CMakeBuildConfiguration(buildConfig, "cmBuildConfigName", |
| 157 | + mockToolchain) { |
| 158 | + @Override |
| 159 | + public Map<String, String> getDefaultProperties() { |
| 160 | + var defs = new HashMap<>(super.getDefaultProperties()); |
| 161 | + // A custom generator for a custom cmake version |
| 162 | + defs.put(CMAKE_GENERATOR, "My Personal Generator"); |
| 163 | + return defs; |
| 164 | + } |
| 165 | + }; |
| 166 | + |
| 167 | + // Call the new method on ICMakeBuildConfiguration to get the default CMake properties. |
| 168 | + ICMakeProperties cMakeProperties = cmBuildConfig.getCMakeProperties(); |
| 169 | + assertThat(cMakeProperties.getGenerator().getCMakeName(), is("My Personal Generator")); |
| 170 | + assertThat(cMakeProperties.getGenerator().getIgnoreErrOption(), is(nullValue())); |
| 171 | + assertThat(cMakeProperties.getGenerator().getMakefileName(), is(nullValue())); |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * Test that a custom cmake generator can be entered and manually-created |
| 176 | + */ |
| 177 | + @Test |
| 178 | + public void customCMakeGeneratorEntryManual() throws Exception { |
| 179 | + // Create a C Build Configuration using the default build config and an arbitrary name |
| 180 | + CMakeBuildConfiguration cmBuildConfig = new CMakeBuildConfiguration(buildConfig, "cmBuildConfigName", |
| 181 | + mockToolchain) { |
| 182 | + @Override |
| 183 | + public Map<String, String> getDefaultProperties() { |
| 184 | + var defs = new HashMap<>(super.getDefaultProperties()); |
| 185 | + // A custom generator for a custom cmake version |
| 186 | + defs.put(CMAKE_GENERATOR, "My Personal Generator"); |
| 187 | + return defs; |
| 188 | + } |
| 189 | + |
| 190 | + @Override |
| 191 | + public ICMakeProperties getCMakeProperties() { |
| 192 | + ICMakeProperties properties = super.getCMakeProperties(); |
| 193 | + if ("My Personal Generator".equals(properties.getGenerator().getCMakeName())) { |
| 194 | + var generator = new ICMakeGenerator() { |
| 195 | + @Override |
| 196 | + public String getMakefileName() { |
| 197 | + return "MyMak.mak"; |
| 198 | + } |
| 199 | + |
| 200 | + @Override |
| 201 | + public String getIgnoreErrOption() { |
| 202 | + return "-mycustom"; |
| 203 | + } |
| 204 | + |
| 205 | + @Override |
| 206 | + public String getCMakeName() { |
| 207 | + return "My Personal Generator"; |
| 208 | + } |
| 209 | + }; |
| 210 | + properties.setGenerator(generator); |
| 211 | + } |
| 212 | + return properties; |
| 213 | + } |
| 214 | + }; |
| 215 | + |
| 216 | + // Call the new method on ICMakeBuildConfiguration to get the default CMake properties. |
| 217 | + ICMakeProperties cMakeProperties = cmBuildConfig.getCMakeProperties(); |
| 218 | + assertThat(cMakeProperties.getGenerator().getCMakeName(), is("My Personal Generator")); |
| 219 | + assertThat(cMakeProperties.getGenerator().getIgnoreErrOption(), is("-mycustom")); |
| 220 | + assertThat(cMakeProperties.getGenerator().getMakefileName(), is("MyMak.mak")); |
| 221 | + } |
| 222 | + |
| 223 | + /** |
| 224 | + * Test all and clean targets and cmake command have working defaults |
| 225 | + */ |
| 226 | + @Test |
| 227 | + public void targetsAndCommandDefaults() throws Exception { |
| 228 | + // Create a C Build Configuration using the default build config and an arbitrary name |
| 229 | + CMakeBuildConfiguration cmBuildConfig = new CMakeBuildConfiguration(buildConfig, "cmBuildConfigName", |
| 230 | + mockToolchain); |
| 231 | + |
| 232 | + // Call the new method on ICMakeBuildConfiguration to get the default CMake properties. |
| 233 | + ICMakeProperties cMakeProperties = cmBuildConfig.getCMakeProperties(); |
| 234 | + assertThat(cMakeProperties.getCommand(), is("cmake")); |
| 235 | + assertThat(cMakeProperties.getAllTarget(), is("all")); |
| 236 | + assertThat(cMakeProperties.getCleanTarget(), is("clean")); |
| 237 | + } |
| 238 | + |
| 239 | + /** |
| 240 | + * Test all and clean targets and cmake command can be overridden |
| 241 | + */ |
| 242 | + @Test |
| 243 | + public void targetsAndCommand() throws Exception { |
| 244 | + // Create a C Build Configuration using the default build config and an arbitrary name |
| 245 | + CMakeBuildConfiguration cmBuildConfig = new CMakeBuildConfiguration(buildConfig, "cmBuildConfigName", |
| 246 | + mockToolchain) { |
| 247 | + @Override |
| 248 | + public Map<String, String> getDefaultProperties() { |
| 249 | + var defs = new HashMap<>(super.getDefaultProperties()); |
| 250 | + defs.put(CMAKE_BUILD_COMMAND, "mycmake"); |
| 251 | + defs.put(CMAKE_ALL_TARGET, "myall"); |
| 252 | + defs.put(CMAKE_CLEAN_TARGET, "myclean"); |
| 253 | + return defs; |
| 254 | + } |
| 255 | + }; |
| 256 | + |
| 257 | + // Call the new method on ICMakeBuildConfiguration to get the default CMake properties. |
| 258 | + ICMakeProperties cMakeProperties = cmBuildConfig.getCMakeProperties(); |
| 259 | + assertThat(cMakeProperties.getCommand(), is("mycmake")); |
| 260 | + assertThat(cMakeProperties.getAllTarget(), is("myall")); |
| 261 | + assertThat(cMakeProperties.getCleanTarget(), is("myclean")); |
| 262 | + } |
| 263 | + |
| 264 | + /** |
| 265 | + * Test that extra arguments parse correctly, e.g. handles ". |
| 266 | + * |
| 267 | + * Note that this test is minimal here as the real functionality is in {@link CommandLineUtil} |
| 268 | + * and all the special cases are tested in CommandLineUtilTest. |
| 269 | + */ |
| 270 | + @Test |
| 271 | + public void extraArgumentsParseCorrectly() throws Exception { |
| 272 | + // Create a C Build Configuration using the default build config and an arbitrary name |
| 273 | + CMakeBuildConfiguration cmBuildConfig = new CMakeBuildConfiguration(buildConfig, "cmBuildConfigName", |
| 274 | + mockToolchain) { |
| 275 | + @Override |
| 276 | + public Map<String, String> getDefaultProperties() { |
| 277 | + var defs = new HashMap<>(super.getDefaultProperties()); |
| 278 | + defs.put(CMAKE_ARGUMENTS, "-Da=\"something with space and quotes\" \"-Danother=quoted\""); |
| 279 | + return defs; |
| 280 | + } |
| 281 | + }; |
| 282 | + |
| 283 | + // Call the new method on ICMakeBuildConfiguration to get the default CMake properties. |
| 284 | + ICMakeProperties cMakeProperties = cmBuildConfig.getCMakeProperties(); |
| 285 | + assertThat(cMakeProperties.getExtraArguments(), |
| 286 | + is(List.of("-Da=something with space and quotes", "-Danother=quoted"))); |
| 287 | + } |
| 288 | + |
| 289 | + private IProject createCMakeProject() throws Exception { |
| 290 | + // Create a plain Eclipse project |
| 291 | + IProject project = ResourceHelper.createProject(this.getName()); |
| 292 | + // Add C/C++ and CMake natures to make it a CMake project |
| 293 | + IProjectDescription description = project.getDescription(); |
| 294 | + description.setNatureIds( |
| 295 | + new String[] { CProjectNature.C_NATURE_ID, CCProjectNature.CC_NATURE_ID, CMakeNature.ID }); |
| 296 | + project.setDescription(description, null); |
| 297 | + return project; |
| 298 | + } |
| 299 | +} |
0 commit comments