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.

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