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.

904 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 UnityPlugIn:
  137. case DynamicLibrary: return ".so";
  138. case SharedCodeTarget:
  139. case StaticLibrary: return ".a";
  140. default: break;
  141. }
  142. return {};
  143. }
  144. String getTargetVarName() const
  145. {
  146. return String (getName()).toUpperCase().replaceCharacter (L' ', L'_');
  147. }
  148. void writeObjects (OutputStream& out) const
  149. {
  150. Array<RelativePath> targetFiles;
  151. for (int i = 0; i < owner.getAllGroups().size(); ++i)
  152. findAllFilesToCompile (owner.getAllGroups().getReference(i), targetFiles);
  153. out << "OBJECTS_" + getTargetVarName() + String (" := \\") << newLine;
  154. for (int i = 0; i < targetFiles.size(); ++i)
  155. out << " $(JUCE_OBJDIR)/" << escapeSpaces (owner.getObjectFileFor (targetFiles.getReference(i))) << " \\" << newLine;
  156. out << newLine;
  157. }
  158. void findAllFilesToCompile (const Project::Item& projectItem, Array<RelativePath>& results) const
  159. {
  160. if (projectItem.isGroup())
  161. {
  162. for (int i = 0; i < projectItem.getNumChildren(); ++i)
  163. findAllFilesToCompile (projectItem.getChild(i), results);
  164. }
  165. else
  166. {
  167. if (projectItem.shouldBeCompiled())
  168. {
  169. auto targetType = (owner.getProject().getProjectType().isAudioPlugin() ? type : SharedCodeTarget);
  170. auto f = projectItem.getFile();
  171. RelativePath relativePath (f, owner.getTargetFolder(), RelativePath::buildTargetFolder);
  172. if (owner.shouldFileBeCompiledByDefault (relativePath)
  173. && owner.getProject().getTargetTypeFromFilePath (f, true) == targetType)
  174. results.add (relativePath);
  175. }
  176. }
  177. }
  178. void addFiles (OutputStream& out)
  179. {
  180. Array<RelativePath> targetFiles;
  181. for (int i = 0; i < owner.getAllGroups().size(); ++i)
  182. findAllFilesToCompile (owner.getAllGroups().getReference(i), targetFiles);
  183. auto cppflagsVarName = "JUCE_CPPFLAGS_" + getTargetVarName();
  184. auto cflagsVarName = "JUCE_CFLAGS_" + getTargetVarName();
  185. for (int i = 0; i < targetFiles.size(); ++i)
  186. {
  187. jassert (targetFiles.getReference(i).getRoot() == RelativePath::buildTargetFolder);
  188. out << "$(JUCE_OBJDIR)/" << escapeSpaces (owner.getObjectFileFor (targetFiles.getReference(i)))
  189. << ": " << escapeSpaces (targetFiles.getReference(i).toUnixStyle()) << newLine
  190. << "\t-$(V_AT)mkdir -p $(JUCE_OBJDIR)" << newLine
  191. << "\t@echo \"Compiling " << targetFiles.getReference(i).getFileName() << "\"" << newLine
  192. << (targetFiles.getReference(i).hasFileExtension ("c;s;S") ? "\t$(V_AT)$(CC) $(JUCE_CFLAGS) "
  193. : "\t$(V_AT)$(CXX) $(JUCE_CXXFLAGS) ")
  194. << "$(" << cppflagsVarName << ") $(" << cflagsVarName << ") -o \"$@\" -c \"$<\""
  195. << newLine
  196. << newLine;
  197. }
  198. }
  199. String getBuildProduct() const
  200. {
  201. return "$(JUCE_OUTDIR)/$(JUCE_TARGET_" + getTargetVarName() + ")";
  202. }
  203. String getPhonyName() const
  204. {
  205. return String (getName()).upToFirstOccurrenceOf (" ", false, false);
  206. }
  207. void writeTargetLine (OutputStream& out, const bool useLinuxPackages)
  208. {
  209. jassert (type != AggregateTarget);
  210. out << getBuildProduct() << " : "
  211. << ((useLinuxPackages) ? "check-pkg-config " : "")
  212. << "$(OBJECTS_" << getTargetVarName() << ") $(RESOURCES)";
  213. if (type != SharedCodeTarget && owner.shouldBuildTargetType (SharedCodeTarget))
  214. out << " $(JUCE_OUTDIR)/$(JUCE_TARGET_SHARED_CODE)";
  215. out << newLine << "\t@echo Linking \"" << owner.projectName << " - " << getName() << "\"" << newLine
  216. << "\t-$(V_AT)mkdir -p $(JUCE_BINDIR)" << newLine
  217. << "\t-$(V_AT)mkdir -p $(JUCE_LIBDIR)" << newLine
  218. << "\t-$(V_AT)mkdir -p $(JUCE_OUTDIR)" << newLine;
  219. if (owner.projectType.isStaticLibrary() || type == SharedCodeTarget)
  220. out << "\t$(V_AT)$(AR) -rcs " << getBuildProduct()
  221. << " $(OBJECTS_" << getTargetVarName() << ")" << newLine;
  222. else
  223. {
  224. out << "\t$(V_AT)$(CXX) -o " << getBuildProduct()
  225. << " $(OBJECTS_" << getTargetVarName() << ") ";
  226. if (owner.shouldBuildTargetType (SharedCodeTarget))
  227. out << "$(JUCE_OUTDIR)/$(JUCE_TARGET_SHARED_CODE) ";
  228. out << "$(JUCE_LDFLAGS) ";
  229. if (getTargetFileType() == sharedLibraryOrDLL || getTargetFileType() == pluginBundle
  230. || type == GUIApp || type == StandalonePlugIn)
  231. out << "$(JUCE_LDFLAGS_" << getTargetVarName() << ") ";
  232. out << "$(RESOURCES) $(TARGET_ARCH)" << newLine;
  233. }
  234. out << newLine;
  235. }
  236. const MakefileProjectExporter& owner;
  237. };
  238. //==============================================================================
  239. static const char* getNameLinux() { return "Linux Makefile"; }
  240. static const char* getValueTreeTypeName() { return "LINUX_MAKE"; }
  241. String getExtraPkgConfigString() const { return extraPkgConfigValue.get(); }
  242. static MakefileProjectExporter* createForSettings (Project& project, const ValueTree& settings)
  243. {
  244. if (settings.hasType (getValueTreeTypeName()))
  245. return new MakefileProjectExporter (project, settings);
  246. return nullptr;
  247. }
  248. //==============================================================================
  249. MakefileProjectExporter (Project& p, const ValueTree& t)
  250. : ProjectExporter (p, t),
  251. extraPkgConfigValue (settings, Ids::linuxExtraPkgConfig, getUndoManager())
  252. {
  253. name = getNameLinux();
  254. targetLocationValue.setDefault (getDefaultBuildsRootFolder() + getTargetFolderForExporter (getValueTreeTypeName()));
  255. }
  256. //==============================================================================
  257. bool canLaunchProject() override { return false; }
  258. bool launchProject() override { return false; }
  259. bool usesMMFiles() const override { return false; }
  260. bool canCopeWithDuplicateFiles() override { return false; }
  261. bool supportsUserDefinedConfigurations() const override { return true; }
  262. bool isXcode() const override { return false; }
  263. bool isVisualStudio() const override { return false; }
  264. bool isCodeBlocks() const override { return false; }
  265. bool isMakefile() const override { return true; }
  266. bool isAndroidStudio() const override { return false; }
  267. bool isCLion() const override { return false; }
  268. bool isAndroid() const override { return false; }
  269. bool isWindows() const override { return false; }
  270. bool isLinux() const override { return true; }
  271. bool isOSX() const override { return false; }
  272. bool isiOS() const override { return false; }
  273. bool supportsTargetType (ProjectType::Target::Type type) const override
  274. {
  275. switch (type)
  276. {
  277. case ProjectType::Target::GUIApp:
  278. case ProjectType::Target::ConsoleApp:
  279. case ProjectType::Target::StaticLibrary:
  280. case ProjectType::Target::SharedCodeTarget:
  281. case ProjectType::Target::AggregateTarget:
  282. case ProjectType::Target::VSTPlugIn:
  283. case ProjectType::Target::StandalonePlugIn:
  284. case ProjectType::Target::DynamicLibrary:
  285. case ProjectType::Target::UnityPlugIn:
  286. return true;
  287. default:
  288. break;
  289. }
  290. return false;
  291. }
  292. void createExporterProperties (PropertyListBuilder& properties) override
  293. {
  294. properties.add (new TextPropertyComponent (extraPkgConfigValue, "pkg-config libraries", 8192, false),
  295. "Extra pkg-config libraries for you application. Each package should be space separated.");
  296. }
  297. //==============================================================================
  298. bool anyTargetIsSharedLibrary() const
  299. {
  300. for (auto* target : targets)
  301. {
  302. auto fileType = target->getTargetFileType();
  303. if (fileType == ProjectType::Target::sharedLibraryOrDLL
  304. || fileType == ProjectType::Target::pluginBundle)
  305. return true;
  306. }
  307. return false;
  308. }
  309. //==============================================================================
  310. void create (const OwnedArray<LibraryModule>&) const override
  311. {
  312. MemoryOutputStream mo;
  313. writeMakefile (mo);
  314. overwriteFileIfDifferentOrThrow (getTargetFolder().getChildFile ("Makefile"), mo);
  315. }
  316. //==============================================================================
  317. void addPlatformSpecificSettingsForProjectType (const ProjectType&) override
  318. {
  319. callForAllSupportedTargets ([this] (ProjectType::Target::Type targetType)
  320. {
  321. if (MakefileTarget* target = new MakefileTarget (targetType, *this))
  322. {
  323. if (targetType == ProjectType::Target::AggregateTarget)
  324. targets.insert (0, target);
  325. else
  326. targets.add (target);
  327. }
  328. });
  329. // If you hit this assert, you tried to generate a project for an exporter
  330. // that does not support any of your targets!
  331. jassert (targets.size() > 0);
  332. }
  333. //==============================================================================
  334. void initialiseDependencyPathValues() override {}
  335. private:
  336. ValueWithDefault extraPkgConfigValue;
  337. //==============================================================================
  338. StringPairArray getDefines (const BuildConfiguration& config) const
  339. {
  340. StringPairArray result;
  341. result.set ("LINUX", "1");
  342. if (config.isDebug())
  343. {
  344. result.set ("DEBUG", "1");
  345. result.set ("_DEBUG", "1");
  346. }
  347. else
  348. {
  349. result.set ("NDEBUG", "1");
  350. }
  351. result = mergePreprocessorDefs (result, getAllPreprocessorDefs (config, ProjectType::Target::unspecified));
  352. return result;
  353. }
  354. StringArray getPackages() const
  355. {
  356. StringArray packages;
  357. packages.addTokens (getExtraPkgConfigString(), " ", "\"'");
  358. packages.removeEmptyStrings();
  359. packages.addArray (linuxPackages);
  360. if (isWebBrowserComponentEnabled())
  361. {
  362. packages.add ("webkit2gtk-4.0");
  363. packages.add ("gtk+-x11-3.0");
  364. }
  365. packages.removeDuplicates (false);
  366. return packages;
  367. }
  368. String getPreprocessorPkgConfigFlags() const
  369. {
  370. auto packages = getPackages();
  371. if (packages.size() > 0)
  372. return "$(shell pkg-config --cflags " + packages.joinIntoString (" ") + ")";
  373. return {};
  374. }
  375. String getLinkerPkgConfigFlags() const
  376. {
  377. auto packages = getPackages();
  378. if (packages.size() > 0)
  379. return "$(shell pkg-config --libs " + packages.joinIntoString (" ") + ")";
  380. return {};
  381. }
  382. StringArray getCPreprocessorFlags (const BuildConfiguration&) const
  383. {
  384. StringArray result;
  385. if (linuxLibs.contains("pthread"))
  386. result.add ("-pthread");
  387. return result;
  388. }
  389. StringArray getCFlags (const BuildConfiguration& config) const
  390. {
  391. StringArray result;
  392. if (anyTargetIsSharedLibrary())
  393. result.add ("-fPIC");
  394. if (config.isDebug())
  395. {
  396. result.add ("-g");
  397. result.add ("-ggdb");
  398. }
  399. result.add ("-O" + config.getGCCOptimisationFlag());
  400. if (config.isLinkTimeOptimisationEnabled())
  401. result.add ("-flto");
  402. auto extra = replacePreprocessorTokens (config, getExtraCompilerFlagsString()).trim();
  403. if (extra.isNotEmpty())
  404. result.add (extra);
  405. return result;
  406. }
  407. StringArray getCXXFlags() const
  408. {
  409. StringArray result;
  410. auto cppStandard = project.getCppStandardString();
  411. if (cppStandard == "latest")
  412. cppStandard = "17";
  413. cppStandard = "-std=" + String (shouldUseGNUExtensions() ? "gnu++" : "c++") + cppStandard;
  414. result.add (cppStandard);
  415. return result;
  416. }
  417. StringArray getHeaderSearchPaths (const BuildConfiguration& config) const
  418. {
  419. StringArray searchPaths (extraSearchPaths);
  420. searchPaths.addArray (config.getHeaderSearchPaths());
  421. searchPaths = getCleanedStringArray (searchPaths);
  422. StringArray result;
  423. for (auto& path : searchPaths)
  424. result.add (FileHelpers::unixStylePath (replacePreprocessorTokens (config, path)));
  425. return result;
  426. }
  427. StringArray getLibraryNames (const BuildConfiguration& config) const
  428. {
  429. StringArray result (linuxLibs);
  430. auto libraries = StringArray::fromTokens (getExternalLibrariesString(), ";", "\"'");
  431. libraries.removeEmptyStrings();
  432. for (auto& lib : libraries)
  433. result.add (replacePreprocessorTokens (config, lib).trim());
  434. return result;
  435. }
  436. StringArray getLibrarySearchPaths (const BuildConfiguration& config) const
  437. {
  438. auto result = getSearchPathsFromString (config.getLibrarySearchPathString());
  439. for (auto path : moduleLibSearchPaths)
  440. result.add (path + "/" + config.getModuleLibraryArchName());
  441. return result;
  442. }
  443. StringArray getLinkerFlags (const BuildConfiguration& config) const
  444. {
  445. auto result = makefileExtraLinkerFlags;
  446. if (! config.isDebug())
  447. result.add ("-fvisibility=hidden");
  448. if (config.isLinkTimeOptimisationEnabled())
  449. result.add ("-flto");
  450. auto extraFlags = getExtraLinkerFlagsString().trim();
  451. if (extraFlags.isNotEmpty())
  452. result.add (replacePreprocessorTokens (config, extraFlags));
  453. return result;
  454. }
  455. bool isWebBrowserComponentEnabled() const
  456. {
  457. static String guiExtrasModule ("juce_gui_extra");
  458. return (project.getModules().isModuleEnabled (guiExtrasModule)
  459. && project.isConfigFlagEnabled ("JUCE_WEB_BROWSER", true));
  460. }
  461. //==============================================================================
  462. void writeDefineFlags (OutputStream& out, const MakeBuildConfiguration& config) const
  463. {
  464. out << createGCCPreprocessorFlags (mergePreprocessorDefs (getDefines (config), getAllPreprocessorDefs (config, ProjectType::Target::unspecified)));
  465. }
  466. void writePkgConfigFlags (OutputStream& out) const
  467. {
  468. auto flags = getPreprocessorPkgConfigFlags();
  469. if (flags.isNotEmpty())
  470. out << " " << flags;
  471. }
  472. void writeCPreprocessorFlags (OutputStream& out, const BuildConfiguration& config) const
  473. {
  474. auto flags = getCPreprocessorFlags (config);
  475. if (! flags.isEmpty())
  476. out << " " << flags.joinIntoString (" ");
  477. }
  478. void writeHeaderPathFlags (OutputStream& out, const BuildConfiguration& config) const
  479. {
  480. for (auto& path : getHeaderSearchPaths (config))
  481. out << " -I" << escapeSpaces (path).replace ("~", "$(HOME)");
  482. }
  483. void writeCppFlags (OutputStream& out, const MakeBuildConfiguration& config) const
  484. {
  485. out << " JUCE_CPPFLAGS := $(DEPFLAGS)";
  486. writeDefineFlags (out, config);
  487. writePkgConfigFlags (out);
  488. writeCPreprocessorFlags (out, config);
  489. writeHeaderPathFlags (out, config);
  490. out << " $(CPPFLAGS)" << newLine;
  491. }
  492. void writeLinkerFlags (OutputStream& out, const BuildConfiguration& config) const
  493. {
  494. out << " JUCE_LDFLAGS += $(TARGET_ARCH) -L$(JUCE_BINDIR) -L$(JUCE_LIBDIR)";
  495. for (auto path : getLibrarySearchPaths (config))
  496. out << " -L" << escapeSpaces (path).replace ("~", "$(HOME)");
  497. auto pkgConfigFlags = getLinkerPkgConfigFlags();
  498. if (pkgConfigFlags.isNotEmpty())
  499. out << " " << getLinkerPkgConfigFlags();
  500. auto linkerFlags = getLinkerFlags (config).joinIntoString (" ");
  501. if (linkerFlags.isNotEmpty())
  502. out << " " << linkerFlags;
  503. for (auto& libName : getLibraryNames (config))
  504. out << " -l" << libName;
  505. out << " $(LDFLAGS)" << newLine;
  506. }
  507. void writeTargetLines (OutputStream& out, const bool useLinuxPackages) const
  508. {
  509. auto n = targets.size();
  510. for (int i = 0; i < n; ++i)
  511. {
  512. if (auto* target = targets.getUnchecked (i))
  513. {
  514. if (target->type == ProjectType::Target::AggregateTarget)
  515. {
  516. StringArray dependencies;
  517. MemoryOutputStream subTargetLines;
  518. for (int j = 0; j < n; ++j)
  519. {
  520. if (i == j) continue;
  521. if (auto* dependency = targets.getUnchecked (j))
  522. {
  523. if (dependency->type != ProjectType::Target::SharedCodeTarget)
  524. {
  525. auto phonyName = dependency->getPhonyName();
  526. subTargetLines << phonyName << " : " << dependency->getBuildProduct() << newLine;
  527. dependencies.add (phonyName);
  528. }
  529. }
  530. }
  531. out << "all : " << dependencies.joinIntoString (" ") << newLine << newLine;
  532. out << subTargetLines.toString() << newLine << newLine;
  533. }
  534. else
  535. {
  536. if (! getProject().getProjectType().isAudioPlugin())
  537. out << "all : " << target->getBuildProduct() << newLine << newLine;
  538. target->writeTargetLine (out, useLinuxPackages);
  539. }
  540. }
  541. }
  542. }
  543. void writeConfig (OutputStream& out, const MakeBuildConfiguration& config) const
  544. {
  545. String buildDirName ("build");
  546. auto intermediatesDirName = buildDirName + "/intermediate/" + config.getName();
  547. auto outputDir = buildDirName;
  548. if (config.getTargetBinaryRelativePathString().isNotEmpty())
  549. {
  550. RelativePath binaryPath (config.getTargetBinaryRelativePathString(), RelativePath::projectFolder);
  551. outputDir = binaryPath.rebased (projectFolder, getTargetFolder(), RelativePath::buildTargetFolder).toUnixStyle();
  552. }
  553. out << "ifeq ($(CONFIG)," << escapeSpaces (config.getName()) << ")" << newLine;
  554. out << " JUCE_BINDIR := " << escapeSpaces (buildDirName) << newLine
  555. << " JUCE_LIBDIR := " << escapeSpaces (buildDirName) << newLine
  556. << " JUCE_OBJDIR := " << escapeSpaces (intermediatesDirName) << newLine
  557. << " JUCE_OUTDIR := " << escapeSpaces (outputDir) << newLine
  558. << newLine
  559. << " ifeq ($(TARGET_ARCH),)" << newLine
  560. << " TARGET_ARCH := " << getArchFlags (config) << newLine
  561. << " endif" << newLine
  562. << newLine;
  563. writeCppFlags (out, config);
  564. for (auto target : targets)
  565. {
  566. auto lines = target->getTargetSettings (config);
  567. if (lines.size() > 0)
  568. out << " " << lines.joinIntoString ("\n ") << newLine;
  569. out << newLine;
  570. }
  571. out << " JUCE_CFLAGS += $(JUCE_CPPFLAGS) $(TARGET_ARCH)";
  572. auto cflags = getCFlags (config).joinIntoString (" ");
  573. if (cflags.isNotEmpty())
  574. out << " " << cflags;
  575. out << " $(CFLAGS)" << newLine;
  576. out << " JUCE_CXXFLAGS += $(JUCE_CFLAGS)";
  577. auto cxxflags = getCXXFlags().joinIntoString (" ");
  578. if (cxxflags.isNotEmpty())
  579. out << " " << cxxflags;
  580. out << " $(CXXFLAGS)" << newLine;
  581. writeLinkerFlags (out, config);
  582. out << newLine;
  583. out << " CLEANCMD = rm -rf $(JUCE_OUTDIR)/$(TARGET) $(JUCE_OBJDIR)" << newLine
  584. << "endif" << newLine
  585. << newLine;
  586. }
  587. void writeIncludeLines (OutputStream& out) const
  588. {
  589. auto n = targets.size();
  590. for (int i = 0; i < n; ++i)
  591. {
  592. if (auto* target = targets.getUnchecked (i))
  593. {
  594. if (target->type == ProjectType::Target::AggregateTarget)
  595. continue;
  596. out << "-include $(OBJECTS_" << target->getTargetVarName()
  597. << ":%.o=%.d)" << newLine;
  598. }
  599. }
  600. }
  601. void writeMakefile (OutputStream& out) const
  602. {
  603. out << "# Automatically generated makefile, created by the Projucer" << newLine
  604. << "# Don't edit this file! Your changes will be overwritten when you re-save the Projucer project!" << newLine
  605. << newLine;
  606. out << "# build with \"V=1\" for verbose builds" << newLine
  607. << "ifeq ($(V), 1)" << newLine
  608. << "V_AT =" << newLine
  609. << "else" << newLine
  610. << "V_AT = @" << newLine
  611. << "endif" << newLine
  612. << newLine;
  613. out << "# (this disables dependency generation if multiple architectures are set)" << newLine
  614. << "DEPFLAGS := $(if $(word 2, $(TARGET_ARCH)), , -MMD)" << newLine
  615. << newLine;
  616. out << "ifndef STRIP" << newLine
  617. << " STRIP=strip" << newLine
  618. << "endif" << newLine
  619. << newLine;
  620. out << "ifndef AR" << newLine
  621. << " AR=ar" << newLine
  622. << "endif" << newLine
  623. << newLine;
  624. out << "ifndef CONFIG" << newLine
  625. << " CONFIG=" << escapeSpaces (getConfiguration(0)->getName()) << newLine
  626. << "endif" << newLine
  627. << newLine;
  628. out << "JUCE_ARCH_LABEL := $(shell uname -m)" << newLine
  629. << newLine;
  630. for (ConstConfigIterator config (*this); config.next();)
  631. writeConfig (out, dynamic_cast<const MakeBuildConfiguration&> (*config));
  632. for (auto target : targets)
  633. target->writeObjects (out);
  634. out << getPhonyTargetLine() << newLine << newLine;
  635. auto packages = getPackages();
  636. writeTargetLines (out, ! packages.isEmpty());
  637. for (auto target : targets)
  638. target->addFiles (out);
  639. if (! packages.isEmpty())
  640. {
  641. out << "check-pkg-config:" << newLine
  642. << "\t@command -v pkg-config >/dev/null 2>&1 || "
  643. "{ echo >&2 \"pkg-config not installed. Please, install it.\"; "
  644. "exit 1; }" << newLine
  645. << "\t@pkg-config --print-errors";
  646. for (auto& pkg : packages)
  647. out << " " << pkg;
  648. out << newLine << newLine;
  649. }
  650. out << "clean:" << newLine
  651. << "\t@echo Cleaning " << projectName << newLine
  652. << "\t$(V_AT)$(CLEANCMD)" << newLine
  653. << newLine;
  654. out << "strip:" << newLine
  655. << "\t@echo Stripping " << projectName << newLine
  656. << "\t-$(V_AT)$(STRIP) --strip-unneeded $(JUCE_OUTDIR)/$(TARGET)" << newLine
  657. << newLine;
  658. writeIncludeLines (out);
  659. }
  660. String getArchFlags (const BuildConfiguration& config) const
  661. {
  662. if (auto* makeConfig = dynamic_cast<const MakeBuildConfiguration*> (&config))
  663. return makeConfig->getArchitectureTypeString();
  664. return "-march=native";
  665. }
  666. String getObjectFileFor (const RelativePath& file) const
  667. {
  668. return file.getFileNameWithoutExtension()
  669. + "_" + String::toHexString (file.toUnixStyle().hashCode()) + ".o";
  670. }
  671. String getPhonyTargetLine() const
  672. {
  673. MemoryOutputStream phonyTargetLine;
  674. phonyTargetLine << ".PHONY: clean all";
  675. if (! getProject().getProjectType().isAudioPlugin())
  676. return phonyTargetLine.toString();
  677. for (auto target : targets)
  678. if (target->type != ProjectType::Target::SharedCodeTarget
  679. && target->type != ProjectType::Target::AggregateTarget)
  680. phonyTargetLine << " " << target->getPhonyName();
  681. return phonyTargetLine.toString();
  682. }
  683. friend class CLionProjectExporter;
  684. OwnedArray<MakefileTarget> targets;
  685. JUCE_DECLARE_NON_COPYABLE (MakefileProjectExporter)
  686. };