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.

575 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 (MakefileProjectExporter::getNameLinux());
  35. s.add (AndroidProjectExporter::getNameAndroid());
  36. s.add (CodeBlocksProjectExporter::getNameCodeBlocks());
  37. return s;
  38. }
  39. String ProjectExporter::getCurrentPlatformExporterName()
  40. {
  41. #if JUCE_MAC
  42. return XCodeProjectExporter::getNameMac();
  43. #elif JUCE_WINDOWS
  44. return MSVCProjectExporterVC2010::getName();
  45. #elif JUCE_LINUX
  46. return MakefileProjectExporter::getNameLinux();
  47. #else
  48. #error // huh?
  49. #endif
  50. }
  51. ProjectExporter* ProjectExporter::createNewExporter (Project& project, const int index)
  52. {
  53. ProjectExporter* exp = nullptr;
  54. switch (index)
  55. {
  56. case 0: exp = new XCodeProjectExporter (project, ValueTree (XCodeProjectExporter ::getValueTreeTypeName (false)), false); break;
  57. case 1: exp = new XCodeProjectExporter (project, ValueTree (XCodeProjectExporter ::getValueTreeTypeName (true)), true); break;
  58. case 2: exp = new MSVCProjectExporterVC2005 (project, ValueTree (MSVCProjectExporterVC2005::getValueTreeTypeName())); break;
  59. case 3: exp = new MSVCProjectExporterVC2008 (project, ValueTree (MSVCProjectExporterVC2008::getValueTreeTypeName())); break;
  60. case 4: exp = new MSVCProjectExporterVC2010 (project, ValueTree (MSVCProjectExporterVC2010::getValueTreeTypeName())); break;
  61. case 5: exp = new MSVCProjectExporterVC2012 (project, ValueTree (MSVCProjectExporterVC2012::getValueTreeTypeName())); break;
  62. case 6: exp = new MakefileProjectExporter (project, ValueTree (MakefileProjectExporter ::getValueTreeTypeName())); break;
  63. case 7: exp = new AndroidProjectExporter (project, ValueTree (AndroidProjectExporter ::getValueTreeTypeName())); break;
  64. case 8: exp = new CodeBlocksProjectExporter (project, ValueTree (CodeBlocksProjectExporter::getValueTreeTypeName())); break;
  65. default: jassertfalse; return 0;
  66. }
  67. File juceFolder (ModuleList::getLocalModulesFolder (&project));
  68. File target (exp->getTargetFolder());
  69. if (FileHelpers::shouldPathsBeRelative (juceFolder.getFullPathName(), project.getFile().getFullPathName()))
  70. exp->getJuceFolderValue() = FileHelpers::getRelativePathFrom (juceFolder, project.getFile().getParentDirectory());
  71. else
  72. exp->getJuceFolderValue() = juceFolder.getFullPathName();
  73. exp->createDefaultConfigs();
  74. return exp;
  75. }
  76. ProjectExporter* ProjectExporter::createNewExporter (Project& project, const String& name)
  77. {
  78. return createNewExporter (project, getExporterNames().indexOf (name));
  79. }
  80. ProjectExporter* ProjectExporter::createExporter (Project& project, const ValueTree& settings)
  81. {
  82. ProjectExporter* exp = MSVCProjectExporterVC2005::createForSettings (project, settings);
  83. if (exp == nullptr) exp = MSVCProjectExporterVC2008::createForSettings (project, settings);
  84. if (exp == nullptr) exp = MSVCProjectExporterVC2010::createForSettings (project, settings);
  85. if (exp == nullptr) exp = MSVCProjectExporterVC2012::createForSettings (project, settings);
  86. if (exp == nullptr) exp = XCodeProjectExporter ::createForSettings (project, settings);
  87. if (exp == nullptr) exp = MakefileProjectExporter ::createForSettings (project, settings);
  88. if (exp == nullptr) exp = AndroidProjectExporter ::createForSettings (project, settings);
  89. if (exp == nullptr) exp = CodeBlocksProjectExporter::createForSettings (project, settings);
  90. jassert (exp != nullptr);
  91. return exp;
  92. }
  93. bool ProjectExporter::canProjectBeLaunched (Project* project)
  94. {
  95. if (project != nullptr)
  96. {
  97. const char* types[] =
  98. {
  99. #if JUCE_MAC
  100. XCodeProjectExporter::getValueTreeTypeName (false),
  101. XCodeProjectExporter::getValueTreeTypeName (true),
  102. #elif JUCE_WINDOWS
  103. MSVCProjectExporterVC2005::getValueTreeTypeName(),
  104. MSVCProjectExporterVC2008::getValueTreeTypeName(),
  105. MSVCProjectExporterVC2010::getValueTreeTypeName(),
  106. MSVCProjectExporterVC2012::getValueTreeTypeName(),
  107. #elif JUCE_LINUX
  108. // (this doesn't currently launch.. not really sure what it would do on linux)
  109. //MakefileProjectExporter::getValueTreeTypeName(),
  110. #endif
  111. nullptr
  112. };
  113. for (const char** type = types; *type != nullptr; ++type)
  114. if (project->getExporters().getChildWithName (*type).isValid())
  115. return true;
  116. }
  117. return false;
  118. }
  119. //==============================================================================
  120. ProjectExporter::ProjectExporter (Project& project_, const ValueTree& settings_)
  121. : xcodeIsBundle (false),
  122. xcodeCreatePList (false),
  123. xcodeCanUseDwarf (true),
  124. makefileIsDLL (false),
  125. msvcIsDLL (false),
  126. msvcIsWindowsSubsystem (true),
  127. settings (settings_),
  128. project (project_),
  129. projectType (project_.getProjectType()),
  130. projectName (project_.getTitle()),
  131. projectFolder (project_.getFile().getParentDirectory()),
  132. modulesGroup (nullptr)
  133. {
  134. }
  135. ProjectExporter::~ProjectExporter()
  136. {
  137. }
  138. File ProjectExporter::getTargetFolder() const
  139. {
  140. return project.resolveFilename (getTargetLocationString());
  141. }
  142. RelativePath ProjectExporter::getJucePathFromProjectFolder() const
  143. {
  144. return RelativePath (getJuceFolderString(), RelativePath::projectFolder);
  145. }
  146. RelativePath ProjectExporter::getJucePathFromTargetFolder() const
  147. {
  148. return rebaseFromProjectFolderToBuildTarget (getJucePathFromProjectFolder());
  149. }
  150. RelativePath ProjectExporter::rebaseFromProjectFolderToBuildTarget (const RelativePath& path) const
  151. {
  152. return path.rebased (project.getFile().getParentDirectory(), getTargetFolder(), RelativePath::buildTargetFolder);
  153. }
  154. bool ProjectExporter::shouldFileBeCompiledByDefault (const RelativePath& file) const
  155. {
  156. return file.hasFileExtension ("cpp;cc;c;cxx");
  157. }
  158. void ProjectExporter::createPropertyEditors (PropertyListBuilder& props)
  159. {
  160. props.add (new TextPropertyComponent (getTargetLocationValue(), "Target Project Folder", 1024, false),
  161. "The location of the folder in which the " + name + " project will be created. "
  162. "This path can be absolute, but it's much more sensible to make it relative to the jucer project directory.");
  163. props.add (new TextPropertyComponent (getJuceFolderValue(), "Local JUCE folder", 1024, false),
  164. "The location of the Juce library folder that the " + name + " project will use to when compiling. "
  165. "This can be an absolute path, or relative to the jucer project folder, but it must be valid on the "
  166. "filesystem of the machine you use to actually do the compiling.");
  167. OwnedArray<LibraryModule> modules;
  168. ModuleList moduleList;
  169. moduleList.rescan (ModuleList::getDefaultModulesFolder (&project));
  170. project.createRequiredModules (moduleList, modules);
  171. for (int i = 0; i < modules.size(); ++i)
  172. modules.getUnchecked(i)->createPropertyEditors (*this, props);
  173. props.add (new TextPropertyComponent (getExporterPreprocessorDefs(), "Extra Preprocessor Definitions", 32768, true),
  174. "Extra preprocessor definitions. Use the form \"NAME1=value NAME2=value\", using whitespace, commas, "
  175. "or new-lines 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, true),
  177. "Extra command-line flags to be passed to the compiler. This string can contain references to preprocessor definitions in the "
  178. "form ${NAME_OF_DEFINITION}, which will be replaced with their values.");
  179. props.add (new TextPropertyComponent (getExtraLinkerFlags(), "Extra linker flags", 2048, true),
  180. "Extra command-line flags to be passed to the linker. You might want to use this for adding additional libraries. "
  181. "This string can contain references to preprocessor definitions in the form ${NAME_OF_VALUE}, which will be replaced with their values.");
  182. props.add (new TextPropertyComponent (getExternalLibraries(), "External libraries to link", 2048, true),
  183. "Additional libraries to link (one per line). You should not add any platform specific decoration to these names. "
  184. "This string can contain references to preprocessor definitions in the form ${NAME_OF_VALUE}, which will be replaced with their values.");
  185. {
  186. OwnedArray<Project::Item> images;
  187. project.findAllImageItems (images);
  188. StringArray choices;
  189. Array<var> ids;
  190. choices.add ("<None>");
  191. ids.add (var::null);
  192. choices.add (String::empty);
  193. ids.add (var::null);
  194. for (int i = 0; i < images.size(); ++i)
  195. {
  196. choices.add (images.getUnchecked(i)->getName());
  197. ids.add (images.getUnchecked(i)->getID());
  198. }
  199. props.add (new ChoicePropertyComponent (getSmallIconImageItemID(), "Icon (small)", choices, ids),
  200. "Sets an icon to use for the executable.");
  201. props.add (new ChoicePropertyComponent (getBigIconImageItemID(), "Icon (large)", choices, ids),
  202. "Sets an icon to use for the executable.");
  203. }
  204. createExporterProperties (props);
  205. props.add (new TextPropertyComponent (getUserNotes(), "Notes", 32768, true),
  206. "Extra comments: This field is not used for code or project generation, it's just a space where you can express your thoughts.");
  207. }
  208. StringPairArray ProjectExporter::getAllPreprocessorDefs (const ProjectExporter::BuildConfiguration& config) const
  209. {
  210. StringPairArray defs (mergePreprocessorDefs (config.getAllPreprocessorDefs(),
  211. parsePreprocessorDefs (getExporterPreprocessorDefsString())));
  212. defs.set (getExporterIdentifierMacro(), "1");
  213. return defs;
  214. }
  215. StringPairArray ProjectExporter::getAllPreprocessorDefs() const
  216. {
  217. StringPairArray defs (mergePreprocessorDefs (project.getPreprocessorDefs(),
  218. parsePreprocessorDefs (getExporterPreprocessorDefsString())));
  219. defs.set (getExporterIdentifierMacro(), "1");
  220. return defs;
  221. }
  222. String ProjectExporter::replacePreprocessorTokens (const ProjectExporter::BuildConfiguration& config, const String& sourceString) const
  223. {
  224. return replacePreprocessorDefs (getAllPreprocessorDefs (config), sourceString);
  225. }
  226. void ProjectExporter::copyMainGroupFromProject()
  227. {
  228. jassert (itemGroups.size() == 0);
  229. itemGroups.add (project.getMainGroup().createCopy());
  230. }
  231. Project::Item& ProjectExporter::getModulesGroup()
  232. {
  233. if (modulesGroup == nullptr)
  234. {
  235. jassert (itemGroups.size() > 0); // must call copyMainGroupFromProject before this.
  236. itemGroups.add (Project::Item::createGroup (project, "Juce Modules", "__modulesgroup__"));
  237. modulesGroup = &(itemGroups.getReference (itemGroups.size() - 1));
  238. }
  239. return *modulesGroup;
  240. }
  241. void ProjectExporter::addToExtraSearchPaths (const RelativePath& pathFromProjectFolder)
  242. {
  243. RelativePath localPath (rebaseFromProjectFolderToBuildTarget (pathFromProjectFolder));
  244. const String path (isVisualStudio() ? localPath.toWindowsStyle() : localPath.toUnixStyle());
  245. extraSearchPaths.addIfNotAlreadyThere (path, false);
  246. }
  247. //==============================================================================
  248. const Identifier ProjectExporter::configurations ("CONFIGURATIONS");
  249. const Identifier ProjectExporter::configuration ("CONFIGURATION");
  250. ValueTree ProjectExporter::getConfigurations() const
  251. {
  252. return settings.getChildWithName (configurations);
  253. }
  254. int ProjectExporter::getNumConfigurations() const
  255. {
  256. return getConfigurations().getNumChildren();
  257. }
  258. ProjectExporter::BuildConfiguration::Ptr ProjectExporter::getConfiguration (int index) const
  259. {
  260. return createBuildConfig (getConfigurations().getChild (index));
  261. }
  262. bool ProjectExporter::hasConfigurationNamed (const String& nameToFind) const
  263. {
  264. const ValueTree configs (getConfigurations());
  265. for (int i = configs.getNumChildren(); --i >= 0;)
  266. if (configs.getChild(i) [Ids::name].toString() == nameToFind)
  267. return true;
  268. return false;
  269. }
  270. String ProjectExporter::getUniqueConfigName (String nm) const
  271. {
  272. String nameRoot (nm);
  273. while (CharacterFunctions::isDigit (nameRoot.getLastCharacter()))
  274. nameRoot = nameRoot.dropLastCharacters (1);
  275. nameRoot = nameRoot.trim();
  276. int suffix = 2;
  277. while (hasConfigurationNamed (name))
  278. nm = nameRoot + " " + String (suffix++);
  279. return nm;
  280. }
  281. void ProjectExporter::addNewConfiguration (const BuildConfiguration* configToCopy)
  282. {
  283. const String configName (getUniqueConfigName (configToCopy != nullptr ? configToCopy->config [Ids::name].toString()
  284. : "New Build Configuration"));
  285. ValueTree configs (getConfigurations());
  286. if (! configs.isValid())
  287. {
  288. settings.addChild (ValueTree (configurations), 0, project.getUndoManagerFor (settings));
  289. configs = getConfigurations();
  290. }
  291. ValueTree newConfig (configuration);
  292. if (configToCopy != nullptr)
  293. newConfig = configToCopy->config.createCopy();
  294. newConfig.setProperty (Ids::name, configName, 0);
  295. configs.addChild (newConfig, -1, project.getUndoManagerFor (configs));
  296. }
  297. void ProjectExporter::BuildConfiguration::removeFromExporter()
  298. {
  299. ValueTree configs (config.getParent());
  300. configs.removeChild (config, project.getUndoManagerFor (configs));
  301. }
  302. void ProjectExporter::createDefaultConfigs()
  303. {
  304. settings.getOrCreateChildWithName (configurations, nullptr);
  305. for (int i = 0; i < 2; ++i)
  306. {
  307. addNewConfiguration (nullptr);
  308. BuildConfiguration::Ptr config (getConfiguration (i));
  309. const bool debugConfig = i == 0;
  310. config->getNameValue() = debugConfig ? "Debug" : "Release";
  311. config->isDebugValue() = debugConfig;
  312. config->getOptimisationLevel() = debugConfig ? optimisationOff : optimiseMinSize;
  313. config->getTargetBinaryName() = project.getProjectFilenameRoot();
  314. }
  315. }
  316. Image ProjectExporter::getBigIcon() const
  317. {
  318. return project.getMainGroup().findItemWithID (settings [Ids::bigIcon]).loadAsImageFile();
  319. }
  320. Image ProjectExporter::getSmallIcon() const
  321. {
  322. return project.getMainGroup().findItemWithID (settings [Ids::smallIcon]).loadAsImageFile();
  323. }
  324. Image ProjectExporter::getBestIconForSize (int size, bool returnNullIfNothingBigEnough) const
  325. {
  326. Image im;
  327. const Image im1 (getSmallIcon());
  328. const Image im2 (getBigIcon());
  329. if (im1.isValid() && im2.isValid())
  330. {
  331. if (im1.getWidth() >= size && im2.getWidth() >= size)
  332. im = im1.getWidth() < im2.getWidth() ? im1 : im2;
  333. else if (im1.getWidth() >= size)
  334. im = im1;
  335. else if (im2.getWidth() >= size)
  336. im = im2;
  337. else
  338. return Image::null;
  339. }
  340. else
  341. {
  342. im = im1.isValid() ? im1 : im2;
  343. }
  344. if (returnNullIfNothingBigEnough && im.getWidth() < size && im.getHeight() < size)
  345. return Image::null;
  346. return rescaleImageForIcon (im, size);
  347. }
  348. Image ProjectExporter::rescaleImageForIcon (Image im, const int size)
  349. {
  350. im = SoftwareImageType().convert (im);
  351. if (size == im.getWidth() && size == im.getHeight())
  352. return im;
  353. // (scale it down in stages for better resampling)
  354. while (im.getWidth() > 2 * size && im.getHeight() > 2 * size)
  355. im = im.rescaled (im.getWidth() / 2,
  356. im.getHeight() / 2);
  357. Image newIm (Image::ARGB, size, size, true, SoftwareImageType());
  358. Graphics g (newIm);
  359. g.drawImageWithin (im, 0, 0, size, size,
  360. RectanglePlacement::centred | RectanglePlacement::onlyReduceInSize, false);
  361. return newIm;
  362. }
  363. //==============================================================================
  364. ProjectExporter::ConfigIterator::ConfigIterator (ProjectExporter& exporter_)
  365. : index (-1), exporter (exporter_)
  366. {
  367. }
  368. bool ProjectExporter::ConfigIterator::next()
  369. {
  370. if (++index >= exporter.getNumConfigurations())
  371. return false;
  372. config = exporter.getConfiguration (index);
  373. return true;
  374. }
  375. ProjectExporter::ConstConfigIterator::ConstConfigIterator (const ProjectExporter& exporter_)
  376. : index (-1), exporter (exporter_)
  377. {
  378. }
  379. bool ProjectExporter::ConstConfigIterator::next()
  380. {
  381. if (++index >= exporter.getNumConfigurations())
  382. return false;
  383. config = exporter.getConfiguration (index);
  384. return true;
  385. }
  386. //==============================================================================
  387. ProjectExporter::BuildConfiguration::BuildConfiguration (Project& project_, const ValueTree& configNode)
  388. : config (configNode), project (project_)
  389. {
  390. }
  391. ProjectExporter::BuildConfiguration::~BuildConfiguration()
  392. {
  393. }
  394. String ProjectExporter::BuildConfiguration::getGCCOptimisationFlag() const
  395. {
  396. switch (getOptimisationLevelInt())
  397. {
  398. case optimiseMaxSpeed: return "3";
  399. case optimiseMinSize: return "s";
  400. default: return "0";
  401. }
  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", "Minimise size", "Maximise speed", 0 };
  410. const int optimisationLevelValues[] = { optimisationOff, optimiseMinSize, optimiseMaxSpeed, 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. }
  450. String ProjectExporter::getExternalLibraryFlags (const BuildConfiguration& config) const
  451. {
  452. StringArray libraries;
  453. libraries.addTokens (getExternalLibrariesString(), ";\n", "\"'");
  454. libraries.removeEmptyStrings (true);
  455. if (libraries.size() != 0)
  456. return replacePreprocessorTokens (config, "-l" + libraries.joinIntoString (" -l")).trim();
  457. return String::empty;
  458. }