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.

563 lines
22KB

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