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.

451 lines
18KB

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