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.

1041 lines
42KB

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