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.

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