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.

907 lines
32KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. #pragma once
  20. //==============================================================================
  21. class MakefileProjectExporter : public ProjectExporter
  22. {
  23. protected:
  24. //==============================================================================
  25. class MakeBuildConfiguration : public BuildConfiguration
  26. {
  27. public:
  28. MakeBuildConfiguration (Project& p, const ValueTree& settings, const ProjectExporter& e)
  29. : BuildConfiguration (p, settings, e),
  30. architectureTypeValue (config, Ids::linuxArchitecture, getUndoManager(), "-march=native")
  31. {
  32. linkTimeOptimisationValue.setDefault (false);
  33. optimisationLevelValue.setDefault (isDebug() ? gccO0 : gccO3);
  34. }
  35. void createConfigProperties (PropertyListBuilder& props) override
  36. {
  37. addGCCOptimisationProperty (props);
  38. props.add (new ChoicePropertyComponent (architectureTypeValue, "Architecture",
  39. { "<None>", "Native", "32-bit (-m32)", "64-bit (-m64)", "ARM v6", "ARM v7" },
  40. { { String() } , "-march=native", "-m32", "-m64", "-march=armv6", "-march=armv7" }),
  41. "Specifies the 32/64-bit architecture to use.");
  42. }
  43. String getModuleLibraryArchName() const override
  44. {
  45. auto archFlag = getArchitectureTypeString();
  46. String prefix ("-march=");
  47. if (archFlag.startsWith (prefix))
  48. return archFlag.substring (prefix.length());
  49. if (archFlag == "-m64")
  50. return "x86_64";
  51. if (archFlag == "-m32")
  52. return "i386";
  53. return "${JUCE_ARCH_LABEL}";
  54. }
  55. String getArchitectureTypeString() const { return architectureTypeValue.get(); }
  56. //==============================================================================
  57. ValueWithDefault architectureTypeValue;
  58. };
  59. BuildConfiguration::Ptr createBuildConfig (const ValueTree& tree) const override
  60. {
  61. return new MakeBuildConfiguration (project, tree, *this);
  62. }
  63. public:
  64. //==============================================================================
  65. class MakefileTarget : public ProjectType::Target
  66. {
  67. public:
  68. MakefileTarget (ProjectType::Target::Type targetType, const MakefileProjectExporter& exporter)
  69. : ProjectType::Target (targetType), owner (exporter)
  70. {}
  71. StringArray getCompilerFlags() const
  72. {
  73. StringArray result;
  74. if (getTargetFileType() == sharedLibraryOrDLL || getTargetFileType() == pluginBundle)
  75. {
  76. result.add ("-fPIC");
  77. result.add ("-fvisibility=hidden");
  78. }
  79. return result;
  80. }
  81. StringArray getLinkerFlags() const
  82. {
  83. StringArray result;
  84. if (getTargetFileType() == sharedLibraryOrDLL || getTargetFileType() == pluginBundle)
  85. {
  86. result.add ("-shared");
  87. if (getTargetFileType() == pluginBundle)
  88. result.add ("-Wl,--no-undefined");
  89. }
  90. return result;
  91. }
  92. StringPairArray getDefines (const BuildConfiguration& config) const
  93. {
  94. StringPairArray result;
  95. auto commonOptionKeys = owner.getAllPreprocessorDefs (config, ProjectType::Target::unspecified).getAllKeys();
  96. auto targetSpecific = owner.getAllPreprocessorDefs (config, type);
  97. for (auto& key : targetSpecific.getAllKeys())
  98. if (! commonOptionKeys.contains (key))
  99. result.set (key, targetSpecific[key]);
  100. return result;
  101. }
  102. StringArray getTargetSettings (const MakeBuildConfiguration& config) const
  103. {
  104. if (type == AggregateTarget)
  105. // the aggregate target should not specify any settings at all!
  106. // it just defines dependencies on the other targets.
  107. return {};
  108. StringArray defines;
  109. auto defs = getDefines (config);
  110. for (auto& key : defs.getAllKeys())
  111. defines.add ("-D" + key + "=" + defs[key]);
  112. StringArray s;
  113. const String 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. String 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. 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. String getExtraPkgConfigString() const { return extraPkgConfigValue.get(); }
  240. static MakefileProjectExporter* createForSettings (Project& project, const ValueTree& settings)
  241. {
  242. if (settings.hasType (getValueTreeTypeName()))
  243. return new MakefileProjectExporter (project, settings);
  244. return nullptr;
  245. }
  246. //==============================================================================
  247. MakefileProjectExporter (Project& p, const ValueTree& t)
  248. : ProjectExporter (p, t),
  249. extraPkgConfigValue (settings, Ids::linuxExtraPkgConfig, getProject().getUndoManagerFor (settings))
  250. {
  251. name = getNameLinux();
  252. targetLocationValue.setDefault (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 (extraPkgConfigValue, "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. ValueWithDefault extraPkgConfigValue;
  339. //==============================================================================
  340. StringPairArray getDefines (const BuildConfiguration& config) const
  341. {
  342. StringPairArray result;
  343. result.set ("LINUX", "1");
  344. if (config.isDebug())
  345. {
  346. result.set ("DEBUG", "1");
  347. result.set ("_DEBUG", "1");
  348. }
  349. else
  350. {
  351. result.set ("NDEBUG", "1");
  352. }
  353. result = mergePreprocessorDefs (result, getAllPreprocessorDefs (config, ProjectType::Target::unspecified));
  354. return result;
  355. }
  356. StringArray getPackages() const
  357. {
  358. StringArray packages;
  359. packages.addTokens (getExtraPkgConfigString(), " ", "\"'");
  360. packages.removeEmptyStrings();
  361. packages.addArray (linuxPackages);
  362. if (isWebBrowserComponentEnabled())
  363. {
  364. packages.add ("webkit2gtk-4.0");
  365. packages.add ("gtk+-x11-3.0");
  366. }
  367. packages.removeDuplicates (false);
  368. return packages;
  369. }
  370. String getPreprocessorPkgConfigFlags() const
  371. {
  372. auto packages = getPackages();
  373. if (packages.size() > 0)
  374. return "$(shell pkg-config --cflags " + packages.joinIntoString (" ") + ")";
  375. return {};
  376. }
  377. String getLinkerPkgConfigFlags() const
  378. {
  379. auto packages = getPackages();
  380. if (packages.size() > 0)
  381. return "$(shell pkg-config --libs " + packages.joinIntoString (" ") + ")";
  382. return {};
  383. }
  384. StringArray getCPreprocessorFlags (const BuildConfiguration&) const
  385. {
  386. StringArray result;
  387. if (linuxLibs.contains("pthread"))
  388. result.add ("-pthread");
  389. return result;
  390. }
  391. StringArray getCFlags (const BuildConfiguration& config) const
  392. {
  393. StringArray result;
  394. if (anyTargetIsSharedLibrary())
  395. result.add ("-fPIC");
  396. if (config.isDebug())
  397. {
  398. result.add ("-g");
  399. result.add ("-ggdb");
  400. }
  401. result.add ("-O" + config.getGCCOptimisationFlag());
  402. if (config.isLinkTimeOptimisationEnabled())
  403. result.add ("-flto");
  404. auto extra = replacePreprocessorTokens (config, getExtraCompilerFlagsString()).trim();
  405. if (extra.isNotEmpty())
  406. result.add (extra);
  407. return result;
  408. }
  409. StringArray getCXXFlags() const
  410. {
  411. StringArray result;
  412. auto cppStandard = project.getCppStandardString();
  413. if (cppStandard == "latest")
  414. cppStandard = "1z";
  415. cppStandard = "-std=" + String (shouldUseGNUExtensions() ? "gnu++" : "c++") + cppStandard;
  416. result.add (cppStandard);
  417. return result;
  418. }
  419. StringArray getHeaderSearchPaths (const BuildConfiguration& config) const
  420. {
  421. StringArray searchPaths (extraSearchPaths);
  422. searchPaths.addArray (config.getHeaderSearchPaths());
  423. searchPaths = getCleanedStringArray (searchPaths);
  424. StringArray result;
  425. for (auto& path : searchPaths)
  426. result.add (FileHelpers::unixStylePath (replacePreprocessorTokens (config, path)));
  427. return result;
  428. }
  429. StringArray getLibraryNames (const BuildConfiguration& config) const
  430. {
  431. StringArray result (linuxLibs);
  432. StringArray libraries;
  433. libraries.addTokens (getExternalLibrariesString(), ";", "\"'");
  434. libraries.removeEmptyStrings();
  435. for (auto& lib : libraries)
  436. result.add (replacePreprocessorTokens (config, lib).trim());
  437. return result;
  438. }
  439. StringArray getLibrarySearchPaths (const BuildConfiguration& config) const
  440. {
  441. auto result = getSearchPathsFromString (config.getLibrarySearchPathString());
  442. for (auto path : moduleLibSearchPaths)
  443. result.add (path + "/" + config.getModuleLibraryArchName());
  444. return result;
  445. }
  446. StringArray getLinkerFlags (const BuildConfiguration& config) const
  447. {
  448. StringArray result (makefileExtraLinkerFlags);
  449. if (! config.isDebug())
  450. result.add ("-fvisibility=hidden");
  451. if (config.isLinkTimeOptimisationEnabled())
  452. result.add ("-flto");
  453. auto extraFlags = getExtraLinkerFlagsString().trim();
  454. if (extraFlags.isNotEmpty())
  455. result.add (replacePreprocessorTokens (config, extraFlags));
  456. return result;
  457. }
  458. bool isWebBrowserComponentEnabled() const
  459. {
  460. static String guiExtrasModule ("juce_gui_extra");
  461. return (project.getModules().isModuleEnabled (guiExtrasModule)
  462. && project.isConfigFlagEnabled ("JUCE_WEB_BROWSER", true));
  463. }
  464. //==============================================================================
  465. void writeDefineFlags (OutputStream& out, const MakeBuildConfiguration& config) const
  466. {
  467. out << createGCCPreprocessorFlags (mergePreprocessorDefs (getDefines (config), getAllPreprocessorDefs (config, ProjectType::Target::unspecified)));
  468. }
  469. void writePkgConfigFlags (OutputStream& out) const
  470. {
  471. auto flags = getPreprocessorPkgConfigFlags();
  472. if (flags.isNotEmpty())
  473. out << " " << flags;
  474. }
  475. void writeCPreprocessorFlags (OutputStream& out, const BuildConfiguration& config) const
  476. {
  477. auto flags = getCPreprocessorFlags (config);
  478. if (! flags.isEmpty())
  479. out << " " << flags.joinIntoString (" ");
  480. }
  481. void writeHeaderPathFlags (OutputStream& out, const BuildConfiguration& config) const
  482. {
  483. for (auto& path : getHeaderSearchPaths (config))
  484. out << " -I" << escapeSpaces (path).replace ("~", "$(HOME)");
  485. }
  486. void writeCppFlags (OutputStream& out, const MakeBuildConfiguration& config) const
  487. {
  488. out << " JUCE_CPPFLAGS := $(DEPFLAGS)";
  489. writeDefineFlags (out, config);
  490. writePkgConfigFlags (out);
  491. writeCPreprocessorFlags (out, config);
  492. writeHeaderPathFlags (out, config);
  493. out << " $(CPPFLAGS)" << newLine;
  494. }
  495. void writeLinkerFlags (OutputStream& out, const BuildConfiguration& config) const
  496. {
  497. out << " JUCE_LDFLAGS += $(TARGET_ARCH) -L$(JUCE_BINDIR) -L$(JUCE_LIBDIR)";
  498. for (auto path : getLibrarySearchPaths (config))
  499. out << " -L" << escapeSpaces (path).replace ("~", "$(HOME)");
  500. auto pkgConfigFlags = getLinkerPkgConfigFlags();
  501. if (pkgConfigFlags.isNotEmpty())
  502. out << " " << getLinkerPkgConfigFlags();
  503. auto linkerFlags = getLinkerFlags (config).joinIntoString (" ");
  504. if (linkerFlags.isNotEmpty())
  505. out << " " << linkerFlags;
  506. for (auto& libName : getLibraryNames (config))
  507. out << " -l" << libName;
  508. out << " $(LDFLAGS)" << newLine;
  509. }
  510. void writeTargetLines (OutputStream& out, const bool useLinuxPackages) const
  511. {
  512. const int n = targets.size();
  513. for (int i = 0; i < n; ++i)
  514. {
  515. if (auto* target = targets.getUnchecked (i))
  516. {
  517. if (target->type == ProjectType::Target::AggregateTarget)
  518. {
  519. StringArray dependencies;
  520. MemoryOutputStream subTargetLines;
  521. for (int j = 0; j < n; ++j)
  522. {
  523. if (i == j) continue;
  524. if (auto* dependency = targets.getUnchecked (j))
  525. {
  526. if (dependency->type != ProjectType::Target::SharedCodeTarget)
  527. {
  528. auto phonyName = dependency->getPhonyName();
  529. subTargetLines << phonyName << " : " << dependency->getBuildProduct() << newLine;
  530. dependencies.add (phonyName);
  531. }
  532. }
  533. }
  534. out << "all : " << dependencies.joinIntoString (" ") << newLine << newLine;
  535. out << subTargetLines.toString() << newLine << newLine;
  536. }
  537. else
  538. {
  539. if (! getProject().getProjectType().isAudioPlugin())
  540. out << "all : " << target->getBuildProduct() << newLine << newLine;
  541. target->writeTargetLine (out, useLinuxPackages);
  542. }
  543. }
  544. }
  545. }
  546. void writeConfig (OutputStream& out, const MakeBuildConfiguration& config) const
  547. {
  548. const String buildDirName ("build");
  549. const String intermediatesDirName (buildDirName + "/intermediate/" + config.getName());
  550. String outputDir (buildDirName);
  551. if (config.getTargetBinaryRelativePathString().isNotEmpty())
  552. {
  553. RelativePath binaryPath (config.getTargetBinaryRelativePathString(), RelativePath::projectFolder);
  554. outputDir = binaryPath.rebased (projectFolder, getTargetFolder(), RelativePath::buildTargetFolder).toUnixStyle();
  555. }
  556. out << "ifeq ($(CONFIG)," << escapeSpaces (config.getName()) << ")" << newLine;
  557. out << " JUCE_BINDIR := " << escapeSpaces (buildDirName) << newLine
  558. << " JUCE_LIBDIR := " << escapeSpaces (buildDirName) << newLine
  559. << " JUCE_OBJDIR := " << escapeSpaces (intermediatesDirName) << newLine
  560. << " JUCE_OUTDIR := " << escapeSpaces (outputDir) << newLine
  561. << newLine
  562. << " ifeq ($(TARGET_ARCH),)" << newLine
  563. << " TARGET_ARCH := " << getArchFlags (config) << newLine
  564. << " endif" << newLine
  565. << newLine;
  566. writeCppFlags (out, config);
  567. for (auto target : targets)
  568. {
  569. StringArray lines = target->getTargetSettings (config);
  570. if (lines.size() > 0)
  571. out << " " << lines.joinIntoString ("\n ") << newLine;
  572. out << newLine;
  573. }
  574. out << " JUCE_CFLAGS += $(JUCE_CPPFLAGS) $(TARGET_ARCH)";
  575. auto cflags = getCFlags (config).joinIntoString (" ");
  576. if (cflags.isNotEmpty())
  577. out << " " << cflags;
  578. out << " $(CFLAGS)" << newLine;
  579. out << " JUCE_CXXFLAGS += $(JUCE_CFLAGS)";
  580. auto cxxflags = getCXXFlags().joinIntoString (" ");
  581. if (cxxflags.isNotEmpty())
  582. out << " " << cxxflags;
  583. out << " $(CXXFLAGS)" << newLine;
  584. writeLinkerFlags (out, config);
  585. out << newLine;
  586. out << " CLEANCMD = rm -rf $(JUCE_OUTDIR)/$(TARGET) $(JUCE_OBJDIR)" << newLine
  587. << "endif" << newLine
  588. << newLine;
  589. }
  590. void writeIncludeLines (OutputStream& out) const
  591. {
  592. const int n = targets.size();
  593. for (int i = 0; i < n; ++i)
  594. {
  595. if (auto* target = targets.getUnchecked (i))
  596. {
  597. if (target->type == ProjectType::Target::AggregateTarget)
  598. continue;
  599. out << "-include $(OBJECTS_" << target->getTargetVarName()
  600. << ":%.o=%.d)" << newLine;
  601. }
  602. }
  603. }
  604. void writeMakefile (OutputStream& out) const
  605. {
  606. out << "# Automatically generated makefile, created by the Projucer" << newLine
  607. << "# Don't edit this file! Your changes will be overwritten when you re-save the Projucer project!" << newLine
  608. << newLine;
  609. out << "# build with \"V=1\" for verbose builds" << newLine
  610. << "ifeq ($(V), 1)" << newLine
  611. << "V_AT =" << newLine
  612. << "else" << newLine
  613. << "V_AT = @" << newLine
  614. << "endif" << newLine
  615. << newLine;
  616. out << "# (this disables dependency generation if multiple architectures are set)" << newLine
  617. << "DEPFLAGS := $(if $(word 2, $(TARGET_ARCH)), , -MMD)" << newLine
  618. << newLine;
  619. out << "ifndef STRIP" << newLine
  620. << " STRIP=strip" << newLine
  621. << "endif" << newLine
  622. << newLine;
  623. out << "ifndef AR" << newLine
  624. << " AR=ar" << newLine
  625. << "endif" << newLine
  626. << newLine;
  627. out << "ifndef CONFIG" << newLine
  628. << " CONFIG=" << escapeSpaces (getConfiguration(0)->getName()) << newLine
  629. << "endif" << newLine
  630. << newLine;
  631. out << "JUCE_ARCH_LABEL := $(shell uname -m)" << newLine
  632. << newLine;
  633. for (ConstConfigIterator config (*this); config.next();)
  634. writeConfig (out, dynamic_cast<const MakeBuildConfiguration&> (*config));
  635. for (auto target : targets)
  636. target->writeObjects (out);
  637. out << getPhonyTargetLine() << newLine << newLine;
  638. auto packages = getPackages();
  639. writeTargetLines (out, ! packages.isEmpty());
  640. for (auto target : targets)
  641. target->addFiles (out);
  642. if (! packages.isEmpty())
  643. {
  644. out << "check-pkg-config:" << newLine
  645. << "\t@command -v pkg-config >/dev/null 2>&1 || "
  646. "{ echo >&2 \"pkg-config not installed. Please, install it.\"; "
  647. "exit 1; }" << newLine
  648. << "\t@pkg-config --print-errors";
  649. for (auto& pkg : packages)
  650. out << " " << pkg;
  651. out << newLine << newLine;
  652. }
  653. out << "clean:" << newLine
  654. << "\t@echo Cleaning " << projectName << newLine
  655. << "\t$(V_AT)$(CLEANCMD)" << newLine
  656. << newLine;
  657. out << "strip:" << newLine
  658. << "\t@echo Stripping " << projectName << newLine
  659. << "\t-$(V_AT)$(STRIP) --strip-unneeded $(JUCE_OUTDIR)/$(TARGET)" << newLine
  660. << newLine;
  661. writeIncludeLines (out);
  662. }
  663. String getArchFlags (const BuildConfiguration& config) const
  664. {
  665. if (auto* makeConfig = dynamic_cast<const MakeBuildConfiguration*> (&config))
  666. return makeConfig->getArchitectureTypeString();
  667. return "-march=native";
  668. }
  669. String getObjectFileFor (const RelativePath& file) const
  670. {
  671. return file.getFileNameWithoutExtension()
  672. + "_" + String::toHexString (file.toUnixStyle().hashCode()) + ".o";
  673. }
  674. String getPhonyTargetLine() const
  675. {
  676. MemoryOutputStream phonyTargetLine;
  677. phonyTargetLine << ".PHONY: clean all";
  678. if (! getProject().getProjectType().isAudioPlugin())
  679. return phonyTargetLine.toString();
  680. for (auto target : targets)
  681. if (target->type != ProjectType::Target::SharedCodeTarget
  682. && target->type != ProjectType::Target::AggregateTarget)
  683. phonyTargetLine << " " << target->getPhonyName();
  684. return phonyTargetLine.toString();
  685. }
  686. friend class CLionProjectExporter;
  687. OwnedArray<MakefileTarget> targets;
  688. JUCE_DECLARE_NON_COPYABLE (MakefileProjectExporter)
  689. };