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.

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