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.

332 lines
12KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. class CodeBlocksProjectExporter : public ProjectExporter
  19. {
  20. public:
  21. //==============================================================================
  22. static const char* getNameCodeBlocks() { return "Code::Blocks project"; }
  23. static const char* getValueTreeTypeName() { return "CODEBLOCKS"; }
  24. static CodeBlocksProjectExporter* createForSettings (Project& project, const ValueTree& settings)
  25. {
  26. if (settings.hasType (getValueTreeTypeName()))
  27. return new CodeBlocksProjectExporter (project, settings);
  28. return nullptr;
  29. }
  30. //==============================================================================
  31. CodeBlocksProjectExporter (Project& p, const ValueTree& t) : ProjectExporter (p, t)
  32. {
  33. name = getNameCodeBlocks();
  34. if (getTargetLocationString().isEmpty())
  35. getTargetLocationValue() = getDefaultBuildsRootFolder() + "CodeBlocks";
  36. }
  37. //==============================================================================
  38. bool launchProject() { return false; }
  39. bool isCodeBlocks() const { return true; }
  40. bool isWindows() const { return true; }
  41. bool usesMMFiles() const { return false; }
  42. bool canCopeWithDuplicateFiles() { return false; }
  43. void createExporterProperties (PropertyListBuilder&)
  44. {
  45. }
  46. //==============================================================================
  47. void create (const OwnedArray<LibraryModule>&) const
  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
  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. flags.addTokens (replacePreprocessorTokens (config, getExtraCompilerFlagsString()).trim(),
  112. " \n", "\"'");
  113. {
  114. const StringArray defines (getDefines (config));
  115. for (int i = 0; i < defines.size(); ++i)
  116. {
  117. String def (defines[i]);
  118. if (! def.containsChar ('='))
  119. def << '=';
  120. flags.add ("-D" + def);
  121. }
  122. }
  123. return cleanArray (flags);
  124. }
  125. StringArray getLinkerFlags (const BuildConfiguration& config) const
  126. {
  127. StringArray flags;
  128. if (! config.isDebug())
  129. flags.add ("-s");
  130. flags.addTokens (replacePreprocessorTokens (config, getExtraLinkerFlagsString()).trim(),
  131. " \n", "\"'");
  132. return cleanArray (flags);
  133. }
  134. StringArray getIncludePaths (const BuildConfiguration& config) const
  135. {
  136. StringArray paths;
  137. paths.add (".");
  138. paths.add (RelativePath (project.getGeneratedCodeFolder(),
  139. getTargetFolder(), RelativePath::buildTargetFolder).toWindowsStyle());
  140. paths.addArray (config.getHeaderSearchPaths());
  141. return cleanArray (paths);
  142. }
  143. static int getTypeIndex (const ProjectType& type)
  144. {
  145. if (type.isGUIApplication()) return 0;
  146. if (type.isCommandLineApp()) return 1;
  147. if (type.isStaticLibrary()) return 2;
  148. if (type.isDynamicLibrary()) return 3;
  149. return 0;
  150. }
  151. void createBuildTarget (XmlElement& xml, const BuildConfiguration& config) const
  152. {
  153. xml.setAttribute ("title", config.getName());
  154. {
  155. XmlElement* output = xml.createNewChildElement ("Option");
  156. String outputPath;
  157. if (config.getTargetBinaryRelativePathString().isNotEmpty())
  158. {
  159. RelativePath binaryPath (config.getTargetBinaryRelativePathString(), RelativePath::projectFolder);
  160. binaryPath = binaryPath.rebased (projectFolder, getTargetFolder(), RelativePath::buildTargetFolder);
  161. outputPath = config.getTargetBinaryRelativePathString();
  162. }
  163. else
  164. {
  165. outputPath ="bin/" + File::createLegalFileName (config.getName().trim());
  166. }
  167. output->setAttribute ("output", outputPath + "/" + config.getTargetBinaryNameString());
  168. output->setAttribute ("prefix_auto", 1);
  169. output->setAttribute ("extension_auto", 1);
  170. }
  171. xml.createNewChildElement ("Option")
  172. ->setAttribute ("object_output", "obj/" + File::createLegalFileName (config.getName().trim()));
  173. xml.createNewChildElement ("Option")->setAttribute ("type", getTypeIndex (project.getProjectType()));
  174. xml.createNewChildElement ("Option")->setAttribute ("compiler", "gcc");
  175. {
  176. XmlElement* const compiler = xml.createNewChildElement ("Compiler");
  177. {
  178. const StringArray compilerFlags (getCompilerFlags (config));
  179. for (int i = 0; i < compilerFlags.size(); ++i)
  180. setAddOption (*compiler, "option", compilerFlags[i]);
  181. }
  182. {
  183. const StringArray includePaths (getIncludePaths (config));
  184. for (int i = 0; i < includePaths.size(); ++i)
  185. setAddOption (*compiler, "directory", includePaths[i]);
  186. }
  187. }
  188. {
  189. XmlElement* const linker = xml.createNewChildElement ("Linker");
  190. const StringArray linkerFlags (getLinkerFlags (config));
  191. for (int i = 0; i < linkerFlags.size(); ++i)
  192. setAddOption (*linker, "option", linkerFlags[i]);
  193. for (int i = 0; i < mingwLibs.size(); ++i)
  194. setAddOption (*linker, "library", mingwLibs[i]);
  195. const StringArray librarySearchPaths (config.getLibrarySearchPaths());
  196. for (int i = 0; i < librarySearchPaths.size(); ++i)
  197. setAddOption (*linker, "directory", replacePreprocessorDefs (getAllPreprocessorDefs(), librarySearchPaths[i]));
  198. }
  199. }
  200. void addBuild (XmlElement& xml) const
  201. {
  202. XmlElement* const build = xml.createNewChildElement ("Build");
  203. for (ConstConfigIterator config (*this); config.next();)
  204. createBuildTarget (*build->createNewChildElement ("Target"), *config);
  205. }
  206. void addProjectCompilerOptions (XmlElement& xml) const
  207. {
  208. XmlElement* const compiler = xml.createNewChildElement ("Compiler");
  209. setAddOption (*compiler, "option", "-Wall");
  210. setAddOption (*compiler, "option", "-Wno-strict-aliasing");
  211. setAddOption (*compiler, "option", "-Wno-strict-overflow");
  212. }
  213. void addProjectLinkerOptions (XmlElement& xml) const
  214. {
  215. XmlElement* const linker = xml.createNewChildElement ("Linker");
  216. const char* defaultLibs[] = { "gdi32", "user32", "kernel32", "comctl32" };
  217. StringArray libs (defaultLibs, numElementsInArray (defaultLibs));
  218. libs.addTokens (getExternalLibrariesString(), ";\n", "\"'");
  219. libs = cleanArray (libs);
  220. for (int i = 0; i < libs.size(); ++i)
  221. setAddOption (*linker, "library", replacePreprocessorDefs (getAllPreprocessorDefs(), libs[i]));
  222. }
  223. void addCompileUnits (const Project::Item& projectItem, XmlElement& xml) const
  224. {
  225. if (projectItem.isGroup())
  226. {
  227. for (int i = 0; i < projectItem.getNumChildren(); ++i)
  228. addCompileUnits (projectItem.getChild(i), xml);
  229. }
  230. else if (projectItem.shouldBeAddedToTargetProject())
  231. {
  232. const RelativePath file (projectItem.getFile(), getTargetFolder(), RelativePath::buildTargetFolder);
  233. XmlElement* unit = xml.createNewChildElement ("Unit");
  234. unit->setAttribute ("filename", file.toUnixStyle());
  235. if (! projectItem.shouldBeCompiled())
  236. {
  237. unit->createNewChildElement("Option")->setAttribute ("compile", 0);
  238. unit->createNewChildElement("Option")->setAttribute ("link", 0);
  239. }
  240. }
  241. }
  242. void addCompileUnits (XmlElement& xml) const
  243. {
  244. for (int i = 0; i < getAllGroups().size(); ++i)
  245. addCompileUnits (getAllGroups().getReference(i), xml);
  246. }
  247. void createProject (XmlElement& xml) const
  248. {
  249. addOptions (xml);
  250. addBuild (xml);
  251. addProjectCompilerOptions (xml);
  252. addProjectLinkerOptions (xml);
  253. addCompileUnits (xml);
  254. }
  255. void setAddOption (XmlElement& xml, const String& name, const String& value) const
  256. {
  257. xml.createNewChildElement ("Add")->setAttribute (name, value);
  258. }
  259. JUCE_DECLARE_NON_COPYABLE (CodeBlocksProjectExporter)
  260. };