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.

847 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. case ProjectType::Target::AAXPlugIn:
  101. case ProjectType::Target::RTASPlugIn:
  102. case ProjectType::Target::UnityPlugIn:
  103. case ProjectType::Target::VST3PlugIn:
  104. case ProjectType::Target::AudioUnitPlugIn:
  105. case ProjectType::Target::AudioUnitv3PlugIn:
  106. case ProjectType::Target::unspecified:
  107. default:
  108. break;
  109. }
  110. return false;
  111. }
  112. void createExporterProperties (PropertyListBuilder& props) override
  113. {
  114. if (isWindows())
  115. {
  116. props.add (new ChoicePropertyComponent (targetPlatformValue, "Target platform",
  117. { "Windows NT 4.0", "Windows 2000", "Windows XP", "Windows Server 2003", "Windows Vista", "Windows Server 2008",
  118. "Windows 7", "Windows 8", "Windows 8.1", "Windows 10" },
  119. { "0x0400", "0x0500", "0x0501", "0x0502", "0x0600", "0x0600",
  120. "0x0601", "0x0602", "0x0603", "0x0A00" }),
  121. "This sets the preprocessor macro WINVER to an appropriate value for the corresponding platform.");
  122. }
  123. }
  124. //==============================================================================
  125. void create (const OwnedArray<LibraryModule>&) const override
  126. {
  127. auto cbpFile = getTargetFolder().getChildFile (project.getProjectFilenameRootString())
  128. .withFileExtension (".cbp");
  129. XmlElement xml ("CodeBlocks_project_file");
  130. addVersion (xml);
  131. createProject (*xml.createNewChildElement ("Project"));
  132. writeXmlOrThrow (xml, cbpFile, "UTF-8", 10, true);
  133. }
  134. //==============================================================================
  135. void addPlatformSpecificSettingsForProjectType (const ProjectType&) override
  136. {
  137. // add shared code target first as order matters for Codeblocks
  138. if (shouldBuildTargetType (ProjectType::Target::SharedCodeTarget))
  139. targets.add (new CodeBlocksTarget (*this, ProjectType::Target::SharedCodeTarget));
  140. //ProjectType::Target::SharedCodeTarget
  141. callForAllSupportedTargets ([this] (ProjectType::Target::Type targetType)
  142. {
  143. if (targetType == ProjectType::Target::SharedCodeTarget)
  144. return;
  145. targets.insert (targetType == ProjectType::Target::AggregateTarget ? 0 : -1,
  146. new CodeBlocksTarget (*this, targetType));
  147. });
  148. // If you hit this assert, you tried to generate a project for an exporter
  149. // that does not support any of your targets!
  150. jassert (targets.size() > 0);
  151. }
  152. void initialiseDependencyPathValues() override
  153. {
  154. auto targetOS = isWindows() ? TargetOS::windows : TargetOS::linux;
  155. vstLegacyPathValueWrapper.init ({ settings, Ids::vstLegacyFolder, nullptr },
  156. getAppSettings().getStoredPath (Ids::vstLegacyPath, targetOS), targetOS);
  157. }
  158. private:
  159. ValueWithDefault targetPlatformValue;
  160. String getTargetPlatformString() const { return targetPlatformValue.get(); }
  161. //==============================================================================
  162. class CodeBlocksBuildConfiguration : public BuildConfiguration
  163. {
  164. public:
  165. CodeBlocksBuildConfiguration (Project& p, const ValueTree& settings, const ProjectExporter& e)
  166. : BuildConfiguration (p, settings, e),
  167. architectureTypeValue (config, exporter.isWindows() ? Ids::windowsCodeBlocksArchitecture
  168. : Ids::linuxCodeBlocksArchitecture, getUndoManager(), "-m64")
  169. {
  170. linkTimeOptimisationValue.setDefault (false);
  171. optimisationLevelValue.setDefault (isDebug() ? gccO0 : gccO3);
  172. }
  173. void createConfigProperties (PropertyListBuilder& props) override
  174. {
  175. addRecommendedLinuxCompilerWarningsProperty (props);
  176. addGCCOptimisationProperty (props);
  177. props.add (new ChoicePropertyComponent (architectureTypeValue, "Architecture",
  178. { "32-bit (-m32)", "64-bit (-m64)", "ARM v6", "ARM v7" },
  179. { "-m32", "-m64", "-march=armv6", "-march=armv7" }),
  180. "Specifies the 32/64-bit architecture to use.");
  181. }
  182. String getModuleLibraryArchName() const override
  183. {
  184. auto archFlag = getArchitectureTypeString();
  185. String prefix ("-march=");
  186. if (archFlag.startsWith (prefix))
  187. return archFlag.substring (prefix.length());
  188. else if (archFlag == "-m64")
  189. return "x86_64";
  190. else if (archFlag == "-m32")
  191. return "i386";
  192. jassertfalse;
  193. return {};
  194. }
  195. String getArchitectureTypeString() const { return architectureTypeValue.get(); }
  196. //==============================================================================
  197. ValueWithDefault architectureTypeValue;
  198. };
  199. BuildConfiguration::Ptr createBuildConfig (const ValueTree& tree) const override
  200. {
  201. return *new CodeBlocksBuildConfiguration (project, tree, *this);
  202. }
  203. //==============================================================================
  204. class CodeBlocksTarget : public ProjectType::Target
  205. {
  206. public:
  207. CodeBlocksTarget (const CodeBlocksProjectExporter& e, ProjectType::Target::Type typeToUse)
  208. : ProjectType::Target (typeToUse),
  209. exporter (e)
  210. {}
  211. String getTargetNameForConfiguration (const BuildConfiguration& config) const
  212. {
  213. if (type == ProjectType::Target::AggregateTarget)
  214. return config.getName();
  215. return getName() + String (" | ") + config.getName();
  216. }
  217. String getTargetSuffix() const
  218. {
  219. auto fileType = getTargetFileType();
  220. if (exporter.isWindows())
  221. {
  222. switch (fileType)
  223. {
  224. case executable: return ".exe";
  225. case staticLibrary: return ".lib";
  226. case sharedLibraryOrDLL:
  227. case pluginBundle: return ".dll";
  228. case macOSAppex:
  229. case unknown:
  230. default:
  231. break;
  232. }
  233. }
  234. else
  235. {
  236. switch (fileType)
  237. {
  238. case executable: return {};
  239. case staticLibrary: return ".a";
  240. case pluginBundle:
  241. case sharedLibraryOrDLL: return ".so";
  242. case macOSAppex:
  243. case unknown:
  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().isAudioPluginProject())
  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().isAudioPluginProject() && 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. if (type == ProjectType::Target::GUIApp || type == ProjectType::Target::StandalonePlugIn) return 0;
  413. if (type == ProjectType::Target::ConsoleApp) return 1;
  414. if (type == ProjectType::Target::StaticLibrary || type == ProjectType::Target::SharedCodeTarget) return 2;
  415. if (type == ProjectType::Target::DynamicLibrary || type == ProjectType::Target::VSTPlugIn) return 3;
  416. return 0;
  417. }
  418. String getOutputPathForTarget (CodeBlocksTarget& target, const BuildConfiguration& config) const
  419. {
  420. String outputPath;
  421. if (config.getTargetBinaryRelativePathString().isNotEmpty())
  422. {
  423. RelativePath binaryPath (config.getTargetBinaryRelativePathString(), RelativePath::projectFolder);
  424. binaryPath = binaryPath.rebased (projectFolder, getTargetFolder(), RelativePath::buildTargetFolder);
  425. outputPath = config.getTargetBinaryRelativePathString();
  426. }
  427. else
  428. {
  429. outputPath ="bin/" + File::createLegalFileName (config.getName().trim());
  430. }
  431. return outputPath + "/" + replacePreprocessorTokens (config, config.getTargetBinaryNameString() + target.getTargetSuffix());
  432. }
  433. String getSharedCodePath (const BuildConfiguration& config) const
  434. {
  435. auto outputPath = getOutputPathForTarget (getTargetWithType (ProjectType::Target::SharedCodeTarget), config);
  436. RelativePath path (outputPath, RelativePath::buildTargetFolder);
  437. auto filename = path.getFileName();
  438. if (isLinux())
  439. filename = "lib" + filename;
  440. return path.getParentDirectory().getChildFile (filename).toUnixStyle();
  441. }
  442. void createBuildTarget (XmlElement& xml, CodeBlocksTarget& target, const BuildConfiguration& config) const
  443. {
  444. xml.setAttribute ("title", target.getTargetNameForConfiguration (config));
  445. {
  446. auto* output = xml.createNewChildElement ("Option");
  447. output->setAttribute ("output", getOutputPathForTarget (target, config));
  448. if (isLinux())
  449. {
  450. bool keepPrefix = (target.type == ProjectType::Target::VSTPlugIn);
  451. output->setAttribute ("prefix_auto", keepPrefix ? 0 : 1);
  452. }
  453. else
  454. {
  455. output->setAttribute ("prefix_auto", 0);
  456. }
  457. output->setAttribute ("extension_auto", 0);
  458. }
  459. xml.createNewChildElement ("Option")
  460. ->setAttribute ("object_output", "obj/" + File::createLegalFileName (config.getName().trim()));
  461. xml.createNewChildElement ("Option")->setAttribute ("type", getTypeIndex (target.type));
  462. xml.createNewChildElement ("Option")->setAttribute ("compiler", "gcc");
  463. if (getProject().isAudioPluginProject() && target.type != ProjectType::Target::SharedCodeTarget)
  464. xml.createNewChildElement ("Option")->setAttribute ("external_deps", getSharedCodePath (config));
  465. {
  466. auto* compiler = xml.createNewChildElement ("Compiler");
  467. {
  468. StringArray flags;
  469. for (auto& def : getDefines (config, target))
  470. {
  471. if (! def.containsChar ('='))
  472. def << '=';
  473. flags.add ("-D" + def);
  474. }
  475. flags.addArray (getCompilerFlags (config, target));
  476. for (auto flag : flags)
  477. setAddOption (*compiler, "option", flag);
  478. }
  479. {
  480. auto includePaths = getIncludePaths (config);
  481. for (auto path : includePaths)
  482. setAddOption (*compiler, "directory", path);
  483. }
  484. }
  485. {
  486. auto* linker = xml.createNewChildElement ("Linker");
  487. if (getProject().isAudioPluginProject() && target.type != ProjectType::Target::SharedCodeTarget)
  488. setAddOption (*linker, "option", getSharedCodePath (config).quoted());
  489. for (auto& flag : getLinkerFlags (config, target))
  490. setAddOption (*linker, "option", flag);
  491. const StringArray& libs = isWindows() ? mingwLibs : linuxLibs;
  492. for (auto lib : libs)
  493. setAddOption (*linker, "library", lib);
  494. for (auto& path : getLinkerSearchPaths (config, target))
  495. setAddOption (*linker, "directory", replacePreprocessorDefs (getAllPreprocessorDefs(), path));
  496. }
  497. }
  498. void addBuild (XmlElement& xml) const
  499. {
  500. auto* build = xml.createNewChildElement ("Build");
  501. for (ConstConfigIterator config (*this); config.next();)
  502. for (auto target : targets)
  503. if (target->type != ProjectType::Target::AggregateTarget)
  504. createBuildTarget (*build->createNewChildElement ("Target"), *target, *config);
  505. }
  506. void addVirtualTargets (XmlElement& xml) const
  507. {
  508. auto* virtualTargets = xml.createNewChildElement ("VirtualTargets");
  509. for (ConstConfigIterator config (*this); config.next();)
  510. {
  511. StringArray allTargets;
  512. for (auto target : targets)
  513. if (target->type != ProjectType::Target::AggregateTarget)
  514. allTargets.add (target->getTargetNameForConfiguration (*config));
  515. for (auto target : targets)
  516. {
  517. if (target->type == ProjectType::Target::AggregateTarget)
  518. {
  519. auto* configTarget = virtualTargets->createNewChildElement ("Add");
  520. configTarget->setAttribute ("alias", config->getName());
  521. configTarget->setAttribute ("targets", allTargets.joinIntoString (";"));
  522. }
  523. }
  524. }
  525. }
  526. StringArray getProjectCompilerOptions() const
  527. {
  528. return { "-Wall", "-Wno-strict-aliasing", "-Wno-strict-overflow" };
  529. }
  530. void addProjectCompilerOptions (XmlElement& xml) const
  531. {
  532. auto* compiler = xml.createNewChildElement ("Compiler");
  533. for (auto& option : getProjectCompilerOptions())
  534. setAddOption (*compiler, "option", option);
  535. }
  536. StringArray getProjectLinkerLibs() const
  537. {
  538. StringArray result;
  539. if (isWindows())
  540. result.addArray ({ "gdi32", "user32", "kernel32", "comctl32" });
  541. result.addTokens (getExternalLibrariesString(), ";\n", "\"'");
  542. result = getCleanedStringArray (result);
  543. for (auto& option : result)
  544. option = replacePreprocessorDefs (getAllPreprocessorDefs(), option);
  545. return result;
  546. }
  547. void addProjectLinkerOptions (XmlElement& xml) const
  548. {
  549. auto* linker = xml.createNewChildElement ("Linker");
  550. for (auto& lib : getProjectLinkerLibs())
  551. setAddOption (*linker, "library", lib);
  552. }
  553. CodeBlocksTarget& getTargetWithType (ProjectType::Target::Type type) const
  554. {
  555. CodeBlocksTarget* nonAggregrateTarget = nullptr;
  556. for (auto* target : targets)
  557. {
  558. if (target->type == type)
  559. return *target;
  560. if (target->type != ProjectType::Target::AggregateTarget)
  561. nonAggregrateTarget = target;
  562. }
  563. // this project has no valid targets
  564. jassert (nonAggregrateTarget != nullptr);
  565. return *nonAggregrateTarget;
  566. }
  567. // Returns SharedCode target for multi-target projects, otherwise it returns
  568. // the single target
  569. CodeBlocksTarget& getMainTarget() const
  570. {
  571. if (getProject().isAudioPluginProject())
  572. return getTargetWithType (ProjectType::Target::SharedCodeTarget);
  573. for (auto* target : targets)
  574. if (target->type != ProjectType::Target::AggregateTarget)
  575. return *target;
  576. jassertfalse;
  577. return *targets[0];
  578. }
  579. CodeBlocksTarget& getTargetForProjectItem (const Project::Item& projectItem) const
  580. {
  581. if (getProject().isAudioPluginProject())
  582. {
  583. if (! projectItem.shouldBeCompiled())
  584. return getTargetWithType (ProjectType::Target::SharedCodeTarget);
  585. return getTargetWithType (getProject().getTargetTypeFromFilePath (projectItem.getFile(), true));
  586. }
  587. return getMainTarget();
  588. }
  589. void addCompileUnits (const Project::Item& projectItem, XmlElement& xml) const
  590. {
  591. if (projectItem.isGroup())
  592. {
  593. for (int i = 0; i < projectItem.getNumChildren(); ++i)
  594. addCompileUnits (projectItem.getChild(i), xml);
  595. }
  596. else if (projectItem.shouldBeAddedToTargetProject() && projectItem.shouldBeAddedToTargetExporter (*this))
  597. {
  598. RelativePath file (projectItem.getFile(), getTargetFolder(), RelativePath::buildTargetFolder);
  599. auto* unit = xml.createNewChildElement ("Unit");
  600. unit->setAttribute ("filename", file.toUnixStyle());
  601. for (ConstConfigIterator config (*this); config.next();)
  602. {
  603. auto targetName = getTargetForProjectItem (projectItem).getTargetNameForConfiguration (*config);
  604. unit->createNewChildElement ("Option")->setAttribute ("target", targetName);
  605. }
  606. if (projectItem.shouldBeCompiled())
  607. {
  608. auto extraCompilerFlags = compilerFlagSchemesMap[projectItem.getCompilerFlagSchemeString()].get().toString();
  609. if (extraCompilerFlags.isNotEmpty())
  610. {
  611. auto* optionElement = unit->createNewChildElement ("Option");
  612. optionElement->setAttribute ("compiler", "gcc");
  613. optionElement->setAttribute ("use", 1);
  614. optionElement->setAttribute ("buildCommand", "$compiler $options " + extraCompilerFlags + " $includes -c $file -o $object");
  615. }
  616. }
  617. else
  618. {
  619. unit->createNewChildElement ("Option")->setAttribute ("compile", 0);
  620. unit->createNewChildElement ("Option")->setAttribute ("link", 0);
  621. }
  622. }
  623. }
  624. bool hasResourceFile() const
  625. {
  626. return ! projectType.isStaticLibrary();
  627. }
  628. void addCompileUnits (XmlElement& xml) const
  629. {
  630. for (int i = 0; i < getAllGroups().size(); ++i)
  631. addCompileUnits (getAllGroups().getReference(i), xml);
  632. if (hasResourceFile())
  633. {
  634. auto iconFile = getTargetFolder().getChildFile ("icon.ico");
  635. MSVCProjectExporterBase::writeIconFile (*this, iconFile);
  636. auto rcFile = getTargetFolder().getChildFile ("resources.rc");
  637. MSVCProjectExporterBase::createRCFile (project, iconFile, rcFile);
  638. auto* unit = xml.createNewChildElement ("Unit");
  639. unit->setAttribute ("filename", rcFile.getFileName());
  640. unit->createNewChildElement ("Option")->setAttribute ("compilerVar", "WINDRES");
  641. }
  642. }
  643. void createProject (XmlElement& xml) const
  644. {
  645. addOptions (xml);
  646. addBuild (xml);
  647. addVirtualTargets (xml);
  648. addProjectCompilerOptions (xml);
  649. addProjectLinkerOptions (xml);
  650. addCompileUnits (xml);
  651. }
  652. void setAddOption (XmlElement& xml, const String& nm, const String& value) const
  653. {
  654. xml.createNewChildElement ("Add")->setAttribute (nm, value);
  655. }
  656. CodeBlocksOS os;
  657. OwnedArray<CodeBlocksTarget> targets;
  658. friend class CLionProjectExporter;
  659. JUCE_DECLARE_NON_COPYABLE (CodeBlocksProjectExporter)
  660. };