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.

851 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 (packages.size() > 0)
  394. {
  395. String pkgconfigLibs ("`pkg-config --libs");
  396. for (auto& p : packages)
  397. pkgconfigLibs << " " << p;
  398. pkgconfigLibs << "`";
  399. flags.add (pkgconfigLibs);
  400. }
  401. }
  402. return getCleanedStringArray (flags);
  403. }
  404. StringArray getLinkerSearchPaths (const BuildConfiguration& config, CodeBlocksTarget& target) const
  405. {
  406. StringArray librarySearchPaths (config.getLibrarySearchPaths());
  407. if (getProject().getProjectType().isAudioPlugin() && target.type != ProjectType::Target::SharedCodeTarget)
  408. librarySearchPaths.add (RelativePath (getSharedCodePath (config), RelativePath::buildTargetFolder).getParentDirectory().toUnixStyle());
  409. return librarySearchPaths;
  410. }
  411. StringArray getIncludePaths (const BuildConfiguration& config) const
  412. {
  413. StringArray paths;
  414. paths.add (".");
  415. paths.addArray (extraSearchPaths);
  416. paths.addArray (config.getHeaderSearchPaths());
  417. if (! isWindows())
  418. {
  419. paths.add ("/usr/include/freetype2");
  420. // Replace ~ character with $(HOME) environment variable
  421. for (auto& path : paths)
  422. path = path.replace ("~", "$(HOME)");
  423. }
  424. return getCleanedStringArray (paths);
  425. }
  426. static int getTypeIndex (const ProjectType::Target::Type& type)
  427. {
  428. switch (type)
  429. {
  430. case ProjectType::Target::GUIApp:
  431. case ProjectType::Target::StandalonePlugIn:
  432. return 0;
  433. case ProjectType::Target::ConsoleApp:
  434. return 1;
  435. case ProjectType::Target::StaticLibrary:
  436. case ProjectType::Target::SharedCodeTarget:
  437. return 2;
  438. case ProjectType::Target::DynamicLibrary:
  439. case ProjectType::Target::VSTPlugIn:
  440. case ProjectType::Target::VST3PlugIn:
  441. return 3;
  442. default:
  443. break;
  444. }
  445. return 0;
  446. }
  447. String getOutputPathForTarget (CodeBlocksTarget& target, const BuildConfiguration& config) const
  448. {
  449. String outputPath;
  450. if (config.getTargetBinaryRelativePathString().isNotEmpty())
  451. {
  452. RelativePath binaryPath (config.getTargetBinaryRelativePathString(), RelativePath::projectFolder);
  453. binaryPath = binaryPath.rebased (projectFolder, getTargetFolder(), RelativePath::buildTargetFolder);
  454. outputPath = config.getTargetBinaryRelativePathString();
  455. }
  456. else
  457. {
  458. outputPath ="bin/" + File::createLegalFileName (config.getName().trim());
  459. }
  460. return outputPath + "/" + replacePreprocessorTokens (config, config.getTargetBinaryNameString() + target.getTargetSuffix());
  461. }
  462. String getSharedCodePath (const BuildConfiguration& config) const
  463. {
  464. const String outputPath = getOutputPathForTarget (getTargetWithType (ProjectType::Target::SharedCodeTarget), config);
  465. RelativePath path (outputPath, RelativePath::buildTargetFolder);
  466. auto filename = path.getFileName();
  467. if (isLinux())
  468. filename = "lib" + filename;
  469. return path.getParentDirectory().getChildFile (filename).toUnixStyle();
  470. }
  471. void createBuildTarget (XmlElement& xml, CodeBlocksTarget& target, const BuildConfiguration& config) const
  472. {
  473. xml.setAttribute ("title", target.getTargetNameForConfiguration (config));
  474. {
  475. XmlElement* output = xml.createNewChildElement ("Option");
  476. output->setAttribute ("output", getOutputPathForTarget (target, config));
  477. if (isLinux())
  478. {
  479. const bool keepPrefix = (target.type == ProjectType::Target::VSTPlugIn || target.type == ProjectType::Target::VST3PlugIn
  480. || target.type == ProjectType::Target::AAXPlugIn || target.type == ProjectType::Target::RTASPlugIn);
  481. output->setAttribute ("prefix_auto", keepPrefix ? 0 : 1);
  482. }
  483. else
  484. {
  485. output->setAttribute ("prefix_auto", 0);
  486. }
  487. output->setAttribute ("extension_auto", 0);
  488. }
  489. xml.createNewChildElement ("Option")
  490. ->setAttribute ("object_output", "obj/" + File::createLegalFileName (config.getName().trim()));
  491. xml.createNewChildElement ("Option")->setAttribute ("type", getTypeIndex (target.type));
  492. xml.createNewChildElement ("Option")->setAttribute ("compiler", "gcc");
  493. if (getProject().getProjectType().isAudioPlugin() && target.type != ProjectType::Target::SharedCodeTarget)
  494. xml.createNewChildElement ("Option")->setAttribute ("external_deps", getSharedCodePath (config));
  495. {
  496. XmlElement* const compiler = xml.createNewChildElement ("Compiler");
  497. {
  498. StringArray flags;
  499. for (auto& def : getDefines (config, target))
  500. {
  501. if (! def.containsChar ('='))
  502. def << '=';
  503. flags.add ("-D" + def);
  504. }
  505. flags.addArray (getCompilerFlags (config, target));
  506. for (auto flag : flags)
  507. setAddOption (*compiler, "option", flag);
  508. }
  509. {
  510. const StringArray includePaths (getIncludePaths (config));
  511. for (auto path : includePaths)
  512. setAddOption (*compiler, "directory", path);
  513. }
  514. }
  515. {
  516. XmlElement* const linker = xml.createNewChildElement ("Linker");
  517. if (getProject().getProjectType().isAudioPlugin() && target.type != ProjectType::Target::SharedCodeTarget)
  518. setAddOption (*linker, "option", getSharedCodePath (config));
  519. for (auto& flag : getLinkerFlags (config, target))
  520. setAddOption (*linker, "option", flag);
  521. const StringArray& libs = isWindows() ? mingwLibs : linuxLibs;
  522. for (auto lib : libs)
  523. setAddOption (*linker, "library", lib);
  524. for (auto& path : getLinkerSearchPaths (config, target))
  525. setAddOption (*linker, "directory", replacePreprocessorDefs (getAllPreprocessorDefs(), path));
  526. }
  527. }
  528. void addBuild (XmlElement& xml) const
  529. {
  530. XmlElement* const build = xml.createNewChildElement ("Build");
  531. for (ConstConfigIterator config (*this); config.next();)
  532. for (auto target : targets)
  533. if (target->type != ProjectType::Target::AggregateTarget)
  534. createBuildTarget (*build->createNewChildElement ("Target"), *target, *config);
  535. }
  536. void addVirtualTargets (XmlElement& xml) const
  537. {
  538. XmlElement* const virtualTargets = xml.createNewChildElement ("VirtualTargets");
  539. for (ConstConfigIterator config (*this); config.next();)
  540. {
  541. StringArray allTargets;
  542. for (auto target : targets)
  543. if (target->type != ProjectType::Target::AggregateTarget)
  544. allTargets.add (target->getTargetNameForConfiguration (*config));
  545. for (auto target : targets)
  546. {
  547. if (target->type == ProjectType::Target::AggregateTarget)
  548. {
  549. auto* configTarget = virtualTargets->createNewChildElement ("Add");
  550. configTarget->setAttribute ("alias", config->getName());
  551. configTarget->setAttribute ("targets", allTargets.joinIntoString (";"));
  552. }
  553. }
  554. }
  555. }
  556. StringArray getProjectCompilerOptions() const
  557. {
  558. return { "-Wall", "-Wno-strict-aliasing", "-Wno-strict-overflow" };
  559. }
  560. void addProjectCompilerOptions (XmlElement& xml) const
  561. {
  562. XmlElement* const compiler = xml.createNewChildElement ("Compiler");
  563. for (auto& option : getProjectCompilerOptions())
  564. setAddOption (*compiler, "option", option);
  565. }
  566. StringArray getProjectLinkerLibs() const
  567. {
  568. StringArray result;
  569. if (isWindows())
  570. result.addArray ({ "gdi32", "user32", "kernel32", "comctl32" });
  571. result.addTokens (getExternalLibrariesString(), ";\n", "\"'");
  572. result = getCleanedStringArray (result);
  573. for (auto& option : result)
  574. option = replacePreprocessorDefs (getAllPreprocessorDefs(), option);
  575. return result;
  576. }
  577. void addProjectLinkerOptions (XmlElement& xml) const
  578. {
  579. XmlElement* const linker = xml.createNewChildElement ("Linker");
  580. for (auto& lib : getProjectLinkerLibs())
  581. setAddOption (*linker, "library", lib);
  582. }
  583. CodeBlocksTarget& getTargetWithType (ProjectType::Target::Type type) const
  584. {
  585. CodeBlocksTarget* nonAggregrateTarget = nullptr;
  586. for (auto* target : targets)
  587. {
  588. if (target->type == type)
  589. return *target;
  590. if (target->type != ProjectType::Target::AggregateTarget)
  591. nonAggregrateTarget = target;
  592. }
  593. // this project has no valid targets
  594. jassert (nonAggregrateTarget != nullptr);
  595. return *nonAggregrateTarget;
  596. }
  597. // Returns SharedCode target for multi-target projects, otherwise it returns
  598. // the single target
  599. CodeBlocksTarget& getMainTarget() const
  600. {
  601. if (getProject().getProjectType().isAudioPlugin())
  602. return getTargetWithType (ProjectType::Target::SharedCodeTarget);
  603. for (auto* target : targets)
  604. if (target->type != ProjectType::Target::AggregateTarget)
  605. return *target;
  606. jassertfalse;
  607. return *targets[0];
  608. }
  609. CodeBlocksTarget& getTargetForProjectItem (const Project::Item& projectItem) const
  610. {
  611. if (getProject().getProjectType().isAudioPlugin())
  612. {
  613. if (! projectItem.shouldBeCompiled())
  614. return getTargetWithType (ProjectType::Target::SharedCodeTarget);
  615. return getTargetWithType (getProject().getTargetTypeFromFilePath (projectItem.getFile(), true));
  616. }
  617. return getMainTarget();
  618. }
  619. void addCompileUnits (const Project::Item& projectItem, XmlElement& xml) const
  620. {
  621. if (projectItem.isGroup())
  622. {
  623. for (int i = 0; i < projectItem.getNumChildren(); ++i)
  624. addCompileUnits (projectItem.getChild(i), xml);
  625. }
  626. else if (projectItem.shouldBeAddedToTargetProject())
  627. {
  628. const RelativePath file (projectItem.getFile(), getTargetFolder(), RelativePath::buildTargetFolder);
  629. XmlElement* unit = xml.createNewChildElement ("Unit");
  630. unit->setAttribute ("filename", file.toUnixStyle());
  631. for (ConstConfigIterator config (*this); config.next();)
  632. {
  633. const String& targetName = getTargetForProjectItem (projectItem).getTargetNameForConfiguration (*config);
  634. unit->createNewChildElement ("Option")->setAttribute ("target", targetName);
  635. }
  636. if (! projectItem.shouldBeCompiled())
  637. {
  638. unit->createNewChildElement ("Option")->setAttribute ("compile", 0);
  639. unit->createNewChildElement ("Option")->setAttribute ("link", 0);
  640. }
  641. }
  642. }
  643. void addCompileUnits (XmlElement& xml) const
  644. {
  645. for (int i = 0; i < getAllGroups().size(); ++i)
  646. addCompileUnits (getAllGroups().getReference(i), xml);
  647. }
  648. void createProject (XmlElement& xml) const
  649. {
  650. addOptions (xml);
  651. addBuild (xml);
  652. addVirtualTargets (xml);
  653. addProjectCompilerOptions (xml);
  654. addProjectLinkerOptions (xml);
  655. addCompileUnits (xml);
  656. }
  657. void setAddOption (XmlElement& xml, const String& nm, const String& value) const
  658. {
  659. xml.createNewChildElement ("Add")->setAttribute (nm, value);
  660. }
  661. CodeBlocksOS os;
  662. OwnedArray<CodeBlocksTarget> targets;
  663. friend class CLionProjectExporter;
  664. JUCE_DECLARE_NON_COPYABLE (CodeBlocksProjectExporter)
  665. };