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.

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