Skip to content

Commit

Permalink
Add API to set CMake generator default (eg Ninja)
Browse files Browse the repository at this point in the history
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
betamaxbandit authored and jonahgraham committed Jan 23, 2025
1 parent 5e62200 commit afb8c41
Show file tree
Hide file tree
Showing 19 changed files with 428 additions and 404 deletions.
5 changes: 5 additions & 0 deletions NewAndNoteworthy/CHANGELOG-API.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ This section describes API removals that occurred in past releases, and upcoming

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

## API Changes in CDT 12.0.
### org.eclipse.cdt.cmake.core.properties.ICMakePropertiesController removed

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.

## API Changes in CDT 11.5.

### org.eclipse.cdt.make.ui.dialogs.DiscoveredPathContainerPage removed
Expand Down
7 changes: 5 additions & 2 deletions cmake/org.eclipse.cdt.cmake.core.tests/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ Bundle-SymbolicName: org.eclipse.cdt.cmake.core.tests
Bundle-Version: 1.0.0.qualifier
Fragment-Host: org.eclipse.cdt.cmake.core;bundle-version="1.5.0"
Import-Package: org.assertj.core.api;version="[3.24.2,4.0.0)",
org.junit.jupiter.api;version="[5.9.3,6.0.0)"
org.junit.jupiter.api;version="[5.9.3,6.0.0)",
org.mockito;version="[5.15.0,6.0.0)",
org.mockito.stubbing;version="[5.15.0,6.0.0)"
Automatic-Module-Name: org.eclipse.cdt.cmake.core.tests
Bundle-Vendor: %Bundle-Vendor
Bundle-Copyright: %Bundle-Copyright
Require-Bundle: org.junit
Require-Bundle: org.junit,
org.eclipse.cdt.core.tests;bundle-version="[5.4.0,6.0.0)"

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;
}
}
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;
}
}

This file was deleted.

2 changes: 1 addition & 1 deletion cmake/org.eclipse.cdt.cmake.core/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: %pluginName
Bundle-SymbolicName: org.eclipse.cdt.cmake.core;singleton:=true
Bundle-Version: 1.6.0.qualifier
Bundle-Version: 2.0.0.qualifier
Bundle-Activator: org.eclipse.cdt.cmake.core.internal.Activator
Bundle-Vendor: %providerName
Require-Bundle: org.eclipse.core.runtime,
Expand Down
Loading

0 comments on commit afb8c41

Please sign in to comment.