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.

908 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 DynamicLibrary: return ".so";
  137. case VST3PlugIn: return ".vst3";
  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() + "LinuxMakefile");
  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. return true;
  286. default:
  287. break;
  288. }
  289. return false;
  290. }
  291. void createExporterProperties (PropertyListBuilder& properties) override
  292. {
  293. properties.add (new TextPropertyComponent (extraPkgConfigValue, "pkg-config libraries", 8192, false),
  294. "Extra pkg-config libraries for you application. Each package should be space separated.");
  295. }
  296. //==============================================================================
  297. bool anyTargetIsSharedLibrary() const
  298. {
  299. for (auto* target : targets)
  300. {
  301. auto fileType = target->getTargetFileType();
  302. if (fileType == ProjectType::Target::sharedLibraryOrDLL
  303. || fileType == ProjectType::Target::pluginBundle)
  304. return true;
  305. }
  306. return false;
  307. }
  308. //==============================================================================
  309. void create (const OwnedArray<LibraryModule>&) const override
  310. {
  311. MemoryOutputStream mo;
  312. writeMakefile (mo);
  313. overwriteFileIfDifferentOrThrow (getTargetFolder().getChildFile ("Makefile"), mo);
  314. }
  315. //==============================================================================
  316. void addPlatformSpecificSettingsForProjectType (const ProjectType&) override
  317. {
  318. callForAllSupportedTargets ([this] (ProjectType::Target::Type targetType)
  319. {
  320. if (MakefileTarget* target = new MakefileTarget (targetType, *this))
  321. {
  322. if (targetType == ProjectType::Target::AggregateTarget)
  323. targets.insert (0, target);
  324. else
  325. targets.add (target);
  326. }
  327. });
  328. // If you hit this assert, you tried to generate a project for an exporter
  329. // that does not support any of your targets!
  330. jassert (targets.size() > 0);
  331. }
  332. //==============================================================================
  333. void initialiseDependencyPathValues() override
  334. {
  335. vst3Path.referTo (Value (new DependencyPathValueSource (getSetting (Ids::vst3Folder),
  336. Ids::vst3Path,
  337. TargetOS::linux)));
  338. }
  339. private:
  340. ValueWithDefault extraPkgConfigValue;
  341. //==============================================================================
  342. StringPairArray getDefines (const BuildConfiguration& config) const
  343. {
  344. StringPairArray result;
  345. result.set ("LINUX", "1");
  346. if (config.isDebug())
  347. {
  348. result.set ("DEBUG", "1");
  349. result.set ("_DEBUG", "1");
  350. }
  351. else
  352. {
  353. result.set ("NDEBUG", "1");
  354. }
  355. result = mergePreprocessorDefs (result, getAllPreprocessorDefs (config, ProjectType::Target::unspecified));
  356. return result;
  357. }
  358. StringArray getPackages() const
  359. {
  360. StringArray packages;
  361. packages.addTokens (getExtraPkgConfigString(), " ", "\"'");
  362. packages.removeEmptyStrings();
  363. packages.addArray (linuxPackages);
  364. if (isWebBrowserComponentEnabled())
  365. {
  366. packages.add ("webkit2gtk-4.0");
  367. packages.add ("gtk+-x11-3.0");
  368. }
  369. packages.removeDuplicates (false);
  370. return packages;
  371. }
  372. String getPreprocessorPkgConfigFlags() const
  373. {
  374. auto packages = getPackages();
  375. if (packages.size() > 0)
  376. return "$(shell pkg-config --cflags " + packages.joinIntoString (" ") + ")";
  377. return {};
  378. }
  379. String getLinkerPkgConfigFlags() const
  380. {
  381. auto packages = getPackages();
  382. if (packages.size() > 0)
  383. return "$(shell pkg-config --libs " + packages.joinIntoString (" ") + ")";
  384. return {};
  385. }
  386. StringArray getCPreprocessorFlags (const BuildConfiguration&) const
  387. {
  388. StringArray result;
  389. if (linuxLibs.contains("pthread"))
  390. result.add ("-pthread");
  391. return result;
  392. }
  393. StringArray getCFlags (const BuildConfiguration& config) const
  394. {
  395. StringArray result;
  396. if (anyTargetIsSharedLibrary())
  397. result.add ("-fPIC");
  398. if (config.isDebug())
  399. {
  400. result.add ("-g");
  401. result.add ("-ggdb");
  402. }
  403. result.add ("-O" + config.getGCCOptimisationFlag());
  404. if (config.isLinkTimeOptimisationEnabled())
  405. result.add ("-flto");
  406. auto extra = replacePreprocessorTokens (config, getExtraCompilerFlagsString()).trim();
  407. if (extra.isNotEmpty())
  408. result.add (extra);
  409. return result;
  410. }
  411. StringArray getCXXFlags() const
  412. {
  413. StringArray result;
  414. auto cppStandard = project.getCppStandardString();
  415. if (cppStandard == "latest")
  416. cppStandard = "1z";
  417. cppStandard = "-std=" + String (shouldUseGNUExtensions() ? "gnu++" : "c++") + cppStandard;
  418. result.add (cppStandard);
  419. return result;
  420. }
  421. StringArray getHeaderSearchPaths (const BuildConfiguration& config) const
  422. {
  423. StringArray searchPaths (extraSearchPaths);
  424. searchPaths.addArray (config.getHeaderSearchPaths());
  425. searchPaths = getCleanedStringArray (searchPaths);
  426. StringArray result;
  427. for (auto& path : searchPaths)
  428. result.add (FileHelpers::unixStylePath (replacePreprocessorTokens (config, path)));
  429. return result;
  430. }
  431. StringArray getLibraryNames (const BuildConfiguration& config) const
  432. {
  433. StringArray result (linuxLibs);
  434. auto libraries = StringArray::fromTokens (getExternalLibrariesString(), ";", "\"'");
  435. libraries.removeEmptyStrings();
  436. for (auto& lib : libraries)
  437. result.add (replacePreprocessorTokens (config, lib).trim());
  438. return result;
  439. }
  440. StringArray getLibrarySearchPaths (const BuildConfiguration& config) const
  441. {
  442. auto result = getSearchPathsFromString (config.getLibrarySearchPathString());
  443. for (auto path : moduleLibSearchPaths)
  444. result.add (path + "/" + config.getModuleLibraryArchName());
  445. return result;
  446. }
  447. StringArray getLinkerFlags (const BuildConfiguration& config) const
  448. {
  449. auto result = makefileExtraLinkerFlags;
  450. if (! config.isDebug())
  451. result.add ("-fvisibility=hidden");
  452. if (config.isLinkTimeOptimisationEnabled())
  453. result.add ("-flto");
  454. auto extraFlags = getExtraLinkerFlagsString().trim();
  455. if (extraFlags.isNotEmpty())
  456. result.add (replacePreprocessorTokens (config, extraFlags));
  457. return result;
  458. }
  459. bool isWebBrowserComponentEnabled() const
  460. {
  461. static String guiExtrasModule ("juce_gui_extra");
  462. return (project.getModules().isModuleEnabled (guiExtrasModule)
  463. && project.isConfigFlagEnabled ("JUCE_WEB_BROWSER", true));
  464. }
  465. //==============================================================================
  466. void writeDefineFlags (OutputStream& out, const MakeBuildConfiguration& config) const
  467. {
  468. out << createGCCPreprocessorFlags (mergePreprocessorDefs (getDefines (config), getAllPreprocessorDefs (config, ProjectType::Target::unspecified)));
  469. }
  470. void writePkgConfigFlags (OutputStream& out) const
  471. {
  472. auto flags = getPreprocessorPkgConfigFlags();
  473. if (flags.isNotEmpty())
  474. out << " " << flags;
  475. }
  476. void writeCPreprocessorFlags (OutputStream& out, const BuildConfiguration& config) const
  477. {
  478. auto flags = getCPreprocessorFlags (config);
  479. if (! flags.isEmpty())
  480. out << " " << flags.joinIntoString (" ");
  481. }
  482. void writeHeaderPathFlags (OutputStream& out, const BuildConfiguration& config) const
  483. {
  484. for (auto& path : getHeaderSearchPaths (config))
  485. out << " -I" << escapeSpaces (path).replace ("~", "$(HOME)");
  486. }
  487. void writeCppFlags (OutputStream& out, const MakeBuildConfiguration& config) const
  488. {
  489. out << " JUCE_CPPFLAGS := $(DEPFLAGS)";
  490. writeDefineFlags (out, config);
  491. writePkgConfigFlags (out);
  492. writeCPreprocessorFlags (out, config);
  493. writeHeaderPathFlags (out, config);
  494. out << " $(CPPFLAGS)" << newLine;
  495. }
  496. void writeLinkerFlags (OutputStream& out, const BuildConfiguration& config) const
  497. {
  498. out << " JUCE_LDFLAGS += $(TARGET_ARCH) -L$(JUCE_BINDIR) -L$(JUCE_LIBDIR)";
  499. for (auto path : getLibrarySearchPaths (config))
  500. out << " -L" << escapeSpaces (path).replace ("~", "$(HOME)");
  501. auto pkgConfigFlags = getLinkerPkgConfigFlags();
  502. if (pkgConfigFlags.isNotEmpty())
  503. out << " " << getLinkerPkgConfigFlags();
  504. auto linkerFlags = getLinkerFlags (config).joinIntoString (" ");
  505. if (linkerFlags.isNotEmpty())
  506. out << " " << linkerFlags;
  507. for (auto& libName : getLibraryNames (config))
  508. out << " -l" << libName;
  509. out << " $(LDFLAGS)" << newLine;
  510. }
  511. void writeTargetLines (OutputStream& out, const bool useLinuxPackages) const
  512. {
  513. auto n = targets.size();
  514. for (int i = 0; i < n; ++i)
  515. {
  516. if (auto* target = targets.getUnchecked (i))
  517. {
  518. if (target->type == ProjectType::Target::AggregateTarget)
  519. {
  520. StringArray dependencies;
  521. MemoryOutputStream subTargetLines;
  522. for (int j = 0; j < n; ++j)
  523. {
  524. if (i == j) continue;
  525. if (auto* dependency = targets.getUnchecked (j))
  526. {
  527. if (dependency->type != ProjectType::Target::SharedCodeTarget)
  528. {
  529. auto phonyName = dependency->getPhonyName();
  530. subTargetLines << phonyName << " : " << dependency->getBuildProduct() << newLine;
  531. dependencies.add (phonyName);
  532. }
  533. }
  534. }
  535. out << "all : " << dependencies.joinIntoString (" ") << newLine << newLine;
  536. out << subTargetLines.toString() << newLine << newLine;
  537. }
  538. else
  539. {
  540. if (! getProject().getProjectType().isAudioPlugin())
  541. out << "all : " << target->getBuildProduct() << newLine << newLine;
  542. target->writeTargetLine (out, useLinuxPackages);
  543. }
  544. }
  545. }
  546. }
  547. void writeConfig (OutputStream& out, const MakeBuildConfiguration& config) const
  548. {
  549. String buildDirName ("build");
  550. auto intermediatesDirName = buildDirName + "/intermediate/" + config.getName();
  551. auto outputDir = buildDirName;
  552. if (config.getTargetBinaryRelativePathString().isNotEmpty())
  553. {
  554. RelativePath binaryPath (config.getTargetBinaryRelativePathString(), RelativePath::projectFolder);
  555. outputDir = binaryPath.rebased (projectFolder, getTargetFolder(), RelativePath::buildTargetFolder).toUnixStyle();
  556. }
  557. out << "ifeq ($(CONFIG)," << escapeSpaces (config.getName()) << ")" << newLine;
  558. out << " JUCE_BINDIR := " << escapeSpaces (buildDirName) << newLine
  559. << " JUCE_LIBDIR := " << escapeSpaces (buildDirName) << newLine
  560. << " JUCE_OBJDIR := " << escapeSpaces (intermediatesDirName) << newLine
  561. << " JUCE_OUTDIR := " << escapeSpaces (outputDir) << newLine
  562. << newLine
  563. << " ifeq ($(TARGET_ARCH),)" << newLine
  564. << " TARGET_ARCH := " << getArchFlags (config) << newLine
  565. << " endif" << newLine
  566. << newLine;
  567. writeCppFlags (out, config);
  568. for (auto target : targets)
  569. {
  570. auto lines = target->getTargetSettings (config);
  571. if (lines.size() > 0)
  572. out << " " << lines.joinIntoString ("\n ") << newLine;
  573. out << newLine;
  574. }
  575. out << " JUCE_CFLAGS += $(JUCE_CPPFLAGS) $(TARGET_ARCH)";
  576. auto cflags = getCFlags (config).joinIntoString (" ");
  577. if (cflags.isNotEmpty())
  578. out << " " << cflags;
  579. out << " $(CFLAGS)" << newLine;
  580. out << " JUCE_CXXFLAGS += $(JUCE_CFLAGS)";
  581. auto cxxflags = getCXXFlags().joinIntoString (" ");
  582. if (cxxflags.isNotEmpty())
  583. out << " " << cxxflags;
  584. out << " $(CXXFLAGS)" << newLine;
  585. writeLinkerFlags (out, config);
  586. out << newLine;
  587. out << " CLEANCMD = rm -rf $(JUCE_OUTDIR)/$(TARGET) $(JUCE_OBJDIR)" << newLine
  588. << "endif" << newLine
  589. << newLine;
  590. }
  591. void writeIncludeLines (OutputStream& out) const
  592. {
  593. auto n = targets.size();
  594. for (int i = 0; i < n; ++i)
  595. {
  596. if (auto* target = targets.getUnchecked (i))
  597. {
  598. if (target->type == ProjectType::Target::AggregateTarget)
  599. continue;
  600. out << "-include $(OBJECTS_" << target->getTargetVarName()
  601. << ":%.o=%.d)" << newLine;
  602. }
  603. }
  604. }
  605. void writeMakefile (OutputStream& out) const
  606. {
  607. out << "# Automatically generated makefile, created by the Projucer" << newLine
  608. << "# Don't edit this file! Your changes will be overwritten when you re-save the Projucer project!" << newLine
  609. << newLine;
  610. out << "# build with \"V=1\" for verbose builds" << newLine
  611. << "ifeq ($(V), 1)" << newLine
  612. << "V_AT =" << newLine
  613. << "else" << newLine
  614. << "V_AT = @" << newLine
  615. << "endif" << newLine
  616. << newLine;
  617. out << "# (this disables dependency generation if multiple architectures are set)" << newLine
  618. << "DEPFLAGS := $(if $(word 2, $(TARGET_ARCH)), , -MMD)" << newLine
  619. << newLine;
  620. out << "ifndef STRIP" << newLine
  621. << " STRIP=strip" << newLine
  622. << "endif" << newLine
  623. << newLine;
  624. out << "ifndef AR" << newLine
  625. << " AR=ar" << newLine
  626. << "endif" << newLine
  627. << newLine;
  628. out << "ifndef CONFIG" << newLine
  629. << " CONFIG=" << escapeSpaces (getConfiguration(0)->getName()) << newLine
  630. << "endif" << newLine
  631. << newLine;
  632. out << "JUCE_ARCH_LABEL := $(shell uname -m)" << newLine
  633. << newLine;
  634. for (ConstConfigIterator config (*this); config.next();)
  635. writeConfig (out, dynamic_cast<const MakeBuildConfiguration&> (*config));
  636. for (auto target : targets)
  637. target->writeObjects (out);
  638. out << getPhonyTargetLine() << newLine << newLine;
  639. auto packages = getPackages();
  640. writeTargetLines (out, ! packages.isEmpty());
  641. for (auto target : targets)
  642. target->addFiles (out);
  643. if (! packages.isEmpty())
  644. {
  645. out << "check-pkg-config:" << newLine
  646. << "\t@command -v pkg-config >/dev/null 2>&1 || "
  647. "{ echo >&2 \"pkg-config not installed. Please, install it.\"; "
  648. "exit 1; }" << newLine
  649. << "\t@pkg-config --print-errors";
  650. for (auto& pkg : packages)
  651. out << " " << pkg;
  652. out << newLine << newLine;
  653. }
  654. out << "clean:" << newLine
  655. << "\t@echo Cleaning " << projectName << newLine
  656. << "\t$(V_AT)$(CLEANCMD)" << newLine
  657. << newLine;
  658. out << "strip:" << newLine
  659. << "\t@echo Stripping " << projectName << newLine
  660. << "\t-$(V_AT)$(STRIP) --strip-unneeded $(JUCE_OUTDIR)/$(TARGET)" << newLine
  661. << newLine;
  662. writeIncludeLines (out);
  663. }
  664. String getArchFlags (const BuildConfiguration& config) const
  665. {
  666. if (auto* makeConfig = dynamic_cast<const MakeBuildConfiguration*> (&config))
  667. return makeConfig->getArchitectureTypeString();
  668. return "-march=native";
  669. }
  670. String getObjectFileFor (const RelativePath& file) const
  671. {
  672. return file.getFileNameWithoutExtension()
  673. + "_" + String::toHexString (file.toUnixStyle().hashCode()) + ".o";
  674. }
  675. String getPhonyTargetLine() const
  676. {
  677. MemoryOutputStream phonyTargetLine;
  678. phonyTargetLine << ".PHONY: clean all";
  679. if (! getProject().getProjectType().isAudioPlugin())
  680. return phonyTargetLine.toString();
  681. for (auto target : targets)
  682. if (target->type != ProjectType::Target::SharedCodeTarget
  683. && target->type != ProjectType::Target::AggregateTarget)
  684. phonyTargetLine << " " << target->getPhonyName();
  685. return phonyTargetLine.toString();
  686. }
  687. friend class CLionProjectExporter;
  688. OwnedArray<MakefileTarget> targets;
  689. JUCE_DECLARE_NON_COPYABLE (MakefileProjectExporter)
  690. };