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.

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