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.

580 lines
23KB

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