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.

473 lines
17KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI 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. enum CodeBlocksOS
  21. {
  22. windowsTarget,
  23. linuxTarget
  24. };
  25. //==============================================================================
  26. static const char* getNameWindows() noexcept { return "Code::Blocks (Windows)"; }
  27. static const char* getNameLinux() noexcept { return "Code::Blocks (Linux)"; }
  28. static const char* getName (CodeBlocksOS os) noexcept
  29. {
  30. if (os == windowsTarget) return getNameWindows();
  31. if (os == linuxTarget) return getNameLinux();
  32. // currently no other OSes supported by Codeblocks exporter!
  33. jassertfalse;
  34. return "Code::Blocks (Unknown OS)";
  35. }
  36. //==============================================================================
  37. static const char* getValueTreeTypeName (CodeBlocksOS os)
  38. {
  39. if (os == windowsTarget) return "CODEBLOCKS_WINDOWS";
  40. if (os == linuxTarget) return "CODEBLOCKS_LINUX";
  41. // currently no other OSes supported by Codeblocks exporter!
  42. jassertfalse;
  43. return "CODEBLOCKS_UNKOWN_OS";
  44. }
  45. //==============================================================================
  46. static String getTargetFolderName (CodeBlocksOS os)
  47. {
  48. if (os == windowsTarget) return "CodeBlocksWindows";
  49. if (os == linuxTarget) return "CodeBlocksLinux";
  50. // currently no other OSes supported by Codeblocks exporter!
  51. jassertfalse;
  52. return "CodeBlocksUnknownOS";
  53. }
  54. //==============================================================================
  55. static CodeBlocksProjectExporter* createForSettings (Project& project, const ValueTree& settings)
  56. {
  57. // this will also import legacy jucer files where CodeBlocks only worked for Windows,
  58. // had valueTreetTypeName "CODEBLOCKS", and there was no OS distinction
  59. if (settings.hasType (getValueTreeTypeName (windowsTarget)) || settings.hasType ("CODEBLOCKS"))
  60. return new CodeBlocksProjectExporter (project, settings, windowsTarget);
  61. if (settings.hasType (getValueTreeTypeName (linuxTarget)))
  62. return new CodeBlocksProjectExporter (project, settings, linuxTarget);
  63. return nullptr;
  64. }
  65. //==============================================================================
  66. CodeBlocksProjectExporter (Project& p, const ValueTree& t, CodeBlocksOS codeBlocksOs)
  67. : ProjectExporter (p, t), os (codeBlocksOs)
  68. {
  69. name = getName (os);
  70. if (getTargetLocationString().isEmpty())
  71. getTargetLocationValue() = getDefaultBuildsRootFolder() + getTargetFolderName (os);
  72. initialiseDependencyPathValues();
  73. }
  74. //==============================================================================
  75. bool canLaunchProject() override { return false; }
  76. bool launchProject() override { return false; }
  77. bool usesMMFiles() const override { return false; }
  78. bool canCopeWithDuplicateFiles() override { return false; }
  79. bool supportsUserDefinedConfigurations() const override { return true; }
  80. bool isXcode() const override { return false; }
  81. bool isVisualStudio() const override { return false; }
  82. bool isCodeBlocks() const override { return true; }
  83. bool isMakefile() const override { return false; }
  84. bool isAndroidStudio() const override { return false; }
  85. bool isAndroidAnt() const override { return false; }
  86. bool isAndroid() const override { return false; }
  87. bool isWindows() const override { return os == windowsTarget; }
  88. bool isLinux() const override { return os == linuxTarget; }
  89. bool isOSX() const override { return false; }
  90. bool isiOS() const override { return false; }
  91. bool supportsVST() const override { return false; }
  92. bool supportsVST3() const override { return false; }
  93. bool supportsAAX() const override { return false; }
  94. bool supportsRTAS() const override { return false; }
  95. bool supportsAU() const override { return false; }
  96. bool supportsAUv3() const override { return false; }
  97. bool supportsStandalone() const override { return false; }
  98. void createExporterProperties (PropertyListBuilder&) override
  99. {
  100. }
  101. //==============================================================================
  102. void create (const OwnedArray<LibraryModule>&) const override
  103. {
  104. const File cbpFile (getTargetFolder().getChildFile (project.getProjectFilenameRoot())
  105. .withFileExtension (".cbp"));
  106. XmlElement xml ("CodeBlocks_project_file");
  107. addVersion (xml);
  108. createProject (*xml.createNewChildElement ("Project"));
  109. writeXmlOrThrow (xml, cbpFile, "UTF-8", 10);
  110. }
  111. //==============================================================================
  112. void addPlatformSpecificSettingsForProjectType (const ProjectType&) override
  113. {
  114. // no-op.
  115. }
  116. private:
  117. //==============================================================================
  118. class CodeBlocksBuildConfiguration : public BuildConfiguration
  119. {
  120. public:
  121. CodeBlocksBuildConfiguration (Project& p, const ValueTree& settings, const ProjectExporter& e)
  122. : BuildConfiguration (p, settings, e)
  123. {
  124. }
  125. void createConfigProperties (PropertyListBuilder& props) override
  126. {
  127. addGCCOptimisationProperty (props);
  128. }
  129. var getDefaultOptimisationLevel() const override { return var ((int) (isDebug() ? gccO0 : gccO3)); }
  130. };
  131. BuildConfiguration::Ptr createBuildConfig (const ValueTree& tree) const override
  132. {
  133. return new CodeBlocksBuildConfiguration (project, tree, *this);
  134. }
  135. //==============================================================================
  136. void addVersion (XmlElement& xml) const
  137. {
  138. XmlElement* fileVersion = xml.createNewChildElement ("FileVersion");
  139. fileVersion->setAttribute ("major", 1);
  140. fileVersion->setAttribute ("minor", 6);
  141. }
  142. void addOptions (XmlElement& xml) const
  143. {
  144. xml.createNewChildElement ("Option")->setAttribute ("title", project.getTitle());
  145. xml.createNewChildElement ("Option")->setAttribute ("pch_mode", 2);
  146. xml.createNewChildElement ("Option")->setAttribute ("compiler", "gcc");
  147. }
  148. StringArray getDefines (const BuildConfiguration& config) const
  149. {
  150. StringPairArray defines;
  151. if (isCodeBlocks() && isWindows())
  152. {
  153. defines.set ("__MINGW__", "1");
  154. defines.set ("__MINGW_EXTENSION", String());
  155. }
  156. else
  157. {
  158. defines.set ("LINUX", "1");
  159. }
  160. if (config.isDebug())
  161. {
  162. defines.set ("DEBUG", "1");
  163. defines.set ("_DEBUG", "1");
  164. }
  165. else
  166. {
  167. defines.set ("NDEBUG", "1");
  168. }
  169. defines = mergePreprocessorDefs (defines, getAllPreprocessorDefs (config));
  170. StringArray defs;
  171. for (int i = 0; i < defines.size(); ++i)
  172. defs.add (defines.getAllKeys()[i] + "=" + defines.getAllValues()[i]);
  173. return getCleanedStringArray (defs);
  174. }
  175. StringArray getCompilerFlags (const BuildConfiguration& config) const
  176. {
  177. StringArray flags;
  178. flags.add ("-O" + config.getGCCOptimisationFlag());
  179. flags.add ("-std=c++11");
  180. flags.add ("-mstackrealign");
  181. if (config.isDebug())
  182. flags.add ("-g");
  183. flags.addTokens (replacePreprocessorTokens (config, getExtraCompilerFlagsString()).trim(),
  184. " \n", "\"'");
  185. {
  186. const StringArray defines (getDefines (config));
  187. for (int i = 0; i < defines.size(); ++i)
  188. {
  189. String def (defines[i]);
  190. if (! def.containsChar ('='))
  191. def << '=';
  192. flags.add ("-D" + def);
  193. }
  194. }
  195. if (config.exporter.isLinux())
  196. {
  197. if (linuxPackages.size() > 0)
  198. {
  199. auto pkgconfigFlags = String ("`pkg-config --cflags");
  200. for (auto p : linuxPackages)
  201. pkgconfigFlags << " " << p;
  202. pkgconfigFlags << "`";
  203. flags.add (pkgconfigFlags);
  204. }
  205. if (linuxLibs.contains("pthread"))
  206. flags.add ("-pthread");
  207. }
  208. return getCleanedStringArray (flags);
  209. }
  210. StringArray getLinkerFlags (const BuildConfiguration& config) const
  211. {
  212. StringArray flags (makefileExtraLinkerFlags);
  213. if (! config.isDebug())
  214. flags.add ("-s");
  215. flags.addTokens (replacePreprocessorTokens (config, getExtraLinkerFlagsString()).trim(),
  216. " \n", "\"'");
  217. if (config.exporter.isLinux() && linuxPackages.size() > 0)
  218. {
  219. auto pkgconfigLibs = String ("`pkg-config --libs");
  220. for (auto p : linuxPackages)
  221. pkgconfigLibs << " " << p;
  222. pkgconfigLibs << "`";
  223. flags.add (pkgconfigLibs);
  224. }
  225. return getCleanedStringArray (flags);
  226. }
  227. StringArray getIncludePaths (const BuildConfiguration& config) const
  228. {
  229. StringArray paths;
  230. paths.add (".");
  231. paths.addArray (extraSearchPaths);
  232. paths.addArray (config.getHeaderSearchPaths());
  233. if (! (isCodeBlocks() && isWindows()))
  234. paths.add ("/usr/include/freetype2");
  235. return getCleanedStringArray (paths);
  236. }
  237. static int getTypeIndex (const ProjectType& type)
  238. {
  239. if (type.isGUIApplication()) return 0;
  240. if (type.isCommandLineApp()) return 1;
  241. if (type.isStaticLibrary()) return 2;
  242. if (type.isDynamicLibrary()) return 3;
  243. if (type.isAudioPlugin()) return 3;
  244. return 0;
  245. }
  246. void createBuildTarget (XmlElement& xml, const BuildConfiguration& config) const
  247. {
  248. xml.setAttribute ("title", config.getName());
  249. {
  250. XmlElement* output = xml.createNewChildElement ("Option");
  251. String outputPath;
  252. if (config.getTargetBinaryRelativePathString().isNotEmpty())
  253. {
  254. RelativePath binaryPath (config.getTargetBinaryRelativePathString(), RelativePath::projectFolder);
  255. binaryPath = binaryPath.rebased (projectFolder, getTargetFolder(), RelativePath::buildTargetFolder);
  256. outputPath = config.getTargetBinaryRelativePathString();
  257. }
  258. else
  259. {
  260. outputPath ="bin/" + File::createLegalFileName (config.getName().trim());
  261. }
  262. output->setAttribute ("output", outputPath + "/" + replacePreprocessorTokens (config, config.getTargetBinaryNameString()));
  263. output->setAttribute ("prefix_auto", 1);
  264. output->setAttribute ("extension_auto", 1);
  265. }
  266. xml.createNewChildElement ("Option")
  267. ->setAttribute ("object_output", "obj/" + File::createLegalFileName (config.getName().trim()));
  268. xml.createNewChildElement ("Option")->setAttribute ("type", getTypeIndex (project.getProjectType()));
  269. xml.createNewChildElement ("Option")->setAttribute ("compiler", "gcc");
  270. {
  271. XmlElement* const compiler = xml.createNewChildElement ("Compiler");
  272. {
  273. const StringArray compilerFlags (getCompilerFlags (config));
  274. for (int i = 0; i < compilerFlags.size(); ++i)
  275. setAddOption (*compiler, "option", compilerFlags[i]);
  276. }
  277. {
  278. const StringArray includePaths (getIncludePaths (config));
  279. for (int i = 0; i < includePaths.size(); ++i)
  280. setAddOption (*compiler, "directory", includePaths[i]);
  281. }
  282. }
  283. {
  284. XmlElement* const linker = xml.createNewChildElement ("Linker");
  285. const StringArray linkerFlags (getLinkerFlags (config));
  286. for (int i = 0; i < linkerFlags.size(); ++i)
  287. setAddOption (*linker, "option", linkerFlags[i]);
  288. const StringArray& libs = isWindows() ? mingwLibs : linuxLibs;
  289. for (int i = 0; i < libs.size(); ++i)
  290. setAddOption (*linker, "library", libs[i]);
  291. const StringArray librarySearchPaths (config.getLibrarySearchPaths());
  292. for (int i = 0; i < librarySearchPaths.size(); ++i)
  293. setAddOption (*linker, "directory", replacePreprocessorDefs (getAllPreprocessorDefs(), librarySearchPaths[i]));
  294. }
  295. }
  296. void addBuild (XmlElement& xml) const
  297. {
  298. XmlElement* const build = xml.createNewChildElement ("Build");
  299. for (ConstConfigIterator config (*this); config.next();)
  300. createBuildTarget (*build->createNewChildElement ("Target"), *config);
  301. }
  302. void addProjectCompilerOptions (XmlElement& xml) const
  303. {
  304. XmlElement* const compiler = xml.createNewChildElement ("Compiler");
  305. setAddOption (*compiler, "option", "-Wall");
  306. setAddOption (*compiler, "option", "-Wno-strict-aliasing");
  307. setAddOption (*compiler, "option", "-Wno-strict-overflow");
  308. }
  309. void addProjectLinkerOptions (XmlElement& xml) const
  310. {
  311. XmlElement* const linker = xml.createNewChildElement ("Linker");
  312. StringArray libs;
  313. if (isWindows())
  314. {
  315. static const char* defaultLibs[] = { "gdi32", "user32", "kernel32", "comctl32" };
  316. libs = StringArray (defaultLibs, numElementsInArray (defaultLibs));
  317. }
  318. libs.addTokens (getExternalLibrariesString(), ";\n", "\"'");
  319. libs = getCleanedStringArray (libs);
  320. for (int i = 0; i < libs.size(); ++i)
  321. setAddOption (*linker, "library", replacePreprocessorDefs (getAllPreprocessorDefs(), libs[i]));
  322. }
  323. void addCompileUnits (const Project::Item& projectItem, XmlElement& xml) const
  324. {
  325. if (projectItem.isGroup())
  326. {
  327. for (int i = 0; i < projectItem.getNumChildren(); ++i)
  328. addCompileUnits (projectItem.getChild(i), xml);
  329. }
  330. else if (projectItem.shouldBeAddedToTargetProject())
  331. {
  332. const RelativePath file (projectItem.getFile(), getTargetFolder(), RelativePath::buildTargetFolder);
  333. XmlElement* unit = xml.createNewChildElement ("Unit");
  334. unit->setAttribute ("filename", file.toUnixStyle());
  335. if (! projectItem.shouldBeCompiled())
  336. {
  337. unit->createNewChildElement("Option")->setAttribute ("compile", 0);
  338. unit->createNewChildElement("Option")->setAttribute ("link", 0);
  339. }
  340. }
  341. }
  342. void addCompileUnits (XmlElement& xml) const
  343. {
  344. for (int i = 0; i < getAllGroups().size(); ++i)
  345. addCompileUnits (getAllGroups().getReference(i), xml);
  346. }
  347. void createProject (XmlElement& xml) const
  348. {
  349. addOptions (xml);
  350. addBuild (xml);
  351. addProjectCompilerOptions (xml);
  352. addProjectLinkerOptions (xml);
  353. addCompileUnits (xml);
  354. }
  355. void setAddOption (XmlElement& xml, const String& nm, const String& value) const
  356. {
  357. xml.createNewChildElement ("Add")->setAttribute (nm, value);
  358. }
  359. void initialiseDependencyPathValues()
  360. {
  361. DependencyPathOS pathOS = isLinux() ? TargetOS::linux
  362. : TargetOS::windows;
  363. vst3Path.referTo (Value (new DependencyPathValueSource (getSetting (Ids::vst3Folder), Ids::vst3Path, pathOS)));
  364. if (! isLinux())
  365. {
  366. aaxPath.referTo (Value (new DependencyPathValueSource (getSetting (Ids::aaxFolder), Ids::aaxPath, pathOS)));
  367. rtasPath.referTo (Value (new DependencyPathValueSource (getSetting (Ids::rtasFolder), Ids::rtasPath, pathOS)));
  368. }
  369. }
  370. CodeBlocksOS os;
  371. JUCE_DECLARE_NON_COPYABLE (CodeBlocksProjectExporter)
  372. };