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.

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