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.

425 lines
15KB

  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 isCodeBlocksWindows() const override { return os == windowsTarget; }
  78. bool isCodeBlocksLinux() const override { return isLinux(); }
  79. bool isLinux() const override { return os == linuxTarget; }
  80. bool usesMMFiles() const override { return false; }
  81. bool canCopeWithDuplicateFiles() override { return false; }
  82. void createExporterProperties (PropertyListBuilder&) override
  83. {
  84. }
  85. //==============================================================================
  86. void create (const OwnedArray<LibraryModule>&) const override
  87. {
  88. const File cbpFile (getTargetFolder().getChildFile (project.getProjectFilenameRoot())
  89. .withFileExtension (".cbp"));
  90. XmlElement xml ("CodeBlocks_project_file");
  91. addVersion (xml);
  92. createProject (*xml.createNewChildElement ("Project"));
  93. writeXmlOrThrow (xml, cbpFile, "UTF-8", 10);
  94. }
  95. private:
  96. //==============================================================================
  97. class CodeBlocksBuildConfiguration : public BuildConfiguration
  98. {
  99. public:
  100. CodeBlocksBuildConfiguration (Project& p, const ValueTree& settings)
  101. : BuildConfiguration (p, settings)
  102. {
  103. }
  104. void createConfigProperties (PropertyListBuilder& props) override
  105. {
  106. addGCCOptimisationProperty (props);
  107. }
  108. var getDefaultOptimisationLevel() const override { return var ((int) (isDebug() ? gccO0 : gccO3)); }
  109. };
  110. BuildConfiguration::Ptr createBuildConfig (const ValueTree& tree) const override
  111. {
  112. return new CodeBlocksBuildConfiguration (project, tree);
  113. }
  114. //==============================================================================
  115. void addVersion (XmlElement& xml) const
  116. {
  117. XmlElement* fileVersion = xml.createNewChildElement ("FileVersion");
  118. fileVersion->setAttribute ("major", 1);
  119. fileVersion->setAttribute ("minor", 6);
  120. }
  121. void addOptions (XmlElement& xml) const
  122. {
  123. xml.createNewChildElement ("Option")->setAttribute ("title", project.getTitle());
  124. xml.createNewChildElement ("Option")->setAttribute ("pch_mode", 2);
  125. xml.createNewChildElement ("Option")->setAttribute ("compiler", "gcc");
  126. }
  127. StringArray getDefines (const BuildConfiguration& config) const
  128. {
  129. StringPairArray defines;
  130. if (isCodeBlocksWindows())
  131. {
  132. defines.set ("__MINGW__", "1");
  133. defines.set ("__MINGW_EXTENSION", String::empty);
  134. }
  135. else
  136. {
  137. defines.set ("LINUX", "1");
  138. }
  139. if (config.isDebug())
  140. {
  141. defines.set ("DEBUG", "1");
  142. defines.set ("_DEBUG", "1");
  143. }
  144. else
  145. {
  146. defines.set ("NDEBUG", "1");
  147. }
  148. defines = mergePreprocessorDefs (defines, getAllPreprocessorDefs (config));
  149. StringArray defs;
  150. for (int i = 0; i < defines.size(); ++i)
  151. defs.add (defines.getAllKeys()[i] + "=" + defines.getAllValues()[i]);
  152. return getCleanedStringArray (defs);
  153. }
  154. StringArray getCompilerFlags (const BuildConfiguration& config) const
  155. {
  156. StringArray flags;
  157. flags.add ("-O" + config.getGCCOptimisationFlag());
  158. flags.add ("-std=c++11");
  159. flags.add ("-mstackrealign");
  160. if (config.isDebug())
  161. flags.add ("-g");
  162. flags.addTokens (replacePreprocessorTokens (config, getExtraCompilerFlagsString()).trim(),
  163. " \n", "\"'");
  164. {
  165. const StringArray defines (getDefines (config));
  166. for (int i = 0; i < defines.size(); ++i)
  167. {
  168. String def (defines[i]);
  169. if (! def.containsChar ('='))
  170. def << '=';
  171. flags.add ("-D" + def);
  172. }
  173. }
  174. return getCleanedStringArray (flags);
  175. }
  176. StringArray getLinkerFlags (const BuildConfiguration& config) const
  177. {
  178. StringArray flags (makefileExtraLinkerFlags);
  179. if (! config.isDebug())
  180. flags.add ("-s");
  181. flags.addTokens (replacePreprocessorTokens (config, getExtraLinkerFlagsString()).trim(),
  182. " \n", "\"'");
  183. return getCleanedStringArray (flags);
  184. }
  185. StringArray getIncludePaths (const BuildConfiguration& config) const
  186. {
  187. StringArray paths;
  188. paths.add (".");
  189. paths.add (RelativePath (project.getGeneratedCodeFolder(),
  190. getTargetFolder(), RelativePath::buildTargetFolder).toWindowsStyle());
  191. paths.addArray (config.getHeaderSearchPaths());
  192. if (! isCodeBlocksWindows())
  193. paths.add ("/usr/include/freetype2");
  194. return getCleanedStringArray (paths);
  195. }
  196. static int getTypeIndex (const ProjectType& type)
  197. {
  198. if (type.isGUIApplication()) return 0;
  199. if (type.isCommandLineApp()) return 1;
  200. if (type.isStaticLibrary()) return 2;
  201. if (type.isDynamicLibrary()) return 3;
  202. if (type.isAudioPlugin()) return 3;
  203. return 0;
  204. }
  205. void createBuildTarget (XmlElement& xml, const BuildConfiguration& config) const
  206. {
  207. xml.setAttribute ("title", config.getName());
  208. {
  209. XmlElement* output = xml.createNewChildElement ("Option");
  210. String outputPath;
  211. if (config.getTargetBinaryRelativePathString().isNotEmpty())
  212. {
  213. RelativePath binaryPath (config.getTargetBinaryRelativePathString(), RelativePath::projectFolder);
  214. binaryPath = binaryPath.rebased (projectFolder, getTargetFolder(), RelativePath::buildTargetFolder);
  215. outputPath = config.getTargetBinaryRelativePathString();
  216. }
  217. else
  218. {
  219. outputPath ="bin/" + File::createLegalFileName (config.getName().trim());
  220. }
  221. output->setAttribute ("output", outputPath + "/" + replacePreprocessorTokens (config, config.getTargetBinaryNameString()));
  222. output->setAttribute ("prefix_auto", 1);
  223. output->setAttribute ("extension_auto", 1);
  224. }
  225. xml.createNewChildElement ("Option")
  226. ->setAttribute ("object_output", "obj/" + File::createLegalFileName (config.getName().trim()));
  227. xml.createNewChildElement ("Option")->setAttribute ("type", getTypeIndex (project.getProjectType()));
  228. xml.createNewChildElement ("Option")->setAttribute ("compiler", "gcc");
  229. {
  230. XmlElement* const compiler = xml.createNewChildElement ("Compiler");
  231. {
  232. const StringArray compilerFlags (getCompilerFlags (config));
  233. for (int i = 0; i < compilerFlags.size(); ++i)
  234. setAddOption (*compiler, "option", compilerFlags[i]);
  235. }
  236. {
  237. const StringArray includePaths (getIncludePaths (config));
  238. for (int i = 0; i < includePaths.size(); ++i)
  239. setAddOption (*compiler, "directory", includePaths[i]);
  240. }
  241. }
  242. {
  243. XmlElement* const linker = xml.createNewChildElement ("Linker");
  244. const StringArray linkerFlags (getLinkerFlags (config));
  245. for (int i = 0; i < linkerFlags.size(); ++i)
  246. setAddOption (*linker, "option", linkerFlags[i]);
  247. const StringArray& libs = isCodeBlocksWindows() ? mingwLibs : linuxLibs;
  248. for (int i = 0; i < libs.size(); ++i)
  249. setAddOption (*linker, "library", libs[i]);
  250. const StringArray librarySearchPaths (config.getLibrarySearchPaths());
  251. for (int i = 0; i < librarySearchPaths.size(); ++i)
  252. setAddOption (*linker, "directory", replacePreprocessorDefs (getAllPreprocessorDefs(), librarySearchPaths[i]));
  253. }
  254. }
  255. void addBuild (XmlElement& xml) const
  256. {
  257. XmlElement* const build = xml.createNewChildElement ("Build");
  258. for (ConstConfigIterator config (*this); config.next();)
  259. createBuildTarget (*build->createNewChildElement ("Target"), *config);
  260. }
  261. void addProjectCompilerOptions (XmlElement& xml) const
  262. {
  263. XmlElement* const compiler = xml.createNewChildElement ("Compiler");
  264. setAddOption (*compiler, "option", "-Wall");
  265. setAddOption (*compiler, "option", "-Wno-strict-aliasing");
  266. setAddOption (*compiler, "option", "-Wno-strict-overflow");
  267. }
  268. void addProjectLinkerOptions (XmlElement& xml) const
  269. {
  270. XmlElement* const linker = xml.createNewChildElement ("Linker");
  271. StringArray libs;
  272. if (isCodeBlocksWindows())
  273. {
  274. static const char* defaultLibs[] = { "gdi32", "user32", "kernel32", "comctl32" };
  275. libs = StringArray (defaultLibs, numElementsInArray (defaultLibs));
  276. }
  277. libs.addTokens (getExternalLibrariesString(), ";\n", "\"'");
  278. libs = getCleanedStringArray (libs);
  279. for (int i = 0; i < libs.size(); ++i)
  280. setAddOption (*linker, "library", replacePreprocessorDefs (getAllPreprocessorDefs(), libs[i]));
  281. }
  282. void addCompileUnits (const Project::Item& projectItem, XmlElement& xml) const
  283. {
  284. if (projectItem.isGroup())
  285. {
  286. for (int i = 0; i < projectItem.getNumChildren(); ++i)
  287. addCompileUnits (projectItem.getChild(i), xml);
  288. }
  289. else if (projectItem.shouldBeAddedToTargetProject())
  290. {
  291. const RelativePath file (projectItem.getFile(), getTargetFolder(), RelativePath::buildTargetFolder);
  292. XmlElement* unit = xml.createNewChildElement ("Unit");
  293. unit->setAttribute ("filename", file.toUnixStyle());
  294. if (! projectItem.shouldBeCompiled())
  295. {
  296. unit->createNewChildElement("Option")->setAttribute ("compile", 0);
  297. unit->createNewChildElement("Option")->setAttribute ("link", 0);
  298. }
  299. }
  300. }
  301. void addCompileUnits (XmlElement& xml) const
  302. {
  303. for (int i = 0; i < getAllGroups().size(); ++i)
  304. addCompileUnits (getAllGroups().getReference(i), xml);
  305. }
  306. void createProject (XmlElement& xml) const
  307. {
  308. addOptions (xml);
  309. addBuild (xml);
  310. addProjectCompilerOptions (xml);
  311. addProjectLinkerOptions (xml);
  312. addCompileUnits (xml);
  313. }
  314. void setAddOption (XmlElement& xml, const String& nm, const String& value) const
  315. {
  316. xml.createNewChildElement ("Add")->setAttribute (nm, value);
  317. }
  318. void initialiseDependencyPathValues()
  319. {
  320. DependencyPathOS pathOS = isLinux() ? TargetOS::linux
  321. : TargetOS::windows;
  322. vst2Path.referTo (Value (new DependencyPathValueSource (getSetting (Ids::vstFolder), Ids::vst2Path, pathOS)));
  323. vst3Path.referTo (Value (new DependencyPathValueSource (getSetting (Ids::vst3Folder), Ids::vst3Path, pathOS)));
  324. if (! isLinux())
  325. {
  326. aaxPath.referTo (Value (new DependencyPathValueSource (getSetting (Ids::aaxFolder), Ids::aaxPath, pathOS)));
  327. rtasPath.referTo (Value (new DependencyPathValueSource (getSetting (Ids::rtasFolder), Ids::rtasPath, pathOS)));
  328. }
  329. }
  330. CodeBlocksOS os;
  331. JUCE_DECLARE_NON_COPYABLE (CodeBlocksProjectExporter)
  332. };