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.

1042 lines
41KB

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