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.

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