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.

1027 lines
42KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. #include "../Application/jucer_Headers.h"
  20. #include "jucer_ProjectExporter.h"
  21. #include "jucer_ProjectSaver.h"
  22. #include "jucer_ProjectExport_Make.h"
  23. #include "jucer_ProjectExport_MSVC.h"
  24. #include "jucer_ProjectExport_Xcode.h"
  25. #include "jucer_ProjectExport_Android.h"
  26. #include "jucer_ProjectExport_CodeBlocks.h"
  27. #include "jucer_ProjectExport_CLion.h"
  28. #include "../Utility/UI/PropertyComponents/jucer_FilePathPropertyComponent.h"
  29. //==============================================================================
  30. static void addType (Array<ProjectExporter::ExporterTypeInfo>& list,
  31. const char* name, const void* iconData, int iconDataSize)
  32. {
  33. ProjectExporter::ExporterTypeInfo type = { name, iconData, iconDataSize };
  34. list.add (type);
  35. }
  36. Array<ProjectExporter::ExporterTypeInfo> ProjectExporter::getExporterTypes()
  37. {
  38. Array<ProjectExporter::ExporterTypeInfo> types;
  39. addType (types, XcodeProjectExporter::getNameMac(), BinaryData::export_xcode_svg, BinaryData::export_xcode_svgSize);
  40. addType (types, XcodeProjectExporter::getNameiOS(), BinaryData::export_xcode_svg, BinaryData::export_xcode_svgSize);
  41. addType (types, MSVCProjectExporterVC2017::getName(), BinaryData::export_visualStudio_svg, BinaryData::export_visualStudio_svgSize);
  42. addType (types, MSVCProjectExporterVC2015::getName(), BinaryData::export_visualStudio_svg, BinaryData::export_visualStudio_svgSize);
  43. addType (types, MSVCProjectExporterVC2013::getName(), BinaryData::export_visualStudio_svg, BinaryData::export_visualStudio_svgSize);
  44. addType (types, MakefileProjectExporter::getNameLinux(), BinaryData::export_linux_svg, BinaryData::export_linux_svgSize);
  45. addType (types, AndroidProjectExporter::getName(), BinaryData::export_android_svg, BinaryData::export_android_svgSize);
  46. addType (types, CodeBlocksProjectExporter::getNameWindows(), BinaryData::export_codeBlocks_svg, BinaryData::export_codeBlocks_svgSize);
  47. addType (types, CodeBlocksProjectExporter::getNameLinux(), BinaryData::export_codeBlocks_svg, BinaryData::export_codeBlocks_svgSize);
  48. addType (types, CLionProjectExporter::getName(), BinaryData::export_clion_svg, BinaryData::export_clion_svgSize);
  49. return types;
  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 MSVCProjectExporterVC2017 (project, ValueTree (MSVCProjectExporterVC2017 ::getValueTreeTypeName())); break;
  59. case 3: exp = new MSVCProjectExporterVC2015 (project, ValueTree (MSVCProjectExporterVC2015 ::getValueTreeTypeName())); break;
  60. case 4: exp = new MSVCProjectExporterVC2013 (project, ValueTree (MSVCProjectExporterVC2013 ::getValueTreeTypeName())); break;
  61. case 5: exp = new MakefileProjectExporter (project, ValueTree (MakefileProjectExporter ::getValueTreeTypeName())); break;
  62. case 6: exp = new AndroidProjectExporter (project, ValueTree (AndroidProjectExporter ::getValueTreeTypeName())); break;
  63. case 7: exp = new CodeBlocksProjectExporter (project, ValueTree (CodeBlocksProjectExporter ::getValueTreeTypeName (CodeBlocksProjectExporter::windowsTarget)), CodeBlocksProjectExporter::windowsTarget); break;
  64. case 8: exp = new CodeBlocksProjectExporter (project, ValueTree (CodeBlocksProjectExporter ::getValueTreeTypeName (CodeBlocksProjectExporter::linuxTarget)), CodeBlocksProjectExporter::linuxTarget); break;
  65. case 9: exp = new CLionProjectExporter (project, ValueTree (CLionProjectExporter ::getValueTreeTypeName())); break;
  66. }
  67. exp->createDefaultConfigs();
  68. exp->createDefaultModulePaths();
  69. return exp;
  70. }
  71. StringArray ProjectExporter::getExporterNames()
  72. {
  73. StringArray s;
  74. for (auto& e : getExporterTypes())
  75. s.add (e.name);
  76. return s;
  77. }
  78. StringArray ProjectExporter::getExporterValueTreeNames()
  79. {
  80. StringArray s;
  81. for (auto& n : getExporterNames())
  82. s.add (getValueTreeNameForExporter (n));
  83. return s;
  84. }
  85. String ProjectExporter::getValueTreeNameForExporter (const String& exporterName)
  86. {
  87. if (exporterName == XcodeProjectExporter::getNameMac())
  88. return XcodeProjectExporter::getValueTreeTypeName (false);
  89. if (exporterName == XcodeProjectExporter::getNameiOS())
  90. return XcodeProjectExporter::getValueTreeTypeName (true);
  91. if (exporterName == MSVCProjectExporterVC2013::getName())
  92. return MSVCProjectExporterVC2013::getValueTreeTypeName();
  93. if (exporterName == MSVCProjectExporterVC2015::getName())
  94. return MSVCProjectExporterVC2015::getValueTreeTypeName();
  95. if (exporterName == MSVCProjectExporterVC2017::getName())
  96. return MSVCProjectExporterVC2017::getValueTreeTypeName();
  97. if (exporterName == MakefileProjectExporter::getNameLinux())
  98. return MakefileProjectExporter::getValueTreeTypeName();
  99. if (exporterName == AndroidProjectExporter::getName())
  100. return AndroidProjectExporter::getValueTreeTypeName();
  101. if (exporterName == CodeBlocksProjectExporter::getNameLinux())
  102. return CodeBlocksProjectExporter::getValueTreeTypeName (CodeBlocksProjectExporter::CodeBlocksOS::linuxTarget);
  103. if (exporterName == CodeBlocksProjectExporter::getNameWindows())
  104. return CodeBlocksProjectExporter::getValueTreeTypeName (CodeBlocksProjectExporter::CodeBlocksOS::windowsTarget);
  105. if (exporterName == CLionProjectExporter::getName())
  106. return CLionProjectExporter::getValueTreeTypeName();
  107. return {};
  108. }
  109. String ProjectExporter::getTargetFolderForExporter (const String& exporterValueTreeName)
  110. {
  111. if (exporterValueTreeName == "XCODE_MAC") return "MacOSX";
  112. if (exporterValueTreeName == "XCODE_IPHONE") return "iOS";
  113. if (exporterValueTreeName == "VS2017") return "VisualStudio2017";
  114. if (exporterValueTreeName == "VS2015") return "VisualStudio2015";
  115. if (exporterValueTreeName == "VS2013") return "VisualStudio2013";
  116. if (exporterValueTreeName == "LINUX_MAKE") return "LinuxMakefile";
  117. if (exporterValueTreeName == "ANDROIDSTUDIO") return "Android";
  118. if (exporterValueTreeName == "CODEBLOCKS_WINDOWS") return "CodeBlocksWindows";
  119. if (exporterValueTreeName == "CODEBLOCKS_LINUX") return "CodeBlocksLinux";
  120. if (exporterValueTreeName == "CLION") return "CLion";
  121. return {};
  122. }
  123. StringArray ProjectExporter::getAllDefaultBuildsFolders()
  124. {
  125. StringArray folders;
  126. folders.add (getDefaultBuildsRootFolder() + "iOS");
  127. folders.add (getDefaultBuildsRootFolder() + "MacOSX");
  128. folders.add (getDefaultBuildsRootFolder() + "VisualStudio2013");
  129. folders.add (getDefaultBuildsRootFolder() + "VisualStudio2015");
  130. folders.add (getDefaultBuildsRootFolder() + "VisualStudio2017");
  131. folders.add (getDefaultBuildsRootFolder() + "LinuxMakefile");
  132. folders.add (getDefaultBuildsRootFolder() + "CodeBlocksWindows");
  133. folders.add (getDefaultBuildsRootFolder() + "CodeBlocksLinux");
  134. folders.add (getDefaultBuildsRootFolder() + "Android");
  135. folders.add (getDefaultBuildsRootFolder() + "CLion");
  136. return folders;
  137. }
  138. String ProjectExporter::getCurrentPlatformExporterName()
  139. {
  140. #if JUCE_MAC
  141. return XcodeProjectExporter::getNameMac();
  142. #elif JUCE_WINDOWS
  143. return MSVCProjectExporterVC2017::getName();
  144. #elif JUCE_LINUX
  145. return MakefileProjectExporter::getNameLinux();
  146. #else
  147. #error // huh?
  148. #endif
  149. }
  150. ProjectExporter* ProjectExporter::createNewExporter (Project& project, const String& name)
  151. {
  152. return createNewExporter (project, getExporterNames().indexOf (name));
  153. }
  154. ProjectExporter* ProjectExporter::createExporter (Project& project, const ValueTree& settings)
  155. {
  156. ProjectExporter* exp = MSVCProjectExporterVC2013 ::createForSettings (project, settings);
  157. if (exp == nullptr) exp = MSVCProjectExporterVC2015 ::createForSettings (project, settings);
  158. if (exp == nullptr) exp = MSVCProjectExporterVC2017 ::createForSettings (project, settings);
  159. if (exp == nullptr) exp = XcodeProjectExporter ::createForSettings (project, settings);
  160. if (exp == nullptr) exp = MakefileProjectExporter ::createForSettings (project, settings);
  161. if (exp == nullptr) exp = AndroidProjectExporter ::createForSettings (project, settings);
  162. if (exp == nullptr) exp = CodeBlocksProjectExporter ::createForSettings (project, settings);
  163. if (exp == nullptr) exp = CLionProjectExporter ::createForSettings (project, settings);
  164. jassert (exp != nullptr);
  165. return exp;
  166. }
  167. bool ProjectExporter::canProjectBeLaunched (Project* project)
  168. {
  169. if (project != nullptr)
  170. {
  171. const char* types[] =
  172. {
  173. #if JUCE_MAC
  174. XcodeProjectExporter::getValueTreeTypeName (false),
  175. XcodeProjectExporter::getValueTreeTypeName (true),
  176. #elif JUCE_WINDOWS
  177. MSVCProjectExporterVC2013::getValueTreeTypeName(),
  178. MSVCProjectExporterVC2015::getValueTreeTypeName(),
  179. MSVCProjectExporterVC2017::getValueTreeTypeName(),
  180. #elif JUCE_LINUX
  181. // (this doesn't currently launch.. not really sure what it would do on linux)
  182. //MakefileProjectExporter::getValueTreeTypeName(),
  183. #endif
  184. AndroidProjectExporter::getValueTreeTypeName(),
  185. nullptr
  186. };
  187. for (const char** type = types; *type != nullptr; ++type)
  188. if (project->getExporters().getChildWithName (*type).isValid())
  189. return true;
  190. }
  191. return false;
  192. }
  193. //==============================================================================
  194. ProjectExporter::ProjectExporter (Project& p, const ValueTree& state)
  195. : settings (state),
  196. project (p),
  197. projectType (p.getProjectType()),
  198. projectName (p.getProjectNameString()),
  199. projectFolder (p.getProjectFolder()),
  200. targetLocationValue (settings, Ids::targetFolder, getUndoManager()),
  201. extraCompilerFlagsValue (settings, Ids::extraCompilerFlags, getUndoManager()),
  202. extraLinkerFlagsValue (settings, Ids::extraLinkerFlags, getUndoManager()),
  203. externalLibrariesValue (settings, Ids::externalLibraries, getUndoManager()),
  204. userNotesValue (settings, Ids::userNotes, getUndoManager()),
  205. gnuExtensionsValue (settings, Ids::enableGNUExtensions, getUndoManager()),
  206. bigIconValue (settings, Ids::bigIcon, getUndoManager()),
  207. smallIconValue (settings, Ids::smallIcon, getUndoManager()),
  208. extraPPDefsValue (settings, Ids::extraDefs, getUndoManager())
  209. {
  210. }
  211. ProjectExporter::~ProjectExporter()
  212. {
  213. }
  214. void ProjectExporter::updateDeprecatedProjectSettingsInteractively() {}
  215. String ProjectExporter::getName() const
  216. {
  217. if (! getAllDefaultBuildsFolders().contains (getTargetLocationString()))
  218. return name + " - " + getTargetLocationString();
  219. return name;
  220. }
  221. File ProjectExporter::getTargetFolder() const
  222. {
  223. return project.resolveFilename (getTargetLocationString());
  224. }
  225. RelativePath ProjectExporter::rebaseFromProjectFolderToBuildTarget (const RelativePath& path) const
  226. {
  227. return path.rebased (project.getProjectFolder(), getTargetFolder(), RelativePath::buildTargetFolder);
  228. }
  229. bool ProjectExporter::shouldFileBeCompiledByDefault (const RelativePath& file) const
  230. {
  231. return file.hasFileExtension (cOrCppFileExtensions)
  232. || file.hasFileExtension (asmFileExtensions);
  233. }
  234. //==============================================================================
  235. void ProjectExporter::createPropertyEditors (PropertyListBuilder& props)
  236. {
  237. if (! isCLion())
  238. {
  239. props.add (new TextPropertyComponent (targetLocationValue, "Target Project Folder", 2048, false),
  240. "The location of the folder in which the " + name + " project will be created. "
  241. "This path can be absolute, but it's much more sensible to make it relative to the jucer project directory.");
  242. if ((shouldBuildTargetType (ProjectType::Target::VSTPlugIn) && project.shouldBuildVST()) || project.isVSTPluginHost())
  243. {
  244. props.add (new FilePathPropertyComponent (vstLegacyPathValueWrapper.wrappedValue, "VST (Legacy) SDK Folder", true,
  245. getTargetOSForExporter() == TargetOS::getThisOS(), "*", project.getProjectFolder()),
  246. "If you're building a VST plug-in or host, you can use this field to override the global VST (Legacy) SDK path with a project-specific path. "
  247. "This can be an absolute path, or a path relative to the Projucer project file.");
  248. }
  249. if ((shouldBuildTargetType (ProjectType::Target::VST3PlugIn) && project.shouldBuildVST3()) || project.isVST3PluginHost())
  250. {
  251. props.add (new FilePathPropertyComponent (vst3PathValueWrapper.wrappedValue, "VST3 SDK Folder", true,
  252. getTargetOSForExporter() == TargetOS::getThisOS(), "*", project.getProjectFolder()),
  253. "If you're building a VST3 plug-in or host, you can use this field to override the global VST3 SDK path with a project-specific path. "
  254. "This can be an absolute path, or a path relative to the Projucer project file.");
  255. }
  256. if (shouldBuildTargetType (ProjectType::Target::AAXPlugIn) && project.shouldBuildAAX())
  257. {
  258. props.add (new FilePathPropertyComponent (aaxPathValueWrapper.wrappedValue, "AAX SDK Folder", true,
  259. getTargetOSForExporter() == TargetOS::getThisOS(), "*", project.getProjectFolder()),
  260. "If you're building an AAX plug-in, this must be the folder containing the AAX SDK. This can be an absolute path, or a path relative to the Projucer project file.");
  261. }
  262. if (shouldBuildTargetType (ProjectType::Target::RTASPlugIn) && project.shouldBuildRTAS())
  263. {
  264. props.add (new FilePathPropertyComponent (rtasPathValueWrapper.wrappedValue, "RTAS SDK Folder", true,
  265. getTargetOSForExporter() == TargetOS::getThisOS(), "*", project.getProjectFolder()),
  266. "If you're building an RTAS plug-in, this must be the folder containing the RTAS SDK. This can be an absolute path, or a path relative to the Projucer project file.");
  267. }
  268. props.add (new TextPropertyComponent (extraPPDefsValue, "Extra Preprocessor Definitions", 32768, true),
  269. "Extra preprocessor definitions. Use the form \"NAME1=value NAME2=value\", using whitespace, commas, "
  270. "or new-lines to separate the items - to include a space or comma in a definition, precede it with a backslash.");
  271. props.add (new TextPropertyComponent (extraCompilerFlagsValue, "Extra Compiler Flags", 8192, true),
  272. "Extra command-line flags to be passed to the compiler. This string can contain references to preprocessor definitions in the "
  273. "form ${NAME_OF_DEFINITION}, which will be replaced with their values.");
  274. props.add (new TextPropertyComponent (extraLinkerFlagsValue, "Extra Linker Flags", 8192, true),
  275. "Extra command-line flags to be passed to the linker. You might want to use this for adding additional libraries. "
  276. "This string can contain references to preprocessor definitions in the form ${NAME_OF_VALUE}, which will be replaced with their values.");
  277. props.add (new TextPropertyComponent (externalLibrariesValue, "External Libraries to Link", 8192, true),
  278. "Additional libraries to link (one per line). You should not add any platform specific decoration to these names. "
  279. "This string can contain references to preprocessor definitions in the form ${NAME_OF_VALUE}, which will be replaced with their values.");
  280. if (! isVisualStudio())
  281. props.add (new ChoicePropertyComponent (gnuExtensionsValue, "GNU Compiler Extensions"),
  282. "Enabling this will use the GNU C++ language standard variant for compilation.");
  283. createIconProperties (props);
  284. }
  285. createExporterProperties (props);
  286. props.add (new TextPropertyComponent (userNotesValue, "Notes", 32768, true),
  287. "Extra comments: This field is not used for code or project generation, it's just a space where you can express your thoughts.");
  288. }
  289. void ProjectExporter::createIconProperties (PropertyListBuilder& props)
  290. {
  291. OwnedArray<Project::Item> images;
  292. project.findAllImageItems (images);
  293. StringArray choices;
  294. Array<var> ids;
  295. choices.add ("<None>");
  296. ids.add (var());
  297. for (int i = 0; i < images.size(); ++i)
  298. {
  299. choices.add (images.getUnchecked(i)->getName());
  300. ids.add (images.getUnchecked(i)->getID());
  301. }
  302. props.add (new ChoicePropertyComponent (smallIconValue, "Icon (Small)", choices, ids),
  303. "Sets an icon to use for the executable.");
  304. props.add (new ChoicePropertyComponent (bigIconValue, "Icon (Large)", choices, ids),
  305. "Sets an icon to use for the executable.");
  306. }
  307. //==============================================================================
  308. void ProjectExporter::addSettingsForProjectType (const ProjectType& type)
  309. {
  310. addVSTPathsIfPluginOrHost();
  311. if (type.isAudioPlugin())
  312. addCommonAudioPluginSettings();
  313. addPlatformSpecificSettingsForProjectType (type);
  314. }
  315. void ProjectExporter::addVSTPathsIfPluginOrHost()
  316. {
  317. if (((shouldBuildTargetType (ProjectType::Target::VSTPlugIn) && project.shouldBuildVST()) || project.isVSTPluginHost())
  318. || ((shouldBuildTargetType (ProjectType::Target::VST3PlugIn) && project.shouldBuildVST3()) || project.isVST3PluginHost()))
  319. {
  320. addLegacyVSTFolderToPathIfSpecified();
  321. addVST3FolderToPath();
  322. }
  323. }
  324. void ProjectExporter::addCommonAudioPluginSettings()
  325. {
  326. if (shouldBuildTargetType (ProjectType::Target::AAXPlugIn))
  327. addAAXFoldersToPath();
  328. // Note: RTAS paths are platform-dependent, impl -> addPlatformSpecificSettingsForProjectType
  329. }
  330. void ProjectExporter::addLegacyVSTFolderToPathIfSpecified()
  331. {
  332. auto vstFolder = getVSTLegacyPathString();
  333. if (vstFolder.isNotEmpty())
  334. addToExtraSearchPaths (RelativePath (vstFolder, RelativePath::projectFolder), 0);
  335. }
  336. RelativePath ProjectExporter::getInternalVST3SDKPath()
  337. {
  338. return getModuleFolderRelativeToProject ("juce_audio_processors")
  339. .getChildFile ("format_types")
  340. .getChildFile ("VST3_SDK");
  341. }
  342. void ProjectExporter::addVST3FolderToPath()
  343. {
  344. auto vst3Folder = getVST3PathString();
  345. if (vst3Folder.isNotEmpty())
  346. addToExtraSearchPaths (RelativePath (vst3Folder, RelativePath::projectFolder), 0);
  347. else
  348. addToExtraSearchPaths (getInternalVST3SDKPath(), 0);
  349. }
  350. void ProjectExporter::addAAXFoldersToPath()
  351. {
  352. auto aaxFolder = getAAXPathString();
  353. if (aaxFolder.isNotEmpty())
  354. {
  355. RelativePath aaxFolderPath (aaxFolder, RelativePath::projectFolder);
  356. addToExtraSearchPaths (aaxFolderPath);
  357. addToExtraSearchPaths (aaxFolderPath.getChildFile ("Interfaces"));
  358. addToExtraSearchPaths (aaxFolderPath.getChildFile ("Interfaces").getChildFile ("ACF"));
  359. }
  360. }
  361. //==============================================================================
  362. StringPairArray ProjectExporter::getAllPreprocessorDefs (const BuildConfiguration& config, const ProjectType::Target::Type targetType) const
  363. {
  364. auto defs = mergePreprocessorDefs (config.getAllPreprocessorDefs(),
  365. parsePreprocessorDefs (getExporterPreprocessorDefsString()));
  366. addDefaultPreprocessorDefs (defs);
  367. addTargetSpecificPreprocessorDefs (defs, targetType);
  368. return defs;
  369. }
  370. StringPairArray ProjectExporter::getAllPreprocessorDefs() const
  371. {
  372. auto defs = mergePreprocessorDefs (project.getPreprocessorDefs(),
  373. parsePreprocessorDefs (getExporterPreprocessorDefsString()));
  374. addDefaultPreprocessorDefs (defs);
  375. return defs;
  376. }
  377. void ProjectExporter::addTargetSpecificPreprocessorDefs (StringPairArray& defs, const ProjectType::Target::Type targetType) const
  378. {
  379. std::pair<String, ProjectType::Target::Type> targetFlags[] = {
  380. {"JucePlugin_Build_VST", ProjectType::Target::VSTPlugIn},
  381. {"JucePlugin_Build_VST3", ProjectType::Target::VST3PlugIn},
  382. {"JucePlugin_Build_AU", ProjectType::Target::AudioUnitPlugIn},
  383. {"JucePlugin_Build_AUv3", ProjectType::Target::AudioUnitv3PlugIn},
  384. {"JucePlugin_Build_RTAS", ProjectType::Target::RTASPlugIn},
  385. {"JucePlugin_Build_AAX", ProjectType::Target::AAXPlugIn},
  386. {"JucePlugin_Build_Standalone", ProjectType::Target::StandalonePlugIn},
  387. {"JucePlugin_Build_Unity", ProjectType::Target::UnityPlugIn}
  388. };
  389. if (targetType == ProjectType::Target::SharedCodeTarget)
  390. {
  391. for (auto& flag : targetFlags)
  392. defs.set (flag.first, (shouldBuildTargetType (flag.second) ? "1" : "0"));
  393. defs.set ("JUCE_SHARED_CODE", "1");
  394. }
  395. else if (targetType != ProjectType::Target::unspecified)
  396. {
  397. for (auto& flag : targetFlags)
  398. defs.set (flag.first, (targetType == flag.second ? "1" : "0"));
  399. }
  400. }
  401. void ProjectExporter::addDefaultPreprocessorDefs (StringPairArray& defs) const
  402. {
  403. defs.set (getExporterIdentifierMacro(), "1");
  404. defs.set ("JUCE_APP_VERSION", project.getVersionString());
  405. defs.set ("JUCE_APP_VERSION_HEX", project.getVersionAsHex());
  406. }
  407. String ProjectExporter::replacePreprocessorTokens (const ProjectExporter::BuildConfiguration& config,
  408. const String& sourceString) const
  409. {
  410. return replacePreprocessorDefs (getAllPreprocessorDefs (config, ProjectType::Target::unspecified), sourceString);
  411. }
  412. void ProjectExporter::copyMainGroupFromProject()
  413. {
  414. jassert (itemGroups.size() == 0);
  415. itemGroups.add (project.getMainGroup().createCopy());
  416. }
  417. Project::Item& ProjectExporter::getModulesGroup()
  418. {
  419. if (modulesGroup == nullptr)
  420. {
  421. jassert (itemGroups.size() > 0); // must call copyMainGroupFromProject before this.
  422. itemGroups.add (Project::Item::createGroup (project, "JUCE Modules", "__modulesgroup__", true));
  423. modulesGroup = &(itemGroups.getReference (itemGroups.size() - 1));
  424. }
  425. return *modulesGroup;
  426. }
  427. void ProjectExporter::addProjectPathToBuildPathList (StringArray& pathList, const RelativePath& pathFromProjectFolder, int index) const
  428. {
  429. auto localPath = RelativePath (rebaseFromProjectFolderToBuildTarget (pathFromProjectFolder));
  430. auto path = isVisualStudio() ? localPath.toWindowsStyle() : localPath.toUnixStyle();
  431. if (! pathList.contains (path))
  432. pathList.insert (index, path);
  433. }
  434. void ProjectExporter::addToModuleLibPaths (const RelativePath& pathFromProjectFolder)
  435. {
  436. addProjectPathToBuildPathList (moduleLibSearchPaths, pathFromProjectFolder);
  437. }
  438. void ProjectExporter::addToExtraSearchPaths (const RelativePath& pathFromProjectFolder, int index)
  439. {
  440. addProjectPathToBuildPathList (extraSearchPaths, pathFromProjectFolder, index);
  441. }
  442. static var getStoredPathForModule (const String& id, const ProjectExporter& exp)
  443. {
  444. return getAppSettings().getStoredPath (isJUCEModule (id) ? Ids::defaultJuceModulePath : Ids::defaultUserModulePath,
  445. exp.getTargetOSForExporter()).get();
  446. }
  447. ValueWithDefault ProjectExporter::getPathForModuleValue (const String& moduleID)
  448. {
  449. auto* um = getUndoManager();
  450. auto paths = settings.getOrCreateChildWithName (Ids::MODULEPATHS, um);
  451. auto m = paths.getChildWithProperty (Ids::ID, moduleID);
  452. if (! m.isValid())
  453. {
  454. m = ValueTree (Ids::MODULEPATH);
  455. m.setProperty (Ids::ID, moduleID, um);
  456. paths.appendChild (m, um);
  457. }
  458. return { m, Ids::path, um, getStoredPathForModule (moduleID, *this) };
  459. }
  460. String ProjectExporter::getPathForModuleString (const String& moduleID) const
  461. {
  462. auto exporterPath = settings.getChildWithName (Ids::MODULEPATHS)
  463. .getChildWithProperty (Ids::ID, moduleID) [Ids::path].toString();
  464. if (exporterPath.isEmpty() || project.getEnabledModules().shouldUseGlobalPath (moduleID))
  465. return getStoredPathForModule (moduleID, *this);
  466. return exporterPath;
  467. }
  468. void ProjectExporter::removePathForModule (const String& moduleID)
  469. {
  470. auto paths = settings.getChildWithName (Ids::MODULEPATHS);
  471. auto m = paths.getChildWithProperty (Ids::ID, moduleID);
  472. paths.removeChild (m, project.getUndoManagerFor (settings));
  473. }
  474. TargetOS::OS ProjectExporter::getTargetOSForExporter() const
  475. {
  476. auto targetOS = TargetOS::unknown;
  477. if (isWindows()) targetOS = TargetOS::windows;
  478. else if (isOSX() || isiOS()) targetOS = TargetOS::osx;
  479. else if (isLinux()) targetOS = TargetOS::linux;
  480. else if (isAndroid() || isCLion()) targetOS = TargetOS::getThisOS();
  481. return targetOS;
  482. }
  483. RelativePath ProjectExporter::getModuleFolderRelativeToProject (const String& moduleID) const
  484. {
  485. if (project.getEnabledModules().shouldCopyModuleFilesLocally (moduleID).getValue())
  486. return RelativePath (project.getRelativePathForFile (project.getLocalModuleFolder (moduleID)),
  487. RelativePath::projectFolder);
  488. auto path = getPathForModuleString (moduleID);
  489. if (path.isEmpty())
  490. return getLegacyModulePath (moduleID).getChildFile (moduleID);
  491. return RelativePath (path, RelativePath::projectFolder).getChildFile (moduleID);
  492. }
  493. String ProjectExporter::getLegacyModulePath() const
  494. {
  495. return getSettingString ("juceFolder");
  496. }
  497. RelativePath ProjectExporter::getLegacyModulePath (const String& moduleID) const
  498. {
  499. if (project.getEnabledModules().state.getChildWithProperty (Ids::ID, moduleID) ["useLocalCopy"])
  500. return RelativePath (project.getRelativePathForFile (project.getGeneratedCodeFolder()
  501. .getChildFile ("modules")
  502. .getChildFile (moduleID)), RelativePath::projectFolder);
  503. auto oldJucePath = getLegacyModulePath();
  504. if (oldJucePath.isEmpty())
  505. return RelativePath();
  506. RelativePath p (oldJucePath, RelativePath::projectFolder);
  507. if (p.getFileName() != "modules")
  508. p = p.getChildFile ("modules");
  509. return p.getChildFile (moduleID);
  510. }
  511. void ProjectExporter::updateOldModulePaths()
  512. {
  513. auto oldPath = getLegacyModulePath();
  514. if (oldPath.isNotEmpty())
  515. {
  516. for (int i = project.getEnabledModules().getNumModules(); --i >= 0;)
  517. {
  518. auto modID = project.getEnabledModules().getModuleID (i);
  519. getPathForModuleValue (modID) = getLegacyModulePath (modID).getParentDirectory().toUnixStyle();
  520. }
  521. settings.removeProperty ("juceFolder", nullptr);
  522. }
  523. }
  524. static bool areCompatibleExporters (const ProjectExporter& p1, const ProjectExporter& p2)
  525. {
  526. return (p1.isVisualStudio() && p2.isVisualStudio())
  527. || (p1.isXcode() && p2.isXcode())
  528. || (p1.isMakefile() && p2.isMakefile())
  529. || (p1.isAndroidStudio() && p2.isAndroidStudio())
  530. || (p1.isCodeBlocks() && p2.isCodeBlocks() && p1.isWindows() != p2.isLinux());
  531. }
  532. void ProjectExporter::createDefaultModulePaths()
  533. {
  534. for (Project::ExporterIterator exporter (project); exporter.next();)
  535. {
  536. if (areCompatibleExporters (*this, *exporter))
  537. {
  538. for (int i = project.getEnabledModules().getNumModules(); --i >= 0;)
  539. {
  540. auto modID = project.getEnabledModules().getModuleID (i);
  541. getPathForModuleValue (modID) = exporter->getPathForModuleValue (modID);
  542. }
  543. return;
  544. }
  545. }
  546. for (Project::ExporterIterator exporter (project); exporter.next();)
  547. {
  548. if (exporter->canLaunchProject())
  549. {
  550. for (int i = project.getEnabledModules().getNumModules(); --i >= 0;)
  551. {
  552. auto modID = project.getEnabledModules().getModuleID (i);
  553. getPathForModuleValue (modID) = exporter->getPathForModuleValue (modID);
  554. }
  555. return;
  556. }
  557. }
  558. for (int i = project.getEnabledModules().getNumModules(); --i >= 0;)
  559. {
  560. auto modID = project.getEnabledModules().getModuleID (i);
  561. getPathForModuleValue (modID) = "../../juce";
  562. }
  563. }
  564. //==============================================================================
  565. ValueTree ProjectExporter::getConfigurations() const
  566. {
  567. return settings.getChildWithName (Ids::CONFIGURATIONS);
  568. }
  569. int ProjectExporter::getNumConfigurations() const
  570. {
  571. return getConfigurations().getNumChildren();
  572. }
  573. ProjectExporter::BuildConfiguration::Ptr ProjectExporter::getConfiguration (int index) const
  574. {
  575. return createBuildConfig (getConfigurations().getChild (index));
  576. }
  577. bool ProjectExporter::hasConfigurationNamed (const String& nameToFind) const
  578. {
  579. auto configs = getConfigurations();
  580. for (int i = configs.getNumChildren(); --i >= 0;)
  581. if (configs.getChild(i) [Ids::name].toString() == nameToFind)
  582. return true;
  583. return false;
  584. }
  585. String ProjectExporter::getUniqueConfigName (String nm) const
  586. {
  587. auto nameRoot = nm;
  588. while (CharacterFunctions::isDigit (nameRoot.getLastCharacter()))
  589. nameRoot = nameRoot.dropLastCharacters (1);
  590. nameRoot = nameRoot.trim();
  591. int suffix = 2;
  592. while (hasConfigurationNamed (name))
  593. nm = nameRoot + " " + String (suffix++);
  594. return nm;
  595. }
  596. void ProjectExporter::addNewConfigurationFromExisting (const BuildConfiguration& configToCopy)
  597. {
  598. auto configs = getConfigurations();
  599. if (! configs.isValid())
  600. {
  601. settings.addChild (ValueTree (Ids::CONFIGURATIONS), 0, project.getUndoManagerFor (settings));
  602. configs = getConfigurations();
  603. }
  604. ValueTree newConfig (Ids::CONFIGURATION);
  605. newConfig = configToCopy.config.createCopy();
  606. newConfig.setProperty (Ids::name, configToCopy.getName(), nullptr);
  607. configs.appendChild (newConfig, project.getUndoManagerFor (configs));
  608. }
  609. void ProjectExporter::addNewConfiguration (bool isDebugConfig)
  610. {
  611. auto configs = getConfigurations();
  612. if (! configs.isValid())
  613. {
  614. settings.addChild (ValueTree (Ids::CONFIGURATIONS), 0, project.getUndoManagerFor (settings));
  615. configs = getConfigurations();
  616. }
  617. ValueTree newConfig (Ids::CONFIGURATION);
  618. newConfig.setProperty (Ids::isDebug, isDebugConfig, project.getUndoManagerFor (settings));
  619. configs.appendChild (newConfig, project.getUndoManagerFor (settings));
  620. }
  621. void ProjectExporter::BuildConfiguration::removeFromExporter()
  622. {
  623. ValueTree configs (config.getParent());
  624. configs.removeChild (config, project.getUndoManagerFor (configs));
  625. }
  626. void ProjectExporter::createDefaultConfigs()
  627. {
  628. settings.getOrCreateChildWithName (Ids::CONFIGURATIONS, nullptr);
  629. for (int i = 0; i < 2; ++i)
  630. {
  631. auto isDebug = i == 0;
  632. addNewConfiguration (isDebug);
  633. BuildConfiguration::Ptr config (getConfiguration (i));
  634. config->getValue (Ids::name) = (isDebug ? "Debug" : "Release");
  635. }
  636. }
  637. Drawable* ProjectExporter::getBigIcon() const
  638. {
  639. return project.getMainGroup().findItemWithID (settings [Ids::bigIcon]).loadAsImageFile();
  640. }
  641. Drawable* ProjectExporter::getSmallIcon() const
  642. {
  643. return project.getMainGroup().findItemWithID (settings [Ids::smallIcon]).loadAsImageFile();
  644. }
  645. Image ProjectExporter::getBestIconForSize (int size, bool returnNullIfNothingBigEnough) const
  646. {
  647. Drawable* im = nullptr;
  648. std::unique_ptr<Drawable> im1 (getSmallIcon());
  649. std::unique_ptr<Drawable> im2 (getBigIcon());
  650. if (im1 != nullptr && im2 != nullptr)
  651. {
  652. if (im1->getWidth() >= size && im2->getWidth() >= size)
  653. im = im1->getWidth() < im2->getWidth() ? im1.get() : im2.get();
  654. else if (im1->getWidth() >= size)
  655. im = im1.get();
  656. else if (im2->getWidth() >= size)
  657. im = im2.get();
  658. }
  659. else
  660. {
  661. im = im1 != nullptr ? im1.get() : im2.get();
  662. }
  663. if (im == nullptr)
  664. return {};
  665. if (returnNullIfNothingBigEnough && im->getWidth() < size && im->getHeight() < size)
  666. return {};
  667. return rescaleImageForIcon (*im, size);
  668. }
  669. Image ProjectExporter::rescaleImageForIcon (Drawable& d, const int size)
  670. {
  671. if (auto* drawableImage = dynamic_cast<DrawableImage*> (&d))
  672. {
  673. auto im = SoftwareImageType().convert (drawableImage->getImage());
  674. if (im.getWidth() == size && im.getHeight() == size)
  675. return im;
  676. // (scale it down in stages for better resampling)
  677. while (im.getWidth() > 2 * size && im.getHeight() > 2 * size)
  678. im = im.rescaled (im.getWidth() / 2,
  679. im.getHeight() / 2);
  680. Image newIm (Image::ARGB, size, size, true, SoftwareImageType());
  681. Graphics g (newIm);
  682. g.drawImageWithin (im, 0, 0, size, size,
  683. RectanglePlacement::centred | RectanglePlacement::onlyReduceInSize, false);
  684. return newIm;
  685. }
  686. Image im (Image::ARGB, size, size, true, SoftwareImageType());
  687. Graphics g (im);
  688. d.drawWithin (g, im.getBounds().toFloat(), RectanglePlacement::centred, 1.0f);
  689. return im;
  690. }
  691. //==============================================================================
  692. ProjectExporter::ConfigIterator::ConfigIterator (ProjectExporter& e)
  693. : index (-1), exporter (e)
  694. {
  695. }
  696. bool ProjectExporter::ConfigIterator::next()
  697. {
  698. if (++index >= exporter.getNumConfigurations())
  699. return false;
  700. config = exporter.getConfiguration (index);
  701. return true;
  702. }
  703. ProjectExporter::ConstConfigIterator::ConstConfigIterator (const ProjectExporter& exporter_)
  704. : index (-1), exporter (exporter_)
  705. {
  706. }
  707. bool ProjectExporter::ConstConfigIterator::next()
  708. {
  709. if (++index >= exporter.getNumConfigurations())
  710. return false;
  711. config = exporter.getConfiguration (index);
  712. return true;
  713. }
  714. //==============================================================================
  715. ProjectExporter::BuildConfiguration::BuildConfiguration (Project& p, const ValueTree& configNode, const ProjectExporter& e)
  716. : config (configNode), project (p), exporter (e),
  717. isDebugValue (config, Ids::isDebug, getUndoManager(), getValue (Ids::isDebug)),
  718. configNameValue (config, Ids::name, getUndoManager(), "Build Configuration"),
  719. targetNameValue (config, Ids::targetName, getUndoManager(), project.getProjectFilenameRootString()),
  720. targetBinaryPathValue (config, Ids::binaryPath, getUndoManager()),
  721. optimisationLevelValue (config, Ids::optimisation, getUndoManager()),
  722. linkTimeOptimisationValue (config, Ids::linkTimeOptimisation, getUndoManager(), ! isDebug()),
  723. ppDefinesValue (config, Ids::defines, getUndoManager()),
  724. headerSearchPathValue (config, Ids::headerPath, getUndoManager()),
  725. librarySearchPathValue (config, Ids::libraryPath, getUndoManager()),
  726. userNotesValue (config, Ids::userNotes, getUndoManager())
  727. {
  728. }
  729. ProjectExporter::BuildConfiguration::~BuildConfiguration()
  730. {
  731. }
  732. String ProjectExporter::BuildConfiguration::getGCCOptimisationFlag() const
  733. {
  734. switch (getOptimisationLevelInt())
  735. {
  736. case gccO0: return "0";
  737. case gccO1: return "1";
  738. case gccO2: return "2";
  739. case gccO3: return "3";
  740. case gccOs: return "s";
  741. case gccOfast: return "fast";
  742. default: break;
  743. }
  744. return "0";
  745. }
  746. void ProjectExporter::BuildConfiguration::addGCCOptimisationProperty (PropertyListBuilder& props)
  747. {
  748. props.add (new ChoicePropertyComponent (optimisationLevelValue, "Optimisation",
  749. { "-O0 (no optimisation)", "-Os (minimise code size)", "-O1 (fast)", "-O2 (faster)",
  750. "-O3 (fastest with safe optimisations)", "-Ofast (uses aggressive optimisations)" },
  751. { gccO0, gccOs, gccO1, gccO2, gccO3, gccOfast }),
  752. "The optimisation level for this configuration");
  753. }
  754. void ProjectExporter::BuildConfiguration::createPropertyEditors (PropertyListBuilder& props)
  755. {
  756. if (exporter.supportsUserDefinedConfigurations())
  757. props.add (new TextPropertyComponent (configNameValue, "Name", 96, false),
  758. "The name of this configuration.");
  759. props.add (new ChoicePropertyComponent (isDebugValue, "Debug Mode"),
  760. "If enabled, this means that the configuration should be built with debug symbols.");
  761. props.add (new TextPropertyComponent (targetNameValue, "Binary Name", 256, false),
  762. "The filename to use for the destination binary executable file. If you don't add a suffix to this name, "
  763. "a suitable platform-specific suffix will be added automatically.");
  764. props.add (new TextPropertyComponent (targetBinaryPathValue, "Binary Location", 1024, false),
  765. "The folder in which the finished binary should be placed. Leave this blank to cause the binary to be placed "
  766. "in its default location in the build folder.");
  767. props.addSearchPathProperty (headerSearchPathValue, "Header Search Paths", "Extra header search paths.");
  768. props.addSearchPathProperty (librarySearchPathValue, "Extra Library Search Paths", "Extra library search paths.");
  769. props.add (new TextPropertyComponent (ppDefinesValue, "Preprocessor Definitions", 32768, true),
  770. "Extra preprocessor definitions. Use the form \"NAME1=value NAME2=value\", using whitespace, commas, or "
  771. "new-lines to separate the items - to include a space or comma in a definition, precede it with a backslash.");
  772. props.add (new ChoicePropertyComponent (linkTimeOptimisationValue, "Link-Time Optimisation"),
  773. "Enable this to perform link-time code optimisation. This is recommended for release builds.");
  774. createConfigProperties (props);
  775. props.add (new TextPropertyComponent (userNotesValue, "Notes", 32768, true),
  776. "Extra comments: This field is not used for code or project generation, it's just a space where you can express your thoughts.");
  777. }
  778. StringPairArray ProjectExporter::BuildConfiguration::getAllPreprocessorDefs() const
  779. {
  780. return mergePreprocessorDefs (project.getPreprocessorDefs(),
  781. parsePreprocessorDefs (getBuildConfigPreprocessorDefsString()));
  782. }
  783. StringPairArray ProjectExporter::BuildConfiguration::getUniquePreprocessorDefs() const
  784. {
  785. auto perConfigurationDefs = parsePreprocessorDefs (getBuildConfigPreprocessorDefsString());
  786. auto globalDefs = project.getPreprocessorDefs();
  787. for (int i = 0; i < globalDefs.size(); ++i)
  788. {
  789. auto globalKey = globalDefs.getAllKeys()[i];
  790. int idx = perConfigurationDefs.getAllKeys().indexOf (globalKey);
  791. if (idx >= 0)
  792. {
  793. auto globalValue = globalDefs.getAllValues()[i];
  794. if (globalValue == perConfigurationDefs.getAllValues()[idx])
  795. perConfigurationDefs.remove (idx);
  796. }
  797. }
  798. return perConfigurationDefs;
  799. }
  800. StringArray ProjectExporter::BuildConfiguration::getHeaderSearchPaths() const
  801. {
  802. return getSearchPathsFromString (getHeaderSearchPathString() + ';' + project.getHeaderSearchPathsString());
  803. }
  804. StringArray ProjectExporter::BuildConfiguration::getLibrarySearchPaths() const
  805. {
  806. auto separator = exporter.isVisualStudio() ? "\\" : "/";
  807. auto s = getSearchPathsFromString (getLibrarySearchPathString());
  808. for (auto path : exporter.moduleLibSearchPaths)
  809. s.add (path + separator + getModuleLibraryArchName());
  810. return s;
  811. }
  812. String ProjectExporter::getExternalLibraryFlags (const BuildConfiguration& config) const
  813. {
  814. auto libraries = StringArray::fromTokens (getExternalLibrariesString(), ";\n", "\"'");
  815. libraries.removeEmptyStrings (true);
  816. if (libraries.size() != 0)
  817. return replacePreprocessorTokens (config, "-l" + libraries.joinIntoString (" -l")).trim();
  818. return {};
  819. }