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.

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