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.

922 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. mo.setNewLineString ("\n");
  332. writeMakefile (mo);
  333. overwriteFileIfDifferentOrThrow (getTargetFolder().getChildFile ("Makefile"), mo);
  334. }
  335. //==============================================================================
  336. void addPlatformSpecificSettingsForProjectType (const ProjectType&) override
  337. {
  338. callForAllSupportedTargets ([this] (ProjectType::Target::Type targetType)
  339. {
  340. if (MakefileTarget* target = new MakefileTarget (targetType, *this))
  341. {
  342. if (targetType == ProjectType::Target::AggregateTarget)
  343. targets.insert (0, target);
  344. else
  345. targets.add (target);
  346. }
  347. });
  348. // If you hit this assert, you tried to generate a project for an exporter
  349. // that does not support any of your targets!
  350. jassert (targets.size() > 0);
  351. }
  352. private:
  353. ValueWithDefault extraPkgConfigValue;
  354. //==============================================================================
  355. StringPairArray getDefines (const BuildConfiguration& config) const
  356. {
  357. StringPairArray result;
  358. result.set ("LINUX", "1");
  359. if (config.isDebug())
  360. {
  361. result.set ("DEBUG", "1");
  362. result.set ("_DEBUG", "1");
  363. }
  364. else
  365. {
  366. result.set ("NDEBUG", "1");
  367. }
  368. result = mergePreprocessorDefs (result, getAllPreprocessorDefs (config, ProjectType::Target::unspecified));
  369. return result;
  370. }
  371. StringArray getPackages() const
  372. {
  373. StringArray packages;
  374. packages.addTokens (getExtraPkgConfigString(), " ", "\"'");
  375. packages.removeEmptyStrings();
  376. packages.addArray (linuxPackages);
  377. if (isWebBrowserComponentEnabled())
  378. {
  379. packages.add ("webkit2gtk-4.0");
  380. packages.add ("gtk+-x11-3.0");
  381. }
  382. // don't add libcurl if curl symbols are loaded at runtime
  383. if (! isLoadCurlSymbolsLazilyEnabled())
  384. packages.add ("libcurl");
  385. packages.removeDuplicates (false);
  386. return packages;
  387. }
  388. String getPreprocessorPkgConfigFlags() const
  389. {
  390. auto packages = getPackages();
  391. if (packages.size() > 0)
  392. return "$(shell pkg-config --cflags " + packages.joinIntoString (" ") + ")";
  393. return {};
  394. }
  395. String getLinkerPkgConfigFlags() const
  396. {
  397. auto packages = getPackages();
  398. if (packages.size() > 0)
  399. return "$(shell pkg-config --libs " + packages.joinIntoString (" ") + ")";
  400. return {};
  401. }
  402. StringArray getCPreprocessorFlags (const BuildConfiguration&) const
  403. {
  404. StringArray result;
  405. if (linuxLibs.contains("pthread"))
  406. result.add ("-pthread");
  407. return result;
  408. }
  409. StringArray getCFlags (const BuildConfiguration& config) const
  410. {
  411. StringArray result;
  412. if (anyTargetIsSharedLibrary())
  413. result.add ("-fPIC");
  414. if (config.isDebug())
  415. {
  416. result.add ("-g");
  417. result.add ("-ggdb");
  418. }
  419. result.add ("-O" + config.getGCCOptimisationFlag());
  420. if (config.isLinkTimeOptimisationEnabled())
  421. result.add ("-flto");
  422. auto extra = replacePreprocessorTokens (config, getExtraCompilerFlagsString()).trim();
  423. if (extra.isNotEmpty())
  424. result.add (extra);
  425. return result;
  426. }
  427. StringArray getCXXFlags() const
  428. {
  429. StringArray result;
  430. auto cppStandard = project.getCppStandardString();
  431. if (cppStandard == "latest")
  432. cppStandard = "17";
  433. cppStandard = "-std=" + String (shouldUseGNUExtensions() ? "gnu++" : "c++") + cppStandard;
  434. result.add (cppStandard);
  435. return result;
  436. }
  437. StringArray getHeaderSearchPaths (const BuildConfiguration& config) const
  438. {
  439. StringArray searchPaths (extraSearchPaths);
  440. searchPaths.addArray (config.getHeaderSearchPaths());
  441. searchPaths = getCleanedStringArray (searchPaths);
  442. StringArray result;
  443. for (auto& path : searchPaths)
  444. result.add (FileHelpers::unixStylePath (replacePreprocessorTokens (config, path)));
  445. return result;
  446. }
  447. StringArray getLibraryNames (const BuildConfiguration& config) const
  448. {
  449. StringArray result (linuxLibs);
  450. auto libraries = StringArray::fromTokens (getExternalLibrariesString(), ";", "\"'");
  451. libraries.removeEmptyStrings();
  452. for (auto& lib : libraries)
  453. result.add (replacePreprocessorTokens (config, lib).trim());
  454. return result;
  455. }
  456. StringArray getLibrarySearchPaths (const BuildConfiguration& config) const
  457. {
  458. auto result = getSearchPathsFromString (config.getLibrarySearchPathString());
  459. for (auto path : moduleLibSearchPaths)
  460. result.add (path + "/" + config.getModuleLibraryArchName());
  461. return result;
  462. }
  463. StringArray getLinkerFlags (const BuildConfiguration& config) const
  464. {
  465. auto result = makefileExtraLinkerFlags;
  466. if (! config.isDebug())
  467. result.add ("-fvisibility=hidden");
  468. if (config.isLinkTimeOptimisationEnabled())
  469. result.add ("-flto");
  470. auto extraFlags = getExtraLinkerFlagsString().trim();
  471. if (extraFlags.isNotEmpty())
  472. result.add (replacePreprocessorTokens (config, extraFlags));
  473. return result;
  474. }
  475. bool isWebBrowserComponentEnabled() const
  476. {
  477. static String guiExtrasModule ("juce_gui_extra");
  478. return (project.getEnabledModules().isModuleEnabled (guiExtrasModule)
  479. && project.isConfigFlagEnabled ("JUCE_WEB_BROWSER", true));
  480. }
  481. bool isLoadCurlSymbolsLazilyEnabled() const
  482. {
  483. static String juceCoreModule ("juce_core");
  484. return (project.getEnabledModules().isModuleEnabled (juceCoreModule)
  485. && project.isConfigFlagEnabled ("JUCE_LOAD_CURL_SYMBOLS_LAZILY", false));
  486. }
  487. //==============================================================================
  488. void writeDefineFlags (OutputStream& out, const MakeBuildConfiguration& config) const
  489. {
  490. out << createGCCPreprocessorFlags (mergePreprocessorDefs (getDefines (config), getAllPreprocessorDefs (config, ProjectType::Target::unspecified)));
  491. }
  492. void writePkgConfigFlags (OutputStream& out) const
  493. {
  494. auto flags = getPreprocessorPkgConfigFlags();
  495. if (flags.isNotEmpty())
  496. out << " " << flags;
  497. }
  498. void writeCPreprocessorFlags (OutputStream& out, const BuildConfiguration& config) const
  499. {
  500. auto flags = getCPreprocessorFlags (config);
  501. if (! flags.isEmpty())
  502. out << " " << flags.joinIntoString (" ");
  503. }
  504. void writeHeaderPathFlags (OutputStream& out, const BuildConfiguration& config) const
  505. {
  506. for (auto& path : getHeaderSearchPaths (config))
  507. out << " -I" << escapeSpaces (path).replace ("~", "$(HOME)");
  508. }
  509. void writeCppFlags (OutputStream& out, const MakeBuildConfiguration& config) const
  510. {
  511. out << " JUCE_CPPFLAGS := $(DEPFLAGS)";
  512. writeDefineFlags (out, config);
  513. writePkgConfigFlags (out);
  514. writeCPreprocessorFlags (out, config);
  515. writeHeaderPathFlags (out, config);
  516. out << " $(CPPFLAGS)" << newLine;
  517. }
  518. void writeLinkerFlags (OutputStream& out, const BuildConfiguration& config) const
  519. {
  520. out << " JUCE_LDFLAGS += $(TARGET_ARCH) -L$(JUCE_BINDIR) -L$(JUCE_LIBDIR)";
  521. for (auto path : getLibrarySearchPaths (config))
  522. out << " -L" << escapeSpaces (path).replace ("~", "$(HOME)");
  523. auto pkgConfigFlags = getLinkerPkgConfigFlags();
  524. if (pkgConfigFlags.isNotEmpty())
  525. out << " " << getLinkerPkgConfigFlags();
  526. auto linkerFlags = getLinkerFlags (config).joinIntoString (" ");
  527. if (linkerFlags.isNotEmpty())
  528. out << " " << linkerFlags;
  529. for (auto& libName : getLibraryNames (config))
  530. out << " -l" << libName;
  531. out << " $(LDFLAGS)" << newLine;
  532. }
  533. void writeTargetLines (OutputStream& out, const StringArray& packages) const
  534. {
  535. auto n = targets.size();
  536. for (int i = 0; i < n; ++i)
  537. {
  538. if (auto* target = targets.getUnchecked (i))
  539. {
  540. if (target->type == ProjectType::Target::AggregateTarget)
  541. {
  542. StringArray dependencies;
  543. MemoryOutputStream subTargetLines;
  544. for (int j = 0; j < n; ++j)
  545. {
  546. if (i == j) continue;
  547. if (auto* dependency = targets.getUnchecked (j))
  548. {
  549. if (dependency->type != ProjectType::Target::SharedCodeTarget)
  550. {
  551. auto phonyName = dependency->getPhonyName();
  552. subTargetLines << phonyName << " : " << dependency->getBuildProduct() << newLine;
  553. dependencies.add (phonyName);
  554. }
  555. }
  556. }
  557. out << "all : " << dependencies.joinIntoString (" ") << newLine << newLine;
  558. out << subTargetLines.toString() << newLine << newLine;
  559. }
  560. else
  561. {
  562. if (! getProject().getProjectType().isAudioPlugin())
  563. out << "all : " << target->getBuildProduct() << newLine << newLine;
  564. target->writeTargetLine (out, packages);
  565. }
  566. }
  567. }
  568. }
  569. void writeConfig (OutputStream& out, const MakeBuildConfiguration& config) const
  570. {
  571. String buildDirName ("build");
  572. auto intermediatesDirName = buildDirName + "/intermediate/" + config.getName();
  573. auto outputDir = buildDirName;
  574. if (config.getTargetBinaryRelativePathString().isNotEmpty())
  575. {
  576. RelativePath binaryPath (config.getTargetBinaryRelativePathString(), RelativePath::projectFolder);
  577. outputDir = binaryPath.rebased (projectFolder, getTargetFolder(), RelativePath::buildTargetFolder).toUnixStyle();
  578. }
  579. out << "ifeq ($(CONFIG)," << escapeSpaces (config.getName()) << ")" << newLine;
  580. out << " JUCE_BINDIR := " << escapeSpaces (buildDirName) << newLine
  581. << " JUCE_LIBDIR := " << escapeSpaces (buildDirName) << newLine
  582. << " JUCE_OBJDIR := " << escapeSpaces (intermediatesDirName) << newLine
  583. << " JUCE_OUTDIR := " << escapeSpaces (outputDir) << newLine
  584. << newLine
  585. << " ifeq ($(TARGET_ARCH),)" << newLine
  586. << " TARGET_ARCH := " << getArchFlags (config) << newLine
  587. << " endif" << newLine
  588. << newLine;
  589. writeCppFlags (out, config);
  590. for (auto target : targets)
  591. {
  592. auto lines = target->getTargetSettings (config);
  593. if (lines.size() > 0)
  594. out << " " << lines.joinIntoString ("\n ") << newLine;
  595. out << newLine;
  596. }
  597. out << " JUCE_CFLAGS += $(JUCE_CPPFLAGS) $(TARGET_ARCH)";
  598. auto cflags = getCFlags (config).joinIntoString (" ");
  599. if (cflags.isNotEmpty())
  600. out << " " << cflags;
  601. out << " $(CFLAGS)" << newLine;
  602. out << " JUCE_CXXFLAGS += $(JUCE_CFLAGS)";
  603. auto cxxflags = getCXXFlags().joinIntoString (" ");
  604. if (cxxflags.isNotEmpty())
  605. out << " " << cxxflags;
  606. out << " $(CXXFLAGS)" << newLine;
  607. writeLinkerFlags (out, config);
  608. out << newLine;
  609. out << " CLEANCMD = rm -rf $(JUCE_OUTDIR)/$(TARGET) $(JUCE_OBJDIR)" << newLine
  610. << "endif" << newLine
  611. << newLine;
  612. }
  613. void writeIncludeLines (OutputStream& out) const
  614. {
  615. auto n = targets.size();
  616. for (int i = 0; i < n; ++i)
  617. {
  618. if (auto* target = targets.getUnchecked (i))
  619. {
  620. if (target->type == ProjectType::Target::AggregateTarget)
  621. continue;
  622. out << "-include $(OBJECTS_" << target->getTargetVarName()
  623. << ":%.o=%.d)" << newLine;
  624. }
  625. }
  626. }
  627. void writeMakefile (OutputStream& out) const
  628. {
  629. out << "# Automatically generated makefile, created by the Projucer" << newLine
  630. << "# Don't edit this file! Your changes will be overwritten when you re-save the Projucer project!" << newLine
  631. << newLine;
  632. out << "# build with \"V=1\" for verbose builds" << newLine
  633. << "ifeq ($(V), 1)" << newLine
  634. << "V_AT =" << newLine
  635. << "else" << newLine
  636. << "V_AT = @" << newLine
  637. << "endif" << newLine
  638. << newLine;
  639. out << "# (this disables dependency generation if multiple architectures are set)" << newLine
  640. << "DEPFLAGS := $(if $(word 2, $(TARGET_ARCH)), , -MMD)" << newLine
  641. << newLine;
  642. out << "ifndef STRIP" << newLine
  643. << " STRIP=strip" << newLine
  644. << "endif" << newLine
  645. << newLine;
  646. out << "ifndef AR" << newLine
  647. << " AR=ar" << newLine
  648. << "endif" << newLine
  649. << newLine;
  650. out << "ifndef CONFIG" << newLine
  651. << " CONFIG=" << escapeSpaces (getConfiguration(0)->getName()) << newLine
  652. << "endif" << newLine
  653. << newLine;
  654. out << "JUCE_ARCH_LABEL := $(shell uname -m)" << newLine
  655. << newLine;
  656. for (ConstConfigIterator config (*this); config.next();)
  657. writeConfig (out, dynamic_cast<const MakeBuildConfiguration&> (*config));
  658. for (auto target : targets)
  659. target->writeObjects (out);
  660. out << getPhonyTargetLine() << newLine << newLine;
  661. writeTargetLines (out, getPackages());
  662. for (auto target : targets)
  663. target->addFiles (out);out << "clean:" << newLine
  664. << "\t@echo Cleaning " << projectName << newLine
  665. << "\t$(V_AT)$(CLEANCMD)" << newLine
  666. << newLine;
  667. out << "strip:" << newLine
  668. << "\t@echo Stripping " << projectName << newLine
  669. << "\t-$(V_AT)$(STRIP) --strip-unneeded $(JUCE_OUTDIR)/$(TARGET)" << newLine
  670. << newLine;
  671. writeIncludeLines (out);
  672. }
  673. String getArchFlags (const BuildConfiguration& config) const
  674. {
  675. if (auto* makeConfig = dynamic_cast<const MakeBuildConfiguration*> (&config))
  676. return makeConfig->getArchitectureTypeString();
  677. return "-march=native";
  678. }
  679. String getObjectFileFor (const RelativePath& file) const
  680. {
  681. return file.getFileNameWithoutExtension()
  682. + "_" + String::toHexString (file.toUnixStyle().hashCode()) + ".o";
  683. }
  684. String getPhonyTargetLine() const
  685. {
  686. MemoryOutputStream phonyTargetLine;
  687. phonyTargetLine << ".PHONY: clean all strip";
  688. if (! getProject().getProjectType().isAudioPlugin())
  689. return phonyTargetLine.toString();
  690. for (auto target : targets)
  691. if (target->type != ProjectType::Target::SharedCodeTarget
  692. && target->type != ProjectType::Target::AggregateTarget)
  693. phonyTargetLine << " " << target->getPhonyName();
  694. return phonyTargetLine.toString();
  695. }
  696. friend class CLionProjectExporter;
  697. OwnedArray<MakefileTarget> targets;
  698. JUCE_DECLARE_NON_COPYABLE (MakefileProjectExporter)
  699. };