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.

468 lines
19KB

  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. #pragma once
  20. #include "../Project/jucer_Project.h"
  21. #include "../Utility/UI/PropertyComponents/jucer_DependencyPathPropertyComponent.h"
  22. #include "../Utility/UI/PropertyComponents/jucer_TextWithDefaultPropertyComponent.h"
  23. #include "../Utility/UI/PropertyComponents/jucer_TextWithDefaultPropertyComponentWithEnablement.h"
  24. class ProjectSaver;
  25. //==============================================================================
  26. class ProjectExporter
  27. {
  28. public:
  29. ProjectExporter (Project&, const ValueTree& settings);
  30. virtual ~ProjectExporter();
  31. struct ExporterTypeInfo
  32. {
  33. String name;
  34. const void* iconData;
  35. int iconDataSize;
  36. Image getIcon() const
  37. {
  38. Image image (Image::ARGB, 200, 200, true);
  39. Graphics g (image);
  40. ScopedPointer<Drawable> svgDrawable = Drawable::createFromImageData (iconData, (size_t) iconDataSize);
  41. svgDrawable->drawWithin (g, image.getBounds().toFloat(), RectanglePlacement::fillDestination, 1.0f);
  42. return image;
  43. }
  44. };
  45. static StringArray getExporterNames();
  46. static Array<ExporterTypeInfo> getExporterTypes();
  47. static String getValueTreeNameForExporter (const String& exporterName);
  48. static StringArray getAllDefaultBuildsFolders();
  49. static ProjectExporter* createNewExporter (Project&, const int index);
  50. static ProjectExporter* createNewExporter (Project&, const String& name);
  51. static ProjectExporter* createExporter (Project&, const ValueTree& settings);
  52. static bool canProjectBeLaunched (Project*);
  53. static String getCurrentPlatformExporterName();
  54. //==============================================================================
  55. // capabilities of exporter
  56. virtual bool usesMMFiles() const = 0;
  57. virtual void createExporterProperties (PropertyListBuilder&) = 0;
  58. virtual bool canLaunchProject() = 0;
  59. virtual bool launchProject() = 0;
  60. virtual void create (const OwnedArray<LibraryModule>&) const = 0; // may throw a SaveError
  61. virtual bool shouldFileBeCompiledByDefault (const RelativePath& path) const;
  62. virtual bool canCopeWithDuplicateFiles() = 0;
  63. virtual bool supportsUserDefinedConfigurations() const = 0; // false if exporter only supports two configs Debug and Release
  64. virtual void updateDeprecatedProjectSettingsInteractively();
  65. virtual void initialiseDependencyPathValues() {}
  66. // IDE targeted by exporter
  67. virtual bool isXcode() const = 0;
  68. virtual bool isVisualStudio() const = 0;
  69. virtual bool isCodeBlocks() const = 0;
  70. virtual bool isMakefile() const = 0;
  71. virtual bool isAndroidStudio() const = 0;
  72. virtual bool isCLion() const = 0;
  73. // operating system targeted by exporter
  74. virtual bool isAndroid() const = 0;
  75. virtual bool isWindows() const = 0;
  76. virtual bool isLinux() const = 0;
  77. virtual bool isOSX() const = 0;
  78. virtual bool isiOS() const = 0;
  79. virtual String getDescription() { return {}; }
  80. //==============================================================================
  81. // cross-platform audio plug-ins supported by exporter
  82. virtual bool supportsTargetType (ProjectType::Target::Type type) const = 0;
  83. inline bool shouldBuildTargetType (ProjectType::Target::Type type) const
  84. {
  85. return project.shouldBuildTargetType (type) && supportsTargetType (type);
  86. }
  87. inline void callForAllSupportedTargets (std::function<void (ProjectType::Target::Type)> callback)
  88. {
  89. for (int i = 0; i < ProjectType::Target::unspecified; ++i)
  90. if (shouldBuildTargetType (static_cast<ProjectType::Target::Type> (i)))
  91. callback (static_cast<ProjectType::Target::Type> (i));
  92. }
  93. //==============================================================================
  94. bool mayCompileOnCurrentOS() const
  95. {
  96. #if JUCE_MAC
  97. return isOSX() || isAndroid() || isiOS();
  98. #elif JUCE_WINDOWS
  99. return isWindows() || isAndroid();
  100. #elif JUCE_LINUX
  101. return isLinux() || isAndroid();
  102. #else
  103. #error
  104. #endif
  105. }
  106. //==============================================================================
  107. String getName() const;
  108. File getTargetFolder() const;
  109. Project& getProject() noexcept { return project; }
  110. const Project& getProject() const noexcept { return project; }
  111. Value getSetting (const Identifier& nm) { return settings.getPropertyAsValue (nm, project.getUndoManagerFor (settings)); }
  112. String getSettingString (const Identifier& nm) const { return settings [nm]; }
  113. Value getTargetLocationValue() { return getSetting (Ids::targetFolder); }
  114. String getTargetLocationString() const { return getSettingString (Ids::targetFolder); }
  115. Value getExtraCompilerFlags() { return getSetting (Ids::extraCompilerFlags); }
  116. String getExtraCompilerFlagsString() const { return getSettingString (Ids::extraCompilerFlags).replaceCharacters ("\r\n", " "); }
  117. Value getExtraLinkerFlags() { return getSetting (Ids::extraLinkerFlags); }
  118. String getExtraLinkerFlagsString() const { return getSettingString (Ids::extraLinkerFlags).replaceCharacters ("\r\n", " "); }
  119. Value getExternalLibraries() { return getSetting (Ids::externalLibraries); }
  120. String getExternalLibrariesString() const { return getSearchPathsFromString (getSettingString (Ids::externalLibraries)).joinIntoString (";"); }
  121. Value getUserNotes() { return getSetting (Ids::userNotes); }
  122. Value getVST3PathValue() const { return vst3Path; }
  123. Value getRTASPathValue() const { return rtasPath; }
  124. Value getAAXPathValue() const { return aaxPath; }
  125. Value getShouldUseGNUExtensionsValue() { return getSetting (Ids::enableGNUExtensions); }
  126. bool shouldUseGNUExtensions() const { return (getSettingString (Ids::enableGNUExtensions) == "1");}
  127. // NB: this is the path to the parent "modules" folder that contains the named module, not the
  128. // module folder itself.
  129. Value getPathForModuleValue (const String& moduleID);
  130. String getPathForModuleString (const String& moduleID) const;
  131. void removePathForModule (const String& moduleID);
  132. TargetOS::OS getTargetOSForExporter() const;
  133. RelativePath getLegacyModulePath (const String& moduleID) const;
  134. String getLegacyModulePath() const;
  135. // Returns a path to the actual module folder itself
  136. RelativePath getModuleFolderRelativeToProject (const String& moduleID) const;
  137. void updateOldModulePaths();
  138. RelativePath rebaseFromProjectFolderToBuildTarget (const RelativePath& path) const;
  139. void addToExtraSearchPaths (const RelativePath& pathFromProjectFolder, int index = -1);
  140. void addToModuleLibPaths (const RelativePath& pathFromProjectFolder);
  141. void addProjectPathToBuildPathList (StringArray&, const RelativePath&, int index = -1) const;
  142. Value getBigIconImageItemID() { return getSetting (Ids::bigIcon); }
  143. Value getSmallIconImageItemID() { return getSetting (Ids::smallIcon); }
  144. Drawable* getBigIcon() const;
  145. Drawable* getSmallIcon() const;
  146. Image getBestIconForSize (int size, bool returnNullIfNothingBigEnough) const;
  147. String getExporterIdentifierMacro() const
  148. {
  149. return "JUCER_" + settings.getType().toString() + "_"
  150. + String::toHexString (getSettingString (Ids::targetFolder).hashCode()).toUpperCase();
  151. }
  152. // An exception that can be thrown by the create() method.
  153. class SaveError
  154. {
  155. public:
  156. SaveError (const String& error) : message (error)
  157. {}
  158. SaveError (const File& fileThatFailedToWrite)
  159. : message ("Can't write to the file: " + fileThatFailedToWrite.getFullPathName())
  160. {}
  161. String message;
  162. };
  163. void createPropertyEditors (PropertyListBuilder&);
  164. void addSettingsForProjectType (const ProjectType&);
  165. //==============================================================================
  166. void copyMainGroupFromProject();
  167. Array<Project::Item>& getAllGroups() noexcept { jassert (itemGroups.size() > 0); return itemGroups; }
  168. const Array<Project::Item>& getAllGroups() const noexcept { jassert (itemGroups.size() > 0); return itemGroups; }
  169. Project::Item& getModulesGroup();
  170. //==============================================================================
  171. StringArray linuxLibs, linuxPackages, makefileExtraLinkerFlags;
  172. //==============================================================================
  173. StringPairArray msvcExtraPreprocessorDefs;
  174. String msvcDelayLoadedDLLs;
  175. StringArray mingwLibs, windowsLibs;
  176. //==============================================================================
  177. StringArray androidLibs;
  178. //==============================================================================
  179. StringArray extraSearchPaths;
  180. StringArray moduleLibSearchPaths;
  181. //==============================================================================
  182. class BuildConfiguration : public ReferenceCountedObject
  183. {
  184. public:
  185. BuildConfiguration (Project& project, const ValueTree& configNode, const ProjectExporter&);
  186. ~BuildConfiguration();
  187. typedef ReferenceCountedObjectPtr<BuildConfiguration> Ptr;
  188. //==============================================================================
  189. virtual void createConfigProperties (PropertyListBuilder&) = 0;
  190. virtual var getDefaultOptimisationLevel() const = 0;
  191. virtual String getModuleLibraryArchName() const = 0;
  192. //==============================================================================
  193. Value getNameValue() { return getValue (Ids::name); }
  194. String getName() const { return config [Ids::name]; }
  195. Value isDebugValue() { return getValue (Ids::isDebug); }
  196. bool isDebug() const { return config [Ids::isDebug]; }
  197. Value getTargetBinaryName() { return getValue (Ids::targetName); }
  198. String getTargetBinaryNameString() const { return config [Ids::targetName]; }
  199. // the path relative to the build folder in which the binary should go
  200. Value getTargetBinaryRelativePath() { return getValue (Ids::binaryPath); }
  201. String getTargetBinaryRelativePathString() const { return config [Ids::binaryPath]; }
  202. Value getOptimisationLevel() { return getValue (Ids::optimisation); }
  203. int getOptimisationLevelInt() const { return config [Ids::optimisation]; }
  204. String getGCCOptimisationFlag() const;
  205. Value getLinkTimeOptimisationEnabledValue() { return getValue (Ids::linkTimeOptimisation); }
  206. bool isLinkTimeOptimisationEnabled() const { return config [Ids::linkTimeOptimisation]; }
  207. Value getBuildConfigPreprocessorDefs() { return getValue (Ids::defines); }
  208. String getBuildConfigPreprocessorDefsString() const { return config [Ids::defines]; }
  209. StringPairArray getAllPreprocessorDefs() const; // includes inherited definitions
  210. StringPairArray getUniquePreprocessorDefs() const; // returns pre-processor definitions that are not already in the project pre-processor defs
  211. Value getHeaderSearchPathValue() { return getValue (Ids::headerPath); }
  212. String getHeaderSearchPathString() const { return config [Ids::headerPath]; }
  213. StringArray getHeaderSearchPaths() const;
  214. Value getLibrarySearchPathValue() { return getValue (Ids::libraryPath); }
  215. String getLibrarySearchPathString() const { return config [Ids::libraryPath]; }
  216. StringArray getLibrarySearchPaths() const;
  217. String getGCCLibraryPathFlags() const;
  218. Value getUserNotes() { return getValue (Ids::userNotes); }
  219. Value getValue (const Identifier& nm) { return config.getPropertyAsValue (nm, getUndoManager()); }
  220. UndoManager* getUndoManager() const { return project.getUndoManagerFor (config); }
  221. void createPropertyEditors (PropertyListBuilder&);
  222. void addGCCOptimisationProperty (PropertyListBuilder&);
  223. void removeFromExporter();
  224. //==============================================================================
  225. ValueTree config;
  226. Project& project;
  227. const ProjectExporter& exporter;
  228. private:
  229. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (BuildConfiguration)
  230. };
  231. void addNewConfiguration (const BuildConfiguration* configToCopy);
  232. bool hasConfigurationNamed (const String& name) const;
  233. String getUniqueConfigName (String name) const;
  234. String getExternalLibraryFlags (const BuildConfiguration& config) const;
  235. //==============================================================================
  236. struct ConfigIterator
  237. {
  238. ConfigIterator (ProjectExporter& exporter);
  239. bool next();
  240. BuildConfiguration& operator*() const { return *config; }
  241. BuildConfiguration* operator->() const { return config; }
  242. BuildConfiguration::Ptr config;
  243. int index;
  244. private:
  245. ProjectExporter& exporter;
  246. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ConfigIterator)
  247. };
  248. struct ConstConfigIterator
  249. {
  250. ConstConfigIterator (const ProjectExporter& exporter);
  251. bool next();
  252. const BuildConfiguration& operator*() const { return *config; }
  253. const BuildConfiguration* operator->() const { return config; }
  254. BuildConfiguration::Ptr config;
  255. int index;
  256. private:
  257. const ProjectExporter& exporter;
  258. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ConstConfigIterator)
  259. };
  260. int getNumConfigurations() const;
  261. BuildConfiguration::Ptr getConfiguration (int index) const;
  262. ValueTree getConfigurations() const;
  263. virtual void createDefaultConfigs();
  264. void createDefaultModulePaths();
  265. //==============================================================================
  266. Value getExporterPreprocessorDefs() { return getSetting (Ids::extraDefs); }
  267. String getExporterPreprocessorDefsString() const { return getSettingString (Ids::extraDefs); }
  268. // includes exporter, project + config defs
  269. StringPairArray getAllPreprocessorDefs (const BuildConfiguration& config, const ProjectType::Target::Type targetType) const;
  270. // includes exporter + project defs..
  271. StringPairArray getAllPreprocessorDefs() const;
  272. void addTargetSpecificPreprocessorDefs (StringPairArray& defs, const ProjectType::Target::Type targetType) const;
  273. String replacePreprocessorTokens (const BuildConfiguration&, const String& sourceString) const;
  274. ValueTree settings;
  275. enum GCCOptimisationLevel
  276. {
  277. gccO0 = 1,
  278. gccO1 = 4,
  279. gccO2 = 5,
  280. gccO3 = 3,
  281. gccOs = 2,
  282. gccOfast = 6
  283. };
  284. protected:
  285. //==============================================================================
  286. String name;
  287. Project& project;
  288. const ProjectType& projectType;
  289. const String projectName;
  290. const File projectFolder;
  291. Value vst3Path, rtasPath, aaxPath; // these must be initialised in the specific exporter c'tors!
  292. mutable Array<Project::Item> itemGroups;
  293. void initItemGroups() const;
  294. Project::Item* modulesGroup = nullptr;
  295. virtual BuildConfiguration::Ptr createBuildConfig (const ValueTree&) const = 0;
  296. void addDefaultPreprocessorDefs (StringPairArray&) const;
  297. static String getDefaultBuildsRootFolder() { return "Builds/"; }
  298. static String getStaticLibbedFilename (String name)
  299. {
  300. return addSuffix (addLibPrefix (name), ".a");
  301. }
  302. static String getDynamicLibbedFilename (String name)
  303. {
  304. return addSuffix (addLibPrefix (name), ".so");
  305. }
  306. virtual void addPlatformSpecificSettingsForProjectType (const ProjectType&) = 0;
  307. //==============================================================================
  308. static void overwriteFileIfDifferentOrThrow (const File& file, const MemoryOutputStream& newData)
  309. {
  310. if (! FileHelpers::overwriteFileWithNewDataIfDifferent (file, newData))
  311. throw SaveError (file);
  312. }
  313. static void overwriteFileIfDifferentOrThrow (const File& file, const String& newData)
  314. {
  315. if (! FileHelpers::overwriteFileWithNewDataIfDifferent (file, newData))
  316. throw SaveError (file);
  317. }
  318. static void createDirectoryOrThrow (const File& dirToCreate)
  319. {
  320. if (! dirToCreate.createDirectory())
  321. throw SaveError ("Can't create folder: " + dirToCreate.getFullPathName());
  322. }
  323. static void writeXmlOrThrow (const XmlElement& xml, const File& file, const String& encoding, int maxCharsPerLine, bool useUnixNewLines = false)
  324. {
  325. MemoryOutputStream mo;
  326. xml.writeToStream (mo, String(), false, true, encoding, maxCharsPerLine);
  327. if (useUnixNewLines)
  328. {
  329. MemoryOutputStream mo2;
  330. mo2 << mo.toString().replace ("\r\n", "\n");
  331. overwriteFileIfDifferentOrThrow (file, mo2);
  332. }
  333. else
  334. {
  335. overwriteFileIfDifferentOrThrow (file, mo);
  336. }
  337. }
  338. static Image rescaleImageForIcon (Drawable&, int iconSize);
  339. private:
  340. //==============================================================================
  341. static String addLibPrefix (const String name)
  342. {
  343. return name.startsWith ("lib") ? name
  344. : "lib" + name;
  345. }
  346. static String addSuffix (const String name, const String suffix)
  347. {
  348. return name.endsWithIgnoreCase (suffix) ? name
  349. : name + suffix;
  350. }
  351. void createDependencyPathProperties (PropertyListBuilder&);
  352. void createIconProperties (PropertyListBuilder&);
  353. void addVSTPathsIfPluginOrHost();
  354. void addCommonAudioPluginSettings();
  355. void addVST3FolderToPath();
  356. void addAAXFoldersToPath();
  357. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ProjectExporter)
  358. };