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.

855 lines
31KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  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 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. #pragma once
  20. //==============================================================================
  21. class CodeBlocksProjectExporter : public ProjectExporter
  22. {
  23. public:
  24. enum CodeBlocksOS
  25. {
  26. windowsTarget,
  27. linuxTarget
  28. };
  29. //==============================================================================
  30. static const char* getNameWindows() noexcept { return "Code::Blocks (Windows)"; }
  31. static const char* getNameLinux() noexcept { return "Code::Blocks (Linux)"; }
  32. static const char* getName (CodeBlocksOS os) noexcept
  33. {
  34. if (os == windowsTarget) return getNameWindows();
  35. if (os == linuxTarget) return getNameLinux();
  36. // currently no other OSes supported by Codeblocks exporter!
  37. jassertfalse;
  38. return "Code::Blocks (Unknown OS)";
  39. }
  40. //==============================================================================
  41. static const char* getValueTreeTypeName (CodeBlocksOS os)
  42. {
  43. if (os == windowsTarget) return "CODEBLOCKS_WINDOWS";
  44. if (os == linuxTarget) return "CODEBLOCKS_LINUX";
  45. // currently no other OSes supported by Codeblocks exporter!
  46. jassertfalse;
  47. return "CODEBLOCKS_UNKNOWN_OS";
  48. }
  49. //==============================================================================
  50. static String getTargetFolderName (CodeBlocksOS os)
  51. {
  52. if (os == windowsTarget) return "CodeBlocksWindows";
  53. if (os == linuxTarget) return "CodeBlocksLinux";
  54. // currently no other OSes supported by Codeblocks exporter!
  55. jassertfalse;
  56. return "CodeBlocksUnknownOS";
  57. }
  58. //==============================================================================
  59. static CodeBlocksProjectExporter* createForSettings (Project& project, const ValueTree& settings)
  60. {
  61. // this will also import legacy jucer files where CodeBlocks only worked for Windows,
  62. // had valueTreetTypeName "CODEBLOCKS", and there was no OS distinction
  63. if (settings.hasType (getValueTreeTypeName (windowsTarget)) || settings.hasType ("CODEBLOCKS"))
  64. return new CodeBlocksProjectExporter (project, settings, windowsTarget);
  65. if (settings.hasType (getValueTreeTypeName (linuxTarget)))
  66. return new CodeBlocksProjectExporter (project, settings, linuxTarget);
  67. return nullptr;
  68. }
  69. //==============================================================================
  70. CodeBlocksProjectExporter (Project& p, const ValueTree& t, CodeBlocksOS codeBlocksOs)
  71. : ProjectExporter (p, t), os (codeBlocksOs)
  72. {
  73. name = getName (os);
  74. if (getTargetLocationString().isEmpty())
  75. getTargetLocationValue() = getDefaultBuildsRootFolder() + getTargetFolderName (os);
  76. }
  77. //==============================================================================
  78. bool canLaunchProject() override { return false; }
  79. bool launchProject() override { return false; }
  80. bool usesMMFiles() const override { return false; }
  81. bool canCopeWithDuplicateFiles() override { return false; }
  82. bool supportsUserDefinedConfigurations() const override { return true; }
  83. bool isXcode() const override { return false; }
  84. bool isVisualStudio() const override { return false; }
  85. bool isCodeBlocks() const override { return true; }
  86. bool isMakefile() const override { return false; }
  87. bool isAndroidStudio() const override { return false; }
  88. bool isCLion() const override { return false; }
  89. bool isAndroid() const override { return false; }
  90. bool isWindows() const override { return os == windowsTarget; }
  91. bool isLinux() const override { return os == linuxTarget; }
  92. bool isOSX() const override { return false; }
  93. bool isiOS() const override { return false; }
  94. bool supportsTargetType (ProjectType::Target::Type type) const override
  95. {
  96. switch (type)
  97. {
  98. case ProjectType::Target::StandalonePlugIn:
  99. case ProjectType::Target::GUIApp:
  100. case ProjectType::Target::ConsoleApp:
  101. case ProjectType::Target::StaticLibrary:
  102. case ProjectType::Target::SharedCodeTarget:
  103. case ProjectType::Target::AggregateTarget:
  104. case ProjectType::Target::VSTPlugIn:
  105. case ProjectType::Target::DynamicLibrary:
  106. return true;
  107. default:
  108. break;
  109. }
  110. return false;
  111. }
  112. void createExporterProperties (PropertyListBuilder& props) override
  113. {
  114. if (isWindows())
  115. {
  116. StringArray toolsetNames = { "(default)", "Windows NT 4.0", "Windows 2000", "Windows XP", "Windows Server 2003", "Windows Vista", "Windows Server 2008", "Windows 7", "Windows 8", "Windows 8.1", "Windows 10" };
  117. Array<var> toolsets = { var(), "0x0400", "0x0500", "0x0501", "0x0502", "0x0600", "0x0600", "0x0601", "0x0602", "0x0603", "0x0A00" };
  118. props.add (new ChoicePropertyComponent (getTargetPlatformValue(), "Target platform", toolsetNames, toolsets),
  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. const File cbpFile (getTargetFolder().getChildFile (project.getProjectFilenameRoot())
  126. .withFileExtension (".cbp"));
  127. XmlElement xml ("CodeBlocks_project_file");
  128. addVersion (xml);
  129. createProject (*xml.createNewChildElement ("Project"));
  130. writeXmlOrThrow (xml, cbpFile, "UTF-8", 10);
  131. }
  132. //==============================================================================
  133. void addPlatformSpecificSettingsForProjectType (const ProjectType&) override
  134. {
  135. // add shared code target first as order matters for Codeblocks
  136. if (shouldBuildTargetType (ProjectType::Target::SharedCodeTarget))
  137. targets.add (new CodeBlocksTarget (*this, ProjectType::Target::SharedCodeTarget));
  138. //ProjectType::Target::SharedCodeTarget
  139. callForAllSupportedTargets ([this] (ProjectType::Target::Type targetType)
  140. {
  141. if (targetType == ProjectType::Target::SharedCodeTarget)
  142. return;
  143. if (auto* target = new CodeBlocksTarget (*this, targetType))
  144. {
  145. if (targetType == ProjectType::Target::AggregateTarget)
  146. targets.insert (0, target);
  147. else
  148. targets.add (target);
  149. }
  150. });
  151. // If you hit this assert, you tried to generate a project for an exporter
  152. // that does not support any of your targets!
  153. jassert (targets.size() > 0);
  154. }
  155. //==============================================================================
  156. void initialiseDependencyPathValues() override
  157. {
  158. DependencyPathOS pathOS = isLinux() ? TargetOS::linux
  159. : TargetOS::windows;
  160. vst3Path.referTo (Value (new DependencyPathValueSource (getSetting (Ids::vst3Folder), Ids::vst3Path, pathOS)));
  161. if (! isLinux())
  162. {
  163. aaxPath.referTo (Value (new DependencyPathValueSource (getSetting (Ids::aaxFolder), Ids::aaxPath, pathOS)));
  164. rtasPath.referTo (Value (new DependencyPathValueSource (getSetting (Ids::rtasFolder), Ids::rtasPath, pathOS)));
  165. }
  166. }
  167. private:
  168. Value getTargetPlatformValue() { return getSetting (Ids::codeBlocksWindowsTarget); }
  169. String getTargetPlatform() const { return settings [Ids::codeBlocksWindowsTarget].toString(); }
  170. //==============================================================================
  171. class CodeBlocksBuildConfiguration : public BuildConfiguration
  172. {
  173. public:
  174. CodeBlocksBuildConfiguration (Project& p, const ValueTree& settings, const ProjectExporter& e)
  175. : BuildConfiguration (p, settings, e)
  176. {
  177. if (getArchitectureType().toString().isEmpty())
  178. getArchitectureType() = static_cast<const char* const> ("-m64");
  179. }
  180. Value getArchitectureType()
  181. {
  182. const auto archID = exporter.isWindows() ? Ids::windowsCodeBlocksArchitecture
  183. : Ids::linuxCodeBlocksArchitecture;
  184. return getValue (archID);
  185. }
  186. var getArchitectureTypeVar() const
  187. {
  188. const auto archID = exporter.isWindows() ? Ids::windowsCodeBlocksArchitecture
  189. : Ids::linuxCodeBlocksArchitecture;
  190. return config [archID];
  191. }
  192. var getDefaultOptimisationLevel() const override { return var ((int) (isDebug() ? gccO0 : gccO3)); }
  193. void createConfigProperties (PropertyListBuilder& props) override
  194. {
  195. addGCCOptimisationProperty (props);
  196. static const char* const archNames[] = { "32-bit (-m32)", "64-bit (-m64)", "ARM v6", "ARM v7" };
  197. const var archFlags[] = { "-m32", "-m64", "-march=armv6", "-march=armv7" };
  198. props.add (new ChoicePropertyComponent (getArchitectureType(), "Architecture",
  199. StringArray (archNames, numElementsInArray (archNames)),
  200. Array<var> (archFlags, numElementsInArray (archFlags))));
  201. }
  202. String getModuleLibraryArchName() const override
  203. {
  204. const String archFlag = getArchitectureTypeVar();
  205. const auto prefix = String ("-march=");
  206. if (archFlag.startsWith (prefix))
  207. return archFlag.substring (prefix.length());
  208. else if (archFlag == "-m64")
  209. return "x86_64";
  210. else if (archFlag == "-m32")
  211. return "i386";
  212. jassertfalse;
  213. return {};
  214. }
  215. };
  216. BuildConfiguration::Ptr createBuildConfig (const ValueTree& tree) const override
  217. {
  218. return new CodeBlocksBuildConfiguration (project, tree, *this);
  219. }
  220. //==============================================================================
  221. class CodeBlocksTarget : public ProjectType::Target
  222. {
  223. public:
  224. CodeBlocksTarget (const CodeBlocksProjectExporter& e, ProjectType::Target::Type typeToUse)
  225. : ProjectType::Target (typeToUse),
  226. exporter (e)
  227. {}
  228. String getTargetNameForConfiguration (const BuildConfiguration& config) const
  229. {
  230. if (type == ProjectType::Target::AggregateTarget)
  231. return config.getName();
  232. return getName() + String (" | ") + config.getName();
  233. }
  234. String getTargetSuffix() const
  235. {
  236. auto fileType = getTargetFileType();
  237. if (exporter.isWindows())
  238. {
  239. switch (fileType)
  240. {
  241. case executable: return ".exe";
  242. case staticLibrary: return ".lib";
  243. case sharedLibraryOrDLL:
  244. case pluginBundle: return ".dll";
  245. default:
  246. break;
  247. }
  248. }
  249. else
  250. {
  251. switch (fileType)
  252. {
  253. case executable: return {};
  254. case staticLibrary: return ".a";
  255. case sharedLibraryOrDLL: return ".so";
  256. case pluginBundle:
  257. switch (type)
  258. {
  259. case VST3PlugIn: return ".vst3";
  260. case VSTPlugIn: return ".so";
  261. default: break;
  262. }
  263. return ".so";
  264. default:
  265. break;
  266. }
  267. }
  268. return {};
  269. }
  270. bool isDynamicLibrary() const
  271. {
  272. return (type == DynamicLibrary || type == VST3PlugIn
  273. || type == VSTPlugIn || type == AAXPlugIn);
  274. }
  275. const CodeBlocksProjectExporter& exporter;
  276. };
  277. //==============================================================================
  278. StringArray getPackages() const
  279. {
  280. StringArray result (linuxPackages);
  281. static String guiExtrasModule ("juce_gui_extra");
  282. if (project.getModules().isModuleEnabled (guiExtrasModule)
  283. && project.isConfigFlagEnabled ("JUCE_WEB_BROWSER", true))
  284. {
  285. result.add ("webkit2gtk-4.0");
  286. result.add ("gtk+-x11-3.0");
  287. }
  288. result.removeDuplicates (false);
  289. return result;
  290. }
  291. void addVersion (XmlElement& xml) const
  292. {
  293. XmlElement* fileVersion = xml.createNewChildElement ("FileVersion");
  294. fileVersion->setAttribute ("major", 1);
  295. fileVersion->setAttribute ("minor", 6);
  296. }
  297. void addOptions (XmlElement& xml) const
  298. {
  299. xml.createNewChildElement ("Option")->setAttribute ("title", project.getTitle());
  300. xml.createNewChildElement ("Option")->setAttribute ("pch_mode", 2);
  301. xml.createNewChildElement ("Option")->setAttribute ("compiler", "gcc");
  302. }
  303. StringArray getDefines (const BuildConfiguration& config, CodeBlocksTarget& target) const
  304. {
  305. StringPairArray defines;
  306. if (isWindows())
  307. {
  308. defines.set ("__MINGW__", "1");
  309. defines.set ("__MINGW_EXTENSION", {});
  310. auto targetPlatform = getTargetPlatform();
  311. if (targetPlatform.isNotEmpty())
  312. defines.set ("WINVER", targetPlatform);
  313. }
  314. else
  315. {
  316. defines.set ("LINUX", "1");
  317. }
  318. if (config.isDebug())
  319. {
  320. defines.set ("DEBUG", "1");
  321. defines.set ("_DEBUG", "1");
  322. }
  323. else
  324. {
  325. defines.set ("NDEBUG", "1");
  326. }
  327. defines = mergePreprocessorDefs (defines, getAllPreprocessorDefs (config, target.type));
  328. StringArray defs;
  329. const auto keys = defines.getAllKeys();
  330. const auto values = defines.getAllValues();
  331. for (int i = 0; i < defines.size(); ++i)
  332. {
  333. auto result = keys[i];
  334. if (values[i].isNotEmpty())
  335. result += "=" + values[i];
  336. defs.add (result);
  337. }
  338. return getCleanedStringArray (defs);
  339. }
  340. StringArray getCompilerFlags (const BuildConfiguration& config, CodeBlocksTarget& target) const
  341. {
  342. StringArray flags;
  343. if (const auto codeBlocksConfig = dynamic_cast<const CodeBlocksBuildConfiguration*> (&config))
  344. flags.add (codeBlocksConfig->getArchitectureTypeVar());
  345. flags.add ("-O" + config.getGCCOptimisationFlag());
  346. if (config.isLinkTimeOptimisationEnabled())
  347. flags.add ("-flto");
  348. {
  349. auto cppStandard = config.project.getCppStandardValue().toString();
  350. if (cppStandard == "latest")
  351. cppStandard = "1z";
  352. cppStandard = "-std=" + String (shouldUseGNUExtensions() ? "gnu++" : "c++") + cppStandard;
  353. flags.add (cppStandard);
  354. }
  355. flags.add ("-mstackrealign");
  356. if (config.isDebug())
  357. flags.add ("-g");
  358. flags.addTokens (replacePreprocessorTokens (config, getExtraCompilerFlagsString()).trim(),
  359. " \n", "\"'");
  360. if (config.exporter.isLinux())
  361. {
  362. if (target.isDynamicLibrary() || getProject().getProjectType().isAudioPlugin())
  363. flags.add ("-fPIC");
  364. const auto packages = getPackages();
  365. if (packages.size() > 0)
  366. {
  367. auto pkgconfigFlags = String ("`pkg-config --cflags");
  368. for (auto p : packages)
  369. pkgconfigFlags << " " << p;
  370. pkgconfigFlags << "`";
  371. flags.add (pkgconfigFlags);
  372. }
  373. if (linuxLibs.contains("pthread"))
  374. flags.add ("-pthread");
  375. }
  376. return getCleanedStringArray (flags);
  377. }
  378. StringArray getLinkerFlags (const BuildConfiguration& config, CodeBlocksTarget& target) const
  379. {
  380. StringArray flags (makefileExtraLinkerFlags);
  381. if (const auto codeBlocksConfig = dynamic_cast<const CodeBlocksBuildConfiguration*> (&config))
  382. flags.add (codeBlocksConfig->getArchitectureTypeVar());
  383. if (! config.isDebug())
  384. flags.add ("-s");
  385. if (config.isLinkTimeOptimisationEnabled())
  386. flags.add ("-flto");
  387. flags.addTokens (replacePreprocessorTokens (config, getExtraLinkerFlagsString()).trim(), " \n", "\"'");
  388. const auto packages = getPackages();
  389. if (config.exporter.isLinux())
  390. {
  391. if (target.isDynamicLibrary())
  392. flags.add ("-shared");
  393. if (target.type == ProjectType::Target::StandalonePlugIn
  394. || target.type == ProjectType::Target::GUIApp)
  395. flags.add ("-no-pie");
  396. if (packages.size() > 0)
  397. {
  398. String pkgconfigLibs ("`pkg-config --libs");
  399. for (auto p : packages)
  400. pkgconfigLibs << " " << p;
  401. pkgconfigLibs << "`";
  402. flags.add (pkgconfigLibs);
  403. }
  404. }
  405. return getCleanedStringArray (flags);
  406. }
  407. StringArray getLinkerSearchPaths (const BuildConfiguration& config, CodeBlocksTarget& target) const
  408. {
  409. StringArray librarySearchPaths (config.getLibrarySearchPaths());
  410. if (getProject().getProjectType().isAudioPlugin() && target.type != ProjectType::Target::SharedCodeTarget)
  411. librarySearchPaths.add (RelativePath (getSharedCodePath (config), RelativePath::buildTargetFolder).getParentDirectory().toUnixStyle());
  412. return librarySearchPaths;
  413. }
  414. StringArray getIncludePaths (const BuildConfiguration& config) const
  415. {
  416. StringArray paths;
  417. paths.add (".");
  418. paths.addArray (extraSearchPaths);
  419. paths.addArray (config.getHeaderSearchPaths());
  420. if (! isWindows())
  421. {
  422. paths.add ("/usr/include/freetype2");
  423. // Replace ~ character with $(HOME) environment variable
  424. for (auto& path : paths)
  425. path = path.replace ("~", "$(HOME)");
  426. }
  427. return getCleanedStringArray (paths);
  428. }
  429. static int getTypeIndex (const ProjectType::Target::Type& type)
  430. {
  431. switch (type)
  432. {
  433. case ProjectType::Target::GUIApp:
  434. case ProjectType::Target::StandalonePlugIn:
  435. return 0;
  436. case ProjectType::Target::ConsoleApp:
  437. return 1;
  438. case ProjectType::Target::StaticLibrary:
  439. case ProjectType::Target::SharedCodeTarget:
  440. return 2;
  441. case ProjectType::Target::DynamicLibrary:
  442. case ProjectType::Target::VSTPlugIn:
  443. case ProjectType::Target::VST3PlugIn:
  444. return 3;
  445. default:
  446. break;
  447. }
  448. return 0;
  449. }
  450. String getOutputPathForTarget (CodeBlocksTarget& target, const BuildConfiguration& config) const
  451. {
  452. String outputPath;
  453. if (config.getTargetBinaryRelativePathString().isNotEmpty())
  454. {
  455. RelativePath binaryPath (config.getTargetBinaryRelativePathString(), RelativePath::projectFolder);
  456. binaryPath = binaryPath.rebased (projectFolder, getTargetFolder(), RelativePath::buildTargetFolder);
  457. outputPath = config.getTargetBinaryRelativePathString();
  458. }
  459. else
  460. {
  461. outputPath ="bin/" + File::createLegalFileName (config.getName().trim());
  462. }
  463. return outputPath + "/" + replacePreprocessorTokens (config, config.getTargetBinaryNameString() + target.getTargetSuffix());
  464. }
  465. String getSharedCodePath (const BuildConfiguration& config) const
  466. {
  467. const String outputPath = getOutputPathForTarget (getTargetWithType (ProjectType::Target::SharedCodeTarget), config);
  468. RelativePath path (outputPath, RelativePath::buildTargetFolder);
  469. auto filename = path.getFileName();
  470. if (isLinux())
  471. filename = "lib" + filename;
  472. return path.getParentDirectory().getChildFile (filename).toUnixStyle();
  473. }
  474. void createBuildTarget (XmlElement& xml, CodeBlocksTarget& target, const BuildConfiguration& config) const
  475. {
  476. xml.setAttribute ("title", target.getTargetNameForConfiguration (config));
  477. {
  478. XmlElement* output = xml.createNewChildElement ("Option");
  479. output->setAttribute ("output", getOutputPathForTarget (target, config));
  480. if (isLinux())
  481. {
  482. const bool keepPrefix = (target.type == ProjectType::Target::VSTPlugIn || target.type == ProjectType::Target::VST3PlugIn
  483. || target.type == ProjectType::Target::AAXPlugIn || target.type == ProjectType::Target::RTASPlugIn);
  484. output->setAttribute ("prefix_auto", keepPrefix ? 0 : 1);
  485. }
  486. else
  487. {
  488. output->setAttribute ("prefix_auto", 0);
  489. }
  490. output->setAttribute ("extension_auto", 0);
  491. }
  492. xml.createNewChildElement ("Option")
  493. ->setAttribute ("object_output", "obj/" + File::createLegalFileName (config.getName().trim()));
  494. xml.createNewChildElement ("Option")->setAttribute ("type", getTypeIndex (target.type));
  495. xml.createNewChildElement ("Option")->setAttribute ("compiler", "gcc");
  496. if (getProject().getProjectType().isAudioPlugin() && target.type != ProjectType::Target::SharedCodeTarget)
  497. xml.createNewChildElement ("Option")->setAttribute ("external_deps", getSharedCodePath (config));
  498. {
  499. XmlElement* const compiler = xml.createNewChildElement ("Compiler");
  500. {
  501. StringArray flags;
  502. for (auto& def : getDefines (config, target))
  503. {
  504. if (! def.containsChar ('='))
  505. def << '=';
  506. flags.add ("-D" + def);
  507. }
  508. flags.addArray (getCompilerFlags (config, target));
  509. for (auto flag : flags)
  510. setAddOption (*compiler, "option", flag);
  511. }
  512. {
  513. const StringArray includePaths (getIncludePaths (config));
  514. for (auto path : includePaths)
  515. setAddOption (*compiler, "directory", path);
  516. }
  517. }
  518. {
  519. XmlElement* const linker = xml.createNewChildElement ("Linker");
  520. if (getProject().getProjectType().isAudioPlugin() && target.type != ProjectType::Target::SharedCodeTarget)
  521. setAddOption (*linker, "option", getSharedCodePath (config));
  522. for (auto& flag : getLinkerFlags (config, target))
  523. setAddOption (*linker, "option", flag);
  524. const StringArray& libs = isWindows() ? mingwLibs : linuxLibs;
  525. for (auto lib : libs)
  526. setAddOption (*linker, "library", lib);
  527. for (auto& path : getLinkerSearchPaths (config, target))
  528. setAddOption (*linker, "directory", replacePreprocessorDefs (getAllPreprocessorDefs(), path));
  529. }
  530. }
  531. void addBuild (XmlElement& xml) const
  532. {
  533. XmlElement* const build = xml.createNewChildElement ("Build");
  534. for (ConstConfigIterator config (*this); config.next();)
  535. for (auto target : targets)
  536. if (target->type != ProjectType::Target::AggregateTarget)
  537. createBuildTarget (*build->createNewChildElement ("Target"), *target, *config);
  538. }
  539. void addVirtualTargets (XmlElement& xml) const
  540. {
  541. XmlElement* const virtualTargets = xml.createNewChildElement ("VirtualTargets");
  542. for (ConstConfigIterator config (*this); config.next();)
  543. {
  544. StringArray allTargets;
  545. for (auto target : targets)
  546. if (target->type != ProjectType::Target::AggregateTarget)
  547. allTargets.add (target->getTargetNameForConfiguration (*config));
  548. for (auto target : targets)
  549. {
  550. if (target->type == ProjectType::Target::AggregateTarget)
  551. {
  552. auto* configTarget = virtualTargets->createNewChildElement ("Add");
  553. configTarget->setAttribute ("alias", config->getName());
  554. configTarget->setAttribute ("targets", allTargets.joinIntoString (";"));
  555. }
  556. }
  557. }
  558. }
  559. StringArray getProjectCompilerOptions() const
  560. {
  561. return { "-Wall", "-Wno-strict-aliasing", "-Wno-strict-overflow" };
  562. }
  563. void addProjectCompilerOptions (XmlElement& xml) const
  564. {
  565. XmlElement* const compiler = xml.createNewChildElement ("Compiler");
  566. for (auto& option : getProjectCompilerOptions())
  567. setAddOption (*compiler, "option", option);
  568. }
  569. StringArray getProjectLinkerLibs() const
  570. {
  571. StringArray result;
  572. if (isWindows())
  573. result.addArray ({ "gdi32", "user32", "kernel32", "comctl32" });
  574. result.addTokens (getExternalLibrariesString(), ";\n", "\"'");
  575. result = getCleanedStringArray (result);
  576. for (auto& option : result)
  577. option = replacePreprocessorDefs (getAllPreprocessorDefs(), option);
  578. return result;
  579. }
  580. void addProjectLinkerOptions (XmlElement& xml) const
  581. {
  582. XmlElement* const linker = xml.createNewChildElement ("Linker");
  583. for (auto& lib : getProjectLinkerLibs())
  584. setAddOption (*linker, "library", lib);
  585. }
  586. CodeBlocksTarget& getTargetWithType (ProjectType::Target::Type type) const
  587. {
  588. CodeBlocksTarget* nonAggregrateTarget = nullptr;
  589. for (auto* target : targets)
  590. {
  591. if (target->type == type)
  592. return *target;
  593. if (target->type != ProjectType::Target::AggregateTarget)
  594. nonAggregrateTarget = target;
  595. }
  596. // this project has no valid targets
  597. jassert (nonAggregrateTarget != nullptr);
  598. return *nonAggregrateTarget;
  599. }
  600. // Returns SharedCode target for multi-target projects, otherwise it returns
  601. // the single target
  602. CodeBlocksTarget& getMainTarget() const
  603. {
  604. if (getProject().getProjectType().isAudioPlugin())
  605. return getTargetWithType (ProjectType::Target::SharedCodeTarget);
  606. for (auto* target : targets)
  607. if (target->type != ProjectType::Target::AggregateTarget)
  608. return *target;
  609. jassertfalse;
  610. return *targets[0];
  611. }
  612. CodeBlocksTarget& getTargetForProjectItem (const Project::Item& projectItem) const
  613. {
  614. if (getProject().getProjectType().isAudioPlugin())
  615. {
  616. if (! projectItem.shouldBeCompiled())
  617. return getTargetWithType (ProjectType::Target::SharedCodeTarget);
  618. return getTargetWithType (getProject().getTargetTypeFromFilePath (projectItem.getFile(), true));
  619. }
  620. return getMainTarget();
  621. }
  622. void addCompileUnits (const Project::Item& projectItem, XmlElement& xml) const
  623. {
  624. if (projectItem.isGroup())
  625. {
  626. for (int i = 0; i < projectItem.getNumChildren(); ++i)
  627. addCompileUnits (projectItem.getChild(i), xml);
  628. }
  629. else if (projectItem.shouldBeAddedToTargetProject())
  630. {
  631. const RelativePath file (projectItem.getFile(), getTargetFolder(), RelativePath::buildTargetFolder);
  632. XmlElement* unit = xml.createNewChildElement ("Unit");
  633. unit->setAttribute ("filename", file.toUnixStyle());
  634. for (ConstConfigIterator config (*this); config.next();)
  635. {
  636. const String& targetName = getTargetForProjectItem (projectItem).getTargetNameForConfiguration (*config);
  637. unit->createNewChildElement ("Option")->setAttribute ("target", targetName);
  638. }
  639. if (! projectItem.shouldBeCompiled())
  640. {
  641. unit->createNewChildElement ("Option")->setAttribute ("compile", 0);
  642. unit->createNewChildElement ("Option")->setAttribute ("link", 0);
  643. }
  644. }
  645. }
  646. void addCompileUnits (XmlElement& xml) const
  647. {
  648. for (int i = 0; i < getAllGroups().size(); ++i)
  649. addCompileUnits (getAllGroups().getReference(i), xml);
  650. }
  651. void createProject (XmlElement& xml) const
  652. {
  653. addOptions (xml);
  654. addBuild (xml);
  655. addVirtualTargets (xml);
  656. addProjectCompilerOptions (xml);
  657. addProjectLinkerOptions (xml);
  658. addCompileUnits (xml);
  659. }
  660. void setAddOption (XmlElement& xml, const String& nm, const String& value) const
  661. {
  662. xml.createNewChildElement ("Add")->setAttribute (nm, value);
  663. }
  664. CodeBlocksOS os;
  665. OwnedArray<CodeBlocksTarget> targets;
  666. friend class CLionProjectExporter;
  667. JUCE_DECLARE_NON_COPYABLE (CodeBlocksProjectExporter)
  668. };