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.

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