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.

415 lines
15KB

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