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.

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