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.

913 lines
33KB

  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. 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. // don't add libcurl if curl symbols are loaded at runtime
  364. if (! isLoadCurlSymbolsLazilyEnabled())
  365. packages.add ("libcurl");
  366. packages.removeDuplicates (false);
  367. return packages;
  368. }
  369. String getPreprocessorPkgConfigFlags() const
  370. {
  371. auto packages = getPackages();
  372. if (packages.size() > 0)
  373. return "$(shell pkg-config --cflags " + packages.joinIntoString (" ") + ")";
  374. return {};
  375. }
  376. String getLinkerPkgConfigFlags() const
  377. {
  378. auto packages = getPackages();
  379. if (packages.size() > 0)
  380. return "$(shell pkg-config --libs " + packages.joinIntoString (" ") + ")";
  381. return {};
  382. }
  383. StringArray getCPreprocessorFlags (const BuildConfiguration&) const
  384. {
  385. StringArray result;
  386. if (linuxLibs.contains("pthread"))
  387. result.add ("-pthread");
  388. return result;
  389. }
  390. StringArray getCFlags (const BuildConfiguration& config) const
  391. {
  392. StringArray result;
  393. if (anyTargetIsSharedLibrary())
  394. result.add ("-fPIC");
  395. if (config.isDebug())
  396. {
  397. result.add ("-g");
  398. result.add ("-ggdb");
  399. }
  400. result.add ("-O" + config.getGCCOptimisationFlag());
  401. if (config.isLinkTimeOptimisationEnabled())
  402. result.add ("-flto");
  403. auto extra = replacePreprocessorTokens (config, getExtraCompilerFlagsString()).trim();
  404. if (extra.isNotEmpty())
  405. result.add (extra);
  406. return result;
  407. }
  408. StringArray getCXXFlags() const
  409. {
  410. StringArray result;
  411. auto cppStandard = project.getCppStandardString();
  412. if (cppStandard == "latest")
  413. cppStandard = "17";
  414. cppStandard = "-std=" + String (shouldUseGNUExtensions() ? "gnu++" : "c++") + cppStandard;
  415. result.add (cppStandard);
  416. return result;
  417. }
  418. StringArray getHeaderSearchPaths (const BuildConfiguration& config) const
  419. {
  420. StringArray searchPaths (extraSearchPaths);
  421. searchPaths.addArray (config.getHeaderSearchPaths());
  422. searchPaths = getCleanedStringArray (searchPaths);
  423. StringArray result;
  424. for (auto& path : searchPaths)
  425. result.add (FileHelpers::unixStylePath (replacePreprocessorTokens (config, path)));
  426. return result;
  427. }
  428. StringArray getLibraryNames (const BuildConfiguration& config) const
  429. {
  430. StringArray result (linuxLibs);
  431. auto libraries = StringArray::fromTokens (getExternalLibrariesString(), ";", "\"'");
  432. libraries.removeEmptyStrings();
  433. for (auto& lib : libraries)
  434. result.add (replacePreprocessorTokens (config, lib).trim());
  435. return result;
  436. }
  437. StringArray getLibrarySearchPaths (const BuildConfiguration& config) const
  438. {
  439. auto result = getSearchPathsFromString (config.getLibrarySearchPathString());
  440. for (auto path : moduleLibSearchPaths)
  441. result.add (path + "/" + config.getModuleLibraryArchName());
  442. return result;
  443. }
  444. StringArray getLinkerFlags (const BuildConfiguration& config) const
  445. {
  446. auto result = makefileExtraLinkerFlags;
  447. if (! config.isDebug())
  448. result.add ("-fvisibility=hidden");
  449. if (config.isLinkTimeOptimisationEnabled())
  450. result.add ("-flto");
  451. auto extraFlags = getExtraLinkerFlagsString().trim();
  452. if (extraFlags.isNotEmpty())
  453. result.add (replacePreprocessorTokens (config, extraFlags));
  454. return result;
  455. }
  456. bool isWebBrowserComponentEnabled() const
  457. {
  458. static String guiExtrasModule ("juce_gui_extra");
  459. return (project.getEnabledModules().isModuleEnabled (guiExtrasModule)
  460. && project.isConfigFlagEnabled ("JUCE_WEB_BROWSER", true));
  461. }
  462. bool isLoadCurlSymbolsLazilyEnabled() const
  463. {
  464. static String juceCoreModule ("juce_core");
  465. return (project.getEnabledModules().isModuleEnabled (juceCoreModule)
  466. && project.isConfigFlagEnabled ("JUCE_LOAD_CURL_SYMBOLS_LAZILY", false));
  467. }
  468. //==============================================================================
  469. void writeDefineFlags (OutputStream& out, const MakeBuildConfiguration& config) const
  470. {
  471. out << createGCCPreprocessorFlags (mergePreprocessorDefs (getDefines (config), getAllPreprocessorDefs (config, ProjectType::Target::unspecified)));
  472. }
  473. void writePkgConfigFlags (OutputStream& out) const
  474. {
  475. auto flags = getPreprocessorPkgConfigFlags();
  476. if (flags.isNotEmpty())
  477. out << " " << flags;
  478. }
  479. void writeCPreprocessorFlags (OutputStream& out, const BuildConfiguration& config) const
  480. {
  481. auto flags = getCPreprocessorFlags (config);
  482. if (! flags.isEmpty())
  483. out << " " << flags.joinIntoString (" ");
  484. }
  485. void writeHeaderPathFlags (OutputStream& out, const BuildConfiguration& config) const
  486. {
  487. for (auto& path : getHeaderSearchPaths (config))
  488. out << " -I" << escapeSpaces (path).replace ("~", "$(HOME)");
  489. }
  490. void writeCppFlags (OutputStream& out, const MakeBuildConfiguration& config) const
  491. {
  492. out << " JUCE_CPPFLAGS := $(DEPFLAGS)";
  493. writeDefineFlags (out, config);
  494. writePkgConfigFlags (out);
  495. writeCPreprocessorFlags (out, config);
  496. writeHeaderPathFlags (out, config);
  497. out << " $(CPPFLAGS)" << newLine;
  498. }
  499. void writeLinkerFlags (OutputStream& out, const BuildConfiguration& config) const
  500. {
  501. out << " JUCE_LDFLAGS += $(TARGET_ARCH) -L$(JUCE_BINDIR) -L$(JUCE_LIBDIR)";
  502. for (auto path : getLibrarySearchPaths (config))
  503. out << " -L" << escapeSpaces (path).replace ("~", "$(HOME)");
  504. auto pkgConfigFlags = getLinkerPkgConfigFlags();
  505. if (pkgConfigFlags.isNotEmpty())
  506. out << " " << getLinkerPkgConfigFlags();
  507. auto linkerFlags = getLinkerFlags (config).joinIntoString (" ");
  508. if (linkerFlags.isNotEmpty())
  509. out << " " << linkerFlags;
  510. for (auto& libName : getLibraryNames (config))
  511. out << " -l" << libName;
  512. out << " $(LDFLAGS)" << newLine;
  513. }
  514. void writeTargetLines (OutputStream& out, const bool useLinuxPackages) const
  515. {
  516. auto n = targets.size();
  517. for (int i = 0; i < n; ++i)
  518. {
  519. if (auto* target = targets.getUnchecked (i))
  520. {
  521. if (target->type == ProjectType::Target::AggregateTarget)
  522. {
  523. StringArray dependencies;
  524. MemoryOutputStream subTargetLines;
  525. for (int j = 0; j < n; ++j)
  526. {
  527. if (i == j) continue;
  528. if (auto* dependency = targets.getUnchecked (j))
  529. {
  530. if (dependency->type != ProjectType::Target::SharedCodeTarget)
  531. {
  532. auto phonyName = dependency->getPhonyName();
  533. subTargetLines << phonyName << " : " << dependency->getBuildProduct() << newLine;
  534. dependencies.add (phonyName);
  535. }
  536. }
  537. }
  538. out << "all : " << dependencies.joinIntoString (" ") << newLine << newLine;
  539. out << subTargetLines.toString() << newLine << newLine;
  540. }
  541. else
  542. {
  543. if (! getProject().getProjectType().isAudioPlugin())
  544. out << "all : " << target->getBuildProduct() << newLine << newLine;
  545. target->writeTargetLine (out, useLinuxPackages);
  546. }
  547. }
  548. }
  549. }
  550. void writeConfig (OutputStream& out, const MakeBuildConfiguration& config) const
  551. {
  552. String buildDirName ("build");
  553. auto intermediatesDirName = buildDirName + "/intermediate/" + config.getName();
  554. auto outputDir = buildDirName;
  555. if (config.getTargetBinaryRelativePathString().isNotEmpty())
  556. {
  557. RelativePath binaryPath (config.getTargetBinaryRelativePathString(), RelativePath::projectFolder);
  558. outputDir = binaryPath.rebased (projectFolder, getTargetFolder(), RelativePath::buildTargetFolder).toUnixStyle();
  559. }
  560. out << "ifeq ($(CONFIG)," << escapeSpaces (config.getName()) << ")" << newLine;
  561. out << " JUCE_BINDIR := " << escapeSpaces (buildDirName) << newLine
  562. << " JUCE_LIBDIR := " << escapeSpaces (buildDirName) << newLine
  563. << " JUCE_OBJDIR := " << escapeSpaces (intermediatesDirName) << newLine
  564. << " JUCE_OUTDIR := " << escapeSpaces (outputDir) << newLine
  565. << newLine
  566. << " ifeq ($(TARGET_ARCH),)" << newLine
  567. << " TARGET_ARCH := " << getArchFlags (config) << newLine
  568. << " endif" << newLine
  569. << newLine;
  570. writeCppFlags (out, config);
  571. for (auto target : targets)
  572. {
  573. auto lines = target->getTargetSettings (config);
  574. if (lines.size() > 0)
  575. out << " " << lines.joinIntoString ("\n ") << newLine;
  576. out << newLine;
  577. }
  578. out << " JUCE_CFLAGS += $(JUCE_CPPFLAGS) $(TARGET_ARCH)";
  579. auto cflags = getCFlags (config).joinIntoString (" ");
  580. if (cflags.isNotEmpty())
  581. out << " " << cflags;
  582. out << " $(CFLAGS)" << newLine;
  583. out << " JUCE_CXXFLAGS += $(JUCE_CFLAGS)";
  584. auto cxxflags = getCXXFlags().joinIntoString (" ");
  585. if (cxxflags.isNotEmpty())
  586. out << " " << cxxflags;
  587. out << " $(CXXFLAGS)" << newLine;
  588. writeLinkerFlags (out, config);
  589. out << newLine;
  590. out << " CLEANCMD = rm -rf $(JUCE_OUTDIR)/$(TARGET) $(JUCE_OBJDIR)" << newLine
  591. << "endif" << newLine
  592. << newLine;
  593. }
  594. void writeIncludeLines (OutputStream& out) const
  595. {
  596. auto n = targets.size();
  597. for (int i = 0; i < n; ++i)
  598. {
  599. if (auto* target = targets.getUnchecked (i))
  600. {
  601. if (target->type == ProjectType::Target::AggregateTarget)
  602. continue;
  603. out << "-include $(OBJECTS_" << target->getTargetVarName()
  604. << ":%.o=%.d)" << newLine;
  605. }
  606. }
  607. }
  608. void writeMakefile (OutputStream& out) const
  609. {
  610. out << "# Automatically generated makefile, created by the Projucer" << newLine
  611. << "# Don't edit this file! Your changes will be overwritten when you re-save the Projucer project!" << newLine
  612. << newLine;
  613. out << "# build with \"V=1\" for verbose builds" << newLine
  614. << "ifeq ($(V), 1)" << newLine
  615. << "V_AT =" << newLine
  616. << "else" << newLine
  617. << "V_AT = @" << newLine
  618. << "endif" << newLine
  619. << newLine;
  620. out << "# (this disables dependency generation if multiple architectures are set)" << newLine
  621. << "DEPFLAGS := $(if $(word 2, $(TARGET_ARCH)), , -MMD)" << newLine
  622. << newLine;
  623. out << "ifndef STRIP" << newLine
  624. << " STRIP=strip" << newLine
  625. << "endif" << newLine
  626. << newLine;
  627. out << "ifndef AR" << newLine
  628. << " AR=ar" << newLine
  629. << "endif" << newLine
  630. << newLine;
  631. out << "ifndef CONFIG" << newLine
  632. << " CONFIG=" << escapeSpaces (getConfiguration(0)->getName()) << newLine
  633. << "endif" << newLine
  634. << newLine;
  635. out << "JUCE_ARCH_LABEL := $(shell uname -m)" << newLine
  636. << newLine;
  637. for (ConstConfigIterator config (*this); config.next();)
  638. writeConfig (out, dynamic_cast<const MakeBuildConfiguration&> (*config));
  639. for (auto target : targets)
  640. target->writeObjects (out);
  641. out << getPhonyTargetLine() << newLine << newLine;
  642. auto packages = getPackages();
  643. writeTargetLines (out, ! packages.isEmpty());
  644. for (auto target : targets)
  645. target->addFiles (out);
  646. if (! packages.isEmpty())
  647. {
  648. out << "check-pkg-config:" << newLine
  649. << "\t@command -v pkg-config >/dev/null 2>&1 || "
  650. "{ echo >&2 \"pkg-config not installed. Please, install it.\"; "
  651. "exit 1; }" << newLine
  652. << "\t@pkg-config --print-errors";
  653. for (auto& pkg : packages)
  654. out << " " << pkg;
  655. out << newLine << newLine;
  656. }
  657. out << "clean:" << newLine
  658. << "\t@echo Cleaning " << projectName << newLine
  659. << "\t$(V_AT)$(CLEANCMD)" << newLine
  660. << newLine;
  661. out << "strip:" << newLine
  662. << "\t@echo Stripping " << projectName << newLine
  663. << "\t-$(V_AT)$(STRIP) --strip-unneeded $(JUCE_OUTDIR)/$(TARGET)" << newLine
  664. << newLine;
  665. writeIncludeLines (out);
  666. }
  667. String getArchFlags (const BuildConfiguration& config) const
  668. {
  669. if (auto* makeConfig = dynamic_cast<const MakeBuildConfiguration*> (&config))
  670. return makeConfig->getArchitectureTypeString();
  671. return "-march=native";
  672. }
  673. String getObjectFileFor (const RelativePath& file) const
  674. {
  675. return file.getFileNameWithoutExtension()
  676. + "_" + String::toHexString (file.toUnixStyle().hashCode()) + ".o";
  677. }
  678. String getPhonyTargetLine() const
  679. {
  680. MemoryOutputStream phonyTargetLine;
  681. phonyTargetLine << ".PHONY: clean all";
  682. if (! getProject().getProjectType().isAudioPlugin())
  683. return phonyTargetLine.toString();
  684. for (auto target : targets)
  685. if (target->type != ProjectType::Target::SharedCodeTarget
  686. && target->type != ProjectType::Target::AggregateTarget)
  687. phonyTargetLine << " " << target->getPhonyName();
  688. return phonyTargetLine.toString();
  689. }
  690. friend class CLionProjectExporter;
  691. OwnedArray<MakefileTarget> targets;
  692. JUCE_DECLARE_NON_COPYABLE (MakefileProjectExporter)
  693. };