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.

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