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.

833 lines
31KB

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