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.

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