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.

331 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.addTokens (replacePreprocessorTokens (config, getExtraCompilerFlagsString()).trim(),
  111. " \n", "\"'");
  112. {
  113. const StringArray defines (getDefines (config));
  114. for (int i = 0; i < defines.size(); ++i)
  115. {
  116. String def (defines[i]);
  117. if (! def.containsChar ('='))
  118. def << '=';
  119. flags.add ("-D" + def);
  120. }
  121. }
  122. return cleanArray (flags);
  123. }
  124. StringArray getLinkerFlags (const BuildConfiguration& config) const
  125. {
  126. StringArray flags;
  127. if (! config.isDebug())
  128. flags.add ("-s");
  129. flags.addTokens (replacePreprocessorTokens (config, getExtraLinkerFlagsString()).trim(),
  130. " \n", "\"'");
  131. return cleanArray (flags);
  132. }
  133. StringArray getIncludePaths (const BuildConfiguration& config) const
  134. {
  135. StringArray paths;
  136. paths.add (".");
  137. paths.add (RelativePath (project.getGeneratedCodeFolder(),
  138. getTargetFolder(), RelativePath::buildTargetFolder).toWindowsStyle());
  139. paths.addArray (config.getHeaderSearchPaths());
  140. return cleanArray (paths);
  141. }
  142. static int getTypeIndex (const ProjectType& type)
  143. {
  144. if (type.isGUIApplication()) return 0;
  145. if (type.isCommandLineApp()) return 1;
  146. if (type.isStaticLibrary()) return 2;
  147. if (type.isDynamicLibrary()) return 3;
  148. return 0;
  149. }
  150. void createBuildTarget (XmlElement& xml, const BuildConfiguration& config) const
  151. {
  152. xml.setAttribute ("title", config.getName());
  153. {
  154. XmlElement* output = xml.createNewChildElement ("Option");
  155. String outputPath;
  156. if (config.getTargetBinaryRelativePathString().isNotEmpty())
  157. {
  158. RelativePath binaryPath (config.getTargetBinaryRelativePathString(), RelativePath::projectFolder);
  159. binaryPath = binaryPath.rebased (projectFolder, getTargetFolder(), RelativePath::buildTargetFolder);
  160. outputPath = config.getTargetBinaryRelativePathString();
  161. }
  162. else
  163. {
  164. outputPath ="bin/" + File::createLegalFileName (config.getName().trim());
  165. }
  166. output->setAttribute ("output", outputPath + "/" + config.getTargetBinaryNameString());
  167. output->setAttribute ("prefix_auto", 1);
  168. output->setAttribute ("extension_auto", 1);
  169. }
  170. xml.createNewChildElement ("Option")
  171. ->setAttribute ("object_output", "obj/" + File::createLegalFileName (config.getName().trim()));
  172. xml.createNewChildElement ("Option")->setAttribute ("type", getTypeIndex (project.getProjectType()));
  173. xml.createNewChildElement ("Option")->setAttribute ("compiler", "gcc");
  174. {
  175. XmlElement* const compiler = xml.createNewChildElement ("Compiler");
  176. {
  177. const StringArray compilerFlags (getCompilerFlags (config));
  178. for (int i = 0; i < compilerFlags.size(); ++i)
  179. setAddOption (*compiler, "option", compilerFlags[i]);
  180. }
  181. {
  182. const StringArray includePaths (getIncludePaths (config));
  183. for (int i = 0; i < includePaths.size(); ++i)
  184. setAddOption (*compiler, "directory", includePaths[i]);
  185. }
  186. }
  187. {
  188. XmlElement* const linker = xml.createNewChildElement ("Linker");
  189. const StringArray linkerFlags (getLinkerFlags (config));
  190. for (int i = 0; i < linkerFlags.size(); ++i)
  191. setAddOption (*linker, "option", linkerFlags[i]);
  192. for (int i = 0; i < mingwLibs.size(); ++i)
  193. setAddOption (*linker, "library", mingwLibs[i]);
  194. const StringArray librarySearchPaths (config.getLibrarySearchPaths());
  195. for (int i = 0; i < librarySearchPaths.size(); ++i)
  196. setAddOption (*linker, "directory", replacePreprocessorDefs (getAllPreprocessorDefs(), librarySearchPaths[i]));
  197. }
  198. }
  199. void addBuild (XmlElement& xml) const
  200. {
  201. XmlElement* const build = xml.createNewChildElement ("Build");
  202. for (ConstConfigIterator config (*this); config.next();)
  203. createBuildTarget (*build->createNewChildElement ("Target"), *config);
  204. }
  205. void addProjectCompilerOptions (XmlElement& xml) const
  206. {
  207. XmlElement* const compiler = xml.createNewChildElement ("Compiler");
  208. setAddOption (*compiler, "option", "-Wall");
  209. setAddOption (*compiler, "option", "-Wno-strict-aliasing");
  210. setAddOption (*compiler, "option", "-Wno-strict-overflow");
  211. }
  212. void addProjectLinkerOptions (XmlElement& xml) const
  213. {
  214. XmlElement* const linker = xml.createNewChildElement ("Linker");
  215. const char* defaultLibs[] = { "gdi32", "user32", "kernel32", "comctl32" };
  216. StringArray libs (defaultLibs, numElementsInArray (defaultLibs));
  217. libs.addTokens (getExternalLibrariesString(), ";\n", "\"'");
  218. libs = cleanArray (libs);
  219. for (int i = 0; i < libs.size(); ++i)
  220. setAddOption (*linker, "library", replacePreprocessorDefs (getAllPreprocessorDefs(), libs[i]));
  221. }
  222. void addCompileUnits (const Project::Item& projectItem, XmlElement& xml) const
  223. {
  224. if (projectItem.isGroup())
  225. {
  226. for (int i = 0; i < projectItem.getNumChildren(); ++i)
  227. addCompileUnits (projectItem.getChild(i), xml);
  228. }
  229. else if (projectItem.shouldBeAddedToTargetProject())
  230. {
  231. const RelativePath file (projectItem.getFile(), getTargetFolder(), RelativePath::buildTargetFolder);
  232. XmlElement* unit = xml.createNewChildElement ("Unit");
  233. unit->setAttribute ("filename", file.toUnixStyle());
  234. if (! projectItem.shouldBeCompiled())
  235. {
  236. unit->createNewChildElement("Option")->setAttribute ("compile", 0);
  237. unit->createNewChildElement("Option")->setAttribute ("link", 0);
  238. }
  239. }
  240. }
  241. void addCompileUnits (XmlElement& xml) const
  242. {
  243. for (int i = 0; i < getAllGroups().size(); ++i)
  244. addCompileUnits (getAllGroups().getReference(i), xml);
  245. }
  246. void createProject (XmlElement& xml) const
  247. {
  248. addOptions (xml);
  249. addBuild (xml);
  250. addProjectCompilerOptions (xml);
  251. addProjectLinkerOptions (xml);
  252. addCompileUnits (xml);
  253. }
  254. void setAddOption (XmlElement& xml, const String& name, const String& value) const
  255. {
  256. xml.createNewChildElement ("Add")->setAttribute (name, value);
  257. }
  258. JUCE_DECLARE_NON_COPYABLE (CodeBlocksProjectExporter)
  259. };