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 committed Jan 20, 2025
1 parent 1abb990 commit 9ec634a
Show file tree
Hide file tree
Showing 15 changed files with 223 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.eclipse.cdt.cmake.core;

import org.eclipse.cdt.cmake.core.properties.ICMakeProperties;
import org.eclipse.core.resources.IBuildConfiguration;
import org.eclipse.core.runtime.CoreException;
import org.junit.Test;

public class CMakeBuildConfigurationTests {

/**
* TODO: TBC
*/
@Test
public void getDefaultCMakeProperties00() {

org.eclipse.cdt.cmake.core.properties.CMakePropertiesFactory.createProperties();

}

private class CMakeBuildConfigurationExtended extends CMakeBuildConfiguration {

public CMakeBuildConfigurationExtended(IBuildConfiguration config, String name) throws CoreException {
super(config, name);
// TODO Auto-generated constructor stub
}

@Override
public ICMakeProperties getDefaultCMakeProperties() {
// get the built-in CDT defaults
ICMakeProperties properties = super.getDefaultCMakeProperties();

// provide my customizations (I don't know what Renesas/extenders may actually want to do here)
properties.setWarnUnitialized(true);
return properties;
}
}
}
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
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.eclipse.cdt.cmake.core.internal.CommandDescriptorBuilder.CommandDescriptor;
import org.eclipse.cdt.cmake.core.internal.IOsOverridesSelector;
import org.eclipse.cdt.cmake.core.properties.CMakeGenerator;
import org.eclipse.cdt.cmake.core.properties.CMakePropertiesFactory;
import org.eclipse.cdt.cmake.core.properties.ICMakeProperties;
import org.eclipse.cdt.cmake.core.properties.ICMakePropertiesController;
import org.eclipse.cdt.cmake.core.properties.IOsOverrides;
Expand Down Expand Up @@ -72,9 +73,9 @@
import org.osgi.service.prefs.Preferences;

/**
* @since 1.6
* @since 2.0
*/
public class CMakeBuildConfiguration extends CBuildConfiguration {
public class CMakeBuildConfiguration extends CBuildConfiguration implements ICMakeBuildConfiguration {

public static final String CMAKE_USE_UI_OVERRIDES = "cmake.use.ui.overrides"; //$NON-NLS-1$
public static final boolean CMAKE_USE_UI_OVERRIDES_DEFAULT = false;
Expand Down Expand Up @@ -124,6 +125,19 @@ public CMakeBuildConfiguration(IBuildConfiguration config, String name, IToolCha
this.toolChainFile = toolChainFile;
}

@Override
public ICMakeProperties getDefaultCMakeProperties() {
ICMakeProperties properties = CMakePropertiesFactory.createProperties();
IOsOverrides windowsOverrides = properties.getWindowsOverrides();
windowsOverrides.setDefaultGenerator(CMakeGenerator.Ninja);

IOsOverrides linuxOverrides = properties.getLinuxOverrides();
linuxOverrides.setDefaultGenerator(CMakeGenerator.Ninja);
// reset(true) causes the CMake generator to be reset to it's default value.
properties.reset(true);
return properties;
}

/**
* Gets the tool-chain description file to pass to the cmake command-line.
*
Expand Down Expand Up @@ -162,7 +176,10 @@ public IProject[] build(int kind, Map<String, String> args, IConsole console, IP
runCMake = true;
}

ICMakeProperties cmakeProperties = getPropertiesController().load();
ICMakePropertiesController propertiesController = getPropertiesController();
ICMakeProperties cmakeProperties = propertiesController.load(getDefaultCMakeProperties());
// propertiesController.save(cmakeProperties);

runCMake |= !Files.exists(buildDir.resolve("CMakeCache.txt")); //$NON-NLS-1$

// Causes CMAKE_BUILD_TYPE to be set according to the launch mode
Expand Down Expand Up @@ -303,7 +320,7 @@ public void clean(IConsole console, IProgressMonitor monitor) throws CoreExcepti

project.deleteMarkers(ICModelMarker.C_MODEL_PROBLEM_MARKER, false, IResource.DEPTH_INFINITE);

ICMakeProperties cmakeProperties = getPropertiesController().load();
ICMakeProperties cmakeProperties = getPropertiesController().load(getDefaultCMakeProperties());
CommandDescriptorBuilder cmdBuilder = new CommandDescriptorBuilder(cmakeProperties,
new SimpleOsOverridesSelector());
CommandDescriptor command = cmdBuilder.makeCMakeBuildCommandline(getCleanCommand());
Expand Down Expand Up @@ -405,7 +422,8 @@ private ICMakePropertiesController getPropertiesController() {
@SuppressWarnings("unchecked")
public <T> T getAdapter(Class<T> adapter) {
if (ICMakePropertiesController.class.equals(adapter)) {
return (T) pc;
// return (T) pc;
return (T) getPropertiesController();
}
return super.getAdapter(adapter);
}
Expand Down Expand Up @@ -582,6 +600,14 @@ public void shutdown() {
}
} // CMakeIndexerInfoConsumer

private IOsOverrides getPlatformOsOverrides(ICMakeProperties cmakeProperties) {
if (Platform.OS_WIN32.equals(Platform.getOS())) {
return cmakeProperties.getWindowsOverrides();
} else {
return cmakeProperties.getLinuxOverrides();
}
}

/**
* Supports OS overrides and also User Interface (UI) overrides, provided in the CMakeBuildTab. The UI
* overrides are stored in the intermediary CBuildConfiguration Preferences (CBuildConfiguration.getSettings())
Expand All @@ -591,17 +617,9 @@ private class SimpleOsOverridesSelector implements IOsOverridesSelector {

@Override
public IOsOverrides getOsOverrides(ICMakeProperties cmakeProperties) {
IOsOverrides overrides;
// get overrides. Simplistic approach ATM, probably a strategy might fit better.
// see comment in CMakeIndexerInfoConsumer#getFileForCMakePath()
final String os = Platform.getOS();
if (Platform.OS_WIN32.equals(os)) {
overrides = cmakeProperties.getWindowsOverrides();
} else {
// fall back to linux, if OS is unknown
overrides = cmakeProperties.getLinuxOverrides();
}
return getUiOrOsOverrides(overrides);
return getUiOrOsOverrides(getPlatformOsOverrides(cmakeProperties));
}

private IOsOverrides getUiOrOsOverrides(IOsOverrides osOverrides) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*******************************************************************************
* 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 org.eclipse.cdt.cmake.core.properties.ICMakeProperties;

/**
* @since 2.0
*/
public interface ICMakeBuildConfiguration {

/**
* Provides a set of default CMake properties. The ICMakeProperties can then, for
* example, provide a default value for the CMake generator.
* @return A ICMakeProperties object containing default values. Must not be null.
*/
ICMakeProperties getDefaultCMakeProperties();
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* @noextend This class is not intended to be subclassed by clients.
* @noinstantiate This class is not intended to be instantiated by clients.
* @noreference This class is not intended to be referenced by clients.
* @since 1.6
* @since 2.0
*/
public class Messages extends NLS {
private static final String BUNDLE_NAME = "org.eclipse.cdt.cmake.core.messages"; //$NON-NLS-1$
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,39 @@ public ICMakeProperties load() throws IOException {
return props;
}

@Override
public ICMakeProperties load(ICMakeProperties defaults) throws IOException {
ICMakeProperties props = null;
if (Files.exists(storageFile)) {
try (InputStream is = Files.newInputStream(storageFile)) {
var classLoader = this.getClass().getClassLoader();

var loaderoptions = new LoaderOptions();
TagInspector taginspector = tag -> tag.getClassName().equals(CMakePropertiesBean.class.getName());
loaderoptions.setTagInspector(taginspector);

var clConstructor = new CustomClassLoaderConstructor(classLoader, loaderoptions);

props = new Yaml(clConstructor).loadAs(is, CMakePropertiesBean.class);
// props is null here if if no document was available in the file
}
}
if (props == null) {
// nothing was persisted, return default properties
if (defaults != null) {
// Use extender's defaults
props = defaults;
}
}
if (props == null) {
// Use system defaults
props = new CMakePropertiesBean();
}

setupModifyDetection(props);
return props;
}

@Override
public void save(ICMakeProperties properties) throws IOException {
// detect whether changes force us to delete file CMakeCache.txt to avoid complaints by cmake
Expand Down Expand Up @@ -158,4 +191,5 @@ private boolean hasExtraArgumentChanged(List<String> expected, List<String> actu
}
return false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import java.util.List;

import org.eclipse.cdt.cmake.core.properties.ICMakeProperties;
import org.eclipse.cdt.cmake.core.properties.LinuxOverrides;
import org.eclipse.cdt.cmake.core.properties.WindowsOverrides;

/**
* Holds project Properties for cmake. Follows the Java-Beans pattern to make (de-)serialization easier.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,28 @@
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/

package org.eclipse.cdt.cmake.core.internal.properties;
package org.eclipse.cdt.cmake.core.properties;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import org.eclipse.cdt.cmake.core.CMakeBuildConfiguration;
import org.eclipse.cdt.cmake.core.properties.CMakeGenerator;
import org.eclipse.cdt.cmake.core.properties.IOsOverrides;

/**
* Preferences that override/augment the generic properties when running under a
* specific OS.
*
* @author Martin Weber
* @since 2.0
*/
public abstract class AbstractOsOverrides implements IOsOverrides {

private String command;
private boolean useDefaultCommand;
private CMakeGenerator generator;
private List<String> extraArguments = new ArrayList<>(0);
private CMakeGenerator defaultGenerator = CMakeGenerator.Ninja;

/**
* Creates a new object, initialized with all default values.
Expand All @@ -45,7 +45,7 @@ public AbstractOsOverrides() {
public void reset() {
setCommand(CMakeBuildConfiguration.BUILD_COMMAND_DEFAULT);
useDefaultCommand = true;
setGenerator(CMakeGenerator.UnixMakefiles);
setGenerator(defaultGenerator);
extraArguments.clear();
}

Expand Down Expand Up @@ -89,4 +89,8 @@ public void setExtraArguments(List<String> extraArguments) {
this.extraArguments = extraArguments;
}

}
@Override
public void setDefaultGenerator(CMakeGenerator defaultGenerator) {
this.defaultGenerator = defaultGenerator;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*******************************************************************************
* 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.properties;

import org.eclipse.cdt.cmake.core.internal.properties.CMakePropertiesBean;

/**
* @since 2.0
*/
public class CMakePropertiesFactory {
public static ICMakeProperties createProperties() {
return new CMakePropertiesBean();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,17 @@ public interface ICMakePropertiesController {
*
* @throws IOException if the persistence store exists but could not be read
*/
@Deprecated
ICMakeProperties load() throws IOException;

/** Creates a new {@code ICMakeProperties} object, initialized from the persistence store.
* If the persistence store does not exist, an object initialized to the default values is returned.
*
* @throws IOException if the persistence store exists but could not be read
* @since 2.0
*/
ICMakeProperties load(ICMakeProperties defaults) throws IOException;

/** Saves the specified {@code ICMakeProperties} object to the persistence store.
*
* @throws IOException if the persistence store could not be written to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,11 @@ public interface IOsOverrides {
* Sets the list of extra arguments to pass on the cmake command-line.
*/
void setExtraArguments(List<String> extraArguments);

/**
* Sets a value that is used when a {@link ICMakeProperties#reset(true)} call is performed, setting the value
* of the CMake generator (see {@link #getGenerator()}) to it's default value.
* @since 2.0
*/
void setDefaultGenerator(CMakeGenerator defaultGenerator);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,18 @@
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/

package org.eclipse.cdt.cmake.core.internal.properties;
package org.eclipse.cdt.cmake.core.properties;

/**
* Preferences that override/augment the generic properties when running under
* Linux.
*
* @author Martin Weber
* @since 2.0
*/
public class LinuxOverrides extends AbstractOsOverrides {

/**
* Creates a new object, initialized with all default values.
*/
public LinuxOverrides() {
super();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,18 @@
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/

package org.eclipse.cdt.cmake.core.internal.properties;

import org.eclipse.cdt.cmake.core.properties.CMakeGenerator;
package org.eclipse.cdt.cmake.core.properties;

/**
* Preferences that override/augment the generic properties when running under
* Windows.
*
* @author Martin Weber
* @since 2.0
*/
public class WindowsOverrides extends AbstractOsOverrides {

/** Overridden to set a sensible generator. */
@Override
public void reset() {
super.reset();
setGenerator(CMakeGenerator.MinGWMakefiles);
public WindowsOverrides() {
super();
}
}
Loading

0 comments on commit 9ec634a

Please sign in to comment.