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.

904 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:
  134. case DynamicLibrary: return ".so";
  135. case VST3PlugIn: return ".vst3";
  136. case SharedCodeTarget:
  137. case StaticLibrary: return ".a";
  138. default: break;
  139. }
  140. return {};
  141. }
  142. String getTargetVarName() const
  143. {
  144. return String (getName()).toUpperCase().replaceCharacter (L' ', L'_');
  145. }
  146. void writeObjects (OutputStream& out) const
  147. {
  148. Array<RelativePath> targetFiles;
  149. for (int i = 0; i < owner.getAllGroups().size(); ++i)
  150. findAllFilesToCompile (owner.getAllGroups().getReference(i), targetFiles);
  151. out << "OBJECTS_" + getTargetVarName() + String (" := \\") << newLine;
  152. for (int i = 0; i < targetFiles.size(); ++i)
  153. out << " $(JUCE_OBJDIR)/" << escapeSpaces (owner.getObjectFileFor (targetFiles.getReference(i))) << " \\" << newLine;
  154. out << newLine;
  155. }
  156. void findAllFilesToCompile (const Project::Item& projectItem, Array<RelativePath>& results) const
  157. {
  158. if (projectItem.isGroup())
  159. {
  160. for (int i = 0; i < projectItem.getNumChildren(); ++i)
  161. findAllFilesToCompile (projectItem.getChild(i), results);
  162. }
  163. else
  164. {
  165. if (projectItem.shouldBeCompiled())
  166. {
  167. const Type targetType = (owner.getProject().getProjectType().isAudioPlugin() ? type : SharedCodeTarget);
  168. const File f = projectItem.getFile();
  169. RelativePath relativePath (f, owner.getTargetFolder(), RelativePath::buildTargetFolder);
  170. if (owner.shouldFileBeCompiledByDefault (relativePath)
  171. && owner.getProject().getTargetTypeFromFilePath (f, true) == targetType)
  172. results.add (relativePath);
  173. }
  174. }
  175. }
  176. void addFiles (OutputStream& out)
  177. {
  178. Array<RelativePath> targetFiles;
  179. for (int i = 0; i < owner.getAllGroups().size(); ++i)
  180. findAllFilesToCompile (owner.getAllGroups().getReference(i), targetFiles);
  181. const String cppflagsVarName = String ("JUCE_CPPFLAGS_") + getTargetVarName();
  182. const String cflagsVarName = String ("JUCE_CFLAGS_") + getTargetVarName();
  183. for (int i = 0; i < targetFiles.size(); ++i)
  184. {
  185. jassert (targetFiles.getReference(i).getRoot() == RelativePath::buildTargetFolder);
  186. out << "$(JUCE_OBJDIR)/" << escapeSpaces (owner.getObjectFileFor (targetFiles.getReference(i)))
  187. << ": " << escapeSpaces (targetFiles.getReference(i).toUnixStyle()) << newLine
  188. << "\t-$(V_AT)mkdir -p $(JUCE_OBJDIR)" << newLine
  189. << "\t@echo \"Compiling " << targetFiles.getReference(i).getFileName() << "\"" << newLine
  190. << (targetFiles.getReference(i).hasFileExtension ("c;s;S") ? "\t$(V_AT)$(CC) $(JUCE_CFLAGS) " : "\t$(V_AT)$(CXX) $(JUCE_CXXFLAGS) ")
  191. << "$(" << cppflagsVarName << ") $(" << cflagsVarName << ") -o \"$@\" -c \"$<\""
  192. << newLine << newLine;
  193. }
  194. }
  195. String getBuildProduct() const
  196. {
  197. return String ("$(JUCE_OUTDIR)/$(JUCE_TARGET_") + getTargetVarName() + String (")");
  198. }
  199. String getPhonyName() const
  200. {
  201. return String (getName()).upToFirstOccurrenceOf (" ", false, false);
  202. }
  203. void writeTargetLine (OutputStream& out, const bool useLinuxPackages)
  204. {
  205. jassert (type != AggregateTarget);
  206. out << getBuildProduct() << " : "
  207. << ((useLinuxPackages) ? "check-pkg-config " : "")
  208. << "$(OBJECTS_" << getTargetVarName() << ") $(RESOURCES)";
  209. if (type != SharedCodeTarget && owner.shouldBuildTargetType (SharedCodeTarget))
  210. out << " $(JUCE_OUTDIR)/$(JUCE_TARGET_SHARED_CODE)";
  211. out << newLine << "\t@echo Linking \"" << owner.projectName << " - " << getName() << "\"" << newLine
  212. << "\t-$(V_AT)mkdir -p $(JUCE_BINDIR)" << newLine
  213. << "\t-$(V_AT)mkdir -p $(JUCE_LIBDIR)" << newLine
  214. << "\t-$(V_AT)mkdir -p $(JUCE_OUTDIR)" << newLine;
  215. if (owner.projectType.isStaticLibrary() || type == SharedCodeTarget)
  216. out << "\t$(V_AT)$(AR) -rcs " << getBuildProduct()
  217. << " $(OBJECTS_" << getTargetVarName() << ")" << newLine;
  218. else
  219. {
  220. out << "\t$(V_AT)$(CXX) -o " << getBuildProduct()
  221. << " $(OBJECTS_" << getTargetVarName() << ") ";
  222. if (owner.shouldBuildTargetType (SharedCodeTarget))
  223. out << "$(JUCE_OUTDIR)/$(JUCE_TARGET_SHARED_CODE) ";
  224. out << "$(JUCE_LDFLAGS) ";
  225. if (getTargetFileType() == sharedLibraryOrDLL || getTargetFileType() == pluginBundle
  226. || type == GUIApp || type == StandalonePlugIn)
  227. out << "$(JUCE_LDFLAGS_" << getTargetVarName() << ") ";
  228. out << "$(RESOURCES) $(TARGET_ARCH)" << newLine;
  229. }
  230. out << newLine;
  231. }
  232. const MakefileProjectExporter& owner;
  233. };
  234. //==============================================================================
  235. static const char* getNameLinux() { return "Linux Makefile"; }
  236. static const char* getValueTreeTypeName() { return "LINUX_MAKE"; }
  237. Value getExtraPkgConfig() { return getSetting (Ids::linuxExtraPkgConfig); }
  238. String getExtraPkgConfigString() const { return getSettingString (Ids::linuxExtraPkgConfig); }
  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) : ProjectExporter (p, t)
  247. {
  248. name = getNameLinux();
  249. if (getTargetLocationString().isEmpty())
  250. getTargetLocationValue() = getDefaultBuildsRootFolder() + "LinuxMakefile";
  251. }
  252. //==============================================================================
  253. bool canLaunchProject() override { return false; }
  254. bool launchProject() override { return false; }
  255. bool usesMMFiles() const override { return false; }
  256. bool canCopeWithDuplicateFiles() override { return false; }
  257. bool supportsUserDefinedConfigurations() const override { return true; }
  258. bool isXcode() const override { return false; }
  259. bool isVisualStudio() const override { return false; }
  260. bool isCodeBlocks() const override { return false; }
  261. bool isMakefile() const override { return true; }
  262. bool isAndroidStudio() const override { return false; }
  263. bool isCLion() const override { return false; }
  264. bool isAndroid() const override { return false; }
  265. bool isWindows() const override { return false; }
  266. bool isLinux() const override { return true; }
  267. bool isOSX() const override { return false; }
  268. bool isiOS() const override { return false; }
  269. bool supportsTargetType (ProjectType::Target::Type type) const override
  270. {
  271. switch (type)
  272. {
  273. case ProjectType::Target::GUIApp:
  274. case ProjectType::Target::ConsoleApp:
  275. case ProjectType::Target::StaticLibrary:
  276. case ProjectType::Target::SharedCodeTarget:
  277. case ProjectType::Target::AggregateTarget:
  278. case ProjectType::Target::VSTPlugIn:
  279. case ProjectType::Target::StandalonePlugIn:
  280. case ProjectType::Target::DynamicLibrary:
  281. return true;
  282. default:
  283. break;
  284. }
  285. return false;
  286. }
  287. void createExporterProperties (PropertyListBuilder& properties) override
  288. {
  289. properties.add (new TextPropertyComponent (getExtraPkgConfig(), "pkg-config libraries", 8192, false),
  290. "Extra pkg-config libraries for you application. Each package should be space separated.");
  291. }
  292. //==============================================================================
  293. bool anyTargetIsSharedLibrary() const
  294. {
  295. for (auto* target : targets)
  296. {
  297. const ProjectType::Target::TargetFileType fileType = target->getTargetFileType();
  298. if (fileType == ProjectType::Target::sharedLibraryOrDLL
  299. || fileType == ProjectType::Target::pluginBundle)
  300. return true;
  301. }
  302. return false;
  303. }
  304. //==============================================================================
  305. void create (const OwnedArray<LibraryModule>&) const override
  306. {
  307. MemoryOutputStream mo;
  308. writeMakefile (mo);
  309. overwriteFileIfDifferentOrThrow (getTargetFolder().getChildFile ("Makefile"), mo);
  310. }
  311. //==============================================================================
  312. void addPlatformSpecificSettingsForProjectType (const ProjectType&) override
  313. {
  314. callForAllSupportedTargets ([this] (ProjectType::Target::Type targetType)
  315. {
  316. if (MakefileTarget* target = new MakefileTarget (targetType, *this))
  317. {
  318. if (targetType == ProjectType::Target::AggregateTarget)
  319. targets.insert (0, target);
  320. else
  321. targets.add (target);
  322. }
  323. });
  324. // If you hit this assert, you tried to generate a project for an exporter
  325. // that does not support any of your targets!
  326. jassert (targets.size() > 0);
  327. }
  328. //==============================================================================
  329. void initialiseDependencyPathValues() override
  330. {
  331. vst3Path.referTo (Value (new DependencyPathValueSource (getSetting (Ids::vst3Folder),
  332. Ids::vst3Path,
  333. TargetOS::linux)));
  334. }
  335. private:
  336. StringPairArray getDefines (const BuildConfiguration& config) const
  337. {
  338. StringPairArray result;
  339. result.set ("LINUX", "1");
  340. if (config.isDebug())
  341. {
  342. result.set ("DEBUG", "1");
  343. result.set ("_DEBUG", "1");
  344. }
  345. else
  346. {
  347. result.set ("NDEBUG", "1");
  348. }
  349. result = mergePreprocessorDefs (result, getAllPreprocessorDefs (config, ProjectType::Target::unspecified));
  350. return result;
  351. }
  352. StringArray getPackages() const
  353. {
  354. StringArray packages;
  355. packages.addTokens (getExtraPkgConfigString(), " ", "\"'");
  356. packages.removeEmptyStrings();
  357. packages.addArray (linuxPackages);
  358. if (isWebBrowserComponentEnabled())
  359. {
  360. packages.add ("webkit2gtk-4.0");
  361. packages.add ("gtk+-x11-3.0");
  362. }
  363. packages.removeDuplicates (false);
  364. return packages;
  365. }
  366. String getPreprocessorPkgConfigFlags() const
  367. {
  368. auto packages = getPackages();
  369. if (packages.size() > 0)
  370. return "$(shell pkg-config --cflags " + packages.joinIntoString (" ") + ")";
  371. return {};
  372. }
  373. String getLinkerPkgConfigFlags() const
  374. {
  375. auto packages = getPackages();
  376. if (packages.size() > 0)
  377. return "$(shell pkg-config --libs " + packages.joinIntoString (" ") + ")";
  378. return {};
  379. }
  380. StringArray getCPreprocessorFlags (const BuildConfiguration&) const
  381. {
  382. StringArray result;
  383. if (linuxLibs.contains("pthread"))
  384. result.add ("-pthread");
  385. return result;
  386. }
  387. StringArray getCFlags (const BuildConfiguration& config) const
  388. {
  389. StringArray result;
  390. if (anyTargetIsSharedLibrary())
  391. result.add ("-fPIC");
  392. if (config.isDebug())
  393. {
  394. result.add ("-g");
  395. result.add ("-ggdb");
  396. }
  397. result.add ("-O" + config.getGCCOptimisationFlag());
  398. if (config.isLinkTimeOptimisationEnabled())
  399. result.add ("-flto");
  400. auto extra = replacePreprocessorTokens (config, getExtraCompilerFlagsString()).trim();
  401. if (extra.isNotEmpty())
  402. result.add (extra);
  403. return result;
  404. }
  405. StringArray getCXXFlags() const
  406. {
  407. StringArray result;
  408. auto cppStandard = project.getCppStandardValue().toString();
  409. if (cppStandard == "latest")
  410. cppStandard = "1z";
  411. cppStandard = "-std=" + String (shouldUseGNUExtensions() ? "gnu++" : "c++") + cppStandard;
  412. result.add (cppStandard);
  413. return result;
  414. }
  415. StringArray getHeaderSearchPaths (const BuildConfiguration& config) const
  416. {
  417. StringArray searchPaths (extraSearchPaths);
  418. searchPaths.addArray (config.getHeaderSearchPaths());
  419. searchPaths = getCleanedStringArray (searchPaths);
  420. StringArray result;
  421. for (auto& path : searchPaths)
  422. result.add (FileHelpers::unixStylePath (replacePreprocessorTokens (config, path)));
  423. return result;
  424. }
  425. StringArray getLibraryNames (const BuildConfiguration& config) const
  426. {
  427. StringArray result (linuxLibs);
  428. StringArray libraries;
  429. libraries.addTokens (getExternalLibrariesString(), ";", "\"'");
  430. libraries.removeEmptyStrings();
  431. for (auto& lib : libraries)
  432. result.add (replacePreprocessorTokens (config, lib).trim());
  433. return result;
  434. }
  435. StringArray getLibrarySearchPaths (const BuildConfiguration& config) const
  436. {
  437. auto result = getSearchPathsFromString (config.getLibrarySearchPathString());
  438. for (auto path : moduleLibSearchPaths)
  439. result.add (path + "/" + config.getModuleLibraryArchName());
  440. return result;
  441. }
  442. StringArray getLinkerFlags (const BuildConfiguration& config) const
  443. {
  444. StringArray result (makefileExtraLinkerFlags);
  445. if (! config.isDebug())
  446. result.add ("-fvisibility=hidden");
  447. if (config.isLinkTimeOptimisationEnabled())
  448. result.add ("-flto");
  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. };