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.

852 lines
31KB

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