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.

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