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.

1040 lines
42KB

  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 (type == UnityPlugIn));
  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 := " + escapeQuotesAndSpaces (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 (" := ") + escapeQuotesAndSpaces (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 + escapeQuotesAndSpaces (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)/" << escapeQuotesAndSpaces (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)/" << escapeQuotesAndSpaces (owner.getObjectFileFor (relativePath)) << ": " << escapeQuotesAndSpaces (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().common)
  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 BuildConfiguration& config) const
  484. {
  485. StringArray result;
  486. for (auto& recommended : config.getRecommendedCompilerWarningFlags().cpp)
  487. result.add (recommended);
  488. auto cppStandard = project.getCppStandardString();
  489. if (cppStandard == "latest")
  490. cppStandard = project.getLatestNumberedCppStandardString();
  491. result.add ("-std=" + String (shouldUseGNUExtensions() ? "gnu++" : "c++") + cppStandard);
  492. return result;
  493. }
  494. StringArray getHeaderSearchPaths (const BuildConfiguration& config) const
  495. {
  496. StringArray searchPaths (extraSearchPaths);
  497. searchPaths.addArray (config.getHeaderSearchPaths());
  498. searchPaths = getCleanedStringArray (searchPaths);
  499. StringArray result;
  500. for (auto& path : searchPaths)
  501. result.add (build_tools::unixStylePath (replacePreprocessorTokens (config, path)));
  502. return result;
  503. }
  504. StringArray getLibraryNames (const BuildConfiguration& config) const
  505. {
  506. StringArray result (linuxLibs);
  507. auto libraries = StringArray::fromTokens (getExternalLibrariesString(), ";", "\"'");
  508. libraries.removeEmptyStrings();
  509. for (auto& lib : libraries)
  510. result.add (replacePreprocessorTokens (config, lib).trim());
  511. return result;
  512. }
  513. StringArray getLibrarySearchPaths (const BuildConfiguration& config) const
  514. {
  515. auto result = getSearchPathsFromString (config.getLibrarySearchPathString());
  516. for (auto path : moduleLibSearchPaths)
  517. result.add (path + "/" + config.getModuleLibraryArchName());
  518. return result;
  519. }
  520. StringArray getLinkerFlags (const BuildConfiguration& config) const
  521. {
  522. auto result = makefileExtraLinkerFlags;
  523. result.add ("-fvisibility=hidden");
  524. if (config.isLinkTimeOptimisationEnabled())
  525. result.add ("-flto");
  526. auto extraFlags = getExtraLinkerFlagsString().trim();
  527. if (extraFlags.isNotEmpty())
  528. result.add (replacePreprocessorTokens (config, extraFlags));
  529. return result;
  530. }
  531. //==============================================================================
  532. void writeDefineFlags (OutputStream& out, const MakeBuildConfiguration& config) const
  533. {
  534. out << createGCCPreprocessorFlags (mergePreprocessorDefs (getDefines (config), getAllPreprocessorDefs (config, build_tools::ProjectType::Target::unspecified)));
  535. }
  536. void writePkgConfigFlags (OutputStream& out) const
  537. {
  538. auto flags = getPreprocessorPkgConfigFlags();
  539. if (flags.isNotEmpty())
  540. out << " " << flags;
  541. }
  542. void writeCPreprocessorFlags (OutputStream& out, const BuildConfiguration& config) const
  543. {
  544. auto flags = getCPreprocessorFlags (config);
  545. if (! flags.isEmpty())
  546. out << " " << flags.joinIntoString (" ");
  547. }
  548. void writeHeaderPathFlags (OutputStream& out, const BuildConfiguration& config) const
  549. {
  550. for (auto& path : getHeaderSearchPaths (config))
  551. out << " -I" << escapeQuotesAndSpaces (path).replace ("~", "$(HOME)");
  552. }
  553. void writeCppFlags (OutputStream& out, const MakeBuildConfiguration& config) const
  554. {
  555. out << " JUCE_CPPFLAGS := $(DEPFLAGS)";
  556. writeDefineFlags (out, config);
  557. writePkgConfigFlags (out);
  558. writeCPreprocessorFlags (out, config);
  559. writeHeaderPathFlags (out, config);
  560. out << " $(CPPFLAGS)" << newLine;
  561. }
  562. void writeLinkerFlags (OutputStream& out, const BuildConfiguration& config) const
  563. {
  564. out << " JUCE_LDFLAGS += $(TARGET_ARCH) -L$(JUCE_BINDIR) -L$(JUCE_LIBDIR)";
  565. for (auto path : getLibrarySearchPaths (config))
  566. out << " -L" << escapeQuotesAndSpaces (path).replace ("~", "$(HOME)");
  567. auto pkgConfigFlags = getLinkerPkgConfigFlags();
  568. if (pkgConfigFlags.isNotEmpty())
  569. out << " " << getLinkerPkgConfigFlags();
  570. auto linkerFlags = getLinkerFlags (config).joinIntoString (" ");
  571. if (linkerFlags.isNotEmpty())
  572. out << " " << linkerFlags;
  573. for (auto& libName : getLibraryNames (config))
  574. out << " -l" << libName;
  575. out << " $(LDFLAGS)" << newLine;
  576. }
  577. void writeTargetLines (OutputStream& out, const StringArray& packages) const
  578. {
  579. auto n = targets.size();
  580. for (int i = 0; i < n; ++i)
  581. {
  582. if (auto* target = targets.getUnchecked (i))
  583. {
  584. if (target->type == build_tools::ProjectType::Target::AggregateTarget)
  585. {
  586. StringArray dependencies;
  587. MemoryOutputStream subTargetLines;
  588. for (int j = 0; j < n; ++j)
  589. {
  590. if (i == j) continue;
  591. if (auto* dependency = targets.getUnchecked (j))
  592. {
  593. if (dependency->type != build_tools::ProjectType::Target::SharedCodeTarget)
  594. {
  595. auto phonyName = dependency->getPhonyName();
  596. subTargetLines << phonyName << " : " << dependency->getBuildProduct() << newLine;
  597. dependencies.add (phonyName);
  598. }
  599. }
  600. }
  601. out << "all : " << dependencies.joinIntoString (" ") << newLine << newLine;
  602. out << subTargetLines.toString() << newLine << newLine;
  603. }
  604. else
  605. {
  606. if (! getProject().isAudioPluginProject())
  607. out << "all : " << target->getBuildProduct() << newLine << newLine;
  608. target->writeTargetLine (out, packages);
  609. }
  610. }
  611. }
  612. }
  613. void writeConfig (OutputStream& out, const MakeBuildConfiguration& config) const
  614. {
  615. String buildDirName ("build");
  616. auto intermediatesDirName = buildDirName + "/intermediate/" + config.getName();
  617. auto outputDir = buildDirName;
  618. if (config.getTargetBinaryRelativePathString().isNotEmpty())
  619. {
  620. build_tools::RelativePath binaryPath (config.getTargetBinaryRelativePathString(), build_tools::RelativePath::projectFolder);
  621. outputDir = binaryPath.rebased (projectFolder, getTargetFolder(), build_tools::RelativePath::buildTargetFolder).toUnixStyle();
  622. }
  623. out << "ifeq ($(CONFIG)," << escapeQuotesAndSpaces (config.getName()) << ")" << newLine
  624. << " JUCE_BINDIR := " << escapeQuotesAndSpaces (buildDirName) << newLine
  625. << " JUCE_LIBDIR := " << escapeQuotesAndSpaces (buildDirName) << newLine
  626. << " JUCE_OBJDIR := " << escapeQuotesAndSpaces (intermediatesDirName) << newLine
  627. << " JUCE_OUTDIR := " << escapeQuotesAndSpaces (outputDir) << newLine
  628. << newLine
  629. << " ifeq ($(TARGET_ARCH),)" << newLine
  630. << " TARGET_ARCH := " << getArchFlags (config) << newLine
  631. << " endif" << newLine
  632. << newLine;
  633. writeCppFlags (out, config);
  634. for (auto target : targets)
  635. {
  636. auto lines = target->getTargetSettings (config);
  637. if (lines.size() > 0)
  638. out << " " << lines.joinIntoString ("\n ") << newLine;
  639. out << newLine;
  640. }
  641. out << " JUCE_CFLAGS += $(JUCE_CPPFLAGS) $(TARGET_ARCH)";
  642. auto cflags = getCFlags (config).joinIntoString (" ");
  643. if (cflags.isNotEmpty())
  644. out << " " << cflags;
  645. out << " $(CFLAGS)" << newLine;
  646. out << " JUCE_CXXFLAGS += $(JUCE_CFLAGS)";
  647. auto cxxflags = getCXXFlags (config).joinIntoString (" ");
  648. if (cxxflags.isNotEmpty())
  649. out << " " << cxxflags;
  650. out << " $(CXXFLAGS)" << newLine;
  651. writeLinkerFlags (out, config);
  652. out << newLine;
  653. out << " CLEANCMD = rm -rf $(JUCE_OUTDIR)/$(TARGET) $(JUCE_OBJDIR)" << newLine
  654. << "endif" << newLine
  655. << newLine;
  656. }
  657. void writeIncludeLines (OutputStream& out) const
  658. {
  659. auto n = targets.size();
  660. for (int i = 0; i < n; ++i)
  661. {
  662. if (auto* target = targets.getUnchecked (i))
  663. {
  664. if (target->type == build_tools::ProjectType::Target::AggregateTarget)
  665. continue;
  666. out << "-include $(OBJECTS_" << target->getTargetVarName()
  667. << ":%.o=%.d)" << newLine;
  668. }
  669. }
  670. }
  671. static String getCompilerFlagSchemeVariableName (const String& schemeName) { return "JUCE_COMPILERFLAGSCHEME_" + schemeName; }
  672. void findAllFilesToCompile (const Project::Item& projectItem, Array<std::pair<File, String>>& results) const
  673. {
  674. if (projectItem.isGroup())
  675. {
  676. for (int i = 0; i < projectItem.getNumChildren(); ++i)
  677. findAllFilesToCompile (projectItem.getChild (i), results);
  678. }
  679. else
  680. {
  681. if (projectItem.shouldBeCompiled())
  682. {
  683. auto f = projectItem.getFile();
  684. if (shouldFileBeCompiledByDefault (f))
  685. {
  686. auto scheme = projectItem.getCompilerFlagSchemeString();
  687. auto flags = compilerFlagSchemesMap[scheme].get().toString();
  688. if (scheme.isNotEmpty() && flags.isNotEmpty())
  689. results.add ({ f, scheme });
  690. else
  691. results.add ({ f, {} });
  692. }
  693. }
  694. }
  695. }
  696. void writeCompilerFlagSchemes (OutputStream& out, const Array<std::pair<File, String>>& filesToCompile) const
  697. {
  698. StringArray schemesToWrite;
  699. for (auto& f : filesToCompile)
  700. if (f.second.isNotEmpty())
  701. schemesToWrite.addIfNotAlreadyThere (f.second);
  702. if (! schemesToWrite.isEmpty())
  703. {
  704. for (auto& s : schemesToWrite)
  705. out << getCompilerFlagSchemeVariableName (s) << " := "
  706. << compilerFlagSchemesMap[s].get().toString() << newLine;
  707. out << newLine;
  708. }
  709. }
  710. void writeMakefile (OutputStream& out) const
  711. {
  712. out << "# Automatically generated makefile, created by the Projucer" << newLine
  713. << "# Don't edit this file! Your changes will be overwritten when you re-save the Projucer project!" << newLine
  714. << newLine;
  715. out << "# build with \"V=1\" for verbose builds" << newLine
  716. << "ifeq ($(V), 1)" << newLine
  717. << "V_AT =" << newLine
  718. << "else" << newLine
  719. << "V_AT = @" << newLine
  720. << "endif" << newLine
  721. << newLine;
  722. out << "# (this disables dependency generation if multiple architectures are set)" << newLine
  723. << "DEPFLAGS := $(if $(word 2, $(TARGET_ARCH)), , -MMD)" << newLine
  724. << newLine;
  725. out << "ifndef STRIP" << newLine
  726. << " STRIP=strip" << newLine
  727. << "endif" << newLine
  728. << newLine;
  729. out << "ifndef AR" << newLine
  730. << " AR=ar" << newLine
  731. << "endif" << newLine
  732. << newLine;
  733. out << "ifndef CONFIG" << newLine
  734. << " CONFIG=" << escapeQuotesAndSpaces (getConfiguration(0)->getName()) << newLine
  735. << "endif" << newLine
  736. << newLine;
  737. out << "JUCE_ARCH_LABEL := $(shell uname -m)" << newLine
  738. << newLine;
  739. for (ConstConfigIterator config (*this); config.next();)
  740. writeConfig (out, dynamic_cast<const MakeBuildConfiguration&> (*config));
  741. Array<std::pair<File, String>> filesToCompile;
  742. for (int i = 0; i < getAllGroups().size(); ++i)
  743. findAllFilesToCompile (getAllGroups().getReference (i), filesToCompile);
  744. writeCompilerFlagSchemes (out, filesToCompile);
  745. auto getFilesForTarget = [] (const Array<std::pair<File, String>>& files,
  746. MakefileTarget* target,
  747. const Project& p) -> Array<std::pair<File, String>>
  748. {
  749. Array<std::pair<File, String>> targetFiles;
  750. auto targetType = (p.isAudioPluginProject() ? target->type : MakefileTarget::SharedCodeTarget);
  751. for (auto& f : files)
  752. if (p.getTargetTypeFromFilePath (f.first, true) == targetType)
  753. targetFiles.add (f);
  754. return targetFiles;
  755. };
  756. for (auto target : targets)
  757. target->writeObjects (out, getFilesForTarget (filesToCompile, target, project));
  758. out << getPhonyTargetLine() << newLine << newLine;
  759. writeTargetLines (out, getLinkPackages());
  760. for (auto target : targets)
  761. target->addFiles (out, getFilesForTarget (filesToCompile, target, project));
  762. out << "clean:" << newLine
  763. << "\t@echo Cleaning " << projectName << newLine
  764. << "\t$(V_AT)$(CLEANCMD)" << newLine
  765. << newLine;
  766. out << "strip:" << newLine
  767. << "\t@echo Stripping " << projectName << newLine
  768. << "\t-$(V_AT)$(STRIP) --strip-unneeded $(JUCE_OUTDIR)/$(TARGET)" << newLine
  769. << newLine;
  770. writeIncludeLines (out);
  771. }
  772. String getArchFlags (const BuildConfiguration& config) const
  773. {
  774. if (auto* makeConfig = dynamic_cast<const MakeBuildConfiguration*> (&config))
  775. return makeConfig->getArchitectureTypeString();
  776. return "-march=native";
  777. }
  778. String getObjectFileFor (const build_tools::RelativePath& file) const
  779. {
  780. return file.getFileNameWithoutExtension()
  781. + "_" + String::toHexString (file.toUnixStyle().hashCode()) + ".o";
  782. }
  783. String getPhonyTargetLine() const
  784. {
  785. MemoryOutputStream phonyTargetLine;
  786. phonyTargetLine << ".PHONY: clean all strip";
  787. if (! getProject().isAudioPluginProject())
  788. return phonyTargetLine.toString();
  789. for (auto target : targets)
  790. if (target->type != build_tools::ProjectType::Target::SharedCodeTarget
  791. && target->type != build_tools::ProjectType::Target::AggregateTarget)
  792. phonyTargetLine << " " << target->getPhonyName();
  793. return phonyTargetLine.toString();
  794. }
  795. friend class CLionProjectExporter;
  796. OwnedArray<MakefileTarget> targets;
  797. JUCE_DECLARE_NON_COPYABLE (MakefileProjectExporter)
  798. };