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.

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