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.

819 lines
30KB

  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&) override
  113. {
  114. }
  115. //==============================================================================
  116. void create (const OwnedArray<LibraryModule>&) const override
  117. {
  118. const File cbpFile (getTargetFolder().getChildFile (project.getProjectFilenameRoot())
  119. .withFileExtension (".cbp"));
  120. XmlElement xml ("CodeBlocks_project_file");
  121. addVersion (xml);
  122. createProject (*xml.createNewChildElement ("Project"));
  123. writeXmlOrThrow (xml, cbpFile, "UTF-8", 10);
  124. }
  125. //==============================================================================
  126. void addPlatformSpecificSettingsForProjectType (const ProjectType&) override
  127. {
  128. // add shared code target first as order matters for Codeblocks
  129. if (shouldBuildTargetType (ProjectType::Target::SharedCodeTarget))
  130. targets.add (new CodeBlocksTarget (*this, ProjectType::Target::SharedCodeTarget));
  131. //ProjectType::Target::SharedCodeTarget
  132. callForAllSupportedTargets ([this] (ProjectType::Target::Type targetType)
  133. {
  134. if (targetType == ProjectType::Target::SharedCodeTarget)
  135. return;
  136. if (auto* target = new CodeBlocksTarget (*this, targetType))
  137. {
  138. if (targetType == ProjectType::Target::AggregateTarget)
  139. targets.insert (0, target);
  140. else
  141. targets.add (target);
  142. }
  143. });
  144. // If you hit this assert, you tried to generate a project for an exporter
  145. // that does not support any of your targets!
  146. jassert (targets.size() > 0);
  147. }
  148. //==============================================================================
  149. void initialiseDependencyPathValues() override
  150. {
  151. DependencyPathOS pathOS = isLinux() ? TargetOS::linux
  152. : TargetOS::windows;
  153. vst3Path.referTo (Value (new DependencyPathValueSource (getSetting (Ids::vst3Folder), Ids::vst3Path, pathOS)));
  154. if (! isLinux())
  155. {
  156. aaxPath.referTo (Value (new DependencyPathValueSource (getSetting (Ids::aaxFolder), Ids::aaxPath, pathOS)));
  157. rtasPath.referTo (Value (new DependencyPathValueSource (getSetting (Ids::rtasFolder), Ids::rtasPath, pathOS)));
  158. }
  159. }
  160. private:
  161. //==============================================================================
  162. class CodeBlocksBuildConfiguration : public BuildConfiguration
  163. {
  164. public:
  165. CodeBlocksBuildConfiguration (Project& p, const ValueTree& settings, const ProjectExporter& e)
  166. : BuildConfiguration (p, settings, e)
  167. {
  168. if (getArchitectureType().toString().isEmpty())
  169. getArchitectureType() = static_cast<const char* const> ("-m64");
  170. }
  171. Value getArchitectureType()
  172. {
  173. const auto archID = exporter.isWindows() ? Ids::windowsCodeBlocksArchitecture
  174. : Ids::linuxCodeBlocksArchitecture;
  175. return getValue (archID);
  176. }
  177. var getArchitectureTypeVar() const
  178. {
  179. const auto archID = exporter.isWindows() ? Ids::windowsCodeBlocksArchitecture
  180. : Ids::linuxCodeBlocksArchitecture;
  181. return config [archID];
  182. }
  183. var getDefaultOptimisationLevel() const override { return var ((int) (isDebug() ? gccO0 : gccO3)); }
  184. void createConfigProperties (PropertyListBuilder& props) override
  185. {
  186. addGCCOptimisationProperty (props);
  187. static const char* const archNames[] = { "32-bit (-m32)", "64-bit (-m64)", "ARM v6", "ARM v7" };
  188. const var archFlags[] = { "-m32", "-m64", "-march=armv6", "-march=armv7" };
  189. props.add (new ChoicePropertyComponent (getArchitectureType(), "Architecture",
  190. StringArray (archNames, numElementsInArray (archNames)),
  191. Array<var> (archFlags, numElementsInArray (archFlags))));
  192. }
  193. String getModuleLibraryArchName() const override
  194. {
  195. const String archFlag = getArchitectureTypeVar();
  196. const auto prefix = String ("-march=");
  197. if (archFlag.startsWith (prefix))
  198. return archFlag.substring (prefix.length());
  199. else if (archFlag == "-m64")
  200. return "x86_64";
  201. else if (archFlag == "-m32")
  202. return "i386";
  203. jassertfalse;
  204. return {};
  205. }
  206. };
  207. BuildConfiguration::Ptr createBuildConfig (const ValueTree& tree) const override
  208. {
  209. return new CodeBlocksBuildConfiguration (project, tree, *this);
  210. }
  211. //==============================================================================
  212. class CodeBlocksTarget : public ProjectType::Target
  213. {
  214. public:
  215. CodeBlocksTarget (const CodeBlocksProjectExporter& e, ProjectType::Target::Type typeToUse)
  216. : ProjectType::Target (typeToUse),
  217. exporter (e)
  218. {}
  219. String getTargetNameForConfiguration (const BuildConfiguration& config) const
  220. {
  221. if (type == ProjectType::Target::AggregateTarget)
  222. return config.getName();
  223. return getName() + String (" | ") + config.getName();
  224. }
  225. String getTargetSuffix() const
  226. {
  227. auto fileType = getTargetFileType();
  228. if (exporter.isWindows())
  229. {
  230. switch (fileType)
  231. {
  232. case executable: return ".exe";
  233. case staticLibrary: return ".lib";
  234. case sharedLibraryOrDLL:
  235. case pluginBundle: return ".dll";
  236. default:
  237. break;
  238. }
  239. }
  240. else
  241. {
  242. switch (fileType)
  243. {
  244. case executable: return {};
  245. case staticLibrary: return ".a";
  246. case sharedLibraryOrDLL: return ".so";
  247. case pluginBundle:
  248. switch (type)
  249. {
  250. case VST3PlugIn: return ".vst3";
  251. case VSTPlugIn: return ".so";
  252. default: break;
  253. }
  254. return ".so";
  255. default:
  256. break;
  257. }
  258. }
  259. return {};
  260. }
  261. bool isDynamicLibrary() const
  262. {
  263. return (type == DynamicLibrary || type == VST3PlugIn
  264. || type == VSTPlugIn || type == AAXPlugIn);
  265. }
  266. const CodeBlocksProjectExporter& exporter;
  267. };
  268. //==============================================================================
  269. StringArray getPackages() const
  270. {
  271. StringArray result (linuxPackages);
  272. static String guiExtrasModule ("juce_gui_extra");
  273. if (project.getModules().isModuleEnabled (guiExtrasModule)
  274. && project.isConfigFlagEnabled ("JUCE_WEB_BROWSER", true))
  275. {
  276. result.add ("webkit2gtk-4.0");
  277. result.add ("gtk+-x11-3.0");
  278. }
  279. result.removeDuplicates (false);
  280. return result;
  281. }
  282. void addVersion (XmlElement& xml) const
  283. {
  284. XmlElement* fileVersion = xml.createNewChildElement ("FileVersion");
  285. fileVersion->setAttribute ("major", 1);
  286. fileVersion->setAttribute ("minor", 6);
  287. }
  288. void addOptions (XmlElement& xml) const
  289. {
  290. xml.createNewChildElement ("Option")->setAttribute ("title", project.getTitle());
  291. xml.createNewChildElement ("Option")->setAttribute ("pch_mode", 2);
  292. xml.createNewChildElement ("Option")->setAttribute ("compiler", "gcc");
  293. }
  294. StringArray getDefines (const BuildConfiguration& config, CodeBlocksTarget& target) const
  295. {
  296. StringPairArray defines;
  297. if (isWindows())
  298. {
  299. defines.set ("__MINGW__", "1");
  300. defines.set ("__MINGW_EXTENSION", String());
  301. }
  302. else
  303. {
  304. defines.set ("LINUX", "1");
  305. }
  306. if (config.isDebug())
  307. {
  308. defines.set ("DEBUG", "1");
  309. defines.set ("_DEBUG", "1");
  310. }
  311. else
  312. {
  313. defines.set ("NDEBUG", "1");
  314. }
  315. defines = mergePreprocessorDefs (defines, getAllPreprocessorDefs (config, target.type));
  316. StringArray defs;
  317. for (int i = 0; i < defines.size(); ++i)
  318. defs.add (defines.getAllKeys()[i] + "=" + defines.getAllValues()[i]);
  319. return getCleanedStringArray (defs);
  320. }
  321. StringArray getCompilerFlags (const BuildConfiguration& config, CodeBlocksTarget& target) const
  322. {
  323. StringArray flags;
  324. if (const auto codeBlocksConfig = dynamic_cast<const CodeBlocksBuildConfiguration*> (&config))
  325. flags.add (codeBlocksConfig->getArchitectureTypeVar());
  326. flags.add ("-O" + config.getGCCOptimisationFlag());
  327. if (config.isLinkTimeOptimisationEnabled())
  328. flags.add ("-flto");
  329. {
  330. auto cppStandard = config.project.getCppStandardValue().toString();
  331. if (cppStandard == "latest")
  332. cppStandard = "1z";
  333. cppStandard = "-std=" + String (shouldUseGNUExtensions() ? "gnu++" : "c++") + cppStandard;
  334. flags.add (cppStandard);
  335. }
  336. flags.add ("-mstackrealign");
  337. if (config.isDebug())
  338. flags.add ("-g");
  339. flags.addTokens (replacePreprocessorTokens (config, getExtraCompilerFlagsString()).trim(),
  340. " \n", "\"'");
  341. if (config.exporter.isLinux())
  342. {
  343. if (target.isDynamicLibrary() || getProject().getProjectType().isAudioPlugin())
  344. flags.add ("-fPIC");
  345. const auto packages = getPackages();
  346. if (packages.size() > 0)
  347. {
  348. auto pkgconfigFlags = String ("`pkg-config --cflags");
  349. for (auto p : packages)
  350. pkgconfigFlags << " " << p;
  351. pkgconfigFlags << "`";
  352. flags.add (pkgconfigFlags);
  353. }
  354. if (linuxLibs.contains("pthread"))
  355. flags.add ("-pthread");
  356. }
  357. return getCleanedStringArray (flags);
  358. }
  359. StringArray getLinkerFlags (const BuildConfiguration& config, CodeBlocksTarget& target) const
  360. {
  361. StringArray flags (makefileExtraLinkerFlags);
  362. if (const auto codeBlocksConfig = dynamic_cast<const CodeBlocksBuildConfiguration*> (&config))
  363. flags.add (codeBlocksConfig->getArchitectureTypeVar());
  364. if (! config.isDebug())
  365. flags.add ("-s");
  366. flags.addTokens (replacePreprocessorTokens (config, getExtraLinkerFlagsString()).trim(), " \n", "\"'");
  367. const auto packages = getPackages();
  368. if (config.exporter.isLinux() && packages.size() > 0)
  369. {
  370. if (target.isDynamicLibrary())
  371. flags.add ("-shared");
  372. auto pkgconfigLibs = String ("`pkg-config --libs");
  373. for (auto p : packages)
  374. pkgconfigLibs << " " << p;
  375. pkgconfigLibs << "`";
  376. flags.add (pkgconfigLibs);
  377. }
  378. return getCleanedStringArray (flags);
  379. }
  380. StringArray getLinkerSearchPaths (const BuildConfiguration& config, CodeBlocksTarget& target) const
  381. {
  382. StringArray librarySearchPaths (config.getLibrarySearchPaths());
  383. if (getProject().getProjectType().isAudioPlugin() && target.type != ProjectType::Target::SharedCodeTarget)
  384. librarySearchPaths.add (RelativePath (getSharedCodePath (config), RelativePath::buildTargetFolder).getParentDirectory().toUnixStyle());
  385. return librarySearchPaths;
  386. }
  387. StringArray getIncludePaths (const BuildConfiguration& config) const
  388. {
  389. StringArray paths;
  390. paths.add (".");
  391. paths.addArray (extraSearchPaths);
  392. paths.addArray (config.getHeaderSearchPaths());
  393. if (! isWindows())
  394. {
  395. paths.add ("/usr/include/freetype2");
  396. // Replace ~ character with $(HOME) environment variable
  397. for (auto& path : paths)
  398. path = path.replace ("~", "$(HOME)");
  399. }
  400. return getCleanedStringArray (paths);
  401. }
  402. static int getTypeIndex (const ProjectType::Target::Type& type)
  403. {
  404. switch (type)
  405. {
  406. case ProjectType::Target::GUIApp:
  407. case ProjectType::Target::StandalonePlugIn:
  408. return 0;
  409. case ProjectType::Target::ConsoleApp:
  410. return 1;
  411. case ProjectType::Target::StaticLibrary:
  412. case ProjectType::Target::SharedCodeTarget:
  413. return 2;
  414. case ProjectType::Target::DynamicLibrary:
  415. case ProjectType::Target::VSTPlugIn:
  416. case ProjectType::Target::VST3PlugIn:
  417. return 3;
  418. default:
  419. break;
  420. }
  421. return 0;
  422. }
  423. String getOutputPathForTarget (CodeBlocksTarget& target, const BuildConfiguration& config) const
  424. {
  425. String outputPath;
  426. if (config.getTargetBinaryRelativePathString().isNotEmpty())
  427. {
  428. RelativePath binaryPath (config.getTargetBinaryRelativePathString(), RelativePath::projectFolder);
  429. binaryPath = binaryPath.rebased (projectFolder, getTargetFolder(), RelativePath::buildTargetFolder);
  430. outputPath = config.getTargetBinaryRelativePathString();
  431. }
  432. else
  433. {
  434. outputPath ="bin/" + File::createLegalFileName (config.getName().trim());
  435. }
  436. return outputPath + "/" + replacePreprocessorTokens (config, config.getTargetBinaryNameString() + target.getTargetSuffix());
  437. }
  438. String getSharedCodePath (const BuildConfiguration& config) const
  439. {
  440. const String outputPath = getOutputPathForTarget (getTargetWithType (ProjectType::Target::SharedCodeTarget), config);
  441. RelativePath path (outputPath, RelativePath::buildTargetFolder);
  442. auto filename = path.getFileName();
  443. if (isLinux())
  444. filename = "lib" + filename;
  445. return path.getParentDirectory().getChildFile (filename).toUnixStyle();
  446. }
  447. void createBuildTarget (XmlElement& xml, CodeBlocksTarget& target, const BuildConfiguration& config) const
  448. {
  449. xml.setAttribute ("title", target.getTargetNameForConfiguration (config));
  450. {
  451. XmlElement* output = xml.createNewChildElement ("Option");
  452. output->setAttribute ("output", getOutputPathForTarget (target, config));
  453. if (isLinux())
  454. {
  455. const bool keepPrefix = (target.type == ProjectType::Target::VSTPlugIn || target.type == ProjectType::Target::VST3PlugIn
  456. || target.type == ProjectType::Target::AAXPlugIn || target.type == ProjectType::Target::RTASPlugIn);
  457. output->setAttribute ("prefix_auto", keepPrefix ? 0 : 1);
  458. }
  459. else
  460. {
  461. output->setAttribute ("prefix_auto", 0);
  462. }
  463. output->setAttribute ("extension_auto", 0);
  464. }
  465. xml.createNewChildElement ("Option")
  466. ->setAttribute ("object_output", "obj/" + File::createLegalFileName (config.getName().trim()));
  467. xml.createNewChildElement ("Option")->setAttribute ("type", getTypeIndex (target.type));
  468. xml.createNewChildElement ("Option")->setAttribute ("compiler", "gcc");
  469. if (getProject().getProjectType().isAudioPlugin() && target.type != ProjectType::Target::SharedCodeTarget)
  470. xml.createNewChildElement ("Option")->setAttribute ("external_deps", getSharedCodePath (config));
  471. {
  472. XmlElement* const compiler = xml.createNewChildElement ("Compiler");
  473. {
  474. StringArray flags;
  475. for (auto& def : getDefines (config, target))
  476. {
  477. if (! def.containsChar ('='))
  478. def << '=';
  479. flags.add ("-D" + def);
  480. }
  481. flags.addArray (getCompilerFlags (config, target));
  482. for (auto flag : flags)
  483. setAddOption (*compiler, "option", flag);
  484. }
  485. {
  486. const StringArray includePaths (getIncludePaths (config));
  487. for (auto path : includePaths)
  488. setAddOption (*compiler, "directory", path);
  489. }
  490. }
  491. {
  492. XmlElement* const linker = xml.createNewChildElement ("Linker");
  493. if (getProject().getProjectType().isAudioPlugin() && target.type != ProjectType::Target::SharedCodeTarget)
  494. setAddOption (*linker, "option", getSharedCodePath (config));
  495. for (auto& flag : getLinkerFlags (config, target))
  496. setAddOption (*linker, "option", flag);
  497. const StringArray& libs = isWindows() ? mingwLibs : linuxLibs;
  498. for (auto lib : libs)
  499. setAddOption (*linker, "library", lib);
  500. for (auto& path : getLinkerSearchPaths (config, target))
  501. setAddOption (*linker, "directory", replacePreprocessorDefs (getAllPreprocessorDefs(), path));
  502. }
  503. }
  504. void addBuild (XmlElement& xml) const
  505. {
  506. XmlElement* const build = xml.createNewChildElement ("Build");
  507. for (ConstConfigIterator config (*this); config.next();)
  508. for (auto target : targets)
  509. if (target->type != ProjectType::Target::AggregateTarget)
  510. createBuildTarget (*build->createNewChildElement ("Target"), *target, *config);
  511. }
  512. void addVirtualTargets (XmlElement& xml) const
  513. {
  514. XmlElement* const virtualTargets = xml.createNewChildElement ("VirtualTargets");
  515. for (ConstConfigIterator config (*this); config.next();)
  516. {
  517. StringArray allTargets;
  518. for (auto target : targets)
  519. if (target->type != ProjectType::Target::AggregateTarget)
  520. allTargets.add (target->getTargetNameForConfiguration (*config));
  521. for (auto target : targets)
  522. {
  523. if (target->type == ProjectType::Target::AggregateTarget)
  524. {
  525. auto* configTarget = virtualTargets->createNewChildElement ("Add");
  526. configTarget->setAttribute ("alias", config->getName());
  527. configTarget->setAttribute ("targets", allTargets.joinIntoString (";"));
  528. }
  529. }
  530. }
  531. }
  532. StringArray getProjectCompilerOptions() const
  533. {
  534. return { "-Wall", "-Wno-strict-aliasing", "-Wno-strict-overflow" };
  535. }
  536. void addProjectCompilerOptions (XmlElement& xml) const
  537. {
  538. XmlElement* const compiler = xml.createNewChildElement ("Compiler");
  539. for (auto& option : getProjectCompilerOptions())
  540. setAddOption (*compiler, "option", option);
  541. }
  542. StringArray getProjectLinkerLibs() const
  543. {
  544. StringArray result;
  545. if (isWindows())
  546. result.addArray ({ "gdi32", "user32", "kernel32", "comctl32" });
  547. result.addTokens (getExternalLibrariesString(), ";\n", "\"'");
  548. result = getCleanedStringArray (result);
  549. for (auto& option : result)
  550. option = replacePreprocessorDefs (getAllPreprocessorDefs(), option);
  551. return result;
  552. }
  553. void addProjectLinkerOptions (XmlElement& xml) const
  554. {
  555. XmlElement* const linker = xml.createNewChildElement ("Linker");
  556. for (auto& lib : getProjectLinkerLibs())
  557. setAddOption (*linker, "library", lib);
  558. }
  559. CodeBlocksTarget& getTargetWithType (ProjectType::Target::Type type) const
  560. {
  561. CodeBlocksTarget* nonAggregrateTarget = nullptr;
  562. for (auto* target : targets)
  563. {
  564. if (target->type == type)
  565. return *target;
  566. if (target->type != ProjectType::Target::AggregateTarget)
  567. nonAggregrateTarget = target;
  568. }
  569. // this project has no valid targets
  570. jassert (nonAggregrateTarget != nullptr);
  571. return *nonAggregrateTarget;
  572. }
  573. // Returns SharedCode target for multi-target projects, otherwise it returns
  574. // the single target
  575. CodeBlocksTarget& getMainTarget() const
  576. {
  577. if (getProject().getProjectType().isAudioPlugin())
  578. return getTargetWithType (ProjectType::Target::SharedCodeTarget);
  579. for (auto* target : targets)
  580. if (target->type != ProjectType::Target::AggregateTarget)
  581. return *target;
  582. jassertfalse;
  583. return *targets[0];
  584. }
  585. CodeBlocksTarget& getTargetForProjectItem (const Project::Item& projectItem) const
  586. {
  587. if (getProject().getProjectType().isAudioPlugin())
  588. {
  589. if (! projectItem.shouldBeCompiled())
  590. return getTargetWithType (ProjectType::Target::SharedCodeTarget);
  591. return getTargetWithType (getProject().getTargetTypeFromFilePath (projectItem.getFile(), true));
  592. }
  593. return getMainTarget();
  594. }
  595. void addCompileUnits (const Project::Item& projectItem, XmlElement& xml) const
  596. {
  597. if (projectItem.isGroup())
  598. {
  599. for (int i = 0; i < projectItem.getNumChildren(); ++i)
  600. addCompileUnits (projectItem.getChild(i), xml);
  601. }
  602. else if (projectItem.shouldBeAddedToTargetProject())
  603. {
  604. const RelativePath file (projectItem.getFile(), getTargetFolder(), RelativePath::buildTargetFolder);
  605. XmlElement* unit = xml.createNewChildElement ("Unit");
  606. unit->setAttribute ("filename", file.toUnixStyle());
  607. for (ConstConfigIterator config (*this); config.next();)
  608. {
  609. const String& targetName = getTargetForProjectItem (projectItem).getTargetNameForConfiguration (*config);
  610. unit->createNewChildElement ("Option")->setAttribute ("target", targetName);
  611. }
  612. if (! projectItem.shouldBeCompiled())
  613. {
  614. unit->createNewChildElement ("Option")->setAttribute ("compile", 0);
  615. unit->createNewChildElement ("Option")->setAttribute ("link", 0);
  616. }
  617. }
  618. }
  619. void addCompileUnits (XmlElement& xml) const
  620. {
  621. for (int i = 0; i < getAllGroups().size(); ++i)
  622. addCompileUnits (getAllGroups().getReference(i), xml);
  623. }
  624. void createProject (XmlElement& xml) const
  625. {
  626. addOptions (xml);
  627. addBuild (xml);
  628. addVirtualTargets (xml);
  629. addProjectCompilerOptions (xml);
  630. addProjectLinkerOptions (xml);
  631. addCompileUnits (xml);
  632. }
  633. void setAddOption (XmlElement& xml, const String& nm, const String& value) const
  634. {
  635. xml.createNewChildElement ("Add")->setAttribute (nm, value);
  636. }
  637. CodeBlocksOS os;
  638. OwnedArray<CodeBlocksTarget> targets;
  639. friend class CLionProjectExporter;
  640. JUCE_DECLARE_NON_COPYABLE (CodeBlocksProjectExporter)
  641. };