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.

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