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 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 ("-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 + "/" + replacePreprocessorTokens (config, 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. static 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& nm, const String& value) const
  258. {
  259. xml.createNewChildElement ("Add")->setAttribute (nm, value);
  260. }
  261. JUCE_DECLARE_NON_COPYABLE (CodeBlocksProjectExporter)
  262. };