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.

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