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.

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