The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

553 lines
21KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #include "jucer_ProjectExporter.h"
  19. #include "jucer_ProjectExport_Make.h"
  20. #include "jucer_ProjectExport_MSVC.h"
  21. #include "jucer_ProjectExport_XCode.h"
  22. #include "jucer_ProjectExport_Android.h"
  23. //==============================================================================
  24. int ProjectExporter::getNumExporters()
  25. {
  26. return 6;
  27. }
  28. StringArray ProjectExporter::getExporterNames()
  29. {
  30. StringArray s;
  31. s.add (XCodeProjectExporter::getNameMac());
  32. s.add (XCodeProjectExporter::getNameiOS());
  33. s.add (MSVCProjectExporterVC2005::getName());
  34. s.add (MSVCProjectExporterVC2008::getName());
  35. s.add (MSVCProjectExporterVC2010::getName());
  36. s.add (MakefileProjectExporter::getNameLinux());
  37. s.add (AndroidProjectExporter::getNameAndroid());
  38. return s;
  39. }
  40. StringArray ProjectExporter::getDefaultExporters()
  41. {
  42. StringArray s;
  43. s.add (XCodeProjectExporter::getNameMac());
  44. s.add (MSVCProjectExporterVC2008::getName());
  45. s.add (MSVCProjectExporterVC2010::getName());
  46. s.add (MakefileProjectExporter::getNameLinux());
  47. return s;
  48. }
  49. ProjectExporter* ProjectExporter::createNewExporter (Project& project, const int index)
  50. {
  51. ProjectExporter* exp = nullptr;
  52. switch (index)
  53. {
  54. case 0: exp = new XCodeProjectExporter (project, ValueTree (XCodeProjectExporter ::getValueTreeTypeName (false)), false); break;
  55. case 1: exp = new XCodeProjectExporter (project, ValueTree (XCodeProjectExporter ::getValueTreeTypeName (true)), true); break;
  56. case 2: exp = new MSVCProjectExporterVC2005 (project, ValueTree (MSVCProjectExporterVC2005::getValueTreeTypeName())); break;
  57. case 3: exp = new MSVCProjectExporterVC2008 (project, ValueTree (MSVCProjectExporterVC2008::getValueTreeTypeName())); break;
  58. case 4: exp = new MSVCProjectExporterVC2010 (project, ValueTree (MSVCProjectExporterVC2010::getValueTreeTypeName())); break;
  59. case 5: exp = new MakefileProjectExporter (project, ValueTree (MakefileProjectExporter ::getValueTreeTypeName())); break;
  60. case 6: exp = new AndroidProjectExporter (project, ValueTree (AndroidProjectExporter ::getValueTreeTypeName())); break;
  61. default: jassertfalse; return 0;
  62. }
  63. File juceFolder (ModuleList::getLocalModulesFolder (&project));
  64. File target (exp->getTargetFolder());
  65. if (FileHelpers::shouldPathsBeRelative (juceFolder.getFullPathName(), project.getFile().getFullPathName()))
  66. exp->getJuceFolderValue() = FileHelpers::getRelativePathFrom (juceFolder, project.getFile().getParentDirectory());
  67. else
  68. exp->getJuceFolderValue() = juceFolder.getFullPathName();
  69. exp->createDefaultConfigs();
  70. return exp;
  71. }
  72. ProjectExporter* ProjectExporter::createNewExporter (Project& project, const String& name)
  73. {
  74. return createNewExporter (project, getExporterNames().indexOf (name));
  75. }
  76. ProjectExporter* ProjectExporter::createExporter (Project& project, const ValueTree& settings)
  77. {
  78. ProjectExporter* exp = MSVCProjectExporterVC2005::createForSettings (project, settings);
  79. if (exp == nullptr) exp = MSVCProjectExporterVC2008::createForSettings (project, settings);
  80. if (exp == nullptr) exp = MSVCProjectExporterVC2010::createForSettings (project, settings);
  81. if (exp == nullptr) exp = XCodeProjectExporter ::createForSettings (project, settings);
  82. if (exp == nullptr) exp = MakefileProjectExporter ::createForSettings (project, settings);
  83. if (exp == nullptr) exp = AndroidProjectExporter ::createForSettings (project, settings);
  84. jassert (exp != nullptr);
  85. return exp;
  86. }
  87. ProjectExporter* ProjectExporter::createPlatformDefaultExporter (Project& project)
  88. {
  89. ScopedPointer <ProjectExporter> best;
  90. int bestPref = 0;
  91. for (Project::ExporterIterator exporter (project); exporter.next();)
  92. {
  93. const int pref = exporter->getLaunchPreferenceOrderForCurrentOS();
  94. if (pref > bestPref)
  95. {
  96. bestPref = pref;
  97. best = exporter.exporter;
  98. }
  99. }
  100. return best.release();
  101. }
  102. //==============================================================================
  103. ProjectExporter::ProjectExporter (Project& project_, const ValueTree& settings_)
  104. : xcodeIsBundle (false),
  105. xcodeCreatePList (false),
  106. xcodeCanUseDwarf (true),
  107. makefileIsDLL (false),
  108. msvcIsDLL (false),
  109. msvcIsWindowsSubsystem (true),
  110. msvcNeedsDLLRuntimeLib (false),
  111. settings (settings_),
  112. project (project_),
  113. projectType (project_.getProjectType()),
  114. projectName (project_.getProjectName().toString()),
  115. projectFolder (project_.getFile().getParentDirectory()),
  116. modulesGroup (nullptr)
  117. {
  118. groups.add (project.getMainGroup().createCopy());
  119. }
  120. ProjectExporter::~ProjectExporter()
  121. {
  122. }
  123. File ProjectExporter::getTargetFolder() const
  124. {
  125. return project.resolveFilename (getTargetLocationString());
  126. }
  127. String ProjectExporter::getIncludePathForFileInJuceFolder (const String& pathFromJuceFolder, const File& targetIncludeFile) const
  128. {
  129. String juceFolderPath (getJuceFolderString());
  130. if (juceFolderPath.startsWithChar ('<'))
  131. {
  132. juceFolderPath = FileHelpers::unixStylePath (File::addTrailingSeparator (juceFolderPath.substring (1).dropLastCharacters(1)));
  133. if (juceFolderPath == "/")
  134. juceFolderPath = String::empty;
  135. return "<" + juceFolderPath + pathFromJuceFolder + ">";
  136. }
  137. else
  138. {
  139. const RelativePath juceFromProject (juceFolderPath, RelativePath::projectFolder);
  140. const RelativePath fileFromProject (juceFromProject.getChildFile (pathFromJuceFolder));
  141. const RelativePath fileFromHere (fileFromProject.rebased (project.getFile().getParentDirectory(),
  142. targetIncludeFile.getParentDirectory(), RelativePath::unknown));
  143. return fileFromHere.toUnixStyle().quoted();
  144. }
  145. }
  146. RelativePath ProjectExporter::getJucePathFromProjectFolder() const
  147. {
  148. return RelativePath (getJuceFolderString(), RelativePath::projectFolder);
  149. }
  150. RelativePath ProjectExporter::getJucePathFromTargetFolder() const
  151. {
  152. return rebaseFromProjectFolderToBuildTarget (getJucePathFromProjectFolder());
  153. }
  154. RelativePath ProjectExporter::rebaseFromProjectFolderToBuildTarget (const RelativePath& path) const
  155. {
  156. return path.rebased (project.getFile().getParentDirectory(), getTargetFolder(), RelativePath::buildTargetFolder);
  157. }
  158. bool ProjectExporter::shouldFileBeCompiledByDefault (const RelativePath& file) const
  159. {
  160. return file.hasFileExtension ("cpp;cc;c;cxx");
  161. }
  162. void ProjectExporter::createPropertyEditors (PropertyListBuilder& props)
  163. {
  164. props.add (new TextPropertyComponent (getTargetLocationValue(), "Target Project Folder", 1024, false),
  165. "The location of the folder in which the " + name + " project will be created. This path can be absolute, but it's much more sensible to make it relative to the jucer project directory.");
  166. props.add (new TextPropertyComponent (getJuceFolderValue(), "Local JUCE folder", 1024, false),
  167. "The location of the Juce library folder that the " + name + " project will use to when compiling. This can be an absolute path, or relative to the jucer project folder, but it must be valid on the filesystem of the machine you use to actually do the compiling.");
  168. OwnedArray<LibraryModule> modules;
  169. ModuleList moduleList;
  170. moduleList.rescan (ModuleList::getDefaultModulesFolder (&project));
  171. project.createRequiredModules (moduleList, modules);
  172. for (int i = 0; i < modules.size(); ++i)
  173. modules.getUnchecked(i)->createPropertyEditors (*this, props);
  174. props.add (new TextPropertyComponent (getExporterPreprocessorDefs(), "Extra Preprocessor Definitions", 32768, false),
  175. "Extra preprocessor definitions. Use the form \"NAME1=value NAME2=value\", using whitespace or commas to separate the items - to include a space or comma in a definition, precede it with a backslash.");
  176. props.add (new TextPropertyComponent (getExtraCompilerFlags(), "Extra compiler flags", 2048, false),
  177. "Extra command-line flags to be passed to the compiler. This string can contain references to preprocessor definitions in the form ${NAME_OF_DEFINITION}, which will be replaced with their values.");
  178. props.add (new TextPropertyComponent (getExtraLinkerFlags(), "Extra linker flags", 2048, false),
  179. "Extra command-line flags to be passed to the linker. You might want to use this for adding additional libraries. This string can contain references to preprocessor definitions in the form ${NAME_OF_VALUE}, which will be replaced with their values.");
  180. {
  181. OwnedArray<Project::Item> images;
  182. project.findAllImageItems (images);
  183. StringArray choices;
  184. Array<var> ids;
  185. choices.add ("<None>");
  186. ids.add (var::null);
  187. choices.add (String::empty);
  188. ids.add (var::null);
  189. for (int i = 0; i < images.size(); ++i)
  190. {
  191. choices.add (images.getUnchecked(i)->getName());
  192. ids.add (images.getUnchecked(i)->getID());
  193. }
  194. props.add (new ChoicePropertyComponent (getSmallIconImageItemID(), "Icon (small)", choices, ids),
  195. "Sets an icon to use for the executable.");
  196. props.add (new ChoicePropertyComponent (getBigIconImageItemID(), "Icon (large)", choices, ids),
  197. "Sets an icon to use for the executable.");
  198. }
  199. }
  200. StringPairArray ProjectExporter::getAllPreprocessorDefs (const ProjectExporter::BuildConfiguration& config) const
  201. {
  202. StringPairArray defs (mergePreprocessorDefs (config.getAllPreprocessorDefs(),
  203. parsePreprocessorDefs (getExporterPreprocessorDefsString())));
  204. defs.set (getExporterIdentifierMacro(), "1");
  205. return defs;
  206. }
  207. StringPairArray ProjectExporter::getAllPreprocessorDefs() const
  208. {
  209. StringPairArray defs (mergePreprocessorDefs (project.getPreprocessorDefs(),
  210. parsePreprocessorDefs (getExporterPreprocessorDefsString())));
  211. defs.set (getExporterIdentifierMacro(), "1");
  212. return defs;
  213. }
  214. String ProjectExporter::replacePreprocessorTokens (const ProjectExporter::BuildConfiguration& config, const String& sourceString) const
  215. {
  216. return replacePreprocessorDefs (getAllPreprocessorDefs (config), sourceString);
  217. }
  218. Project::Item& ProjectExporter::getModulesGroup()
  219. {
  220. if (modulesGroup == nullptr)
  221. {
  222. groups.add (Project::Item::createGroup (project, "Juce Modules", "__modulesgroup__"));
  223. modulesGroup = &(groups.getReference (groups.size() - 1));
  224. }
  225. return *modulesGroup;
  226. }
  227. void ProjectExporter::addToExtraSearchPaths (const RelativePath& pathFromProjectFolder)
  228. {
  229. RelativePath localPath (rebaseFromProjectFolderToBuildTarget (pathFromProjectFolder));
  230. const String path (isVisualStudio() ? localPath.toWindowsStyle() : localPath.toUnixStyle());
  231. extraSearchPaths.addIfNotAlreadyThere (path, false);
  232. }
  233. //==============================================================================
  234. const Identifier ProjectExporter::configurations ("CONFIGURATIONS");
  235. const Identifier ProjectExporter::configuration ("CONFIGURATION");
  236. ValueTree ProjectExporter::getConfigurations() const
  237. {
  238. return settings.getChildWithName (configurations);
  239. }
  240. int ProjectExporter::getNumConfigurations() const
  241. {
  242. return getConfigurations().getNumChildren();
  243. }
  244. ProjectExporter::BuildConfiguration::Ptr ProjectExporter::getConfiguration (int index) const
  245. {
  246. return createBuildConfig (getConfigurations().getChild (index));
  247. }
  248. bool ProjectExporter::hasConfigurationNamed (const String& name) const
  249. {
  250. const ValueTree configs (getConfigurations());
  251. for (int i = configs.getNumChildren(); --i >= 0;)
  252. if (configs.getChild(i) [Ids::name].toString() == name)
  253. return true;
  254. return false;
  255. }
  256. String ProjectExporter::getUniqueConfigName (String name) const
  257. {
  258. String nameRoot (name);
  259. while (CharacterFunctions::isDigit (nameRoot.getLastCharacter()))
  260. nameRoot = nameRoot.dropLastCharacters (1);
  261. nameRoot = nameRoot.trim();
  262. int suffix = 2;
  263. while (hasConfigurationNamed (name))
  264. name = nameRoot + " " + String (suffix++);
  265. return name;
  266. }
  267. void ProjectExporter::addNewConfiguration (const BuildConfiguration* configToCopy)
  268. {
  269. const String configName (getUniqueConfigName (configToCopy != nullptr ? configToCopy->config [Ids::name].toString()
  270. : "New Build Configuration"));
  271. ValueTree configs (getConfigurations());
  272. if (! configs.isValid())
  273. {
  274. settings.addChild (ValueTree (configurations), 0, project.getUndoManagerFor (settings));
  275. configs = getConfigurations();
  276. }
  277. ValueTree newConfig (configuration);
  278. if (configToCopy != nullptr)
  279. newConfig = configToCopy->config.createCopy();
  280. newConfig.setProperty (Ids::name, configName, 0);
  281. configs.addChild (newConfig, -1, project.getUndoManagerFor (configs));
  282. }
  283. void ProjectExporter::deleteConfiguration (int index)
  284. {
  285. ValueTree configs (getConfigurations());
  286. configs.removeChild (index, project.getUndoManagerFor (configs));
  287. }
  288. void ProjectExporter::createDefaultConfigs()
  289. {
  290. settings.getOrCreateChildWithName (configurations, nullptr);
  291. for (int i = 0; i < 2; ++i)
  292. {
  293. addNewConfiguration (nullptr);
  294. BuildConfiguration::Ptr config (getConfiguration (i));
  295. const bool debugConfig = i == 0;
  296. config->getNameValue() = debugConfig ? "Debug" : "Release";
  297. config->isDebugValue() = debugConfig;
  298. config->getOptimisationLevel() = debugConfig ? 1 : 2;
  299. config->getTargetBinaryName() = project.getProjectFilenameRoot();
  300. }
  301. }
  302. Image ProjectExporter::getBigIcon() const
  303. {
  304. return project.getMainGroup().findItemWithID (settings [Ids::bigIcon]).loadAsImageFile();
  305. }
  306. Image ProjectExporter::getSmallIcon() const
  307. {
  308. return project.getMainGroup().findItemWithID (settings [Ids::smallIcon]).loadAsImageFile();
  309. }
  310. Image ProjectExporter::getBestIconForSize (int size, bool returnNullIfNothingBigEnough) const
  311. {
  312. Image im;
  313. const Image im1 (getSmallIcon());
  314. const Image im2 (getBigIcon());
  315. if (im1.isValid() && im2.isValid())
  316. {
  317. if (im1.getWidth() >= size && im2.getWidth() >= size)
  318. im = im1.getWidth() < im2.getWidth() ? im1 : im2;
  319. else if (im1.getWidth() >= size)
  320. im = im1;
  321. else if (im2.getWidth() >= size)
  322. im = im2;
  323. else
  324. return Image::null;
  325. }
  326. else
  327. {
  328. im = im1.isValid() ? im1 : im2;
  329. }
  330. if (returnNullIfNothingBigEnough && im.getWidth() < size && im.getHeight() < size)
  331. return Image::null;
  332. return rescaleImageForIcon (im, size);
  333. }
  334. Image ProjectExporter::rescaleImageForIcon (Image im, const int size)
  335. {
  336. im = SoftwareImageType().convert (im);
  337. if (size == im.getWidth() && size == im.getHeight())
  338. return im;
  339. // (scale it down in stages for better resampling)
  340. while (im.getWidth() > 2 * size && im.getHeight() > 2 * size)
  341. im = im.rescaled (im.getWidth() / 2,
  342. im.getHeight() / 2);
  343. Image newIm (Image::ARGB, size, size, true, SoftwareImageType());
  344. Graphics g (newIm);
  345. g.drawImageWithin (im, 0, 0, size, size,
  346. RectanglePlacement::centred | RectanglePlacement::onlyReduceInSize, false);
  347. return newIm;
  348. }
  349. //==============================================================================
  350. ProjectExporter::ConfigIterator::ConfigIterator (ProjectExporter& exporter_)
  351. : index (-1), exporter (exporter_)
  352. {
  353. }
  354. bool ProjectExporter::ConfigIterator::next()
  355. {
  356. if (++index >= exporter.getNumConfigurations())
  357. return false;
  358. config = exporter.getConfiguration (index);
  359. return true;
  360. }
  361. ProjectExporter::ConstConfigIterator::ConstConfigIterator (const ProjectExporter& exporter_)
  362. : index (-1), exporter (exporter_)
  363. {
  364. }
  365. bool ProjectExporter::ConstConfigIterator::next()
  366. {
  367. if (++index >= exporter.getNumConfigurations())
  368. return false;
  369. config = exporter.getConfiguration (index);
  370. return true;
  371. }
  372. //==============================================================================
  373. ProjectExporter::BuildConfiguration::BuildConfiguration (Project& project_, const ValueTree& configNode)
  374. : config (configNode), project (project_)
  375. {
  376. }
  377. ProjectExporter::BuildConfiguration::~BuildConfiguration()
  378. {
  379. }
  380. String ProjectExporter::BuildConfiguration::getGCCOptimisationFlag() const
  381. {
  382. const int level = getOptimisationLevelInt();
  383. return String (level <= 1 ? "0" : (level == 2 ? "s" : "3"));
  384. }
  385. void ProjectExporter::BuildConfiguration::createBasicPropertyEditors (PropertyListBuilder& props)
  386. {
  387. props.add (new TextPropertyComponent (getNameValue(), "Name", 96, false),
  388. "The name of this configuration.");
  389. props.add (new BooleanPropertyComponent (isDebugValue(), "Debug mode", "Debugging enabled"),
  390. "If enabled, this means that the configuration should be built with debug synbols.");
  391. const char* optimisationLevels[] = { "No optimisation", "Optimise for size and speed", "Optimise for maximum speed", 0 };
  392. const int optimisationLevelValues[] = { 1, 2, 3, 0 };
  393. props.add (new ChoicePropertyComponent (getOptimisationLevel(), "Optimisation",
  394. StringArray (optimisationLevels), Array<var> (optimisationLevelValues)),
  395. "The optimisation level for this configuration");
  396. props.add (new TextPropertyComponent (getTargetBinaryName(), "Binary name", 256, false),
  397. "The filename to use for the destination binary executable file. If you don't add a suffix to this name, "
  398. "a suitable platform-specific suffix will be added automatically.");
  399. props.add (new TextPropertyComponent (getTargetBinaryRelativePath(), "Binary location", 1024, false),
  400. "The folder in which the finished binary should be placed. Leave this blank to cause the binary to be placed "
  401. "in its default location in the build folder.");
  402. props.add (new TextPropertyComponent (getHeaderSearchPathValue(), "Header search paths", 16384, false),
  403. "Extra header search paths. Use semi-colons to separate multiple paths.");
  404. props.add (new TextPropertyComponent (getLibrarySearchPathValue(), "Extra library search paths", 16384, false),
  405. "Extra library search paths. Use semi-colons to separate multiple paths.");
  406. props.add (new TextPropertyComponent (getBuildConfigPreprocessorDefs(), "Preprocessor definitions", 32768, false),
  407. "Extra preprocessor definitions. Use the form \"NAME1=value NAME2=value\", using whitespace or commas to separate "
  408. "the items - to include a space or comma in a definition, precede it with a backslash.");
  409. props.setPreferredHeight (22);
  410. }
  411. StringPairArray ProjectExporter::BuildConfiguration::getAllPreprocessorDefs() const
  412. {
  413. return mergePreprocessorDefs (project.getPreprocessorDefs(),
  414. parsePreprocessorDefs (getBuildConfigPreprocessorDefsString()));
  415. }
  416. StringArray ProjectExporter::BuildConfiguration::getHeaderSearchPaths() const
  417. {
  418. StringArray s;
  419. s.addTokens (getHeaderSearchPathString(), ";", String::empty);
  420. s.trim();
  421. s.removeEmptyStrings();
  422. s.removeDuplicates (false);
  423. return s;
  424. }
  425. StringArray ProjectExporter::BuildConfiguration::getLibrarySearchPaths() const
  426. {
  427. StringArray s;
  428. s.addTokens (getLibrarySearchPathString(), ";", String::empty);
  429. s.trim();
  430. s.removeEmptyStrings();
  431. s.removeDuplicates (false);
  432. return s;
  433. }
  434. String ProjectExporter::BuildConfiguration::getGCCLibraryPathFlags() const
  435. {
  436. String s;
  437. const StringArray libraryPaths (getLibrarySearchPaths());
  438. for (int i = 0; i < libraryPaths.size(); ++i)
  439. s << " -L" << addQuotesIfContainsSpaces (libraryPaths[i]);
  440. return s;
  441. }