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.

992 lines
38KB

  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::LV2Helper:
  102. case Target::VST3PlugIn:
  103. case Target::VST3Helper:
  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 Vista", "Windows Server 2008", "Windows 7", "Windows 8", "Windows 8.1", "Windows 10" },
  118. { "0x0600", "0x0600", "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. linuxSubprocessHelperProperties.deployLinuxSubprocessHelperSourceFilesIfNecessary();
  132. }
  133. //==============================================================================
  134. void addPlatformSpecificSettingsForProjectType (const build_tools::ProjectType&) override
  135. {
  136. linuxSubprocessHelperProperties.addToExtraSearchPathsIfNecessary();
  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. if (archFlag == "-m64")
  189. return "x86_64";
  190. 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. enum class IsDynamicLibrary
  205. {
  206. no,
  207. yes
  208. };
  209. enum class ShouldBeCompiled
  210. {
  211. no,
  212. yes
  213. };
  214. class CodeBlocksTarget : public build_tools::ProjectType::Target
  215. {
  216. public:
  217. CodeBlocksTarget (const CodeBlocksProjectExporter& e,
  218. build_tools::ProjectType::Target::Type typeToUse)
  219. : Target (typeToUse),
  220. exporter (e)
  221. {}
  222. String getTargetNameForConfiguration (const BuildConfiguration& config) const
  223. {
  224. if (type == build_tools::ProjectType::Target::AggregateTarget)
  225. return config.getName();
  226. return getName() + String (" | ") + config.getName();
  227. }
  228. String getTargetSuffix() const
  229. {
  230. auto fileType = getTargetFileType();
  231. if (exporter.isWindows())
  232. {
  233. switch (fileType)
  234. {
  235. case executable: return ".exe";
  236. case staticLibrary: return ".lib";
  237. case sharedLibraryOrDLL:
  238. case pluginBundle: return ".dll";
  239. case macOSAppex:
  240. case unknown:
  241. default:
  242. break;
  243. }
  244. }
  245. else
  246. {
  247. switch (fileType)
  248. {
  249. case executable: return {};
  250. case staticLibrary: return ".a";
  251. case pluginBundle:
  252. case sharedLibraryOrDLL: return ".so";
  253. case macOSAppex:
  254. case unknown:
  255. default:
  256. break;
  257. }
  258. }
  259. return {};
  260. }
  261. IsDynamicLibrary isDynamicLibrary() const
  262. {
  263. return (type == DynamicLibrary || type == VSTPlugIn) ? IsDynamicLibrary::yes
  264. : IsDynamicLibrary::no;
  265. }
  266. const CodeBlocksProjectExporter& exporter;
  267. };
  268. //==============================================================================
  269. class LinuxSubprocessHelperTarget
  270. {
  271. public:
  272. enum class HelperType
  273. {
  274. linuxSubprocessHelper,
  275. simpleBinaryBuilder
  276. };
  277. LinuxSubprocessHelperTarget (const CodeBlocksProjectExporter& exporter, HelperType helperTypeIn)
  278. : owner (exporter),
  279. helperType (helperTypeIn)
  280. {
  281. }
  282. String getTargetNameForConfiguration (const BuildConfiguration& config) const
  283. {
  284. return getName (helperType) + " | " + config.getName();
  285. }
  286. void addTarget (XmlElement& xml, const BuildConfiguration& config) const
  287. {
  288. xml.setAttribute ("title", getTargetNameForConfiguration (config));
  289. auto* output = xml.createNewChildElement ("Option");
  290. output->setAttribute ("output", getOutput (helperType, config));
  291. xml.createNewChildElement ("Option")->setAttribute ("object_output",
  292. "obj/" + File::createLegalFileName (config.getName().trim()));
  293. xml.createNewChildElement ("Option")->setAttribute ("type", getTypeIndex (type));
  294. xml.createNewChildElement ("Option")->setAttribute ("compiler", "gcc");
  295. const auto isDynamicLibrary = IsDynamicLibrary::no;
  296. {
  297. auto* compiler = xml.createNewChildElement ("Compiler");
  298. for (auto flag : owner.getCompilerFlags (config, isDynamicLibrary))
  299. owner.setAddOption (*compiler, "option", flag);
  300. }
  301. {
  302. auto* linker = xml.createNewChildElement ("Linker");
  303. for (auto& flag : owner.getLinkerFlags (config, isDynamicLibrary))
  304. owner.setAddOption (*linker, "option", flag);
  305. }
  306. if (helperType == HelperType::simpleBinaryBuilder)
  307. {
  308. auto* postBuildCommands = xml.createNewChildElement ("ExtraCommands");
  309. const auto binaryDataSource = owner.linuxSubprocessHelperProperties
  310. .getLinuxSubprocessHelperBinaryDataSource();
  311. owner.setAddOption (*postBuildCommands,
  312. "after",
  313. getOutput (HelperType::simpleBinaryBuilder, config)
  314. + " " + getOutput (HelperType::linuxSubprocessHelper, config)
  315. + " pre_build"
  316. + " " + binaryDataSource.getFileNameWithoutExtension().quoted()
  317. + " LinuxSubprocessHelperBinaryData");
  318. }
  319. }
  320. void addCompileUnits (XmlElement& xml) const
  321. {
  322. const auto file = getSource (helperType);
  323. auto* unit = xml.createNewChildElement ("Unit");
  324. unit->setAttribute ("filename", file.toUnixStyle());
  325. for (ConstConfigIterator config (owner); config.next();)
  326. {
  327. auto targetName = getTargetNameForConfiguration (*config);
  328. unit->createNewChildElement ("Option")->setAttribute ("target", targetName);
  329. }
  330. }
  331. private:
  332. build_tools::RelativePath getSource (HelperType helperTypeForSource) const
  333. {
  334. if (helperTypeForSource == HelperType::linuxSubprocessHelper)
  335. return owner.linuxSubprocessHelperProperties.getLinuxSubprocessHelperSource();
  336. return owner.linuxSubprocessHelperProperties.getSimpleBinaryBuilderSource();
  337. }
  338. String getName (HelperType helperTypeForName) const
  339. {
  340. return LinuxSubprocessHelperProperties::getBinaryNameFromSource (getSource (helperTypeForName));
  341. }
  342. String getOutput (HelperType helperTypeForOutput, const BuildConfiguration& config) const
  343. {
  344. return owner.getOutputPathForConfig (config) + "/" + getName (helperTypeForOutput);
  345. }
  346. const CodeBlocksProjectExporter& owner;
  347. HelperType helperType;
  348. build_tools::ProjectType::Target::Type type = build_tools::ProjectType::Target::ConsoleApp;
  349. };
  350. void addSubprocessHelperBinarySourceCompileUnit (XmlElement& xml) const
  351. {
  352. auto* unit = xml.createNewChildElement ("Unit");
  353. const auto binaryDataSource = linuxSubprocessHelperProperties.getLinuxSubprocessHelperBinaryDataSource();
  354. unit->setAttribute ("filename", binaryDataSource.toUnixStyle());
  355. for (ConstConfigIterator config (*this); config.next();)
  356. {
  357. const auto& target = getTargetForFile (resolveRelativePath (binaryDataSource), ShouldBeCompiled::yes);
  358. const auto targetName = target.getTargetNameForConfiguration (*config);
  359. unit->createNewChildElement ("Option")->setAttribute ("target", targetName);
  360. }
  361. }
  362. //==============================================================================
  363. void addVersion (XmlElement& xml) const
  364. {
  365. auto* fileVersion = xml.createNewChildElement ("FileVersion");
  366. fileVersion->setAttribute ("major", 1);
  367. fileVersion->setAttribute ("minor", 6);
  368. }
  369. void addOptions (XmlElement& xml) const
  370. {
  371. xml.createNewChildElement ("Option")->setAttribute ("title", project.getProjectNameString());
  372. xml.createNewChildElement ("Option")->setAttribute ("pch_mode", 2);
  373. xml.createNewChildElement ("Option")->setAttribute ("compiler", "gcc");
  374. }
  375. StringArray getDefines (const BuildConfiguration& config, CodeBlocksTarget& target) const
  376. {
  377. StringPairArray defines;
  378. if (isWindows())
  379. {
  380. defines.set ("__MINGW__", "1");
  381. defines.set ("__MINGW_EXTENSION", {});
  382. auto targetPlatform = getTargetPlatformString();
  383. if (targetPlatform.isNotEmpty())
  384. defines.set ("WINVER", targetPlatform);
  385. }
  386. else
  387. {
  388. defines.set ("LINUX", "1");
  389. }
  390. if (config.isDebug())
  391. {
  392. defines.set ("DEBUG", "1");
  393. defines.set ("_DEBUG", "1");
  394. }
  395. else
  396. {
  397. defines.set ("NDEBUG", "1");
  398. }
  399. defines = mergePreprocessorDefs (defines, getAllPreprocessorDefs (config, target.type));
  400. StringArray defs;
  401. auto keys = defines.getAllKeys();
  402. auto values = defines.getAllValues();
  403. const auto escapedQuote = isWindows() ? "\\\"" : "\\\\\"";
  404. for (int i = 0; i < defines.size(); ++i)
  405. {
  406. auto result = keys[i];
  407. if (values[i].isNotEmpty())
  408. result += "=\"" + values[i].replace ("\"", escapedQuote) + "\"";
  409. defs.add (result);
  410. }
  411. return getCleanedStringArray (defs);
  412. }
  413. StringArray getCompilerFlags (const BuildConfiguration& config, IsDynamicLibrary isDynamicLibrary) const
  414. {
  415. StringArray flags;
  416. if (auto* codeBlocksConfig = dynamic_cast<const CodeBlocksBuildConfiguration*> (&config))
  417. flags.add (codeBlocksConfig->getArchitectureTypeString());
  418. auto recommendedFlags = config.getRecommendedCompilerWarningFlags();
  419. for (auto& recommendedFlagsType : { recommendedFlags.common, recommendedFlags.cpp })
  420. for (auto& recommended : recommendedFlagsType)
  421. flags.add (recommended);
  422. flags.add ("-O" + config.getGCCOptimisationFlag());
  423. if (config.isLinkTimeOptimisationEnabled())
  424. flags.add ("-flto");
  425. {
  426. auto cppStandard = config.project.getCppStandardString();
  427. if (cppStandard == "latest")
  428. cppStandard = project.getLatestNumberedCppStandardString();
  429. flags.add ("-std=" + String (shouldUseGNUExtensions() ? "gnu++" : "c++") + cppStandard);
  430. }
  431. flags.add ("-mstackrealign");
  432. if (config.isDebug())
  433. flags.add ("-g");
  434. flags.addTokens (replacePreprocessorTokens (config, config.getAllCompilerFlagsString()).trim(),
  435. " \n", "\"'");
  436. if (config.exporter.isLinux())
  437. {
  438. if (isDynamicLibrary == IsDynamicLibrary::yes || getProject().isAudioPluginProject())
  439. flags.add ("-fPIC");
  440. auto packages = config.exporter.getLinuxPackages (PackageDependencyType::compile);
  441. if (! packages.isEmpty())
  442. {
  443. auto pkgconfigFlags = String ("`pkg-config --cflags");
  444. for (auto& p : packages)
  445. pkgconfigFlags << " " << p;
  446. pkgconfigFlags << "`";
  447. flags.add (pkgconfigFlags);
  448. }
  449. if (linuxLibs.contains ("pthread"))
  450. flags.add ("-pthread");
  451. }
  452. return getCleanedStringArray (flags);
  453. }
  454. StringArray getLinkerFlags (const BuildConfiguration& config, IsDynamicLibrary isDynamicLibrary) const
  455. {
  456. auto flags = makefileExtraLinkerFlags;
  457. if (auto* codeBlocksConfig = dynamic_cast<const CodeBlocksBuildConfiguration*> (&config))
  458. flags.add (codeBlocksConfig->getArchitectureTypeString());
  459. if (! config.isDebug())
  460. flags.add ("-s");
  461. if (config.isLinkTimeOptimisationEnabled())
  462. flags.add ("-flto");
  463. flags.addTokens (replacePreprocessorTokens (config, config.getAllLinkerFlagsString()).trim(), " \n", "\"'");
  464. if (config.exporter.isLinux())
  465. {
  466. if (isDynamicLibrary == IsDynamicLibrary::yes)
  467. flags.add ("-shared");
  468. auto packages = config.exporter.getLinuxPackages (PackageDependencyType::link);
  469. if (! packages.isEmpty())
  470. {
  471. String pkgconfigLibs ("`pkg-config --libs");
  472. for (auto& p : packages)
  473. pkgconfigLibs << " " << p;
  474. pkgconfigLibs << "`";
  475. flags.add (pkgconfigLibs);
  476. }
  477. }
  478. return getCleanedStringArray (flags);
  479. }
  480. StringArray getLinkerSearchPaths (const BuildConfiguration& config, CodeBlocksTarget& target) const
  481. {
  482. auto librarySearchPaths = config.getLibrarySearchPaths();
  483. if (getProject().isAudioPluginProject() && target.type != build_tools::ProjectType::Target::SharedCodeTarget)
  484. librarySearchPaths.add (build_tools::RelativePath (getSharedCodePath (config), build_tools::RelativePath::buildTargetFolder).getParentDirectory().toUnixStyle().quoted());
  485. return librarySearchPaths;
  486. }
  487. StringArray getIncludePaths (const BuildConfiguration& config) const
  488. {
  489. StringArray paths;
  490. paths.add (".");
  491. paths.addArray (extraSearchPaths);
  492. paths.addArray (config.getHeaderSearchPaths());
  493. if (! isWindows())
  494. {
  495. paths.add ("/usr/include/freetype2");
  496. // Replace ~ character with $(HOME) environment variable
  497. for (auto& path : paths)
  498. path = path.replace ("~", "$(HOME)");
  499. }
  500. return getCleanedStringArray (paths);
  501. }
  502. static int getTypeIndex (const build_tools::ProjectType::Target::Type& type)
  503. {
  504. if (type == build_tools::ProjectType::Target::GUIApp || type == build_tools::ProjectType::Target::StandalonePlugIn) return 0;
  505. if (type == build_tools::ProjectType::Target::ConsoleApp) return 1;
  506. if (type == build_tools::ProjectType::Target::StaticLibrary || type == build_tools::ProjectType::Target::SharedCodeTarget) return 2;
  507. if (type == build_tools::ProjectType::Target::DynamicLibrary || type == build_tools::ProjectType::Target::VSTPlugIn) return 3;
  508. return 0;
  509. }
  510. String getOutputPathForConfig (const BuildConfiguration& config) const
  511. {
  512. if (config.getTargetBinaryRelativePathString().isNotEmpty())
  513. {
  514. build_tools::RelativePath binaryPath (config.getTargetBinaryRelativePathString(), build_tools::RelativePath::projectFolder);
  515. binaryPath = binaryPath.rebased (projectFolder, getTargetFolder(), build_tools::RelativePath::buildTargetFolder);
  516. return config.getTargetBinaryRelativePathString();
  517. }
  518. return "bin/" + File::createLegalFileName (config.getName().trim());
  519. }
  520. String getOutputPathForTarget (CodeBlocksTarget& target, const BuildConfiguration& config) const
  521. {
  522. return getOutputPathForConfig (config)
  523. + "/"
  524. + replacePreprocessorTokens (config, config.getTargetBinaryNameString() + target.getTargetSuffix());
  525. }
  526. String getSharedCodePath (const BuildConfiguration& config) const
  527. {
  528. auto outputPath = getOutputPathForTarget (getTargetWithType (build_tools::ProjectType::Target::SharedCodeTarget), config);
  529. build_tools::RelativePath path (outputPath, build_tools::RelativePath::buildTargetFolder);
  530. auto filename = path.getFileName();
  531. if (isLinux())
  532. filename = "lib" + filename;
  533. return path.getParentDirectory().getChildFile (filename).toUnixStyle();
  534. }
  535. void createBuildTarget (XmlElement& xml, CodeBlocksTarget& target, const BuildConfiguration& config) const
  536. {
  537. xml.setAttribute ("title", target.getTargetNameForConfiguration (config));
  538. {
  539. auto* output = xml.createNewChildElement ("Option");
  540. output->setAttribute ("output", getOutputPathForTarget (target, config));
  541. if (isLinux())
  542. {
  543. bool keepPrefix = (target.type == build_tools::ProjectType::Target::VSTPlugIn);
  544. output->setAttribute ("prefix_auto", keepPrefix ? 0 : 1);
  545. }
  546. else
  547. {
  548. output->setAttribute ("prefix_auto", 0);
  549. }
  550. output->setAttribute ("extension_auto", 0);
  551. }
  552. xml.createNewChildElement ("Option")
  553. ->setAttribute ("object_output", "obj/" + File::createLegalFileName (config.getName().trim()));
  554. xml.createNewChildElement ("Option")->setAttribute ("type", getTypeIndex (target.type));
  555. xml.createNewChildElement ("Option")->setAttribute ("compiler", "gcc");
  556. if (getProject().isAudioPluginProject() && target.type != build_tools::ProjectType::Target::SharedCodeTarget)
  557. xml.createNewChildElement ("Option")->setAttribute ("external_deps", getSharedCodePath (config));
  558. {
  559. auto* compiler = xml.createNewChildElement ("Compiler");
  560. {
  561. StringArray flags;
  562. for (auto& def : getDefines (config, target))
  563. {
  564. if (! def.containsChar ('='))
  565. def << '=';
  566. flags.add ("-D" + def);
  567. }
  568. flags.addArray (getCompilerFlags (config, target.isDynamicLibrary()));
  569. for (auto flag : flags)
  570. setAddOption (*compiler, "option", flag);
  571. }
  572. {
  573. auto includePaths = getIncludePaths (config);
  574. for (auto path : includePaths)
  575. setAddOption (*compiler, "directory", path);
  576. }
  577. }
  578. {
  579. auto* linker = xml.createNewChildElement ("Linker");
  580. if (getProject().isAudioPluginProject() && target.type != build_tools::ProjectType::Target::SharedCodeTarget)
  581. setAddOption (*linker, "option", getSharedCodePath (config).quoted());
  582. for (auto& flag : getLinkerFlags (config, target.isDynamicLibrary()))
  583. setAddOption (*linker, "option", flag);
  584. const StringArray& libs = isWindows() ? mingwLibs : linuxLibs;
  585. for (auto lib : libs)
  586. setAddOption (*linker, "library", lib);
  587. for (auto& path : getLinkerSearchPaths (config, target))
  588. setAddOption (*linker, "directory",
  589. build_tools::replacePreprocessorDefs (getAllPreprocessorDefs(), path));
  590. }
  591. }
  592. void addBuild (XmlElement& xml) const
  593. {
  594. auto* build = xml.createNewChildElement ("Build");
  595. for (ConstConfigIterator config (*this); config.next();)
  596. {
  597. if (linuxSubprocessHelperProperties.shouldUseLinuxSubprocessHelper())
  598. for (const auto& helperTarget : helperTargets)
  599. helperTarget.addTarget (*build->createNewChildElement ("Target"), *config);
  600. for (auto target : targets)
  601. if (target->type != build_tools::ProjectType::Target::AggregateTarget)
  602. createBuildTarget (*build->createNewChildElement ("Target"), *target, *config);
  603. }
  604. }
  605. void addVirtualTargets (XmlElement& xml) const
  606. {
  607. auto* virtualTargets = xml.createNewChildElement ("VirtualTargets");
  608. for (ConstConfigIterator config (*this); config.next();)
  609. {
  610. StringArray allTargets;
  611. if (linuxSubprocessHelperProperties.shouldUseLinuxSubprocessHelper())
  612. for (const auto& target : helperTargets)
  613. allTargets.add (target.getTargetNameForConfiguration (*config));
  614. for (auto target : targets)
  615. if (target->type != build_tools::ProjectType::Target::AggregateTarget)
  616. allTargets.add (target->getTargetNameForConfiguration (*config));
  617. for (auto target : targets)
  618. {
  619. if (target->type == build_tools::ProjectType::Target::AggregateTarget)
  620. {
  621. auto* configTarget = virtualTargets->createNewChildElement ("Add");
  622. configTarget->setAttribute ("alias", config->getName());
  623. configTarget->setAttribute ("targets", allTargets.joinIntoString (";"));
  624. }
  625. }
  626. }
  627. }
  628. StringArray getProjectCompilerOptions() const
  629. {
  630. return { "-Wall", "-Wno-strict-aliasing", "-Wno-strict-overflow" };
  631. }
  632. void addProjectCompilerOptions (XmlElement& xml) const
  633. {
  634. auto* compiler = xml.createNewChildElement ("Compiler");
  635. for (auto& option : getProjectCompilerOptions())
  636. setAddOption (*compiler, "option", option);
  637. }
  638. StringArray getProjectLinkerLibs() const
  639. {
  640. StringArray result;
  641. if (isWindows())
  642. result.addArray ({ "gdi32", "user32", "kernel32", "comctl32" });
  643. result.addTokens (getExternalLibrariesString(), ";\n", "\"'");
  644. result = getCleanedStringArray (result);
  645. for (auto& option : result)
  646. option = build_tools::replacePreprocessorDefs (getAllPreprocessorDefs(), option);
  647. return result;
  648. }
  649. void addProjectLinkerOptions (XmlElement& xml) const
  650. {
  651. auto* linker = xml.createNewChildElement ("Linker");
  652. for (auto& lib : getProjectLinkerLibs())
  653. setAddOption (*linker, "library", lib);
  654. }
  655. CodeBlocksTarget& getTargetWithType (build_tools::ProjectType::Target::Type type) const
  656. {
  657. CodeBlocksTarget* nonAggregrateTarget = nullptr;
  658. for (auto* target : targets)
  659. {
  660. if (target->type == type)
  661. return *target;
  662. if (target->type != build_tools::ProjectType::Target::AggregateTarget)
  663. nonAggregrateTarget = target;
  664. }
  665. // this project has no valid targets
  666. jassert (nonAggregrateTarget != nullptr);
  667. return *nonAggregrateTarget;
  668. }
  669. // Returns SharedCode target for multi-target projects, otherwise it returns
  670. // the single target
  671. CodeBlocksTarget& getMainTarget() const
  672. {
  673. if (getProject().isAudioPluginProject())
  674. return getTargetWithType (build_tools::ProjectType::Target::SharedCodeTarget);
  675. for (auto* target : targets)
  676. if (target->type != build_tools::ProjectType::Target::AggregateTarget)
  677. return *target;
  678. jassertfalse;
  679. return *targets[0];
  680. }
  681. CodeBlocksTarget& getTargetForFile (const File& file, ShouldBeCompiled shouldBeCompiled) const
  682. {
  683. if (getProject().isAudioPluginProject())
  684. {
  685. if (shouldBeCompiled == ShouldBeCompiled::no)
  686. return getTargetWithType (build_tools::ProjectType::Target::SharedCodeTarget);
  687. return getTargetWithType (getProject().getTargetTypeFromFilePath (file, true));
  688. }
  689. return getMainTarget();
  690. }
  691. CodeBlocksTarget& getTargetForProjectItem (const Project::Item& projectItem) const
  692. {
  693. return getTargetForFile (projectItem.getFile(),
  694. projectItem.shouldBeCompiled() ? ShouldBeCompiled::yes
  695. : ShouldBeCompiled::no);
  696. }
  697. void addCompileUnits (const Project::Item& projectItem, XmlElement& xml) const
  698. {
  699. if (projectItem.isGroup())
  700. {
  701. for (int i = 0; i < projectItem.getNumChildren(); ++i)
  702. addCompileUnits (projectItem.getChild(i), xml);
  703. }
  704. else if (projectItem.shouldBeAddedToTargetProject() && projectItem.shouldBeAddedToTargetExporter (*this))
  705. {
  706. build_tools::RelativePath file (projectItem.getFile(), getTargetFolder(), build_tools::RelativePath::buildTargetFolder);
  707. auto* unit = xml.createNewChildElement ("Unit");
  708. unit->setAttribute ("filename", file.toUnixStyle());
  709. for (ConstConfigIterator config (*this); config.next();)
  710. {
  711. auto targetName = getTargetForProjectItem (projectItem).getTargetNameForConfiguration (*config);
  712. unit->createNewChildElement ("Option")->setAttribute ("target", targetName);
  713. }
  714. if (projectItem.shouldBeCompiled())
  715. {
  716. auto extraCompilerFlags = getCompilerFlagsForProjectItem (projectItem);
  717. if (extraCompilerFlags.isNotEmpty())
  718. {
  719. auto* optionElement = unit->createNewChildElement ("Option");
  720. optionElement->setAttribute ("compiler", "gcc");
  721. optionElement->setAttribute ("use", 1);
  722. optionElement->setAttribute ("buildCommand", "$compiler $options " + extraCompilerFlags + " $includes -c $file -o $object");
  723. }
  724. }
  725. else
  726. {
  727. unit->createNewChildElement ("Option")->setAttribute ("compile", 0);
  728. unit->createNewChildElement ("Option")->setAttribute ("link", 0);
  729. }
  730. }
  731. }
  732. bool hasResourceFile() const
  733. {
  734. return ! projectType.isStaticLibrary();
  735. }
  736. void addCompileUnits (XmlElement& xml) const
  737. {
  738. if (linuxSubprocessHelperProperties.shouldUseLinuxSubprocessHelper())
  739. {
  740. for (const auto& helperTarget : helperTargets)
  741. helperTarget.addCompileUnits (xml);
  742. addSubprocessHelperBinarySourceCompileUnit (xml);
  743. }
  744. for (int i = 0; i < getAllGroups().size(); ++i)
  745. addCompileUnits (getAllGroups().getReference(i), xml);
  746. if (hasResourceFile())
  747. {
  748. const auto iconFile = getTargetFolder().getChildFile ("icon.ico");
  749. if (! build_tools::asArray (getIcons()).isEmpty())
  750. build_tools::writeWinIcon (getIcons(), iconFile);
  751. auto rcFile = getTargetFolder().getChildFile ("resources.rc");
  752. MSVCProjectExporterBase::createRCFile (project, iconFile, rcFile);
  753. auto* unit = xml.createNewChildElement ("Unit");
  754. unit->setAttribute ("filename", rcFile.getFileName());
  755. unit->createNewChildElement ("Option")->setAttribute ("compilerVar", "WINDRES");
  756. }
  757. }
  758. void createProject (XmlElement& xml) const
  759. {
  760. addOptions (xml);
  761. addBuild (xml);
  762. addVirtualTargets (xml);
  763. addProjectCompilerOptions (xml);
  764. addProjectLinkerOptions (xml);
  765. addCompileUnits (xml);
  766. }
  767. void setAddOption (XmlElement& xml, const String& nm, const String& value) const
  768. {
  769. xml.createNewChildElement ("Add")->setAttribute (nm, value);
  770. }
  771. CodeBlocksOS os;
  772. OwnedArray<CodeBlocksTarget> targets;
  773. // The order of these targets is significant, as latter targets depend on earlier ones
  774. const LinuxSubprocessHelperTarget helperTargets[2] { { *this, LinuxSubprocessHelperTarget::HelperType::linuxSubprocessHelper },
  775. { *this, LinuxSubprocessHelperTarget::HelperType::simpleBinaryBuilder } };
  776. JUCE_DECLARE_NON_COPYABLE (CodeBlocksProjectExporter)
  777. };