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.

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