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.

372 lines
15KB

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