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.

400 lines
17KB

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