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.

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