diff options
Diffstat (limited to 'extraplugins/qompass/codegen/cpp/org.eclipse.papyrus.cpp.cdtproject/src')
8 files changed, 467 insertions, 0 deletions
diff --git a/extraplugins/qompass/codegen/cpp/org.eclipse.papyrus.cpp.cdtproject/src/org/eclipse/papyrus/cpp/cdtproject/Activator.java b/extraplugins/qompass/codegen/cpp/org.eclipse.papyrus.cpp.cdtproject/src/org/eclipse/papyrus/cpp/cdtproject/Activator.java new file mode 100644 index 00000000000..ad6ab20019d --- /dev/null +++ b/extraplugins/qompass/codegen/cpp/org.eclipse.papyrus.cpp.cdtproject/src/org/eclipse/papyrus/cpp/cdtproject/Activator.java @@ -0,0 +1,54 @@ +package org.eclipse.papyrus.cpp.cdtproject; + +import org.eclipse.ui.plugin.AbstractUIPlugin; +import org.osgi.framework.BundleContext; + +/** + * The activator class controls the plug-in life cycle + */ +public class Activator extends AbstractUIPlugin { + + // The plug-in ID + public static final String PLUGIN_ID = "org.eclipse.papyrus.cpp.cdtproject"; //$NON-NLS-1$ + + // The shared instance + private static Activator plugin; + + /** + * The constructor + */ + public Activator() { + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext) + */ + @Override + public void start(BundleContext context) throws Exception { + super.start(context); + plugin = this; + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext) + */ + @Override + public void stop(BundleContext context) throws Exception { + plugin = null; + super.stop(context); + } + + /** + * Returns the shared instance + * + * @return the shared instance + */ + public static Activator getDefault() { + return plugin; + } + +} diff --git a/extraplugins/qompass/codegen/cpp/org.eclipse.papyrus.cpp.cdtproject/src/org/eclipse/papyrus/cpp/cdtproject/CCNamedProjectWizard.java b/extraplugins/qompass/codegen/cpp/org.eclipse.papyrus.cpp.cdtproject/src/org/eclipse/papyrus/cpp/cdtproject/CCNamedProjectWizard.java new file mode 100644 index 00000000000..b712ac949aa --- /dev/null +++ b/extraplugins/qompass/codegen/cpp/org.eclipse.papyrus.cpp.cdtproject/src/org/eclipse/papyrus/cpp/cdtproject/CCNamedProjectWizard.java @@ -0,0 +1,47 @@ +package org.eclipse.papyrus.cpp.cdtproject; + +import org.eclipse.cdt.ui.wizards.CCProjectWizard; +import org.eclipse.jface.wizard.IWizardPage; + +/** + * Variant of CCProjectWizard that allows for setting an initial + * project name. It creates the pages and sets the initial name. + * It does not use the standard CDTMainWizardPage but a variant + * called CDTMainWizardPageV that skips the first validation which + * would other trigger an error. + * + * @see org.eclipse.papyrus.cpp.cdtproject.CDTMainWizardPageV + */ +public class CCNamedProjectWizard extends CCProjectWizard { + + public CCNamedProjectWizard(String projectName) { + this.projectName = projectName; + dontAdd = false; + } + + @Override + public void addPages() { + dontAdd = true; + // avoid that page is actually added, since we want to add our page below + super.addPages(); + dontAdd = false; + String title = fMainPage.getTitle(); + String desc = fMainPage.getDescription(); + fMainPage = new CDTMainWizardPageV(fMainPage.getName()); + fMainPage.setTitle(title); + fMainPage.setDescription(desc); + fMainPage.setInitialProjectName(projectName); + addPage(fMainPage); + } + + @Override + public void addPage(IWizardPage page) { + if (!dontAdd) { + super.addPage(page); + } + } + + private String projectName; + + private boolean dontAdd; +} diff --git a/extraplugins/qompass/codegen/cpp/org.eclipse.papyrus.cpp.cdtproject/src/org/eclipse/papyrus/cpp/cdtproject/CDTMainWizardPageV.java b/extraplugins/qompass/codegen/cpp/org.eclipse.papyrus.cpp.cdtproject/src/org/eclipse/papyrus/cpp/cdtproject/CDTMainWizardPageV.java new file mode 100644 index 00000000000..9066abf68d1 --- /dev/null +++ b/extraplugins/qompass/codegen/cpp/org.eclipse.papyrus.cpp.cdtproject/src/org/eclipse/papyrus/cpp/cdtproject/CDTMainWizardPageV.java @@ -0,0 +1,33 @@ +package org.eclipse.papyrus.cpp.cdtproject; + +import org.eclipse.cdt.ui.wizards.CDTMainWizardPage; + +/** + * Variant of the CDTMainWizardPage that skips the first validation. This + * is required in the context of setting an initial project name, since the + * update of the project name will trigger a validation which stops with an + * exception, since some elements (notable tree) are not yet initialized. + * + * Verify if future versions of CDT still require this "hack" {@link CDTMainWizardPage} + * + * @author ansgar + * + */ +public class CDTMainWizardPageV extends CDTMainWizardPage { + + public CDTMainWizardPageV(String pageName) { + super(pageName); + firstValidate = true; + } + + @Override + protected boolean validatePage() { + if (firstValidate) { + return true; + } else { + return super.validatePage(); + } + } + + private boolean firstValidate; +} diff --git a/extraplugins/qompass/codegen/cpp/org.eclipse.papyrus.cpp.cdtproject/src/org/eclipse/papyrus/cpp/cdtproject/CDTSettings.java b/extraplugins/qompass/codegen/cpp/org.eclipse.papyrus.cpp.cdtproject/src/org/eclipse/papyrus/cpp/cdtproject/CDTSettings.java new file mode 100644 index 00000000000..7f44c642855 --- /dev/null +++ b/extraplugins/qompass/codegen/cpp/org.eclipse.papyrus.cpp.cdtproject/src/org/eclipse/papyrus/cpp/cdtproject/CDTSettings.java @@ -0,0 +1,28 @@ +package org.eclipse.papyrus.cpp.cdtproject; + +import java.util.List; + +import org.eclipse.papyrus.codegen.extensionpoints.AbstractSettings; + +public class CDTSettings extends AbstractSettings { + + /** + * include paths for compiler + */ + List<String> includePaths; + + /** + * list of libraries for linker + */ + List<String> libs; + + /** + * list of library paths for linker + */ + List<String> libPaths; + + /** + * list of predefined macros (#define) + */ + List<String> macros; +} diff --git a/extraplugins/qompass/codegen/cpp/org.eclipse.papyrus.cpp.cdtproject/src/org/eclipse/papyrus/cpp/cdtproject/CNamedProjectWizard.java b/extraplugins/qompass/codegen/cpp/org.eclipse.papyrus.cpp.cdtproject/src/org/eclipse/papyrus/cpp/cdtproject/CNamedProjectWizard.java new file mode 100644 index 00000000000..af5e1592c11 --- /dev/null +++ b/extraplugins/qompass/codegen/cpp/org.eclipse.papyrus.cpp.cdtproject/src/org/eclipse/papyrus/cpp/cdtproject/CNamedProjectWizard.java @@ -0,0 +1,47 @@ +package org.eclipse.papyrus.cpp.cdtproject; + +import org.eclipse.cdt.ui.wizards.CProjectWizard; +import org.eclipse.jface.wizard.IWizardPage; + +/** + * Variant of CProjectWizard that allows for setting an initial + * project name. It creates the pages and sets the initial name. + * It does not use the standard CDTMainWizardPage but a variant + * called CDTMainWizardPageV that skips the first validation which + * would other trigger an error. + * + * @see org.eclipse.papyrus.cpp.cdtproject.CDTMainWizardPageV + */ +public class CNamedProjectWizard extends CProjectWizard { + + public CNamedProjectWizard(String projectName) { + this.projectName = projectName; + dontAdd = false; + } + + @Override + public void addPages() { + dontAdd = true; + // avoid that page is actually added, since we want to add our page below + super.addPages(); + dontAdd = false; + String title = fMainPage.getTitle(); + String desc = fMainPage.getDescription(); + fMainPage = new CDTMainWizardPageV(fMainPage.getName()); + fMainPage.setTitle(title); + fMainPage.setDescription(desc); + fMainPage.setInitialProjectName(projectName); + addPage(fMainPage); + } + + @Override + public void addPage(IWizardPage page) { + if (!dontAdd) { + super.addPage(page); + } + } + + private String projectName; + + private boolean dontAdd; +} diff --git a/extraplugins/qompass/codegen/cpp/org.eclipse.papyrus.cpp.cdtproject/src/org/eclipse/papyrus/cpp/cdtproject/CProjectSupport.java b/extraplugins/qompass/codegen/cpp/org.eclipse.papyrus.cpp.cdtproject/src/org/eclipse/papyrus/cpp/cdtproject/CProjectSupport.java new file mode 100644 index 00000000000..9f020fd2354 --- /dev/null +++ b/extraplugins/qompass/codegen/cpp/org.eclipse.papyrus.cpp.cdtproject/src/org/eclipse/papyrus/cpp/cdtproject/CProjectSupport.java @@ -0,0 +1,16 @@ +package org.eclipse.papyrus.cpp.cdtproject; + +import org.eclipse.core.resources.IProject; +import org.eclipse.papyrus.codegen.extensionpoints.ILangProjectSupport; + +/** + * C project support + */ +public class CProjectSupport extends C_CppProjectSupport implements ILangProjectSupport { + + @Override + public IProject createProject(String projectName) { + IProject project = super.createProject(projectName); + return project; + } +} diff --git a/extraplugins/qompass/codegen/cpp/org.eclipse.papyrus.cpp.cdtproject/src/org/eclipse/papyrus/cpp/cdtproject/C_CppProjectSupport.java b/extraplugins/qompass/codegen/cpp/org.eclipse.papyrus.cpp.cdtproject/src/org/eclipse/papyrus/cpp/cdtproject/C_CppProjectSupport.java new file mode 100644 index 00000000000..f86106b7577 --- /dev/null +++ b/extraplugins/qompass/codegen/cpp/org.eclipse.papyrus.cpp.cdtproject/src/org/eclipse/papyrus/cpp/cdtproject/C_CppProjectSupport.java @@ -0,0 +1,225 @@ +package org.eclipse.papyrus.cpp.cdtproject; + +import org.eclipse.cdt.core.model.CoreModel; +import org.eclipse.cdt.core.settings.model.CIncludePathEntry; +import org.eclipse.cdt.core.settings.model.CMacroEntry; +import org.eclipse.cdt.core.settings.model.ICConfigurationDescription; +import org.eclipse.cdt.core.settings.model.ICFolderDescription; +import org.eclipse.cdt.core.settings.model.ICLanguageSetting; +import org.eclipse.cdt.core.settings.model.ICLanguageSettingEntry; +import org.eclipse.cdt.core.settings.model.ICProjectDescription; +import org.eclipse.cdt.core.settings.model.ICProjectDescriptionManager; +import org.eclipse.cdt.core.settings.model.ICSettingEntry; +import org.eclipse.cdt.managedbuilder.core.BuildException; +import org.eclipse.cdt.managedbuilder.core.IConfiguration; +import org.eclipse.cdt.managedbuilder.core.IOption; +import org.eclipse.cdt.managedbuilder.core.ITool; +import org.eclipse.cdt.managedbuilder.core.ManagedBuildManager; +import org.eclipse.cdt.ui.wizards.CDTCommonProjectWizard; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.IWorkspaceRoot; +import org.eclipse.core.resources.ResourcesPlugin; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.emf.common.util.BasicEList; +import org.eclipse.emf.common.util.EList; +import org.eclipse.emf.common.util.UniqueEList; +import org.eclipse.jface.wizard.WizardDialog; +import org.eclipse.papyrus.C_Cpp.ExternLibrary; +import org.eclipse.papyrus.codegen.extensionpoints.AbstractSettings; +import org.eclipse.papyrus.codegen.extensionpoints.ILangProjectSupport; +import org.eclipse.swt.widgets.Display; +import org.eclipse.ui.IWorkbench; +import org.eclipse.ui.PlatformUI; +import org.eclipse.uml2.uml.Class; +import org.eclipse.uml2.uml.Element; +import org.eclipse.uml2.uml.Package; +import org.eclipse.uml2.uml.util.UMLUtil; + +/** + * Supports the creation and configuration of CDT projects + */ +public class C_CppProjectSupport implements ILangProjectSupport { + + // TODO specific "root" is only required for component based code generation + private static final String ROOT = "root"; //$NON-NLS-1$ + + private static final String C = "c"; //$NON-NLS-1$ + + private static final String CPP = "cpp"; //$NON-NLS-1$ + + private int dialogStatus; + + /** + * Create a C++ project. + * Caller should test before calling, whether the project exists already + * + * @param projectName + * @return the created project + */ + @Override + public IProject createProject(String projectName) + { + IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); + + IProject project = root.getProject(projectName); + dialogStatus = 0; + try { + IWorkbench wb = PlatformUI.getWorkbench(); + + // create CDT wizard for C++ or C + final CDTCommonProjectWizard wiz = this instanceof CppProjectSupport ? + new CCNamedProjectWizard(projectName) : + new CNamedProjectWizard(projectName); + + wiz.setWindowTitle("create project " + projectName); //$NON-NLS-1$ + wiz.init(wb, null); + + Display.getDefault().syncExec(new Runnable() { + + @Override + public void run() { + WizardDialog wizDiag = new WizardDialog(Display.getCurrent().getActiveShell(), wiz); + + wizDiag.create(); + dialogStatus = wizDiag.open(); + } + }); + } catch (Exception e) { + e.printStackTrace(); + project = null; + } + if (dialogStatus == 1) { + // corresponds to Cancel + return null; + } + if ((project == null) || !project.exists()) { + throw new RuntimeException("Could not create CDT project. This might indicate that there is a problem with your CDT installation."); //$NON-NLS-1$ + } + return project; + } + + @Override + public void setSettings(IProject project, AbstractSettings abstractSettings) + { + CDTSettings settings = (CDTSettings) abstractSettings; + try { + // ((CProject) project). + // IProjectDescription desc = m_project.getDescription(); + + ICProjectDescriptionManager mngr = + CoreModel.getDefault().getProjectDescriptionManager(); + ICProjectDescription cdesc = mngr.getProjectDescription(project, true); + + // loop over all configurations + for (ICConfigurationDescription configDescr : cdesc.getConfigurations()) { + + ICFolderDescription folderDescription = + configDescr.getRootFolderDescription(); + + ICLanguageSetting[] languageSettings = folderDescription.getLanguageSettings(); + + // copy string array into ICLanguageSetting array + ICLanguageSettingEntry[] icIncludePaths = new ICLanguageSettingEntry[settings.includePaths.size()]; + for (int i = 0; i < settings.includePaths.size(); i++) { + icIncludePaths[i] = new CIncludePathEntry(settings.includePaths.get(i), ICSettingEntry.VALUE_WORKSPACE_PATH); + } + + // define name of used operating system from model (attribute of "Target" stereotype) + // and add it to list of macros + if (settings.targetOS != null) { + settings.macros.add("OS_" + settings.targetOS); //$NON-NLS-1$ + } + + // define macros + EList<ICLanguageSettingEntry> icMacros = + new BasicEList<ICLanguageSettingEntry>(); + for (int i = 0; i < settings.macros.size(); i++) { + // TODO: need to define values for macros as well? + icMacros.add(new CMacroEntry(settings.macros.get(i), "", 0)); //$NON-NLS-1$ + } + + // now set include path and preprocessor code + for (ICLanguageSetting lang : languageSettings) { + // selection better via ID? (instead of extension) + // Log.log(Status.INFO, Log.CODEGEN, "CppLanguageSupport: lang.getID: " + lang.getId() + " lang.getLanguageID: " + lang.getLanguageId()); + for (String ext : lang.getSourceExtensions()) { + if (ext.equals(CPP) || ext.equals(C)) { + lang.setSettingEntries(ICSettingEntry.INCLUDE_PATH, icIncludePaths); + ICLanguageSettingEntry icOldMacros[] = + lang.getSettingEntries(ICSettingEntry.MACRO); + for (ICLanguageSettingEntry entry : icOldMacros) { + icMacros.add(entry); + } + lang.setSettingEntries(ICSettingEntry.MACRO, icMacros); + break; + } + } + } + IConfiguration main = ManagedBuildManager.getConfigurationForDescription(configDescr); + // change artifact name + // main.setArtifactName(main.getArtifactName () + ".bin"); + + // add to -l (libraries) + ITool cfTool = main.calculateTargetTool(); + + // IOption libOption = cfTool.getOptionBy(IOption.TYPE_LIB); + + for (IOption opt : cfTool.getOptions()) { + if (opt.getValueType() == IOption.LIBRARIES) { + main.setOption(cfTool, opt, settings.libs.toArray(new String[0])); + } else if (opt.getValueType() == IOption.LIBRARY_PATHS) { + main.setOption(cfTool, opt, settings.libPaths.toArray(new String[0])); + } + } + mngr.setProjectDescription(project, cdesc, true, null); + } + ManagedBuildManager.saveBuildInfo(project, true); + } catch (BuildException be) { + throw new RuntimeException(be.getMessage()); + } catch (CoreException ce) { + throw new RuntimeException(ce.getMessage()); + } + } + + @Override + public AbstractSettings initialConfigurationData() { + CDTSettings settings = new CDTSettings(); + settings.includePaths = new UniqueEList<String>(); + // include project directory (all paths are relative to it => ".") + settings.includePaths.add("."); //$NON-NLS-1$ + // include also "root" (relative path) + settings.includePaths.add(ROOT); + + settings.libs = new UniqueEList<String>(); + settings.libPaths = new UniqueEList<String>(); + settings.macros = new UniqueEList<String>(); + return settings; + } + + @Override + public void gatherConfigData(Class implementation, AbstractSettings abstractSettings) { + CDTSettings settings = (CDTSettings) abstractSettings; + Element owner = implementation.getOwner(); + while (owner instanceof Package) { + ExternLibrary cppLibrary = UMLUtil.getStereotypeApplication(owner, ExternLibrary.class); + if ((cppLibrary != null) && (settings != null)) { + settings.includePaths.addAll(cppLibrary.getIncludes()); + for (String libPath : cppLibrary.getLibPaths()) { + if (libPath.startsWith("/")) { + // libPaths starting with a slash are relative to workspace location + // TODO: need to support absolute paths (host file system?) as well? + // (additional prefix. Eclipse standards?) Problem: workspace_loc is added + // automatically for absolute includePaths + settings.libPaths.add("${workspace_loc:" + libPath + "}"); + } else { + // relative to project root, otherwise + settings.libPaths.add(libPath); + } + } + settings.libs.addAll(cppLibrary.getLibs()); + settings.macros.addAll(cppLibrary.getMacros()); + } + owner = owner.getOwner(); + } + } +} diff --git a/extraplugins/qompass/codegen/cpp/org.eclipse.papyrus.cpp.cdtproject/src/org/eclipse/papyrus/cpp/cdtproject/CppProjectSupport.java b/extraplugins/qompass/codegen/cpp/org.eclipse.papyrus.cpp.cdtproject/src/org/eclipse/papyrus/cpp/cdtproject/CppProjectSupport.java new file mode 100644 index 00000000000..aaebf98d1d7 --- /dev/null +++ b/extraplugins/qompass/codegen/cpp/org.eclipse.papyrus.cpp.cdtproject/src/org/eclipse/papyrus/cpp/cdtproject/CppProjectSupport.java @@ -0,0 +1,17 @@ +package org.eclipse.papyrus.cpp.cdtproject; + +import org.eclipse.core.resources.IProject; +import org.eclipse.papyrus.codegen.extensionpoints.ILangProjectSupport; + +/** + * C++ project support + */ +public class CppProjectSupport extends C_CppProjectSupport implements ILangProjectSupport { + + @Override + public IProject createProject(String projectName) { + IProject project = super.createProject(projectName); + + return project; + } +} |