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.

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