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