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.

1049 lines
41KB

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