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.

1036 lines
41KB

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