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.

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