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.

1083 lines
44KB

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