1
2
3
4
5
6
7 package org.eclipse.papyrus.designer.languages.cpp.cdt.project;
8
9 import org.eclipse.cdt.core.model.CoreModel;
10 import org.eclipse.cdt.core.settings.model.CIncludePathEntry;
11 import org.eclipse.cdt.core.settings.model.CMacroEntry;
12 import org.eclipse.cdt.core.settings.model.ICConfigurationDescription;
13 import org.eclipse.cdt.core.settings.model.ICFolderDescription;
14 import org.eclipse.cdt.core.settings.model.ICLanguageSetting;
15 import org.eclipse.cdt.core.settings.model.ICLanguageSettingEntry;
16 import org.eclipse.cdt.core.settings.model.ICProjectDescription;
17 import org.eclipse.cdt.core.settings.model.ICProjectDescriptionManager;
18 import org.eclipse.cdt.core.settings.model.ICSettingEntry;
19 import org.eclipse.cdt.managedbuilder.core.BuildException;
20 import org.eclipse.cdt.managedbuilder.core.IConfiguration;
21 import org.eclipse.cdt.managedbuilder.core.IOption;
22 import org.eclipse.cdt.managedbuilder.core.ITool;
23 import org.eclipse.cdt.managedbuilder.core.ManagedBuildManager;
24 import org.eclipse.cdt.ui.wizards.CDTCommonProjectWizard;
25 import org.eclipse.core.resources.IProject;
26 import org.eclipse.core.resources.IWorkspaceRoot;
27 import org.eclipse.core.resources.ResourcesPlugin;
28 import org.eclipse.core.runtime.CoreException;
29 import org.eclipse.emf.common.util.BasicEList;
30 import org.eclipse.emf.common.util.EList;
31 import org.eclipse.emf.common.util.UniqueEList;
32 import org.eclipse.jface.window.Window;
33 import org.eclipse.jface.wizard.WizardDialog;
34 import org.eclipse.papyrus.designer.languages.common.extensionpoints.AbstractSettings;
35 import org.eclipse.papyrus.designer.languages.common.extensionpoints.ILangProjectSupport;
36 import org.eclipse.papyrus.designer.languages.cpp.profile.C_Cpp.ExternLibrary;
37 import org.eclipse.swt.widgets.Display;
38 import org.eclipse.ui.IWorkbench;
39 import org.eclipse.ui.PlatformUI;
40 import org.eclipse.uml2.uml.Classifier;
41 import org.eclipse.uml2.uml.Element;
42 import org.eclipse.uml2.uml.Package;
43 import org.eclipse.uml2.uml.util.UMLUtil;
44
45
46
47
48 public class C_CppProjectSupport implements ILangProjectSupport {
49
50
51 public static final String WS_PREFIX = "ws:";
52
53 private static final String C = "c";
54
55 private static final String CPP = "cpp";
56
57 protected int dialogStatus;
58
59
60
61
62
63
64
65
66 @Override
67 public IProject createProject(String projectName) {
68 IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
69
70 IProject project = root.getProject(projectName);
71 dialogStatus = 0;
72 try {
73 IWorkbench wb = PlatformUI.getWorkbench();
74
75
76 final CDTCommonProjectWizard wiz = this instanceof CppProjectSupport ? new CCNamedProjectWizard(projectName)
77 : new CNamedProjectWizard(projectName);
78
79 wiz.setWindowTitle("create project " + projectName);
80 wiz.init(wb, null);
81
82 Display.getDefault().syncExec(new Runnable() {
83
84 @Override
85 public void run() {
86 WizardDialog wizDiag = new WizardDialog(Display.getCurrent().getActiveShell(), wiz);
87
88 wizDiag.create();
89 dialogStatus = wizDiag.open();
90 }
91 });
92 if (dialogStatus == Window.OK) {
93
94 project = wiz.getLastProject();
95 } else if (dialogStatus == Window.CANCEL) {
96 return null;
97 }
98 } catch (Exception e) {
99 e.printStackTrace();
100 project = null;
101 }
102
103 if ((project == null) || !project.exists()) {
104 throw new RuntimeException(
105 "Could not create CDT project. This might indicate that there is a problem with your CDT installation.");
106 }
107 return project;
108 }
109
110 @Override
111 public void setSettings(IProject project, AbstractSettings abstractSettings) {
112 CDTSettings settings = (CDTSettings) abstractSettings;
113 try {
114
115
116
117 ICProjectDescriptionManager mngr = CoreModel.getDefault().getProjectDescriptionManager();
118 ICProjectDescription cdesc = mngr.getProjectDescription(project, true);
119
120
121 for (ICConfigurationDescription configDescr : cdesc.getConfigurations()) {
122
123 ICFolderDescription folderDescription = configDescr.getRootFolderDescription();
124
125 ICLanguageSetting[] languageSettings = folderDescription.getLanguageSettings();
126
127
128 ICLanguageSettingEntry[] icIncludePaths = new ICLanguageSettingEntry[settings.includePaths.size()];
129 for (int i = 0; i < settings.includePaths.size(); i++) {
130 String path = settings.includePaths.get(i);
131 if (isWsRelPath(path)) {
132 icIncludePaths[i] = new CIncludePathEntry(removeWsPrefix(path),
133 ICSettingEntry.VALUE_WORKSPACE_PATH);
134 } else {
135 icIncludePaths[i] = new CIncludePathEntry(path, 0);
136 }
137 }
138
139
140
141
142 if (settings.targetOS != null) {
143 settings.macros.add("OS_" + settings.targetOS);
144 }
145
146
147 EList<ICLanguageSettingEntry> icMacros = new BasicEList<ICLanguageSettingEntry>();
148 for (int i = 0; i < settings.macros.size(); i++) {
149
150 icMacros.add(new CMacroEntry(settings.macros.get(i), "", 0));
151 }
152
153
154 for (ICLanguageSetting lang : languageSettings) {
155
156
157
158
159 for (String ext : lang.getSourceExtensions()) {
160 if (ext.equals(CPP) || ext.equals(C)) {
161 lang.setSettingEntries(ICSettingEntry.INCLUDE_PATH, icIncludePaths);
162 ICLanguageSettingEntry icOldMacros[] = lang.getSettingEntries(ICSettingEntry.MACRO);
163 for (ICLanguageSettingEntry entry : icOldMacros) {
164 icMacros.add(entry);
165 }
166 lang.setSettingEntries(ICSettingEntry.MACRO, icMacros);
167 break;
168 }
169 }
170 }
171 IConfiguration main = ManagedBuildManager.getConfigurationForDescription(configDescr);
172
173
174
175
176 ITool cfTool = main.calculateTargetTool();
177
178
179
180 for (IOption opt : cfTool.getOptions()) {
181 if (opt.getValueType() == IOption.LIBRARIES) {
182 main.setOption(cfTool, opt, settings.libs.toArray(new String[0]));
183 } else if (opt.getValueType() == IOption.LIBRARY_PATHS) {
184 main.setOption(cfTool, opt, settings.libPaths.toArray(new String[0]));
185 }
186 }
187 mngr.setProjectDescription(project, cdesc, true, null);
188 }
189 ManagedBuildManager.saveBuildInfo(project, true);
190 } catch (BuildException be) {
191 throw new RuntimeException(be.getMessage());
192 } catch (CoreException ce) {
193 throw new RuntimeException(ce.getMessage());
194 }
195 }
196
197 @Override
198 public AbstractSettings initialConfigurationData() {
199 CDTSettings settings = new CDTSettings();
200 settings.includePaths = new UniqueEList<String>();
201
202
203
204 settings.includePaths.add(WS_PREFIX + "src-gen");
205
206 settings.libs = new UniqueEList<String>();
207 settings.libPaths = new UniqueEList<String>();
208 settings.macros = new UniqueEList<String>();
209 return settings;
210 }
211
212 @Override
213 public void gatherConfigData(Classifier implementation, AbstractSettings abstractSettings) {
214 CDTSettings settings = (CDTSettings) abstractSettings;
215 Element owner = implementation.getNearestPackage();
216 while (owner instanceof Package) {
217 ExternLibrary cppLibrary = UMLUtil.getStereotypeApplication(owner, ExternLibrary.class);
218 if ((cppLibrary != null) && (settings != null)) {
219 settings.includePaths.addAll(cppLibrary.getIncludePaths());
220 for (String libPath : cppLibrary.getLibPaths()) {
221 if (isWsRelPath(libPath)) {
222
223
224
225
226
227
228
229 settings.libPaths.add("${workspace_loc:" + removeWsPrefix(libPath) + "}");
230 } else {
231
232 settings.libPaths.add(libPath);
233 }
234 }
235 settings.libs.addAll(cppLibrary.getLibs());
236 settings.macros.addAll(cppLibrary.getMacros());
237 }
238 owner = owner.getOwner();
239 }
240 }
241
242 public static String removeWsPrefix(String path) {
243 return path.substring(WS_PREFIX.length());
244 }
245
246
247
248
249 public static boolean isWsRelPath(String path) {
250 return path.startsWith(WS_PREFIX);
251
252 }
253
254 @Override
255 public IProject createProject(String projectName, Package modelRoot) {
256 return createProject(projectName);
257 }
258 }