1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-22 06:02:11 +02:00

Get CMake projects created and building. New C/C++ Project Wizard.

Fixed up CMake new project creation. Hooked up the CMake build config
properly.

Introducing the New C/C++ Project Wizard that hold all the new project
types: Arduino, Qt, CMake.

Change-Id: I9f9c8b1f51b136515fe226c15059cdbb99106275
This commit is contained in:
Doug Schaefer 2016-08-09 14:54:14 -04:00 committed by Gerrit Code Review @ Eclipse.org
parent 35921b4bbd
commit 20c4e5ba88
20 changed files with 240 additions and 192 deletions

View file

@ -7,10 +7,10 @@ Bundle-Activator: org.eclipse.cdt.cmake.core.internal.Activator
Bundle-Vendor: Eclipse CDT
Require-Bundle: org.eclipse.core.runtime,
org.eclipse.core.resources;bundle-version="3.11.0",
org.freemarker;bundle-version="2.3.22",
org.eclipse.debug.core;bundle-version="3.10.0",
org.eclipse.launchbar.core;bundle-version="2.0.0",
org.eclipse.cdt.core;bundle-version="5.12.0"
org.eclipse.cdt.core;bundle-version="5.12.0",
org.eclipse.tools.templates.freemarker;bundle-version="1.0.0";visibility:=reexport
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Bundle-ActivationPolicy: lazy
Export-Package: org.eclipse.cdt.cmake.core

View file

@ -9,23 +9,6 @@
class="org.eclipse.cdt.cmake.core.CMakeNature">
</run>
</runtime>
<builder
id="org.eclipse.cdt.cmake.core.cmakeBuilder">
</builder>
</extension>
<extension
id="cmakeBuilder"
name="CMake Builder"
point="org.eclipse.core.resources.builders">
<builder
callOnEmptyDelta="true"
hasNature="true"
isConfigurable="true"
supportsConfigurations="true">
<run
class="org.eclipse.cdt.cmake.core.internal.CMakeBuilder">
</run>
</builder>
</extension>
<extension
point="org.eclipse.launchbar.core.launchBarContributions">
@ -43,14 +26,6 @@
value="org.eclipse.cdt.cmake.core.cmakeNature">
</test>
</enablement>
<enablement>
<instanceof
value="org.eclipse.core.resources.IProject">
</instanceof>
<test
property="org.eclipse.core.resources.projectNature">
</test>
</enablement>
</descriptorType>
<configProvider
class="org.eclipse.cdt.cmake.core.internal.CMakeLocalLaunchConfigurationProvider"
@ -69,11 +44,11 @@
</launchConfigurationType>
</extension>
<extension
point="org.eclipse.cdt.core.ScannerInfoProvider2">
point="org.eclipse.cdt.core.buildConfigProvider">
<provider
builder="org.eclipse.cdt.cmake.core.cmakeBuilder"
class="org.eclipse.cdt.cmake.core.internal.CMakeScannerInfoProvider">
class="org.eclipse.cdt.cmake.core.internal.CMakeBuildConfigurationProvider"
id="org.eclipse.cdt.cmake.core.provider"
natureId="org.eclipse.cdt.cmake.core.cmakeNature">
</provider>
</extension>
</plugin>

View file

@ -7,59 +7,64 @@
*******************************************************************************/
package org.eclipse.cdt.cmake.core;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.eclipse.cdt.cmake.core.internal.CMakeTemplateGenerator;
import org.eclipse.cdt.cmake.core.internal.Activator;
import org.eclipse.cdt.core.CCProjectNature;
import org.eclipse.cdt.core.CProjectNature;
import org.eclipse.cdt.core.build.CBuilder;
import org.eclipse.cdt.core.model.CoreModel;
import org.eclipse.cdt.core.model.IPathEntry;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.ICommand;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.tools.templates.freemarker.FMProjectGenerator;
import org.eclipse.tools.templates.freemarker.SourceRoot;
import org.osgi.framework.Bundle;
public class CMakeProjectGenerator {
public class CMakeProjectGenerator extends FMProjectGenerator {
private final IProject project;
public CMakeProjectGenerator(IProject project) {
this.project = project;
public CMakeProjectGenerator(String manifestFile) {
super(manifestFile);
}
public void generate(IProgressMonitor monitor) throws CoreException {
// Generate the files
IFolder sourceFolder = project.getFolder("src"); //$NON-NLS-1$
if (!sourceFolder.exists()) {
sourceFolder.create(true, true, monitor);
@Override
protected void initProjectDescription(IProjectDescription description) {
description
.setNatureIds(
new String[] { CProjectNature.C_NATURE_ID, CCProjectNature.CC_NATURE_ID, CMakeNature.ID });
ICommand command = description.newCommand();
CBuilder.setupBuilder(command);
description.setBuildSpec(new ICommand[] { command });
}
@Override
public Bundle getSourceBundle() {
return Activator.getPlugin().getBundle();
}
@Override
public void generate(Map<String, Object> model, IProgressMonitor monitor) throws CoreException {
super.generate(model, monitor);
// Create the source folders
IProject project = getProject();
List<IPathEntry> entries = new ArrayList<>();
for (SourceRoot srcRoot : getManifest().getSrcRoots()) {
IFolder sourceFolder = project.getFolder(srcRoot.getDir());
if (!sourceFolder.exists()) {
sourceFolder.create(true, true, monitor);
}
entries.add(CoreModel.newSourceEntry(sourceFolder.getFullPath()));
}
CMakeTemplateGenerator templateGen = new CMakeTemplateGenerator();
Map<String, Object> fmModel = new HashMap<>();
fmModel.put("projectName", project.getName()); //$NON-NLS-1$
IFile sourceFile = sourceFolder.getFile("main.cpp"); //$NON-NLS-1$
templateGen.generateFile(fmModel, "simple/main.cpp", sourceFile, monitor); //$NON-NLS-1$
sourceFile = project.getFile("CMakeLists.txt"); //$NON-NLS-1$
templateGen.generateFile(fmModel, "simple/CMakeLists.txt", sourceFile, monitor); //$NON-NLS-1$
// Set up the project
IProjectDescription projDesc = project.getDescription();
String[] oldIds = projDesc.getNatureIds();
String[] newIds = new String[oldIds.length + 3];
System.arraycopy(oldIds, 0, newIds, 0, oldIds.length);
newIds[newIds.length - 3] = CProjectNature.C_NATURE_ID;
newIds[newIds.length - 2] = CCProjectNature.CC_NATURE_ID;
newIds[newIds.length - 1] = CMakeNature.ID;
projDesc.setNatureIds(newIds);
project.setDescription(projDesc, monitor);
IPathEntry[] entries = new IPathEntry[] { CoreModel.newOutputEntry(sourceFolder.getFullPath()) };
CoreModel.getDefault().create(project).setRawPathEntries(entries, monitor);
CoreModel.getDefault().create(project).setRawPathEntries(entries.toArray(new IPathEntry[entries.size()]),
monitor);
}
}

View file

@ -52,7 +52,7 @@ public class CMakeBuildConfiguration extends CBuildConfiguration {
if (!Files.exists(buildDir.resolve("Makefile"))) { //$NON-NLS-1$
// TODO assuming cmake is in the path here, probably need a
// preference in case it isn't.
List<String> command = Arrays.asList("cmake", //$NON-NLS-1$
List<String> command = Arrays.asList("/usr/local/bin/cmake", //$NON-NLS-1$
"-DCMAKE_EXPORT_COMPILE_COMMANDS=ON", new File(project.getLocationURI()).getAbsolutePath()); //$NON-NLS-1$
ProcessBuilder processBuilder = new ProcessBuilder(command).directory(buildDir.toFile());
Process process = processBuilder.start();

View file

@ -0,0 +1,64 @@
/*******************************************************************************
* Copyright (c) 2016 QNX Software Systems and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*******************************************************************************/
package org.eclipse.cdt.cmake.core.internal;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.cdt.core.build.ICBuildConfiguration;
import org.eclipse.cdt.core.build.ICBuildConfigurationProvider;
import org.eclipse.cdt.core.build.IToolChain;
import org.eclipse.cdt.core.build.IToolChainManager;
import org.eclipse.core.resources.IBuildConfiguration;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.Platform;
public class CMakeBuildConfigurationProvider implements ICBuildConfigurationProvider {
public static final String ID = "org.eclipse.cdt.cmake.core.provider"; //$NON-NLS-1$
@Override
public String getId() {
return ID;
}
@Override
public ICBuildConfiguration getCBuildConfiguration(IBuildConfiguration config, String name) throws CoreException {
if (config.getName().equals(IBuildConfiguration.DEFAULT_CONFIG_NAME)) {
IToolChain toolChain = null;
// try the toolchain for the local target
Map<String, String> properties = new HashMap<>();
properties.put(IToolChain.ATTR_OS, Platform.getOS());
properties.put(IToolChain.ATTR_ARCH, Platform.getOSArch());
IToolChainManager toolChainManager = Activator.getService(IToolChainManager.class);
for (IToolChain tc : toolChainManager.getToolChainsMatching(properties)) {
toolChain = tc;
break;
}
// local didn't work, try and find one that does
if (toolChain == null) {
for (IToolChain tc : toolChainManager.getToolChainsMatching(new HashMap<>())) {
toolChain = tc;
break;
}
}
if (toolChain != null) {
return new CMakeBuildConfiguration(config, name, toolChain);
} else {
// No valid combinations
return null;
}
} else {
return new CMakeBuildConfiguration(config, name);
}
}
}

View file

@ -1,62 +0,0 @@
/*******************************************************************************
* Copyright (c) 2015 QNX Software Systems and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*******************************************************************************/
package org.eclipse.cdt.cmake.core.internal;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Path;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
public class CMakeTemplateGenerator {
private final Configuration config;
public CMakeTemplateGenerator() throws CoreException {
config = new Configuration(Configuration.VERSION_2_3_22);
URL templateDirURL = FileLocator.find(Activator.getPlugin().getBundle(), new Path("/templates"), null); //$NON-NLS-1$
try {
config.setDirectoryForTemplateLoading(new File(FileLocator.toFileURL(templateDirURL).toURI()));
} catch (IOException | URISyntaxException e) {
throw new CoreException(Activator.errorStatus("Template configuration", e));
}
}
public void generateFile(final Object model, String templateFile, final IFile outputFile, IProgressMonitor monitor)
throws CoreException {
try {
final Template template = config.getTemplate(templateFile);
try (StringWriter writer = new StringWriter()) {
template.process(model, writer);
try (ByteArrayInputStream in = new ByteArrayInputStream(
writer.getBuffer().toString().getBytes(StandardCharsets.UTF_8))) {
if (outputFile.exists()) {
outputFile.setContents(in, true, true, monitor);
} else {
outputFile.create(in, true, monitor);
}
}
}
} catch (IOException | TemplateException e) {
throw new CoreException(Activator.errorStatus("Processing template " + templateFile, e));
}
}
}

View file

@ -1,3 +1,5 @@
cmake_minimum_required (VERSION 2.6)
project (${projectName})
add_executable(${projectName} src/main.cpp)
add_executable(${projectName} src/${projectName}.cpp)

View file

@ -0,0 +1,8 @@
<templateManifest>
<srcRoot dir="src"/>
<file src="/templates/simple/CMakeLists.txt"
dest="/${projectName}/CMakeLists.txt"/>
<file src="/templates/simple/main.cpp"
dest="/${projectName}/src/${projectName}.cpp"
open="true"/>
</templateManifest>

View file

@ -9,6 +9,7 @@ Require-Bundle: org.eclipse.core.runtime,
org.eclipse.core.resources;bundle-version="3.11.0",
org.eclipse.ui,
org.eclipse.ui.ide,
org.eclipse.cdt.cmake.core
org.eclipse.cdt.cmake.core,
org.eclipse.tools.templates.ui;bundle-version="1.1.0"
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Bundle-ActivationPolicy: lazy

Binary file not shown.

After

Width:  |  Height:  |  Size: 5 KiB

View file

@ -1,18 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>
<extension
point="org.eclipse.ui.newWizards">
<wizard
category="org.eclipse.cdt.ui.newCWizards"
class="org.eclipse.cdt.cmake.ui.internal.NewCMakeProjectWizard"
finalPerspective="org.eclipse.cdt.ui.CPerspective"
icon="icons/newcc_app.gif"
id="org.eclipse.cdt.cmake.ui.newProjectWizard"
name="CMake Project"
project="true">
</wizard>
</extension>
<extension
point="org.eclipse.ui.propertyPages">
<page
@ -32,5 +20,27 @@
</enabledWhen>
</page>
</extension>
<extension
point="org.eclipse.tools.templates.ui.templates">
<tag
id="org.eclipse.cdt.cmake.ui.tag"
label="CMake">
</tag>
<template
icon="icons/cmake_logo-main.png"
id="org.eclipse.cdt.cmake.ui.emptyProjectTemplate"
label="CMake Project"
wizard="org.eclipse.cdt.cmake.ui.internal.NewCMakeProjectWizard">
<description>
A CMake project with a Hello World executable to get started.
</description>
<tagReference
id="org.eclipse.cdt.ui.cdtTag">
</tagReference>
<tagReference
id="org.eclipse.cdt.cmake.ui.tag">
</tagReference>
</template>
</extension>
</plugin>

View file

@ -1,49 +1,40 @@
/*******************************************************************************
* Copyright (c) 2015 QNX Software Systems and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*******************************************************************************/
package org.eclipse.cdt.cmake.ui.internal;
import java.lang.reflect.InvocationTargetException;
import org.eclipse.cdt.cmake.core.CMakeProjectGenerator;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.ui.actions.WorkspaceModifyDelegatingOperation;
import org.eclipse.ui.wizards.newresource.BasicNewProjectResourceWizard;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.tools.templates.core.IGenerator;
import org.eclipse.tools.templates.ui.TemplateWizard;
import org.eclipse.ui.dialogs.WizardNewProjectCreationPage;
public class NewCMakeProjectWizard extends BasicNewProjectResourceWizard {
public class NewCMakeProjectWizard extends TemplateWizard {
private WizardNewProjectCreationPage mainPage;
@Override
public boolean performFinish() {
if (!super.performFinish()) {
return false;
}
IRunnableWithProgress op = new WorkspaceModifyDelegatingOperation(new IRunnableWithProgress() {
public void addPages() {
mainPage = new WizardNewProjectCreationPage("basicNewProjectPage") { //$NON-NLS-1$
@Override
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
try {
monitor.beginTask("Generating project", 1);
CMakeProjectGenerator generator = new CMakeProjectGenerator(getNewProject());
generator.generate(monitor);
monitor.done();
} catch (CoreException e) {
Activator.log(e);
}
public void createControl(Composite parent) {
super.createControl(parent);
createWorkingSetGroup((Composite) getControl(), getSelection(),
new String[] { "org.eclipse.ui.resourceWorkingSetPage" }); //$NON-NLS-1$
Dialog.applyDialogFont(getControl());
}
});
};
mainPage.setTitle("New Arduino Project"); //$NON-NLS-1$
mainPage.setDescription("Specify properties of new Arduino project."); //$NON-NLS-1$
this.addPage(mainPage);
}
try {
getContainer().run(false, true, op);
} catch (InvocationTargetException | InterruptedException e) {
return false;
@Override
protected IGenerator getGenerator() {
CMakeProjectGenerator generator = new CMakeProjectGenerator("templates/simple/manifest.xml"); //$NON-NLS-1$
generator.setProjectName(mainPage.getProjectName());
if (!mainPage.useDefaults()) {
generator.setLocationURI(mainPage.getLocationURI());
}
return true;
return generator;
}
}

View file

@ -122,6 +122,7 @@ Require-Bundle: org.eclipse.cdt.core;bundle-version="[5.2.0,7.0.0)",
org.eclipse.ui.views;bundle-version="[3.2.0,4.0.0)",
org.eclipse.ui.workbench.texteditor;bundle-version="[3.5.0,4.0.0)",
com.ibm.icu;bundle-version="4.4.2",
org.eclipse.e4.ui.css.swt.theme
org.eclipse.e4.ui.css.swt.theme,
org.eclipse.tools.templates.ui;bundle-version="1.1.0"
Bundle-ActivationPolicy: lazy
Bundle-RequiredExecutionEnvironment: JavaSE-1.8

View file

@ -534,8 +534,8 @@ Index.menu=Index
C.Cpp.Index.menu=C/C++ &Index
CDTWizard=CDT New Project Wizard
CDTproject=CDT Project
CDTproject.desc=Create a new language-neutral project
CDTproject=C/C++ Project
CDTproject.desc=Create a new C or C++ project
CPPproject=C++ Project
CPPproject.desc=Create a new C++ project
Cproject=C Project

View file

@ -458,6 +458,20 @@
%Cproject.desc
</description>
</wizard>
<wizard
canFinishEarly="false"
category="org.eclipse.cdt.ui.newCWizards"
class="org.eclipse.cdt.internal.ui.wizards.project.NewCDTProjectWizard"
finalPerspective="org.eclipse.cdt.ui.CPerspective"
hasPages="true"
icon="icons/elcl16/newmngc_app.gif"
id="org.eclipse.cdt.ui.wizard.project"
name="%CDTproject"
project="true">
<description>
%CDTproject.desc
</description>
</wizard>
</extension>
<!-- Define the document setup participant for the C/C++ and Assembly Editors -->
@ -4933,5 +4947,12 @@
</themeid>
</stylesheet>
</extension>
<extension
point="org.eclipse.tools.templates.ui.templates">
<tag
id="org.eclipse.cdt.ui.cdtTag"
label="C/C++">
</tag>
</extension>
</plugin>

View file

@ -0,0 +1,21 @@
/*******************************************************************************
* Copyright (c) 2016 QNX Software Systems and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*******************************************************************************/
package org.eclipse.cdt.internal.ui.wizards.project;
import org.eclipse.tools.templates.ui.NewWizard;
public class NewCDTProjectWizard extends NewWizard {
private static final String cdtTag = "org.eclipse.cdt.ui.cdtTag"; //$NON-NLS-1$
public NewCDTProjectWizard() {
super(cdtTag);
setTemplateSelectionPageTitle("Templates for New C/C++ Project");
}
}

View file

@ -264,6 +264,7 @@ Java and all Java-based trademarks are trademarks of Oracle Corporation in the U
<plugin id="org.eclipse.e4.ui.workbench"/>
<plugin id="org.eclipse.e4.ui.workbench.addons.swt"/>
<plugin id="org.eclipse.e4.ui.workbench.renderers.swt"/>
<plugin id="org.eclipse.e4.ui.workbench.renderers.swt.cocoa" fragment="true"/>
<plugin id="org.eclipse.e4.ui.workbench.swt"/>
<plugin id="org.eclipse.e4.ui.workbench3"/>
<plugin id="org.eclipse.ecf"/>
@ -312,6 +313,7 @@ Java and all Java-based trademarks are trademarks of Oracle Corporation in the U
<plugin id="org.eclipse.equinox.preferences"/>
<plugin id="org.eclipse.equinox.registry"/>
<plugin id="org.eclipse.equinox.security"/>
<plugin id="org.eclipse.equinox.security.macosx" fragment="true"/>
<plugin id="org.eclipse.equinox.security.ui"/>
<plugin id="org.eclipse.equinox.simpleconfigurator"/>
<plugin id="org.eclipse.equinox.simpleconfigurator.manipulator"/>
@ -345,7 +347,10 @@ Java and all Java-based trademarks are trademarks of Oracle Corporation in the U
<plugin id="org.eclipse.team.core"/>
<plugin id="org.eclipse.team.ui"/>
<plugin id="org.eclipse.text"/>
<plugin id="org.eclipse.tools.templates.core"/>
<plugin id="org.eclipse.tools.templates.ui"/>
<plugin id="org.eclipse.ui"/>
<plugin id="org.eclipse.ui.cocoa" fragment="true"/>
<plugin id="org.eclipse.ui.console"/>
<plugin id="org.eclipse.ui.editors"/>
<plugin id="org.eclipse.ui.forms"/>

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.2 KiB

View file

@ -131,16 +131,19 @@
label="Qt">
</tag>
<template
icon="icons/HelloWorld48.png"
icon="icons/qt_logo.png"
id="org.eclipse.cdt.qt.ui.template.helloWorld"
label="C++/QML HelloWorld"
label="Qt C++/QML Application"
wizard="org.eclipse.cdt.internal.qt.ui.wizards.HelloWorldWizard">
<description>
A simple Hello World Qt Application with main function in C++ and the UI in QML.
</description>
<tagReference
id="org.eclipse.cdt.qt.ui.tag">
</tagReference>
<description>
A simple Hello World App with main function in C++ and the UI in QML.
</description>
<tagReference
id="org.eclipse.cdt.ui.cdtTag">
</tagReference>
</template>
</extension>
<extension

View file

@ -142,12 +142,15 @@
id="org.eclipse.cdt.arduino.ui.template.sketch"
label="Arduino C++ Sketch"
wizard="org.eclipse.cdt.arduino.ui.internal.project.NewArduinoCPPSketchWizard">
<tagReference
id="org.eclipse.cdt.arduino.ui.tag">
</tagReference>
<description>
A single C++ file with empty setup() and loop() functions.
</description>
<tagReference
id="org.eclipse.cdt.arduino.ui.tag">
</tagReference>
<tagReference
id="org.eclipse.cdt.ui.cdtTag">
</tagReference>
</template>
</extension>
<extension