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.

390 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, xcodeLibs;
  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. virtual var getDefaultOptimisationLevel() const = 0;
  155. //==============================================================================
  156. Value getNameValue() { return getValue (Ids::name); }
  157. String getName() const { return config [Ids::name]; }
  158. Value isDebugValue() { return getValue (Ids::isDebug); }
  159. bool isDebug() const { return config [Ids::isDebug]; }
  160. Value getTargetBinaryName() { return getValue (Ids::targetName); }
  161. String getTargetBinaryNameString() const { return config [Ids::targetName]; }
  162. // the path relative to the build folder in which the binary should go
  163. Value getTargetBinaryRelativePath() { return getValue (Ids::binaryPath); }
  164. String getTargetBinaryRelativePathString() const { return config [Ids::binaryPath]; }
  165. Value getOptimisationLevel() { return getValue (Ids::optimisation); }
  166. int getOptimisationLevelInt() const { return config [Ids::optimisation]; }
  167. String getGCCOptimisationFlag() const;
  168. Value getBuildConfigPreprocessorDefs() { return getValue (Ids::defines); }
  169. String getBuildConfigPreprocessorDefsString() const { return config [Ids::defines]; }
  170. StringPairArray getAllPreprocessorDefs() const; // includes inherited definitions
  171. Value getHeaderSearchPathValue() { return getValue (Ids::headerPath); }
  172. String getHeaderSearchPathString() const { return config [Ids::headerPath]; }
  173. StringArray getHeaderSearchPaths() const;
  174. Value getLibrarySearchPathValue() { return getValue (Ids::libraryPath); }
  175. String getLibrarySearchPathString() const { return config [Ids::libraryPath]; }
  176. StringArray getLibrarySearchPaths() const;
  177. String getGCCLibraryPathFlags() const;
  178. Value getUserNotes() { return getValue (Ids::userNotes); }
  179. Value getValue (const Identifier& nm) { return config.getPropertyAsValue (nm, getUndoManager()); }
  180. UndoManager* getUndoManager() const { return project.getUndoManagerFor (config); }
  181. void createPropertyEditors (PropertyListBuilder&);
  182. void addGCCOptimisationProperty (PropertyListBuilder&);
  183. void removeFromExporter();
  184. //==============================================================================
  185. ValueTree config;
  186. Project& project;
  187. protected:
  188. private:
  189. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (BuildConfiguration)
  190. };
  191. void addNewConfiguration (const BuildConfiguration* configToCopy);
  192. bool hasConfigurationNamed (const String& name) const;
  193. String getUniqueConfigName (String name) const;
  194. String getExternalLibraryFlags (const BuildConfiguration& config) const;
  195. //==============================================================================
  196. struct ConfigIterator
  197. {
  198. ConfigIterator (ProjectExporter& exporter);
  199. bool next();
  200. BuildConfiguration& operator*() const { return *config; }
  201. BuildConfiguration* operator->() const { return config; }
  202. BuildConfiguration::Ptr config;
  203. int index;
  204. private:
  205. ProjectExporter& exporter;
  206. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ConfigIterator)
  207. };
  208. struct ConstConfigIterator
  209. {
  210. ConstConfigIterator (const ProjectExporter& exporter);
  211. bool next();
  212. const BuildConfiguration& operator*() const { return *config; }
  213. const BuildConfiguration* operator->() const { return config; }
  214. BuildConfiguration::Ptr config;
  215. int index;
  216. private:
  217. const ProjectExporter& exporter;
  218. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ConstConfigIterator)
  219. };
  220. int getNumConfigurations() const;
  221. BuildConfiguration::Ptr getConfiguration (int index) const;
  222. ValueTree getConfigurations() const;
  223. void createDefaultConfigs();
  224. void createDefaultModulePaths();
  225. //==============================================================================
  226. Value getExporterPreprocessorDefs() { return getSetting (Ids::extraDefs); }
  227. String getExporterPreprocessorDefsString() const { return getSettingString (Ids::extraDefs); }
  228. // includes exporter, project + config defs
  229. StringPairArray getAllPreprocessorDefs (const BuildConfiguration& config) const;
  230. // includes exporter + project defs..
  231. StringPairArray getAllPreprocessorDefs() const;
  232. String replacePreprocessorTokens (const BuildConfiguration&, const String& sourceString) const;
  233. ValueTree settings;
  234. enum GCCOptimisationLevel
  235. {
  236. gccO0 = 1,
  237. gccO1 = 4,
  238. gccO2 = 5,
  239. gccO3 = 3,
  240. gccOs = 2,
  241. gccOfast = 6
  242. };
  243. protected:
  244. //==============================================================================
  245. String name;
  246. Project& project;
  247. const ProjectType& projectType;
  248. const String projectName;
  249. const File projectFolder;
  250. mutable Array<Project::Item> itemGroups;
  251. void initItemGroups() const;
  252. Project::Item* modulesGroup;
  253. virtual BuildConfiguration::Ptr createBuildConfig (const ValueTree&) const = 0;
  254. void addDefaultPreprocessorDefs (StringPairArray&) const;
  255. static String getDefaultBuildsRootFolder() { return "Builds/"; }
  256. static String getLibbedFilename (String name)
  257. {
  258. if (! name.startsWith ("lib")) name = "lib" + name;
  259. if (! name.endsWithIgnoreCase (".a")) name += ".a";
  260. return name;
  261. }
  262. //==============================================================================
  263. static void overwriteFileIfDifferentOrThrow (const File& file, const MemoryOutputStream& newData)
  264. {
  265. if (! FileHelpers::overwriteFileWithNewDataIfDifferent (file, newData))
  266. throw SaveError (file);
  267. }
  268. static void overwriteFileIfDifferentOrThrow (const File& file, const String& newData)
  269. {
  270. if (! FileHelpers::overwriteFileWithNewDataIfDifferent (file, newData))
  271. throw SaveError (file);
  272. }
  273. static void createDirectoryOrThrow (const File& dirToCreate)
  274. {
  275. if (! dirToCreate.createDirectory())
  276. throw SaveError ("Can't create folder: " + dirToCreate.getFullPathName());
  277. }
  278. static void writeXmlOrThrow (const XmlElement& xml, const File& file, const String& encoding, int maxCharsPerLine, bool useUnixNewLines = false)
  279. {
  280. MemoryOutputStream mo;
  281. xml.writeToStream (mo, String::empty, false, true, encoding, maxCharsPerLine);
  282. if (useUnixNewLines)
  283. {
  284. MemoryOutputStream mo2;
  285. mo2 << mo.toString().replace ("\r\n", "\n");
  286. overwriteFileIfDifferentOrThrow (file, mo2);
  287. }
  288. else
  289. {
  290. overwriteFileIfDifferentOrThrow (file, mo);
  291. }
  292. }
  293. static Image rescaleImageForIcon (Drawable&, int iconSize);
  294. private:
  295. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ProjectExporter)
  296. };
  297. #endif // __JUCER_PROJECTEXPORTER_JUCEHEADER__