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.

921 lines
33KB

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