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.

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