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.

906 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. }
  42. String getModuleLibraryArchName() const override
  43. {
  44. auto archFlag = getArchitectureTypeString();
  45. String prefix ("-march=");
  46. if (archFlag.startsWith (prefix))
  47. return archFlag.substring (prefix.length());
  48. if (archFlag == "-m64")
  49. return "x86_64";
  50. if (archFlag == "-m32")
  51. return "i386";
  52. return "${JUCE_ARCH_LABEL}";
  53. }
  54. String getArchitectureTypeString() const { return architectureTypeValue.get(); }
  55. //==============================================================================
  56. ValueWithDefault architectureTypeValue;
  57. };
  58. BuildConfiguration::Ptr createBuildConfig (const ValueTree& tree) const override
  59. {
  60. return new MakeBuildConfiguration (project, tree, *this);
  61. }
  62. public:
  63. //==============================================================================
  64. class MakefileTarget : public ProjectType::Target
  65. {
  66. public:
  67. MakefileTarget (ProjectType::Target::Type targetType, const MakefileProjectExporter& exporter)
  68. : ProjectType::Target (targetType), owner (exporter)
  69. {}
  70. StringArray getCompilerFlags() const
  71. {
  72. StringArray result;
  73. if (getTargetFileType() == sharedLibraryOrDLL || getTargetFileType() == pluginBundle)
  74. {
  75. result.add ("-fPIC");
  76. result.add ("-fvisibility=hidden");
  77. }
  78. return result;
  79. }
  80. StringArray getLinkerFlags() const
  81. {
  82. StringArray result;
  83. if (getTargetFileType() == sharedLibraryOrDLL || getTargetFileType() == pluginBundle)
  84. {
  85. result.add ("-shared");
  86. if (getTargetFileType() == pluginBundle)
  87. result.add ("-Wl,--no-undefined");
  88. }
  89. return result;
  90. }
  91. StringPairArray getDefines (const BuildConfiguration& config) const
  92. {
  93. StringPairArray result;
  94. auto commonOptionKeys = owner.getAllPreprocessorDefs (config, ProjectType::Target::unspecified).getAllKeys();
  95. auto targetSpecific = owner.getAllPreprocessorDefs (config, type);
  96. for (auto& key : targetSpecific.getAllKeys())
  97. if (! commonOptionKeys.contains (key))
  98. result.set (key, targetSpecific[key]);
  99. return result;
  100. }
  101. StringArray getTargetSettings (const MakeBuildConfiguration& config) const
  102. {
  103. if (type == AggregateTarget)
  104. // the aggregate target should not specify any settings at all!
  105. // it just defines dependencies on the other targets.
  106. return {};
  107. StringArray defines;
  108. auto defs = getDefines (config);
  109. for (auto& key : defs.getAllKeys())
  110. defines.add ("-D" + key + "=" + defs[key]);
  111. StringArray s;
  112. const String cppflagsVarName ("JUCE_CPPFLAGS_" + getTargetVarName());
  113. s.add (cppflagsVarName + " := " + defines.joinIntoString (" "));
  114. auto cflags = getCompilerFlags();
  115. if (! cflags.isEmpty())
  116. s.add ("JUCE_CFLAGS_" + getTargetVarName() + " := " + cflags.joinIntoString (" "));
  117. auto ldflags = getLinkerFlags();
  118. if (! ldflags.isEmpty())
  119. s.add ("JUCE_LDFLAGS_" + getTargetVarName() + " := " + ldflags.joinIntoString (" "));
  120. String targetName (owner.replacePreprocessorTokens (config, config.getTargetBinaryNameString()));
  121. if (owner.projectType.isStaticLibrary())
  122. targetName = getStaticLibbedFilename (targetName);
  123. else if (owner.projectType.isDynamicLibrary())
  124. targetName = getDynamicLibbedFilename (targetName);
  125. else
  126. targetName = targetName.upToLastOccurrenceOf (".", false, false) + getTargetFileSuffix();
  127. s.add ("JUCE_TARGET_" + getTargetVarName() + String (" := ") + escapeSpaces (targetName));
  128. return s;
  129. }
  130. String getTargetFileSuffix() const
  131. {
  132. switch (type)
  133. {
  134. case VSTPlugIn:
  135. case DynamicLibrary: return ".so";
  136. case VST3PlugIn: return ".vst3";
  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. const Type targetType = (owner.getProject().getProjectType().isAudioPlugin() ? type : SharedCodeTarget);
  169. const File 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. const String cppflagsVarName = String ("JUCE_CPPFLAGS_") + getTargetVarName();
  183. const String cflagsVarName = String ("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) " : "\t$(V_AT)$(CXX) $(JUCE_CXXFLAGS) ")
  192. << "$(" << cppflagsVarName << ") $(" << cflagsVarName << ") -o \"$@\" -c \"$<\""
  193. << newLine << newLine;
  194. }
  195. }
  196. String getBuildProduct() const
  197. {
  198. return String ("$(JUCE_OUTDIR)/$(JUCE_TARGET_") + getTargetVarName() + String (")");
  199. }
  200. String getPhonyName() const
  201. {
  202. return String (getName()).upToFirstOccurrenceOf (" ", false, false);
  203. }
  204. void writeTargetLine (OutputStream& out, const bool useLinuxPackages)
  205. {
  206. jassert (type != AggregateTarget);
  207. out << getBuildProduct() << " : "
  208. << ((useLinuxPackages) ? "check-pkg-config " : "")
  209. << "$(OBJECTS_" << getTargetVarName() << ") $(RESOURCES)";
  210. if (type != SharedCodeTarget && owner.shouldBuildTargetType (SharedCodeTarget))
  211. out << " $(JUCE_OUTDIR)/$(JUCE_TARGET_SHARED_CODE)";
  212. out << newLine << "\t@echo Linking \"" << owner.projectName << " - " << getName() << "\"" << newLine
  213. << "\t-$(V_AT)mkdir -p $(JUCE_BINDIR)" << newLine
  214. << "\t-$(V_AT)mkdir -p $(JUCE_LIBDIR)" << newLine
  215. << "\t-$(V_AT)mkdir -p $(JUCE_OUTDIR)" << newLine;
  216. if (owner.projectType.isStaticLibrary() || type == SharedCodeTarget)
  217. out << "\t$(V_AT)$(AR) -rcs " << getBuildProduct()
  218. << " $(OBJECTS_" << getTargetVarName() << ")" << newLine;
  219. else
  220. {
  221. out << "\t$(V_AT)$(CXX) -o " << getBuildProduct()
  222. << " $(OBJECTS_" << getTargetVarName() << ") ";
  223. if (owner.shouldBuildTargetType (SharedCodeTarget))
  224. out << "$(JUCE_OUTDIR)/$(JUCE_TARGET_SHARED_CODE) ";
  225. out << "$(JUCE_LDFLAGS) ";
  226. if (getTargetFileType() == sharedLibraryOrDLL || getTargetFileType() == pluginBundle
  227. || type == GUIApp || type == StandalonePlugIn)
  228. out << "$(JUCE_LDFLAGS_" << getTargetVarName() << ") ";
  229. out << "$(RESOURCES) $(TARGET_ARCH)" << newLine;
  230. }
  231. out << newLine;
  232. }
  233. const MakefileProjectExporter& owner;
  234. };
  235. //==============================================================================
  236. static const char* getNameLinux() { return "Linux Makefile"; }
  237. static const char* getValueTreeTypeName() { return "LINUX_MAKE"; }
  238. String getExtraPkgConfigString() const { return extraPkgConfigValue.get(); }
  239. static MakefileProjectExporter* createForSettings (Project& project, const ValueTree& settings)
  240. {
  241. if (settings.hasType (getValueTreeTypeName()))
  242. return new MakefileProjectExporter (project, settings);
  243. return nullptr;
  244. }
  245. //==============================================================================
  246. MakefileProjectExporter (Project& p, const ValueTree& t)
  247. : ProjectExporter (p, t),
  248. extraPkgConfigValue (settings, Ids::linuxExtraPkgConfig, getProject().getUndoManagerFor (settings))
  249. {
  250. name = getNameLinux();
  251. targetLocationValue.setDefault (getDefaultBuildsRootFolder() + "LinuxMakefile");
  252. }
  253. //==============================================================================
  254. bool canLaunchProject() override { return false; }
  255. bool launchProject() override { return false; }
  256. bool usesMMFiles() const override { return false; }
  257. bool canCopeWithDuplicateFiles() override { return false; }
  258. bool supportsUserDefinedConfigurations() const override { return true; }
  259. bool isXcode() const override { return false; }
  260. bool isVisualStudio() const override { return false; }
  261. bool isCodeBlocks() const override { return false; }
  262. bool isMakefile() const override { return true; }
  263. bool isAndroidStudio() const override { return false; }
  264. bool isCLion() const override { return false; }
  265. bool isAndroid() const override { return false; }
  266. bool isWindows() const override { return false; }
  267. bool isLinux() const override { return true; }
  268. bool isOSX() const override { return false; }
  269. bool isiOS() const override { return false; }
  270. bool supportsTargetType (ProjectType::Target::Type type) const override
  271. {
  272. switch (type)
  273. {
  274. case ProjectType::Target::GUIApp:
  275. case ProjectType::Target::ConsoleApp:
  276. case ProjectType::Target::StaticLibrary:
  277. case ProjectType::Target::SharedCodeTarget:
  278. case ProjectType::Target::AggregateTarget:
  279. case ProjectType::Target::VSTPlugIn:
  280. case ProjectType::Target::StandalonePlugIn:
  281. case ProjectType::Target::DynamicLibrary:
  282. return true;
  283. default:
  284. break;
  285. }
  286. return false;
  287. }
  288. void createExporterProperties (PropertyListBuilder& properties) override
  289. {
  290. properties.add (new TextPropertyComponent (extraPkgConfigValue, "pkg-config libraries", 8192, false),
  291. "Extra pkg-config libraries for you application. Each package should be space separated.");
  292. }
  293. //==============================================================================
  294. bool anyTargetIsSharedLibrary() const
  295. {
  296. for (auto* target : targets)
  297. {
  298. const ProjectType::Target::TargetFileType fileType = target->getTargetFileType();
  299. if (fileType == ProjectType::Target::sharedLibraryOrDLL
  300. || fileType == ProjectType::Target::pluginBundle)
  301. return true;
  302. }
  303. return false;
  304. }
  305. //==============================================================================
  306. void create (const OwnedArray<LibraryModule>&) const override
  307. {
  308. MemoryOutputStream mo;
  309. writeMakefile (mo);
  310. overwriteFileIfDifferentOrThrow (getTargetFolder().getChildFile ("Makefile"), mo);
  311. }
  312. //==============================================================================
  313. void addPlatformSpecificSettingsForProjectType (const ProjectType&) override
  314. {
  315. callForAllSupportedTargets ([this] (ProjectType::Target::Type targetType)
  316. {
  317. if (MakefileTarget* target = new MakefileTarget (targetType, *this))
  318. {
  319. if (targetType == ProjectType::Target::AggregateTarget)
  320. targets.insert (0, target);
  321. else
  322. targets.add (target);
  323. }
  324. });
  325. // If you hit this assert, you tried to generate a project for an exporter
  326. // that does not support any of your targets!
  327. jassert (targets.size() > 0);
  328. }
  329. //==============================================================================
  330. void initialiseDependencyPathValues() override
  331. {
  332. vst3Path.referTo (Value (new DependencyPathValueSource (getSetting (Ids::vst3Folder),
  333. Ids::vst3Path,
  334. TargetOS::linux)));
  335. }
  336. private:
  337. ValueWithDefault extraPkgConfigValue;
  338. //==============================================================================
  339. StringPairArray getDefines (const BuildConfiguration& config) const
  340. {
  341. StringPairArray result;
  342. result.set ("LINUX", "1");
  343. if (config.isDebug())
  344. {
  345. result.set ("DEBUG", "1");
  346. result.set ("_DEBUG", "1");
  347. }
  348. else
  349. {
  350. result.set ("NDEBUG", "1");
  351. }
  352. result = mergePreprocessorDefs (result, getAllPreprocessorDefs (config, ProjectType::Target::unspecified));
  353. return result;
  354. }
  355. StringArray getPackages() const
  356. {
  357. StringArray packages;
  358. packages.addTokens (getExtraPkgConfigString(), " ", "\"'");
  359. packages.removeEmptyStrings();
  360. packages.addArray (linuxPackages);
  361. if (isWebBrowserComponentEnabled())
  362. {
  363. packages.add ("webkit2gtk-4.0");
  364. packages.add ("gtk+-x11-3.0");
  365. }
  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 = "1z";
  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. StringArray libraries;
  432. libraries.addTokens (getExternalLibrariesString(), ";", "\"'");
  433. libraries.removeEmptyStrings();
  434. for (auto& lib : libraries)
  435. result.add (replacePreprocessorTokens (config, lib).trim());
  436. return result;
  437. }
  438. StringArray getLibrarySearchPaths (const BuildConfiguration& config) const
  439. {
  440. auto result = getSearchPathsFromString (config.getLibrarySearchPathString());
  441. for (auto path : moduleLibSearchPaths)
  442. result.add (path + "/" + config.getModuleLibraryArchName());
  443. return result;
  444. }
  445. StringArray getLinkerFlags (const BuildConfiguration& config) const
  446. {
  447. StringArray result (makefileExtraLinkerFlags);
  448. if (! config.isDebug())
  449. result.add ("-fvisibility=hidden");
  450. if (config.isLinkTimeOptimisationEnabled())
  451. result.add ("-flto");
  452. auto extraFlags = getExtraLinkerFlagsString().trim();
  453. if (extraFlags.isNotEmpty())
  454. result.add (replacePreprocessorTokens (config, extraFlags));
  455. return result;
  456. }
  457. bool isWebBrowserComponentEnabled() const
  458. {
  459. static String guiExtrasModule ("juce_gui_extra");
  460. return (project.getModules().isModuleEnabled (guiExtrasModule)
  461. && project.isConfigFlagEnabled ("JUCE_WEB_BROWSER", true));
  462. }
  463. //==============================================================================
  464. void writeDefineFlags (OutputStream& out, const MakeBuildConfiguration& config) const
  465. {
  466. out << createGCCPreprocessorFlags (mergePreprocessorDefs (getDefines (config), getAllPreprocessorDefs (config, ProjectType::Target::unspecified)));
  467. }
  468. void writePkgConfigFlags (OutputStream& out) const
  469. {
  470. auto flags = getPreprocessorPkgConfigFlags();
  471. if (flags.isNotEmpty())
  472. out << " " << flags;
  473. }
  474. void writeCPreprocessorFlags (OutputStream& out, const BuildConfiguration& config) const
  475. {
  476. auto flags = getCPreprocessorFlags (config);
  477. if (! flags.isEmpty())
  478. out << " " << flags.joinIntoString (" ");
  479. }
  480. void writeHeaderPathFlags (OutputStream& out, const BuildConfiguration& config) const
  481. {
  482. for (auto& path : getHeaderSearchPaths (config))
  483. out << " -I" << escapeSpaces (path).replace ("~", "$(HOME)");
  484. }
  485. void writeCppFlags (OutputStream& out, const MakeBuildConfiguration& config) const
  486. {
  487. out << " JUCE_CPPFLAGS := $(DEPFLAGS)";
  488. writeDefineFlags (out, config);
  489. writePkgConfigFlags (out);
  490. writeCPreprocessorFlags (out, config);
  491. writeHeaderPathFlags (out, config);
  492. out << " $(CPPFLAGS)" << newLine;
  493. }
  494. void writeLinkerFlags (OutputStream& out, const BuildConfiguration& config) const
  495. {
  496. out << " JUCE_LDFLAGS += $(TARGET_ARCH) -L$(JUCE_BINDIR) -L$(JUCE_LIBDIR)";
  497. for (auto path : getLibrarySearchPaths (config))
  498. out << " -L" << escapeSpaces (path).replace ("~", "$(HOME)");
  499. auto pkgConfigFlags = getLinkerPkgConfigFlags();
  500. if (pkgConfigFlags.isNotEmpty())
  501. out << " " << getLinkerPkgConfigFlags();
  502. auto linkerFlags = getLinkerFlags (config).joinIntoString (" ");
  503. if (linkerFlags.isNotEmpty())
  504. out << " " << linkerFlags;
  505. for (auto& libName : getLibraryNames (config))
  506. out << " -l" << libName;
  507. out << " $(LDFLAGS)" << newLine;
  508. }
  509. void writeTargetLines (OutputStream& out, const bool useLinuxPackages) const
  510. {
  511. const int n = targets.size();
  512. for (int i = 0; i < n; ++i)
  513. {
  514. if (auto* target = targets.getUnchecked (i))
  515. {
  516. if (target->type == ProjectType::Target::AggregateTarget)
  517. {
  518. StringArray dependencies;
  519. MemoryOutputStream subTargetLines;
  520. for (int j = 0; j < n; ++j)
  521. {
  522. if (i == j) continue;
  523. if (auto* dependency = targets.getUnchecked (j))
  524. {
  525. if (dependency->type != ProjectType::Target::SharedCodeTarget)
  526. {
  527. auto phonyName = dependency->getPhonyName();
  528. subTargetLines << phonyName << " : " << dependency->getBuildProduct() << newLine;
  529. dependencies.add (phonyName);
  530. }
  531. }
  532. }
  533. out << "all : " << dependencies.joinIntoString (" ") << newLine << newLine;
  534. out << subTargetLines.toString() << newLine << newLine;
  535. }
  536. else
  537. {
  538. if (! getProject().getProjectType().isAudioPlugin())
  539. out << "all : " << target->getBuildProduct() << newLine << newLine;
  540. target->writeTargetLine (out, useLinuxPackages);
  541. }
  542. }
  543. }
  544. }
  545. void writeConfig (OutputStream& out, const MakeBuildConfiguration& config) const
  546. {
  547. const String buildDirName ("build");
  548. const String intermediatesDirName (buildDirName + "/intermediate/" + config.getName());
  549. String outputDir (buildDirName);
  550. if (config.getTargetBinaryRelativePathString().isNotEmpty())
  551. {
  552. RelativePath binaryPath (config.getTargetBinaryRelativePathString(), RelativePath::projectFolder);
  553. outputDir = binaryPath.rebased (projectFolder, getTargetFolder(), RelativePath::buildTargetFolder).toUnixStyle();
  554. }
  555. out << "ifeq ($(CONFIG)," << escapeSpaces (config.getName()) << ")" << newLine;
  556. out << " JUCE_BINDIR := " << escapeSpaces (buildDirName) << newLine
  557. << " JUCE_LIBDIR := " << escapeSpaces (buildDirName) << newLine
  558. << " JUCE_OBJDIR := " << escapeSpaces (intermediatesDirName) << newLine
  559. << " JUCE_OUTDIR := " << escapeSpaces (outputDir) << newLine
  560. << newLine
  561. << " ifeq ($(TARGET_ARCH),)" << newLine
  562. << " TARGET_ARCH := " << getArchFlags (config) << newLine
  563. << " endif" << newLine
  564. << newLine;
  565. writeCppFlags (out, config);
  566. for (auto target : targets)
  567. {
  568. StringArray lines = target->getTargetSettings (config);
  569. if (lines.size() > 0)
  570. out << " " << lines.joinIntoString ("\n ") << newLine;
  571. out << newLine;
  572. }
  573. out << " JUCE_CFLAGS += $(JUCE_CPPFLAGS) $(TARGET_ARCH)";
  574. auto cflags = getCFlags (config).joinIntoString (" ");
  575. if (cflags.isNotEmpty())
  576. out << " " << cflags;
  577. out << " $(CFLAGS)" << newLine;
  578. out << " JUCE_CXXFLAGS += $(JUCE_CFLAGS)";
  579. auto cxxflags = getCXXFlags().joinIntoString (" ");
  580. if (cxxflags.isNotEmpty())
  581. out << " " << cxxflags;
  582. out << " $(CXXFLAGS)" << newLine;
  583. writeLinkerFlags (out, config);
  584. out << newLine;
  585. out << " CLEANCMD = rm -rf $(JUCE_OUTDIR)/$(TARGET) $(JUCE_OBJDIR)" << newLine
  586. << "endif" << newLine
  587. << newLine;
  588. }
  589. void writeIncludeLines (OutputStream& out) const
  590. {
  591. const int n = targets.size();
  592. for (int i = 0; i < n; ++i)
  593. {
  594. if (auto* target = targets.getUnchecked (i))
  595. {
  596. if (target->type == ProjectType::Target::AggregateTarget)
  597. continue;
  598. out << "-include $(OBJECTS_" << target->getTargetVarName()
  599. << ":%.o=%.d)" << newLine;
  600. }
  601. }
  602. }
  603. void writeMakefile (OutputStream& out) const
  604. {
  605. out << "# Automatically generated makefile, created by the Projucer" << newLine
  606. << "# Don't edit this file! Your changes will be overwritten when you re-save the Projucer project!" << newLine
  607. << newLine;
  608. out << "# build with \"V=1\" for verbose builds" << newLine
  609. << "ifeq ($(V), 1)" << newLine
  610. << "V_AT =" << newLine
  611. << "else" << newLine
  612. << "V_AT = @" << newLine
  613. << "endif" << newLine
  614. << newLine;
  615. out << "# (this disables dependency generation if multiple architectures are set)" << newLine
  616. << "DEPFLAGS := $(if $(word 2, $(TARGET_ARCH)), , -MMD)" << newLine
  617. << newLine;
  618. out << "ifndef STRIP" << newLine
  619. << " STRIP=strip" << newLine
  620. << "endif" << newLine
  621. << newLine;
  622. out << "ifndef AR" << newLine
  623. << " AR=ar" << newLine
  624. << "endif" << newLine
  625. << newLine;
  626. out << "ifndef CONFIG" << newLine
  627. << " CONFIG=" << escapeSpaces (getConfiguration(0)->getName()) << newLine
  628. << "endif" << newLine
  629. << newLine;
  630. out << "JUCE_ARCH_LABEL := $(shell uname -m)" << newLine
  631. << newLine;
  632. for (ConstConfigIterator config (*this); config.next();)
  633. writeConfig (out, dynamic_cast<const MakeBuildConfiguration&> (*config));
  634. for (auto target : targets)
  635. target->writeObjects (out);
  636. out << getPhonyTargetLine() << newLine << newLine;
  637. auto packages = getPackages();
  638. writeTargetLines (out, ! packages.isEmpty());
  639. for (auto target : targets)
  640. target->addFiles (out);
  641. if (! packages.isEmpty())
  642. {
  643. out << "check-pkg-config:" << newLine
  644. << "\t@command -v pkg-config >/dev/null 2>&1 || "
  645. "{ echo >&2 \"pkg-config not installed. Please, install it.\"; "
  646. "exit 1; }" << newLine
  647. << "\t@pkg-config --print-errors";
  648. for (auto& pkg : packages)
  649. out << " " << pkg;
  650. out << newLine << newLine;
  651. }
  652. out << "clean:" << newLine
  653. << "\t@echo Cleaning " << projectName << newLine
  654. << "\t$(V_AT)$(CLEANCMD)" << newLine
  655. << newLine;
  656. out << "strip:" << newLine
  657. << "\t@echo Stripping " << projectName << newLine
  658. << "\t-$(V_AT)$(STRIP) --strip-unneeded $(JUCE_OUTDIR)/$(TARGET)" << newLine
  659. << newLine;
  660. writeIncludeLines (out);
  661. }
  662. String getArchFlags (const BuildConfiguration& config) const
  663. {
  664. if (auto* makeConfig = dynamic_cast<const MakeBuildConfiguration*> (&config))
  665. return makeConfig->getArchitectureTypeString();
  666. return "-march=native";
  667. }
  668. String getObjectFileFor (const RelativePath& file) const
  669. {
  670. return file.getFileNameWithoutExtension()
  671. + "_" + String::toHexString (file.toUnixStyle().hashCode()) + ".o";
  672. }
  673. String getPhonyTargetLine() const
  674. {
  675. MemoryOutputStream phonyTargetLine;
  676. phonyTargetLine << ".PHONY: clean all";
  677. if (! getProject().getProjectType().isAudioPlugin())
  678. return phonyTargetLine.toString();
  679. for (auto target : targets)
  680. if (target->type != ProjectType::Target::SharedCodeTarget
  681. && target->type != ProjectType::Target::AggregateTarget)
  682. phonyTargetLine << " " << target->getPhonyName();
  683. return phonyTargetLine.toString();
  684. }
  685. friend class CLionProjectExporter;
  686. OwnedArray<MakefileTarget> targets;
  687. JUCE_DECLARE_NON_COPYABLE (MakefileProjectExporter)
  688. };