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.

829 lines
32KB

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