CreateNativeFolders.java 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. ///////////////////////////////////////////////////////////////////////////
  2. // Copyright (c) 2018 Intel Corporation. //
  3. // //
  4. // All rights reserved. This program and the accompanying materials //
  5. // are made available under the terms of the Eclipse Public License v1.0 //
  6. // which accompanies this distribution, and is available at //
  7. // http://www.eclipse.org/legal/epl-v10.html //
  8. // //
  9. // Contributors: //
  10. // Intel Corporation - initial implementation and documentation //
  11. ///////////////////////////////////////////////////////////////////////////
  12. package com.intel.sgx.templates;
  13. import java.util.ArrayList;
  14. import java.util.List;
  15. import org.eclipse.cdt.core.CCorePlugin;
  16. import org.eclipse.cdt.core.model.CoreModel;
  17. import org.eclipse.cdt.core.model.ICProject;
  18. import org.eclipse.cdt.core.model.IPathEntry;
  19. import org.eclipse.cdt.core.settings.model.CIncludePathEntry;
  20. import org.eclipse.cdt.core.settings.model.ICConfigurationDescription;
  21. import org.eclipse.cdt.core.settings.model.ICFolderDescription;
  22. import org.eclipse.cdt.core.settings.model.ICLanguageSetting;
  23. import org.eclipse.cdt.core.settings.model.ICLanguageSettingEntry;
  24. import org.eclipse.cdt.core.settings.model.ICProjectDescription;
  25. import org.eclipse.cdt.core.settings.model.ICSettingEntry;
  26. import org.eclipse.cdt.core.templateengine.TemplateCore;
  27. import org.eclipse.cdt.core.templateengine.process.ProcessArgument;
  28. import org.eclipse.cdt.core.templateengine.process.ProcessFailureException;
  29. import org.eclipse.cdt.core.templateengine.process.ProcessRunner;
  30. import org.eclipse.cdt.managedbuilder.core.IConfiguration;
  31. import org.eclipse.cdt.managedbuilder.core.ManagedBuildManager;
  32. import org.eclipse.cdt.managedbuilder.internal.core.Configuration;
  33. import org.eclipse.cdt.managedbuilder.internal.core.ManagedProject;
  34. import org.eclipse.core.resources.IFolder;
  35. import org.eclipse.core.resources.IProject;
  36. import org.eclipse.core.resources.IResource;
  37. import org.eclipse.core.resources.ResourcesPlugin;
  38. import org.eclipse.core.runtime.CoreException;
  39. import org.eclipse.core.runtime.IProgressMonitor;
  40. import org.eclipse.core.runtime.Path;
  41. import com.intel.sgx.Activator;
  42. import com.intel.sgx.Messages;
  43. import com.intel.sgx.preferences.PreferenceConstants;
  44. @SuppressWarnings("restriction")
  45. public class CreateNativeFolders extends ProcessRunner {
  46. @Override
  47. public void process(TemplateCore template, ProcessArgument[] args,
  48. String processId, IProgressMonitor monitor)
  49. throws ProcessFailureException {
  50. String projectName = null;
  51. String[] sourceFolders = null;
  52. String[] outputFolders = null;
  53. for (ProcessArgument arg : args) {
  54. String argName = arg.getName();
  55. if (argName.equals("projectName")) {
  56. projectName = arg.getSimpleValue();
  57. } else if (argName.equals("sourceFolders")) {
  58. sourceFolders = arg.getSimpleArrayValue();
  59. } else if (argName.equals("outputFolders")) {
  60. outputFolders = arg.getSimpleArrayValue();
  61. }
  62. }
  63. if (projectName == null)
  64. throw new ProcessFailureException(
  65. Messages.CreateNativeFolders_Missing_project_name);
  66. IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
  67. if (!project.exists())
  68. throw new ProcessFailureException(
  69. Messages.CreateNativeFolders_Project_does_not_exist);
  70. if (sourceFolders == null && outputFolders == null)
  71. throw new ProcessFailureException(
  72. Messages.CreateNativeFolders_No_folders);
  73. try {
  74. ICProject cproject = CCorePlugin.getDefault().getCoreModel()
  75. .create(project);
  76. IPathEntry[] pathEntries = cproject.getRawPathEntries();
  77. List<IPathEntry> newEntries = new ArrayList<IPathEntry>(
  78. pathEntries.length);
  79. for (IPathEntry pathEntry : pathEntries) {
  80. if (pathEntry.getEntryKind() != IPathEntry.CDT_SOURCE
  81. && pathEntry.getEntryKind() != IPathEntry.CDT_OUTPUT) {
  82. newEntries.add(pathEntry);
  83. }
  84. }
  85. if (sourceFolders != null)
  86. for (String sourceFolder : sourceFolders) {
  87. IFolder folder = project.getFolder(new Path(sourceFolder));
  88. if (!folder.exists())
  89. folder.create(true, true, monitor);
  90. newEntries.add(CoreModel.newSourceEntry(folder
  91. .getFullPath()));
  92. }
  93. if (outputFolders != null)
  94. for (String outputFolder : outputFolders) {
  95. IFolder folder = project.getFolder(new Path(outputFolder));
  96. if (!folder.exists())
  97. folder.create(true, true, monitor);
  98. newEntries.add(CoreModel.newOutputEntry(folder
  99. .getFullPath()));
  100. }
  101. cproject.setRawPathEntries(
  102. newEntries.toArray(new IPathEntry[newEntries.size()]),
  103. monitor);
  104. // IConfiguration[] configs = managedProject.getConfigurations();
  105. // for(IConfiguration conf:configs){
  106. // managedProject.removeConfiguration(conf.getId());
  107. // }
  108. IConfiguration conSimDebug = ManagedBuildManager
  109. .getExtensionConfiguration("com.intel.sgx.configuration.Sim.Debug");
  110. IConfiguration conSimRelease = ManagedBuildManager
  111. .getExtensionConfiguration("com.intel.sgx.configuration.Sim.Release");
  112. IConfiguration conHwDebug = ManagedBuildManager
  113. .getExtensionConfiguration("com.intel.sgx.configuration.HW.Debug");
  114. IConfiguration conHwPrerelease = ManagedBuildManager
  115. .getExtensionConfiguration("com.intel.sgx.configuration.HW.Prerelease");
  116. IConfiguration conHwRelease = ManagedBuildManager
  117. .getExtensionConfiguration("com.intel.sgx.configuration.HW.Release");
  118. addConfigurationToProject(project, conSimDebug);
  119. addConfigurationToProject(project, conSimRelease);
  120. addConfigurationToProject(project, conHwDebug);
  121. addConfigurationToProject(project, conHwPrerelease);
  122. addConfigurationToProject(project, conHwRelease);
  123. changeProjectConfiguration(project, conSimDebug);
  124. project.refreshLocal(IResource.DEPTH_INFINITE, null);
  125. } catch (CoreException e) {
  126. throw new ProcessFailureException(e);
  127. }
  128. }
  129. void addConfigurationToProject(IProject project, IConfiguration config) {
  130. createConfiguration(project, config);
  131. addSGXIncludePathsToConfiguration(project, config);
  132. }
  133. private void addSGXIncludePathsToConfiguration(IProject project,
  134. IConfiguration config) {
  135. ICProjectDescription projectDescription = CoreModel.getDefault()
  136. .getProjectDescription(project, true);
  137. ICConfigurationDescription configDecriptions[] = projectDescription
  138. .getConfigurations();
  139. for (ICConfigurationDescription configDescription : configDecriptions) {
  140. ICFolderDescription projectRoot = configDescription
  141. .getRootFolderDescription();
  142. ICLanguageSetting[] settings = projectRoot.getLanguageSettings();
  143. for (ICLanguageSetting setting : settings) {
  144. if (!"org.eclipse.cdt.core.gcc".equals(setting.getLanguageId()) && !"org.eclipse.cdt.core.g++".equals(setting.getLanguageId()) ) {
  145. continue;
  146. }
  147. List<ICLanguageSettingEntry> includes = new ArrayList<ICLanguageSettingEntry>();
  148. includes.add(new CIncludePathEntry( Activator.getDefault().getPreferenceStore().getString(PreferenceConstants.SDK_PATH) +
  149. "/include/",
  150. ICSettingEntry.LOCAL));
  151. setting.setSettingEntries(ICSettingEntry.INCLUDE_PATH, includes);
  152. }
  153. }
  154. try {
  155. CoreModel.getDefault().setProjectDescription(project,
  156. projectDescription);
  157. } catch (CoreException e) {
  158. e.printStackTrace();
  159. }
  160. }
  161. private void createConfiguration(IProject project,
  162. IConfiguration config) {
  163. ManagedProject managedProject = (ManagedProject) ManagedBuildManager.getBuildInfo(project)
  164. .getManagedProject();;
  165. Configuration cloneConfig1 = (Configuration) config;
  166. Configuration cfg1 = new Configuration(managedProject, cloneConfig1,
  167. cloneConfig1.getId(), false, false);
  168. String target = cfg1.getArtifactName();
  169. if (target == null || target.length() == 0)
  170. cfg1.setArtifactName(managedProject.getDefaultArtifactName());
  171. cfg1.exportArtifactInfo();
  172. ManagedBuildManager.saveBuildInfo(project, true);
  173. }
  174. private void changeProjectConfiguration(IProject project,
  175. IConfiguration conSimDebug) {
  176. ICProjectDescription prjd = CCorePlugin.getDefault()
  177. .getProjectDescriptionManager().getProjectDescription(project);
  178. ICConfigurationDescription[] configs = prjd.getConfigurations();
  179. if (configs != null && configs.length > 0) {
  180. for (ICConfigurationDescription config : configs) {
  181. if (config.getConfiguration().getId()
  182. .equals(conSimDebug.getId())) {
  183. config.setActive();
  184. try {
  185. CoreModel.getDefault().setProjectDescription(project,
  186. prjd);
  187. } catch (CoreException e) {
  188. // TODO Auto-generated catch block
  189. e.printStackTrace();
  190. }
  191. break;
  192. }
  193. }
  194. }
  195. }
  196. }