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.

838 lines
32KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. #pragma once
  19. #include "jucer_ProjectExport_MSVC.h"
  20. //==============================================================================
  21. class CodeBlocksProjectExporter : public ProjectExporter
  22. {
  23. public:
  24. enum CodeBlocksOS
  25. {
  26. windowsTarget,
  27. linuxTarget
  28. };
  29. //==============================================================================
  30. static String getDisplayNameWindows() { return "Code::Blocks (Windows)"; }
  31. static String getDisplayNameLinux() { return "Code::Blocks (Linux)"; }
  32. static String getValueTreeTypeNameWindows() { return "CODEBLOCKS_WINDOWS"; }
  33. static String getValueTreeTypeNameLinux() { return "CODEBLOCKS_LINUX"; }
  34. static String getTargetFolderNameWindows() { return "CodeBlocksWindows"; }
  35. static String getTargetFolderNameLinux() { return "CodeBlocksLinux"; }
  36. //==============================================================================
  37. static CodeBlocksProjectExporter* createForSettings (Project& projectToUse, const ValueTree& settingsToUse)
  38. {
  39. // this will also import legacy jucer files where CodeBlocks only worked for Windows,
  40. // had valueTreetTypeName "CODEBLOCKS", and there was no OS distinction
  41. if (settingsToUse.hasType (getValueTreeTypeNameWindows()) || settingsToUse.hasType ("CODEBLOCKS"))
  42. return new CodeBlocksProjectExporter (projectToUse, settingsToUse, windowsTarget);
  43. if (settingsToUse.hasType (getValueTreeTypeNameLinux()))
  44. return new CodeBlocksProjectExporter (projectToUse, settingsToUse, linuxTarget);
  45. return nullptr;
  46. }
  47. //==============================================================================
  48. CodeBlocksProjectExporter (Project& p, const ValueTree& t, CodeBlocksOS codeBlocksOs)
  49. : ProjectExporter (p, t), os (codeBlocksOs)
  50. {
  51. if (isWindows())
  52. {
  53. name = getDisplayNameWindows();
  54. targetLocationValue.setDefault (getDefaultBuildsRootFolder() + getTargetFolderNameWindows());
  55. targetPlatformValue.referTo (settings, Ids::codeBlocksWindowsTarget, getUndoManager());
  56. }
  57. else
  58. {
  59. name = getDisplayNameLinux();
  60. targetLocationValue.setDefault (getDefaultBuildsRootFolder() + getTargetFolderNameLinux());
  61. }
  62. }
  63. //==============================================================================
  64. bool canLaunchProject() override { return false; }
  65. bool launchProject() override { return false; }
  66. bool usesMMFiles() const override { return false; }
  67. bool canCopeWithDuplicateFiles() override { return false; }
  68. bool supportsUserDefinedConfigurations() const override { return true; }
  69. bool isXcode() const override { return false; }
  70. bool isVisualStudio() const override { return false; }
  71. bool isCodeBlocks() const override { return true; }
  72. bool isMakefile() const override { return false; }
  73. bool isAndroidStudio() const override { return false; }
  74. bool isCLion() const override { return false; }
  75. bool isAndroid() const override { return false; }
  76. bool isWindows() const override { return os == windowsTarget; }
  77. bool isLinux() const override { return os == linuxTarget; }
  78. bool isOSX() const override { return false; }
  79. bool isiOS() const override { return false; }
  80. bool supportsTargetType (build_tools::ProjectType::Target::Type type) const override
  81. {
  82. switch (type)
  83. {
  84. case build_tools::ProjectType::Target::StandalonePlugIn:
  85. case build_tools::ProjectType::Target::GUIApp:
  86. case build_tools::ProjectType::Target::ConsoleApp:
  87. case build_tools::ProjectType::Target::StaticLibrary:
  88. case build_tools::ProjectType::Target::SharedCodeTarget:
  89. case build_tools::ProjectType::Target::AggregateTarget:
  90. case build_tools::ProjectType::Target::VSTPlugIn:
  91. case build_tools::ProjectType::Target::DynamicLibrary:
  92. return true;
  93. case build_tools::ProjectType::Target::AAXPlugIn:
  94. case build_tools::ProjectType::Target::RTASPlugIn:
  95. case build_tools::ProjectType::Target::UnityPlugIn:
  96. case build_tools::ProjectType::Target::VST3PlugIn:
  97. case build_tools::ProjectType::Target::AudioUnitPlugIn:
  98. case build_tools::ProjectType::Target::AudioUnitv3PlugIn:
  99. case build_tools::ProjectType::Target::unspecified:
  100. default:
  101. break;
  102. }
  103. return false;
  104. }
  105. void createExporterProperties (PropertyListBuilder& props) override
  106. {
  107. if (isWindows())
  108. {
  109. props.add (new ChoicePropertyComponent (targetPlatformValue, "Target platform",
  110. { "Windows NT 4.0", "Windows 2000", "Windows XP", "Windows Server 2003", "Windows Vista", "Windows Server 2008",
  111. "Windows 7", "Windows 8", "Windows 8.1", "Windows 10" },
  112. { "0x0400", "0x0500", "0x0501", "0x0502", "0x0600", "0x0600",
  113. "0x0601", "0x0602", "0x0603", "0x0A00" }),
  114. "This sets the preprocessor macro WINVER to an appropriate value for the corresponding platform.");
  115. }
  116. }
  117. //==============================================================================
  118. void create (const OwnedArray<LibraryModule>&) const override
  119. {
  120. auto cbpFile = getTargetFolder().getChildFile (project.getProjectFilenameRootString())
  121. .withFileExtension (".cbp");
  122. XmlElement xml ("CodeBlocks_project_file");
  123. addVersion (xml);
  124. createProject (*xml.createNewChildElement ("Project"));
  125. writeXmlOrThrow (xml, cbpFile, "UTF-8", 10, true);
  126. }
  127. //==============================================================================
  128. void addPlatformSpecificSettingsForProjectType (const build_tools::ProjectType&) override
  129. {
  130. // add shared code target first as order matters for Codeblocks
  131. if (shouldBuildTargetType (build_tools::ProjectType::Target::SharedCodeTarget))
  132. targets.add (new CodeBlocksTarget (*this, build_tools::ProjectType::Target::SharedCodeTarget));
  133. //resource::ProjectType::Target::SharedCodeTarget
  134. callForAllSupportedTargets ([this] (build_tools::ProjectType::Target::Type targetType)
  135. {
  136. if (targetType == build_tools::ProjectType::Target::SharedCodeTarget)
  137. return;
  138. targets.insert (targetType == build_tools::ProjectType::Target::AggregateTarget ? 0 : -1,
  139. new CodeBlocksTarget (*this, targetType));
  140. });
  141. // If you hit this assert, you tried to generate a project for an exporter
  142. // that does not support any of your targets!
  143. jassert (targets.size() > 0);
  144. }
  145. void initialiseDependencyPathValues() override
  146. {
  147. auto targetOS = isWindows() ? TargetOS::windows : TargetOS::linux;
  148. vstLegacyPathValueWrapper.init ({ settings, Ids::vstLegacyFolder, nullptr },
  149. getAppSettings().getStoredPath (Ids::vstLegacyPath, targetOS), targetOS);
  150. }
  151. private:
  152. ValueWithDefault targetPlatformValue;
  153. String getTargetPlatformString() const { return targetPlatformValue.get(); }
  154. //==============================================================================
  155. class CodeBlocksBuildConfiguration : public BuildConfiguration
  156. {
  157. public:
  158. CodeBlocksBuildConfiguration (Project& p, const ValueTree& settings, const ProjectExporter& e)
  159. : BuildConfiguration (p, settings, e),
  160. architectureTypeValue (config, exporter.isWindows() ? Ids::windowsCodeBlocksArchitecture
  161. : Ids::linuxCodeBlocksArchitecture, getUndoManager(), "-m64")
  162. {
  163. linkTimeOptimisationValue.setDefault (false);
  164. optimisationLevelValue.setDefault (isDebug() ? gccO0 : gccO3);
  165. }
  166. void createConfigProperties (PropertyListBuilder& props) override
  167. {
  168. addRecommendedLinuxCompilerWarningsProperty (props);
  169. addGCCOptimisationProperty (props);
  170. props.add (new ChoicePropertyComponent (architectureTypeValue, "Architecture",
  171. { "32-bit (-m32)", "64-bit (-m64)", "ARM v6", "ARM v7" },
  172. { "-m32", "-m64", "-march=armv6", "-march=armv7" }),
  173. "Specifies the 32/64-bit architecture to use.");
  174. }
  175. String getModuleLibraryArchName() const override
  176. {
  177. auto archFlag = getArchitectureTypeString();
  178. String prefix ("-march=");
  179. if (archFlag.startsWith (prefix))
  180. return archFlag.substring (prefix.length());
  181. else if (archFlag == "-m64")
  182. return "x86_64";
  183. else if (archFlag == "-m32")
  184. return "i386";
  185. jassertfalse;
  186. return {};
  187. }
  188. String getArchitectureTypeString() const { return architectureTypeValue.get(); }
  189. //==============================================================================
  190. ValueWithDefault architectureTypeValue;
  191. };
  192. BuildConfiguration::Ptr createBuildConfig (const ValueTree& tree) const override
  193. {
  194. return *new CodeBlocksBuildConfiguration (project, tree, *this);
  195. }
  196. //==============================================================================
  197. class CodeBlocksTarget : public build_tools::ProjectType::Target
  198. {
  199. public:
  200. CodeBlocksTarget (const CodeBlocksProjectExporter& e,
  201. build_tools::ProjectType::Target::Type typeToUse)
  202. : Target (typeToUse),
  203. exporter (e)
  204. {}
  205. String getTargetNameForConfiguration (const BuildConfiguration& config) const
  206. {
  207. if (type == build_tools::ProjectType::Target::AggregateTarget)
  208. return config.getName();
  209. return getName() + String (" | ") + config.getName();
  210. }
  211. String getTargetSuffix() const
  212. {
  213. auto fileType = getTargetFileType();
  214. if (exporter.isWindows())
  215. {
  216. switch (fileType)
  217. {
  218. case executable: return ".exe";
  219. case staticLibrary: return ".lib";
  220. case sharedLibraryOrDLL:
  221. case pluginBundle: return ".dll";
  222. case macOSAppex:
  223. case unknown:
  224. default:
  225. break;
  226. }
  227. }
  228. else
  229. {
  230. switch (fileType)
  231. {
  232. case executable: return {};
  233. case staticLibrary: return ".a";
  234. case pluginBundle:
  235. case sharedLibraryOrDLL: return ".so";
  236. case macOSAppex:
  237. case unknown:
  238. default:
  239. break;
  240. }
  241. }
  242. return {};
  243. }
  244. bool isDynamicLibrary() const
  245. {
  246. return (type == DynamicLibrary || type == VSTPlugIn);
  247. }
  248. const CodeBlocksProjectExporter& exporter;
  249. };
  250. //==============================================================================
  251. StringArray getPackages() const
  252. {
  253. auto result = linuxPackages;
  254. if (project.getEnabledModules().isModuleEnabled ("juce_gui_extra")
  255. && project.isConfigFlagEnabled ("JUCE_WEB_BROWSER", true))
  256. {
  257. result.add ("webkit2gtk-4.0");
  258. result.add ("gtk+-x11-3.0");
  259. }
  260. if (project.getEnabledModules().isModuleEnabled ("juce_core")
  261. && project.isConfigFlagEnabled ("JUCE_USE_CURL", true)
  262. && ! project.isConfigFlagEnabled ("JUCE_LOAD_CURL_SYMBOLS_LAZILY", false))
  263. result.add ("libcurl");
  264. result.removeDuplicates (false);
  265. return result;
  266. }
  267. void addVersion (XmlElement& xml) const
  268. {
  269. auto* fileVersion = xml.createNewChildElement ("FileVersion");
  270. fileVersion->setAttribute ("major", 1);
  271. fileVersion->setAttribute ("minor", 6);
  272. }
  273. void addOptions (XmlElement& xml) const
  274. {
  275. xml.createNewChildElement ("Option")->setAttribute ("title", project.getProjectNameString());
  276. xml.createNewChildElement ("Option")->setAttribute ("pch_mode", 2);
  277. xml.createNewChildElement ("Option")->setAttribute ("compiler", "gcc");
  278. }
  279. StringArray getDefines (const BuildConfiguration& config, CodeBlocksTarget& target) const
  280. {
  281. StringPairArray defines;
  282. if (isWindows())
  283. {
  284. defines.set ("__MINGW__", "1");
  285. defines.set ("__MINGW_EXTENSION", {});
  286. auto targetPlatform = getTargetPlatformString();
  287. if (targetPlatform.isNotEmpty())
  288. defines.set ("WINVER", targetPlatform);
  289. }
  290. else
  291. {
  292. defines.set ("LINUX", "1");
  293. }
  294. if (config.isDebug())
  295. {
  296. defines.set ("DEBUG", "1");
  297. defines.set ("_DEBUG", "1");
  298. }
  299. else
  300. {
  301. defines.set ("NDEBUG", "1");
  302. }
  303. defines = mergePreprocessorDefs (defines, getAllPreprocessorDefs (config, target.type));
  304. StringArray defs;
  305. auto keys = defines.getAllKeys();
  306. auto values = defines.getAllValues();
  307. for (int i = 0; i < defines.size(); ++i)
  308. {
  309. auto result = keys[i];
  310. if (values[i].isNotEmpty())
  311. result += "=" + values[i];
  312. defs.add (result);
  313. }
  314. return getCleanedStringArray (defs);
  315. }
  316. StringArray getCompilerFlags (const BuildConfiguration& config, CodeBlocksTarget& target) const
  317. {
  318. StringArray flags;
  319. if (auto* codeBlocksConfig = dynamic_cast<const CodeBlocksBuildConfiguration*> (&config))
  320. flags.add (codeBlocksConfig->getArchitectureTypeString());
  321. for (auto& recommended : config.getRecommendedCompilerWarningFlags())
  322. flags.add (recommended);
  323. flags.add ("-O" + config.getGCCOptimisationFlag());
  324. if (config.isLinkTimeOptimisationEnabled())
  325. flags.add ("-flto");
  326. {
  327. auto cppStandard = config.project.getCppStandardString();
  328. if (cppStandard == "latest")
  329. cppStandard = "17";
  330. cppStandard = "-std=" + String (shouldUseGNUExtensions() ? "gnu++" : "c++") + cppStandard;
  331. flags.add (cppStandard);
  332. }
  333. flags.add ("-mstackrealign");
  334. if (config.isDebug())
  335. flags.add ("-g");
  336. flags.addTokens (replacePreprocessorTokens (config, getExtraCompilerFlagsString()).trim(),
  337. " \n", "\"'");
  338. if (config.exporter.isLinux())
  339. {
  340. if (target.isDynamicLibrary() || getProject().isAudioPluginProject())
  341. flags.add ("-fPIC");
  342. auto packages = getPackages();
  343. if (packages.size() > 0)
  344. {
  345. auto pkgconfigFlags = String ("`pkg-config --cflags");
  346. for (auto p : packages)
  347. pkgconfigFlags << " " << p;
  348. pkgconfigFlags << "`";
  349. flags.add (pkgconfigFlags);
  350. }
  351. if (linuxLibs.contains("pthread"))
  352. flags.add ("-pthread");
  353. }
  354. return getCleanedStringArray (flags);
  355. }
  356. StringArray getLinkerFlags (const BuildConfiguration& config, CodeBlocksTarget& target) const
  357. {
  358. auto flags = makefileExtraLinkerFlags;
  359. if (auto* codeBlocksConfig = dynamic_cast<const CodeBlocksBuildConfiguration*> (&config))
  360. flags.add (codeBlocksConfig->getArchitectureTypeString());
  361. if (! config.isDebug())
  362. flags.add ("-s");
  363. if (config.isLinkTimeOptimisationEnabled())
  364. flags.add ("-flto");
  365. flags.addTokens (replacePreprocessorTokens (config, getExtraLinkerFlagsString()).trim(), " \n", "\"'");
  366. auto packages = getPackages();
  367. if (config.exporter.isLinux())
  368. {
  369. if (target.isDynamicLibrary())
  370. flags.add ("-shared");
  371. if (packages.size() > 0)
  372. {
  373. String pkgconfigLibs ("`pkg-config --libs");
  374. for (auto& p : packages)
  375. pkgconfigLibs << " " << p;
  376. pkgconfigLibs << "`";
  377. flags.add (pkgconfigLibs);
  378. }
  379. }
  380. return getCleanedStringArray (flags);
  381. }
  382. StringArray getLinkerSearchPaths (const BuildConfiguration& config, CodeBlocksTarget& target) const
  383. {
  384. auto librarySearchPaths = config.getLibrarySearchPaths();
  385. if (getProject().isAudioPluginProject() && target.type != build_tools::ProjectType::Target::SharedCodeTarget)
  386. librarySearchPaths.add (build_tools::RelativePath (getSharedCodePath (config), build_tools::RelativePath::buildTargetFolder).getParentDirectory().toUnixStyle().quoted());
  387. return librarySearchPaths;
  388. }
  389. StringArray getIncludePaths (const BuildConfiguration& config) const
  390. {
  391. StringArray paths;
  392. paths.add (".");
  393. paths.addArray (extraSearchPaths);
  394. paths.addArray (config.getHeaderSearchPaths());
  395. if (! isWindows())
  396. {
  397. paths.add ("/usr/include/freetype2");
  398. // Replace ~ character with $(HOME) environment variable
  399. for (auto& path : paths)
  400. path = path.replace ("~", "$(HOME)");
  401. }
  402. return getCleanedStringArray (paths);
  403. }
  404. static int getTypeIndex (const build_tools::ProjectType::Target::Type& type)
  405. {
  406. if (type == build_tools::ProjectType::Target::GUIApp || type == build_tools::ProjectType::Target::StandalonePlugIn) return 0;
  407. if (type == build_tools::ProjectType::Target::ConsoleApp) return 1;
  408. if (type == build_tools::ProjectType::Target::StaticLibrary || type == build_tools::ProjectType::Target::SharedCodeTarget) return 2;
  409. if (type == build_tools::ProjectType::Target::DynamicLibrary || type == build_tools::ProjectType::Target::VSTPlugIn) return 3;
  410. return 0;
  411. }
  412. String getOutputPathForTarget (CodeBlocksTarget& target, const BuildConfiguration& config) const
  413. {
  414. String outputPath;
  415. if (config.getTargetBinaryRelativePathString().isNotEmpty())
  416. {
  417. build_tools::RelativePath binaryPath (config.getTargetBinaryRelativePathString(), build_tools::RelativePath::projectFolder);
  418. binaryPath = binaryPath.rebased (projectFolder, getTargetFolder(), build_tools::RelativePath::buildTargetFolder);
  419. outputPath = config.getTargetBinaryRelativePathString();
  420. }
  421. else
  422. {
  423. outputPath ="bin/" + File::createLegalFileName (config.getName().trim());
  424. }
  425. return outputPath + "/" + replacePreprocessorTokens (config, config.getTargetBinaryNameString() + target.getTargetSuffix());
  426. }
  427. String getSharedCodePath (const BuildConfiguration& config) const
  428. {
  429. auto outputPath = getOutputPathForTarget (getTargetWithType (build_tools::ProjectType::Target::SharedCodeTarget), config);
  430. build_tools::RelativePath path (outputPath, build_tools::RelativePath::buildTargetFolder);
  431. auto filename = path.getFileName();
  432. if (isLinux())
  433. filename = "lib" + filename;
  434. return path.getParentDirectory().getChildFile (filename).toUnixStyle();
  435. }
  436. void createBuildTarget (XmlElement& xml, CodeBlocksTarget& target, const BuildConfiguration& config) const
  437. {
  438. xml.setAttribute ("title", target.getTargetNameForConfiguration (config));
  439. {
  440. auto* output = xml.createNewChildElement ("Option");
  441. output->setAttribute ("output", getOutputPathForTarget (target, config));
  442. if (isLinux())
  443. {
  444. bool keepPrefix = (target.type == build_tools::ProjectType::Target::VSTPlugIn);
  445. output->setAttribute ("prefix_auto", keepPrefix ? 0 : 1);
  446. }
  447. else
  448. {
  449. output->setAttribute ("prefix_auto", 0);
  450. }
  451. output->setAttribute ("extension_auto", 0);
  452. }
  453. xml.createNewChildElement ("Option")
  454. ->setAttribute ("object_output", "obj/" + File::createLegalFileName (config.getName().trim()));
  455. xml.createNewChildElement ("Option")->setAttribute ("type", getTypeIndex (target.type));
  456. xml.createNewChildElement ("Option")->setAttribute ("compiler", "gcc");
  457. if (getProject().isAudioPluginProject() && target.type != build_tools::ProjectType::Target::SharedCodeTarget)
  458. xml.createNewChildElement ("Option")->setAttribute ("external_deps", getSharedCodePath (config));
  459. {
  460. auto* compiler = xml.createNewChildElement ("Compiler");
  461. {
  462. StringArray flags;
  463. for (auto& def : getDefines (config, target))
  464. {
  465. if (! def.containsChar ('='))
  466. def << '=';
  467. flags.add ("-D" + def);
  468. }
  469. flags.addArray (getCompilerFlags (config, target));
  470. for (auto flag : flags)
  471. setAddOption (*compiler, "option", flag);
  472. }
  473. {
  474. auto includePaths = getIncludePaths (config);
  475. for (auto path : includePaths)
  476. setAddOption (*compiler, "directory", path);
  477. }
  478. }
  479. {
  480. auto* linker = xml.createNewChildElement ("Linker");
  481. if (getProject().isAudioPluginProject() && target.type != build_tools::ProjectType::Target::SharedCodeTarget)
  482. setAddOption (*linker, "option", getSharedCodePath (config).quoted());
  483. for (auto& flag : getLinkerFlags (config, target))
  484. setAddOption (*linker, "option", flag);
  485. const StringArray& libs = isWindows() ? mingwLibs : linuxLibs;
  486. for (auto lib : libs)
  487. setAddOption (*linker, "library", lib);
  488. for (auto& path : getLinkerSearchPaths (config, target))
  489. setAddOption (*linker, "directory",
  490. build_tools::replacePreprocessorDefs (getAllPreprocessorDefs(), path));
  491. }
  492. }
  493. void addBuild (XmlElement& xml) const
  494. {
  495. auto* build = xml.createNewChildElement ("Build");
  496. for (ConstConfigIterator config (*this); config.next();)
  497. for (auto target : targets)
  498. if (target->type != build_tools::ProjectType::Target::AggregateTarget)
  499. createBuildTarget (*build->createNewChildElement ("Target"), *target, *config);
  500. }
  501. void addVirtualTargets (XmlElement& xml) const
  502. {
  503. auto* virtualTargets = xml.createNewChildElement ("VirtualTargets");
  504. for (ConstConfigIterator config (*this); config.next();)
  505. {
  506. StringArray allTargets;
  507. for (auto target : targets)
  508. if (target->type != build_tools::ProjectType::Target::AggregateTarget)
  509. allTargets.add (target->getTargetNameForConfiguration (*config));
  510. for (auto target : targets)
  511. {
  512. if (target->type == build_tools::ProjectType::Target::AggregateTarget)
  513. {
  514. auto* configTarget = virtualTargets->createNewChildElement ("Add");
  515. configTarget->setAttribute ("alias", config->getName());
  516. configTarget->setAttribute ("targets", allTargets.joinIntoString (";"));
  517. }
  518. }
  519. }
  520. }
  521. StringArray getProjectCompilerOptions() const
  522. {
  523. return { "-Wall", "-Wno-strict-aliasing", "-Wno-strict-overflow" };
  524. }
  525. void addProjectCompilerOptions (XmlElement& xml) const
  526. {
  527. auto* compiler = xml.createNewChildElement ("Compiler");
  528. for (auto& option : getProjectCompilerOptions())
  529. setAddOption (*compiler, "option", option);
  530. }
  531. StringArray getProjectLinkerLibs() const
  532. {
  533. StringArray result;
  534. if (isWindows())
  535. result.addArray ({ "gdi32", "user32", "kernel32", "comctl32" });
  536. result.addTokens (getExternalLibrariesString(), ";\n", "\"'");
  537. result = getCleanedStringArray (result);
  538. for (auto& option : result)
  539. option = build_tools::replacePreprocessorDefs (getAllPreprocessorDefs(), option);
  540. return result;
  541. }
  542. void addProjectLinkerOptions (XmlElement& xml) const
  543. {
  544. auto* linker = xml.createNewChildElement ("Linker");
  545. for (auto& lib : getProjectLinkerLibs())
  546. setAddOption (*linker, "library", lib);
  547. }
  548. CodeBlocksTarget& getTargetWithType (build_tools::ProjectType::Target::Type type) const
  549. {
  550. CodeBlocksTarget* nonAggregrateTarget = nullptr;
  551. for (auto* target : targets)
  552. {
  553. if (target->type == type)
  554. return *target;
  555. if (target->type != build_tools::ProjectType::Target::AggregateTarget)
  556. nonAggregrateTarget = target;
  557. }
  558. // this project has no valid targets
  559. jassert (nonAggregrateTarget != nullptr);
  560. return *nonAggregrateTarget;
  561. }
  562. // Returns SharedCode target for multi-target projects, otherwise it returns
  563. // the single target
  564. CodeBlocksTarget& getMainTarget() const
  565. {
  566. if (getProject().isAudioPluginProject())
  567. return getTargetWithType (build_tools::ProjectType::Target::SharedCodeTarget);
  568. for (auto* target : targets)
  569. if (target->type != build_tools::ProjectType::Target::AggregateTarget)
  570. return *target;
  571. jassertfalse;
  572. return *targets[0];
  573. }
  574. CodeBlocksTarget& getTargetForProjectItem (const Project::Item& projectItem) const
  575. {
  576. if (getProject().isAudioPluginProject())
  577. {
  578. if (! projectItem.shouldBeCompiled())
  579. return getTargetWithType (build_tools::ProjectType::Target::SharedCodeTarget);
  580. return getTargetWithType (getProject().getTargetTypeFromFilePath (projectItem.getFile(), true));
  581. }
  582. return getMainTarget();
  583. }
  584. void addCompileUnits (const Project::Item& projectItem, XmlElement& xml) const
  585. {
  586. if (projectItem.isGroup())
  587. {
  588. for (int i = 0; i < projectItem.getNumChildren(); ++i)
  589. addCompileUnits (projectItem.getChild(i), xml);
  590. }
  591. else if (projectItem.shouldBeAddedToTargetProject() && projectItem.shouldBeAddedToTargetExporter (*this))
  592. {
  593. build_tools::RelativePath file (projectItem.getFile(), getTargetFolder(), build_tools::RelativePath::buildTargetFolder);
  594. auto* unit = xml.createNewChildElement ("Unit");
  595. unit->setAttribute ("filename", file.toUnixStyle());
  596. for (ConstConfigIterator config (*this); config.next();)
  597. {
  598. auto targetName = getTargetForProjectItem (projectItem).getTargetNameForConfiguration (*config);
  599. unit->createNewChildElement ("Option")->setAttribute ("target", targetName);
  600. }
  601. if (projectItem.shouldBeCompiled())
  602. {
  603. auto extraCompilerFlags = compilerFlagSchemesMap[projectItem.getCompilerFlagSchemeString()].get().toString();
  604. if (extraCompilerFlags.isNotEmpty())
  605. {
  606. auto* optionElement = unit->createNewChildElement ("Option");
  607. optionElement->setAttribute ("compiler", "gcc");
  608. optionElement->setAttribute ("use", 1);
  609. optionElement->setAttribute ("buildCommand", "$compiler $options " + extraCompilerFlags + " $includes -c $file -o $object");
  610. }
  611. }
  612. else
  613. {
  614. unit->createNewChildElement ("Option")->setAttribute ("compile", 0);
  615. unit->createNewChildElement ("Option")->setAttribute ("link", 0);
  616. }
  617. }
  618. }
  619. bool hasResourceFile() const
  620. {
  621. return ! projectType.isStaticLibrary();
  622. }
  623. void addCompileUnits (XmlElement& xml) const
  624. {
  625. for (int i = 0; i < getAllGroups().size(); ++i)
  626. addCompileUnits (getAllGroups().getReference(i), xml);
  627. if (hasResourceFile())
  628. {
  629. const auto iconFile = getTargetFolder().getChildFile ("icon.ico");
  630. build_tools::writeMacIcon (getIcons(), iconFile);
  631. auto rcFile = getTargetFolder().getChildFile ("resources.rc");
  632. MSVCProjectExporterBase::createRCFile (project, iconFile, rcFile);
  633. auto* unit = xml.createNewChildElement ("Unit");
  634. unit->setAttribute ("filename", rcFile.getFileName());
  635. unit->createNewChildElement ("Option")->setAttribute ("compilerVar", "WINDRES");
  636. }
  637. }
  638. void createProject (XmlElement& xml) const
  639. {
  640. addOptions (xml);
  641. addBuild (xml);
  642. addVirtualTargets (xml);
  643. addProjectCompilerOptions (xml);
  644. addProjectLinkerOptions (xml);
  645. addCompileUnits (xml);
  646. }
  647. void setAddOption (XmlElement& xml, const String& nm, const String& value) const
  648. {
  649. xml.createNewChildElement ("Add")->setAttribute (nm, value);
  650. }
  651. CodeBlocksOS os;
  652. OwnedArray<CodeBlocksTarget> targets;
  653. friend class CLionProjectExporter;
  654. JUCE_DECLARE_NON_COPYABLE (CodeBlocksProjectExporter)
  655. };