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.

1111 lines
45KB

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