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.

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