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.

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