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.

579 lines
23KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #include "jucer_ProjectExporter.h"
  18. #include "jucer_ProjectSaver.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. #include "jucer_ProjectExport_CodeBlocks.h"
  24. //==============================================================================
  25. StringArray ProjectExporter::getExporterNames()
  26. {
  27. StringArray s;
  28. s.add (XCodeProjectExporter::getNameMac());
  29. s.add (XCodeProjectExporter::getNameiOS());
  30. s.add (MSVCProjectExporterVC2005::getName());
  31. s.add (MSVCProjectExporterVC2008::getName());
  32. s.add (MSVCProjectExporterVC2010::getName());
  33. s.add (MSVCProjectExporterVC2012::getName());
  34. s.add (MSVCProjectExporterVC2013::getName());
  35. s.add (MakefileProjectExporter::getNameLinux());
  36. s.add (AndroidProjectExporter::getNameAndroid());
  37. s.add (CodeBlocksProjectExporter::getNameCodeBlocks());
  38. return s;
  39. }
  40. String ProjectExporter::getCurrentPlatformExporterName()
  41. {
  42. #if JUCE_MAC
  43. return XCodeProjectExporter::getNameMac();
  44. #elif JUCE_WINDOWS
  45. return MSVCProjectExporterVC2010::getName();
  46. #elif JUCE_LINUX
  47. return MakefileProjectExporter::getNameLinux();
  48. #else
  49. #error // huh?
  50. #endif
  51. }
  52. ProjectExporter* ProjectExporter::createNewExporter (Project& project, const int index)
  53. {
  54. ProjectExporter* exp = nullptr;
  55. switch (index)
  56. {
  57. case 0: exp = new XCodeProjectExporter (project, ValueTree (XCodeProjectExporter ::getValueTreeTypeName (false)), false); break;
  58. case 1: exp = new XCodeProjectExporter (project, ValueTree (XCodeProjectExporter ::getValueTreeTypeName (true)), true); break;
  59. case 2: exp = new MSVCProjectExporterVC2005 (project, ValueTree (MSVCProjectExporterVC2005::getValueTreeTypeName())); break;
  60. case 3: exp = new MSVCProjectExporterVC2008 (project, ValueTree (MSVCProjectExporterVC2008::getValueTreeTypeName())); break;
  61. case 4: exp = new MSVCProjectExporterVC2010 (project, ValueTree (MSVCProjectExporterVC2010::getValueTreeTypeName())); break;
  62. case 5: exp = new MSVCProjectExporterVC2012 (project, ValueTree (MSVCProjectExporterVC2012::getValueTreeTypeName())); break;
  63. case 6: exp = new MSVCProjectExporterVC2013 (project, ValueTree (MSVCProjectExporterVC2013::getValueTreeTypeName())); break;
  64. case 7: exp = new MakefileProjectExporter (project, ValueTree (MakefileProjectExporter ::getValueTreeTypeName())); break;
  65. case 8: exp = new AndroidProjectExporter (project, ValueTree (AndroidProjectExporter ::getValueTreeTypeName())); break;
  66. case 9: exp = new CodeBlocksProjectExporter (project, ValueTree (CodeBlocksProjectExporter::getValueTreeTypeName())); break;
  67. default: jassertfalse; return 0;
  68. }
  69. File juceFolder (ModuleList::getLocalModulesFolder (&project));
  70. File target (exp->getTargetFolder());
  71. if (FileHelpers::shouldPathsBeRelative (juceFolder.getFullPathName(), project.getFile().getFullPathName()))
  72. exp->getJuceFolderValue() = FileHelpers::getRelativePathFrom (juceFolder, project.getFile().getParentDirectory());
  73. else
  74. exp->getJuceFolderValue() = juceFolder.getFullPathName();
  75. exp->createDefaultConfigs();
  76. return exp;
  77. }
  78. ProjectExporter* ProjectExporter::createNewExporter (Project& project, const String& name)
  79. {
  80. return createNewExporter (project, getExporterNames().indexOf (name));
  81. }
  82. ProjectExporter* ProjectExporter::createExporter (Project& project, const ValueTree& settings)
  83. {
  84. ProjectExporter* exp = MSVCProjectExporterVC2005::createForSettings (project, settings);
  85. if (exp == nullptr) exp = MSVCProjectExporterVC2008::createForSettings (project, settings);
  86. if (exp == nullptr) exp = MSVCProjectExporterVC2010::createForSettings (project, settings);
  87. if (exp == nullptr) exp = MSVCProjectExporterVC2012::createForSettings (project, settings);
  88. if (exp == nullptr) exp = MSVCProjectExporterVC2013::createForSettings (project, settings);
  89. if (exp == nullptr) exp = XCodeProjectExporter ::createForSettings (project, settings);
  90. if (exp == nullptr) exp = MakefileProjectExporter ::createForSettings (project, settings);
  91. if (exp == nullptr) exp = AndroidProjectExporter ::createForSettings (project, settings);
  92. if (exp == nullptr) exp = CodeBlocksProjectExporter::createForSettings (project, settings);
  93. jassert (exp != nullptr);
  94. return exp;
  95. }
  96. bool ProjectExporter::canProjectBeLaunched (Project* project)
  97. {
  98. if (project != nullptr)
  99. {
  100. const char* types[] =
  101. {
  102. #if JUCE_MAC
  103. XCodeProjectExporter::getValueTreeTypeName (false),
  104. XCodeProjectExporter::getValueTreeTypeName (true),
  105. #elif JUCE_WINDOWS
  106. MSVCProjectExporterVC2005::getValueTreeTypeName(),
  107. MSVCProjectExporterVC2008::getValueTreeTypeName(),
  108. MSVCProjectExporterVC2010::getValueTreeTypeName(),
  109. MSVCProjectExporterVC2012::getValueTreeTypeName(),
  110. MSVCProjectExporterVC2013::getValueTreeTypeName(),
  111. #elif JUCE_LINUX
  112. // (this doesn't currently launch.. not really sure what it would do on linux)
  113. //MakefileProjectExporter::getValueTreeTypeName(),
  114. #endif
  115. nullptr
  116. };
  117. for (const char** type = types; *type != nullptr; ++type)
  118. if (project->getExporters().getChildWithName (*type).isValid())
  119. return true;
  120. }
  121. return false;
  122. }
  123. //==============================================================================
  124. ProjectExporter::ProjectExporter (Project& project_, const ValueTree& settings_)
  125. : xcodeIsBundle (false),
  126. xcodeCreatePList (false),
  127. xcodeCanUseDwarf (true),
  128. makefileIsDLL (false),
  129. msvcIsDLL (false),
  130. msvcIsWindowsSubsystem (true),
  131. settings (settings_),
  132. project (project_),
  133. projectType (project_.getProjectType()),
  134. projectName (project_.getTitle()),
  135. projectFolder (project_.getFile().getParentDirectory()),
  136. modulesGroup (nullptr)
  137. {
  138. }
  139. ProjectExporter::~ProjectExporter()
  140. {
  141. }
  142. File ProjectExporter::getTargetFolder() const
  143. {
  144. return project.resolveFilename (getTargetLocationString());
  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. "
  166. "This path can be absolute, but it's much more sensible to make it relative to the jucer project directory.");
  167. props.add (new TextPropertyComponent (getJuceFolderValue(), "Local JUCE folder", 1024, false),
  168. "The location of the Juce library folder that the " + name + " project will use to when compiling. "
  169. "This can be an absolute path, or relative to the jucer project folder, but it must be valid on the "
  170. "filesystem of the machine you use to actually do the compiling.");
  171. OwnedArray<LibraryModule> modules;
  172. ModuleList moduleList;
  173. moduleList.rescan (ModuleList::getDefaultModulesFolder (&project));
  174. project.createRequiredModules (moduleList, modules);
  175. for (int i = 0; i < modules.size(); ++i)
  176. modules.getUnchecked(i)->createPropertyEditors (*this, props);
  177. props.add (new TextPropertyComponent (getExporterPreprocessorDefs(), "Extra Preprocessor Definitions", 32768, true),
  178. "Extra preprocessor definitions. Use the form \"NAME1=value NAME2=value\", using whitespace, commas, "
  179. "or new-lines to separate the items - to include a space or comma in a definition, precede it with a backslash.");
  180. props.add (new TextPropertyComponent (getExtraCompilerFlags(), "Extra compiler flags", 2048, true),
  181. "Extra command-line flags to be passed to the compiler. This string can contain references to preprocessor definitions in the "
  182. "form ${NAME_OF_DEFINITION}, which will be replaced with their values.");
  183. props.add (new TextPropertyComponent (getExtraLinkerFlags(), "Extra linker flags", 2048, true),
  184. "Extra command-line flags to be passed to the linker. You might want to use this for adding additional libraries. "
  185. "This string can contain references to preprocessor definitions in the form ${NAME_OF_VALUE}, which will be replaced with their values.");
  186. props.add (new TextPropertyComponent (getExternalLibraries(), "External libraries to link", 2048, true),
  187. "Additional libraries to link (one per line). You should not add any platform specific decoration to these names. "
  188. "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& nameToFind) const
  267. {
  268. const ValueTree configs (getConfigurations());
  269. for (int i = configs.getNumChildren(); --i >= 0;)
  270. if (configs.getChild(i) [Ids::name].toString() == nameToFind)
  271. return true;
  272. return false;
  273. }
  274. String ProjectExporter::getUniqueConfigName (String nm) const
  275. {
  276. String nameRoot (nm);
  277. while (CharacterFunctions::isDigit (nameRoot.getLastCharacter()))
  278. nameRoot = nameRoot.dropLastCharacters (1);
  279. nameRoot = nameRoot.trim();
  280. int suffix = 2;
  281. while (hasConfigurationNamed (name))
  282. nm = nameRoot + " " + String (suffix++);
  283. return nm;
  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 ? optimisationOff : optimiseMinSize;
  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. switch (getOptimisationLevelInt())
  401. {
  402. case optimiseMaxSpeed: return "3";
  403. case optimiseMinSize: return "s";
  404. default: return "0";
  405. }
  406. }
  407. void ProjectExporter::BuildConfiguration::createPropertyEditors (PropertyListBuilder& props)
  408. {
  409. props.add (new TextPropertyComponent (getNameValue(), "Name", 96, false),
  410. "The name of this configuration.");
  411. props.add (new BooleanPropertyComponent (isDebugValue(), "Debug mode", "Debugging enabled"),
  412. "If enabled, this means that the configuration should be built with debug synbols.");
  413. const char* optimisationLevels[] = { "No optimisation", "Minimise size", "Maximise speed", 0 };
  414. const int optimisationLevelValues[] = { optimisationOff, optimiseMinSize, optimiseMaxSpeed, 0 };
  415. props.add (new ChoicePropertyComponent (getOptimisationLevel(), "Optimisation",
  416. StringArray (optimisationLevels), Array<var> (optimisationLevelValues)),
  417. "The optimisation level for this configuration");
  418. props.add (new TextPropertyComponent (getTargetBinaryName(), "Binary name", 256, false),
  419. "The filename to use for the destination binary executable file. If you don't add a suffix to this name, "
  420. "a suitable platform-specific suffix will be added automatically.");
  421. props.add (new TextPropertyComponent (getTargetBinaryRelativePath(), "Binary location", 1024, false),
  422. "The folder in which the finished binary should be placed. Leave this blank to cause the binary to be placed "
  423. "in its default location in the build folder.");
  424. props.addSearchPathProperty (getHeaderSearchPathValue(), "Header search paths", "Extra header search paths.");
  425. props.addSearchPathProperty (getLibrarySearchPathValue(), "Extra library search paths", "Extra library search paths.");
  426. props.add (new TextPropertyComponent (getBuildConfigPreprocessorDefs(), "Preprocessor definitions", 32768, true),
  427. "Extra preprocessor definitions. Use the form \"NAME1=value NAME2=value\", using whitespace, commas, or "
  428. "new-lines to separate the items - to include a space or comma in a definition, precede it with a backslash.");
  429. createConfigProperties (props);
  430. props.add (new TextPropertyComponent (getUserNotes(), "Notes", 32768, true),
  431. "Extra comments: This field is not used for code or project generation, it's just a space where you can express your thoughts.");
  432. }
  433. StringPairArray ProjectExporter::BuildConfiguration::getAllPreprocessorDefs() const
  434. {
  435. return mergePreprocessorDefs (project.getPreprocessorDefs(),
  436. parsePreprocessorDefs (getBuildConfigPreprocessorDefsString()));
  437. }
  438. StringArray ProjectExporter::BuildConfiguration::getHeaderSearchPaths() const
  439. {
  440. return getSearchPathsFromString (getHeaderSearchPathString());
  441. }
  442. StringArray ProjectExporter::BuildConfiguration::getLibrarySearchPaths() const
  443. {
  444. return getSearchPathsFromString (getLibrarySearchPathString());
  445. }
  446. String ProjectExporter::BuildConfiguration::getGCCLibraryPathFlags() const
  447. {
  448. String s;
  449. const StringArray libraryPaths (getLibrarySearchPaths());
  450. for (int i = 0; i < libraryPaths.size(); ++i)
  451. s << " -L" << addQuotesIfContainsSpaces (libraryPaths[i]);
  452. return s;
  453. }
  454. String ProjectExporter::getExternalLibraryFlags (const BuildConfiguration& config) const
  455. {
  456. StringArray libraries;
  457. libraries.addTokens (getExternalLibrariesString(), ";\n", "\"'");
  458. libraries.removeEmptyStrings (true);
  459. if (libraries.size() != 0)
  460. return replacePreprocessorTokens (config, "-l" + libraries.joinIntoString (" -l")).trim();
  461. return String::empty;
  462. }