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.

386 lines
16KB

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