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.

1090 lines
44KB

  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 (config.isPluginBinaryCopyStepEnabled()
  180. && (type == VST3PlugIn || type == VSTPlugIn || type == UnityPlugIn || type == LV2PlugIn))
  181. {
  182. String copyCmd ("JUCE_COPYCMD_" + getTargetVarName() + String (" := $(JUCE_OUTDIR)/"));
  183. if (type == VST3PlugIn)
  184. {
  185. s.add ("JUCE_VST3DESTDIR := " + config.getVST3BinaryLocationString());
  186. s.add (copyCmd + "$(JUCE_VST3DIR) $(JUCE_VST3DESTDIR)");
  187. }
  188. else if (type == VSTPlugIn)
  189. {
  190. s.add ("JUCE_VSTDESTDIR := " + config.getVSTBinaryLocationString());
  191. s.add (copyCmd + escapeQuotesAndSpaces (targetName) + " $(JUCE_VSTDESTDIR)");
  192. }
  193. else if (type == UnityPlugIn)
  194. {
  195. s.add ("JUCE_UNITYDESTDIR := " + config.getUnityPluginBinaryLocationString());
  196. s.add (copyCmd + "$(JUCE_UNITYDIR)/. $(JUCE_UNITYDESTDIR)");
  197. }
  198. else if (type == LV2PlugIn)
  199. {
  200. s.add ("JUCE_LV2DESTDIR := " + config.getLV2BinaryLocationString());
  201. s.add ("JUCE_LV2_FULL_PATH := $(JUCE_OUTDIR)/$(JUCE_TARGET_LV2_PLUGIN)");
  202. s.add (copyCmd + "$(JUCE_LV2DIR) $(JUCE_LV2DESTDIR)");
  203. }
  204. }
  205. return s;
  206. }
  207. String getTargetFileSuffix() const
  208. {
  209. if (type == VSTPlugIn || type == VST3PlugIn || type == UnityPlugIn || type == DynamicLibrary)
  210. return ".so";
  211. if (type == SharedCodeTarget || type == StaticLibrary)
  212. return ".a";
  213. return {};
  214. }
  215. String getTargetVarName() const
  216. {
  217. return String (getName()).toUpperCase().replaceCharacter (L' ', L'_');
  218. }
  219. void writeObjects (OutputStream& out, const Array<std::pair<File, String>>& filesToCompile) const
  220. {
  221. out << "OBJECTS_" + getTargetVarName() + String (" := \\") << newLine;
  222. for (auto& f : filesToCompile)
  223. out << " $(JUCE_OBJDIR)/" << escapeQuotesAndSpaces (owner.getObjectFileFor ({ f.first, owner.getTargetFolder(), build_tools::RelativePath::buildTargetFolder }))
  224. << " \\" << newLine;
  225. out << newLine;
  226. }
  227. void addFiles (OutputStream& out, const Array<std::pair<File, String>>& filesToCompile)
  228. {
  229. auto cppflagsVarName = "JUCE_CPPFLAGS_" + getTargetVarName();
  230. auto cflagsVarName = "JUCE_CFLAGS_" + getTargetVarName();
  231. for (auto& f : filesToCompile)
  232. {
  233. build_tools::RelativePath relativePath (f.first, owner.getTargetFolder(), build_tools::RelativePath::buildTargetFolder);
  234. out << "$(JUCE_OBJDIR)/" << escapeQuotesAndSpaces (owner.getObjectFileFor (relativePath)) << ": " << escapeQuotesAndSpaces (relativePath.toUnixStyle()) << newLine
  235. << "\t-$(V_AT)mkdir -p $(JUCE_OBJDIR)" << newLine
  236. << "\t@echo \"Compiling " << relativePath.getFileName() << "\"" << newLine
  237. << (relativePath.hasFileExtension ("c;s;S") ? "\t$(V_AT)$(CC) $(JUCE_CFLAGS) " : "\t$(V_AT)$(CXX) $(JUCE_CXXFLAGS) ")
  238. << "$(" << cppflagsVarName << ") $(" << cflagsVarName << ")"
  239. << (f.second.isNotEmpty() ? " $(" + owner.getCompilerFlagSchemeVariableName (f.second) + ")" : "") << " -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)mkdir -p $(JUCE_VST3DESTDIR)" << newLine
  301. << "\t-$(V_AT)cp -R $(JUCE_COPYCMD_VST3)" << newLine;
  302. }
  303. else if (type == VSTPlugIn)
  304. {
  305. out << "\t-$(V_AT)mkdir -p $(JUCE_VSTDESTDIR)" << newLine
  306. << "\t-$(V_AT)cp -R $(JUCE_COPYCMD_VST)" << newLine;
  307. }
  308. else if (type == UnityPlugIn)
  309. {
  310. auto scriptName = owner.getProject().getUnityScriptName();
  311. build_tools::RelativePath scriptPath (owner.getProject().getGeneratedCodeFolder().getChildFile (scriptName),
  312. owner.getTargetFolder(),
  313. build_tools::RelativePath::projectFolder);
  314. out << "\t-$(V_AT)cp " + scriptPath.toUnixStyle() + " $(JUCE_OUTDIR)/$(JUCE_UNITYDIR)" << newLine
  315. << "\t-$(V_AT)mkdir -p $(JUCE_UNITYDESTDIR)" << newLine
  316. << "\t-$(V_AT)cp -R $(JUCE_COPYCMD_UNITY_PLUGIN)" << newLine;
  317. }
  318. else if (type == LV2PlugIn)
  319. {
  320. out << "\t$(V_AT) $(JUCE_OUTDIR)/$(JUCE_TARGET_LV2_MANIFEST_HELPER) "
  321. "$(abspath $(JUCE_LV2_FULL_PATH))" << newLine
  322. << "\t-$(V_AT)mkdir -p $(JUCE_LV2DESTDIR)" << newLine
  323. << "\t-$(V_AT)cp -R $(JUCE_COPYCMD_LV2_PLUGIN)" << newLine;
  324. }
  325. out << newLine;
  326. }
  327. const MakefileProjectExporter& owner;
  328. };
  329. //==============================================================================
  330. static String getDisplayName() { return "Linux Makefile"; }
  331. static String getValueTreeTypeName() { return "LINUX_MAKE"; }
  332. static String getTargetFolderName() { return "LinuxMakefile"; }
  333. Identifier getExporterIdentifier() const override { return getValueTreeTypeName(); }
  334. static MakefileProjectExporter* createForSettings (Project& projectToUse, const ValueTree& settingsToUse)
  335. {
  336. if (settingsToUse.hasType (getValueTreeTypeName()))
  337. return new MakefileProjectExporter (projectToUse, settingsToUse);
  338. return nullptr;
  339. }
  340. //==============================================================================
  341. MakefileProjectExporter (Project& p, const ValueTree& t)
  342. : ProjectExporter (p, t),
  343. extraPkgConfigValue (settings, Ids::linuxExtraPkgConfig, getUndoManager())
  344. {
  345. name = getDisplayName();
  346. targetLocationValue.setDefault (getDefaultBuildsRootFolder() + getTargetFolderName());
  347. }
  348. //==============================================================================
  349. bool canLaunchProject() override { return false; }
  350. bool launchProject() override { return false; }
  351. bool usesMMFiles() const override { return false; }
  352. bool canCopeWithDuplicateFiles() override { return false; }
  353. bool supportsUserDefinedConfigurations() const override { return true; }
  354. bool isXcode() const override { return false; }
  355. bool isVisualStudio() const override { return false; }
  356. bool isCodeBlocks() const override { return false; }
  357. bool isMakefile() const override { return true; }
  358. bool isAndroidStudio() const override { return false; }
  359. bool isCLion() const override { return false; }
  360. bool isAndroid() const override { return false; }
  361. bool isWindows() const override { return false; }
  362. bool isLinux() const override { return true; }
  363. bool isOSX() const override { return false; }
  364. bool isiOS() const override { return false; }
  365. String getNewLineString() const override { return "\n"; }
  366. bool supportsTargetType (build_tools::ProjectType::Target::Type type) const override
  367. {
  368. using Target = build_tools::ProjectType::Target;
  369. switch (type)
  370. {
  371. case Target::GUIApp:
  372. case Target::ConsoleApp:
  373. case Target::StaticLibrary:
  374. case Target::SharedCodeTarget:
  375. case Target::AggregateTarget:
  376. case Target::VSTPlugIn:
  377. case Target::VST3PlugIn:
  378. case Target::StandalonePlugIn:
  379. case Target::DynamicLibrary:
  380. case Target::UnityPlugIn:
  381. case Target::LV2PlugIn:
  382. case Target::LV2TurtleProgram:
  383. return true;
  384. case Target::AAXPlugIn:
  385. case Target::AudioUnitPlugIn:
  386. case Target::AudioUnitv3PlugIn:
  387. case Target::unspecified:
  388. default:
  389. break;
  390. }
  391. return false;
  392. }
  393. void createExporterProperties (PropertyListBuilder& properties) override
  394. {
  395. properties.add (new TextPropertyComponent (extraPkgConfigValue, "pkg-config libraries", 8192, false),
  396. "Extra pkg-config libraries for you application. Each package should be space separated.");
  397. }
  398. void initialiseDependencyPathValues() override
  399. {
  400. vstLegacyPathValueWrapper.init ({ settings, Ids::vstLegacyFolder, nullptr },
  401. getAppSettings().getStoredPath (Ids::vstLegacyPath, TargetOS::linux), TargetOS::linux);
  402. }
  403. //==============================================================================
  404. bool anyTargetIsSharedLibrary() const
  405. {
  406. for (auto* target : targets)
  407. {
  408. auto fileType = target->getTargetFileType();
  409. if (fileType == build_tools::ProjectType::Target::sharedLibraryOrDLL
  410. || fileType == build_tools::ProjectType::Target::pluginBundle)
  411. return true;
  412. }
  413. return false;
  414. }
  415. //==============================================================================
  416. void create (const OwnedArray<LibraryModule>&) const override
  417. {
  418. build_tools::writeStreamToFile (getTargetFolder().getChildFile ("Makefile"), [&] (MemoryOutputStream& mo)
  419. {
  420. mo.setNewLineString (getNewLineString());
  421. writeMakefile (mo);
  422. });
  423. if (project.shouldBuildVST3())
  424. {
  425. auto helperDir = getTargetFolder().getChildFile ("make_helpers");
  426. helperDir.createDirectory();
  427. build_tools::overwriteFileIfDifferentOrThrow (helperDir.getChildFile ("arch_detection.cpp"),
  428. BinaryData::juce_runtime_arch_detection_cpp);
  429. }
  430. }
  431. //==============================================================================
  432. void addPlatformSpecificSettingsForProjectType (const build_tools::ProjectType&) override
  433. {
  434. callForAllSupportedTargets ([this] (build_tools::ProjectType::Target::Type targetType)
  435. {
  436. targets.insert (targetType == build_tools::ProjectType::Target::AggregateTarget ? 0 : -1,
  437. new MakefileTarget (targetType, *this));
  438. });
  439. // If you hit this assert, you tried to generate a project for an exporter
  440. // that does not support any of your targets!
  441. jassert (targets.size() > 0);
  442. }
  443. private:
  444. ValueTreePropertyWithDefault extraPkgConfigValue;
  445. //==============================================================================
  446. StringPairArray getDefines (const BuildConfiguration& config) const
  447. {
  448. StringPairArray result;
  449. result.set ("LINUX", "1");
  450. if (config.isDebug())
  451. {
  452. result.set ("DEBUG", "1");
  453. result.set ("_DEBUG", "1");
  454. }
  455. else
  456. {
  457. result.set ("NDEBUG", "1");
  458. }
  459. result = mergePreprocessorDefs (result, getAllPreprocessorDefs (config, build_tools::ProjectType::Target::unspecified));
  460. return result;
  461. }
  462. StringArray getExtraPkgConfigPackages() const
  463. {
  464. auto packages = StringArray::fromTokens (extraPkgConfigValue.get().toString(), " ", "\"'");
  465. packages.removeEmptyStrings();
  466. return packages;
  467. }
  468. StringArray getCompilePackages() const
  469. {
  470. auto packages = getLinuxPackages (PackageDependencyType::compile);
  471. packages.addArray (getExtraPkgConfigPackages());
  472. return packages;
  473. }
  474. StringArray getLinkPackages() const
  475. {
  476. auto packages = getLinuxPackages (PackageDependencyType::link);
  477. packages.addArray (getExtraPkgConfigPackages());
  478. return packages;
  479. }
  480. String getPreprocessorPkgConfigFlags() const
  481. {
  482. auto compilePackages = getCompilePackages();
  483. if (compilePackages.size() > 0)
  484. return "$(shell $(PKG_CONFIG) --cflags " + compilePackages.joinIntoString (" ") + ")";
  485. return {};
  486. }
  487. String getLinkerPkgConfigFlags() const
  488. {
  489. auto linkPackages = getLinkPackages();
  490. if (linkPackages.size() > 0)
  491. return "$(shell $(PKG_CONFIG) --libs " + linkPackages.joinIntoString (" ") + ")";
  492. return {};
  493. }
  494. StringArray getCPreprocessorFlags (const BuildConfiguration&) const
  495. {
  496. StringArray result;
  497. if (linuxLibs.contains ("pthread"))
  498. result.add ("-pthread");
  499. return result;
  500. }
  501. StringArray getCFlags (const BuildConfiguration& config) const
  502. {
  503. StringArray result;
  504. if (anyTargetIsSharedLibrary())
  505. result.add ("-fPIC");
  506. if (config.isDebug())
  507. {
  508. result.add ("-g");
  509. result.add ("-ggdb");
  510. }
  511. result.add ("-O" + config.getGCCOptimisationFlag());
  512. if (config.isLinkTimeOptimisationEnabled())
  513. result.add ("-flto");
  514. for (auto& recommended : config.getRecommendedCompilerWarningFlags().common)
  515. result.add (recommended);
  516. auto extra = replacePreprocessorTokens (config, getExtraCompilerFlagsString()).trim();
  517. if (extra.isNotEmpty())
  518. result.add (extra);
  519. return result;
  520. }
  521. StringArray getCXXFlags (const BuildConfiguration& config) const
  522. {
  523. StringArray result;
  524. for (auto& recommended : config.getRecommendedCompilerWarningFlags().cpp)
  525. result.add (recommended);
  526. auto cppStandard = project.getCppStandardString();
  527. if (cppStandard == "latest")
  528. cppStandard = project.getLatestNumberedCppStandardString();
  529. result.add ("-std=" + String (shouldUseGNUExtensions() ? "gnu++" : "c++") + cppStandard);
  530. return result;
  531. }
  532. StringArray getHeaderSearchPaths (const BuildConfiguration& config) const
  533. {
  534. StringArray searchPaths (extraSearchPaths);
  535. searchPaths.addArray (config.getHeaderSearchPaths());
  536. searchPaths = getCleanedStringArray (searchPaths);
  537. StringArray result;
  538. for (auto& path : searchPaths)
  539. result.add (build_tools::unixStylePath (replacePreprocessorTokens (config, path)));
  540. return result;
  541. }
  542. StringArray getLibraryNames (const BuildConfiguration& config) const
  543. {
  544. StringArray result (linuxLibs);
  545. auto libraries = StringArray::fromTokens (getExternalLibrariesString(), ";", "\"'");
  546. libraries.removeEmptyStrings();
  547. for (auto& lib : libraries)
  548. result.add (replacePreprocessorTokens (config, lib).trim());
  549. return result;
  550. }
  551. StringArray getLibrarySearchPaths (const BuildConfiguration& config) const
  552. {
  553. auto result = getSearchPathsFromString (config.getLibrarySearchPathString());
  554. for (auto path : moduleLibSearchPaths)
  555. result.add (path + "/" + config.getModuleLibraryArchName());
  556. return result;
  557. }
  558. StringArray getLinkerFlags (const BuildConfiguration& config) const
  559. {
  560. auto result = makefileExtraLinkerFlags;
  561. result.add ("-fvisibility=hidden");
  562. if (config.isLinkTimeOptimisationEnabled())
  563. result.add ("-flto");
  564. auto extraFlags = getExtraLinkerFlagsString().trim();
  565. if (extraFlags.isNotEmpty())
  566. result.add (replacePreprocessorTokens (config, extraFlags));
  567. return result;
  568. }
  569. //==============================================================================
  570. void writeDefineFlags (OutputStream& out, const MakeBuildConfiguration& config) const
  571. {
  572. out << createGCCPreprocessorFlags (mergePreprocessorDefs (getDefines (config), getAllPreprocessorDefs (config, build_tools::ProjectType::Target::unspecified)));
  573. }
  574. void writePkgConfigFlags (OutputStream& out) const
  575. {
  576. auto flags = getPreprocessorPkgConfigFlags();
  577. if (flags.isNotEmpty())
  578. out << " " << flags;
  579. }
  580. void writeCPreprocessorFlags (OutputStream& out, const BuildConfiguration& config) const
  581. {
  582. auto flags = getCPreprocessorFlags (config);
  583. if (! flags.isEmpty())
  584. out << " " << flags.joinIntoString (" ");
  585. }
  586. void writeHeaderPathFlags (OutputStream& out, const BuildConfiguration& config) const
  587. {
  588. for (auto& path : getHeaderSearchPaths (config))
  589. out << " -I" << escapeQuotesAndSpaces (path).replace ("~", "$(HOME)");
  590. }
  591. void writeCppFlags (OutputStream& out, const MakeBuildConfiguration& config) const
  592. {
  593. out << " JUCE_CPPFLAGS := $(DEPFLAGS)";
  594. writeDefineFlags (out, config);
  595. writePkgConfigFlags (out);
  596. writeCPreprocessorFlags (out, config);
  597. writeHeaderPathFlags (out, config);
  598. out << " $(CPPFLAGS)" << newLine;
  599. }
  600. void writeLinkerFlags (OutputStream& out, const BuildConfiguration& config) const
  601. {
  602. out << " JUCE_LDFLAGS += $(TARGET_ARCH) -L$(JUCE_BINDIR) -L$(JUCE_LIBDIR)";
  603. for (auto path : getLibrarySearchPaths (config))
  604. out << " -L" << escapeQuotesAndSpaces (path).replace ("~", "$(HOME)");
  605. auto pkgConfigFlags = getLinkerPkgConfigFlags();
  606. if (pkgConfigFlags.isNotEmpty())
  607. out << " " << getLinkerPkgConfigFlags();
  608. auto linkerFlags = getLinkerFlags (config).joinIntoString (" ");
  609. if (linkerFlags.isNotEmpty())
  610. out << " " << linkerFlags;
  611. for (auto& libName : getLibraryNames (config))
  612. out << " -l" << libName;
  613. out << " $(LDFLAGS)" << newLine;
  614. }
  615. void writeTargetLines (OutputStream& out, const StringArray& packages) const
  616. {
  617. auto n = targets.size();
  618. for (int i = 0; i < n; ++i)
  619. {
  620. if (auto* target = targets.getUnchecked (i))
  621. {
  622. if (target->type == build_tools::ProjectType::Target::AggregateTarget)
  623. {
  624. StringArray dependencies;
  625. MemoryOutputStream subTargetLines;
  626. for (int j = 0; j < n; ++j)
  627. {
  628. if (i == j) continue;
  629. if (auto* dependency = targets.getUnchecked (j))
  630. {
  631. if (dependency->type != build_tools::ProjectType::Target::SharedCodeTarget)
  632. {
  633. auto phonyName = dependency->getPhonyName();
  634. subTargetLines << phonyName << " : " << dependency->getBuildProduct() << newLine;
  635. dependencies.add (phonyName);
  636. }
  637. }
  638. }
  639. out << "all : " << dependencies.joinIntoString (" ") << newLine << newLine;
  640. out << subTargetLines.toString() << newLine << newLine;
  641. }
  642. else
  643. {
  644. if (! getProject().isAudioPluginProject())
  645. out << "all : " << target->getBuildProduct() << newLine << newLine;
  646. target->writeTargetLine (out, packages);
  647. }
  648. }
  649. }
  650. }
  651. void writeConfig (OutputStream& out, const MakeBuildConfiguration& config) const
  652. {
  653. String buildDirName ("build");
  654. auto intermediatesDirName = buildDirName + "/intermediate/" + config.getName();
  655. auto outputDir = buildDirName;
  656. if (config.getTargetBinaryRelativePathString().isNotEmpty())
  657. {
  658. build_tools::RelativePath binaryPath (config.getTargetBinaryRelativePathString(), build_tools::RelativePath::projectFolder);
  659. outputDir = binaryPath.rebased (projectFolder, getTargetFolder(), build_tools::RelativePath::buildTargetFolder).toUnixStyle();
  660. }
  661. out << "ifeq ($(CONFIG)," << escapeQuotesAndSpaces (config.getName()) << ")" << newLine
  662. << " JUCE_BINDIR := " << escapeQuotesAndSpaces (buildDirName) << newLine
  663. << " JUCE_LIBDIR := " << escapeQuotesAndSpaces (buildDirName) << newLine
  664. << " JUCE_OBJDIR := " << escapeQuotesAndSpaces (intermediatesDirName) << newLine
  665. << " JUCE_OUTDIR := " << escapeQuotesAndSpaces (outputDir) << newLine
  666. << newLine
  667. << " ifeq ($(TARGET_ARCH),)" << newLine
  668. << " TARGET_ARCH := " << getArchFlags (config) << newLine
  669. << " endif" << newLine
  670. << newLine;
  671. writeCppFlags (out, config);
  672. for (auto target : targets)
  673. {
  674. auto lines = target->getTargetSettings (config);
  675. if (lines.size() > 0)
  676. out << " " << lines.joinIntoString ("\n ") << newLine;
  677. out << newLine;
  678. }
  679. out << " JUCE_CFLAGS += $(JUCE_CPPFLAGS) $(TARGET_ARCH)";
  680. auto cflags = getCFlags (config).joinIntoString (" ");
  681. if (cflags.isNotEmpty())
  682. out << " " << cflags;
  683. out << " $(CFLAGS)" << newLine;
  684. out << " JUCE_CXXFLAGS += $(JUCE_CFLAGS)";
  685. auto cxxflags = getCXXFlags (config).joinIntoString (" ");
  686. if (cxxflags.isNotEmpty())
  687. out << " " << cxxflags;
  688. out << " $(CXXFLAGS)" << newLine;
  689. writeLinkerFlags (out, config);
  690. out << newLine;
  691. out << " CLEANCMD = rm -rf $(JUCE_OUTDIR)/$(TARGET) $(JUCE_OBJDIR)" << newLine
  692. << "endif" << newLine
  693. << newLine;
  694. }
  695. void writeIncludeLines (OutputStream& out) const
  696. {
  697. auto n = targets.size();
  698. for (int i = 0; i < n; ++i)
  699. {
  700. if (auto* target = targets.getUnchecked (i))
  701. {
  702. if (target->type == build_tools::ProjectType::Target::AggregateTarget)
  703. continue;
  704. out << "-include $(OBJECTS_" << target->getTargetVarName()
  705. << ":%.o=%.d)" << newLine;
  706. }
  707. }
  708. }
  709. static String getCompilerFlagSchemeVariableName (const String& schemeName) { return "JUCE_COMPILERFLAGSCHEME_" + schemeName; }
  710. void findAllFilesToCompile (const Project::Item& projectItem, Array<std::pair<File, String>>& results) const
  711. {
  712. if (projectItem.isGroup())
  713. {
  714. for (int i = 0; i < projectItem.getNumChildren(); ++i)
  715. findAllFilesToCompile (projectItem.getChild (i), results);
  716. }
  717. else
  718. {
  719. if (projectItem.shouldBeCompiled())
  720. {
  721. auto f = projectItem.getFile();
  722. if (shouldFileBeCompiledByDefault (f))
  723. {
  724. auto scheme = projectItem.getCompilerFlagSchemeString();
  725. auto flags = compilerFlagSchemesMap[scheme].get().toString();
  726. if (scheme.isNotEmpty() && flags.isNotEmpty())
  727. results.add ({ f, scheme });
  728. else
  729. results.add ({ f, {} });
  730. }
  731. }
  732. }
  733. }
  734. void writeCompilerFlagSchemes (OutputStream& out, const Array<std::pair<File, String>>& filesToCompile) const
  735. {
  736. StringArray schemesToWrite;
  737. for (auto& f : filesToCompile)
  738. if (f.second.isNotEmpty())
  739. schemesToWrite.addIfNotAlreadyThere (f.second);
  740. if (! schemesToWrite.isEmpty())
  741. {
  742. for (auto& s : schemesToWrite)
  743. out << getCompilerFlagSchemeVariableName (s) << " := "
  744. << compilerFlagSchemesMap[s].get().toString() << newLine;
  745. out << newLine;
  746. }
  747. }
  748. void writeMakefile (OutputStream& out) const
  749. {
  750. out << "# Automatically generated makefile, created by the Projucer" << newLine
  751. << "# Don't edit this file! Your changes will be overwritten when you re-save the Projucer project!" << newLine
  752. << newLine;
  753. out << "# build with \"V=1\" for verbose builds" << newLine
  754. << "ifeq ($(V), 1)" << newLine
  755. << "V_AT =" << newLine
  756. << "else" << newLine
  757. << "V_AT = @" << newLine
  758. << "endif" << newLine
  759. << newLine;
  760. out << "# (this disables dependency generation if multiple architectures are set)" << newLine
  761. << "DEPFLAGS := $(if $(word 2, $(TARGET_ARCH)), , -MMD)" << newLine
  762. << newLine;
  763. out << "ifndef PKG_CONFIG" << newLine
  764. << " PKG_CONFIG=pkg-config" << newLine
  765. << "endif" << newLine
  766. << newLine;
  767. out << "ifndef STRIP" << newLine
  768. << " STRIP=strip" << newLine
  769. << "endif" << newLine
  770. << newLine;
  771. out << "ifndef AR" << newLine
  772. << " AR=ar" << newLine
  773. << "endif" << newLine
  774. << newLine;
  775. out << "ifndef CONFIG" << newLine
  776. << " CONFIG=" << escapeQuotesAndSpaces (getConfiguration(0)->getName()) << newLine
  777. << "endif" << newLine
  778. << newLine;
  779. out << "JUCE_ARCH_LABEL := $(shell uname -m)" << newLine
  780. << newLine;
  781. for (ConstConfigIterator config (*this); config.next();)
  782. writeConfig (out, dynamic_cast<const MakeBuildConfiguration&> (*config));
  783. Array<std::pair<File, String>> filesToCompile;
  784. for (int i = 0; i < getAllGroups().size(); ++i)
  785. findAllFilesToCompile (getAllGroups().getReference (i), filesToCompile);
  786. writeCompilerFlagSchemes (out, filesToCompile);
  787. auto getFilesForTarget = [this] (const Array<std::pair<File, String>>& files,
  788. MakefileTarget* target,
  789. const Project& p) -> Array<std::pair<File, String>>
  790. {
  791. Array<std::pair<File, String>> targetFiles;
  792. auto targetType = (p.isAudioPluginProject() ? target->type : MakefileTarget::SharedCodeTarget);
  793. for (auto& f : files)
  794. if (p.getTargetTypeFromFilePath (f.first, true) == targetType)
  795. targetFiles.add (f);
  796. if (targetType == MakefileTarget::LV2TurtleProgram)
  797. targetFiles.add ({ project.resolveFilename (getLV2TurtleDumpProgramSource().toUnixStyle()), {} });
  798. return targetFiles;
  799. };
  800. for (auto target : targets)
  801. target->writeObjects (out, getFilesForTarget (filesToCompile, target, project));
  802. out << getPhonyTargetLine() << newLine << newLine;
  803. writeTargetLines (out, getLinkPackages());
  804. for (auto target : targets)
  805. target->addFiles (out, getFilesForTarget (filesToCompile, target, project));
  806. out << "clean:" << newLine
  807. << "\t@echo Cleaning " << projectName << newLine
  808. << "\t$(V_AT)$(CLEANCMD)" << newLine
  809. << newLine;
  810. out << "strip:" << newLine
  811. << "\t@echo Stripping " << projectName << newLine
  812. << "\t-$(V_AT)$(STRIP) --strip-unneeded $(JUCE_OUTDIR)/$(TARGET)" << newLine
  813. << newLine;
  814. writeIncludeLines (out);
  815. }
  816. String getArchFlags (const BuildConfiguration& config) const
  817. {
  818. if (auto* makeConfig = dynamic_cast<const MakeBuildConfiguration*> (&config))
  819. return makeConfig->getArchitectureTypeString();
  820. return "-march=native";
  821. }
  822. String getObjectFileFor (const build_tools::RelativePath& file) const
  823. {
  824. return file.getFileNameWithoutExtension()
  825. + "_" + String::toHexString (file.toUnixStyle().hashCode()) + ".o";
  826. }
  827. String getPhonyTargetLine() const
  828. {
  829. MemoryOutputStream phonyTargetLine;
  830. phonyTargetLine << ".PHONY: clean all strip";
  831. if (! getProject().isAudioPluginProject())
  832. return phonyTargetLine.toString();
  833. for (auto target : targets)
  834. if (target->type != build_tools::ProjectType::Target::SharedCodeTarget
  835. && target->type != build_tools::ProjectType::Target::AggregateTarget)
  836. phonyTargetLine << " " << target->getPhonyName();
  837. return phonyTargetLine.toString();
  838. }
  839. friend class CLionProjectExporter;
  840. OwnedArray<MakefileTarget> targets;
  841. JUCE_DECLARE_NON_COPYABLE (MakefileProjectExporter)
  842. };