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.

482 lines
20KB

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