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.

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