From 9d428201c3ad24d8b8535352650f6b941b70bf2b Mon Sep 17 00:00:00 2001 From: Mikhail Sennikovsky Date: Thu, 10 May 2007 17:41:24 +0000 Subject: [PATCH] Hellow World template for Makefile Project --- .../plugin.xml | 5 + .../BuildDescriptionGnuMakefileGenerator.java | 236 ++++++++++++++++++ .../internal/buildmodel/BuildStep.java | 2 +- .../GenerateMakefileWithBuildDescription.java | 66 +++++ .../plugin.xml | 16 +- .../src/Basename.cpp | 20 ++ .../MakefileHelloWorldCCProject/template.xml | 60 +++++ .../ui/wizards/StdBuildWizard.java | 3 +- 8 files changed, 405 insertions(+), 3 deletions(-) create mode 100644 build/org.eclipse.cdt.managedbuilder.core/src/org/eclipse/cdt/managedbuilder/internal/buildmodel/BuildDescriptionGnuMakefileGenerator.java create mode 100644 build/org.eclipse.cdt.managedbuilder.core/src/org/eclipse/cdt/managedbuilder/templateengine/processes/GenerateMakefileWithBuildDescription.java create mode 100644 build/org.eclipse.cdt.managedbuilder.gnu.ui/templates/projecttemplates/MakefileHelloWorldCCProject/src/Basename.cpp create mode 100644 build/org.eclipse.cdt.managedbuilder.gnu.ui/templates/projecttemplates/MakefileHelloWorldCCProject/template.xml diff --git a/build/org.eclipse.cdt.managedbuilder.core/plugin.xml b/build/org.eclipse.cdt.managedbuilder.core/plugin.xml index 327818a0691..fb530d589b3 100644 --- a/build/org.eclipse.cdt.managedbuilder.core/plugin.xml +++ b/build/org.eclipse.cdt.managedbuilder.core/plugin.xml @@ -543,6 +543,11 @@ + + + diff --git a/build/org.eclipse.cdt.managedbuilder.core/src/org/eclipse/cdt/managedbuilder/internal/buildmodel/BuildDescriptionGnuMakefileGenerator.java b/build/org.eclipse.cdt.managedbuilder.core/src/org/eclipse/cdt/managedbuilder/internal/buildmodel/BuildDescriptionGnuMakefileGenerator.java new file mode 100644 index 00000000000..ed8a0dd36b9 --- /dev/null +++ b/build/org.eclipse.cdt.managedbuilder.core/src/org/eclipse/cdt/managedbuilder/internal/buildmodel/BuildDescriptionGnuMakefileGenerator.java @@ -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; + } + +} diff --git a/build/org.eclipse.cdt.managedbuilder.core/src/org/eclipse/cdt/managedbuilder/internal/buildmodel/BuildStep.java b/build/org.eclipse.cdt.managedbuilder.core/src/org/eclipse/cdt/managedbuilder/internal/buildmodel/BuildStep.java index 077ed51e378..e891c15f955 100644 --- a/build/org.eclipse.cdt.managedbuilder.core/src/org/eclipse/cdt/managedbuilder/internal/buildmodel/BuildStep.java +++ b/build/org.eclipse.cdt.managedbuilder.core/src/org/eclipse/cdt/managedbuilder/internal/buildmodel/BuildStep.java @@ -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) diff --git a/build/org.eclipse.cdt.managedbuilder.core/src/org/eclipse/cdt/managedbuilder/templateengine/processes/GenerateMakefileWithBuildDescription.java b/build/org.eclipse.cdt.managedbuilder.core/src/org/eclipse/cdt/managedbuilder/templateengine/processes/GenerateMakefileWithBuildDescription.java new file mode 100644 index 00000000000..7a78e3a300e --- /dev/null +++ b/build/org.eclipse.cdt.managedbuilder.core/src/org/eclipse/cdt/managedbuilder/templateengine/processes/GenerateMakefileWithBuildDescription.java @@ -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); + } + } +} diff --git a/build/org.eclipse.cdt.managedbuilder.gnu.ui/plugin.xml b/build/org.eclipse.cdt.managedbuilder.gnu.ui/plugin.xml index 3c5a596bef8..e11bff2929a 100644 --- a/build/org.eclipse.cdt.managedbuilder.gnu.ui/plugin.xml +++ b/build/org.eclipse.cdt.managedbuilder.gnu.ui/plugin.xml @@ -3750,7 +3750,13 @@ location="templates/projecttemplates/EmptyProject/template.xml" projectType="org.eclipse.cdt.build.core.buildArtefactType.exe"> - + + @@ -3775,6 +3781,14 @@ + diff --git a/build/org.eclipse.cdt.managedbuilder.gnu.ui/templates/projecttemplates/MakefileHelloWorldCCProject/src/Basename.cpp b/build/org.eclipse.cdt.managedbuilder.gnu.ui/templates/projecttemplates/MakefileHelloWorldCCProject/src/Basename.cpp new file mode 100644 index 00000000000..c9fd0add173 --- /dev/null +++ b/build/org.eclipse.cdt.managedbuilder.gnu.ui/templates/projecttemplates/MakefileHelloWorldCCProject/src/Basename.cpp @@ -0,0 +1,20 @@ +/* +============================================================================ + Name : $(baseName).cpp + Author : $(author) + Version : + Copyright : $(copyright) + Description : Exe source file +============================================================================ +*/ +/* Hello World in C, Ansi-style */ + +#include +#include + +int main(void) +{ + puts("$(message)"); + return EXIT_SUCCESS; +} + diff --git a/build/org.eclipse.cdt.managedbuilder.gnu.ui/templates/projecttemplates/MakefileHelloWorldCCProject/template.xml b/build/org.eclipse.cdt.managedbuilder.gnu.ui/templates/projecttemplates/MakefileHelloWorldCCProject/template.xml new file mode 100644 index 00000000000..e319bd80b1f --- /dev/null +++ b/build/org.eclipse.cdt.managedbuilder.gnu.ui/templates/projecttemplates/MakefileHelloWorldCCProject/template.xml @@ -0,0 +1,60 @@ + + + diff --git a/build/org.eclipse.cdt.managedbuilder.ui/src/org/eclipse/cdt/managedbuilder/ui/wizards/StdBuildWizard.java b/build/org.eclipse.cdt.managedbuilder.ui/src/org/eclipse/cdt/managedbuilder/ui/wizards/StdBuildWizard.java index 00bb96b7fff..1bf65225877 100644 --- a/build/org.eclipse.cdt.managedbuilder.ui/src/org/eclipse/cdt/managedbuilder/ui/wizards/StdBuildWizard.java +++ b/build/org.eclipse.cdt.managedbuilder.ui/src/org/eclipse/cdt/managedbuilder/ui/wizards/StdBuildWizard.java @@ -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