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.

1042 lines
41KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. #pragma once
  14. //==============================================================================
  15. class MakefileProjectExporter : public ProjectExporter
  16. {
  17. protected:
  18. //==============================================================================
  19. class MakeBuildConfiguration : public BuildConfiguration
  20. {
  21. public:
  22. MakeBuildConfiguration (Project& p, const ValueTree& settings, const ProjectExporter& e)
  23. : BuildConfiguration (p, settings, e),
  24. architectureTypeValue (config, Ids::linuxArchitecture, getUndoManager(), String()),
  25. pluginBinaryCopyStepValue (config, Ids::enablePluginBinaryCopyStep, getUndoManager(), true),
  26. vstBinaryLocation (config, Ids::vstBinaryLocation, getUndoManager(), "$(HOME)/.vst"),
  27. vst3BinaryLocation (config, Ids::vst3BinaryLocation, getUndoManager(), "$(HOME)/.vst3"),
  28. unityPluginBinaryLocation (config, Ids::unityPluginBinaryLocation, getUndoManager(), "$(HOME)/UnityPlugins")
  29. {
  30. linkTimeOptimisationValue.setDefault (false);
  31. optimisationLevelValue.setDefault (isDebug() ? gccO0 : gccO3);
  32. }
  33. void createConfigProperties (PropertyListBuilder& props) override
  34. {
  35. addRecommendedLinuxCompilerWarningsProperty (props);
  36. addGCCOptimisationProperty (props);
  37. props.add (new ChoicePropertyComponent (architectureTypeValue, "Architecture",
  38. { "<None>", "Native", "32-bit (-m32)", "64-bit (-m64)", "ARM v6", "ARM v7" },
  39. { { String() }, "-march=native", "-m32", "-m64", "-march=armv6", "-march=armv7" }),
  40. "Specifies the 32/64-bit architecture to use.");
  41. auto isBuildingAnyPlugins = (project.shouldBuildVST() || project.shouldBuildVST3() || project.shouldBuildUnityPlugin());
  42. if (isBuildingAnyPlugins)
  43. {
  44. props.add (new ChoicePropertyComponent (pluginBinaryCopyStepValue, "Enable Plugin Copy Step"),
  45. "Enable this to copy plugin binaries to a specified folder after building.");
  46. if (project.shouldBuildVST3())
  47. props.add (new TextPropertyComponentWithEnablement (vst3BinaryLocation, pluginBinaryCopyStepValue, "VST3 Binary Location",
  48. 1024, false),
  49. "The folder in which the compiled VST3 binary should be placed.");
  50. if (project.shouldBuildUnityPlugin())
  51. props.add (new TextPropertyComponentWithEnablement (unityPluginBinaryLocation, pluginBinaryCopyStepValue, "Unity Binary Location",
  52. 1024, false),
  53. "The folder in which the compiled Unity plugin binary and associated C# GUI script should be placed.");
  54. if (project.shouldBuildVST())
  55. props.add (new TextPropertyComponentWithEnablement (vstBinaryLocation, pluginBinaryCopyStepValue, "VST (Legacy) Binary Location",
  56. 1024, false),
  57. "The folder in which the compiled legacy VST binary should be placed.");
  58. }
  59. }
  60. String getModuleLibraryArchName() const override
  61. {
  62. auto archFlag = getArchitectureTypeString();
  63. String prefix ("-march=");
  64. if (archFlag.startsWith (prefix))
  65. return archFlag.substring (prefix.length());
  66. if (archFlag == "-m64")
  67. return "x86_64";
  68. if (archFlag == "-m32")
  69. return "i386";
  70. return "${JUCE_ARCH_LABEL}";
  71. }
  72. String getArchitectureTypeString() const { return architectureTypeValue.get(); }
  73. bool isPluginBinaryCopyStepEnabled() const { return pluginBinaryCopyStepValue.get(); }
  74. String getVSTBinaryLocationString() const { return vstBinaryLocation.get(); }
  75. String getVST3BinaryLocationString() const { return vst3BinaryLocation.get(); }
  76. String getUnityPluginBinaryLocationString() const { return unityPluginBinaryLocation.get(); }
  77. private:
  78. //==============================================================================
  79. ValueWithDefault architectureTypeValue, pluginBinaryCopyStepValue, vstBinaryLocation, vst3BinaryLocation, unityPluginBinaryLocation;
  80. };
  81. BuildConfiguration::Ptr createBuildConfig (const ValueTree& tree) const override
  82. {
  83. return *new MakeBuildConfiguration (project, tree, *this);
  84. }
  85. public:
  86. //==============================================================================
  87. class MakefileTarget : public build_tools::ProjectType::Target
  88. {
  89. public:
  90. MakefileTarget (build_tools::ProjectType::Target::Type targetType, const MakefileProjectExporter& exporter)
  91. : build_tools::ProjectType::Target (targetType), owner (exporter)
  92. {}
  93. StringArray getCompilerFlags() const
  94. {
  95. StringArray result;
  96. if (getTargetFileType() == sharedLibraryOrDLL || getTargetFileType() == pluginBundle)
  97. {
  98. result.add ("-fPIC");
  99. result.add ("-fvisibility=hidden");
  100. }
  101. return result;
  102. }
  103. StringArray getLinkerFlags() const
  104. {
  105. StringArray result;
  106. if (getTargetFileType() == sharedLibraryOrDLL || getTargetFileType() == pluginBundle)
  107. {
  108. result.add ("-shared");
  109. if (getTargetFileType() == pluginBundle)
  110. result.add ("-Wl,--no-undefined");
  111. }
  112. return result;
  113. }
  114. StringPairArray getDefines (const BuildConfiguration& config) const
  115. {
  116. StringPairArray result;
  117. auto commonOptionKeys = owner.getAllPreprocessorDefs (config, build_tools::ProjectType::Target::unspecified).getAllKeys();
  118. auto targetSpecific = owner.getAllPreprocessorDefs (config, type);
  119. for (auto& key : targetSpecific.getAllKeys())
  120. if (! commonOptionKeys.contains (key))
  121. result.set (key, targetSpecific[key]);
  122. return result;
  123. }
  124. StringArray getTargetSettings (const MakeBuildConfiguration& config) const
  125. {
  126. if (type == AggregateTarget) // the aggregate target should not specify any settings at all!
  127. return {}; // it just defines dependencies on the other targets.
  128. StringArray s;
  129. auto cppflagsVarName = "JUCE_CPPFLAGS_" + getTargetVarName();
  130. s.add (cppflagsVarName + " := " + createGCCPreprocessorFlags (getDefines (config)));
  131. auto cflags = getCompilerFlags();
  132. if (! cflags.isEmpty())
  133. s.add ("JUCE_CFLAGS_" + getTargetVarName() + " := " + cflags.joinIntoString (" "));
  134. auto ldflags = getLinkerFlags();
  135. if (! ldflags.isEmpty())
  136. s.add ("JUCE_LDFLAGS_" + getTargetVarName() + " := " + ldflags.joinIntoString (" "));
  137. auto targetName = owner.replacePreprocessorTokens (config, config.getTargetBinaryNameString());
  138. if (owner.projectType.isStaticLibrary())
  139. targetName = getStaticLibbedFilename (targetName);
  140. else if (owner.projectType.isDynamicLibrary())
  141. targetName = getDynamicLibbedFilename (targetName);
  142. else
  143. targetName = targetName.upToLastOccurrenceOf (".", false, false) + getTargetFileSuffix();
  144. if (type == VST3PlugIn)
  145. {
  146. s.add ("JUCE_VST3DIR := " + targetName.upToLastOccurrenceOf (".", false, false) + ".vst3");
  147. auto is32Bit = config.getArchitectureTypeString().contains ("m32");
  148. s.add (String ("JUCE_VST3SUBDIR := Contents/") + (is32Bit ? "i386" : "x86_64") + "-linux");
  149. targetName = "$(JUCE_VST3DIR)/$(JUCE_VST3SUBDIR)/" + targetName;
  150. }
  151. else if (type == UnityPlugIn)
  152. {
  153. s.add ("JUCE_UNITYDIR := Unity");
  154. targetName = "$(JUCE_UNITYDIR)/" + targetName;
  155. }
  156. s.add ("JUCE_TARGET_" + getTargetVarName() + String (" := ") + escapeSpaces (targetName));
  157. if (config.isPluginBinaryCopyStepEnabled() && (type == VST3PlugIn || type == VSTPlugIn || type == UnityPlugIn))
  158. {
  159. String copyCmd ("JUCE_COPYCMD_" + getTargetVarName() + String (" := $(JUCE_OUTDIR)/"));
  160. if (type == VST3PlugIn)
  161. {
  162. s.add ("JUCE_VST3DESTDIR := " + config.getVST3BinaryLocationString());
  163. s.add (copyCmd + "$(JUCE_VST3DIR) $(JUCE_VST3DESTDIR)");
  164. }
  165. else if (type == VSTPlugIn)
  166. {
  167. s.add ("JUCE_VSTDESTDIR := " + config.getVSTBinaryLocationString());
  168. s.add (copyCmd + targetName + " $(JUCE_VSTDESTDIR)");
  169. }
  170. else if (type == UnityPlugIn)
  171. {
  172. s.add ("JUCE_UNITYDESTDIR := " + config.getUnityPluginBinaryLocationString());
  173. s.add (copyCmd + "$(JUCE_UNITYDIR)/. $(JUCE_UNITYDESTDIR)");
  174. }
  175. }
  176. return s;
  177. }
  178. String getTargetFileSuffix() const
  179. {
  180. if (type == VSTPlugIn || type == VST3PlugIn || type == UnityPlugIn || type == DynamicLibrary)
  181. return ".so";
  182. if (type == SharedCodeTarget || type == StaticLibrary)
  183. return ".a";
  184. return {};
  185. }
  186. String getTargetVarName() const
  187. {
  188. return String (getName()).toUpperCase().replaceCharacter (L' ', L'_');
  189. }
  190. void writeObjects (OutputStream& out, const Array<std::pair<File, String>>& filesToCompile) const
  191. {
  192. out << "OBJECTS_" + getTargetVarName() + String (" := \\") << newLine;
  193. for (auto& f : filesToCompile)
  194. out << " $(JUCE_OBJDIR)/" << escapeSpaces (owner.getObjectFileFor ({ f.first, owner.getTargetFolder(), build_tools::RelativePath::buildTargetFolder })) << " \\" << newLine;
  195. out << newLine;
  196. }
  197. void addFiles (OutputStream& out, const Array<std::pair<File, String>>& filesToCompile)
  198. {
  199. auto cppflagsVarName = "JUCE_CPPFLAGS_" + getTargetVarName();
  200. auto cflagsVarName = "JUCE_CFLAGS_" + getTargetVarName();
  201. for (auto& f : filesToCompile)
  202. {
  203. build_tools::RelativePath relativePath (f.first, owner.getTargetFolder(), build_tools::RelativePath::buildTargetFolder);
  204. out << "$(JUCE_OBJDIR)/" << escapeSpaces (owner.getObjectFileFor (relativePath)) << ": " << escapeSpaces (relativePath.toUnixStyle()) << newLine
  205. << "\t-$(V_AT)mkdir -p $(JUCE_OBJDIR)" << newLine
  206. << "\t@echo \"Compiling " << relativePath.getFileName() << "\"" << newLine
  207. << (relativePath.hasFileExtension ("c;s;S") ? "\t$(V_AT)$(CC) $(JUCE_CFLAGS) " : "\t$(V_AT)$(CXX) $(JUCE_CXXFLAGS) ")
  208. << "$(" << cppflagsVarName << ") $(" << cflagsVarName << ")"
  209. << (f.second.isNotEmpty() ? " $(" + owner.getCompilerFlagSchemeVariableName (f.second) + ")" : "") << " -o \"$@\" -c \"$<\"" << newLine
  210. << newLine;
  211. }
  212. }
  213. String getBuildProduct() const
  214. {
  215. return "$(JUCE_OUTDIR)/$(JUCE_TARGET_" + getTargetVarName() + ")";
  216. }
  217. String getPhonyName() const
  218. {
  219. return String (getName()).upToFirstOccurrenceOf (" ", false, false);
  220. }
  221. void writeTargetLine (OutputStream& out, const StringArray& packages)
  222. {
  223. jassert (type != AggregateTarget);
  224. out << getBuildProduct() << " : "
  225. << "$(OBJECTS_" << getTargetVarName() << ") $(RESOURCES)";
  226. if (type != SharedCodeTarget && owner.shouldBuildTargetType (SharedCodeTarget))
  227. out << " $(JUCE_OUTDIR)/$(JUCE_TARGET_SHARED_CODE)";
  228. out << newLine;
  229. if (! packages.isEmpty())
  230. {
  231. out << "\t@command -v pkg-config >/dev/null 2>&1 || { echo >&2 \"pkg-config not installed. Please, install it.\"; exit 1; }" << newLine
  232. << "\t@pkg-config --print-errors";
  233. for (auto& pkg : packages)
  234. out << " " << pkg;
  235. out << newLine;
  236. }
  237. out << "\t@echo Linking \"" << owner.projectName << " - " << getName() << "\"" << newLine
  238. << "\t-$(V_AT)mkdir -p $(JUCE_BINDIR)" << newLine
  239. << "\t-$(V_AT)mkdir -p $(JUCE_LIBDIR)" << newLine
  240. << "\t-$(V_AT)mkdir -p $(JUCE_OUTDIR)" << newLine;
  241. if (type == VST3PlugIn)
  242. out << "\t-$(V_AT)mkdir -p $(JUCE_OUTDIR)/$(JUCE_VST3DIR)/$(JUCE_VST3SUBDIR)" << newLine;
  243. else if (type == UnityPlugIn)
  244. out << "\t-$(V_AT)mkdir -p $(JUCE_OUTDIR)/$(JUCE_UNITYDIR)" << newLine;
  245. if (owner.projectType.isStaticLibrary() || type == SharedCodeTarget)
  246. {
  247. out << "\t$(V_AT)$(AR) -rcs " << getBuildProduct()
  248. << " $(OBJECTS_" << getTargetVarName() << ")" << newLine;
  249. }
  250. else
  251. {
  252. out << "\t$(V_AT)$(CXX) -o " << getBuildProduct()
  253. << " $(OBJECTS_" << getTargetVarName() << ") ";
  254. if (owner.shouldBuildTargetType (SharedCodeTarget))
  255. out << "$(JUCE_OUTDIR)/$(JUCE_TARGET_SHARED_CODE) ";
  256. out << "$(JUCE_LDFLAGS) ";
  257. if (getTargetFileType() == sharedLibraryOrDLL || getTargetFileType() == pluginBundle
  258. || type == GUIApp || type == StandalonePlugIn)
  259. out << "$(JUCE_LDFLAGS_" << getTargetVarName() << ") ";
  260. out << "$(RESOURCES) $(TARGET_ARCH)" << newLine;
  261. }
  262. if (type == VST3PlugIn)
  263. {
  264. out << "\t-$(V_AT)mkdir -p $(JUCE_VST3DESTDIR)" << newLine
  265. << "\t-$(V_AT)cp -R $(JUCE_COPYCMD_VST3)" << newLine;
  266. }
  267. else if (type == VSTPlugIn)
  268. {
  269. out << "\t-$(V_AT)mkdir -p $(JUCE_VSTDESTDIR)" << newLine
  270. << "\t-$(V_AT)cp -R $(JUCE_COPYCMD_VST)" << newLine;
  271. }
  272. else if (type == UnityPlugIn)
  273. {
  274. auto scriptName = owner.getProject().getUnityScriptName();
  275. build_tools::RelativePath scriptPath (owner.getProject().getGeneratedCodeFolder().getChildFile (scriptName),
  276. owner.getTargetFolder(),
  277. build_tools::RelativePath::projectFolder);
  278. out << "\t-$(V_AT)cp " + scriptPath.toUnixStyle() + " $(JUCE_OUTDIR)/$(JUCE_UNITYDIR)" << newLine
  279. << "\t-$(V_AT)mkdir -p $(JUCE_UNITYDESTDIR)" << newLine
  280. << "\t-$(V_AT)cp -R $(JUCE_COPYCMD_UNITY_PLUGIN)" << newLine;
  281. }
  282. out << newLine;
  283. }
  284. const MakefileProjectExporter& owner;
  285. };
  286. //==============================================================================
  287. static String getDisplayName() { return "Linux Makefile"; }
  288. static String getValueTreeTypeName() { return "LINUX_MAKE"; }
  289. static String getTargetFolderName() { return "LinuxMakefile"; }
  290. String getExtraPkgConfigString() const { return extraPkgConfigValue.get(); }
  291. static MakefileProjectExporter* createForSettings (Project& projectToUse, const ValueTree& settingsToUse)
  292. {
  293. if (settingsToUse.hasType (getValueTreeTypeName()))
  294. return new MakefileProjectExporter (projectToUse, settingsToUse);
  295. return nullptr;
  296. }
  297. //==============================================================================
  298. MakefileProjectExporter (Project& p, const ValueTree& t)
  299. : ProjectExporter (p, t),
  300. extraPkgConfigValue (settings, Ids::linuxExtraPkgConfig, getUndoManager())
  301. {
  302. name = getDisplayName();
  303. targetLocationValue.setDefault (getDefaultBuildsRootFolder() + getTargetFolderName());
  304. }
  305. //==============================================================================
  306. bool canLaunchProject() override { return false; }
  307. bool launchProject() override { return false; }
  308. bool usesMMFiles() const override { return false; }
  309. bool canCopeWithDuplicateFiles() override { return false; }
  310. bool supportsUserDefinedConfigurations() const override { return true; }
  311. bool isXcode() const override { return false; }
  312. bool isVisualStudio() const override { return false; }
  313. bool isCodeBlocks() const override { return false; }
  314. bool isMakefile() const override { return true; }
  315. bool isAndroidStudio() const override { return false; }
  316. bool isCLion() const override { return false; }
  317. bool isAndroid() const override { return false; }
  318. bool isWindows() const override { return false; }
  319. bool isLinux() const override { return true; }
  320. bool isOSX() const override { return false; }
  321. bool isiOS() const override { return false; }
  322. bool supportsTargetType (build_tools::ProjectType::Target::Type type) const override
  323. {
  324. switch (type)
  325. {
  326. case build_tools::ProjectType::Target::GUIApp:
  327. case build_tools::ProjectType::Target::ConsoleApp:
  328. case build_tools::ProjectType::Target::StaticLibrary:
  329. case build_tools::ProjectType::Target::SharedCodeTarget:
  330. case build_tools::ProjectType::Target::AggregateTarget:
  331. case build_tools::ProjectType::Target::VSTPlugIn:
  332. case build_tools::ProjectType::Target::VST3PlugIn:
  333. case build_tools::ProjectType::Target::StandalonePlugIn:
  334. case build_tools::ProjectType::Target::DynamicLibrary:
  335. case build_tools::ProjectType::Target::UnityPlugIn:
  336. return true;
  337. case build_tools::ProjectType::Target::AAXPlugIn:
  338. case build_tools::ProjectType::Target::RTASPlugIn:
  339. case build_tools::ProjectType::Target::AudioUnitPlugIn:
  340. case build_tools::ProjectType::Target::AudioUnitv3PlugIn:
  341. case build_tools::ProjectType::Target::unspecified:
  342. default:
  343. break;
  344. }
  345. return false;
  346. }
  347. void createExporterProperties (PropertyListBuilder& properties) override
  348. {
  349. properties.add (new TextPropertyComponent (extraPkgConfigValue, "pkg-config libraries", 8192, false),
  350. "Extra pkg-config libraries for you application. Each package should be space separated.");
  351. }
  352. void initialiseDependencyPathValues() override
  353. {
  354. vstLegacyPathValueWrapper.init ({ settings, Ids::vstLegacyFolder, nullptr },
  355. getAppSettings().getStoredPath (Ids::vstLegacyPath, TargetOS::linux), TargetOS::linux);
  356. }
  357. //==============================================================================
  358. bool anyTargetIsSharedLibrary() const
  359. {
  360. for (auto* target : targets)
  361. {
  362. auto fileType = target->getTargetFileType();
  363. if (fileType == build_tools::ProjectType::Target::sharedLibraryOrDLL
  364. || fileType == build_tools::ProjectType::Target::pluginBundle)
  365. return true;
  366. }
  367. return false;
  368. }
  369. //==============================================================================
  370. void create (const OwnedArray<LibraryModule>&) const override
  371. {
  372. build_tools::writeStreamToFile (getTargetFolder().getChildFile ("Makefile"), [&] (MemoryOutputStream& mo)
  373. {
  374. mo.setNewLineString ("\n");
  375. writeMakefile (mo);
  376. });
  377. }
  378. //==============================================================================
  379. void addPlatformSpecificSettingsForProjectType (const build_tools::ProjectType&) override
  380. {
  381. callForAllSupportedTargets ([this] (build_tools::ProjectType::Target::Type targetType)
  382. {
  383. targets.insert (targetType == build_tools::ProjectType::Target::AggregateTarget ? 0 : -1,
  384. new MakefileTarget (targetType, *this));
  385. });
  386. // If you hit this assert, you tried to generate a project for an exporter
  387. // that does not support any of your targets!
  388. jassert (targets.size() > 0);
  389. }
  390. private:
  391. ValueWithDefault extraPkgConfigValue;
  392. //==============================================================================
  393. StringPairArray getDefines (const BuildConfiguration& config) const
  394. {
  395. StringPairArray result;
  396. result.set ("LINUX", "1");
  397. if (config.isDebug())
  398. {
  399. result.set ("DEBUG", "1");
  400. result.set ("_DEBUG", "1");
  401. }
  402. else
  403. {
  404. result.set ("NDEBUG", "1");
  405. }
  406. result = mergePreprocessorDefs (result, getAllPreprocessorDefs (config, build_tools::ProjectType::Target::unspecified));
  407. return result;
  408. }
  409. StringArray getPackages() const
  410. {
  411. StringArray packages;
  412. packages.addTokens (getExtraPkgConfigString(), " ", "\"'");
  413. packages.removeEmptyStrings();
  414. packages.addArray (linuxPackages);
  415. // don't add libcurl if curl symbols are loaded at runtime
  416. if (isCurlEnabled() && ! isLoadCurlSymbolsLazilyEnabled())
  417. packages.add ("libcurl");
  418. packages.removeDuplicates (false);
  419. return packages;
  420. }
  421. String getPreprocessorPkgConfigFlags() const
  422. {
  423. auto packages = getPackages();
  424. if (isWebBrowserComponentEnabled())
  425. {
  426. packages.add ("webkit2gtk-4.0");
  427. packages.add ("gtk+-x11-3.0");
  428. }
  429. if (packages.size() > 0)
  430. return "$(shell pkg-config --cflags " + packages.joinIntoString (" ") + ")";
  431. return {};
  432. }
  433. String getLinkerPkgConfigFlags() const
  434. {
  435. auto packages = getPackages();
  436. if (packages.size() > 0)
  437. return "$(shell pkg-config --libs " + packages.joinIntoString (" ") + ")";
  438. return {};
  439. }
  440. StringArray getCPreprocessorFlags (const BuildConfiguration&) const
  441. {
  442. StringArray result;
  443. if (linuxLibs.contains("pthread"))
  444. result.add ("-pthread");
  445. return result;
  446. }
  447. StringArray getCFlags (const BuildConfiguration& config) const
  448. {
  449. StringArray result;
  450. if (anyTargetIsSharedLibrary())
  451. result.add ("-fPIC");
  452. if (config.isDebug())
  453. {
  454. result.add ("-g");
  455. result.add ("-ggdb");
  456. }
  457. result.add ("-O" + config.getGCCOptimisationFlag());
  458. if (config.isLinkTimeOptimisationEnabled())
  459. result.add ("-flto");
  460. for (auto& recommended : config.getRecommendedCompilerWarningFlags())
  461. result.add (recommended);
  462. auto extra = replacePreprocessorTokens (config, getExtraCompilerFlagsString()).trim();
  463. if (extra.isNotEmpty())
  464. result.add (extra);
  465. return result;
  466. }
  467. StringArray getCXXFlags() const
  468. {
  469. StringArray result;
  470. auto cppStandard = project.getCppStandardString();
  471. if (cppStandard == "latest")
  472. cppStandard = "17";
  473. cppStandard = "-std=" + String (shouldUseGNUExtensions() ? "gnu++" : "c++") + cppStandard;
  474. result.add (cppStandard);
  475. return result;
  476. }
  477. StringArray getHeaderSearchPaths (const BuildConfiguration& config) const
  478. {
  479. StringArray searchPaths (extraSearchPaths);
  480. searchPaths.addArray (config.getHeaderSearchPaths());
  481. searchPaths = getCleanedStringArray (searchPaths);
  482. StringArray result;
  483. for (auto& path : searchPaths)
  484. result.add (build_tools::unixStylePath (replacePreprocessorTokens (config, path)));
  485. return result;
  486. }
  487. StringArray getLibraryNames (const BuildConfiguration& config) const
  488. {
  489. StringArray result (linuxLibs);
  490. auto libraries = StringArray::fromTokens (getExternalLibrariesString(), ";", "\"'");
  491. libraries.removeEmptyStrings();
  492. for (auto& lib : libraries)
  493. result.add (replacePreprocessorTokens (config, lib).trim());
  494. return result;
  495. }
  496. StringArray getLibrarySearchPaths (const BuildConfiguration& config) const
  497. {
  498. auto result = getSearchPathsFromString (config.getLibrarySearchPathString());
  499. for (auto path : moduleLibSearchPaths)
  500. result.add (path + "/" + config.getModuleLibraryArchName());
  501. return result;
  502. }
  503. StringArray getLinkerFlags (const BuildConfiguration& config) const
  504. {
  505. auto result = makefileExtraLinkerFlags;
  506. result.add ("-fvisibility=hidden");
  507. if (config.isLinkTimeOptimisationEnabled())
  508. result.add ("-flto");
  509. auto extraFlags = getExtraLinkerFlagsString().trim();
  510. if (extraFlags.isNotEmpty())
  511. result.add (replacePreprocessorTokens (config, extraFlags));
  512. return result;
  513. }
  514. bool isWebBrowserComponentEnabled() const
  515. {
  516. static String guiExtrasModule ("juce_gui_extra");
  517. return (project.getEnabledModules().isModuleEnabled (guiExtrasModule)
  518. && project.isConfigFlagEnabled ("JUCE_WEB_BROWSER", true));
  519. }
  520. bool isCurlEnabled() const
  521. {
  522. static String juceCoreModule ("juce_core");
  523. return (project.getEnabledModules().isModuleEnabled (juceCoreModule)
  524. && project.isConfigFlagEnabled ("JUCE_USE_CURL", true));
  525. }
  526. bool isLoadCurlSymbolsLazilyEnabled() const
  527. {
  528. static String juceCoreModule ("juce_core");
  529. return (project.getEnabledModules().isModuleEnabled (juceCoreModule)
  530. && project.isConfigFlagEnabled ("JUCE_LOAD_CURL_SYMBOLS_LAZILY", false));
  531. }
  532. //==============================================================================
  533. void writeDefineFlags (OutputStream& out, const MakeBuildConfiguration& config) const
  534. {
  535. out << createGCCPreprocessorFlags (mergePreprocessorDefs (getDefines (config), getAllPreprocessorDefs (config, build_tools::ProjectType::Target::unspecified)));
  536. }
  537. void writePkgConfigFlags (OutputStream& out) const
  538. {
  539. auto flags = getPreprocessorPkgConfigFlags();
  540. if (flags.isNotEmpty())
  541. out << " " << flags;
  542. }
  543. void writeCPreprocessorFlags (OutputStream& out, const BuildConfiguration& config) const
  544. {
  545. auto flags = getCPreprocessorFlags (config);
  546. if (! flags.isEmpty())
  547. out << " " << flags.joinIntoString (" ");
  548. }
  549. void writeHeaderPathFlags (OutputStream& out, const BuildConfiguration& config) const
  550. {
  551. for (auto& path : getHeaderSearchPaths (config))
  552. out << " -I" << escapeSpaces (path).replace ("~", "$(HOME)");
  553. }
  554. void writeCppFlags (OutputStream& out, const MakeBuildConfiguration& config) const
  555. {
  556. out << " JUCE_CPPFLAGS := $(DEPFLAGS)";
  557. writeDefineFlags (out, config);
  558. writePkgConfigFlags (out);
  559. writeCPreprocessorFlags (out, config);
  560. writeHeaderPathFlags (out, config);
  561. out << " $(CPPFLAGS)" << newLine;
  562. }
  563. void writeLinkerFlags (OutputStream& out, const BuildConfiguration& config) const
  564. {
  565. out << " JUCE_LDFLAGS += $(TARGET_ARCH) -L$(JUCE_BINDIR) -L$(JUCE_LIBDIR)";
  566. for (auto path : getLibrarySearchPaths (config))
  567. out << " -L" << escapeSpaces (path).replace ("~", "$(HOME)");
  568. auto pkgConfigFlags = getLinkerPkgConfigFlags();
  569. if (pkgConfigFlags.isNotEmpty())
  570. out << " " << getLinkerPkgConfigFlags();
  571. auto linkerFlags = getLinkerFlags (config).joinIntoString (" ");
  572. if (linkerFlags.isNotEmpty())
  573. out << " " << linkerFlags;
  574. for (auto& libName : getLibraryNames (config))
  575. out << " -l" << libName;
  576. out << " $(LDFLAGS)" << newLine;
  577. }
  578. void writeTargetLines (OutputStream& out, const StringArray& packages) const
  579. {
  580. auto n = targets.size();
  581. for (int i = 0; i < n; ++i)
  582. {
  583. if (auto* target = targets.getUnchecked (i))
  584. {
  585. if (target->type == build_tools::ProjectType::Target::AggregateTarget)
  586. {
  587. StringArray dependencies;
  588. MemoryOutputStream subTargetLines;
  589. for (int j = 0; j < n; ++j)
  590. {
  591. if (i == j) continue;
  592. if (auto* dependency = targets.getUnchecked (j))
  593. {
  594. if (dependency->type != build_tools::ProjectType::Target::SharedCodeTarget)
  595. {
  596. auto phonyName = dependency->getPhonyName();
  597. subTargetLines << phonyName << " : " << dependency->getBuildProduct() << newLine;
  598. dependencies.add (phonyName);
  599. }
  600. }
  601. }
  602. out << "all : " << dependencies.joinIntoString (" ") << newLine << newLine;
  603. out << subTargetLines.toString() << newLine << newLine;
  604. }
  605. else
  606. {
  607. if (! getProject().isAudioPluginProject())
  608. out << "all : " << target->getBuildProduct() << newLine << newLine;
  609. target->writeTargetLine (out, packages);
  610. }
  611. }
  612. }
  613. }
  614. void writeConfig (OutputStream& out, const MakeBuildConfiguration& config) const
  615. {
  616. String buildDirName ("build");
  617. auto intermediatesDirName = buildDirName + "/intermediate/" + config.getName();
  618. auto outputDir = buildDirName;
  619. if (config.getTargetBinaryRelativePathString().isNotEmpty())
  620. {
  621. build_tools::RelativePath binaryPath (config.getTargetBinaryRelativePathString(), build_tools::RelativePath::projectFolder);
  622. outputDir = binaryPath.rebased (projectFolder, getTargetFolder(), build_tools::RelativePath::buildTargetFolder).toUnixStyle();
  623. }
  624. out << "ifeq ($(CONFIG)," << escapeSpaces (config.getName()) << ")" << newLine
  625. << " JUCE_BINDIR := " << escapeSpaces (buildDirName) << newLine
  626. << " JUCE_LIBDIR := " << escapeSpaces (buildDirName) << newLine
  627. << " JUCE_OBJDIR := " << escapeSpaces (intermediatesDirName) << newLine
  628. << " JUCE_OUTDIR := " << escapeSpaces (outputDir) << newLine
  629. << newLine
  630. << " ifeq ($(TARGET_ARCH),)" << newLine
  631. << " TARGET_ARCH := " << getArchFlags (config) << newLine
  632. << " endif" << newLine
  633. << newLine;
  634. writeCppFlags (out, config);
  635. for (auto target : targets)
  636. {
  637. auto lines = target->getTargetSettings (config);
  638. if (lines.size() > 0)
  639. out << " " << lines.joinIntoString ("\n ") << newLine;
  640. out << newLine;
  641. }
  642. out << " JUCE_CFLAGS += $(JUCE_CPPFLAGS) $(TARGET_ARCH)";
  643. auto cflags = getCFlags (config).joinIntoString (" ");
  644. if (cflags.isNotEmpty())
  645. out << " " << cflags;
  646. out << " $(CFLAGS)" << newLine;
  647. out << " JUCE_CXXFLAGS += $(JUCE_CFLAGS)";
  648. auto cxxflags = getCXXFlags().joinIntoString (" ");
  649. if (cxxflags.isNotEmpty())
  650. out << " " << cxxflags;
  651. out << " $(CXXFLAGS)" << newLine;
  652. writeLinkerFlags (out, config);
  653. out << newLine;
  654. out << " CLEANCMD = rm -rf $(JUCE_OUTDIR)/$(TARGET) $(JUCE_OBJDIR)" << newLine
  655. << "endif" << newLine
  656. << newLine;
  657. }
  658. void writeIncludeLines (OutputStream& out) const
  659. {
  660. auto n = targets.size();
  661. for (int i = 0; i < n; ++i)
  662. {
  663. if (auto* target = targets.getUnchecked (i))
  664. {
  665. if (target->type == build_tools::ProjectType::Target::AggregateTarget)
  666. continue;
  667. out << "-include $(OBJECTS_" << target->getTargetVarName()
  668. << ":%.o=%.d)" << newLine;
  669. }
  670. }
  671. }
  672. static String getCompilerFlagSchemeVariableName (const String& schemeName) { return "JUCE_COMPILERFLAGSCHEME_" + schemeName; }
  673. void findAllFilesToCompile (const Project::Item& projectItem, Array<std::pair<File, String>>& results) const
  674. {
  675. if (projectItem.isGroup())
  676. {
  677. for (int i = 0; i < projectItem.getNumChildren(); ++i)
  678. findAllFilesToCompile (projectItem.getChild (i), results);
  679. }
  680. else
  681. {
  682. if (projectItem.shouldBeCompiled())
  683. {
  684. auto f = projectItem.getFile();
  685. if (shouldFileBeCompiledByDefault (f))
  686. {
  687. auto scheme = projectItem.getCompilerFlagSchemeString();
  688. auto flags = compilerFlagSchemesMap[scheme].get().toString();
  689. if (scheme.isNotEmpty() && flags.isNotEmpty())
  690. results.add ({ f, scheme });
  691. else
  692. results.add ({ f, {} });
  693. }
  694. }
  695. }
  696. }
  697. void writeCompilerFlagSchemes (OutputStream& out, const Array<std::pair<File, String>>& filesToCompile) const
  698. {
  699. StringArray schemesToWrite;
  700. for (auto& f : filesToCompile)
  701. if (f.second.isNotEmpty())
  702. schemesToWrite.addIfNotAlreadyThere (f.second);
  703. if (! schemesToWrite.isEmpty())
  704. {
  705. for (auto& s : schemesToWrite)
  706. out << getCompilerFlagSchemeVariableName (s) << " := "
  707. << compilerFlagSchemesMap[s].get().toString() << newLine;
  708. out << newLine;
  709. }
  710. }
  711. void writeMakefile (OutputStream& out) const
  712. {
  713. out << "# Automatically generated makefile, created by the Projucer" << newLine
  714. << "# Don't edit this file! Your changes will be overwritten when you re-save the Projucer project!" << newLine
  715. << newLine;
  716. out << "# build with \"V=1\" for verbose builds" << newLine
  717. << "ifeq ($(V), 1)" << newLine
  718. << "V_AT =" << newLine
  719. << "else" << newLine
  720. << "V_AT = @" << newLine
  721. << "endif" << newLine
  722. << newLine;
  723. out << "# (this disables dependency generation if multiple architectures are set)" << newLine
  724. << "DEPFLAGS := $(if $(word 2, $(TARGET_ARCH)), , -MMD)" << newLine
  725. << newLine;
  726. out << "ifndef STRIP" << newLine
  727. << " STRIP=strip" << newLine
  728. << "endif" << newLine
  729. << newLine;
  730. out << "ifndef AR" << newLine
  731. << " AR=ar" << newLine
  732. << "endif" << newLine
  733. << newLine;
  734. out << "ifndef CONFIG" << newLine
  735. << " CONFIG=" << escapeSpaces (getConfiguration(0)->getName()) << newLine
  736. << "endif" << newLine
  737. << newLine;
  738. out << "JUCE_ARCH_LABEL := $(shell uname -m)" << newLine
  739. << newLine;
  740. for (ConstConfigIterator config (*this); config.next();)
  741. writeConfig (out, dynamic_cast<const MakeBuildConfiguration&> (*config));
  742. Array<std::pair<File, String>> filesToCompile;
  743. for (int i = 0; i < getAllGroups().size(); ++i)
  744. findAllFilesToCompile (getAllGroups().getReference (i), filesToCompile);
  745. writeCompilerFlagSchemes (out, filesToCompile);
  746. auto getFilesForTarget = [] (const Array<std::pair<File, String>>& files, MakefileTarget* target, const Project& p) -> Array<std::pair<File, String>>
  747. {
  748. Array<std::pair<File, String>> targetFiles;
  749. auto targetType = (p.isAudioPluginProject() ? target->type : MakefileTarget::SharedCodeTarget);
  750. for (auto& f : files)
  751. if (p.getTargetTypeFromFilePath (f.first, true) == targetType)
  752. targetFiles.add (f);
  753. return targetFiles;
  754. };
  755. for (auto target : targets)
  756. target->writeObjects (out, getFilesForTarget (filesToCompile, target, project));
  757. out << getPhonyTargetLine() << newLine << newLine;
  758. writeTargetLines (out, getPackages());
  759. for (auto target : targets)
  760. target->addFiles (out, getFilesForTarget (filesToCompile, target, project));
  761. out << "clean:" << newLine
  762. << "\t@echo Cleaning " << projectName << newLine
  763. << "\t$(V_AT)$(CLEANCMD)" << newLine
  764. << newLine;
  765. out << "strip:" << newLine
  766. << "\t@echo Stripping " << projectName << newLine
  767. << "\t-$(V_AT)$(STRIP) --strip-unneeded $(JUCE_OUTDIR)/$(TARGET)" << newLine
  768. << newLine;
  769. writeIncludeLines (out);
  770. }
  771. String getArchFlags (const BuildConfiguration& config) const
  772. {
  773. if (auto* makeConfig = dynamic_cast<const MakeBuildConfiguration*> (&config))
  774. return makeConfig->getArchitectureTypeString();
  775. return "-march=native";
  776. }
  777. String getObjectFileFor (const build_tools::RelativePath& file) const
  778. {
  779. return file.getFileNameWithoutExtension()
  780. + "_" + String::toHexString (file.toUnixStyle().hashCode()) + ".o";
  781. }
  782. String getPhonyTargetLine() const
  783. {
  784. MemoryOutputStream phonyTargetLine;
  785. phonyTargetLine << ".PHONY: clean all strip";
  786. if (! getProject().isAudioPluginProject())
  787. return phonyTargetLine.toString();
  788. for (auto target : targets)
  789. if (target->type != build_tools::ProjectType::Target::SharedCodeTarget
  790. && target->type != build_tools::ProjectType::Target::AggregateTarget)
  791. phonyTargetLine << " " << target->getPhonyName();
  792. return phonyTargetLine.toString();
  793. }
  794. friend class CLionProjectExporter;
  795. OwnedArray<MakefileTarget> targets;
  796. JUCE_DECLARE_NON_COPYABLE (MakefileProjectExporter)
  797. };