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.

1124 lines
51KB

  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. #include "jucer_ProjectExport_CodeBlocks.h"
  21. #include "jucer_ProjectExport_Make.h"
  22. #include "jucer_ProjectExport_Xcode.h"
  23. //==============================================================================
  24. class CLionProjectExporter : public ProjectExporter
  25. {
  26. protected:
  27. //==============================================================================
  28. class CLionBuildConfiguration : public BuildConfiguration
  29. {
  30. public:
  31. CLionBuildConfiguration (Project& p, const ValueTree& settings, const ProjectExporter& e)
  32. : BuildConfiguration (p, settings, e)
  33. {
  34. }
  35. void createConfigProperties (PropertyListBuilder&) override {}
  36. String getModuleLibraryArchName() const override { return {}; }
  37. };
  38. BuildConfiguration::Ptr createBuildConfig (const ValueTree& tree) const override
  39. {
  40. return *new CLionBuildConfiguration (project, tree, *this);
  41. }
  42. public:
  43. //==============================================================================
  44. static const char* getName() { return "CLion (beta)"; }
  45. static const char* getValueTreeTypeName() { return "CLION"; }
  46. static CLionProjectExporter* createForSettings (Project& project, const ValueTree& settings)
  47. {
  48. if (settings.hasType (getValueTreeTypeName()))
  49. return new CLionProjectExporter (project, settings);
  50. return nullptr;
  51. }
  52. static bool isExporterSupported (const ProjectExporter& exporter)
  53. {
  54. return exporter.isMakefile()
  55. || (exporter.isXcode() && ! exporter.isiOS())
  56. || (exporter.isCodeBlocks() && exporter.isWindows());
  57. }
  58. //==============================================================================
  59. CLionProjectExporter (Project& p, const ValueTree& t) : ProjectExporter (p, t)
  60. {
  61. name = getName();
  62. targetLocationValue.setDefault (getDefaultBuildsRootFolder() + getTargetFolderForExporter (getValueTreeTypeName()));
  63. }
  64. //==============================================================================
  65. bool usesMMFiles() const override { return false; }
  66. bool canCopeWithDuplicateFiles() override { return false; }
  67. bool supportsUserDefinedConfigurations() const override { return false; }
  68. bool isXcode() const override { return false; }
  69. bool isVisualStudio() const override { return false; }
  70. bool isCodeBlocks() const override { return false; }
  71. bool isMakefile() const override { return false; }
  72. bool isAndroidStudio() const override { return false; }
  73. bool isCLion() const override { return true; }
  74. bool isAndroid() const override { return false; }
  75. bool isWindows() const override { return false; }
  76. bool isLinux() const override { return false; }
  77. bool isOSX() const override { return false; }
  78. bool isiOS() const override { return false; }
  79. bool supportsTargetType (ProjectType::Target::Type) const override { return true; }
  80. void addPlatformSpecificSettingsForProjectType (const ProjectType&) override {}
  81. //==============================================================================
  82. bool canLaunchProject() override
  83. {
  84. #if JUCE_MAC
  85. static Identifier exporterName ("XCODE_MAC");
  86. #elif JUCE_WINDOWS
  87. static Identifier exporterName ("CODEBLOCKS_WINDOWS");
  88. #elif JUCE_LINUX
  89. static Identifier exporterName ("LINUX_MAKE");
  90. #else
  91. static Identifier exporterName;
  92. #endif
  93. if (getProject().getExporters().getChildWithName (exporterName).isValid())
  94. return getCLionExecutableOrApp().exists();
  95. return false;
  96. }
  97. bool launchProject() override
  98. {
  99. return getCLionExecutableOrApp().startAsProcess (getTargetFolder().getFullPathName().quoted());
  100. }
  101. String getDescription() override
  102. {
  103. String description;
  104. description << "The " << getName() << " exporter produces a single CMakeLists.txt file with "
  105. << "multiple platform dependent sections, where the configuration for each section "
  106. << "is inherited from other exporters added to this project." << newLine
  107. << newLine
  108. << "The exporters which provide the CLion configuration for the corresponding platform are:" << newLine
  109. << newLine;
  110. for (auto& exporterName : getExporterNames())
  111. {
  112. std::unique_ptr<ProjectExporter> exporter (createNewExporter (getProject(), exporterName));
  113. if (isExporterSupported (*exporter))
  114. description << exporter->getName() << newLine;
  115. }
  116. description << newLine
  117. << "Add these exporters to the project to enable CLion builds." << newLine
  118. << newLine
  119. << "Not all features of all the exporters are currently supported. Notable omissions are AUv3 "
  120. << "plug-ins, embedding resources and fat binaries on MacOS, and adding application icons. On "
  121. << "Windows the CLion exporter requires a GCC-based compiler like MinGW.";
  122. return description;
  123. }
  124. void createExporterProperties (PropertyListBuilder& properties) override
  125. {
  126. for (Project::ExporterIterator exporter (getProject()); exporter.next();)
  127. if (isExporterSupported (*exporter))
  128. properties.add (new BooleanPropertyComponent (getExporterEnabledValue (*exporter), "Import settings from exporter", exporter->getName()),
  129. "If this is enabled then settings from the corresponding exporter will "
  130. "be used in the generated CMakeLists.txt");
  131. }
  132. void createDefaultConfigs() override {}
  133. void create (const OwnedArray<LibraryModule>&) const override
  134. {
  135. MemoryOutputStream out;
  136. out << "# Automatically generated CMakeLists, created by the Projucer" << newLine
  137. << "# Don't edit this file! Your changes will be overwritten when you re-save the Projucer project!" << newLine
  138. << newLine;
  139. out << "cmake_minimum_required (VERSION 3.4.1)" << newLine
  140. << newLine;
  141. out << "if (NOT CMAKE_BUILD_TYPE)" << newLine
  142. << " set (CMAKE_BUILD_TYPE \"Debug\" CACHE STRING \"Choose the type of build.\" FORCE)" << newLine
  143. << "endif (NOT CMAKE_BUILD_TYPE)" << newLine
  144. << newLine;
  145. // We'll append to this later.
  146. overwriteFileIfDifferentOrThrow (getTargetFolder().getChildFile ("CMakeLists.txt"), out);
  147. // CMake has stopped adding PkgInfo files to bundles, so we need to do it manually
  148. MemoryOutputStream pkgInfoOut;
  149. pkgInfoOut << "BNDL????";
  150. overwriteFileIfDifferentOrThrow (getTargetFolder().getChildFile ("PkgInfo"), out);
  151. }
  152. void writeCMakeListsExporterSection (ProjectExporter* exporter) const
  153. {
  154. if (! (isExporterSupported (*exporter) && isExporterEnabled (*exporter)))
  155. return;
  156. MemoryBlock existingContent;
  157. getTargetFolder().getChildFile ("CMakeLists.txt").loadFileAsData (existingContent);
  158. MemoryOutputStream out (existingContent, true);
  159. out << "###############################################################################" << newLine
  160. << "# " << exporter->getName() << newLine
  161. << "###############################################################################" << newLine
  162. << newLine;
  163. if (auto* makefileExporter = dynamic_cast<MakefileProjectExporter*> (exporter))
  164. {
  165. out << "if (UNIX AND NOT APPLE)" << newLine << newLine;
  166. writeCMakeListsMakefileSection (out, *makefileExporter);
  167. }
  168. else if (auto* xcodeExporter = dynamic_cast<XcodeProjectExporter*> (exporter))
  169. {
  170. out << "if (APPLE)" << newLine << newLine;
  171. writeCMakeListsXcodeSection (out, *xcodeExporter);
  172. }
  173. else if (auto* codeBlocksExporter = dynamic_cast<CodeBlocksProjectExporter*> (exporter))
  174. {
  175. out << "if (WIN32)" << newLine << newLine;
  176. writeCMakeListsCodeBlocksSection (out, *codeBlocksExporter);
  177. }
  178. out << "endif()" << newLine << newLine;
  179. overwriteFileIfDifferentOrThrow (getTargetFolder().getChildFile ("CMakeLists.txt"), out);
  180. }
  181. private:
  182. //==============================================================================
  183. static File getCLionExecutableOrApp()
  184. {
  185. File clionExeOrApp (getAppSettings()
  186. .getStoredPath (Ids::clionExePath)
  187. .toString()
  188. .replace ("${user.home}", File::getSpecialLocation (File::userHomeDirectory).getFullPathName()));
  189. #if JUCE_MAC
  190. if (clionExeOrApp.getFullPathName().endsWith ("/Contents/MacOS/clion"))
  191. clionExeOrApp = clionExeOrApp.getParentDirectory()
  192. .getParentDirectory()
  193. .getParentDirectory();
  194. #endif
  195. return clionExeOrApp;
  196. }
  197. //==============================================================================
  198. Identifier getExporterEnabledId (const ProjectExporter& exporter) const
  199. {
  200. jassert (isExporterSupported (exporter));
  201. if (exporter.isMakefile()) return Ids::clionMakefileEnabled;
  202. else if (exporter.isXcode()) return Ids::clionXcodeEnabled;
  203. else if (exporter.isCodeBlocks()) return Ids::clionCodeBlocksEnabled;
  204. jassertfalse;
  205. return {};
  206. }
  207. bool isExporterEnabled (const ProjectExporter& exporter) const
  208. {
  209. auto setting = settings[getExporterEnabledId (exporter)];
  210. return setting.isVoid() || setting;
  211. }
  212. Value getExporterEnabledValue (const ProjectExporter& exporter)
  213. {
  214. auto enabledID = getExporterEnabledId (exporter);
  215. getSetting (enabledID) = isExporterEnabled (exporter);
  216. return getSetting (enabledID);
  217. }
  218. //==============================================================================
  219. static bool isWindowsAbsolutePath (const String& path)
  220. {
  221. return path.length() > 1 && path[1] == ':';
  222. }
  223. static bool isUnixAbsolutePath (const String& path)
  224. {
  225. return path.isNotEmpty() && (path[0] == '/' || path[0] == '~' || path.startsWith ("$ENV{HOME}"));
  226. }
  227. //==============================================================================
  228. static String setCMakeVariable (const String& variableName, const String& value)
  229. {
  230. return "set (" + variableName + " \"" + value + "\")";
  231. }
  232. static String addToCMakeVariable (const String& variableName, const String& value)
  233. {
  234. return setCMakeVariable (variableName, "${" + variableName + "} " + value);
  235. }
  236. static String getTargetVarName (ProjectType::Target& target)
  237. {
  238. return String (target.getName()).toUpperCase().replaceCharacter (L' ', L'_');
  239. }
  240. template <class Target, class Exporter>
  241. void getFileInfoList (Target& target, Exporter& exporter, const Project::Item& projectItem, std::vector<std::pair<String, bool>>& fileInfoList) const
  242. {
  243. auto targetType = (getProject().getProjectType().isAudioPlugin() ? target.type : Target::Type::SharedCodeTarget);
  244. if (projectItem.isGroup())
  245. {
  246. for (int i = 0; i < projectItem.getNumChildren(); ++i)
  247. getFileInfoList (target, exporter, projectItem.getChild(i), fileInfoList);
  248. }
  249. else if (projectItem.shouldBeAddedToTargetProject() && getProject().getTargetTypeFromFilePath (projectItem.getFile(), true) == targetType )
  250. {
  251. auto path = RelativePath (projectItem.getFile(), exporter.getTargetFolder(), RelativePath::buildTargetFolder).toUnixStyle();
  252. fileInfoList.push_back ({ path, projectItem.shouldBeCompiled() });
  253. }
  254. }
  255. template <class Exporter>
  256. void writeCMakeTargets (OutputStream& out, Exporter& exporter) const
  257. {
  258. for (auto* target : exporter.targets)
  259. {
  260. if (target->type == ProjectType::Target::Type::AggregateTarget
  261. || target->type == ProjectType::Target::Type::AudioUnitv3PlugIn)
  262. continue;
  263. String functionName;
  264. StringArray properties;
  265. switch (target->getTargetFileType())
  266. {
  267. case ProjectType::Target::TargetFileType::executable:
  268. functionName = "add_executable";
  269. if (exporter.isCodeBlocks() && exporter.isWindows()
  270. && target->type != ProjectType::Target::Type::ConsoleApp)
  271. properties.add ("WIN32");
  272. break;
  273. case ProjectType::Target::TargetFileType::staticLibrary:
  274. case ProjectType::Target::TargetFileType::sharedLibraryOrDLL:
  275. case ProjectType::Target::TargetFileType::pluginBundle:
  276. functionName = "add_library";
  277. if (target->getTargetFileType() == ProjectType::Target::TargetFileType::staticLibrary)
  278. properties.add ("STATIC");
  279. else if (target->getTargetFileType() == ProjectType::Target::TargetFileType::sharedLibraryOrDLL)
  280. properties.add ("SHARED");
  281. else
  282. properties.add ("MODULE");
  283. break;
  284. default:
  285. continue;
  286. }
  287. out << functionName << " (" << getTargetVarName (*target);
  288. if (! properties.isEmpty())
  289. out << " " << properties.joinIntoString (" ");
  290. out << newLine;
  291. std::vector<std::pair<String, bool>> fileInfoList;
  292. for (auto& group : exporter.getAllGroups())
  293. getFileInfoList (*target, exporter, group, fileInfoList);
  294. for (auto& fileInfo : fileInfoList)
  295. out << " " << fileInfo.first.quoted() << newLine;
  296. auto isCMakeBundle = exporter.isXcode() && target->getTargetFileType() == ProjectType::Target::TargetFileType::pluginBundle;
  297. String pkgInfoPath = File (getTargetFolder().getChildFile ("PkgInfo")).getFullPathName().quoted();
  298. if (isCMakeBundle)
  299. out << " " << pkgInfoPath << newLine;
  300. out << ")" << newLine << newLine;
  301. if (isCMakeBundle)
  302. out << "set_source_files_properties (" << pkgInfoPath << " PROPERTIES MACOSX_PACKAGE_LOCATION .)" << newLine;
  303. for (auto& fileInfo : fileInfoList)
  304. if (! fileInfo.second)
  305. out << "set_source_files_properties (" << fileInfo.first.quoted() << " PROPERTIES HEADER_FILE_ONLY TRUE)" << newLine;
  306. out << newLine;
  307. }
  308. }
  309. //==============================================================================
  310. void writeCMakeListsMakefileSection (OutputStream& out, MakefileProjectExporter& exporter) const
  311. {
  312. out << "project (" << getProject().getProjectNameString().quoted() << " C CXX)" << newLine
  313. << newLine;
  314. out << "find_package (PkgConfig REQUIRED)" << newLine;
  315. StringArray cmakePkgconfigPackages;
  316. for (auto& package : exporter.getPackages())
  317. {
  318. cmakePkgconfigPackages.add (package.toUpperCase());
  319. out << "pkg_search_module (" << cmakePkgconfigPackages.strings.getLast() << " REQUIRED " << package << ")" << newLine;
  320. }
  321. out << newLine;
  322. writeCMakeTargets (out, exporter);
  323. for (auto* target : exporter.targets)
  324. {
  325. if (target->type == ProjectType::Target::Type::AggregateTarget)
  326. continue;
  327. if (target->getTargetFileType() == ProjectType::Target::TargetFileType::pluginBundle)
  328. out << "set_target_properties (" << getTargetVarName (*target) << " PROPERTIES PREFIX \"\")" << newLine;
  329. out << "set_target_properties (" << getTargetVarName (*target) << " PROPERTIES SUFFIX \"" << target->getTargetFileSuffix() << "\")" << newLine
  330. << newLine;
  331. }
  332. for (ProjectExporter::ConstConfigIterator c (exporter); c.next();)
  333. {
  334. auto& config = dynamic_cast<const MakefileProjectExporter::MakeBuildConfiguration&> (*c);
  335. out << "#------------------------------------------------------------------------------" << newLine
  336. << "# Config: " << config.getName() << newLine
  337. << "#------------------------------------------------------------------------------" << newLine
  338. << newLine;
  339. auto buildTypeCondition = String ("CMAKE_BUILD_TYPE STREQUAL " + config.getName());
  340. out << "if (" << buildTypeCondition << ")" << newLine
  341. << newLine;
  342. out << "execute_process (COMMAND uname -m OUTPUT_VARIABLE JUCE_ARCH_LABEL OUTPUT_STRIP_TRAILING_WHITESPACE)" << newLine
  343. << newLine;
  344. out << "include_directories (" << newLine;
  345. for (auto& path : exporter.getHeaderSearchPaths (config))
  346. out << " " << path.quoted() << newLine;
  347. for (auto& package : cmakePkgconfigPackages)
  348. out << " ${" << package << "_INCLUDE_DIRS}" << newLine;
  349. out << ")" << newLine << newLine;
  350. StringArray cmakeFoundLibraries;
  351. for (auto& library : exporter.getLibraryNames (config))
  352. {
  353. String cmakeLibraryID (library.toUpperCase());
  354. cmakeFoundLibraries.add (String ("${") + cmakeLibraryID + "}");
  355. out << "find_library (" << cmakeLibraryID << " " << library << newLine;
  356. for (auto& path : exporter.getLibrarySearchPaths (config))
  357. out << " " << path.quoted() << newLine;
  358. out << ")" << newLine
  359. << newLine;
  360. }
  361. for (auto* target : exporter.targets)
  362. {
  363. if (target->type == ProjectType::Target::Type::AggregateTarget)
  364. continue;
  365. auto targetVarName = getTargetVarName (*target);
  366. out << "set_target_properties (" << targetVarName << " PROPERTIES" << newLine
  367. << " OUTPUT_NAME " << config.getTargetBinaryNameString().quoted() << newLine;
  368. auto cxxStandard = project.getCppStandardString();
  369. if (cxxStandard == "latest")
  370. cxxStandard = "17";
  371. out << " CXX_STANDARD " << cxxStandard << newLine;
  372. if (! shouldUseGNUExtensions())
  373. out << " CXX_EXTENSIONS OFF" << newLine;
  374. out << ")" << newLine << newLine;
  375. auto defines = exporter.getDefines (config);
  376. defines.addArray (target->getDefines (config));
  377. out << "target_compile_definitions (" << targetVarName << " PRIVATE" << newLine;
  378. for (auto& key : defines.getAllKeys())
  379. out << " " << key << "=" << defines[key] << newLine;
  380. out << ")" << newLine << newLine;
  381. auto targetFlags = target->getCompilerFlags();
  382. if (! targetFlags.isEmpty())
  383. {
  384. out << "target_compile_options (" << targetVarName << " PRIVATE" << newLine;
  385. for (auto& flag : targetFlags)
  386. out << " " << flag << newLine;
  387. out << ")" << newLine << newLine;
  388. }
  389. out << "target_link_libraries (" << targetVarName << " PRIVATE" << newLine;
  390. if (target->getTargetFileType() == ProjectType::Target::TargetFileType::pluginBundle
  391. || target->type == ProjectType::Target::Type::StandalonePlugIn)
  392. out << " SHARED_CODE" << newLine;
  393. out << " " << exporter.getArchFlags (config) << newLine;
  394. for (auto& flag : target->getLinkerFlags())
  395. out << " " << flag << newLine;
  396. for (auto& flag : exporter.getLinkerFlags (config))
  397. out << " " << flag << newLine;
  398. for (auto& lib : cmakeFoundLibraries)
  399. out << " " << lib << newLine;
  400. for (auto& package : cmakePkgconfigPackages)
  401. out << " ${" << package << "_LIBRARIES}" << newLine;
  402. out << ")" << newLine << newLine;
  403. if (target->getTargetFileType() == ProjectType::Target::TargetFileType::pluginBundle
  404. || target->type == ProjectType::Target::Type::StandalonePlugIn)
  405. out << "add_dependencies (" << targetVarName << " " << "SHARED_CODE)" << newLine << newLine;
  406. }
  407. StringArray cFlags;
  408. cFlags.add (exporter.getArchFlags (config));
  409. cFlags.addArray (exporter.getCPreprocessorFlags (config));
  410. cFlags.addArray (exporter.getCFlags (config));
  411. out << addToCMakeVariable ("CMAKE_C_FLAGS", cFlags.joinIntoString (" ")) << newLine;
  412. String cxxFlags;
  413. for (auto& flag : exporter.getCXXFlags())
  414. if (! flag.startsWith ("-std="))
  415. cxxFlags += " " + flag;
  416. out << addToCMakeVariable ("CMAKE_CXX_FLAGS", "${CMAKE_C_FLAGS} " + cxxFlags) << newLine
  417. << newLine;
  418. out << "endif (" << buildTypeCondition << ")" << newLine
  419. << newLine;
  420. }
  421. }
  422. //==============================================================================
  423. void writeCMakeListsCodeBlocksSection (OutputStream& out, CodeBlocksProjectExporter& exporter) const
  424. {
  425. out << "project (" << getProject().getProjectNameString().quoted() << " C CXX)" << newLine
  426. << newLine;
  427. writeCMakeTargets (out, exporter);
  428. for (auto* target : exporter.targets)
  429. {
  430. if (target->type == ProjectType::Target::Type::AggregateTarget)
  431. continue;
  432. out << "set_target_properties (" << getTargetVarName (*target) << " PROPERTIES PREFIX \"\")" << newLine
  433. << "set_target_properties (" << getTargetVarName (*target) << " PROPERTIES SUFFIX " << target->getTargetSuffix().quoted() << ")" << newLine
  434. << newLine;
  435. }
  436. for (ProjectExporter::ConstConfigIterator c (exporter); c.next();)
  437. {
  438. auto& config = dynamic_cast<const CodeBlocksProjectExporter::CodeBlocksBuildConfiguration&> (*c);
  439. out << "#------------------------------------------------------------------------------" << newLine
  440. << "# Config: " << config.getName() << newLine
  441. << "#------------------------------------------------------------------------------" << newLine
  442. << newLine;
  443. auto buildTypeCondition = String ("CMAKE_BUILD_TYPE STREQUAL " + config.getName());
  444. out << "if (" << buildTypeCondition << ")" << newLine
  445. << newLine;
  446. out << "include_directories (" << newLine;
  447. for (auto& path : exporter.getIncludePaths (config))
  448. out << " " << path.replace ("\\", "/").quoted() << newLine;
  449. out << ")" << newLine << newLine;
  450. for (auto* target : exporter.targets)
  451. {
  452. if (target->type == ProjectType::Target::Type::AggregateTarget)
  453. continue;
  454. auto targetVarName = getTargetVarName (*target);
  455. out << "set_target_properties (" << targetVarName << " PROPERTIES" << newLine
  456. << " OUTPUT_NAME " << config.getTargetBinaryNameString().quoted() << newLine;
  457. auto cxxStandard = project.getCppStandardString();
  458. if (cxxStandard == "latest")
  459. cxxStandard = "17";
  460. out << " CXX_STANDARD " << cxxStandard << newLine;
  461. if (! shouldUseGNUExtensions())
  462. out << " CXX_EXTENSIONS OFF" << newLine;
  463. out << ")" << newLine << newLine;
  464. out << "target_compile_definitions (" << targetVarName << " PRIVATE" << newLine;
  465. for (auto& def : exporter.getDefines (config, *target))
  466. out << " " << def << newLine;
  467. out << ")" << newLine << newLine;
  468. out << "target_compile_options (" << targetVarName << " PRIVATE" << newLine;
  469. for (auto& option : exporter.getCompilerFlags (config, *target))
  470. if (! option.startsWith ("-std="))
  471. out << " " << option.quoted() << newLine;
  472. out << ")" << newLine << newLine;
  473. out << "target_link_libraries (" << targetVarName << " PRIVATE" << newLine;
  474. if (target->getTargetFileType() == ProjectType::Target::TargetFileType::pluginBundle
  475. || target->type == ProjectType::Target::Type::StandalonePlugIn)
  476. out << " SHARED_CODE" << newLine
  477. << " -L." << newLine;
  478. for (auto& path : exporter.getLinkerSearchPaths (config, *target))
  479. {
  480. out << " \"-L\\\"";
  481. if (! isWindowsAbsolutePath (path))
  482. out << "${CMAKE_CURRENT_SOURCE_DIR}/";
  483. out << path.replace ("\\", "/") << "\\\"\"" << newLine;
  484. }
  485. for (auto& flag : exporter.getLinkerFlags (config, *target))
  486. out << " " << flag << newLine;
  487. for (auto& flag : exporter.getProjectLinkerLibs())
  488. out << " -l" << flag << newLine;
  489. for (auto& lib : exporter.mingwLibs)
  490. out << " -l" << lib << newLine;
  491. out << ")" << newLine << newLine;
  492. }
  493. out << addToCMakeVariable ("CMAKE_CXX_FLAGS", exporter.getProjectCompilerOptions().joinIntoString (" ")) << newLine
  494. << addToCMakeVariable ("CMAKE_C_FLAGS", "${CMAKE_CXX_FLAGS}") << newLine
  495. << newLine;
  496. out << "endif (" << buildTypeCondition << ")" << newLine
  497. << newLine;
  498. }
  499. }
  500. //==============================================================================
  501. void writeCMakeListsXcodeSection (OutputStream& out, XcodeProjectExporter& exporter) const
  502. {
  503. // We need to dind out the SDK root before defining the project. Unfortunately this is
  504. // set per-target in the Xcode project, but we want it per-configuration.
  505. for (ProjectExporter::ConstConfigIterator c (exporter); c.next();)
  506. {
  507. auto& config = dynamic_cast<const XcodeProjectExporter::XcodeBuildConfiguration&> (*c);
  508. for (auto* target : exporter.targets)
  509. {
  510. if (target->getTargetFileType() == ProjectType::Target::TargetFileType::macOSAppex
  511. || target->type == ProjectType::Target::Type::AggregateTarget
  512. || target->type == ProjectType::Target::Type::AudioUnitv3PlugIn)
  513. continue;
  514. auto targetAttributes = target->getTargetSettings (config);
  515. auto targetAttributeKeys = targetAttributes.getAllKeys();
  516. if (targetAttributes.getAllKeys().contains ("SDKROOT"))
  517. {
  518. out << "if (CMAKE_BUILD_TYPE STREQUAL " + config.getName() << ")" << newLine
  519. << " set (CMAKE_OSX_SYSROOT " << targetAttributes["SDKROOT"] << ")" << newLine
  520. << "endif()" << newLine << newLine;
  521. break;
  522. }
  523. }
  524. }
  525. out << "project (" << getProject().getProjectNameString().quoted() << " C CXX)" << newLine << newLine;
  526. writeCMakeTargets (out, exporter);
  527. for (auto* target : exporter.targets)
  528. {
  529. if (target->getTargetFileType() == ProjectType::Target::TargetFileType::macOSAppex
  530. || target->type == ProjectType::Target::Type::AggregateTarget
  531. || target->type == ProjectType::Target::Type::AudioUnitv3PlugIn)
  532. continue;
  533. if (target->type == ProjectType::Target::Type::AudioUnitPlugIn)
  534. out << "find_program (RC_COMPILER Rez NO_DEFAULT_PATHS PATHS /Applications/Xcode.app/Contents/Developer/usr/bin)" << newLine
  535. << "if (NOT RC_COMPILER)" << newLine
  536. << " message (WARNING \"failed to find Rez; older resource-based AU plug-ins may not work correctly\")" << newLine
  537. << "endif (NOT RC_COMPILER)" << newLine << newLine;
  538. if (target->getTargetFileType() == ProjectType::Target::TargetFileType::staticLibrary
  539. || target->getTargetFileType() == ProjectType::Target::TargetFileType::sharedLibraryOrDLL)
  540. out << "set_target_properties (" << getTargetVarName (*target) << " PROPERTIES SUFFIX \"" << target->xcodeBundleExtension << "\")" << newLine
  541. << newLine;
  542. }
  543. for (ProjectExporter::ConstConfigIterator c (exporter); c.next();)
  544. {
  545. auto& config = dynamic_cast<const XcodeProjectExporter::XcodeBuildConfiguration&> (*c);
  546. out << "#------------------------------------------------------------------------------" << newLine
  547. << "# Config: " << config.getName() << newLine
  548. << "#------------------------------------------------------------------------------" << newLine
  549. << newLine;
  550. auto buildTypeCondition = String ("CMAKE_BUILD_TYPE STREQUAL " + config.getName());
  551. out << "if (" << buildTypeCondition << ")" << newLine
  552. << newLine;
  553. out << "execute_process (COMMAND uname -m OUTPUT_VARIABLE JUCE_ARCH_LABEL OUTPUT_STRIP_TRAILING_WHITESPACE)" << newLine
  554. << newLine;
  555. auto configSettings = exporter.getProjectSettings (config);
  556. auto configSettingsKeys = configSettings.getAllKeys();
  557. auto binaryName = config.getTargetBinaryNameString();
  558. if (configSettingsKeys.contains ("PRODUCT_NAME"))
  559. binaryName = configSettings["PRODUCT_NAME"].unquoted();
  560. for (auto* target : exporter.targets)
  561. {
  562. if (target->getTargetFileType() == ProjectType::Target::TargetFileType::macOSAppex
  563. || target->type == ProjectType::Target::Type::AggregateTarget
  564. || target->type == ProjectType::Target::Type::AudioUnitv3PlugIn)
  565. continue;
  566. auto targetVarName = getTargetVarName (*target);
  567. auto targetAttributes = target->getTargetSettings (config);
  568. auto targetAttributeKeys = targetAttributes.getAllKeys();
  569. StringArray headerSearchPaths;
  570. if (targetAttributeKeys.contains ("HEADER_SEARCH_PATHS"))
  571. {
  572. auto paths = targetAttributes["HEADER_SEARCH_PATHS"].trim().substring (1).dropLastCharacters (1);
  573. paths = paths.replace ("\"$(inherited)\"", {})
  574. .replace ("$(HOME)", "$ENV{HOME}")
  575. .replace ("~", "$ENV{HOME}");
  576. headerSearchPaths.addTokens (paths, ",\"\t\\", {});
  577. headerSearchPaths.removeEmptyStrings();
  578. targetAttributeKeys.removeString ("HEADER_SEARCH_PATHS");
  579. }
  580. out << "target_include_directories (" << targetVarName << " PRIVATE" << newLine;
  581. for (auto& path : headerSearchPaths)
  582. out << " " << path.quoted() << newLine;
  583. out << ")" << newLine << newLine;
  584. StringArray defines;
  585. if (targetAttributeKeys.contains ("GCC_PREPROCESSOR_DEFINITIONS"))
  586. {
  587. defines.addTokens (targetAttributes["GCC_PREPROCESSOR_DEFINITIONS"], "() ,\t", {});
  588. defines.removeEmptyStrings();
  589. targetAttributeKeys.removeString ("GCC_PREPROCESSOR_DEFINITIONS");
  590. }
  591. out << "target_compile_definitions (" << targetVarName << " PRIVATE" << newLine;
  592. for (auto& def : defines)
  593. out << " " << def << newLine;
  594. out << ")" << newLine << newLine;
  595. StringArray cppFlags;
  596. String archLabel ("${JUCE_ARCH_LABEL}");
  597. // Fat binaries are not supported.
  598. if (targetAttributeKeys.contains ("ARCHS"))
  599. {
  600. auto value = targetAttributes["ARCHS"].unquoted();
  601. if (value.contains ("NATIVE_ARCH_ACTUAL"))
  602. {
  603. cppFlags.add ("-march=native");
  604. }
  605. else if (value.contains ("ARCHS_STANDARD_32_BIT"))
  606. {
  607. archLabel = "i386";
  608. cppFlags.add ("-arch x86");
  609. }
  610. else if (value.contains ("ARCHS_STANDARD_32_64_BIT")
  611. || value.contains ("ARCHS_STANDARD_64_BIT"))
  612. {
  613. archLabel = "x86_64";
  614. cppFlags.add ("-arch x86_64");
  615. }
  616. targetAttributeKeys.removeString ("ARCHS");
  617. }
  618. if (targetAttributeKeys.contains ("MACOSX_DEPLOYMENT_TARGET"))
  619. {
  620. cppFlags.add ("-mmacosx-version-min=" + targetAttributes["MACOSX_DEPLOYMENT_TARGET"]);
  621. targetAttributeKeys.removeString ("MACOSX_DEPLOYMENT_TARGET");
  622. }
  623. if (targetAttributeKeys.contains ("OTHER_CPLUSPLUSFLAGS"))
  624. {
  625. cppFlags.add (targetAttributes["OTHER_CPLUSPLUSFLAGS"].unquoted());
  626. targetAttributeKeys.removeString ("OTHER_CPLUSPLUSFLAGS");
  627. }
  628. if (targetAttributeKeys.contains ("GCC_OPTIMIZATION_LEVEL"))
  629. {
  630. cppFlags.add ("-O" + targetAttributes["GCC_OPTIMIZATION_LEVEL"]);
  631. targetAttributeKeys.removeString ("GCC_OPTIMIZATION_LEVEL");
  632. }
  633. if (targetAttributeKeys.contains ("LLVM_LTO"))
  634. {
  635. cppFlags.add ("-flto");
  636. targetAttributeKeys.removeString ("LLVM_LTO");
  637. }
  638. if (targetAttributeKeys.contains ("GCC_FAST_MATH"))
  639. {
  640. cppFlags.add ("-ffast-math");
  641. targetAttributeKeys.removeString ("GCC_FAST_MATH");
  642. }
  643. // We'll take this setting from the project
  644. targetAttributeKeys.removeString ("CLANG_CXX_LANGUAGE_STANDARD");
  645. if (targetAttributeKeys.contains ("CLANG_CXX_LIBRARY"))
  646. {
  647. cppFlags.add ("-stdlib=" + targetAttributes["CLANG_CXX_LIBRARY"].unquoted());
  648. targetAttributeKeys.removeString ("CLANG_CXX_LIBRARY");
  649. }
  650. out << "target_compile_options (" << targetVarName << " PRIVATE" << newLine;
  651. for (auto& flag : cppFlags)
  652. out << " " << flag << newLine;
  653. out << ")" << newLine << newLine;
  654. StringArray libSearchPaths;
  655. if (targetAttributeKeys.contains ("LIBRARY_SEARCH_PATHS"))
  656. {
  657. auto paths = targetAttributes["LIBRARY_SEARCH_PATHS"].trim().substring (1).dropLastCharacters (1);
  658. paths = paths.replace ("\"$(inherited)\"", {});
  659. paths = paths.replace ("$(HOME)", "$ENV{HOME}");
  660. libSearchPaths.addTokens (paths, ",\"\t\\", {});
  661. libSearchPaths.removeEmptyStrings();
  662. for (auto& libPath : libSearchPaths)
  663. {
  664. libPath = libPath.replace ("${CURRENT_ARCH}", archLabel);
  665. if (! isUnixAbsolutePath (libPath))
  666. libPath = "${CMAKE_CURRENT_SOURCE_DIR}/" + libPath;
  667. }
  668. targetAttributeKeys.removeString ("LIBRARY_SEARCH_PATHS");
  669. }
  670. StringArray linkerFlags;
  671. if (targetAttributeKeys.contains ("OTHER_LDFLAGS"))
  672. {
  673. // CMake adds its own SHARED_CODE library linking flags
  674. auto flagsWithReplacedSpaces = targetAttributes["OTHER_LDFLAGS"].unquoted().replace ("\\\\ ", "^^%%^^");
  675. linkerFlags.addTokens (flagsWithReplacedSpaces, true);
  676. linkerFlags.removeString ("-bundle");
  677. linkerFlags.removeString ("-l" + binaryName.replace (" ", "^^%%^^"));
  678. for (auto& flag : linkerFlags)
  679. flag = flag.replace ("^^%%^^", " ");
  680. targetAttributeKeys.removeString ("OTHER_LDFLAGS");
  681. }
  682. if (target->type == ProjectType::Target::Type::AudioUnitPlugIn)
  683. {
  684. String rezFlags;
  685. if (targetAttributeKeys.contains ("OTHER_REZFLAGS"))
  686. {
  687. rezFlags = targetAttributes["OTHER_REZFLAGS"];
  688. targetAttributeKeys.removeString ("OTHER_REZFLAGS");
  689. }
  690. for (auto& item : exporter.getAllGroups())
  691. {
  692. if (item.getName() == ProjectSaver::getJuceCodeGroupName())
  693. {
  694. auto resSourcesVar = targetVarName + "_REZ_SOURCES";
  695. auto resOutputVar = targetVarName + "_REZ_OUTPUT";
  696. out << "if (RC_COMPILER)" << newLine
  697. << " set (" << resSourcesVar << newLine
  698. << " \"" << item.determineGroupFolder().getFullPathName() + "/include_juce_audio_plugin_client_AU.r\"" << newLine
  699. << " )" << newLine
  700. << " set (" << resOutputVar << " \"${CMAKE_CURRENT_BINARY_DIR}/" << binaryName << ".rsrc\")" << newLine
  701. << " target_sources (" << targetVarName << " PRIVATE" << newLine
  702. << " ${" << resSourcesVar << "}" << newLine
  703. << " ${" << resOutputVar << "}" << newLine
  704. << " )" << newLine
  705. << " execute_process (COMMAND" << newLine
  706. << " ${RC_COMPILER}" << newLine
  707. << " " << rezFlags.unquoted().removeCharacters ("\\") << newLine;
  708. for (auto& path : headerSearchPaths)
  709. {
  710. out << " -I \"";
  711. if (! isUnixAbsolutePath (path))
  712. out << "${PROJECT_SOURCE_DIR}/";
  713. out << path << "\"" << newLine;
  714. }
  715. out << " ${" << resSourcesVar << "}" << newLine
  716. << " -o ${" << resOutputVar << "}" << newLine
  717. << " )" << newLine
  718. << " set_source_files_properties (${" << resOutputVar << "} PROPERTIES" << newLine
  719. << " GENERATED TRUE" << newLine
  720. << " MACOSX_PACKAGE_LOCATION Resources" << newLine
  721. << " )" << newLine
  722. << "endif (RC_COMPILER)" << newLine << newLine;
  723. break;
  724. }
  725. }
  726. }
  727. if (targetAttributeKeys.contains ("INFOPLIST_FILE"))
  728. {
  729. auto plistFile = exporter.getTargetFolder().getChildFile (targetAttributes["INFOPLIST_FILE"]);
  730. XmlDocument infoPlistData (plistFile);
  731. std::unique_ptr<XmlElement> plist (infoPlistData.getDocumentElement());
  732. if (plist != nullptr)
  733. {
  734. if (auto* dict = plist->getChildByName ("dict"))
  735. {
  736. if (auto* entry = dict->getChildByName ("key"))
  737. {
  738. while (entry != nullptr)
  739. {
  740. if (entry->getAllSubText() == "CFBundleExecutable")
  741. {
  742. if (auto* bundleName = entry->getNextElementWithTagName ("string"))
  743. {
  744. bundleName->deleteAllTextElements();
  745. bundleName->addTextElement (binaryName);
  746. }
  747. }
  748. entry = entry->getNextElementWithTagName ("key");
  749. }
  750. }
  751. }
  752. auto updatedPlist = getTargetFolder().getChildFile (config.getName() + "-" + plistFile.getFileName());
  753. plist->writeToFile (updatedPlist, "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">");
  754. targetAttributes.set ("INFOPLIST_FILE", updatedPlist.getFullPathName().quoted());
  755. }
  756. else
  757. {
  758. targetAttributeKeys.removeString ("INFOPLIST_FILE");
  759. }
  760. }
  761. targetAttributeKeys.sort (false);
  762. out << "set_target_properties (" << targetVarName << " PROPERTIES" << newLine
  763. << " OUTPUT_NAME " << binaryName.quoted() << newLine;
  764. auto cxxStandard = project.getCppStandardString();
  765. if (cxxStandard == "latest")
  766. cxxStandard = "17";
  767. out << " CXX_STANDARD " << cxxStandard << newLine;
  768. if (! shouldUseGNUExtensions())
  769. out << " CXX_EXTENSIONS OFF" << newLine;
  770. for (auto& key : targetAttributeKeys)
  771. out << " XCODE_ATTRIBUTE_" << key << " " << targetAttributes[key] << newLine;
  772. if (target->getTargetFileType() == ProjectType::Target::TargetFileType::executable
  773. || target->getTargetFileType() == ProjectType::Target::TargetFileType::pluginBundle)
  774. {
  775. out << " MACOSX_BUNDLE_INFO_PLIST " << targetAttributes.getValue ("INFOPLIST_FILE", "\"\"") << newLine
  776. << " XCODE_ATTRIBUTE_PRODUCT_NAME " << binaryName.quoted() << newLine;
  777. if (target->getTargetFileType() == ProjectType::Target::TargetFileType::executable)
  778. {
  779. out << " MACOSX_BUNDLE TRUE" << newLine;
  780. }
  781. else
  782. {
  783. out << " BUNDLE TRUE" << newLine
  784. << " BUNDLE_EXTENSION " << targetAttributes.getValue ("WRAPPER_EXTENSION", "\"\"") << newLine
  785. << " XCODE_ATTRIBUTE_MACH_O_TYPE \"mh_bundle\"" << newLine;
  786. }
  787. }
  788. out << ")" << newLine << newLine;
  789. out << "target_link_libraries (" << targetVarName << " PRIVATE" << newLine;
  790. if (target->getTargetFileType() == ProjectType::Target::TargetFileType::pluginBundle
  791. || target->type == ProjectType::Target::Type::StandalonePlugIn)
  792. out << " SHARED_CODE" << newLine;
  793. for (auto& path : libSearchPaths)
  794. out << " \"-L\\\"" << path << "\\\"\"" << newLine;
  795. for (auto& flag : linkerFlags)
  796. out << " " << flag.quoted() << newLine;
  797. for (auto& framework : target->frameworkNames)
  798. out << " \"-framework " << framework << "\"" << newLine;
  799. out << ")" << newLine << newLine;
  800. if (target->getTargetFileType() == ProjectType::Target::TargetFileType::pluginBundle
  801. || target->type == ProjectType::Target::Type::StandalonePlugIn)
  802. {
  803. if (target->getTargetFileType() == ProjectType::Target::TargetFileType::pluginBundle
  804. && targetAttributeKeys.contains("INSTALL_PATH"))
  805. {
  806. auto installPath = targetAttributes["INSTALL_PATH"].unquoted().replace ("$(HOME)", "$ENV{HOME}");
  807. auto productFilename = binaryName + (targetAttributeKeys.contains ("WRAPPER_EXTENSION") ? "." + targetAttributes["WRAPPER_EXTENSION"] : String());
  808. auto productPath = (installPath + productFilename).quoted();
  809. out << "add_custom_command (TARGET " << targetVarName << " POST_BUILD" << newLine
  810. << " COMMAND ${CMAKE_COMMAND} -E remove_directory " << productPath << newLine
  811. << " COMMAND ${CMAKE_COMMAND} -E copy_directory \"${CMAKE_BINARY_DIR}/" << productFilename << "\" " << productPath << newLine
  812. << " COMMENT \"Copying \\\"" << productFilename << "\\\" to \\\"" << installPath.unquoted() << "\\\"\"" << newLine
  813. << ")" << newLine << newLine;
  814. }
  815. }
  816. }
  817. std::map<String, String> basicWarnings
  818. {
  819. { "CLANG_WARN_BOOL_CONVERSION", "bool-conversion" },
  820. { "CLANG_WARN_COMMA", "comma" },
  821. { "CLANG_WARN_CONSTANT_CONVERSION", "constant-conversion" },
  822. { "CLANG_WARN_EMPTY_BODY", "empty-body" },
  823. { "CLANG_WARN_ENUM_CONVERSION", "enum-conversion" },
  824. { "CLANG_WARN_INFINITE_RECURSION", "infinite-recursion" },
  825. { "CLANG_WARN_INT_CONVERSION", "int-conversion" },
  826. { "CLANG_WARN_RANGE_LOOP_ANALYSIS", "range-loop-analysis" },
  827. { "CLANG_WARN_STRICT_PROTOTYPES", "strict-prototypes" },
  828. { "GCC_WARN_CHECK_SWITCH_STATEMENTS", "switch" },
  829. { "GCC_WARN_UNUSED_VARIABLE", "unused-variable" },
  830. { "GCC_WARN_MISSING_PARENTHESES", "parentheses" },
  831. { "GCC_WARN_NON_VIRTUAL_DESTRUCTOR", "non-virtual-dtor" },
  832. { "GCC_WARN_64_TO_32_BIT_CONVERSION", "shorten-64-to-32" },
  833. { "GCC_WARN_UNDECLARED_SELECTOR", "undeclared-selector" },
  834. { "GCC_WARN_UNUSED_FUNCTION", "unused-function" }
  835. };
  836. StringArray compilerFlags;
  837. for (auto& key : configSettingsKeys)
  838. {
  839. auto basicWarning = basicWarnings.find (key);
  840. if (basicWarning != basicWarnings.end())
  841. {
  842. compilerFlags.add (configSettings[key] == "YES" ? "-W" + basicWarning->second : "-Wno-" + basicWarning->second);
  843. }
  844. else if (key == "CLANG_WARN_SUSPICIOUS_MOVE" && configSettings[key] == "YES") compilerFlags.add ("-Wmove");
  845. else if (key == "CLANG_WARN_UNREACHABLE_CODE" && configSettings[key] == "YES") compilerFlags.add ("-Wunreachable-code");
  846. else if (key == "CLANG_WARN__DUPLICATE_METHOD_MATCH" && configSettings[key] == "YES") compilerFlags.add ("-Wduplicate-method-match");
  847. else if (key == "GCC_INLINES_ARE_PRIVATE_EXTERN" && configSettings[key] == "YES") compilerFlags.add ("-fvisibility-inlines-hidden");
  848. else if (key == "GCC_NO_COMMON_BLOCKS" && configSettings[key] == "YES") compilerFlags.add ("-fno-common");
  849. else if (key == "GCC_WARN_ABOUT_RETURN_TYPE" && configSettings[key] != "YES") compilerFlags.add (configSettings[key] == "YES_ERROR" ? "-Werror=return-type" : "-Wno-return-type");
  850. else if (key == "GCC_WARN_TYPECHECK_CALLS_TO_PRINTF" && configSettings[key] != "YES") compilerFlags.add ("-Wno-format");
  851. else if (key == "GCC_WARN_UNINITIALIZED_AUTOS")
  852. {
  853. if (configSettings[key] == "YES") compilerFlags.add ("-Wuninitialized");
  854. else if (configSettings[key] == "YES_AGGRESSIVE") compilerFlags.add ("--Wconditional-uninitialized");
  855. else compilerFlags.add (")-Wno-uninitialized");
  856. }
  857. else if (key == "WARNING_CFLAGS") compilerFlags.add (configSettings[key]);
  858. }
  859. out << addToCMakeVariable ("CMAKE_CXX_FLAGS", compilerFlags.joinIntoString (" ")) << newLine
  860. << addToCMakeVariable ("CMAKE_C_FLAGS", "${CMAKE_CXX_FLAGS}") << newLine
  861. << newLine;
  862. out << "endif (" << buildTypeCondition << ")" << newLine
  863. << newLine;
  864. }
  865. }
  866. //==============================================================================
  867. JUCE_DECLARE_NON_COPYABLE (CLionProjectExporter)
  868. };