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.

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