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.

335 lines
12KB

  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. //==============================================================================
  21. static const char* getNameCodeBlocks() { return "Code::Blocks project"; }
  22. static const char* getValueTreeTypeName() { return "CODEBLOCKS"; }
  23. static CodeBlocksProjectExporter* createForSettings (Project& project, const ValueTree& settings)
  24. {
  25. if (settings.hasType (getValueTreeTypeName()))
  26. return new CodeBlocksProjectExporter (project, settings);
  27. return nullptr;
  28. }
  29. //==============================================================================
  30. CodeBlocksProjectExporter (Project& p, const ValueTree& t) : ProjectExporter (p, t)
  31. {
  32. name = getNameCodeBlocks();
  33. if (getTargetLocationString().isEmpty())
  34. getTargetLocationValue() = getDefaultBuildsRootFolder() + "CodeBlocks";
  35. }
  36. //==============================================================================
  37. bool canLaunchProject() override { return false; }
  38. bool launchProject() override { return false; }
  39. bool isCodeBlocks() const override { return true; }
  40. bool isWindows() const override { return true; }
  41. bool usesMMFiles() const override { return false; }
  42. bool canCopeWithDuplicateFiles() override { return false; }
  43. void createExporterProperties (PropertyListBuilder&) override
  44. {
  45. }
  46. //==============================================================================
  47. void create (const OwnedArray<LibraryModule>&) const override
  48. {
  49. const File cbpFile (getTargetFolder().getChildFile (project.getProjectFilenameRoot())
  50. .withFileExtension (".cbp"));
  51. XmlElement xml ("CodeBlocks_project_file");
  52. addVersion (xml);
  53. createProject (*xml.createNewChildElement ("Project"));
  54. writeXmlOrThrow (xml, cbpFile, "UTF-8", 10);
  55. }
  56. private:
  57. //==============================================================================
  58. class CodeBlocksBuildConfiguration : public BuildConfiguration
  59. {
  60. public:
  61. CodeBlocksBuildConfiguration (Project& p, const ValueTree& settings)
  62. : BuildConfiguration (p, settings)
  63. {
  64. }
  65. void createConfigProperties (PropertyListBuilder&)
  66. {
  67. }
  68. };
  69. BuildConfiguration::Ptr createBuildConfig (const ValueTree& tree) const override
  70. {
  71. return new CodeBlocksBuildConfiguration (project, tree);
  72. }
  73. //==============================================================================
  74. void addVersion (XmlElement& xml) const
  75. {
  76. XmlElement* fileVersion = xml.createNewChildElement ("FileVersion");
  77. fileVersion->setAttribute ("major", 1);
  78. fileVersion->setAttribute ("minor", 6);
  79. }
  80. void addOptions (XmlElement& xml) const
  81. {
  82. xml.createNewChildElement ("Option")->setAttribute ("title", project.getTitle());
  83. xml.createNewChildElement ("Option")->setAttribute ("pch_mode", 2);
  84. xml.createNewChildElement ("Option")->setAttribute ("compiler", "gcc");
  85. }
  86. static StringArray cleanArray (StringArray s)
  87. {
  88. s.trim();
  89. s.removeDuplicates (false);
  90. s.removeEmptyStrings (true);
  91. return s;
  92. }
  93. StringArray getDefines (const BuildConfiguration& config) const
  94. {
  95. StringPairArray defines;
  96. defines.set ("__MINGW__", "1");
  97. defines.set ("__MINGW_EXTENSION", String::empty);
  98. defines = mergePreprocessorDefs (defines, getAllPreprocessorDefs (config));
  99. StringArray defs;
  100. for (int i = 0; i < defines.size(); ++i)
  101. defs.add (defines.getAllKeys()[i] + "=" + defines.getAllValues()[i]);
  102. return cleanArray (defs);
  103. }
  104. StringArray getCompilerFlags (const BuildConfiguration& config) const
  105. {
  106. StringArray flags;
  107. flags.add ("-O" + config.getGCCOptimisationFlag());
  108. flags.add ("-std=gnu++0x");
  109. flags.add ("-march=pentium4");
  110. flags.add ("-mstackrealign");
  111. if (config.isDebug())
  112. flags.add ("-g");
  113. flags.addTokens (replacePreprocessorTokens (config, getExtraCompilerFlagsString()).trim(),
  114. " \n", "\"'");
  115. {
  116. const StringArray defines (getDefines (config));
  117. for (int i = 0; i < defines.size(); ++i)
  118. {
  119. String def (defines[i]);
  120. if (! def.containsChar ('='))
  121. def << '=';
  122. flags.add ("-D" + def);
  123. }
  124. }
  125. return cleanArray (flags);
  126. }
  127. StringArray getLinkerFlags (const BuildConfiguration& config) const
  128. {
  129. StringArray flags;
  130. if (! config.isDebug())
  131. flags.add ("-s");
  132. flags.addTokens (replacePreprocessorTokens (config, getExtraLinkerFlagsString()).trim(),
  133. " \n", "\"'");
  134. return cleanArray (flags);
  135. }
  136. StringArray getIncludePaths (const BuildConfiguration& config) const
  137. {
  138. StringArray paths;
  139. paths.add (".");
  140. paths.add (RelativePath (project.getGeneratedCodeFolder(),
  141. getTargetFolder(), RelativePath::buildTargetFolder).toWindowsStyle());
  142. paths.addArray (config.getHeaderSearchPaths());
  143. return cleanArray (paths);
  144. }
  145. static int getTypeIndex (const ProjectType& type)
  146. {
  147. if (type.isGUIApplication()) return 0;
  148. if (type.isCommandLineApp()) return 1;
  149. if (type.isStaticLibrary()) return 2;
  150. if (type.isDynamicLibrary()) return 3;
  151. if (type.isAudioPlugin()) return 3;
  152. return 0;
  153. }
  154. void createBuildTarget (XmlElement& xml, const BuildConfiguration& config) const
  155. {
  156. xml.setAttribute ("title", config.getName());
  157. {
  158. XmlElement* output = xml.createNewChildElement ("Option");
  159. String outputPath;
  160. if (config.getTargetBinaryRelativePathString().isNotEmpty())
  161. {
  162. RelativePath binaryPath (config.getTargetBinaryRelativePathString(), RelativePath::projectFolder);
  163. binaryPath = binaryPath.rebased (projectFolder, getTargetFolder(), RelativePath::buildTargetFolder);
  164. outputPath = config.getTargetBinaryRelativePathString();
  165. }
  166. else
  167. {
  168. outputPath ="bin/" + File::createLegalFileName (config.getName().trim());
  169. }
  170. output->setAttribute ("output", outputPath + "/" + config.getTargetBinaryNameString());
  171. output->setAttribute ("prefix_auto", 1);
  172. output->setAttribute ("extension_auto", 1);
  173. }
  174. xml.createNewChildElement ("Option")
  175. ->setAttribute ("object_output", "obj/" + File::createLegalFileName (config.getName().trim()));
  176. xml.createNewChildElement ("Option")->setAttribute ("type", getTypeIndex (project.getProjectType()));
  177. xml.createNewChildElement ("Option")->setAttribute ("compiler", "gcc");
  178. {
  179. XmlElement* const compiler = xml.createNewChildElement ("Compiler");
  180. {
  181. const StringArray compilerFlags (getCompilerFlags (config));
  182. for (int i = 0; i < compilerFlags.size(); ++i)
  183. setAddOption (*compiler, "option", compilerFlags[i]);
  184. }
  185. {
  186. const StringArray includePaths (getIncludePaths (config));
  187. for (int i = 0; i < includePaths.size(); ++i)
  188. setAddOption (*compiler, "directory", includePaths[i]);
  189. }
  190. }
  191. {
  192. XmlElement* const linker = xml.createNewChildElement ("Linker");
  193. const StringArray linkerFlags (getLinkerFlags (config));
  194. for (int i = 0; i < linkerFlags.size(); ++i)
  195. setAddOption (*linker, "option", linkerFlags[i]);
  196. for (int i = 0; i < mingwLibs.size(); ++i)
  197. setAddOption (*linker, "library", mingwLibs[i]);
  198. const StringArray librarySearchPaths (config.getLibrarySearchPaths());
  199. for (int i = 0; i < librarySearchPaths.size(); ++i)
  200. setAddOption (*linker, "directory", replacePreprocessorDefs (getAllPreprocessorDefs(), librarySearchPaths[i]));
  201. }
  202. }
  203. void addBuild (XmlElement& xml) const
  204. {
  205. XmlElement* const build = xml.createNewChildElement ("Build");
  206. for (ConstConfigIterator config (*this); config.next();)
  207. createBuildTarget (*build->createNewChildElement ("Target"), *config);
  208. }
  209. void addProjectCompilerOptions (XmlElement& xml) const
  210. {
  211. XmlElement* const compiler = xml.createNewChildElement ("Compiler");
  212. setAddOption (*compiler, "option", "-Wall");
  213. setAddOption (*compiler, "option", "-Wno-strict-aliasing");
  214. setAddOption (*compiler, "option", "-Wno-strict-overflow");
  215. }
  216. void addProjectLinkerOptions (XmlElement& xml) const
  217. {
  218. XmlElement* const linker = xml.createNewChildElement ("Linker");
  219. const char* defaultLibs[] = { "gdi32", "user32", "kernel32", "comctl32" };
  220. StringArray libs (defaultLibs, numElementsInArray (defaultLibs));
  221. libs.addTokens (getExternalLibrariesString(), ";\n", "\"'");
  222. libs = cleanArray (libs);
  223. for (int i = 0; i < libs.size(); ++i)
  224. setAddOption (*linker, "library", replacePreprocessorDefs (getAllPreprocessorDefs(), libs[i]));
  225. }
  226. void addCompileUnits (const Project::Item& projectItem, XmlElement& xml) const
  227. {
  228. if (projectItem.isGroup())
  229. {
  230. for (int i = 0; i < projectItem.getNumChildren(); ++i)
  231. addCompileUnits (projectItem.getChild(i), xml);
  232. }
  233. else if (projectItem.shouldBeAddedToTargetProject())
  234. {
  235. const RelativePath file (projectItem.getFile(), getTargetFolder(), RelativePath::buildTargetFolder);
  236. XmlElement* unit = xml.createNewChildElement ("Unit");
  237. unit->setAttribute ("filename", file.toUnixStyle());
  238. if (! projectItem.shouldBeCompiled())
  239. {
  240. unit->createNewChildElement("Option")->setAttribute ("compile", 0);
  241. unit->createNewChildElement("Option")->setAttribute ("link", 0);
  242. }
  243. }
  244. }
  245. void addCompileUnits (XmlElement& xml) const
  246. {
  247. for (int i = 0; i < getAllGroups().size(); ++i)
  248. addCompileUnits (getAllGroups().getReference(i), xml);
  249. }
  250. void createProject (XmlElement& xml) const
  251. {
  252. addOptions (xml);
  253. addBuild (xml);
  254. addProjectCompilerOptions (xml);
  255. addProjectLinkerOptions (xml);
  256. addCompileUnits (xml);
  257. }
  258. void setAddOption (XmlElement& xml, const String& name, const String& value) const
  259. {
  260. xml.createNewChildElement ("Add")->setAttribute (name, value);
  261. }
  262. JUCE_DECLARE_NON_COPYABLE (CodeBlocksProjectExporter)
  263. };