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.

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