mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-29 19:45:01 +02:00
Hellow World template for Makefile Project
This commit is contained in:
parent
13968f3102
commit
9d428201c3
8 changed files with 405 additions and 3 deletions
|
@ -543,6 +543,11 @@
|
|||
<simple name="path"/>
|
||||
</baseType>
|
||||
</complexArray>
|
||||
</processType>
|
||||
<processType
|
||||
name="GenerateMakefileWithBuildDescription"
|
||||
processRunner="org.eclipse.cdt.managedbuilder.templateengine.processes.GenerateMakefileWithBuildDescription">
|
||||
<simple name="projectName"/>
|
||||
</processType>
|
||||
</extension>
|
||||
|
||||
|
|
|
@ -0,0 +1,236 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2007 Intel Corporation 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
|
||||
*
|
||||
* Contributors:
|
||||
* Intel Corporation - Initial API and implementation
|
||||
*******************************************************************************/
|
||||
package org.eclipse.cdt.managedbuilder.internal.buildmodel;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.io.Writer;
|
||||
|
||||
import org.eclipse.cdt.core.settings.model.util.CDataUtil;
|
||||
import org.eclipse.cdt.managedbuilder.buildmodel.BuildDescriptionManager;
|
||||
import org.eclipse.cdt.managedbuilder.buildmodel.IBuildCommand;
|
||||
import org.eclipse.cdt.managedbuilder.buildmodel.IBuildDescription;
|
||||
import org.eclipse.cdt.managedbuilder.buildmodel.IBuildResource;
|
||||
import org.eclipse.cdt.managedbuilder.buildmodel.IBuildStep;
|
||||
import org.eclipse.cdt.managedbuilder.buildmodel.IStepVisitor;
|
||||
import org.eclipse.cdt.managedbuilder.core.ManagedBuilderCorePlugin;
|
||||
import org.eclipse.core.runtime.CoreException;
|
||||
import org.eclipse.core.runtime.IStatus;
|
||||
import org.eclipse.core.runtime.Status;
|
||||
|
||||
public class BuildDescriptionGnuMakefileGenerator {
|
||||
private static final String OUT_STEP_RULE = "post_build"; //$NON-NLS-1$
|
||||
private static final String IN_STEP_RULE = "pre_build"; //$NON-NLS-1$
|
||||
private static final String ALL = "all"; //$NON-NLS-1$
|
||||
private static final String TARGET_SEPARATOR = ":"; //$NON-NLS-1$
|
||||
private static final String LINE_SEPARATOR = "\n"; //$NON-NLS-1$
|
||||
private static final String TAB = "\t"; //$NON-NLS-1$
|
||||
private static final String SPACE = " "; //$NON-NLS-1$
|
||||
private static final String ENCODING = "utf-8"; //$NON-NLS-1$
|
||||
private static final String VAR_SOURCES = "SOURCES"; //$NON-NLS-1$
|
||||
private static final String VAR_TARGETS = "TARGETS"; //$NON-NLS-1$
|
||||
private static final String EQUALS = "="; //$NON-NLS-1$
|
||||
private static final String VARREF_PREFIX = "${"; //$NON-NLS-1$
|
||||
private static final String VARREF_SUFFIX = "}"; //$NON-NLS-1$
|
||||
|
||||
private static final String DOT_DOT_SLASH = "../"; //$NON-NLS-1$
|
||||
private static final String DOT_DOT_BACKSLASH = "..\\"; //$NON-NLS-1$
|
||||
|
||||
private IBuildDescription fDes;
|
||||
|
||||
private class DescriptionVisitor implements IStepVisitor {
|
||||
Writer fWriter;
|
||||
DescriptionVisitor(Writer writer){
|
||||
fWriter = writer;
|
||||
}
|
||||
|
||||
public int visit(IBuildStep step) throws CoreException {
|
||||
if(step == fDes.getInputStep() || step == fDes.getOutputStep())
|
||||
return VISIT_CONTINUE;
|
||||
|
||||
try {
|
||||
write(fWriter, step);
|
||||
} catch (IOException e) {
|
||||
throw new CoreException(new Status(IStatus.ERROR, ManagedBuilderCorePlugin.getUniqueIdentifier(), "IO exception occured: ", e));
|
||||
}
|
||||
return VISIT_CONTINUE;
|
||||
}
|
||||
}
|
||||
|
||||
public BuildDescriptionGnuMakefileGenerator(IBuildDescription des){
|
||||
fDes = des;
|
||||
}
|
||||
|
||||
public void store(OutputStream stream) throws CoreException{
|
||||
Writer writer = createWriter(stream);
|
||||
|
||||
try {
|
||||
writer.write(VAR_SOURCES);
|
||||
writer.write(EQUALS);
|
||||
IBuildStep step = fDes.getInputStep();
|
||||
String tmp = toString(step.getOutputResources());
|
||||
writer.write(tmp);
|
||||
writer.write(LINE_SEPARATOR);
|
||||
writer.write(LINE_SEPARATOR);
|
||||
|
||||
writer.write(VAR_TARGETS);
|
||||
writer.write(EQUALS);
|
||||
step = fDes.getOutputStep();
|
||||
tmp = toString(step.getInputResources());
|
||||
writer.write(tmp);
|
||||
writer.write(LINE_SEPARATOR);
|
||||
writer.write(LINE_SEPARATOR);
|
||||
|
||||
writer.write(LINE_SEPARATOR);
|
||||
writeRuleHeader(writer, ALL, IN_STEP_RULE + SPACE + OUT_STEP_RULE);
|
||||
writer.write(LINE_SEPARATOR);
|
||||
writer.write(LINE_SEPARATOR);
|
||||
|
||||
write(writer, fDes.getOutputStep());
|
||||
|
||||
write(writer, fDes.getInputStep());
|
||||
|
||||
BuildDescriptionManager.accept(new DescriptionVisitor(writer), fDes, true);
|
||||
|
||||
writer.flush();
|
||||
} catch (IOException e) {
|
||||
throw new CoreException(new Status(IStatus.ERROR, ManagedBuilderCorePlugin.getUniqueIdentifier(), "IO exception occured: ", e));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
protected Writer createWriter(OutputStream stream){
|
||||
try {
|
||||
return new OutputStreamWriter(stream, ENCODING);
|
||||
} catch (UnsupportedEncodingException e1) {
|
||||
ManagedBuilderCorePlugin.log(e1);
|
||||
}
|
||||
return new OutputStreamWriter(stream);
|
||||
|
||||
}
|
||||
|
||||
protected String createVarRef(String var){
|
||||
return new StringBuffer().append(VARREF_PREFIX).append(var).append(VARREF_SUFFIX).toString();
|
||||
}
|
||||
|
||||
protected void write(Writer writer, IBuildStep step) throws CoreException, IOException {
|
||||
writer.write(LINE_SEPARATOR);
|
||||
|
||||
String target, deps;
|
||||
if(step == fDes.getOutputStep()){
|
||||
target = OUT_STEP_RULE;
|
||||
deps = createVarRef(VAR_TARGETS);
|
||||
} else if (step == fDes.getInputStep()){
|
||||
target = IN_STEP_RULE;
|
||||
deps = "";
|
||||
} else {
|
||||
IBuildResource[] inputs = step.getInputResources();
|
||||
IBuildResource[] outputs = step.getOutputResources();
|
||||
target = toString(outputs);
|
||||
deps = toString(inputs);
|
||||
}
|
||||
|
||||
writeRuleHeader(writer, target, deps);
|
||||
|
||||
IBuildCommand[] cmds = step.getCommands(null, null, null, true);
|
||||
for(int i = 0; i < cmds.length; i++){
|
||||
String cmdStr = toString(cmds[i]);
|
||||
writeCommand(writer, cmdStr);
|
||||
}
|
||||
|
||||
writer.write(LINE_SEPARATOR);
|
||||
writer.write(LINE_SEPARATOR);
|
||||
|
||||
}
|
||||
|
||||
protected void writeCommand(Writer writer, String cmd) throws IOException{
|
||||
writer.write(TAB);
|
||||
writer.write(cmd);
|
||||
writer.write(LINE_SEPARATOR);
|
||||
}
|
||||
|
||||
protected String toString(IBuildCommand cmd){
|
||||
StringBuffer buf = new StringBuffer();
|
||||
buf.append(cmd.getCommand());
|
||||
String argsString = CDataUtil.arrayToString(cmd.getArgs(), SPACE);
|
||||
if(argsString != null && argsString.length() != 0){
|
||||
buf.append(SPACE);
|
||||
buf.append(argsString);
|
||||
}
|
||||
return removeDotDotSlashesAndBackSlashesHack(buf.toString());
|
||||
}
|
||||
|
||||
protected void writeRuleHeader(Writer writer, String target, String deps) throws IOException{
|
||||
writer.write(target);
|
||||
writer.write(TARGET_SEPARATOR);
|
||||
writer.write(SPACE);
|
||||
writer.write(deps);
|
||||
writer.write(LINE_SEPARATOR);
|
||||
}
|
||||
|
||||
protected String toString(IBuildResource[] rcs){
|
||||
StringBuffer buf = new StringBuffer();
|
||||
for(int i = 0; i < rcs.length; i++){
|
||||
if(i != 0)
|
||||
buf.append(SPACE);
|
||||
buf.append(toString(rcs[i]));
|
||||
|
||||
}
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
protected String toString(IBuildResource rc){
|
||||
return removeDotDotSlashesAndBackSlashesHack(BuildDescriptionManager.getRelPath(fDes.getDefaultBuildDirLocation(), rc.getLocation()).toString());
|
||||
}
|
||||
|
||||
/*
|
||||
* this is a very bad hack that removes the "../" and "..\" from the string
|
||||
* this is needed to overcome an assumption that the source root is ../
|
||||
* the BuildDescription calculation mechanism should be fixed to remove this assumption
|
||||
*/
|
||||
private String removeDotDotSlashesAndBackSlashesHack(String str){
|
||||
str = removeDotDotSlashes(str);
|
||||
return removeDotDotBackslashes(str);
|
||||
}
|
||||
|
||||
private String removeDotDotSlashes(String str){
|
||||
int index = str.indexOf(DOT_DOT_SLASH, 0);
|
||||
if(index != -1){
|
||||
StringBuffer buf = new StringBuffer();
|
||||
int start = 0;
|
||||
for(; index != -1; index = str.indexOf(DOT_DOT_SLASH, start)){
|
||||
buf.append(str.substring(start, index));
|
||||
start = index + DOT_DOT_SLASH.length();
|
||||
}
|
||||
buf.append(str.substring(start));
|
||||
return buf.toString();
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
private String removeDotDotBackslashes(String str){
|
||||
int index = str.indexOf(DOT_DOT_BACKSLASH, 0);
|
||||
if(index != -1){
|
||||
StringBuffer buf = new StringBuffer();
|
||||
int start = 0;
|
||||
for(; index != -1; index = str.indexOf(DOT_DOT_BACKSLASH, start)){
|
||||
buf.append(str.substring(start, index));
|
||||
start = index + DOT_DOT_BACKSLASH.length();
|
||||
}
|
||||
buf.append(str.substring(start));
|
||||
return buf.toString();
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
}
|
|
@ -266,7 +266,7 @@ public class BuildStep implements IBuildStep {
|
|||
}
|
||||
return (IBuildCommand[])list.toArray(new BuildCommand[list.size()]);
|
||||
}
|
||||
return null;
|
||||
return new IBuildCommand[0];
|
||||
}
|
||||
|
||||
if(cwd == null)
|
||||
|
|
|
@ -0,0 +1,66 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2007 Intel Corporation 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
|
||||
*
|
||||
* Contributors:
|
||||
* Intel Corporation - Initial API and implementation
|
||||
*******************************************************************************/
|
||||
package org.eclipse.cdt.managedbuilder.templateengine.processes;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
|
||||
import org.eclipse.cdt.core.templateengine.TemplateCore;
|
||||
import org.eclipse.cdt.core.templateengine.process.ProcessArgument;
|
||||
import org.eclipse.cdt.core.templateengine.process.ProcessFailureException;
|
||||
import org.eclipse.cdt.core.templateengine.process.ProcessRunner;
|
||||
import org.eclipse.cdt.managedbuilder.buildmodel.BuildDescriptionManager;
|
||||
import org.eclipse.cdt.managedbuilder.buildmodel.IBuildDescription;
|
||||
import org.eclipse.cdt.managedbuilder.core.IConfiguration;
|
||||
import org.eclipse.cdt.managedbuilder.core.IManagedBuildInfo;
|
||||
import org.eclipse.cdt.managedbuilder.core.ManagedBuildManager;
|
||||
import org.eclipse.cdt.managedbuilder.internal.buildmodel.BuildDescriptionGnuMakefileGenerator;
|
||||
import org.eclipse.core.resources.IFile;
|
||||
import org.eclipse.core.resources.IProject;
|
||||
import org.eclipse.core.resources.ResourcesPlugin;
|
||||
import org.eclipse.core.runtime.CoreException;
|
||||
import org.eclipse.core.runtime.IProgressMonitor;
|
||||
|
||||
public class GenerateMakefileWithBuildDescription extends ProcessRunner{
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public void process(TemplateCore template, ProcessArgument[] args, String processId, IProgressMonitor monitor) throws ProcessFailureException {
|
||||
String projectName = args[0].getSimpleValue();
|
||||
IProject projectHandle = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
|
||||
IManagedBuildInfo info = ManagedBuildManager.getBuildInfo(projectHandle);
|
||||
if(info == null)
|
||||
throw new ProcessFailureException("info is null");
|
||||
|
||||
IConfiguration cfg = info.getDefaultConfiguration();
|
||||
if(cfg == null)
|
||||
throw new ProcessFailureException("cfg is null");
|
||||
|
||||
IBuildDescription des;
|
||||
try {
|
||||
des = BuildDescriptionManager.createBuildDescription(cfg, null, 0);
|
||||
IFile file = projectHandle.getFile("makefile");
|
||||
ByteArrayOutputStream oStream = new ByteArrayOutputStream(100);
|
||||
BuildDescriptionGnuMakefileGenerator gen = new BuildDescriptionGnuMakefileGenerator(des);
|
||||
gen.store(oStream);
|
||||
byte[] bytes = oStream.toByteArray();
|
||||
ByteArrayInputStream iStream = new ByteArrayInputStream(bytes);
|
||||
|
||||
if(!file.exists()){
|
||||
file.create(iStream, true, monitor);
|
||||
} else {
|
||||
file.setContents(iStream, true, false, monitor);
|
||||
}
|
||||
} catch (CoreException e1) {
|
||||
throw new ProcessFailureException(e1);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -3750,6 +3750,12 @@
|
|||
location="templates/projecttemplates/EmptyProject/template.xml"
|
||||
projectType="org.eclipse.cdt.build.core.buildArtefactType.exe">
|
||||
</template>
|
||||
<template
|
||||
id="org.eclipse.cdt.build.core.templates.MakefileHelloWorldCCProject"
|
||||
filterPattern=".*"
|
||||
location="templates/projecttemplates/MakefileHelloWorldCCProject/template.xml"
|
||||
projectType="org.eclipse.cdt.build.makefile.projectType">
|
||||
</template>
|
||||
</extension>
|
||||
|
||||
<extension
|
||||
|
@ -3775,6 +3781,14 @@
|
|||
<toolChain id="cdt.managedbuild.toolchain.gnu.macosx.base"/>
|
||||
<toolChain id="cdt.managedbuild.toolchain.gnu.solaris.base"/>
|
||||
</template>
|
||||
<template
|
||||
id="org.eclipse.cdt.build.core.templates.MakefileHelloWorldCCProject">
|
||||
<toolChain id="cdt.managedbuild.toolchain.gnu.mingw.base"/>
|
||||
<toolChain id="cdt.managedbuild.toolchain.gnu.cygwin.base"/>
|
||||
<toolChain id="cdt.managedbuild.toolchain.gnu.base"/>
|
||||
<toolChain id="cdt.managedbuild.toolchain.gnu.macosx.base"/>
|
||||
<toolChain id="cdt.managedbuild.toolchain.gnu.solaris.base"/>
|
||||
</template>
|
||||
</extension>
|
||||
|
||||
</plugin>
|
||||
|
|
|
@ -0,0 +1,20 @@
|
|||
/*
|
||||
============================================================================
|
||||
Name : $(baseName).cpp
|
||||
Author : $(author)
|
||||
Version :
|
||||
Copyright : $(copyright)
|
||||
Description : Exe source file
|
||||
============================================================================
|
||||
*/
|
||||
/* Hello World in C, Ansi-style */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
puts("$(message)");
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
|
@ -0,0 +1,60 @@
|
|||
<?xml version="1.0" encoding="ISO-8859-1"?>
|
||||
<template type="ProjTempl" version="1.0" supplier="Eclipse.org" revision="1.0" author="Intel Corporation"
|
||||
copyright="Copyright (c) 2007 Intel Corporation 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"
|
||||
id="MakefileHelloWorldCCProject" label="Hello World C++ Project" description="A Hello World C++ Project"
|
||||
help="help.html">
|
||||
<property-group id="basics" label="Basic Settings" description="Basic properties of a project" type="PAGES-ONLY" help="help.html">
|
||||
<property id="author"
|
||||
label="Author"
|
||||
description="Name of the author"
|
||||
type="input"
|
||||
pattern=".*"
|
||||
default=""
|
||||
hidden="false"
|
||||
persist="true"/>
|
||||
<property id="copyright"
|
||||
label="Copyright notice"
|
||||
description="Your copyright notice"
|
||||
type="input"
|
||||
pattern=".*"
|
||||
default="Your copyright notice"
|
||||
hidden="false"
|
||||
persist="true"/>
|
||||
<property id="message"
|
||||
label="Hello world greeting"
|
||||
description="Your hello world greeting message"
|
||||
type="input"
|
||||
pattern=".*"
|
||||
default="Hello World!!!"
|
||||
hidden="false"
|
||||
persist="true"/>
|
||||
</property-group>
|
||||
|
||||
<!--process type="org.eclipse.cdt.managedbuilder.core.NewManagedProject">
|
||||
<simple name="name" value="$(projectName)" />
|
||||
<simple name="artifactExtension" value="exe" />
|
||||
<simple name="isCProject" value="true" />
|
||||
</process-->
|
||||
|
||||
<process type="org.eclipse.cdt.core.CreateSourceFolder">
|
||||
<simple name="projectName" value="$(projectName)"/>
|
||||
<simple name="path" value="src"/>
|
||||
</process>
|
||||
|
||||
<process type="org.eclipse.cdt.core.AddFiles">
|
||||
<simple name="projectName" value="$(projectName)"/>
|
||||
<complex-array name="files">
|
||||
<element>
|
||||
<simple name="source" value="src/Basename.cpp"/>
|
||||
<simple name="target" value="src/$(projectName).cpp"/>
|
||||
<simple name="replaceable" value="true"/>
|
||||
</element>
|
||||
</complex-array>
|
||||
</process>
|
||||
|
||||
<process type="org.eclipse.cdt.managedbuilder.core.GenerateMakefileWithBuildDescription">
|
||||
<simple name="projectName" value="$(projectName)"/>
|
||||
</process>
|
||||
|
||||
</template>
|
||||
|
|
@ -18,6 +18,7 @@ import org.eclipse.jface.wizard.IWizard;
|
|||
|
||||
public class StdBuildWizard extends AbstractCWizard {
|
||||
private static final String NAME = Messages.getString("StdBuildWizard.0"); //$NON-NLS-1$
|
||||
private static final String ID = "org.eclipse.cdt.build.makefile.projectType"; //$NON-NLS-1$
|
||||
|
||||
public EntryDescriptor[] createItems(boolean supportedOnly, IWizard wizard) {
|
||||
STDWizardHandler h = new STDWizardHandler(parent, wizard);
|
||||
|
@ -26,7 +27,7 @@ public class StdBuildWizard extends AbstractCWizard {
|
|||
for (int i=0; i<tcs.length; i++)
|
||||
if (isValid(tcs[i], supportedOnly, wizard))
|
||||
h.addTc(tcs[i]);
|
||||
EntryDescriptor wd = new EntryDescriptor(NAME, null, NAME, false, h, null);
|
||||
EntryDescriptor wd = new EntryDescriptor(ID, null, NAME, false, h, null);
|
||||
return new EntryDescriptor[] {wd};
|
||||
|
||||
// test only: creating items like of Templates
|
||||
|
|
Loading…
Add table
Reference in a new issue