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.

824 lines
31KB

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