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.

449 lines
17KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI 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. class CodeBlocksProjectExporter : public ProjectExporter
  18. {
  19. public:
  20. enum CodeBlocksOS
  21. {
  22. windowsTarget,
  23. linuxTarget
  24. };
  25. //==============================================================================
  26. static const char* getNameWindows() noexcept { return "Code::Blocks (Windows)"; }
  27. static const char* getNameLinux() noexcept { return "Code::Blocks (Linux)"; }
  28. static const char* getName (CodeBlocksOS os) noexcept
  29. {
  30. if (os == windowsTarget) return getNameWindows();
  31. if (os == linuxTarget) return getNameLinux();
  32. // currently no other OSes supported by Codeblocks exporter!
  33. jassertfalse;
  34. return "Code::Blocks (Unknown OS)";
  35. }
  36. //==============================================================================
  37. static const char* getValueTreeTypeName (CodeBlocksOS os)
  38. {
  39. if (os == windowsTarget) return "CODEBLOCKS_WINDOWS";
  40. if (os == linuxTarget) return "CODEBLOCKS_LINUX";
  41. // currently no other OSes supported by Codeblocks exporter!
  42. jassertfalse;
  43. return "CODEBLOCKS_UNKOWN_OS";
  44. }
  45. //==============================================================================
  46. static String getTargetFolderName (CodeBlocksOS os)
  47. {
  48. if (os == windowsTarget) return "CodeBlocksWindows";
  49. if (os == linuxTarget) return "CodeBlocksLinux";
  50. // currently no other OSes supported by Codeblocks exporter!
  51. jassertfalse;
  52. return "CodeBlocksUnknownOS";
  53. }
  54. //==============================================================================
  55. static CodeBlocksProjectExporter* createForSettings (Project& project, const ValueTree& settings)
  56. {
  57. // this will also import legacy jucer files where CodeBlocks only worked for Windows,
  58. // had valueTreetTypeName "CODEBLOCKS", and there was no OS distinction
  59. if (settings.hasType (getValueTreeTypeName (windowsTarget)) || settings.hasType ("CODEBLOCKS"))
  60. return new CodeBlocksProjectExporter (project, settings, windowsTarget);
  61. if (settings.hasType (getValueTreeTypeName (linuxTarget)))
  62. return new CodeBlocksProjectExporter (project, settings, linuxTarget);
  63. return nullptr;
  64. }
  65. //==============================================================================
  66. CodeBlocksProjectExporter (Project& p, const ValueTree& t, CodeBlocksOS codeBlocksOs)
  67. : ProjectExporter (p, t), os (codeBlocksOs)
  68. {
  69. name = getName (os);
  70. if (getTargetLocationString().isEmpty())
  71. getTargetLocationValue() = getDefaultBuildsRootFolder() + getTargetFolderName (os);
  72. initialiseDependencyPathValues();
  73. }
  74. //==============================================================================
  75. bool canLaunchProject() override { return false; }
  76. bool launchProject() override { return false; }
  77. bool usesMMFiles() const override { return false; }
  78. bool canCopeWithDuplicateFiles() override { return false; }
  79. bool supportsUserDefinedConfigurations() const override { return true; }
  80. bool isXcode() const override { return false; }
  81. bool isVisualStudio() const override { return false; }
  82. bool isCodeBlocks() const override { return true; }
  83. bool isMakefile() const override { return false; }
  84. bool isAndroidStudio() const override { return false; }
  85. bool isAndroidAnt() const override { return false; }
  86. bool isAndroid() const override { return false; }
  87. bool isWindows() const override { return os == windowsTarget; }
  88. bool isLinux() const override { return os == linuxTarget; }
  89. bool isOSX() const override { return false; }
  90. bool isiOS() const override { return false; }
  91. bool supportsVST() const override { return false; }
  92. bool supportsVST3() const override { return false; }
  93. bool supportsAAX() const override { return false; }
  94. bool supportsRTAS() const override { return false; }
  95. bool supportsAU() const override { return false; }
  96. bool supportsAUv3() const override { return false; }
  97. bool supportsStandalone() const override { return false; }
  98. void createExporterProperties (PropertyListBuilder&) override
  99. {
  100. }
  101. //==============================================================================
  102. void create (const OwnedArray<LibraryModule>&) const override
  103. {
  104. const File cbpFile (getTargetFolder().getChildFile (project.getProjectFilenameRoot())
  105. .withFileExtension (".cbp"));
  106. XmlElement xml ("CodeBlocks_project_file");
  107. addVersion (xml);
  108. createProject (*xml.createNewChildElement ("Project"));
  109. writeXmlOrThrow (xml, cbpFile, "UTF-8", 10);
  110. }
  111. //==============================================================================
  112. void addPlatformSpecificSettingsForProjectType (const ProjectType&) override
  113. {
  114. // no-op.
  115. }
  116. private:
  117. //==============================================================================
  118. class CodeBlocksBuildConfiguration : public BuildConfiguration
  119. {
  120. public:
  121. CodeBlocksBuildConfiguration (Project& p, const ValueTree& settings, const ProjectExporter& e)
  122. : BuildConfiguration (p, settings, e)
  123. {
  124. }
  125. void createConfigProperties (PropertyListBuilder& props) override
  126. {
  127. addGCCOptimisationProperty (props);
  128. }
  129. var getDefaultOptimisationLevel() const override { return var ((int) (isDebug() ? gccO0 : gccO3)); }
  130. };
  131. BuildConfiguration::Ptr createBuildConfig (const ValueTree& tree) const override
  132. {
  133. return new CodeBlocksBuildConfiguration (project, tree, *this);
  134. }
  135. //==============================================================================
  136. void addVersion (XmlElement& xml) const
  137. {
  138. XmlElement* fileVersion = xml.createNewChildElement ("FileVersion");
  139. fileVersion->setAttribute ("major", 1);
  140. fileVersion->setAttribute ("minor", 6);
  141. }
  142. void addOptions (XmlElement& xml) const
  143. {
  144. xml.createNewChildElement ("Option")->setAttribute ("title", project.getTitle());
  145. xml.createNewChildElement ("Option")->setAttribute ("pch_mode", 2);
  146. xml.createNewChildElement ("Option")->setAttribute ("compiler", "gcc");
  147. }
  148. StringArray getDefines (const BuildConfiguration& config) const
  149. {
  150. StringPairArray defines;
  151. if (isCodeBlocks() && isWindows())
  152. {
  153. defines.set ("__MINGW__", "1");
  154. defines.set ("__MINGW_EXTENSION", String());
  155. }
  156. else
  157. {
  158. defines.set ("LINUX", "1");
  159. }
  160. if (config.isDebug())
  161. {
  162. defines.set ("DEBUG", "1");
  163. defines.set ("_DEBUG", "1");
  164. }
  165. else
  166. {
  167. defines.set ("NDEBUG", "1");
  168. }
  169. defines = mergePreprocessorDefs (defines, getAllPreprocessorDefs (config));
  170. StringArray defs;
  171. for (int i = 0; i < defines.size(); ++i)
  172. defs.add (defines.getAllKeys()[i] + "=" + defines.getAllValues()[i]);
  173. return getCleanedStringArray (defs);
  174. }
  175. StringArray getCompilerFlags (const BuildConfiguration& config) const
  176. {
  177. StringArray flags;
  178. flags.add ("-O" + config.getGCCOptimisationFlag());
  179. flags.add ("-std=c++11");
  180. flags.add ("-mstackrealign");
  181. if (config.isDebug())
  182. flags.add ("-g");
  183. flags.addTokens (replacePreprocessorTokens (config, getExtraCompilerFlagsString()).trim(),
  184. " \n", "\"'");
  185. {
  186. const StringArray defines (getDefines (config));
  187. for (int i = 0; i < defines.size(); ++i)
  188. {
  189. String def (defines[i]);
  190. if (! def.containsChar ('='))
  191. def << '=';
  192. flags.add ("-D" + def);
  193. }
  194. }
  195. return getCleanedStringArray (flags);
  196. }
  197. StringArray getLinkerFlags (const BuildConfiguration& config) const
  198. {
  199. StringArray flags (makefileExtraLinkerFlags);
  200. if (! config.isDebug())
  201. flags.add ("-s");
  202. flags.addTokens (replacePreprocessorTokens (config, getExtraLinkerFlagsString()).trim(),
  203. " \n", "\"'");
  204. return getCleanedStringArray (flags);
  205. }
  206. StringArray getIncludePaths (const BuildConfiguration& config) const
  207. {
  208. StringArray paths;
  209. paths.add (".");
  210. paths.add (RelativePath (project.getGeneratedCodeFolder(),
  211. getTargetFolder(), RelativePath::buildTargetFolder).toWindowsStyle());
  212. paths.addArray (config.getHeaderSearchPaths());
  213. if (! (isCodeBlocks() && isWindows()))
  214. paths.add ("/usr/include/freetype2");
  215. return getCleanedStringArray (paths);
  216. }
  217. static int getTypeIndex (const ProjectType& type)
  218. {
  219. if (type.isGUIApplication()) return 0;
  220. if (type.isCommandLineApp()) return 1;
  221. if (type.isStaticLibrary()) return 2;
  222. if (type.isDynamicLibrary()) return 3;
  223. if (type.isAudioPlugin()) return 3;
  224. return 0;
  225. }
  226. void createBuildTarget (XmlElement& xml, const BuildConfiguration& config) const
  227. {
  228. xml.setAttribute ("title", config.getName());
  229. {
  230. XmlElement* output = xml.createNewChildElement ("Option");
  231. String outputPath;
  232. if (config.getTargetBinaryRelativePathString().isNotEmpty())
  233. {
  234. RelativePath binaryPath (config.getTargetBinaryRelativePathString(), RelativePath::projectFolder);
  235. binaryPath = binaryPath.rebased (projectFolder, getTargetFolder(), RelativePath::buildTargetFolder);
  236. outputPath = config.getTargetBinaryRelativePathString();
  237. }
  238. else
  239. {
  240. outputPath ="bin/" + File::createLegalFileName (config.getName().trim());
  241. }
  242. output->setAttribute ("output", outputPath + "/" + replacePreprocessorTokens (config, config.getTargetBinaryNameString()));
  243. output->setAttribute ("prefix_auto", 1);
  244. output->setAttribute ("extension_auto", 1);
  245. }
  246. xml.createNewChildElement ("Option")
  247. ->setAttribute ("object_output", "obj/" + File::createLegalFileName (config.getName().trim()));
  248. xml.createNewChildElement ("Option")->setAttribute ("type", getTypeIndex (project.getProjectType()));
  249. xml.createNewChildElement ("Option")->setAttribute ("compiler", "gcc");
  250. {
  251. XmlElement* const compiler = xml.createNewChildElement ("Compiler");
  252. {
  253. const StringArray compilerFlags (getCompilerFlags (config));
  254. for (int i = 0; i < compilerFlags.size(); ++i)
  255. setAddOption (*compiler, "option", compilerFlags[i]);
  256. }
  257. {
  258. const StringArray includePaths (getIncludePaths (config));
  259. for (int i = 0; i < includePaths.size(); ++i)
  260. setAddOption (*compiler, "directory", includePaths[i]);
  261. }
  262. }
  263. {
  264. XmlElement* const linker = xml.createNewChildElement ("Linker");
  265. const StringArray linkerFlags (getLinkerFlags (config));
  266. for (int i = 0; i < linkerFlags.size(); ++i)
  267. setAddOption (*linker, "option", linkerFlags[i]);
  268. const StringArray& libs = isWindows() ? mingwLibs : linuxLibs;
  269. for (int i = 0; i < libs.size(); ++i)
  270. setAddOption (*linker, "library", libs[i]);
  271. const StringArray librarySearchPaths (config.getLibrarySearchPaths());
  272. for (int i = 0; i < librarySearchPaths.size(); ++i)
  273. setAddOption (*linker, "directory", replacePreprocessorDefs (getAllPreprocessorDefs(), librarySearchPaths[i]));
  274. }
  275. }
  276. void addBuild (XmlElement& xml) const
  277. {
  278. XmlElement* const build = xml.createNewChildElement ("Build");
  279. for (ConstConfigIterator config (*this); config.next();)
  280. createBuildTarget (*build->createNewChildElement ("Target"), *config);
  281. }
  282. void addProjectCompilerOptions (XmlElement& xml) const
  283. {
  284. XmlElement* const compiler = xml.createNewChildElement ("Compiler");
  285. setAddOption (*compiler, "option", "-Wall");
  286. setAddOption (*compiler, "option", "-Wno-strict-aliasing");
  287. setAddOption (*compiler, "option", "-Wno-strict-overflow");
  288. }
  289. void addProjectLinkerOptions (XmlElement& xml) const
  290. {
  291. XmlElement* const linker = xml.createNewChildElement ("Linker");
  292. StringArray libs;
  293. if (isWindows())
  294. {
  295. static const char* defaultLibs[] = { "gdi32", "user32", "kernel32", "comctl32" };
  296. libs = StringArray (defaultLibs, numElementsInArray (defaultLibs));
  297. }
  298. libs.addTokens (getExternalLibrariesString(), ";\n", "\"'");
  299. libs = getCleanedStringArray (libs);
  300. for (int i = 0; i < libs.size(); ++i)
  301. setAddOption (*linker, "library", replacePreprocessorDefs (getAllPreprocessorDefs(), libs[i]));
  302. }
  303. void addCompileUnits (const Project::Item& projectItem, XmlElement& xml) const
  304. {
  305. if (projectItem.isGroup())
  306. {
  307. for (int i = 0; i < projectItem.getNumChildren(); ++i)
  308. addCompileUnits (projectItem.getChild(i), xml);
  309. }
  310. else if (projectItem.shouldBeAddedToTargetProject())
  311. {
  312. const RelativePath file (projectItem.getFile(), getTargetFolder(), RelativePath::buildTargetFolder);
  313. XmlElement* unit = xml.createNewChildElement ("Unit");
  314. unit->setAttribute ("filename", file.toUnixStyle());
  315. if (! projectItem.shouldBeCompiled())
  316. {
  317. unit->createNewChildElement("Option")->setAttribute ("compile", 0);
  318. unit->createNewChildElement("Option")->setAttribute ("link", 0);
  319. }
  320. }
  321. }
  322. void addCompileUnits (XmlElement& xml) const
  323. {
  324. for (int i = 0; i < getAllGroups().size(); ++i)
  325. addCompileUnits (getAllGroups().getReference(i), xml);
  326. }
  327. void createProject (XmlElement& xml) const
  328. {
  329. addOptions (xml);
  330. addBuild (xml);
  331. addProjectCompilerOptions (xml);
  332. addProjectLinkerOptions (xml);
  333. addCompileUnits (xml);
  334. }
  335. void setAddOption (XmlElement& xml, const String& nm, const String& value) const
  336. {
  337. xml.createNewChildElement ("Add")->setAttribute (nm, value);
  338. }
  339. void initialiseDependencyPathValues()
  340. {
  341. DependencyPathOS pathOS = isLinux() ? TargetOS::linux
  342. : TargetOS::windows;
  343. vst3Path.referTo (Value (new DependencyPathValueSource (getSetting (Ids::vst3Folder), Ids::vst3Path, pathOS)));
  344. if (! isLinux())
  345. {
  346. aaxPath.referTo (Value (new DependencyPathValueSource (getSetting (Ids::aaxFolder), Ids::aaxPath, pathOS)));
  347. rtasPath.referTo (Value (new DependencyPathValueSource (getSetting (Ids::rtasFolder), Ids::rtasPath, pathOS)));
  348. }
  349. }
  350. CodeBlocksOS os;
  351. JUCE_DECLARE_NON_COPYABLE (CodeBlocksProjectExporter)
  352. };