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.

977 lines
36KB

  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(), String())
  31. {
  32. linkTimeOptimisationValue.setDefault (false);
  33. optimisationLevelValue.setDefault (isDebug() ? gccO0 : gccO3);
  34. }
  35. void createConfigProperties (PropertyListBuilder& props) override
  36. {
  37. addRecommendedLinuxCompilerWarningsProperty (props);
  38. addGCCOptimisationProperty (props);
  39. props.add (new ChoicePropertyComponent (architectureTypeValue, "Architecture",
  40. { "<None>", "Native", "32-bit (-m32)", "64-bit (-m64)", "ARM v6", "ARM v7" },
  41. { { String() }, "-march=native", "-m32", "-m64", "-march=armv6", "-march=armv7" }),
  42. "Specifies the 32/64-bit architecture to use.");
  43. }
  44. String getModuleLibraryArchName() const override
  45. {
  46. auto archFlag = getArchitectureTypeString();
  47. String prefix ("-march=");
  48. if (archFlag.startsWith (prefix))
  49. return archFlag.substring (prefix.length());
  50. if (archFlag == "-m64")
  51. return "x86_64";
  52. if (archFlag == "-m32")
  53. return "i386";
  54. return "${JUCE_ARCH_LABEL}";
  55. }
  56. String getArchitectureTypeString() const { return architectureTypeValue.get(); }
  57. //==============================================================================
  58. ValueWithDefault architectureTypeValue;
  59. };
  60. BuildConfiguration::Ptr createBuildConfig (const ValueTree& tree) const override
  61. {
  62. return *new MakeBuildConfiguration (project, tree, *this);
  63. }
  64. public:
  65. //==============================================================================
  66. class MakefileTarget : public ProjectType::Target
  67. {
  68. public:
  69. MakefileTarget (ProjectType::Target::Type targetType, const MakefileProjectExporter& exporter)
  70. : ProjectType::Target (targetType), owner (exporter)
  71. {}
  72. StringArray getCompilerFlags() const
  73. {
  74. StringArray result;
  75. if (getTargetFileType() == sharedLibraryOrDLL || getTargetFileType() == pluginBundle)
  76. {
  77. result.add ("-fPIC");
  78. result.add ("-fvisibility=hidden");
  79. }
  80. return result;
  81. }
  82. StringArray getLinkerFlags() const
  83. {
  84. StringArray result;
  85. if (getTargetFileType() == sharedLibraryOrDLL || getTargetFileType() == pluginBundle)
  86. {
  87. result.add ("-shared");
  88. if (getTargetFileType() == pluginBundle)
  89. result.add ("-Wl,--no-undefined");
  90. }
  91. return result;
  92. }
  93. StringPairArray getDefines (const BuildConfiguration& config) const
  94. {
  95. StringPairArray result;
  96. auto commonOptionKeys = owner.getAllPreprocessorDefs (config, ProjectType::Target::unspecified).getAllKeys();
  97. auto targetSpecific = owner.getAllPreprocessorDefs (config, type);
  98. for (auto& key : targetSpecific.getAllKeys())
  99. if (! commonOptionKeys.contains (key))
  100. result.set (key, targetSpecific[key]);
  101. return result;
  102. }
  103. StringArray getTargetSettings (const MakeBuildConfiguration& config) const
  104. {
  105. if (type == AggregateTarget) // the aggregate target should not specify any settings at all!
  106. return {}; // it just defines dependencies on the other targets.
  107. StringArray defines;
  108. auto defs = getDefines (config);
  109. for (auto& key : defs.getAllKeys())
  110. defines.add ("-D" + key + "=" + defs[key]);
  111. StringArray s;
  112. auto cppflagsVarName = "JUCE_CPPFLAGS_" + getTargetVarName();
  113. s.add (cppflagsVarName + " := " + defines.joinIntoString (" "));
  114. auto cflags = getCompilerFlags();
  115. if (! cflags.isEmpty())
  116. s.add ("JUCE_CFLAGS_" + getTargetVarName() + " := " + cflags.joinIntoString (" "));
  117. auto ldflags = getLinkerFlags();
  118. if (! ldflags.isEmpty())
  119. s.add ("JUCE_LDFLAGS_" + getTargetVarName() + " := " + ldflags.joinIntoString (" "));
  120. auto targetName = owner.replacePreprocessorTokens (config, config.getTargetBinaryNameString());
  121. if (owner.projectType.isStaticLibrary())
  122. targetName = getStaticLibbedFilename (targetName);
  123. else if (owner.projectType.isDynamicLibrary())
  124. targetName = getDynamicLibbedFilename (targetName);
  125. else
  126. targetName = targetName.upToLastOccurrenceOf (".", false, false) + getTargetFileSuffix();
  127. s.add ("JUCE_TARGET_" + getTargetVarName() + String (" := ") + escapeSpaces (targetName));
  128. return s;
  129. }
  130. String getTargetFileSuffix() const
  131. {
  132. if (type == VSTPlugIn || type == UnityPlugIn || type == DynamicLibrary)
  133. return ".so";
  134. if (type == SharedCodeTarget || type == StaticLibrary)
  135. return ".a";
  136. return {};
  137. }
  138. String getTargetVarName() const
  139. {
  140. return String (getName()).toUpperCase().replaceCharacter (L' ', L'_');
  141. }
  142. void writeObjects (OutputStream& out, const Array<std::pair<File, String>>& filesToCompile) const
  143. {
  144. out << "OBJECTS_" + getTargetVarName() + String (" := \\") << newLine;
  145. for (auto& f : filesToCompile)
  146. out << " $(JUCE_OBJDIR)/" << escapeSpaces (owner.getObjectFileFor ({ f.first, owner.getTargetFolder(), RelativePath::buildTargetFolder })) << " \\" << newLine;
  147. out << newLine;
  148. }
  149. void addFiles (OutputStream& out, const Array<std::pair<File, String>>& filesToCompile)
  150. {
  151. auto cppflagsVarName = "JUCE_CPPFLAGS_" + getTargetVarName();
  152. auto cflagsVarName = "JUCE_CFLAGS_" + getTargetVarName();
  153. for (auto& f : filesToCompile)
  154. {
  155. RelativePath relativePath (f.first, owner.getTargetFolder(), RelativePath::buildTargetFolder);
  156. out << "$(JUCE_OBJDIR)/" << escapeSpaces (owner.getObjectFileFor (relativePath)) << ": " << escapeSpaces (relativePath.toUnixStyle()) << newLine
  157. << "\t-$(V_AT)mkdir -p $(JUCE_OBJDIR)" << newLine
  158. << "\t@echo \"Compiling " << relativePath.getFileName() << "\"" << newLine
  159. << (relativePath.hasFileExtension ("c;s;S") ? "\t$(V_AT)$(CC) $(JUCE_CFLAGS) " : "\t$(V_AT)$(CXX) $(JUCE_CXXFLAGS) ")
  160. << "$(" << cppflagsVarName << ") $(" << cflagsVarName << ")"
  161. << (f.second.isNotEmpty() ? " $(" + owner.getCompilerFlagSchemeVariableName (f.second) + ")" : "") << " -o \"$@\" -c \"$<\"" << newLine
  162. << newLine;
  163. }
  164. }
  165. String getBuildProduct() const
  166. {
  167. return "$(JUCE_OUTDIR)/$(JUCE_TARGET_" + getTargetVarName() + ")";
  168. }
  169. String getPhonyName() const
  170. {
  171. return String (getName()).upToFirstOccurrenceOf (" ", false, false);
  172. }
  173. void writeTargetLine (OutputStream& out, const StringArray& packages)
  174. {
  175. jassert (type != AggregateTarget);
  176. out << getBuildProduct() << " : "
  177. << "$(OBJECTS_" << getTargetVarName() << ") $(RESOURCES)";
  178. if (type != SharedCodeTarget && owner.shouldBuildTargetType (SharedCodeTarget))
  179. out << " $(JUCE_OUTDIR)/$(JUCE_TARGET_SHARED_CODE)";
  180. out << newLine;
  181. if (! packages.isEmpty())
  182. {
  183. out << "\t@command -v pkg-config >/dev/null 2>&1 || { echo >&2 \"pkg-config not installed. Please, install it.\"; exit 1; }" << newLine
  184. << "\t@pkg-config --print-errors";
  185. for (auto& pkg : packages)
  186. out << " " << pkg;
  187. out << newLine;
  188. }
  189. out << "\t@echo Linking \"" << owner.projectName << " - " << getName() << "\"" << newLine
  190. << "\t-$(V_AT)mkdir -p $(JUCE_BINDIR)" << newLine
  191. << "\t-$(V_AT)mkdir -p $(JUCE_LIBDIR)" << newLine
  192. << "\t-$(V_AT)mkdir -p $(JUCE_OUTDIR)" << newLine;
  193. if (type == UnityPlugIn)
  194. {
  195. auto scriptName = owner.getProject().getUnityScriptName();
  196. RelativePath scriptPath (owner.getProject().getGeneratedCodeFolder().getChildFile (scriptName),
  197. owner.getTargetFolder(),
  198. RelativePath::projectFolder);
  199. out << "\t-$(V_AT)cp " + scriptPath.toUnixStyle() + " $(JUCE_OUTDIR)/" + scriptName << newLine;
  200. }
  201. if (owner.projectType.isStaticLibrary() || type == SharedCodeTarget)
  202. {
  203. out << "\t$(V_AT)$(AR) -rcs " << getBuildProduct()
  204. << " $(OBJECTS_" << getTargetVarName() << ")" << newLine;
  205. }
  206. else
  207. {
  208. out << "\t$(V_AT)$(CXX) -o " << getBuildProduct()
  209. << " $(OBJECTS_" << getTargetVarName() << ") ";
  210. if (owner.shouldBuildTargetType (SharedCodeTarget))
  211. out << "$(JUCE_OUTDIR)/$(JUCE_TARGET_SHARED_CODE) ";
  212. out << "$(JUCE_LDFLAGS) ";
  213. if (getTargetFileType() == sharedLibraryOrDLL || getTargetFileType() == pluginBundle
  214. || type == GUIApp || type == StandalonePlugIn)
  215. out << "$(JUCE_LDFLAGS_" << getTargetVarName() << ") ";
  216. out << "$(RESOURCES) $(TARGET_ARCH)" << newLine;
  217. }
  218. out << newLine;
  219. }
  220. const MakefileProjectExporter& owner;
  221. };
  222. //==============================================================================
  223. static const char* getNameLinux() { return "Linux Makefile"; }
  224. static const char* getValueTreeTypeName() { return "LINUX_MAKE"; }
  225. String getExtraPkgConfigString() const { return extraPkgConfigValue.get(); }
  226. static MakefileProjectExporter* createForSettings (Project& projectToUse, const ValueTree& settingsToUse)
  227. {
  228. if (settingsToUse.hasType (getValueTreeTypeName()))
  229. return new MakefileProjectExporter (projectToUse, settingsToUse);
  230. return nullptr;
  231. }
  232. //==============================================================================
  233. MakefileProjectExporter (Project& p, const ValueTree& t)
  234. : ProjectExporter (p, t),
  235. extraPkgConfigValue (settings, Ids::linuxExtraPkgConfig, getUndoManager())
  236. {
  237. name = getNameLinux();
  238. targetLocationValue.setDefault (getDefaultBuildsRootFolder() + getTargetFolderForExporter (getValueTreeTypeName()));
  239. }
  240. //==============================================================================
  241. bool canLaunchProject() override { return false; }
  242. bool launchProject() override { return false; }
  243. bool usesMMFiles() const override { return false; }
  244. bool canCopeWithDuplicateFiles() override { return false; }
  245. bool supportsUserDefinedConfigurations() const override { return true; }
  246. bool isXcode() const override { return false; }
  247. bool isVisualStudio() const override { return false; }
  248. bool isCodeBlocks() const override { return false; }
  249. bool isMakefile() const override { return true; }
  250. bool isAndroidStudio() const override { return false; }
  251. bool isCLion() const override { return false; }
  252. bool isAndroid() const override { return false; }
  253. bool isWindows() const override { return false; }
  254. bool isLinux() const override { return true; }
  255. bool isOSX() const override { return false; }
  256. bool isiOS() const override { return false; }
  257. bool supportsTargetType (ProjectType::Target::Type type) const override
  258. {
  259. switch (type)
  260. {
  261. case ProjectType::Target::GUIApp:
  262. case ProjectType::Target::ConsoleApp:
  263. case ProjectType::Target::StaticLibrary:
  264. case ProjectType::Target::SharedCodeTarget:
  265. case ProjectType::Target::AggregateTarget:
  266. case ProjectType::Target::VSTPlugIn:
  267. case ProjectType::Target::StandalonePlugIn:
  268. case ProjectType::Target::DynamicLibrary:
  269. case ProjectType::Target::UnityPlugIn:
  270. return true;
  271. case ProjectType::Target::VST3PlugIn:
  272. case ProjectType::Target::AAXPlugIn:
  273. case ProjectType::Target::RTASPlugIn:
  274. case ProjectType::Target::AudioUnitPlugIn:
  275. case ProjectType::Target::AudioUnitv3PlugIn:
  276. case ProjectType::Target::unspecified:
  277. default:
  278. break;
  279. }
  280. return false;
  281. }
  282. void createExporterProperties (PropertyListBuilder& properties) override
  283. {
  284. properties.add (new TextPropertyComponent (extraPkgConfigValue, "pkg-config libraries", 8192, false),
  285. "Extra pkg-config libraries for you application. Each package should be space separated.");
  286. }
  287. void initialiseDependencyPathValues() override
  288. {
  289. vstLegacyPathValueWrapper.init ({ settings, Ids::vstLegacyFolder, nullptr },
  290. getAppSettings().getStoredPath (Ids::vstLegacyPath, TargetOS::linux), TargetOS::linux);
  291. }
  292. //==============================================================================
  293. bool anyTargetIsSharedLibrary() const
  294. {
  295. for (auto* target : targets)
  296. {
  297. auto fileType = target->getTargetFileType();
  298. if (fileType == ProjectType::Target::sharedLibraryOrDLL
  299. || fileType == ProjectType::Target::pluginBundle)
  300. return true;
  301. }
  302. return false;
  303. }
  304. //==============================================================================
  305. void create (const OwnedArray<LibraryModule>&) const override
  306. {
  307. MemoryOutputStream mo;
  308. mo.setNewLineString ("\n");
  309. writeMakefile (mo);
  310. overwriteFileIfDifferentOrThrow (getTargetFolder().getChildFile ("Makefile"), mo);
  311. }
  312. //==============================================================================
  313. void addPlatformSpecificSettingsForProjectType (const ProjectType&) override
  314. {
  315. callForAllSupportedTargets ([this] (ProjectType::Target::Type targetType)
  316. {
  317. if (MakefileTarget* target = new MakefileTarget (targetType, *this))
  318. {
  319. if (targetType == ProjectType::Target::AggregateTarget)
  320. targets.insert (0, target);
  321. else
  322. targets.add (target);
  323. }
  324. });
  325. // If you hit this assert, you tried to generate a project for an exporter
  326. // that does not support any of your targets!
  327. jassert (targets.size() > 0);
  328. }
  329. private:
  330. ValueWithDefault extraPkgConfigValue;
  331. //==============================================================================
  332. StringPairArray getDefines (const BuildConfiguration& config) const
  333. {
  334. StringPairArray result;
  335. result.set ("LINUX", "1");
  336. if (config.isDebug())
  337. {
  338. result.set ("DEBUG", "1");
  339. result.set ("_DEBUG", "1");
  340. }
  341. else
  342. {
  343. result.set ("NDEBUG", "1");
  344. }
  345. result = mergePreprocessorDefs (result, getAllPreprocessorDefs (config, ProjectType::Target::unspecified));
  346. return result;
  347. }
  348. StringArray getPackages() const
  349. {
  350. StringArray packages;
  351. packages.addTokens (getExtraPkgConfigString(), " ", "\"'");
  352. packages.removeEmptyStrings();
  353. packages.addArray (linuxPackages);
  354. if (isWebBrowserComponentEnabled())
  355. {
  356. packages.add ("webkit2gtk-4.0");
  357. packages.add ("gtk+-x11-3.0");
  358. }
  359. // don't add libcurl if curl symbols are loaded at runtime
  360. if (isCurlEnabled() && ! isLoadCurlSymbolsLazilyEnabled())
  361. packages.add ("libcurl");
  362. packages.removeDuplicates (false);
  363. return packages;
  364. }
  365. String getPreprocessorPkgConfigFlags() const
  366. {
  367. auto packages = getPackages();
  368. if (packages.size() > 0)
  369. return "$(shell pkg-config --cflags " + packages.joinIntoString (" ") + ")";
  370. return {};
  371. }
  372. String getLinkerPkgConfigFlags() const
  373. {
  374. auto packages = getPackages();
  375. if (packages.size() > 0)
  376. return "$(shell pkg-config --libs " + packages.joinIntoString (" ") + ")";
  377. return {};
  378. }
  379. StringArray getCPreprocessorFlags (const BuildConfiguration&) const
  380. {
  381. StringArray result;
  382. if (linuxLibs.contains("pthread"))
  383. result.add ("-pthread");
  384. return result;
  385. }
  386. StringArray getCFlags (const BuildConfiguration& config) const
  387. {
  388. StringArray result;
  389. if (anyTargetIsSharedLibrary())
  390. result.add ("-fPIC");
  391. if (config.isDebug())
  392. {
  393. result.add ("-g");
  394. result.add ("-ggdb");
  395. }
  396. result.add ("-O" + config.getGCCOptimisationFlag());
  397. if (config.isLinkTimeOptimisationEnabled())
  398. result.add ("-flto");
  399. for (auto& recommended : config.getRecommendedCompilerWarningFlags())
  400. result.add (recommended);
  401. auto extra = replacePreprocessorTokens (config, getExtraCompilerFlagsString()).trim();
  402. if (extra.isNotEmpty())
  403. result.add (extra);
  404. return result;
  405. }
  406. StringArray getCXXFlags() const
  407. {
  408. StringArray result;
  409. auto cppStandard = project.getCppStandardString();
  410. if (cppStandard == "latest")
  411. cppStandard = "17";
  412. cppStandard = "-std=" + String (shouldUseGNUExtensions() ? "gnu++" : "c++") + cppStandard;
  413. result.add (cppStandard);
  414. return result;
  415. }
  416. StringArray getHeaderSearchPaths (const BuildConfiguration& config) const
  417. {
  418. StringArray searchPaths (extraSearchPaths);
  419. searchPaths.addArray (config.getHeaderSearchPaths());
  420. searchPaths = getCleanedStringArray (searchPaths);
  421. StringArray result;
  422. for (auto& path : searchPaths)
  423. result.add (FileHelpers::unixStylePath (replacePreprocessorTokens (config, path)));
  424. return result;
  425. }
  426. StringArray getLibraryNames (const BuildConfiguration& config) const
  427. {
  428. StringArray result (linuxLibs);
  429. auto libraries = StringArray::fromTokens (getExternalLibrariesString(), ";", "\"'");
  430. libraries.removeEmptyStrings();
  431. for (auto& lib : libraries)
  432. result.add (replacePreprocessorTokens (config, lib).trim());
  433. return result;
  434. }
  435. StringArray getLibrarySearchPaths (const BuildConfiguration& config) const
  436. {
  437. auto result = getSearchPathsFromString (config.getLibrarySearchPathString());
  438. for (auto path : moduleLibSearchPaths)
  439. result.add (path + "/" + config.getModuleLibraryArchName());
  440. return result;
  441. }
  442. StringArray getLinkerFlags (const BuildConfiguration& config) const
  443. {
  444. auto result = makefileExtraLinkerFlags;
  445. if (! config.isDebug())
  446. result.add ("-fvisibility=hidden");
  447. if (config.isLinkTimeOptimisationEnabled())
  448. result.add ("-flto");
  449. auto extraFlags = getExtraLinkerFlagsString().trim();
  450. if (extraFlags.isNotEmpty())
  451. result.add (replacePreprocessorTokens (config, extraFlags));
  452. return result;
  453. }
  454. bool isWebBrowserComponentEnabled() const
  455. {
  456. static String guiExtrasModule ("juce_gui_extra");
  457. return (project.getEnabledModules().isModuleEnabled (guiExtrasModule)
  458. && project.isConfigFlagEnabled ("JUCE_WEB_BROWSER", true));
  459. }
  460. bool isCurlEnabled() const
  461. {
  462. static String juceCoreModule ("juce_core");
  463. return (project.getEnabledModules().isModuleEnabled (juceCoreModule)
  464. && project.isConfigFlagEnabled ("JUCE_USE_CURL", true));
  465. }
  466. bool isLoadCurlSymbolsLazilyEnabled() const
  467. {
  468. static String juceCoreModule ("juce_core");
  469. return (project.getEnabledModules().isModuleEnabled (juceCoreModule)
  470. && project.isConfigFlagEnabled ("JUCE_LOAD_CURL_SYMBOLS_LAZILY", false));
  471. }
  472. //==============================================================================
  473. void writeDefineFlags (OutputStream& out, const MakeBuildConfiguration& config) const
  474. {
  475. out << createGCCPreprocessorFlags (mergePreprocessorDefs (getDefines (config), getAllPreprocessorDefs (config, ProjectType::Target::unspecified)));
  476. }
  477. void writePkgConfigFlags (OutputStream& out) const
  478. {
  479. auto flags = getPreprocessorPkgConfigFlags();
  480. if (flags.isNotEmpty())
  481. out << " " << flags;
  482. }
  483. void writeCPreprocessorFlags (OutputStream& out, const BuildConfiguration& config) const
  484. {
  485. auto flags = getCPreprocessorFlags (config);
  486. if (! flags.isEmpty())
  487. out << " " << flags.joinIntoString (" ");
  488. }
  489. void writeHeaderPathFlags (OutputStream& out, const BuildConfiguration& config) const
  490. {
  491. for (auto& path : getHeaderSearchPaths (config))
  492. out << " -I" << escapeSpaces (path).replace ("~", "$(HOME)");
  493. }
  494. void writeCppFlags (OutputStream& out, const MakeBuildConfiguration& config) const
  495. {
  496. out << " JUCE_CPPFLAGS := $(DEPFLAGS)";
  497. writeDefineFlags (out, config);
  498. writePkgConfigFlags (out);
  499. writeCPreprocessorFlags (out, config);
  500. writeHeaderPathFlags (out, config);
  501. out << " $(CPPFLAGS)" << newLine;
  502. }
  503. void writeLinkerFlags (OutputStream& out, const BuildConfiguration& config) const
  504. {
  505. out << " JUCE_LDFLAGS += $(TARGET_ARCH) -L$(JUCE_BINDIR) -L$(JUCE_LIBDIR)";
  506. for (auto path : getLibrarySearchPaths (config))
  507. out << " -L" << escapeSpaces (path).replace ("~", "$(HOME)");
  508. auto pkgConfigFlags = getLinkerPkgConfigFlags();
  509. if (pkgConfigFlags.isNotEmpty())
  510. out << " " << getLinkerPkgConfigFlags();
  511. auto linkerFlags = getLinkerFlags (config).joinIntoString (" ");
  512. if (linkerFlags.isNotEmpty())
  513. out << " " << linkerFlags;
  514. for (auto& libName : getLibraryNames (config))
  515. out << " -l" << libName;
  516. out << " $(LDFLAGS)" << newLine;
  517. }
  518. void writeTargetLines (OutputStream& out, const StringArray& packages) const
  519. {
  520. auto n = targets.size();
  521. for (int i = 0; i < n; ++i)
  522. {
  523. if (auto* target = targets.getUnchecked (i))
  524. {
  525. if (target->type == ProjectType::Target::AggregateTarget)
  526. {
  527. StringArray dependencies;
  528. MemoryOutputStream subTargetLines;
  529. for (int j = 0; j < n; ++j)
  530. {
  531. if (i == j) continue;
  532. if (auto* dependency = targets.getUnchecked (j))
  533. {
  534. if (dependency->type != ProjectType::Target::SharedCodeTarget)
  535. {
  536. auto phonyName = dependency->getPhonyName();
  537. subTargetLines << phonyName << " : " << dependency->getBuildProduct() << newLine;
  538. dependencies.add (phonyName);
  539. }
  540. }
  541. }
  542. out << "all : " << dependencies.joinIntoString (" ") << newLine << newLine;
  543. out << subTargetLines.toString() << newLine << newLine;
  544. }
  545. else
  546. {
  547. if (! getProject().isAudioPluginProject())
  548. out << "all : " << target->getBuildProduct() << newLine << newLine;
  549. target->writeTargetLine (out, packages);
  550. }
  551. }
  552. }
  553. }
  554. void writeConfig (OutputStream& out, const MakeBuildConfiguration& config) const
  555. {
  556. String buildDirName ("build");
  557. auto intermediatesDirName = buildDirName + "/intermediate/" + config.getName();
  558. auto outputDir = buildDirName;
  559. if (config.getTargetBinaryRelativePathString().isNotEmpty())
  560. {
  561. RelativePath binaryPath (config.getTargetBinaryRelativePathString(), RelativePath::projectFolder);
  562. outputDir = binaryPath.rebased (projectFolder, getTargetFolder(), RelativePath::buildTargetFolder).toUnixStyle();
  563. }
  564. out << "ifeq ($(CONFIG)," << escapeSpaces (config.getName()) << ")" << newLine
  565. << " JUCE_BINDIR := " << escapeSpaces (buildDirName) << newLine
  566. << " JUCE_LIBDIR := " << escapeSpaces (buildDirName) << newLine
  567. << " JUCE_OBJDIR := " << escapeSpaces (intermediatesDirName) << newLine
  568. << " JUCE_OUTDIR := " << escapeSpaces (outputDir) << newLine
  569. << newLine
  570. << " ifeq ($(TARGET_ARCH),)" << newLine
  571. << " TARGET_ARCH := " << getArchFlags (config) << newLine
  572. << " endif" << newLine
  573. << newLine;
  574. writeCppFlags (out, config);
  575. for (auto target : targets)
  576. {
  577. auto lines = target->getTargetSettings (config);
  578. if (lines.size() > 0)
  579. out << " " << lines.joinIntoString ("\n ") << newLine;
  580. out << newLine;
  581. }
  582. out << " JUCE_CFLAGS += $(JUCE_CPPFLAGS) $(TARGET_ARCH)";
  583. auto cflags = getCFlags (config).joinIntoString (" ");
  584. if (cflags.isNotEmpty())
  585. out << " " << cflags;
  586. out << " $(CFLAGS)" << newLine;
  587. out << " JUCE_CXXFLAGS += $(JUCE_CFLAGS)";
  588. auto cxxflags = getCXXFlags().joinIntoString (" ");
  589. if (cxxflags.isNotEmpty())
  590. out << " " << cxxflags;
  591. out << " $(CXXFLAGS)" << newLine;
  592. writeLinkerFlags (out, config);
  593. out << newLine;
  594. out << " CLEANCMD = rm -rf $(JUCE_OUTDIR)/$(TARGET) $(JUCE_OBJDIR)" << newLine
  595. << "endif" << newLine
  596. << newLine;
  597. }
  598. void writeIncludeLines (OutputStream& out) const
  599. {
  600. auto n = targets.size();
  601. for (int i = 0; i < n; ++i)
  602. {
  603. if (auto* target = targets.getUnchecked (i))
  604. {
  605. if (target->type == ProjectType::Target::AggregateTarget)
  606. continue;
  607. out << "-include $(OBJECTS_" << target->getTargetVarName()
  608. << ":%.o=%.d)" << newLine;
  609. }
  610. }
  611. }
  612. static String getCompilerFlagSchemeVariableName (const String& schemeName) { return "JUCE_COMPILERFLAGSCHEME_" + schemeName; }
  613. void findAllFilesToCompile (const Project::Item& projectItem, Array<std::pair<File, String>>& results) const
  614. {
  615. if (projectItem.isGroup())
  616. {
  617. for (int i = 0; i < projectItem.getNumChildren(); ++i)
  618. findAllFilesToCompile (projectItem.getChild (i), results);
  619. }
  620. else
  621. {
  622. if (projectItem.shouldBeCompiled())
  623. {
  624. auto f = projectItem.getFile();
  625. if (shouldFileBeCompiledByDefault (f))
  626. {
  627. auto scheme = projectItem.getCompilerFlagSchemeString();
  628. auto flags = compilerFlagSchemesMap[scheme].get().toString();
  629. if (scheme.isNotEmpty() && flags.isNotEmpty())
  630. results.add ({ f, scheme });
  631. else
  632. results.add ({ f, {} });
  633. }
  634. }
  635. }
  636. }
  637. void writeCompilerFlagSchemes (OutputStream& out, const Array<std::pair<File, String>>& filesToCompile) const
  638. {
  639. StringArray schemesToWrite;
  640. for (auto& f : filesToCompile)
  641. if (f.second.isNotEmpty())
  642. schemesToWrite.addIfNotAlreadyThere (f.second);
  643. if (! schemesToWrite.isEmpty())
  644. {
  645. for (auto& s : schemesToWrite)
  646. out << getCompilerFlagSchemeVariableName (s) << " := "
  647. << compilerFlagSchemesMap[s].get().toString() << newLine;
  648. out << newLine;
  649. }
  650. }
  651. void writeMakefile (OutputStream& out) const
  652. {
  653. out << "# Automatically generated makefile, created by the Projucer" << newLine
  654. << "# Don't edit this file! Your changes will be overwritten when you re-save the Projucer project!" << newLine
  655. << newLine;
  656. out << "# build with \"V=1\" for verbose builds" << newLine
  657. << "ifeq ($(V), 1)" << newLine
  658. << "V_AT =" << newLine
  659. << "else" << newLine
  660. << "V_AT = @" << newLine
  661. << "endif" << newLine
  662. << newLine;
  663. out << "# (this disables dependency generation if multiple architectures are set)" << newLine
  664. << "DEPFLAGS := $(if $(word 2, $(TARGET_ARCH)), , -MMD)" << newLine
  665. << newLine;
  666. out << "ifndef STRIP" << newLine
  667. << " STRIP=strip" << newLine
  668. << "endif" << newLine
  669. << newLine;
  670. out << "ifndef AR" << newLine
  671. << " AR=ar" << newLine
  672. << "endif" << newLine
  673. << newLine;
  674. out << "ifndef CONFIG" << newLine
  675. << " CONFIG=" << escapeSpaces (getConfiguration(0)->getName()) << newLine
  676. << "endif" << newLine
  677. << newLine;
  678. out << "JUCE_ARCH_LABEL := $(shell uname -m)" << newLine
  679. << newLine;
  680. for (ConstConfigIterator config (*this); config.next();)
  681. writeConfig (out, dynamic_cast<const MakeBuildConfiguration&> (*config));
  682. Array<std::pair<File, String>> filesToCompile;
  683. for (int i = 0; i < getAllGroups().size(); ++i)
  684. findAllFilesToCompile (getAllGroups().getReference (i), filesToCompile);
  685. writeCompilerFlagSchemes (out, filesToCompile);
  686. auto getFilesForTarget = [] (const Array<std::pair<File, String>>& files, MakefileTarget* target, const Project& p) -> Array<std::pair<File, String>>
  687. {
  688. Array<std::pair<File, String>> targetFiles;
  689. auto targetType = (p.isAudioPluginProject() ? target->type : MakefileTarget::SharedCodeTarget);
  690. for (auto& f : files)
  691. if (p.getTargetTypeFromFilePath (f.first, true) == targetType)
  692. targetFiles.add (f);
  693. return targetFiles;
  694. };
  695. for (auto target : targets)
  696. target->writeObjects (out, getFilesForTarget (filesToCompile, target, project));
  697. out << getPhonyTargetLine() << newLine << newLine;
  698. writeTargetLines (out, getPackages());
  699. for (auto target : targets)
  700. target->addFiles (out, getFilesForTarget (filesToCompile, target, project));
  701. out << "clean:" << newLine
  702. << "\t@echo Cleaning " << projectName << newLine
  703. << "\t$(V_AT)$(CLEANCMD)" << newLine
  704. << newLine;
  705. out << "strip:" << newLine
  706. << "\t@echo Stripping " << projectName << newLine
  707. << "\t-$(V_AT)$(STRIP) --strip-unneeded $(JUCE_OUTDIR)/$(TARGET)" << newLine
  708. << newLine;
  709. writeIncludeLines (out);
  710. }
  711. String getArchFlags (const BuildConfiguration& config) const
  712. {
  713. if (auto* makeConfig = dynamic_cast<const MakeBuildConfiguration*> (&config))
  714. return makeConfig->getArchitectureTypeString();
  715. return "-march=native";
  716. }
  717. String getObjectFileFor (const RelativePath& file) const
  718. {
  719. return file.getFileNameWithoutExtension()
  720. + "_" + String::toHexString (file.toUnixStyle().hashCode()) + ".o";
  721. }
  722. String getPhonyTargetLine() const
  723. {
  724. MemoryOutputStream phonyTargetLine;
  725. phonyTargetLine << ".PHONY: clean all strip";
  726. if (! getProject().isAudioPluginProject())
  727. return phonyTargetLine.toString();
  728. for (auto target : targets)
  729. if (target->type != ProjectType::Target::SharedCodeTarget
  730. && target->type != ProjectType::Target::AggregateTarget)
  731. phonyTargetLine << " " << target->getPhonyName();
  732. return phonyTargetLine.toString();
  733. }
  734. friend class CLionProjectExporter;
  735. OwnedArray<MakefileTarget> targets;
  736. JUCE_DECLARE_NON_COPYABLE (MakefileProjectExporter)
  737. };