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.

845 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. "Specifies the 32/64-bit architecture to use.");
  192. }
  193. String getModuleLibraryArchName() const override
  194. {
  195. auto archFlag = getArchitectureTypeString();
  196. String prefix ("-march=");
  197. if (archFlag.startsWith (prefix))
  198. return archFlag.substring (prefix.length());
  199. else if (archFlag == "-m64")
  200. return "x86_64";
  201. else if (archFlag == "-m32")
  202. return "i386";
  203. jassertfalse;
  204. return {};
  205. }
  206. String getArchitectureTypeString() const { return architectureTypeValue.get(); }
  207. //==============================================================================
  208. ValueWithDefault architectureTypeValue;
  209. };
  210. BuildConfiguration::Ptr createBuildConfig (const ValueTree& tree) const override
  211. {
  212. return new CodeBlocksBuildConfiguration (project, tree, *this);
  213. }
  214. //==============================================================================
  215. class CodeBlocksTarget : public ProjectType::Target
  216. {
  217. public:
  218. CodeBlocksTarget (const CodeBlocksProjectExporter& e, ProjectType::Target::Type typeToUse)
  219. : ProjectType::Target (typeToUse),
  220. exporter (e)
  221. {}
  222. String getTargetNameForConfiguration (const BuildConfiguration& config) const
  223. {
  224. if (type == ProjectType::Target::AggregateTarget)
  225. return config.getName();
  226. return getName() + String (" | ") + config.getName();
  227. }
  228. String getTargetSuffix() const
  229. {
  230. auto fileType = getTargetFileType();
  231. if (exporter.isWindows())
  232. {
  233. switch (fileType)
  234. {
  235. case executable: return ".exe";
  236. case staticLibrary: return ".lib";
  237. case sharedLibraryOrDLL:
  238. case pluginBundle: return ".dll";
  239. default:
  240. break;
  241. }
  242. }
  243. else
  244. {
  245. switch (fileType)
  246. {
  247. case executable: return {};
  248. case staticLibrary: return ".a";
  249. case sharedLibraryOrDLL: return ".so";
  250. case pluginBundle:
  251. switch (type)
  252. {
  253. case VST3PlugIn: return ".vst3";
  254. case VSTPlugIn: return ".so";
  255. default: break;
  256. }
  257. return ".so";
  258. default:
  259. break;
  260. }
  261. }
  262. return {};
  263. }
  264. bool isDynamicLibrary() const
  265. {
  266. return (type == DynamicLibrary || type == VST3PlugIn
  267. || type == VSTPlugIn || type == AAXPlugIn);
  268. }
  269. const CodeBlocksProjectExporter& exporter;
  270. };
  271. //==============================================================================
  272. StringArray getPackages() const
  273. {
  274. StringArray result (linuxPackages);
  275. static String guiExtrasModule ("juce_gui_extra");
  276. if (project.getModules().isModuleEnabled (guiExtrasModule)
  277. && project.isConfigFlagEnabled ("JUCE_WEB_BROWSER", true))
  278. {
  279. result.add ("webkit2gtk-4.0");
  280. result.add ("gtk+-x11-3.0");
  281. }
  282. result.removeDuplicates (false);
  283. return result;
  284. }
  285. void addVersion (XmlElement& xml) const
  286. {
  287. XmlElement* fileVersion = xml.createNewChildElement ("FileVersion");
  288. fileVersion->setAttribute ("major", 1);
  289. fileVersion->setAttribute ("minor", 6);
  290. }
  291. void addOptions (XmlElement& xml) const
  292. {
  293. xml.createNewChildElement ("Option")->setAttribute ("title", project.getProjectNameString());
  294. xml.createNewChildElement ("Option")->setAttribute ("pch_mode", 2);
  295. xml.createNewChildElement ("Option")->setAttribute ("compiler", "gcc");
  296. }
  297. StringArray getDefines (const BuildConfiguration& config, CodeBlocksTarget& target) const
  298. {
  299. StringPairArray defines;
  300. if (isWindows())
  301. {
  302. defines.set ("__MINGW__", "1");
  303. defines.set ("__MINGW_EXTENSION", {});
  304. auto targetPlatform = getTargetPlatformString();
  305. if (targetPlatform.isNotEmpty())
  306. defines.set ("WINVER", targetPlatform);
  307. }
  308. else
  309. {
  310. defines.set ("LINUX", "1");
  311. }
  312. if (config.isDebug())
  313. {
  314. defines.set ("DEBUG", "1");
  315. defines.set ("_DEBUG", "1");
  316. }
  317. else
  318. {
  319. defines.set ("NDEBUG", "1");
  320. }
  321. defines = mergePreprocessorDefs (defines, getAllPreprocessorDefs (config, target.type));
  322. StringArray defs;
  323. const auto keys = defines.getAllKeys();
  324. const auto values = defines.getAllValues();
  325. for (int i = 0; i < defines.size(); ++i)
  326. {
  327. auto result = keys[i];
  328. if (values[i].isNotEmpty())
  329. result += "=" + values[i];
  330. defs.add (result);
  331. }
  332. return getCleanedStringArray (defs);
  333. }
  334. StringArray getCompilerFlags (const BuildConfiguration& config, CodeBlocksTarget& target) const
  335. {
  336. StringArray flags;
  337. if (auto* codeBlocksConfig = dynamic_cast<const CodeBlocksBuildConfiguration*> (&config))
  338. flags.add (codeBlocksConfig->getArchitectureTypeString());
  339. flags.add ("-O" + config.getGCCOptimisationFlag());
  340. if (config.isLinkTimeOptimisationEnabled())
  341. flags.add ("-flto");
  342. {
  343. auto cppStandard = config.project.getCppStandardString();
  344. if (cppStandard == "latest")
  345. cppStandard = "1z";
  346. cppStandard = "-std=" + String (shouldUseGNUExtensions() ? "gnu++" : "c++") + cppStandard;
  347. flags.add (cppStandard);
  348. }
  349. flags.add ("-mstackrealign");
  350. if (config.isDebug())
  351. flags.add ("-g");
  352. flags.addTokens (replacePreprocessorTokens (config, getExtraCompilerFlagsString()).trim(),
  353. " \n", "\"'");
  354. if (config.exporter.isLinux())
  355. {
  356. if (target.isDynamicLibrary() || getProject().getProjectType().isAudioPlugin())
  357. flags.add ("-fPIC");
  358. const auto packages = getPackages();
  359. if (packages.size() > 0)
  360. {
  361. auto pkgconfigFlags = String ("`pkg-config --cflags");
  362. for (auto p : packages)
  363. pkgconfigFlags << " " << p;
  364. pkgconfigFlags << "`";
  365. flags.add (pkgconfigFlags);
  366. }
  367. if (linuxLibs.contains("pthread"))
  368. flags.add ("-pthread");
  369. }
  370. return getCleanedStringArray (flags);
  371. }
  372. StringArray getLinkerFlags (const BuildConfiguration& config, CodeBlocksTarget& target) const
  373. {
  374. StringArray flags (makefileExtraLinkerFlags);
  375. if (auto* codeBlocksConfig = dynamic_cast<const CodeBlocksBuildConfiguration*> (&config))
  376. flags.add (codeBlocksConfig->getArchitectureTypeString());
  377. if (! config.isDebug())
  378. flags.add ("-s");
  379. if (config.isLinkTimeOptimisationEnabled())
  380. flags.add ("-flto");
  381. flags.addTokens (replacePreprocessorTokens (config, getExtraLinkerFlagsString()).trim(), " \n", "\"'");
  382. const auto packages = getPackages();
  383. if (config.exporter.isLinux())
  384. {
  385. if (target.isDynamicLibrary())
  386. flags.add ("-shared");
  387. if (packages.size() > 0)
  388. {
  389. String pkgconfigLibs ("`pkg-config --libs");
  390. for (auto& p : packages)
  391. pkgconfigLibs << " " << p;
  392. pkgconfigLibs << "`";
  393. flags.add (pkgconfigLibs);
  394. }
  395. }
  396. return getCleanedStringArray (flags);
  397. }
  398. StringArray getLinkerSearchPaths (const BuildConfiguration& config, CodeBlocksTarget& target) const
  399. {
  400. StringArray librarySearchPaths (config.getLibrarySearchPaths());
  401. if (getProject().getProjectType().isAudioPlugin() && target.type != ProjectType::Target::SharedCodeTarget)
  402. librarySearchPaths.add (RelativePath (getSharedCodePath (config), RelativePath::buildTargetFolder).getParentDirectory().toUnixStyle());
  403. return librarySearchPaths;
  404. }
  405. StringArray getIncludePaths (const BuildConfiguration& config) const
  406. {
  407. StringArray paths;
  408. paths.add (".");
  409. paths.addArray (extraSearchPaths);
  410. paths.addArray (config.getHeaderSearchPaths());
  411. if (! isWindows())
  412. {
  413. paths.add ("/usr/include/freetype2");
  414. // Replace ~ character with $(HOME) environment variable
  415. for (auto& path : paths)
  416. path = path.replace ("~", "$(HOME)");
  417. }
  418. return getCleanedStringArray (paths);
  419. }
  420. static int getTypeIndex (const ProjectType::Target::Type& type)
  421. {
  422. switch (type)
  423. {
  424. case ProjectType::Target::GUIApp:
  425. case ProjectType::Target::StandalonePlugIn:
  426. return 0;
  427. case ProjectType::Target::ConsoleApp:
  428. return 1;
  429. case ProjectType::Target::StaticLibrary:
  430. case ProjectType::Target::SharedCodeTarget:
  431. return 2;
  432. case ProjectType::Target::DynamicLibrary:
  433. case ProjectType::Target::VSTPlugIn:
  434. case ProjectType::Target::VST3PlugIn:
  435. return 3;
  436. default:
  437. break;
  438. }
  439. return 0;
  440. }
  441. String getOutputPathForTarget (CodeBlocksTarget& target, const BuildConfiguration& config) const
  442. {
  443. String outputPath;
  444. if (config.getTargetBinaryRelativePathString().isNotEmpty())
  445. {
  446. RelativePath binaryPath (config.getTargetBinaryRelativePathString(), RelativePath::projectFolder);
  447. binaryPath = binaryPath.rebased (projectFolder, getTargetFolder(), RelativePath::buildTargetFolder);
  448. outputPath = config.getTargetBinaryRelativePathString();
  449. }
  450. else
  451. {
  452. outputPath ="bin/" + File::createLegalFileName (config.getName().trim());
  453. }
  454. return outputPath + "/" + replacePreprocessorTokens (config, config.getTargetBinaryNameString() + target.getTargetSuffix());
  455. }
  456. String getSharedCodePath (const BuildConfiguration& config) const
  457. {
  458. const String outputPath = getOutputPathForTarget (getTargetWithType (ProjectType::Target::SharedCodeTarget), config);
  459. RelativePath path (outputPath, RelativePath::buildTargetFolder);
  460. auto filename = path.getFileName();
  461. if (isLinux())
  462. filename = "lib" + filename;
  463. return path.getParentDirectory().getChildFile (filename).toUnixStyle();
  464. }
  465. void createBuildTarget (XmlElement& xml, CodeBlocksTarget& target, const BuildConfiguration& config) const
  466. {
  467. xml.setAttribute ("title", target.getTargetNameForConfiguration (config));
  468. {
  469. XmlElement* output = xml.createNewChildElement ("Option");
  470. output->setAttribute ("output", getOutputPathForTarget (target, config));
  471. if (isLinux())
  472. {
  473. const bool keepPrefix = (target.type == ProjectType::Target::VSTPlugIn || target.type == ProjectType::Target::VST3PlugIn
  474. || target.type == ProjectType::Target::AAXPlugIn || target.type == ProjectType::Target::RTASPlugIn);
  475. output->setAttribute ("prefix_auto", keepPrefix ? 0 : 1);
  476. }
  477. else
  478. {
  479. output->setAttribute ("prefix_auto", 0);
  480. }
  481. output->setAttribute ("extension_auto", 0);
  482. }
  483. xml.createNewChildElement ("Option")
  484. ->setAttribute ("object_output", "obj/" + File::createLegalFileName (config.getName().trim()));
  485. xml.createNewChildElement ("Option")->setAttribute ("type", getTypeIndex (target.type));
  486. xml.createNewChildElement ("Option")->setAttribute ("compiler", "gcc");
  487. if (getProject().getProjectType().isAudioPlugin() && target.type != ProjectType::Target::SharedCodeTarget)
  488. xml.createNewChildElement ("Option")->setAttribute ("external_deps", getSharedCodePath (config));
  489. {
  490. XmlElement* const compiler = xml.createNewChildElement ("Compiler");
  491. {
  492. StringArray flags;
  493. for (auto& def : getDefines (config, target))
  494. {
  495. if (! def.containsChar ('='))
  496. def << '=';
  497. flags.add ("-D" + def);
  498. }
  499. flags.addArray (getCompilerFlags (config, target));
  500. for (auto flag : flags)
  501. setAddOption (*compiler, "option", flag);
  502. }
  503. {
  504. const StringArray includePaths (getIncludePaths (config));
  505. for (auto path : includePaths)
  506. setAddOption (*compiler, "directory", path);
  507. }
  508. }
  509. {
  510. XmlElement* const linker = xml.createNewChildElement ("Linker");
  511. if (getProject().getProjectType().isAudioPlugin() && target.type != ProjectType::Target::SharedCodeTarget)
  512. setAddOption (*linker, "option", getSharedCodePath (config));
  513. for (auto& flag : getLinkerFlags (config, target))
  514. setAddOption (*linker, "option", flag);
  515. const StringArray& libs = isWindows() ? mingwLibs : linuxLibs;
  516. for (auto lib : libs)
  517. setAddOption (*linker, "library", lib);
  518. for (auto& path : getLinkerSearchPaths (config, target))
  519. setAddOption (*linker, "directory", replacePreprocessorDefs (getAllPreprocessorDefs(), path));
  520. }
  521. }
  522. void addBuild (XmlElement& xml) const
  523. {
  524. XmlElement* const build = xml.createNewChildElement ("Build");
  525. for (ConstConfigIterator config (*this); config.next();)
  526. for (auto target : targets)
  527. if (target->type != ProjectType::Target::AggregateTarget)
  528. createBuildTarget (*build->createNewChildElement ("Target"), *target, *config);
  529. }
  530. void addVirtualTargets (XmlElement& xml) const
  531. {
  532. XmlElement* const virtualTargets = xml.createNewChildElement ("VirtualTargets");
  533. for (ConstConfigIterator config (*this); config.next();)
  534. {
  535. StringArray allTargets;
  536. for (auto target : targets)
  537. if (target->type != ProjectType::Target::AggregateTarget)
  538. allTargets.add (target->getTargetNameForConfiguration (*config));
  539. for (auto target : targets)
  540. {
  541. if (target->type == ProjectType::Target::AggregateTarget)
  542. {
  543. auto* configTarget = virtualTargets->createNewChildElement ("Add");
  544. configTarget->setAttribute ("alias", config->getName());
  545. configTarget->setAttribute ("targets", allTargets.joinIntoString (";"));
  546. }
  547. }
  548. }
  549. }
  550. StringArray getProjectCompilerOptions() const
  551. {
  552. return { "-Wall", "-Wno-strict-aliasing", "-Wno-strict-overflow" };
  553. }
  554. void addProjectCompilerOptions (XmlElement& xml) const
  555. {
  556. XmlElement* const compiler = xml.createNewChildElement ("Compiler");
  557. for (auto& option : getProjectCompilerOptions())
  558. setAddOption (*compiler, "option", option);
  559. }
  560. StringArray getProjectLinkerLibs() const
  561. {
  562. StringArray result;
  563. if (isWindows())
  564. result.addArray ({ "gdi32", "user32", "kernel32", "comctl32" });
  565. result.addTokens (getExternalLibrariesString(), ";\n", "\"'");
  566. result = getCleanedStringArray (result);
  567. for (auto& option : result)
  568. option = replacePreprocessorDefs (getAllPreprocessorDefs(), option);
  569. return result;
  570. }
  571. void addProjectLinkerOptions (XmlElement& xml) const
  572. {
  573. XmlElement* const linker = xml.createNewChildElement ("Linker");
  574. for (auto& lib : getProjectLinkerLibs())
  575. setAddOption (*linker, "library", lib);
  576. }
  577. CodeBlocksTarget& getTargetWithType (ProjectType::Target::Type type) const
  578. {
  579. CodeBlocksTarget* nonAggregrateTarget = nullptr;
  580. for (auto* target : targets)
  581. {
  582. if (target->type == type)
  583. return *target;
  584. if (target->type != ProjectType::Target::AggregateTarget)
  585. nonAggregrateTarget = target;
  586. }
  587. // this project has no valid targets
  588. jassert (nonAggregrateTarget != nullptr);
  589. return *nonAggregrateTarget;
  590. }
  591. // Returns SharedCode target for multi-target projects, otherwise it returns
  592. // the single target
  593. CodeBlocksTarget& getMainTarget() const
  594. {
  595. if (getProject().getProjectType().isAudioPlugin())
  596. return getTargetWithType (ProjectType::Target::SharedCodeTarget);
  597. for (auto* target : targets)
  598. if (target->type != ProjectType::Target::AggregateTarget)
  599. return *target;
  600. jassertfalse;
  601. return *targets[0];
  602. }
  603. CodeBlocksTarget& getTargetForProjectItem (const Project::Item& projectItem) const
  604. {
  605. if (getProject().getProjectType().isAudioPlugin())
  606. {
  607. if (! projectItem.shouldBeCompiled())
  608. return getTargetWithType (ProjectType::Target::SharedCodeTarget);
  609. return getTargetWithType (getProject().getTargetTypeFromFilePath (projectItem.getFile(), true));
  610. }
  611. return getMainTarget();
  612. }
  613. void addCompileUnits (const Project::Item& projectItem, XmlElement& xml) const
  614. {
  615. if (projectItem.isGroup())
  616. {
  617. for (int i = 0; i < projectItem.getNumChildren(); ++i)
  618. addCompileUnits (projectItem.getChild(i), xml);
  619. }
  620. else if (projectItem.shouldBeAddedToTargetProject())
  621. {
  622. const RelativePath file (projectItem.getFile(), getTargetFolder(), RelativePath::buildTargetFolder);
  623. XmlElement* unit = xml.createNewChildElement ("Unit");
  624. unit->setAttribute ("filename", file.toUnixStyle());
  625. for (ConstConfigIterator config (*this); config.next();)
  626. {
  627. const String& targetName = getTargetForProjectItem (projectItem).getTargetNameForConfiguration (*config);
  628. unit->createNewChildElement ("Option")->setAttribute ("target", targetName);
  629. }
  630. if (! projectItem.shouldBeCompiled())
  631. {
  632. unit->createNewChildElement ("Option")->setAttribute ("compile", 0);
  633. unit->createNewChildElement ("Option")->setAttribute ("link", 0);
  634. }
  635. }
  636. }
  637. void addCompileUnits (XmlElement& xml) const
  638. {
  639. for (int i = 0; i < getAllGroups().size(); ++i)
  640. addCompileUnits (getAllGroups().getReference(i), xml);
  641. }
  642. void createProject (XmlElement& xml) const
  643. {
  644. addOptions (xml);
  645. addBuild (xml);
  646. addVirtualTargets (xml);
  647. addProjectCompilerOptions (xml);
  648. addProjectLinkerOptions (xml);
  649. addCompileUnits (xml);
  650. }
  651. void setAddOption (XmlElement& xml, const String& nm, const String& value) const
  652. {
  653. xml.createNewChildElement ("Add")->setAttribute (nm, value);
  654. }
  655. CodeBlocksOS os;
  656. OwnedArray<CodeBlocksTarget> targets;
  657. friend class CLionProjectExporter;
  658. JUCE_DECLARE_NON_COPYABLE (CodeBlocksProjectExporter)
  659. };