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.

1024 lines
40KB

  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. auto is32Bit = config.getArchitectureTypeString().contains ("m32");
  153. s.add (String ("JUCE_VST3SUBDIR := Contents/") + (is32Bit ? "i386" : "x86_64") + "-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. }
  382. //==============================================================================
  383. void addPlatformSpecificSettingsForProjectType (const build_tools::ProjectType&) override
  384. {
  385. callForAllSupportedTargets ([this] (build_tools::ProjectType::Target::Type targetType)
  386. {
  387. targets.insert (targetType == build_tools::ProjectType::Target::AggregateTarget ? 0 : -1,
  388. new MakefileTarget (targetType, *this));
  389. });
  390. // If you hit this assert, you tried to generate a project for an exporter
  391. // that does not support any of your targets!
  392. jassert (targets.size() > 0);
  393. }
  394. private:
  395. ValueWithDefault extraPkgConfigValue;
  396. //==============================================================================
  397. StringPairArray getDefines (const BuildConfiguration& config) const
  398. {
  399. StringPairArray result;
  400. result.set ("LINUX", "1");
  401. if (config.isDebug())
  402. {
  403. result.set ("DEBUG", "1");
  404. result.set ("_DEBUG", "1");
  405. }
  406. else
  407. {
  408. result.set ("NDEBUG", "1");
  409. }
  410. result = mergePreprocessorDefs (result, getAllPreprocessorDefs (config, build_tools::ProjectType::Target::unspecified));
  411. return result;
  412. }
  413. StringArray getExtraPkgConfigPackages() const
  414. {
  415. auto packages = StringArray::fromTokens (extraPkgConfigValue.get().toString(), " ", "\"'");
  416. packages.removeEmptyStrings();
  417. return packages;
  418. }
  419. StringArray getCompilePackages() const
  420. {
  421. auto packages = getLinuxPackages (PackageDependencyType::compile);
  422. packages.addArray (getExtraPkgConfigPackages());
  423. return packages;
  424. }
  425. StringArray getLinkPackages() const
  426. {
  427. auto packages = getLinuxPackages (PackageDependencyType::link);
  428. packages.addArray (getExtraPkgConfigPackages());
  429. return packages;
  430. }
  431. String getPreprocessorPkgConfigFlags() const
  432. {
  433. auto compilePackages = getCompilePackages();
  434. if (compilePackages.size() > 0)
  435. return "$(shell pkg-config --cflags " + compilePackages.joinIntoString (" ") + ")";
  436. return {};
  437. }
  438. String getLinkerPkgConfigFlags() const
  439. {
  440. auto linkPackages = getLinkPackages();
  441. if (linkPackages.size() > 0)
  442. return "$(shell pkg-config --libs " + linkPackages.joinIntoString (" ") + ")";
  443. return {};
  444. }
  445. StringArray getCPreprocessorFlags (const BuildConfiguration&) const
  446. {
  447. StringArray result;
  448. if (linuxLibs.contains ("pthread"))
  449. result.add ("-pthread");
  450. return result;
  451. }
  452. StringArray getCFlags (const BuildConfiguration& config) const
  453. {
  454. StringArray result;
  455. if (anyTargetIsSharedLibrary())
  456. result.add ("-fPIC");
  457. if (config.isDebug())
  458. {
  459. result.add ("-g");
  460. result.add ("-ggdb");
  461. }
  462. result.add ("-O" + config.getGCCOptimisationFlag());
  463. if (config.isLinkTimeOptimisationEnabled())
  464. result.add ("-flto");
  465. for (auto& recommended : config.getRecommendedCompilerWarningFlags())
  466. result.add (recommended);
  467. auto extra = replacePreprocessorTokens (config, getExtraCompilerFlagsString()).trim();
  468. if (extra.isNotEmpty())
  469. result.add (extra);
  470. return result;
  471. }
  472. StringArray getCXXFlags() const
  473. {
  474. StringArray result;
  475. auto cppStandard = project.getCppStandardString();
  476. if (cppStandard == "latest")
  477. cppStandard = "17";
  478. cppStandard = "-std=" + String (shouldUseGNUExtensions() ? "gnu++" : "c++") + cppStandard;
  479. result.add (cppStandard);
  480. return result;
  481. }
  482. StringArray getHeaderSearchPaths (const BuildConfiguration& config) const
  483. {
  484. StringArray searchPaths (extraSearchPaths);
  485. searchPaths.addArray (config.getHeaderSearchPaths());
  486. searchPaths = getCleanedStringArray (searchPaths);
  487. StringArray result;
  488. for (auto& path : searchPaths)
  489. result.add (build_tools::unixStylePath (replacePreprocessorTokens (config, path)));
  490. return result;
  491. }
  492. StringArray getLibraryNames (const BuildConfiguration& config) const
  493. {
  494. StringArray result (linuxLibs);
  495. auto libraries = StringArray::fromTokens (getExternalLibrariesString(), ";", "\"'");
  496. libraries.removeEmptyStrings();
  497. for (auto& lib : libraries)
  498. result.add (replacePreprocessorTokens (config, lib).trim());
  499. return result;
  500. }
  501. StringArray getLibrarySearchPaths (const BuildConfiguration& config) const
  502. {
  503. auto result = getSearchPathsFromString (config.getLibrarySearchPathString());
  504. for (auto path : moduleLibSearchPaths)
  505. result.add (path + "/" + config.getModuleLibraryArchName());
  506. return result;
  507. }
  508. StringArray getLinkerFlags (const BuildConfiguration& config) const
  509. {
  510. auto result = makefileExtraLinkerFlags;
  511. result.add ("-fvisibility=hidden");
  512. if (config.isLinkTimeOptimisationEnabled())
  513. result.add ("-flto");
  514. auto extraFlags = getExtraLinkerFlagsString().trim();
  515. if (extraFlags.isNotEmpty())
  516. result.add (replacePreprocessorTokens (config, extraFlags));
  517. return result;
  518. }
  519. //==============================================================================
  520. void writeDefineFlags (OutputStream& out, const MakeBuildConfiguration& config) const
  521. {
  522. out << createGCCPreprocessorFlags (mergePreprocessorDefs (getDefines (config), getAllPreprocessorDefs (config, build_tools::ProjectType::Target::unspecified)));
  523. }
  524. void writePkgConfigFlags (OutputStream& out) const
  525. {
  526. auto flags = getPreprocessorPkgConfigFlags();
  527. if (flags.isNotEmpty())
  528. out << " " << flags;
  529. }
  530. void writeCPreprocessorFlags (OutputStream& out, const BuildConfiguration& config) const
  531. {
  532. auto flags = getCPreprocessorFlags (config);
  533. if (! flags.isEmpty())
  534. out << " " << flags.joinIntoString (" ");
  535. }
  536. void writeHeaderPathFlags (OutputStream& out, const BuildConfiguration& config) const
  537. {
  538. for (auto& path : getHeaderSearchPaths (config))
  539. out << " -I" << escapeSpaces (path).replace ("~", "$(HOME)");
  540. }
  541. void writeCppFlags (OutputStream& out, const MakeBuildConfiguration& config) const
  542. {
  543. out << " JUCE_CPPFLAGS := $(DEPFLAGS)";
  544. writeDefineFlags (out, config);
  545. writePkgConfigFlags (out);
  546. writeCPreprocessorFlags (out, config);
  547. writeHeaderPathFlags (out, config);
  548. out << " $(CPPFLAGS)" << newLine;
  549. }
  550. void writeLinkerFlags (OutputStream& out, const BuildConfiguration& config) const
  551. {
  552. out << " JUCE_LDFLAGS += $(TARGET_ARCH) -L$(JUCE_BINDIR) -L$(JUCE_LIBDIR)";
  553. for (auto path : getLibrarySearchPaths (config))
  554. out << " -L" << escapeSpaces (path).replace ("~", "$(HOME)");
  555. auto pkgConfigFlags = getLinkerPkgConfigFlags();
  556. if (pkgConfigFlags.isNotEmpty())
  557. out << " " << getLinkerPkgConfigFlags();
  558. auto linkerFlags = getLinkerFlags (config).joinIntoString (" ");
  559. if (linkerFlags.isNotEmpty())
  560. out << " " << linkerFlags;
  561. for (auto& libName : getLibraryNames (config))
  562. out << " -l" << libName;
  563. out << " $(LDFLAGS)" << newLine;
  564. }
  565. void writeTargetLines (OutputStream& out, const StringArray& packages) const
  566. {
  567. auto n = targets.size();
  568. for (int i = 0; i < n; ++i)
  569. {
  570. if (auto* target = targets.getUnchecked (i))
  571. {
  572. if (target->type == build_tools::ProjectType::Target::AggregateTarget)
  573. {
  574. StringArray dependencies;
  575. MemoryOutputStream subTargetLines;
  576. for (int j = 0; j < n; ++j)
  577. {
  578. if (i == j) continue;
  579. if (auto* dependency = targets.getUnchecked (j))
  580. {
  581. if (dependency->type != build_tools::ProjectType::Target::SharedCodeTarget)
  582. {
  583. auto phonyName = dependency->getPhonyName();
  584. subTargetLines << phonyName << " : " << dependency->getBuildProduct() << newLine;
  585. dependencies.add (phonyName);
  586. }
  587. }
  588. }
  589. out << "all : " << dependencies.joinIntoString (" ") << newLine << newLine;
  590. out << subTargetLines.toString() << newLine << newLine;
  591. }
  592. else
  593. {
  594. if (! getProject().isAudioPluginProject())
  595. out << "all : " << target->getBuildProduct() << newLine << newLine;
  596. target->writeTargetLine (out, packages);
  597. }
  598. }
  599. }
  600. }
  601. void writeConfig (OutputStream& out, const MakeBuildConfiguration& config) const
  602. {
  603. String buildDirName ("build");
  604. auto intermediatesDirName = buildDirName + "/intermediate/" + config.getName();
  605. auto outputDir = buildDirName;
  606. if (config.getTargetBinaryRelativePathString().isNotEmpty())
  607. {
  608. build_tools::RelativePath binaryPath (config.getTargetBinaryRelativePathString(), build_tools::RelativePath::projectFolder);
  609. outputDir = binaryPath.rebased (projectFolder, getTargetFolder(), build_tools::RelativePath::buildTargetFolder).toUnixStyle();
  610. }
  611. out << "ifeq ($(CONFIG)," << escapeSpaces (config.getName()) << ")" << newLine
  612. << " JUCE_BINDIR := " << escapeSpaces (buildDirName) << newLine
  613. << " JUCE_LIBDIR := " << escapeSpaces (buildDirName) << newLine
  614. << " JUCE_OBJDIR := " << escapeSpaces (intermediatesDirName) << newLine
  615. << " JUCE_OUTDIR := " << escapeSpaces (outputDir) << newLine
  616. << newLine
  617. << " ifeq ($(TARGET_ARCH),)" << newLine
  618. << " TARGET_ARCH := " << getArchFlags (config) << newLine
  619. << " endif" << newLine
  620. << newLine;
  621. writeCppFlags (out, config);
  622. for (auto target : targets)
  623. {
  624. auto lines = target->getTargetSettings (config);
  625. if (lines.size() > 0)
  626. out << " " << lines.joinIntoString ("\n ") << newLine;
  627. out << newLine;
  628. }
  629. out << " JUCE_CFLAGS += $(JUCE_CPPFLAGS) $(TARGET_ARCH)";
  630. auto cflags = getCFlags (config).joinIntoString (" ");
  631. if (cflags.isNotEmpty())
  632. out << " " << cflags;
  633. out << " $(CFLAGS)" << newLine;
  634. out << " JUCE_CXXFLAGS += $(JUCE_CFLAGS)";
  635. auto cxxflags = getCXXFlags().joinIntoString (" ");
  636. if (cxxflags.isNotEmpty())
  637. out << " " << cxxflags;
  638. out << " $(CXXFLAGS)" << newLine;
  639. writeLinkerFlags (out, config);
  640. out << newLine;
  641. out << " CLEANCMD = rm -rf $(JUCE_OUTDIR)/$(TARGET) $(JUCE_OBJDIR)" << newLine
  642. << "endif" << newLine
  643. << newLine;
  644. }
  645. void writeIncludeLines (OutputStream& out) const
  646. {
  647. auto n = targets.size();
  648. for (int i = 0; i < n; ++i)
  649. {
  650. if (auto* target = targets.getUnchecked (i))
  651. {
  652. if (target->type == build_tools::ProjectType::Target::AggregateTarget)
  653. continue;
  654. out << "-include $(OBJECTS_" << target->getTargetVarName()
  655. << ":%.o=%.d)" << newLine;
  656. }
  657. }
  658. }
  659. static String getCompilerFlagSchemeVariableName (const String& schemeName) { return "JUCE_COMPILERFLAGSCHEME_" + schemeName; }
  660. void findAllFilesToCompile (const Project::Item& projectItem, Array<std::pair<File, String>>& results) const
  661. {
  662. if (projectItem.isGroup())
  663. {
  664. for (int i = 0; i < projectItem.getNumChildren(); ++i)
  665. findAllFilesToCompile (projectItem.getChild (i), results);
  666. }
  667. else
  668. {
  669. if (projectItem.shouldBeCompiled())
  670. {
  671. auto f = projectItem.getFile();
  672. if (shouldFileBeCompiledByDefault (f))
  673. {
  674. auto scheme = projectItem.getCompilerFlagSchemeString();
  675. auto flags = compilerFlagSchemesMap[scheme].get().toString();
  676. if (scheme.isNotEmpty() && flags.isNotEmpty())
  677. results.add ({ f, scheme });
  678. else
  679. results.add ({ f, {} });
  680. }
  681. }
  682. }
  683. }
  684. void writeCompilerFlagSchemes (OutputStream& out, const Array<std::pair<File, String>>& filesToCompile) const
  685. {
  686. StringArray schemesToWrite;
  687. for (auto& f : filesToCompile)
  688. if (f.second.isNotEmpty())
  689. schemesToWrite.addIfNotAlreadyThere (f.second);
  690. if (! schemesToWrite.isEmpty())
  691. {
  692. for (auto& s : schemesToWrite)
  693. out << getCompilerFlagSchemeVariableName (s) << " := "
  694. << compilerFlagSchemesMap[s].get().toString() << newLine;
  695. out << newLine;
  696. }
  697. }
  698. void writeMakefile (OutputStream& out) const
  699. {
  700. out << "# Automatically generated makefile, created by the Projucer" << newLine
  701. << "# Don't edit this file! Your changes will be overwritten when you re-save the Projucer project!" << newLine
  702. << newLine;
  703. out << "# build with \"V=1\" for verbose builds" << newLine
  704. << "ifeq ($(V), 1)" << newLine
  705. << "V_AT =" << newLine
  706. << "else" << newLine
  707. << "V_AT = @" << newLine
  708. << "endif" << newLine
  709. << newLine;
  710. out << "# (this disables dependency generation if multiple architectures are set)" << newLine
  711. << "DEPFLAGS := $(if $(word 2, $(TARGET_ARCH)), , -MMD)" << newLine
  712. << newLine;
  713. out << "ifndef STRIP" << newLine
  714. << " STRIP=strip" << newLine
  715. << "endif" << newLine
  716. << newLine;
  717. out << "ifndef AR" << newLine
  718. << " AR=ar" << newLine
  719. << "endif" << newLine
  720. << newLine;
  721. out << "ifndef CONFIG" << newLine
  722. << " CONFIG=" << escapeSpaces (getConfiguration(0)->getName()) << newLine
  723. << "endif" << newLine
  724. << newLine;
  725. out << "JUCE_ARCH_LABEL := $(shell uname -m)" << newLine
  726. << newLine;
  727. for (ConstConfigIterator config (*this); config.next();)
  728. writeConfig (out, dynamic_cast<const MakeBuildConfiguration&> (*config));
  729. Array<std::pair<File, String>> filesToCompile;
  730. for (int i = 0; i < getAllGroups().size(); ++i)
  731. findAllFilesToCompile (getAllGroups().getReference (i), filesToCompile);
  732. writeCompilerFlagSchemes (out, filesToCompile);
  733. auto getFilesForTarget = [] (const Array<std::pair<File, String>>& files, MakefileTarget* target, const Project& p) -> Array<std::pair<File, String>>
  734. {
  735. Array<std::pair<File, String>> targetFiles;
  736. auto targetType = (p.isAudioPluginProject() ? target->type : MakefileTarget::SharedCodeTarget);
  737. for (auto& f : files)
  738. if (p.getTargetTypeFromFilePath (f.first, true) == targetType)
  739. targetFiles.add (f);
  740. return targetFiles;
  741. };
  742. for (auto target : targets)
  743. target->writeObjects (out, getFilesForTarget (filesToCompile, target, project));
  744. out << getPhonyTargetLine() << newLine << newLine;
  745. writeTargetLines (out, getLinkPackages());
  746. for (auto target : targets)
  747. target->addFiles (out, getFilesForTarget (filesToCompile, target, project));
  748. out << "clean:" << newLine
  749. << "\t@echo Cleaning " << projectName << newLine
  750. << "\t$(V_AT)$(CLEANCMD)" << newLine
  751. << newLine;
  752. out << "strip:" << newLine
  753. << "\t@echo Stripping " << projectName << newLine
  754. << "\t-$(V_AT)$(STRIP) --strip-unneeded $(JUCE_OUTDIR)/$(TARGET)" << newLine
  755. << newLine;
  756. writeIncludeLines (out);
  757. }
  758. String getArchFlags (const BuildConfiguration& config) const
  759. {
  760. if (auto* makeConfig = dynamic_cast<const MakeBuildConfiguration*> (&config))
  761. return makeConfig->getArchitectureTypeString();
  762. return "-march=native";
  763. }
  764. String getObjectFileFor (const build_tools::RelativePath& file) const
  765. {
  766. return file.getFileNameWithoutExtension()
  767. + "_" + String::toHexString (file.toUnixStyle().hashCode()) + ".o";
  768. }
  769. String getPhonyTargetLine() const
  770. {
  771. MemoryOutputStream phonyTargetLine;
  772. phonyTargetLine << ".PHONY: clean all strip";
  773. if (! getProject().isAudioPluginProject())
  774. return phonyTargetLine.toString();
  775. for (auto target : targets)
  776. if (target->type != build_tools::ProjectType::Target::SharedCodeTarget
  777. && target->type != build_tools::ProjectType::Target::AggregateTarget)
  778. phonyTargetLine << " " << target->getPhonyName();
  779. return phonyTargetLine.toString();
  780. }
  781. friend class CLionProjectExporter;
  782. OwnedArray<MakefileTarget> targets;
  783. JUCE_DECLARE_NON_COPYABLE (MakefileProjectExporter)
  784. };