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.

480 lines
18KB

  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& type) override
  113. {
  114. createDynamicLibrary = type.isAudioPlugin() || type.isDynamicLibrary();
  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 (createDynamicLibrary)
  198. flags.add ("-fPIC");
  199. if (linuxPackages.size() > 0)
  200. {
  201. auto pkgconfigFlags = String ("`pkg-config --cflags");
  202. for (auto p : linuxPackages)
  203. pkgconfigFlags << " " << p;
  204. pkgconfigFlags << "`";
  205. flags.add (pkgconfigFlags);
  206. }
  207. if (linuxLibs.contains("pthread"))
  208. flags.add ("-pthread");
  209. }
  210. return getCleanedStringArray (flags);
  211. }
  212. StringArray getLinkerFlags (const BuildConfiguration& config) const
  213. {
  214. StringArray flags (makefileExtraLinkerFlags);
  215. if (! config.isDebug())
  216. flags.add ("-s");
  217. flags.addTokens (replacePreprocessorTokens (config, getExtraLinkerFlagsString()).trim(),
  218. " \n", "\"'");
  219. if (config.exporter.isLinux() && linuxPackages.size() > 0)
  220. {
  221. if (createDynamicLibrary)
  222. flags.add ("-shared");
  223. auto pkgconfigLibs = String ("`pkg-config --libs");
  224. for (auto p : linuxPackages)
  225. pkgconfigLibs << " " << p;
  226. pkgconfigLibs << "`";
  227. flags.add (pkgconfigLibs);
  228. }
  229. return getCleanedStringArray (flags);
  230. }
  231. StringArray getIncludePaths (const BuildConfiguration& config) const
  232. {
  233. StringArray paths;
  234. paths.add (".");
  235. paths.addArray (extraSearchPaths);
  236. paths.addArray (config.getHeaderSearchPaths());
  237. if (! (isCodeBlocks() && isWindows()))
  238. paths.add ("/usr/include/freetype2");
  239. return getCleanedStringArray (paths);
  240. }
  241. static int getTypeIndex (const ProjectType& type)
  242. {
  243. if (type.isGUIApplication()) return 0;
  244. if (type.isCommandLineApp()) return 1;
  245. if (type.isStaticLibrary()) return 2;
  246. if (type.isDynamicLibrary()) return 3;
  247. if (type.isAudioPlugin()) return 3;
  248. return 0;
  249. }
  250. void createBuildTarget (XmlElement& xml, const BuildConfiguration& config) const
  251. {
  252. xml.setAttribute ("title", config.getName());
  253. {
  254. XmlElement* output = xml.createNewChildElement ("Option");
  255. String outputPath;
  256. if (config.getTargetBinaryRelativePathString().isNotEmpty())
  257. {
  258. RelativePath binaryPath (config.getTargetBinaryRelativePathString(), RelativePath::projectFolder);
  259. binaryPath = binaryPath.rebased (projectFolder, getTargetFolder(), RelativePath::buildTargetFolder);
  260. outputPath = config.getTargetBinaryRelativePathString();
  261. }
  262. else
  263. {
  264. outputPath ="bin/" + File::createLegalFileName (config.getName().trim());
  265. }
  266. output->setAttribute ("output", outputPath + "/" + replacePreprocessorTokens (config, config.getTargetBinaryNameString()));
  267. output->setAttribute ("prefix_auto", 1);
  268. output->setAttribute ("extension_auto", 1);
  269. }
  270. xml.createNewChildElement ("Option")
  271. ->setAttribute ("object_output", "obj/" + File::createLegalFileName (config.getName().trim()));
  272. xml.createNewChildElement ("Option")->setAttribute ("type", getTypeIndex (project.getProjectType()));
  273. xml.createNewChildElement ("Option")->setAttribute ("compiler", "gcc");
  274. {
  275. XmlElement* const compiler = xml.createNewChildElement ("Compiler");
  276. {
  277. const StringArray compilerFlags (getCompilerFlags (config));
  278. for (int i = 0; i < compilerFlags.size(); ++i)
  279. setAddOption (*compiler, "option", compilerFlags[i]);
  280. }
  281. {
  282. const StringArray includePaths (getIncludePaths (config));
  283. for (int i = 0; i < includePaths.size(); ++i)
  284. setAddOption (*compiler, "directory", includePaths[i]);
  285. }
  286. }
  287. {
  288. XmlElement* const linker = xml.createNewChildElement ("Linker");
  289. const StringArray linkerFlags (getLinkerFlags (config));
  290. for (int i = 0; i < linkerFlags.size(); ++i)
  291. setAddOption (*linker, "option", linkerFlags[i]);
  292. const StringArray& libs = isWindows() ? mingwLibs : linuxLibs;
  293. for (int i = 0; i < libs.size(); ++i)
  294. setAddOption (*linker, "library", libs[i]);
  295. const StringArray librarySearchPaths (config.getLibrarySearchPaths());
  296. for (int i = 0; i < librarySearchPaths.size(); ++i)
  297. setAddOption (*linker, "directory", replacePreprocessorDefs (getAllPreprocessorDefs(), librarySearchPaths[i]));
  298. }
  299. }
  300. void addBuild (XmlElement& xml) const
  301. {
  302. XmlElement* const build = xml.createNewChildElement ("Build");
  303. for (ConstConfigIterator config (*this); config.next();)
  304. createBuildTarget (*build->createNewChildElement ("Target"), *config);
  305. }
  306. void addProjectCompilerOptions (XmlElement& xml) const
  307. {
  308. XmlElement* const compiler = xml.createNewChildElement ("Compiler");
  309. setAddOption (*compiler, "option", "-Wall");
  310. setAddOption (*compiler, "option", "-Wno-strict-aliasing");
  311. setAddOption (*compiler, "option", "-Wno-strict-overflow");
  312. }
  313. void addProjectLinkerOptions (XmlElement& xml) const
  314. {
  315. XmlElement* const linker = xml.createNewChildElement ("Linker");
  316. StringArray libs;
  317. if (isWindows())
  318. {
  319. static const char* defaultLibs[] = { "gdi32", "user32", "kernel32", "comctl32" };
  320. libs = StringArray (defaultLibs, numElementsInArray (defaultLibs));
  321. }
  322. libs.addTokens (getExternalLibrariesString(), ";\n", "\"'");
  323. libs = getCleanedStringArray (libs);
  324. for (int i = 0; i < libs.size(); ++i)
  325. setAddOption (*linker, "library", replacePreprocessorDefs (getAllPreprocessorDefs(), libs[i]));
  326. }
  327. void addCompileUnits (const Project::Item& projectItem, XmlElement& xml) const
  328. {
  329. if (projectItem.isGroup())
  330. {
  331. for (int i = 0; i < projectItem.getNumChildren(); ++i)
  332. addCompileUnits (projectItem.getChild(i), xml);
  333. }
  334. else if (projectItem.shouldBeAddedToTargetProject())
  335. {
  336. const RelativePath file (projectItem.getFile(), getTargetFolder(), RelativePath::buildTargetFolder);
  337. XmlElement* unit = xml.createNewChildElement ("Unit");
  338. unit->setAttribute ("filename", file.toUnixStyle());
  339. if (! projectItem.shouldBeCompiled())
  340. {
  341. unit->createNewChildElement("Option")->setAttribute ("compile", 0);
  342. unit->createNewChildElement("Option")->setAttribute ("link", 0);
  343. }
  344. }
  345. }
  346. void addCompileUnits (XmlElement& xml) const
  347. {
  348. for (int i = 0; i < getAllGroups().size(); ++i)
  349. addCompileUnits (getAllGroups().getReference(i), xml);
  350. }
  351. void createProject (XmlElement& xml) const
  352. {
  353. addOptions (xml);
  354. addBuild (xml);
  355. addProjectCompilerOptions (xml);
  356. addProjectLinkerOptions (xml);
  357. addCompileUnits (xml);
  358. }
  359. void setAddOption (XmlElement& xml, const String& nm, const String& value) const
  360. {
  361. xml.createNewChildElement ("Add")->setAttribute (nm, value);
  362. }
  363. void initialiseDependencyPathValues()
  364. {
  365. DependencyPathOS pathOS = isLinux() ? TargetOS::linux
  366. : TargetOS::windows;
  367. vst3Path.referTo (Value (new DependencyPathValueSource (getSetting (Ids::vst3Folder), Ids::vst3Path, pathOS)));
  368. if (! isLinux())
  369. {
  370. aaxPath.referTo (Value (new DependencyPathValueSource (getSetting (Ids::aaxFolder), Ids::aaxPath, pathOS)));
  371. rtasPath.referTo (Value (new DependencyPathValueSource (getSetting (Ids::rtasFolder), Ids::rtasPath, pathOS)));
  372. }
  373. }
  374. CodeBlocksOS os;
  375. bool createDynamicLibrary = false;
  376. JUCE_DECLARE_NON_COPYABLE (CodeBlocksProjectExporter)
  377. };