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.

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