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.

844 lines
31KB

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