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.

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