|
| 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 | +} |
0 commit comments