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.

399 lines
16KB

  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. #ifndef JUCER_PROJECTEXPORTER_H_INCLUDED
  18. #define JUCER_PROJECTEXPORTER_H_INCLUDED
  19. #include "../jucer_Headers.h"
  20. #include "../Project/jucer_Project.h"
  21. #include "../Project/jucer_ProjectType.h"
  22. #include "../Application/jucer_GlobalPreferences.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. };
  36. static StringArray getExporterNames();
  37. static Array<ExporterTypeInfo> getExporterTypes();
  38. static ProjectExporter* createNewExporter (Project&, const int index);
  39. static ProjectExporter* createNewExporter (Project&, const String& name);
  40. static ProjectExporter* createExporter (Project&, const ValueTree& settings);
  41. static bool canProjectBeLaunched (Project*);
  42. static String getCurrentPlatformExporterName();
  43. //=============================================================================
  44. virtual bool usesMMFiles() const = 0;
  45. virtual void createExporterProperties (PropertyListBuilder&) = 0;
  46. virtual bool canLaunchProject() = 0;
  47. virtual bool launchProject() = 0;
  48. virtual void create (const OwnedArray<LibraryModule>&) const = 0; // may throw a SaveError
  49. virtual bool shouldFileBeCompiledByDefault (const RelativePath& path) const;
  50. virtual bool canCopeWithDuplicateFiles() = 0;
  51. virtual bool isXcode() const { return false; }
  52. virtual bool isVisualStudio() const { return false; }
  53. virtual int getVisualStudioVersion() const { return 0; }
  54. virtual bool isCodeBlocksWindows() const { return false; }
  55. virtual bool isCodeBlocksLinux() const { return false; }
  56. virtual bool isLinuxMakefile() const { return false; }
  57. virtual bool isAndroid() const { return false; }
  58. virtual bool isWindows() const { return false; }
  59. virtual bool isLinux() const { return false; }
  60. virtual bool isOSX() const { return false; }
  61. bool mayCompileOnCurrentOS() const
  62. {
  63. #if JUCE_MAC
  64. return isOSX() || isAndroid();
  65. #elif JUCE_WINDOWS
  66. return isWindows() || isAndroid();
  67. #elif JUCE_LINUX
  68. return isLinux() || isAndroid();
  69. #else
  70. #error
  71. #endif
  72. }
  73. //==============================================================================
  74. String getName() const { return name; }
  75. File getTargetFolder() const;
  76. Project& getProject() noexcept { return project; }
  77. const Project& getProject() const noexcept { return project; }
  78. Value getSetting (const Identifier& nm) { return settings.getPropertyAsValue (nm, project.getUndoManagerFor (settings)); }
  79. String getSettingString (const Identifier& nm) const { return settings [nm]; }
  80. Value getTargetLocationValue() { return getSetting (Ids::targetFolder); }
  81. String getTargetLocationString() const { return getSettingString (Ids::targetFolder); }
  82. Value getExtraCompilerFlags() { return getSetting (Ids::extraCompilerFlags); }
  83. String getExtraCompilerFlagsString() const { return getSettingString (Ids::extraCompilerFlags).replaceCharacters ("\r\n", " "); }
  84. Value getExtraLinkerFlags() { return getSetting (Ids::extraLinkerFlags); }
  85. String getExtraLinkerFlagsString() const { return getSettingString (Ids::extraLinkerFlags).replaceCharacters ("\r\n", " "); }
  86. Value getExternalLibraries() { return getSetting (Ids::externalLibraries); }
  87. String getExternalLibrariesString() const { return getSettingString (Ids::externalLibraries).replaceCharacters ("\r\n", " ;"); }
  88. Value getUserNotes() { return getSetting (Ids::userNotes); }
  89. Value getVSTPathValue (bool isVST3) const { return isVST3 ? vst3Path : vst2Path; }
  90. Value getRTASPathValue() const { return rtasPath; }
  91. Value getAAXPathValue() const { return aaxPath; }
  92. // NB: this is the path to the parent "modules" folder that contains the named module, not the
  93. // module folder itself.
  94. Value getPathForModuleValue (const String& moduleID);
  95. String getPathForModuleString (const String& moduleID) const;
  96. void removePathForModule (const String& moduleID);
  97. RelativePath getLegacyModulePath (const String& moduleID) const;
  98. String getLegacyModulePath() const;
  99. // Returns a path to the actual module folder itself
  100. RelativePath getModuleFolderRelativeToProject (const String& moduleID) const;
  101. void updateOldModulePaths();
  102. RelativePath rebaseFromProjectFolderToBuildTarget (const RelativePath& path) const;
  103. void addToExtraSearchPaths (const RelativePath& pathFromProjectFolder);
  104. Value getBigIconImageItemID() { return getSetting (Ids::bigIcon); }
  105. Value getSmallIconImageItemID() { return getSetting (Ids::smallIcon); }
  106. Drawable* getBigIcon() const;
  107. Drawable* getSmallIcon() const;
  108. Image getBestIconForSize (int size, bool returnNullIfNothingBigEnough) const;
  109. String getExporterIdentifierMacro() const
  110. {
  111. return "JUCER_" + settings.getType().toString() + "_"
  112. + String::toHexString (getSettingString (Ids::targetFolder).hashCode()).toUpperCase();
  113. }
  114. // An exception that can be thrown by the create() method.
  115. class SaveError
  116. {
  117. public:
  118. SaveError (const String& error) : message (error)
  119. {}
  120. SaveError (const File& fileThatFailedToWrite)
  121. : message ("Can't write to the file: " + fileThatFailedToWrite.getFullPathName())
  122. {}
  123. String message;
  124. };
  125. void createPropertyEditors (PropertyListBuilder& props);
  126. //==============================================================================
  127. void copyMainGroupFromProject();
  128. Array<Project::Item>& getAllGroups() noexcept { jassert (itemGroups.size() > 0); return itemGroups; }
  129. const Array<Project::Item>& getAllGroups() const noexcept { jassert (itemGroups.size() > 0); return itemGroups; }
  130. Project::Item& getModulesGroup();
  131. //==============================================================================
  132. String xcodePackageType, xcodeBundleSignature, xcodeBundleExtension;
  133. String xcodeProductType, xcodeProductInstallPath, xcodeFileType;
  134. String xcodeOtherRezFlags, xcodeExcludedFiles64Bit;
  135. bool xcodeIsBundle, xcodeCreatePList, xcodeCanUseDwarf;
  136. StringArray xcodeFrameworks, xcodeLibs;
  137. Array<RelativePath> xcodeExtraLibrariesDebug, xcodeExtraLibrariesRelease;
  138. Array<XmlElement> xcodeExtraPListEntries;
  139. //==============================================================================
  140. String makefileTargetSuffix;
  141. bool makefileIsDLL;
  142. StringArray linuxLibs, makefileExtraLinkerFlags;
  143. //==============================================================================
  144. String msvcTargetSuffix;
  145. StringPairArray msvcExtraPreprocessorDefs;
  146. bool msvcIsDLL, msvcIsWindowsSubsystem;
  147. String msvcDelayLoadedDLLs;
  148. StringArray mingwLibs;
  149. //==============================================================================
  150. StringArray extraSearchPaths;
  151. //==============================================================================
  152. class BuildConfiguration : public ReferenceCountedObject
  153. {
  154. public:
  155. BuildConfiguration (Project& project, const ValueTree& configNode);
  156. ~BuildConfiguration();
  157. typedef ReferenceCountedObjectPtr<BuildConfiguration> Ptr;
  158. //==============================================================================
  159. virtual void createConfigProperties (PropertyListBuilder&) = 0;
  160. virtual var getDefaultOptimisationLevel() const = 0;
  161. //==============================================================================
  162. Value getNameValue() { return getValue (Ids::name); }
  163. String getName() const { return config [Ids::name]; }
  164. Value isDebugValue() { return getValue (Ids::isDebug); }
  165. bool isDebug() const { return config [Ids::isDebug]; }
  166. Value getTargetBinaryName() { return getValue (Ids::targetName); }
  167. String getTargetBinaryNameString() const { return config [Ids::targetName]; }
  168. // the path relative to the build folder in which the binary should go
  169. Value getTargetBinaryRelativePath() { return getValue (Ids::binaryPath); }
  170. String getTargetBinaryRelativePathString() const { return config [Ids::binaryPath]; }
  171. Value getOptimisationLevel() { return getValue (Ids::optimisation); }
  172. int getOptimisationLevelInt() const { return config [Ids::optimisation]; }
  173. String getGCCOptimisationFlag() const;
  174. Value getBuildConfigPreprocessorDefs() { return getValue (Ids::defines); }
  175. String getBuildConfigPreprocessorDefsString() const { return config [Ids::defines]; }
  176. StringPairArray getAllPreprocessorDefs() const; // includes inherited definitions
  177. Value getHeaderSearchPathValue() { return getValue (Ids::headerPath); }
  178. String getHeaderSearchPathString() const { return config [Ids::headerPath]; }
  179. StringArray getHeaderSearchPaths() const;
  180. Value getLibrarySearchPathValue() { return getValue (Ids::libraryPath); }
  181. String getLibrarySearchPathString() const { return config [Ids::libraryPath]; }
  182. StringArray getLibrarySearchPaths() const;
  183. String getGCCLibraryPathFlags() const;
  184. Value getUserNotes() { return getValue (Ids::userNotes); }
  185. Value getValue (const Identifier& nm) { return config.getPropertyAsValue (nm, getUndoManager()); }
  186. UndoManager* getUndoManager() const { return project.getUndoManagerFor (config); }
  187. void createPropertyEditors (PropertyListBuilder&);
  188. void addGCCOptimisationProperty (PropertyListBuilder&);
  189. void removeFromExporter();
  190. //==============================================================================
  191. ValueTree config;
  192. Project& project;
  193. protected:
  194. private:
  195. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (BuildConfiguration)
  196. };
  197. void addNewConfiguration (const BuildConfiguration* configToCopy);
  198. bool hasConfigurationNamed (const String& name) const;
  199. String getUniqueConfigName (String name) const;
  200. String getExternalLibraryFlags (const BuildConfiguration& config) const;
  201. //==============================================================================
  202. struct ConfigIterator
  203. {
  204. ConfigIterator (ProjectExporter& exporter);
  205. bool next();
  206. BuildConfiguration& operator*() const { return *config; }
  207. BuildConfiguration* operator->() const { return config; }
  208. BuildConfiguration::Ptr config;
  209. int index;
  210. private:
  211. ProjectExporter& exporter;
  212. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ConfigIterator)
  213. };
  214. struct ConstConfigIterator
  215. {
  216. ConstConfigIterator (const ProjectExporter& exporter);
  217. bool next();
  218. const BuildConfiguration& operator*() const { return *config; }
  219. const BuildConfiguration* operator->() const { return config; }
  220. BuildConfiguration::Ptr config;
  221. int index;
  222. private:
  223. const ProjectExporter& exporter;
  224. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ConstConfigIterator)
  225. };
  226. int getNumConfigurations() const;
  227. BuildConfiguration::Ptr getConfiguration (int index) const;
  228. ValueTree getConfigurations() const;
  229. void createDefaultConfigs();
  230. void createDefaultModulePaths();
  231. //==============================================================================
  232. Value getExporterPreprocessorDefs() { return getSetting (Ids::extraDefs); }
  233. String getExporterPreprocessorDefsString() const { return getSettingString (Ids::extraDefs); }
  234. // includes exporter, project + config defs
  235. StringPairArray getAllPreprocessorDefs (const BuildConfiguration& config) const;
  236. // includes exporter + project defs..
  237. StringPairArray getAllPreprocessorDefs() const;
  238. String replacePreprocessorTokens (const BuildConfiguration&, const String& sourceString) const;
  239. ValueTree settings;
  240. enum GCCOptimisationLevel
  241. {
  242. gccO0 = 1,
  243. gccO1 = 4,
  244. gccO2 = 5,
  245. gccO3 = 3,
  246. gccOs = 2,
  247. gccOfast = 6
  248. };
  249. protected:
  250. //==============================================================================
  251. String name;
  252. Project& project;
  253. const ProjectType& projectType;
  254. const String projectName;
  255. const File projectFolder;
  256. Value vst2Path, vst3Path, rtasPath, aaxPath; // these must be initialised in the specific exporter c'tors!
  257. mutable Array<Project::Item> itemGroups;
  258. void initItemGroups() const;
  259. Project::Item* modulesGroup;
  260. virtual BuildConfiguration::Ptr createBuildConfig (const ValueTree&) const = 0;
  261. void addDefaultPreprocessorDefs (StringPairArray&) const;
  262. static String getDefaultBuildsRootFolder() { return "Builds/"; }
  263. static String getLibbedFilename (String name)
  264. {
  265. if (! name.startsWith ("lib")) name = "lib" + name;
  266. if (! name.endsWithIgnoreCase (".a")) name += ".a";
  267. return name;
  268. }
  269. //==============================================================================
  270. static void overwriteFileIfDifferentOrThrow (const File& file, const MemoryOutputStream& newData)
  271. {
  272. if (! FileHelpers::overwriteFileWithNewDataIfDifferent (file, newData))
  273. throw SaveError (file);
  274. }
  275. static void overwriteFileIfDifferentOrThrow (const File& file, const String& newData)
  276. {
  277. if (! FileHelpers::overwriteFileWithNewDataIfDifferent (file, newData))
  278. throw SaveError (file);
  279. }
  280. static void createDirectoryOrThrow (const File& dirToCreate)
  281. {
  282. if (! dirToCreate.createDirectory())
  283. throw SaveError ("Can't create folder: " + dirToCreate.getFullPathName());
  284. }
  285. static void writeXmlOrThrow (const XmlElement& xml, const File& file, const String& encoding, int maxCharsPerLine, bool useUnixNewLines = false)
  286. {
  287. MemoryOutputStream mo;
  288. xml.writeToStream (mo, String::empty, false, true, encoding, maxCharsPerLine);
  289. if (useUnixNewLines)
  290. {
  291. MemoryOutputStream mo2;
  292. mo2 << mo.toString().replace ("\r\n", "\n");
  293. overwriteFileIfDifferentOrThrow (file, mo2);
  294. }
  295. else
  296. {
  297. overwriteFileIfDifferentOrThrow (file, mo);
  298. }
  299. }
  300. static Image rescaleImageForIcon (Drawable&, int iconSize);
  301. private:
  302. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ProjectExporter)
  303. };
  304. #endif // JUCER_PROJECTEXPORTER_H_INCLUDED