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.

1039 lines
42KB

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