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.

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