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.

902 lines
32KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  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 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. #pragma once
  20. //==============================================================================
  21. class MakefileProjectExporter : public ProjectExporter
  22. {
  23. protected:
  24. //==============================================================================
  25. class MakeBuildConfiguration : public BuildConfiguration
  26. {
  27. public:
  28. MakeBuildConfiguration (Project& p, const ValueTree& settings, const ProjectExporter& e)
  29. : BuildConfiguration (p, settings, e),
  30. architectureTypeValue (config, Ids::linuxArchitecture, getUndoManager(), "-march=native")
  31. {
  32. linkTimeOptimisationValue.setDefault (false);
  33. optimisationLevelValue.setDefault (isDebug() ? gccO0 : gccO3);
  34. }
  35. void createConfigProperties (PropertyListBuilder& props) override
  36. {
  37. addGCCOptimisationProperty (props);
  38. props.add (new ChoicePropertyComponent (architectureTypeValue, "Architecture",
  39. { "<None>", "Native", "32-bit (-m32)", "64-bit (-m64)", "ARM v6", "ARM v7" },
  40. { { String() } , "-march=native", "-m32", "-m64", "-march=armv6", "-march=armv7" }),
  41. "Specifies the 32/64-bit architecture to use.");
  42. }
  43. String getModuleLibraryArchName() const override
  44. {
  45. auto archFlag = getArchitectureTypeString();
  46. String prefix ("-march=");
  47. if (archFlag.startsWith (prefix))
  48. return archFlag.substring (prefix.length());
  49. if (archFlag == "-m64")
  50. return "x86_64";
  51. if (archFlag == "-m32")
  52. return "i386";
  53. return "${JUCE_ARCH_LABEL}";
  54. }
  55. String getArchitectureTypeString() const { return architectureTypeValue.get(); }
  56. //==============================================================================
  57. ValueWithDefault architectureTypeValue;
  58. };
  59. BuildConfiguration::Ptr createBuildConfig (const ValueTree& tree) const override
  60. {
  61. return new MakeBuildConfiguration (project, tree, *this);
  62. }
  63. public:
  64. //==============================================================================
  65. class MakefileTarget : public ProjectType::Target
  66. {
  67. public:
  68. MakefileTarget (ProjectType::Target::Type targetType, const MakefileProjectExporter& exporter)
  69. : ProjectType::Target (targetType), owner (exporter)
  70. {}
  71. StringArray getCompilerFlags() const
  72. {
  73. StringArray result;
  74. if (getTargetFileType() == sharedLibraryOrDLL || getTargetFileType() == pluginBundle)
  75. {
  76. result.add ("-fPIC");
  77. result.add ("-fvisibility=hidden");
  78. }
  79. return result;
  80. }
  81. StringArray getLinkerFlags() const
  82. {
  83. StringArray result;
  84. if (getTargetFileType() == sharedLibraryOrDLL || getTargetFileType() == pluginBundle)
  85. {
  86. result.add ("-shared");
  87. if (getTargetFileType() == pluginBundle)
  88. result.add ("-Wl,--no-undefined");
  89. }
  90. return result;
  91. }
  92. StringPairArray getDefines (const BuildConfiguration& config) const
  93. {
  94. StringPairArray result;
  95. auto commonOptionKeys = owner.getAllPreprocessorDefs (config, ProjectType::Target::unspecified).getAllKeys();
  96. auto targetSpecific = owner.getAllPreprocessorDefs (config, type);
  97. for (auto& key : targetSpecific.getAllKeys())
  98. if (! commonOptionKeys.contains (key))
  99. result.set (key, targetSpecific[key]);
  100. return result;
  101. }
  102. StringArray getTargetSettings (const MakeBuildConfiguration& config) const
  103. {
  104. if (type == AggregateTarget)
  105. // the aggregate target should not specify any settings at all!
  106. // it just defines dependencies on the other targets.
  107. return {};
  108. StringArray defines;
  109. auto defs = getDefines (config);
  110. for (auto& key : defs.getAllKeys())
  111. defines.add ("-D" + key + "=" + defs[key]);
  112. StringArray s;
  113. auto cppflagsVarName = "JUCE_CPPFLAGS_" + getTargetVarName();
  114. s.add (cppflagsVarName + " := " + defines.joinIntoString (" "));
  115. auto cflags = getCompilerFlags();
  116. if (! cflags.isEmpty())
  117. s.add ("JUCE_CFLAGS_" + getTargetVarName() + " := " + cflags.joinIntoString (" "));
  118. auto ldflags = getLinkerFlags();
  119. if (! ldflags.isEmpty())
  120. s.add ("JUCE_LDFLAGS_" + getTargetVarName() + " := " + ldflags.joinIntoString (" "));
  121. auto targetName = owner.replacePreprocessorTokens (config, config.getTargetBinaryNameString());
  122. if (owner.projectType.isStaticLibrary())
  123. targetName = getStaticLibbedFilename (targetName);
  124. else if (owner.projectType.isDynamicLibrary())
  125. targetName = getDynamicLibbedFilename (targetName);
  126. else
  127. targetName = targetName.upToLastOccurrenceOf (".", false, false) + getTargetFileSuffix();
  128. s.add ("JUCE_TARGET_" + getTargetVarName() + String (" := ") + escapeSpaces (targetName));
  129. return s;
  130. }
  131. String getTargetFileSuffix() const
  132. {
  133. switch (type)
  134. {
  135. case VSTPlugIn:
  136. case DynamicLibrary: return ".so";
  137. case SharedCodeTarget:
  138. case StaticLibrary: return ".a";
  139. default: break;
  140. }
  141. return {};
  142. }
  143. String getTargetVarName() const
  144. {
  145. return String (getName()).toUpperCase().replaceCharacter (L' ', L'_');
  146. }
  147. void writeObjects (OutputStream& out) const
  148. {
  149. Array<RelativePath> targetFiles;
  150. for (int i = 0; i < owner.getAllGroups().size(); ++i)
  151. findAllFilesToCompile (owner.getAllGroups().getReference(i), targetFiles);
  152. out << "OBJECTS_" + getTargetVarName() + String (" := \\") << newLine;
  153. for (int i = 0; i < targetFiles.size(); ++i)
  154. out << " $(JUCE_OBJDIR)/" << escapeSpaces (owner.getObjectFileFor (targetFiles.getReference(i))) << " \\" << newLine;
  155. out << newLine;
  156. }
  157. void findAllFilesToCompile (const Project::Item& projectItem, Array<RelativePath>& results) const
  158. {
  159. if (projectItem.isGroup())
  160. {
  161. for (int i = 0; i < projectItem.getNumChildren(); ++i)
  162. findAllFilesToCompile (projectItem.getChild(i), results);
  163. }
  164. else
  165. {
  166. if (projectItem.shouldBeCompiled())
  167. {
  168. auto targetType = (owner.getProject().getProjectType().isAudioPlugin() ? type : SharedCodeTarget);
  169. auto f = projectItem.getFile();
  170. RelativePath relativePath (f, owner.getTargetFolder(), RelativePath::buildTargetFolder);
  171. if (owner.shouldFileBeCompiledByDefault (relativePath)
  172. && owner.getProject().getTargetTypeFromFilePath (f, true) == targetType)
  173. results.add (relativePath);
  174. }
  175. }
  176. }
  177. void addFiles (OutputStream& out)
  178. {
  179. Array<RelativePath> targetFiles;
  180. for (int i = 0; i < owner.getAllGroups().size(); ++i)
  181. findAllFilesToCompile (owner.getAllGroups().getReference(i), targetFiles);
  182. auto cppflagsVarName = "JUCE_CPPFLAGS_" + getTargetVarName();
  183. auto cflagsVarName = "JUCE_CFLAGS_" + getTargetVarName();
  184. for (int i = 0; i < targetFiles.size(); ++i)
  185. {
  186. jassert (targetFiles.getReference(i).getRoot() == RelativePath::buildTargetFolder);
  187. out << "$(JUCE_OBJDIR)/" << escapeSpaces (owner.getObjectFileFor (targetFiles.getReference(i)))
  188. << ": " << escapeSpaces (targetFiles.getReference(i).toUnixStyle()) << newLine
  189. << "\t-$(V_AT)mkdir -p $(JUCE_OBJDIR)" << newLine
  190. << "\t@echo \"Compiling " << targetFiles.getReference(i).getFileName() << "\"" << newLine
  191. << (targetFiles.getReference(i).hasFileExtension ("c;s;S") ? "\t$(V_AT)$(CC) $(JUCE_CFLAGS) "
  192. : "\t$(V_AT)$(CXX) $(JUCE_CXXFLAGS) ")
  193. << "$(" << cppflagsVarName << ") $(" << cflagsVarName << ") -o \"$@\" -c \"$<\""
  194. << newLine
  195. << newLine;
  196. }
  197. }
  198. String getBuildProduct() const
  199. {
  200. return "$(JUCE_OUTDIR)/$(JUCE_TARGET_" + getTargetVarName() + ")";
  201. }
  202. String getPhonyName() const
  203. {
  204. return String (getName()).upToFirstOccurrenceOf (" ", false, false);
  205. }
  206. void writeTargetLine (OutputStream& out, const bool useLinuxPackages)
  207. {
  208. jassert (type != AggregateTarget);
  209. out << getBuildProduct() << " : "
  210. << ((useLinuxPackages) ? "check-pkg-config " : "")
  211. << "$(OBJECTS_" << getTargetVarName() << ") $(RESOURCES)";
  212. if (type != SharedCodeTarget && owner.shouldBuildTargetType (SharedCodeTarget))
  213. out << " $(JUCE_OUTDIR)/$(JUCE_TARGET_SHARED_CODE)";
  214. out << newLine << "\t@echo Linking \"" << owner.projectName << " - " << getName() << "\"" << newLine
  215. << "\t-$(V_AT)mkdir -p $(JUCE_BINDIR)" << newLine
  216. << "\t-$(V_AT)mkdir -p $(JUCE_LIBDIR)" << newLine
  217. << "\t-$(V_AT)mkdir -p $(JUCE_OUTDIR)" << newLine;
  218. if (owner.projectType.isStaticLibrary() || type == SharedCodeTarget)
  219. out << "\t$(V_AT)$(AR) -rcs " << getBuildProduct()
  220. << " $(OBJECTS_" << getTargetVarName() << ")" << newLine;
  221. else
  222. {
  223. out << "\t$(V_AT)$(CXX) -o " << getBuildProduct()
  224. << " $(OBJECTS_" << getTargetVarName() << ") ";
  225. if (owner.shouldBuildTargetType (SharedCodeTarget))
  226. out << "$(JUCE_OUTDIR)/$(JUCE_TARGET_SHARED_CODE) ";
  227. out << "$(JUCE_LDFLAGS) ";
  228. if (getTargetFileType() == sharedLibraryOrDLL || getTargetFileType() == pluginBundle
  229. || type == GUIApp || type == StandalonePlugIn)
  230. out << "$(JUCE_LDFLAGS_" << getTargetVarName() << ") ";
  231. out << "$(RESOURCES) $(TARGET_ARCH)" << newLine;
  232. }
  233. out << newLine;
  234. }
  235. const MakefileProjectExporter& owner;
  236. };
  237. //==============================================================================
  238. static const char* getNameLinux() { return "Linux Makefile"; }
  239. static const char* getValueTreeTypeName() { return "LINUX_MAKE"; }
  240. String getExtraPkgConfigString() const { return extraPkgConfigValue.get(); }
  241. static MakefileProjectExporter* createForSettings (Project& project, const ValueTree& settings)
  242. {
  243. if (settings.hasType (getValueTreeTypeName()))
  244. return new MakefileProjectExporter (project, settings);
  245. return nullptr;
  246. }
  247. //==============================================================================
  248. MakefileProjectExporter (Project& p, const ValueTree& t)
  249. : ProjectExporter (p, t),
  250. extraPkgConfigValue (settings, Ids::linuxExtraPkgConfig, getUndoManager())
  251. {
  252. name = getNameLinux();
  253. targetLocationValue.setDefault (getDefaultBuildsRootFolder() + getTargetFolderForExporter (getValueTreeTypeName()));
  254. }
  255. //==============================================================================
  256. bool canLaunchProject() override { return false; }
  257. bool launchProject() override { return false; }
  258. bool usesMMFiles() const override { return false; }
  259. bool canCopeWithDuplicateFiles() override { return false; }
  260. bool supportsUserDefinedConfigurations() const override { return true; }
  261. bool isXcode() const override { return false; }
  262. bool isVisualStudio() const override { return false; }
  263. bool isCodeBlocks() const override { return false; }
  264. bool isMakefile() const override { return true; }
  265. bool isAndroidStudio() const override { return false; }
  266. bool isCLion() const override { return false; }
  267. bool isAndroid() const override { return false; }
  268. bool isWindows() const override { return false; }
  269. bool isLinux() const override { return true; }
  270. bool isOSX() const override { return false; }
  271. bool isiOS() const override { return false; }
  272. bool supportsTargetType (ProjectType::Target::Type type) const override
  273. {
  274. switch (type)
  275. {
  276. case ProjectType::Target::GUIApp:
  277. case ProjectType::Target::ConsoleApp:
  278. case ProjectType::Target::StaticLibrary:
  279. case ProjectType::Target::SharedCodeTarget:
  280. case ProjectType::Target::AggregateTarget:
  281. case ProjectType::Target::VSTPlugIn:
  282. case ProjectType::Target::StandalonePlugIn:
  283. case ProjectType::Target::DynamicLibrary:
  284. return true;
  285. default:
  286. break;
  287. }
  288. return false;
  289. }
  290. void createExporterProperties (PropertyListBuilder& properties) override
  291. {
  292. properties.add (new TextPropertyComponent (extraPkgConfigValue, "pkg-config libraries", 8192, false),
  293. "Extra pkg-config libraries for you application. Each package should be space separated.");
  294. }
  295. //==============================================================================
  296. bool anyTargetIsSharedLibrary() const
  297. {
  298. for (auto* target : targets)
  299. {
  300. auto fileType = target->getTargetFileType();
  301. if (fileType == ProjectType::Target::sharedLibraryOrDLL
  302. || fileType == ProjectType::Target::pluginBundle)
  303. return true;
  304. }
  305. return false;
  306. }
  307. //==============================================================================
  308. void create (const OwnedArray<LibraryModule>&) const override
  309. {
  310. MemoryOutputStream mo;
  311. writeMakefile (mo);
  312. overwriteFileIfDifferentOrThrow (getTargetFolder().getChildFile ("Makefile"), mo);
  313. }
  314. //==============================================================================
  315. void addPlatformSpecificSettingsForProjectType (const ProjectType&) override
  316. {
  317. callForAllSupportedTargets ([this] (ProjectType::Target::Type targetType)
  318. {
  319. if (MakefileTarget* target = new MakefileTarget (targetType, *this))
  320. {
  321. if (targetType == ProjectType::Target::AggregateTarget)
  322. targets.insert (0, target);
  323. else
  324. targets.add (target);
  325. }
  326. });
  327. // If you hit this assert, you tried to generate a project for an exporter
  328. // that does not support any of your targets!
  329. jassert (targets.size() > 0);
  330. }
  331. //==============================================================================
  332. void initialiseDependencyPathValues() override {}
  333. private:
  334. ValueWithDefault extraPkgConfigValue;
  335. //==============================================================================
  336. StringPairArray getDefines (const BuildConfiguration& config) const
  337. {
  338. StringPairArray result;
  339. result.set ("LINUX", "1");
  340. if (config.isDebug())
  341. {
  342. result.set ("DEBUG", "1");
  343. result.set ("_DEBUG", "1");
  344. }
  345. else
  346. {
  347. result.set ("NDEBUG", "1");
  348. }
  349. result = mergePreprocessorDefs (result, getAllPreprocessorDefs (config, ProjectType::Target::unspecified));
  350. return result;
  351. }
  352. StringArray getPackages() const
  353. {
  354. StringArray packages;
  355. packages.addTokens (getExtraPkgConfigString(), " ", "\"'");
  356. packages.removeEmptyStrings();
  357. packages.addArray (linuxPackages);
  358. if (isWebBrowserComponentEnabled())
  359. {
  360. packages.add ("webkit2gtk-4.0");
  361. packages.add ("gtk+-x11-3.0");
  362. }
  363. packages.removeDuplicates (false);
  364. return packages;
  365. }
  366. String getPreprocessorPkgConfigFlags() const
  367. {
  368. auto packages = getPackages();
  369. if (packages.size() > 0)
  370. return "$(shell pkg-config --cflags " + packages.joinIntoString (" ") + ")";
  371. return {};
  372. }
  373. String getLinkerPkgConfigFlags() const
  374. {
  375. auto packages = getPackages();
  376. if (packages.size() > 0)
  377. return "$(shell pkg-config --libs " + packages.joinIntoString (" ") + ")";
  378. return {};
  379. }
  380. StringArray getCPreprocessorFlags (const BuildConfiguration&) const
  381. {
  382. StringArray result;
  383. if (linuxLibs.contains("pthread"))
  384. result.add ("-pthread");
  385. return result;
  386. }
  387. StringArray getCFlags (const BuildConfiguration& config) const
  388. {
  389. StringArray result;
  390. if (anyTargetIsSharedLibrary())
  391. result.add ("-fPIC");
  392. if (config.isDebug())
  393. {
  394. result.add ("-g");
  395. result.add ("-ggdb");
  396. }
  397. result.add ("-O" + config.getGCCOptimisationFlag());
  398. if (config.isLinkTimeOptimisationEnabled())
  399. result.add ("-flto");
  400. auto extra = replacePreprocessorTokens (config, getExtraCompilerFlagsString()).trim();
  401. if (extra.isNotEmpty())
  402. result.add (extra);
  403. return result;
  404. }
  405. StringArray getCXXFlags() const
  406. {
  407. StringArray result;
  408. auto cppStandard = project.getCppStandardString();
  409. if (cppStandard == "latest")
  410. cppStandard = "17";
  411. cppStandard = "-std=" + String (shouldUseGNUExtensions() ? "gnu++" : "c++") + cppStandard;
  412. result.add (cppStandard);
  413. return result;
  414. }
  415. StringArray getHeaderSearchPaths (const BuildConfiguration& config) const
  416. {
  417. StringArray searchPaths (extraSearchPaths);
  418. searchPaths.addArray (config.getHeaderSearchPaths());
  419. searchPaths = getCleanedStringArray (searchPaths);
  420. StringArray result;
  421. for (auto& path : searchPaths)
  422. result.add (FileHelpers::unixStylePath (replacePreprocessorTokens (config, path)));
  423. return result;
  424. }
  425. StringArray getLibraryNames (const BuildConfiguration& config) const
  426. {
  427. StringArray result (linuxLibs);
  428. auto libraries = StringArray::fromTokens (getExternalLibrariesString(), ";", "\"'");
  429. libraries.removeEmptyStrings();
  430. for (auto& lib : libraries)
  431. result.add (replacePreprocessorTokens (config, lib).trim());
  432. return result;
  433. }
  434. StringArray getLibrarySearchPaths (const BuildConfiguration& config) const
  435. {
  436. auto result = getSearchPathsFromString (config.getLibrarySearchPathString());
  437. for (auto path : moduleLibSearchPaths)
  438. result.add (path + "/" + config.getModuleLibraryArchName());
  439. return result;
  440. }
  441. StringArray getLinkerFlags (const BuildConfiguration& config) const
  442. {
  443. auto result = makefileExtraLinkerFlags;
  444. if (! config.isDebug())
  445. result.add ("-fvisibility=hidden");
  446. if (config.isLinkTimeOptimisationEnabled())
  447. result.add ("-flto");
  448. auto extraFlags = getExtraLinkerFlagsString().trim();
  449. if (extraFlags.isNotEmpty())
  450. result.add (replacePreprocessorTokens (config, extraFlags));
  451. return result;
  452. }
  453. bool isWebBrowserComponentEnabled() const
  454. {
  455. static String guiExtrasModule ("juce_gui_extra");
  456. return (project.getModules().isModuleEnabled (guiExtrasModule)
  457. && project.isConfigFlagEnabled ("JUCE_WEB_BROWSER", true));
  458. }
  459. //==============================================================================
  460. void writeDefineFlags (OutputStream& out, const MakeBuildConfiguration& config) const
  461. {
  462. out << createGCCPreprocessorFlags (mergePreprocessorDefs (getDefines (config), getAllPreprocessorDefs (config, ProjectType::Target::unspecified)));
  463. }
  464. void writePkgConfigFlags (OutputStream& out) const
  465. {
  466. auto flags = getPreprocessorPkgConfigFlags();
  467. if (flags.isNotEmpty())
  468. out << " " << flags;
  469. }
  470. void writeCPreprocessorFlags (OutputStream& out, const BuildConfiguration& config) const
  471. {
  472. auto flags = getCPreprocessorFlags (config);
  473. if (! flags.isEmpty())
  474. out << " " << flags.joinIntoString (" ");
  475. }
  476. void writeHeaderPathFlags (OutputStream& out, const BuildConfiguration& config) const
  477. {
  478. for (auto& path : getHeaderSearchPaths (config))
  479. out << " -I" << escapeSpaces (path).replace ("~", "$(HOME)");
  480. }
  481. void writeCppFlags (OutputStream& out, const MakeBuildConfiguration& config) const
  482. {
  483. out << " JUCE_CPPFLAGS := $(DEPFLAGS)";
  484. writeDefineFlags (out, config);
  485. writePkgConfigFlags (out);
  486. writeCPreprocessorFlags (out, config);
  487. writeHeaderPathFlags (out, config);
  488. out << " $(CPPFLAGS)" << newLine;
  489. }
  490. void writeLinkerFlags (OutputStream& out, const BuildConfiguration& config) const
  491. {
  492. out << " JUCE_LDFLAGS += $(TARGET_ARCH) -L$(JUCE_BINDIR) -L$(JUCE_LIBDIR)";
  493. for (auto path : getLibrarySearchPaths (config))
  494. out << " -L" << escapeSpaces (path).replace ("~", "$(HOME)");
  495. auto pkgConfigFlags = getLinkerPkgConfigFlags();
  496. if (pkgConfigFlags.isNotEmpty())
  497. out << " " << getLinkerPkgConfigFlags();
  498. auto linkerFlags = getLinkerFlags (config).joinIntoString (" ");
  499. if (linkerFlags.isNotEmpty())
  500. out << " " << linkerFlags;
  501. for (auto& libName : getLibraryNames (config))
  502. out << " -l" << libName;
  503. out << " $(LDFLAGS)" << newLine;
  504. }
  505. void writeTargetLines (OutputStream& out, const bool useLinuxPackages) const
  506. {
  507. auto n = targets.size();
  508. for (int i = 0; i < n; ++i)
  509. {
  510. if (auto* target = targets.getUnchecked (i))
  511. {
  512. if (target->type == ProjectType::Target::AggregateTarget)
  513. {
  514. StringArray dependencies;
  515. MemoryOutputStream subTargetLines;
  516. for (int j = 0; j < n; ++j)
  517. {
  518. if (i == j) continue;
  519. if (auto* dependency = targets.getUnchecked (j))
  520. {
  521. if (dependency->type != ProjectType::Target::SharedCodeTarget)
  522. {
  523. auto phonyName = dependency->getPhonyName();
  524. subTargetLines << phonyName << " : " << dependency->getBuildProduct() << newLine;
  525. dependencies.add (phonyName);
  526. }
  527. }
  528. }
  529. out << "all : " << dependencies.joinIntoString (" ") << newLine << newLine;
  530. out << subTargetLines.toString() << newLine << newLine;
  531. }
  532. else
  533. {
  534. if (! getProject().getProjectType().isAudioPlugin())
  535. out << "all : " << target->getBuildProduct() << newLine << newLine;
  536. target->writeTargetLine (out, useLinuxPackages);
  537. }
  538. }
  539. }
  540. }
  541. void writeConfig (OutputStream& out, const MakeBuildConfiguration& config) const
  542. {
  543. String buildDirName ("build");
  544. auto intermediatesDirName = buildDirName + "/intermediate/" + config.getName();
  545. auto outputDir = buildDirName;
  546. if (config.getTargetBinaryRelativePathString().isNotEmpty())
  547. {
  548. RelativePath binaryPath (config.getTargetBinaryRelativePathString(), RelativePath::projectFolder);
  549. outputDir = binaryPath.rebased (projectFolder, getTargetFolder(), RelativePath::buildTargetFolder).toUnixStyle();
  550. }
  551. out << "ifeq ($(CONFIG)," << escapeSpaces (config.getName()) << ")" << newLine;
  552. out << " JUCE_BINDIR := " << escapeSpaces (buildDirName) << newLine
  553. << " JUCE_LIBDIR := " << escapeSpaces (buildDirName) << newLine
  554. << " JUCE_OBJDIR := " << escapeSpaces (intermediatesDirName) << newLine
  555. << " JUCE_OUTDIR := " << escapeSpaces (outputDir) << newLine
  556. << newLine
  557. << " ifeq ($(TARGET_ARCH),)" << newLine
  558. << " TARGET_ARCH := " << getArchFlags (config) << newLine
  559. << " endif" << newLine
  560. << newLine;
  561. writeCppFlags (out, config);
  562. for (auto target : targets)
  563. {
  564. auto lines = target->getTargetSettings (config);
  565. if (lines.size() > 0)
  566. out << " " << lines.joinIntoString ("\n ") << newLine;
  567. out << newLine;
  568. }
  569. out << " JUCE_CFLAGS += $(JUCE_CPPFLAGS) $(TARGET_ARCH)";
  570. auto cflags = getCFlags (config).joinIntoString (" ");
  571. if (cflags.isNotEmpty())
  572. out << " " << cflags;
  573. out << " $(CFLAGS)" << newLine;
  574. out << " JUCE_CXXFLAGS += $(JUCE_CFLAGS)";
  575. auto cxxflags = getCXXFlags().joinIntoString (" ");
  576. if (cxxflags.isNotEmpty())
  577. out << " " << cxxflags;
  578. out << " $(CXXFLAGS)" << newLine;
  579. writeLinkerFlags (out, config);
  580. out << newLine;
  581. out << " CLEANCMD = rm -rf $(JUCE_OUTDIR)/$(TARGET) $(JUCE_OBJDIR)" << newLine
  582. << "endif" << newLine
  583. << newLine;
  584. }
  585. void writeIncludeLines (OutputStream& out) const
  586. {
  587. auto n = targets.size();
  588. for (int i = 0; i < n; ++i)
  589. {
  590. if (auto* target = targets.getUnchecked (i))
  591. {
  592. if (target->type == ProjectType::Target::AggregateTarget)
  593. continue;
  594. out << "-include $(OBJECTS_" << target->getTargetVarName()
  595. << ":%.o=%.d)" << newLine;
  596. }
  597. }
  598. }
  599. void writeMakefile (OutputStream& out) const
  600. {
  601. out << "# Automatically generated makefile, created by the Projucer" << newLine
  602. << "# Don't edit this file! Your changes will be overwritten when you re-save the Projucer project!" << newLine
  603. << newLine;
  604. out << "# build with \"V=1\" for verbose builds" << newLine
  605. << "ifeq ($(V), 1)" << newLine
  606. << "V_AT =" << newLine
  607. << "else" << newLine
  608. << "V_AT = @" << newLine
  609. << "endif" << newLine
  610. << newLine;
  611. out << "# (this disables dependency generation if multiple architectures are set)" << newLine
  612. << "DEPFLAGS := $(if $(word 2, $(TARGET_ARCH)), , -MMD)" << newLine
  613. << newLine;
  614. out << "ifndef STRIP" << newLine
  615. << " STRIP=strip" << newLine
  616. << "endif" << newLine
  617. << newLine;
  618. out << "ifndef AR" << newLine
  619. << " AR=ar" << newLine
  620. << "endif" << newLine
  621. << newLine;
  622. out << "ifndef CONFIG" << newLine
  623. << " CONFIG=" << escapeSpaces (getConfiguration(0)->getName()) << newLine
  624. << "endif" << newLine
  625. << newLine;
  626. out << "JUCE_ARCH_LABEL := $(shell uname -m)" << newLine
  627. << newLine;
  628. for (ConstConfigIterator config (*this); config.next();)
  629. writeConfig (out, dynamic_cast<const MakeBuildConfiguration&> (*config));
  630. for (auto target : targets)
  631. target->writeObjects (out);
  632. out << getPhonyTargetLine() << newLine << newLine;
  633. auto packages = getPackages();
  634. writeTargetLines (out, ! packages.isEmpty());
  635. for (auto target : targets)
  636. target->addFiles (out);
  637. if (! packages.isEmpty())
  638. {
  639. out << "check-pkg-config:" << newLine
  640. << "\t@command -v pkg-config >/dev/null 2>&1 || "
  641. "{ echo >&2 \"pkg-config not installed. Please, install it.\"; "
  642. "exit 1; }" << newLine
  643. << "\t@pkg-config --print-errors";
  644. for (auto& pkg : packages)
  645. out << " " << pkg;
  646. out << newLine << newLine;
  647. }
  648. out << "clean:" << newLine
  649. << "\t@echo Cleaning " << projectName << newLine
  650. << "\t$(V_AT)$(CLEANCMD)" << newLine
  651. << newLine;
  652. out << "strip:" << newLine
  653. << "\t@echo Stripping " << projectName << newLine
  654. << "\t-$(V_AT)$(STRIP) --strip-unneeded $(JUCE_OUTDIR)/$(TARGET)" << newLine
  655. << newLine;
  656. writeIncludeLines (out);
  657. }
  658. String getArchFlags (const BuildConfiguration& config) const
  659. {
  660. if (auto* makeConfig = dynamic_cast<const MakeBuildConfiguration*> (&config))
  661. return makeConfig->getArchitectureTypeString();
  662. return "-march=native";
  663. }
  664. String getObjectFileFor (const RelativePath& file) const
  665. {
  666. return file.getFileNameWithoutExtension()
  667. + "_" + String::toHexString (file.toUnixStyle().hashCode()) + ".o";
  668. }
  669. String getPhonyTargetLine() const
  670. {
  671. MemoryOutputStream phonyTargetLine;
  672. phonyTargetLine << ".PHONY: clean all";
  673. if (! getProject().getProjectType().isAudioPlugin())
  674. return phonyTargetLine.toString();
  675. for (auto target : targets)
  676. if (target->type != ProjectType::Target::SharedCodeTarget
  677. && target->type != ProjectType::Target::AggregateTarget)
  678. phonyTargetLine << " " << target->getPhonyName();
  679. return phonyTargetLine.toString();
  680. }
  681. friend class CLionProjectExporter;
  682. OwnedArray<MakefileTarget> targets;
  683. JUCE_DECLARE_NON_COPYABLE (MakefileProjectExporter)
  684. };