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.

902 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. return result;
  89. }
  90. StringPairArray getDefines (const BuildConfiguration& config) const
  91. {
  92. StringPairArray result;
  93. auto commonOptionKeys = owner.getAllPreprocessorDefs (config, ProjectType::Target::unspecified).getAllKeys();
  94. auto targetSpecific = owner.getAllPreprocessorDefs (config, type);
  95. for (auto& key : targetSpecific.getAllKeys())
  96. if (! commonOptionKeys.contains (key))
  97. result.set (key, targetSpecific[key]);
  98. return result;
  99. }
  100. StringArray getTargetSettings (const MakeBuildConfiguration& config) const
  101. {
  102. if (type == AggregateTarget)
  103. // the aggregate target should not specify any settings at all!
  104. // it just defines dependencies on the other targets.
  105. return {};
  106. StringArray defines;
  107. auto defs = getDefines (config);
  108. for (auto& key : defs.getAllKeys())
  109. defines.add ("-D" + key + "=" + defs[key]);
  110. StringArray s;
  111. const String cppflagsVarName ("JUCE_CPPFLAGS_" + getTargetVarName());
  112. s.add (cppflagsVarName + " := " + defines.joinIntoString (" "));
  113. auto cflags = getCompilerFlags();
  114. if (! cflags.isEmpty())
  115. s.add ("JUCE_CFLAGS_" + getTargetVarName() + " := " + cflags.joinIntoString (" "));
  116. auto ldflags = getLinkerFlags();
  117. if (! ldflags.isEmpty())
  118. s.add ("JUCE_LDFLAGS_" + getTargetVarName() + " := " + ldflags.joinIntoString (" "));
  119. String targetName (owner.replacePreprocessorTokens (config, config.getTargetBinaryNameString()));
  120. if (owner.projectType.isStaticLibrary())
  121. targetName = getStaticLibbedFilename (targetName);
  122. else if (owner.projectType.isDynamicLibrary())
  123. targetName = getDynamicLibbedFilename (targetName);
  124. else
  125. targetName = targetName.upToLastOccurrenceOf (".", false, false) + getTargetFileSuffix();
  126. s.add ("JUCE_TARGET_" + getTargetVarName() + String (" := ") + escapeSpaces (targetName));
  127. return s;
  128. }
  129. String getTargetFileSuffix() const
  130. {
  131. switch (type)
  132. {
  133. case VSTPlugIn: return ".so";
  134. case VST3PlugIn: return ".vst3";
  135. case SharedCodeTarget: return ".a";
  136. default: break;
  137. }
  138. return {};
  139. }
  140. String getTargetVarName() const
  141. {
  142. return String (getName()).toUpperCase().replaceCharacter (L' ', L'_');
  143. }
  144. void writeObjects (OutputStream& out) const
  145. {
  146. Array<RelativePath> targetFiles;
  147. for (int i = 0; i < owner.getAllGroups().size(); ++i)
  148. findAllFilesToCompile (owner.getAllGroups().getReference(i), targetFiles);
  149. out << "OBJECTS_" + getTargetVarName() + String (" := \\") << newLine;
  150. for (int i = 0; i < targetFiles.size(); ++i)
  151. out << " $(JUCE_OBJDIR)/" << escapeSpaces (owner.getObjectFileFor (targetFiles.getReference(i))) << " \\" << newLine;
  152. out << newLine;
  153. }
  154. void findAllFilesToCompile (const Project::Item& projectItem, Array<RelativePath>& results) const
  155. {
  156. if (projectItem.isGroup())
  157. {
  158. for (int i = 0; i < projectItem.getNumChildren(); ++i)
  159. findAllFilesToCompile (projectItem.getChild(i), results);
  160. }
  161. else
  162. {
  163. if (projectItem.shouldBeCompiled())
  164. {
  165. const Type targetType = (owner.getProject().getProjectType().isAudioPlugin() ? type : SharedCodeTarget);
  166. const File f = projectItem.getFile();
  167. RelativePath relativePath (f, owner.getTargetFolder(), RelativePath::buildTargetFolder);
  168. if (owner.shouldFileBeCompiledByDefault (relativePath)
  169. && owner.getProject().getTargetTypeFromFilePath (f, true) == targetType)
  170. results.add (relativePath);
  171. }
  172. }
  173. }
  174. void addFiles (OutputStream& out)
  175. {
  176. Array<RelativePath> targetFiles;
  177. for (int i = 0; i < owner.getAllGroups().size(); ++i)
  178. findAllFilesToCompile (owner.getAllGroups().getReference(i), targetFiles);
  179. const String cppflagsVarName = String ("JUCE_CPPFLAGS_") + getTargetVarName();
  180. const String cflagsVarName = String ("JUCE_CFLAGS_") + getTargetVarName();
  181. for (int i = 0; i < targetFiles.size(); ++i)
  182. {
  183. jassert (targetFiles.getReference(i).getRoot() == RelativePath::buildTargetFolder);
  184. out << "$(JUCE_OBJDIR)/" << escapeSpaces (owner.getObjectFileFor (targetFiles.getReference(i)))
  185. << ": " << escapeSpaces (targetFiles.getReference(i).toUnixStyle()) << newLine
  186. << "\t-$(V_AT)mkdir -p $(JUCE_OBJDIR)" << newLine
  187. << "\t@echo \"Compiling " << targetFiles.getReference(i).getFileName() << "\"" << newLine
  188. << (targetFiles.getReference(i).hasFileExtension ("c;s;S") ? "\t$(V_AT)$(CC) $(JUCE_CFLAGS) " : "\t$(V_AT)$(CXX) $(JUCE_CXXFLAGS) ")
  189. << "$(" << cppflagsVarName << ") $(" << cflagsVarName << ") -o \"$@\" -c \"$<\""
  190. << newLine << newLine;
  191. }
  192. }
  193. String getBuildProduct() const
  194. {
  195. return String ("$(JUCE_OUTDIR)/$(JUCE_TARGET_") + getTargetVarName() + String (")");
  196. }
  197. String getPhonyName() const
  198. {
  199. return String (getName()).upToFirstOccurrenceOf (" ", false, false);
  200. }
  201. void writeTargetLine (OutputStream& out, const bool useLinuxPackages)
  202. {
  203. jassert (type != AggregateTarget);
  204. out << getBuildProduct() << " : "
  205. << ((useLinuxPackages) ? "check-pkg-config " : "")
  206. << "$(OBJECTS_" << getTargetVarName() << ") $(RESOURCES)";
  207. if (type != SharedCodeTarget && owner.shouldBuildTargetType (SharedCodeTarget))
  208. out << " $(JUCE_OUTDIR)/$(JUCE_TARGET_SHARED_CODE)";
  209. out << newLine << "\t@echo Linking \"" << owner.projectName << " - " << getName() << "\"" << newLine
  210. << "\t-$(V_AT)mkdir -p $(JUCE_BINDIR)" << newLine
  211. << "\t-$(V_AT)mkdir -p $(JUCE_LIBDIR)" << newLine
  212. << "\t-$(V_AT)mkdir -p $(JUCE_OUTDIR)" << newLine;
  213. if (owner.projectType.isStaticLibrary() || type == SharedCodeTarget)
  214. out << "\t$(V_AT)$(AR) -rcs " << getBuildProduct()
  215. << " $(OBJECTS_" << getTargetVarName() << ")" << newLine;
  216. else
  217. {
  218. out << "\t$(V_AT)$(CXX) -o " << getBuildProduct()
  219. << " $(OBJECTS_" << getTargetVarName() << ") ";
  220. if (owner.shouldBuildTargetType (SharedCodeTarget))
  221. out << "$(JUCE_OUTDIR)/$(JUCE_TARGET_SHARED_CODE) ";
  222. out << "$(JUCE_LDFLAGS) ";
  223. if (getTargetFileType() == sharedLibraryOrDLL || getTargetFileType() == pluginBundle
  224. || type == GUIApp || type == StandalonePlugIn)
  225. out << "$(JUCE_LDFLAGS_" << getTargetVarName() << ") ";
  226. out << "$(RESOURCES) $(TARGET_ARCH)" << newLine;
  227. }
  228. out << newLine;
  229. }
  230. const MakefileProjectExporter& owner;
  231. };
  232. //==============================================================================
  233. static const char* getNameLinux() { return "Linux Makefile"; }
  234. static const char* getValueTreeTypeName() { return "LINUX_MAKE"; }
  235. Value getExtraPkgConfig() { return getSetting (Ids::linuxExtraPkgConfig); }
  236. String getExtraPkgConfigString() const { return getSettingString (Ids::linuxExtraPkgConfig); }
  237. static MakefileProjectExporter* createForSettings (Project& project, const ValueTree& settings)
  238. {
  239. if (settings.hasType (getValueTreeTypeName()))
  240. return new MakefileProjectExporter (project, settings);
  241. return nullptr;
  242. }
  243. //==============================================================================
  244. MakefileProjectExporter (Project& p, const ValueTree& t) : ProjectExporter (p, t)
  245. {
  246. name = getNameLinux();
  247. if (getTargetLocationString().isEmpty())
  248. getTargetLocationValue() = getDefaultBuildsRootFolder() + "LinuxMakefile";
  249. }
  250. //==============================================================================
  251. bool canLaunchProject() override { return false; }
  252. bool launchProject() override { return false; }
  253. bool usesMMFiles() const override { return false; }
  254. bool canCopeWithDuplicateFiles() override { return false; }
  255. bool supportsUserDefinedConfigurations() const override { return true; }
  256. bool isXcode() const override { return false; }
  257. bool isVisualStudio() const override { return false; }
  258. bool isCodeBlocks() const override { return false; }
  259. bool isMakefile() const override { return true; }
  260. bool isAndroidStudio() const override { return false; }
  261. bool isCLion() const override { return false; }
  262. bool isAndroid() const override { return false; }
  263. bool isWindows() const override { return false; }
  264. bool isLinux() const override { return true; }
  265. bool isOSX() const override { return false; }
  266. bool isiOS() const override { return false; }
  267. bool supportsTargetType (ProjectType::Target::Type type) const override
  268. {
  269. switch (type)
  270. {
  271. case ProjectType::Target::GUIApp:
  272. case ProjectType::Target::ConsoleApp:
  273. case ProjectType::Target::StaticLibrary:
  274. case ProjectType::Target::SharedCodeTarget:
  275. case ProjectType::Target::AggregateTarget:
  276. case ProjectType::Target::VSTPlugIn:
  277. case ProjectType::Target::StandalonePlugIn:
  278. case ProjectType::Target::DynamicLibrary:
  279. return true;
  280. default:
  281. break;
  282. }
  283. return false;
  284. }
  285. void createExporterProperties (PropertyListBuilder& properties) override
  286. {
  287. properties.add (new TextPropertyComponent (getExtraPkgConfig(), "pkg-config libraries", 8192, false),
  288. "Extra pkg-config libraries for you application. Each package should be space separated.");
  289. }
  290. //==============================================================================
  291. bool anyTargetIsSharedLibrary() const
  292. {
  293. for (auto* target : targets)
  294. {
  295. const ProjectType::Target::TargetFileType fileType = target->getTargetFileType();
  296. if (fileType == ProjectType::Target::sharedLibraryOrDLL
  297. || fileType == ProjectType::Target::pluginBundle)
  298. return true;
  299. }
  300. return false;
  301. }
  302. //==============================================================================
  303. void create (const OwnedArray<LibraryModule>&) const override
  304. {
  305. MemoryOutputStream mo;
  306. writeMakefile (mo);
  307. overwriteFileIfDifferentOrThrow (getTargetFolder().getChildFile ("Makefile"), mo);
  308. }
  309. //==============================================================================
  310. void addPlatformSpecificSettingsForProjectType (const ProjectType&) override
  311. {
  312. callForAllSupportedTargets ([this] (ProjectType::Target::Type targetType)
  313. {
  314. if (MakefileTarget* target = new MakefileTarget (targetType, *this))
  315. {
  316. if (targetType == ProjectType::Target::AggregateTarget)
  317. targets.insert (0, target);
  318. else
  319. targets.add (target);
  320. }
  321. });
  322. // If you hit this assert, you tried to generate a project for an exporter
  323. // that does not support any of your targets!
  324. jassert (targets.size() > 0);
  325. }
  326. //==============================================================================
  327. void initialiseDependencyPathValues() override
  328. {
  329. vst3Path.referTo (Value (new DependencyPathValueSource (getSetting (Ids::vst3Folder),
  330. Ids::vst3Path,
  331. TargetOS::linux)));
  332. }
  333. private:
  334. StringPairArray getDefines (const BuildConfiguration& config) const
  335. {
  336. StringPairArray result;
  337. result.set ("LINUX", "1");
  338. if (config.isDebug())
  339. {
  340. result.set ("DEBUG", "1");
  341. result.set ("_DEBUG", "1");
  342. }
  343. else
  344. {
  345. result.set ("NDEBUG", "1");
  346. }
  347. result = mergePreprocessorDefs (result, getAllPreprocessorDefs (config, ProjectType::Target::unspecified));
  348. return result;
  349. }
  350. StringArray getPackages() const
  351. {
  352. StringArray packages;
  353. packages.addTokens (getExtraPkgConfigString(), " ", "\"'");
  354. packages.removeEmptyStrings();
  355. packages.addArray (linuxPackages);
  356. if (isWebBrowserComponentEnabled())
  357. {
  358. packages.add ("webkit2gtk-4.0");
  359. packages.add ("gtk+-x11-3.0");
  360. }
  361. packages.removeDuplicates (false);
  362. return packages;
  363. }
  364. String getPreprocessorPkgConfigFlags() const
  365. {
  366. auto packages = getPackages();
  367. if (packages.size() > 0)
  368. return "$(shell pkg-config --cflags " + packages.joinIntoString (" ") + ")";
  369. return {};
  370. }
  371. String getLinkerPkgConfigFlags() const
  372. {
  373. auto packages = getPackages();
  374. if (packages.size() > 0)
  375. return "$(shell pkg-config --libs " + packages.joinIntoString (" ") + ")";
  376. return {};
  377. }
  378. StringArray getCPreprocessorFlags (const BuildConfiguration&) const
  379. {
  380. StringArray result;
  381. if (linuxLibs.contains("pthread"))
  382. result.add ("-pthread");
  383. return result;
  384. }
  385. StringArray getCFlags (const BuildConfiguration& config) const
  386. {
  387. StringArray result;
  388. if (anyTargetIsSharedLibrary())
  389. result.add ("-fPIC");
  390. if (config.isDebug())
  391. {
  392. result.add ("-g");
  393. result.add ("-ggdb");
  394. }
  395. result.add ("-O" + config.getGCCOptimisationFlag());
  396. if (config.isLinkTimeOptimisationEnabled())
  397. result.add ("-flto");
  398. auto extra = replacePreprocessorTokens (config, getExtraCompilerFlagsString()).trim();
  399. if (extra.isNotEmpty())
  400. result.add (extra);
  401. return result;
  402. }
  403. StringArray getCXXFlags() const
  404. {
  405. StringArray result;
  406. auto cppStandard = project.getCppStandardValue().toString();
  407. if (cppStandard == "latest")
  408. cppStandard = "1z";
  409. cppStandard = "-std=" + String (shouldUseGNUExtensions() ? "gnu++" : "c++") + cppStandard;
  410. result.add (cppStandard);
  411. return result;
  412. }
  413. StringArray getHeaderSearchPaths (const BuildConfiguration& config) const
  414. {
  415. StringArray searchPaths (extraSearchPaths);
  416. searchPaths.addArray (config.getHeaderSearchPaths());
  417. searchPaths = getCleanedStringArray (searchPaths);
  418. StringArray result;
  419. for (auto& path : searchPaths)
  420. result.add (FileHelpers::unixStylePath (replacePreprocessorTokens (config, path)));
  421. return result;
  422. }
  423. StringArray getLibraryNames (const BuildConfiguration& config) const
  424. {
  425. StringArray result (linuxLibs);
  426. StringArray libraries;
  427. libraries.addTokens (getExternalLibrariesString(), ";", "\"'");
  428. libraries.removeEmptyStrings();
  429. for (auto& lib : libraries)
  430. result.add (replacePreprocessorTokens (config, lib).trim());
  431. return result;
  432. }
  433. StringArray getLibrarySearchPaths (const BuildConfiguration& config) const
  434. {
  435. auto result = getSearchPathsFromString (config.getLibrarySearchPathString());
  436. for (auto path : moduleLibSearchPaths)
  437. result.add (path + "/" + config.getModuleLibraryArchName());
  438. return result;
  439. }
  440. StringArray getLinkerFlags (const BuildConfiguration& config) const
  441. {
  442. StringArray result (makefileExtraLinkerFlags);
  443. if (! config.isDebug())
  444. result.add ("-fvisibility=hidden");
  445. if (config.isLinkTimeOptimisationEnabled())
  446. result.add ("-flto");
  447. auto extraFlags = getExtraLinkerFlagsString().trim();
  448. if (extraFlags.isNotEmpty())
  449. result.add (replacePreprocessorTokens (config, extraFlags));
  450. return result;
  451. }
  452. bool isWebBrowserComponentEnabled() const
  453. {
  454. static String guiExtrasModule ("juce_gui_extra");
  455. return (project.getModules().isModuleEnabled (guiExtrasModule)
  456. && project.isConfigFlagEnabled ("JUCE_WEB_BROWSER", true));
  457. }
  458. //==============================================================================
  459. void writeDefineFlags (OutputStream& out, const MakeBuildConfiguration& config) const
  460. {
  461. out << createGCCPreprocessorFlags (mergePreprocessorDefs (getDefines (config), getAllPreprocessorDefs (config, ProjectType::Target::unspecified)));
  462. }
  463. void writePkgConfigFlags (OutputStream& out) const
  464. {
  465. auto flags = getPreprocessorPkgConfigFlags();
  466. if (flags.isNotEmpty())
  467. out << " " << flags;
  468. }
  469. void writeCPreprocessorFlags (OutputStream& out, const BuildConfiguration& config) const
  470. {
  471. auto flags = getCPreprocessorFlags (config);
  472. if (! flags.isEmpty())
  473. out << " " << flags.joinIntoString (" ");
  474. }
  475. void writeHeaderPathFlags (OutputStream& out, const BuildConfiguration& config) const
  476. {
  477. for (auto& path : getHeaderSearchPaths (config))
  478. out << " -I" << escapeSpaces (path).replace ("~", "$(HOME)");
  479. }
  480. void writeCppFlags (OutputStream& out, const MakeBuildConfiguration& config) const
  481. {
  482. out << " JUCE_CPPFLAGS := $(DEPFLAGS)";
  483. writeDefineFlags (out, config);
  484. writePkgConfigFlags (out);
  485. writeCPreprocessorFlags (out, config);
  486. writeHeaderPathFlags (out, config);
  487. out << " $(CPPFLAGS)" << newLine;
  488. }
  489. void writeLinkerFlags (OutputStream& out, const BuildConfiguration& config) const
  490. {
  491. out << " JUCE_LDFLAGS += $(TARGET_ARCH) -L$(JUCE_BINDIR) -L$(JUCE_LIBDIR)";
  492. for (auto path : getLibrarySearchPaths (config))
  493. out << " -L" << escapeSpaces (path).replace ("~", "$(HOME)");
  494. auto pkgConfigFlags = getLinkerPkgConfigFlags();
  495. if (pkgConfigFlags.isNotEmpty())
  496. out << " " << getLinkerPkgConfigFlags();
  497. auto linkerFlags = getLinkerFlags (config).joinIntoString (" ");
  498. if (linkerFlags.isNotEmpty())
  499. out << " " << linkerFlags;
  500. for (auto& libName : getLibraryNames (config))
  501. out << " -l" << libName;
  502. out << " $(LDFLAGS)" << newLine;
  503. }
  504. void writeTargetLines (OutputStream& out, const bool useLinuxPackages) const
  505. {
  506. const int n = targets.size();
  507. for (int i = 0; i < n; ++i)
  508. {
  509. if (auto* target = targets.getUnchecked (i))
  510. {
  511. if (target->type == ProjectType::Target::AggregateTarget)
  512. {
  513. StringArray dependencies;
  514. MemoryOutputStream subTargetLines;
  515. for (int j = 0; j < n; ++j)
  516. {
  517. if (i == j) continue;
  518. if (auto* dependency = targets.getUnchecked (j))
  519. {
  520. if (dependency->type != ProjectType::Target::SharedCodeTarget)
  521. {
  522. auto phonyName = dependency->getPhonyName();
  523. subTargetLines << phonyName << " : " << dependency->getBuildProduct() << newLine;
  524. dependencies.add (phonyName);
  525. }
  526. }
  527. }
  528. out << "all : " << dependencies.joinIntoString (" ") << newLine << newLine;
  529. out << subTargetLines.toString() << newLine << newLine;
  530. }
  531. else
  532. {
  533. if (! getProject().getProjectType().isAudioPlugin())
  534. out << "all : " << target->getBuildProduct() << newLine << newLine;
  535. target->writeTargetLine (out, useLinuxPackages);
  536. }
  537. }
  538. }
  539. }
  540. void writeConfig (OutputStream& out, const MakeBuildConfiguration& config) const
  541. {
  542. const String buildDirName ("build");
  543. const String intermediatesDirName (buildDirName + "/intermediate/" + config.getName());
  544. String outputDir (buildDirName);
  545. if (config.getTargetBinaryRelativePathString().isNotEmpty())
  546. {
  547. RelativePath binaryPath (config.getTargetBinaryRelativePathString(), RelativePath::projectFolder);
  548. outputDir = binaryPath.rebased (projectFolder, getTargetFolder(), RelativePath::buildTargetFolder).toUnixStyle();
  549. }
  550. out << "ifeq ($(CONFIG)," << escapeSpaces (config.getName()) << ")" << newLine;
  551. out << " JUCE_BINDIR := " << escapeSpaces (buildDirName) << newLine
  552. << " JUCE_LIBDIR := " << escapeSpaces (buildDirName) << newLine
  553. << " JUCE_OBJDIR := " << escapeSpaces (intermediatesDirName) << newLine
  554. << " JUCE_OUTDIR := " << escapeSpaces (outputDir) << newLine
  555. << newLine
  556. << " ifeq ($(TARGET_ARCH),)" << newLine
  557. << " TARGET_ARCH := " << getArchFlags (config) << newLine
  558. << " endif" << newLine
  559. << newLine;
  560. writeCppFlags (out, config);
  561. for (auto target : targets)
  562. {
  563. StringArray lines = target->getTargetSettings (config);
  564. if (lines.size() > 0)
  565. out << " " << lines.joinIntoString ("\n ") << newLine;
  566. out << newLine;
  567. }
  568. out << " JUCE_CFLAGS += $(JUCE_CPPFLAGS) $(TARGET_ARCH)";
  569. auto cflags = getCFlags (config).joinIntoString (" ");
  570. if (cflags.isNotEmpty())
  571. out << " " << cflags;
  572. out << " $(CFLAGS)" << newLine;
  573. out << " JUCE_CXXFLAGS += $(JUCE_CFLAGS)";
  574. auto cxxflags = getCXXFlags().joinIntoString (" ");
  575. if (cxxflags.isNotEmpty())
  576. out << " " << cxxflags;
  577. out << " $(CXXFLAGS)" << newLine;
  578. writeLinkerFlags (out, config);
  579. out << newLine;
  580. out << " CLEANCMD = rm -rf $(JUCE_OUTDIR)/$(TARGET) $(JUCE_OBJDIR)" << newLine
  581. << "endif" << newLine
  582. << newLine;
  583. }
  584. void writeIncludeLines (OutputStream& out) const
  585. {
  586. const int n = targets.size();
  587. for (int i = 0; i < n; ++i)
  588. {
  589. if (auto* target = targets.getUnchecked (i))
  590. {
  591. if (target->type == ProjectType::Target::AggregateTarget)
  592. continue;
  593. out << "-include $(OBJECTS_" << target->getTargetVarName()
  594. << ":%.o=%.d)" << newLine;
  595. }
  596. }
  597. }
  598. void writeMakefile (OutputStream& out) const
  599. {
  600. out << "# Automatically generated makefile, created by the Projucer" << newLine
  601. << "# Don't edit this file! Your changes will be overwritten when you re-save the Projucer project!" << newLine
  602. << newLine;
  603. out << "# build with \"V=1\" for verbose builds" << newLine
  604. << "ifeq ($(V), 1)" << newLine
  605. << "V_AT =" << newLine
  606. << "else" << newLine
  607. << "V_AT = @" << newLine
  608. << "endif" << newLine
  609. << newLine;
  610. out << "# (this disables dependency generation if multiple architectures are set)" << newLine
  611. << "DEPFLAGS := $(if $(word 2, $(TARGET_ARCH)), , -MMD)" << newLine
  612. << newLine;
  613. out << "ifndef STRIP" << newLine
  614. << " STRIP=strip" << newLine
  615. << "endif" << newLine
  616. << newLine;
  617. out << "ifndef AR" << newLine
  618. << " AR=ar" << newLine
  619. << "endif" << newLine
  620. << newLine;
  621. out << "ifndef CONFIG" << newLine
  622. << " CONFIG=" << escapeSpaces (getConfiguration(0)->getName()) << newLine
  623. << "endif" << newLine
  624. << newLine;
  625. out << "JUCE_ARCH_LABEL := $(shell uname -m)" << newLine
  626. << newLine;
  627. for (ConstConfigIterator config (*this); config.next();)
  628. writeConfig (out, dynamic_cast<const MakeBuildConfiguration&> (*config));
  629. for (auto target : targets)
  630. target->writeObjects (out);
  631. out << getPhonyTargetLine() << newLine << newLine;
  632. auto packages = getPackages();
  633. writeTargetLines (out, ! packages.isEmpty());
  634. for (auto target : targets)
  635. target->addFiles (out);
  636. if (! packages.isEmpty())
  637. {
  638. out << "check-pkg-config:" << newLine
  639. << "\t@command -v pkg-config >/dev/null 2>&1 || "
  640. "{ echo >&2 \"pkg-config not installed. Please, install it.\"; "
  641. "exit 1; }" << newLine
  642. << "\t@pkg-config --print-errors";
  643. for (auto& pkg : packages)
  644. out << " " << pkg;
  645. out << newLine << newLine;
  646. }
  647. out << "clean:" << newLine
  648. << "\t@echo Cleaning " << projectName << newLine
  649. << "\t$(V_AT)$(CLEANCMD)" << newLine
  650. << newLine;
  651. out << "strip:" << newLine
  652. << "\t@echo Stripping " << projectName << newLine
  653. << "\t-$(V_AT)$(STRIP) --strip-unneeded $(JUCE_OUTDIR)/$(TARGET)" << newLine
  654. << newLine;
  655. writeIncludeLines (out);
  656. }
  657. String getArchFlags (const BuildConfiguration& config) const
  658. {
  659. if (const MakeBuildConfiguration* makeConfig = dynamic_cast<const MakeBuildConfiguration*> (&config))
  660. if (! makeConfig->getArchitectureTypeVar().isVoid())
  661. return makeConfig->getArchitectureTypeVar();
  662. return "-march=native";
  663. }
  664. String getObjectFileFor (const RelativePath& file) const
  665. {
  666. return file.getFileNameWithoutExtension()
  667. + "_" + String::toHexString (file.toUnixStyle().hashCode()) + ".o";
  668. }
  669. String getPhonyTargetLine() const
  670. {
  671. MemoryOutputStream phonyTargetLine;
  672. phonyTargetLine << ".PHONY: clean all";
  673. if (! getProject().getProjectType().isAudioPlugin())
  674. return phonyTargetLine.toString();
  675. for (auto target : targets)
  676. if (target->type != ProjectType::Target::SharedCodeTarget
  677. && target->type != ProjectType::Target::AggregateTarget)
  678. phonyTargetLine << " " << target->getPhonyName();
  679. return phonyTargetLine.toString();
  680. }
  681. friend class CLionProjectExporter;
  682. OwnedArray<MakefileTarget> targets;
  683. JUCE_DECLARE_NON_COPYABLE (MakefileProjectExporter)
  684. };