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
18KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #pragma once
  18. #include "../Project/jucer_Project.h"
  19. #include "../Project/jucer_ProjectType.h"
  20. #include "../Application/jucer_GlobalPreferences.h"
  21. class ProjectSaver;
  22. //==============================================================================
  23. class ProjectExporter
  24. {
  25. public:
  26. ProjectExporter (Project&, const ValueTree& settings);
  27. virtual ~ProjectExporter();
  28. struct ExporterTypeInfo
  29. {
  30. String name;
  31. const void* iconData;
  32. int iconDataSize;
  33. Image getIcon() const
  34. {
  35. Image image (Image::ARGB, 200, 200, true);
  36. Graphics g (image);
  37. ScopedPointer<Drawable> svgDrawable = Drawable::createFromImageData (iconData, (size_t) iconDataSize);
  38. svgDrawable->drawWithin (g, image.getBounds().toFloat(), RectanglePlacement::fillDestination, 1.0f);
  39. return image;
  40. }
  41. };
  42. static StringArray getExporterNames();
  43. static Array<ExporterTypeInfo> getExporterTypes();
  44. static ProjectExporter* createNewExporter (Project&, const int index);
  45. static ProjectExporter* createNewExporter (Project&, const String& name);
  46. static ProjectExporter* createExporter (Project&, const ValueTree& settings);
  47. static bool canProjectBeLaunched (Project*);
  48. static String getCurrentPlatformExporterName();
  49. //==============================================================================
  50. // capabilities of exporter
  51. virtual bool usesMMFiles() const = 0;
  52. virtual void createExporterProperties (PropertyListBuilder&) = 0;
  53. virtual bool canLaunchProject() = 0;
  54. virtual bool launchProject() = 0;
  55. virtual void create (const OwnedArray<LibraryModule>&) const = 0; // may throw a SaveError
  56. virtual bool shouldFileBeCompiledByDefault (const RelativePath& path) const;
  57. virtual bool canCopeWithDuplicateFiles() = 0;
  58. virtual bool supportsUserDefinedConfigurations() const = 0; // false if exporter only supports two configs Debug and Release
  59. virtual void updateDeprecatedProjectSettingsInteractively();
  60. // IDE targeted by exporter
  61. virtual bool isXcode() const = 0;
  62. virtual bool isVisualStudio() const = 0;
  63. virtual bool isCodeBlocks() const = 0;
  64. virtual bool isMakefile() const = 0;
  65. virtual bool isAndroidStudio() const = 0;
  66. // operating system targeted by exporter
  67. virtual bool isAndroid() const = 0;
  68. virtual bool isWindows() const = 0;
  69. virtual bool isLinux() const = 0;
  70. virtual bool isOSX() const = 0;
  71. virtual bool isiOS() const = 0;
  72. //==============================================================================
  73. // cross-platform audio plug-ins supported by exporter
  74. virtual bool supportsTargetType (ProjectType::Target::Type type) const = 0;
  75. inline bool shouldBuildTargetType (ProjectType::Target::Type type) const
  76. {
  77. return project.shouldBuildTargetType (type) && supportsTargetType (type);
  78. }
  79. inline void callForAllSupportedTargets (std::function<void (ProjectType::Target::Type)> callback)
  80. {
  81. for (int i = 0; i < ProjectType::Target::unspecified; ++i)
  82. if (shouldBuildTargetType (static_cast<ProjectType::Target::Type> (i)))
  83. callback (static_cast<ProjectType::Target::Type> (i));
  84. }
  85. //==============================================================================
  86. bool mayCompileOnCurrentOS() const
  87. {
  88. #if JUCE_MAC
  89. return isOSX() || isAndroid();
  90. #elif JUCE_WINDOWS
  91. return isWindows() || isAndroid();
  92. #elif JUCE_LINUX
  93. return isLinux() || isAndroid();
  94. #else
  95. #error
  96. #endif
  97. }
  98. //==============================================================================
  99. String getName() const { return name; }
  100. File getTargetFolder() const;
  101. Project& getProject() noexcept { return project; }
  102. const Project& getProject() const noexcept { return project; }
  103. Value getSetting (const Identifier& nm) { return settings.getPropertyAsValue (nm, project.getUndoManagerFor (settings)); }
  104. String getSettingString (const Identifier& nm) const { return settings [nm]; }
  105. Value getTargetLocationValue() { return getSetting (Ids::targetFolder); }
  106. String getTargetLocationString() const { return getSettingString (Ids::targetFolder); }
  107. Value getExtraCompilerFlags() { return getSetting (Ids::extraCompilerFlags); }
  108. String getExtraCompilerFlagsString() const { return getSettingString (Ids::extraCompilerFlags).replaceCharacters ("\r\n", " "); }
  109. Value getExtraLinkerFlags() { return getSetting (Ids::extraLinkerFlags); }
  110. String getExtraLinkerFlagsString() const { return getSettingString (Ids::extraLinkerFlags).replaceCharacters ("\r\n", " "); }
  111. Value getExternalLibraries() { return getSetting (Ids::externalLibraries); }
  112. String getExternalLibrariesString() const { return getSearchPathsFromString (getSettingString (Ids::externalLibraries)).joinIntoString (";"); }
  113. Value getUserNotes() { return getSetting (Ids::userNotes); }
  114. Value getVST3PathValue() const { return vst3Path; }
  115. Value getRTASPathValue() const { return rtasPath; }
  116. Value getAAXPathValue() const { return aaxPath; }
  117. // NB: this is the path to the parent "modules" folder that contains the named module, not the
  118. // module folder itself.
  119. Value getPathForModuleValue (const String& moduleID);
  120. String getPathForModuleString (const String& moduleID) const;
  121. void removePathForModule (const String& moduleID);
  122. RelativePath getLegacyModulePath (const String& moduleID) const;
  123. String getLegacyModulePath() const;
  124. // Returns a path to the actual module folder itself
  125. RelativePath getModuleFolderRelativeToProject (const String& moduleID) const;
  126. void updateOldModulePaths();
  127. RelativePath rebaseFromProjectFolderToBuildTarget (const RelativePath& path) const;
  128. void addToExtraSearchPaths (const RelativePath& pathFromProjectFolder, int index = -1);
  129. void addToModuleLibPaths (const RelativePath& pathFromProjectFolder);
  130. void addProjectPathToBuildPathList (StringArray&, const RelativePath&, int index = -1) const;
  131. Value getBigIconImageItemID() { return getSetting (Ids::bigIcon); }
  132. Value getSmallIconImageItemID() { return getSetting (Ids::smallIcon); }
  133. Drawable* getBigIcon() const;
  134. Drawable* getSmallIcon() const;
  135. Image getBestIconForSize (int size, bool returnNullIfNothingBigEnough) const;
  136. String getExporterIdentifierMacro() const
  137. {
  138. return "JUCER_" + settings.getType().toString() + "_"
  139. + String::toHexString (getSettingString (Ids::targetFolder).hashCode()).toUpperCase();
  140. }
  141. // An exception that can be thrown by the create() method.
  142. class SaveError
  143. {
  144. public:
  145. SaveError (const String& error) : message (error)
  146. {}
  147. SaveError (const File& fileThatFailedToWrite)
  148. : message ("Can't write to the file: " + fileThatFailedToWrite.getFullPathName())
  149. {}
  150. String message;
  151. };
  152. void createPropertyEditors (PropertyListBuilder&);
  153. void addSettingsForProjectType (const ProjectType&);
  154. //==============================================================================
  155. void copyMainGroupFromProject();
  156. Array<Project::Item>& getAllGroups() noexcept { jassert (itemGroups.size() > 0); return itemGroups; }
  157. const Array<Project::Item>& getAllGroups() const noexcept { jassert (itemGroups.size() > 0); return itemGroups; }
  158. Project::Item& getModulesGroup();
  159. //==============================================================================
  160. StringArray linuxLibs, linuxPackages, makefileExtraLinkerFlags;
  161. //==============================================================================
  162. StringPairArray msvcExtraPreprocessorDefs;
  163. String msvcDelayLoadedDLLs;
  164. StringArray mingwLibs, windowsLibs;
  165. //==============================================================================
  166. StringArray extraSearchPaths;
  167. StringArray moduleLibSearchPaths;
  168. //==============================================================================
  169. class BuildConfiguration : public ReferenceCountedObject
  170. {
  171. public:
  172. BuildConfiguration (Project& project, const ValueTree& configNode, const ProjectExporter&);
  173. ~BuildConfiguration();
  174. typedef ReferenceCountedObjectPtr<BuildConfiguration> Ptr;
  175. //==============================================================================
  176. virtual void createConfigProperties (PropertyListBuilder&) = 0;
  177. virtual var getDefaultOptimisationLevel() const = 0;
  178. virtual String getLibrarySubdirPath() const { return {}; }
  179. //==============================================================================
  180. Value getNameValue() { return getValue (Ids::name); }
  181. String getName() const { return config [Ids::name]; }
  182. Value isDebugValue() { return getValue (Ids::isDebug); }
  183. bool isDebug() const { return config [Ids::isDebug]; }
  184. Value getTargetBinaryName() { return getValue (Ids::targetName); }
  185. String getTargetBinaryNameString() const { return config [Ids::targetName]; }
  186. // the path relative to the build folder in which the binary should go
  187. Value getTargetBinaryRelativePath() { return getValue (Ids::binaryPath); }
  188. String getTargetBinaryRelativePathString() const { return config [Ids::binaryPath]; }
  189. Value getOptimisationLevel() { return getValue (Ids::optimisation); }
  190. int getOptimisationLevelInt() const { return config [Ids::optimisation]; }
  191. String getGCCOptimisationFlag() const;
  192. Value getBuildConfigPreprocessorDefs() { return getValue (Ids::defines); }
  193. String getBuildConfigPreprocessorDefsString() const { return config [Ids::defines]; }
  194. StringPairArray getAllPreprocessorDefs() const; // includes inherited definitions
  195. StringPairArray getUniquePreprocessorDefs() const; // returns pre-processor definitions that are not already in the project pre-processor defs
  196. Value getHeaderSearchPathValue() { return getValue (Ids::headerPath); }
  197. String getHeaderSearchPathString() const { return config [Ids::headerPath]; }
  198. StringArray getHeaderSearchPaths() const;
  199. Value getLibrarySearchPathValue() { return getValue (Ids::libraryPath); }
  200. String getLibrarySearchPathString() const { return config [Ids::libraryPath]; }
  201. StringArray getLibrarySearchPaths() const;
  202. String getGCCLibraryPathFlags() const;
  203. Value getUserNotes() { return getValue (Ids::userNotes); }
  204. Value getValue (const Identifier& nm) { return config.getPropertyAsValue (nm, getUndoManager()); }
  205. UndoManager* getUndoManager() const { return project.getUndoManagerFor (config); }
  206. void createPropertyEditors (PropertyListBuilder&);
  207. void addGCCOptimisationProperty (PropertyListBuilder&);
  208. void removeFromExporter();
  209. //==============================================================================
  210. ValueTree config;
  211. Project& project;
  212. const ProjectExporter& exporter;
  213. private:
  214. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (BuildConfiguration)
  215. };
  216. void addNewConfiguration (const BuildConfiguration* configToCopy);
  217. bool hasConfigurationNamed (const String& name) const;
  218. String getUniqueConfigName (String name) const;
  219. String getExternalLibraryFlags (const BuildConfiguration& config) const;
  220. //==============================================================================
  221. struct ConfigIterator
  222. {
  223. ConfigIterator (ProjectExporter& exporter);
  224. bool next();
  225. BuildConfiguration& operator*() const { return *config; }
  226. BuildConfiguration* operator->() const { return config; }
  227. BuildConfiguration::Ptr config;
  228. int index;
  229. private:
  230. ProjectExporter& exporter;
  231. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ConfigIterator)
  232. };
  233. struct ConstConfigIterator
  234. {
  235. ConstConfigIterator (const ProjectExporter& exporter);
  236. bool next();
  237. const BuildConfiguration& operator*() const { return *config; }
  238. const BuildConfiguration* operator->() const { return config; }
  239. BuildConfiguration::Ptr config;
  240. int index;
  241. private:
  242. const ProjectExporter& exporter;
  243. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ConstConfigIterator)
  244. };
  245. int getNumConfigurations() const;
  246. BuildConfiguration::Ptr getConfiguration (int index) const;
  247. ValueTree getConfigurations() const;
  248. void createDefaultConfigs();
  249. void createDefaultModulePaths();
  250. //==============================================================================
  251. Value getExporterPreprocessorDefs() { return getSetting (Ids::extraDefs); }
  252. String getExporterPreprocessorDefsString() const { return getSettingString (Ids::extraDefs); }
  253. // includes exporter, project + config defs
  254. StringPairArray getAllPreprocessorDefs (const BuildConfiguration& config, const ProjectType::Target::Type targetType) const;
  255. // includes exporter + project defs..
  256. StringPairArray getAllPreprocessorDefs() const;
  257. void addTargetSpecificPreprocessorDefs (StringPairArray& defs, const ProjectType::Target::Type targetType) const;
  258. String replacePreprocessorTokens (const BuildConfiguration&, const String& sourceString) const;
  259. ValueTree settings;
  260. enum GCCOptimisationLevel
  261. {
  262. gccO0 = 1,
  263. gccO1 = 4,
  264. gccO2 = 5,
  265. gccO3 = 3,
  266. gccOs = 2,
  267. gccOfast = 6
  268. };
  269. protected:
  270. //==============================================================================
  271. String name;
  272. Project& project;
  273. const ProjectType& projectType;
  274. const String projectName;
  275. const File projectFolder;
  276. Value vst3Path, rtasPath, aaxPath; // these must be initialised in the specific exporter c'tors!
  277. mutable Array<Project::Item> itemGroups;
  278. void initItemGroups() const;
  279. Project::Item* modulesGroup = nullptr;
  280. virtual BuildConfiguration::Ptr createBuildConfig (const ValueTree&) const = 0;
  281. void addDefaultPreprocessorDefs (StringPairArray&) const;
  282. static String getDefaultBuildsRootFolder() { return "Builds/"; }
  283. static String getStaticLibbedFilename (String name)
  284. {
  285. return addSuffix (addLibPrefix (name), ".a");
  286. }
  287. static String getDynamicLibbedFilename (String name)
  288. {
  289. return addSuffix (addLibPrefix (name), ".so");
  290. }
  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. };