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.

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