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.

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