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.

976 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. result.add ("-fvisibility=hidden");
  446. if (config.isLinkTimeOptimisationEnabled())
  447. result.add ("-flto");
  448. auto extraFlags = getExtraLinkerFlagsString().trim();
  449. if (extraFlags.isNotEmpty())
  450. result.add (replacePreprocessorTokens (config, extraFlags));
  451. return result;
  452. }
  453. bool isWebBrowserComponentEnabled() const
  454. {
  455. static String guiExtrasModule ("juce_gui_extra");
  456. return (project.getEnabledModules().isModuleEnabled (guiExtrasModule)
  457. && project.isConfigFlagEnabled ("JUCE_WEB_BROWSER", true));
  458. }
  459. bool isCurlEnabled() const
  460. {
  461. static String juceCoreModule ("juce_core");
  462. return (project.getEnabledModules().isModuleEnabled (juceCoreModule)
  463. && project.isConfigFlagEnabled ("JUCE_USE_CURL", true));
  464. }
  465. bool isLoadCurlSymbolsLazilyEnabled() const
  466. {
  467. static String juceCoreModule ("juce_core");
  468. return (project.getEnabledModules().isModuleEnabled (juceCoreModule)
  469. && project.isConfigFlagEnabled ("JUCE_LOAD_CURL_SYMBOLS_LAZILY", false));
  470. }
  471. //==============================================================================
  472. void writeDefineFlags (OutputStream& out, const MakeBuildConfiguration& config) const
  473. {
  474. out << createGCCPreprocessorFlags (mergePreprocessorDefs (getDefines (config), getAllPreprocessorDefs (config, ProjectType::Target::unspecified)));
  475. }
  476. void writePkgConfigFlags (OutputStream& out) const
  477. {
  478. auto flags = getPreprocessorPkgConfigFlags();
  479. if (flags.isNotEmpty())
  480. out << " " << flags;
  481. }
  482. void writeCPreprocessorFlags (OutputStream& out, const BuildConfiguration& config) const
  483. {
  484. auto flags = getCPreprocessorFlags (config);
  485. if (! flags.isEmpty())
  486. out << " " << flags.joinIntoString (" ");
  487. }
  488. void writeHeaderPathFlags (OutputStream& out, const BuildConfiguration& config) const
  489. {
  490. for (auto& path : getHeaderSearchPaths (config))
  491. out << " -I" << escapeSpaces (path).replace ("~", "$(HOME)");
  492. }
  493. void writeCppFlags (OutputStream& out, const MakeBuildConfiguration& config) const
  494. {
  495. out << " JUCE_CPPFLAGS := $(DEPFLAGS)";
  496. writeDefineFlags (out, config);
  497. writePkgConfigFlags (out);
  498. writeCPreprocessorFlags (out, config);
  499. writeHeaderPathFlags (out, config);
  500. out << " $(CPPFLAGS)" << newLine;
  501. }
  502. void writeLinkerFlags (OutputStream& out, const BuildConfiguration& config) const
  503. {
  504. out << " JUCE_LDFLAGS += $(TARGET_ARCH) -L$(JUCE_BINDIR) -L$(JUCE_LIBDIR)";
  505. for (auto path : getLibrarySearchPaths (config))
  506. out << " -L" << escapeSpaces (path).replace ("~", "$(HOME)");
  507. auto pkgConfigFlags = getLinkerPkgConfigFlags();
  508. if (pkgConfigFlags.isNotEmpty())
  509. out << " " << getLinkerPkgConfigFlags();
  510. auto linkerFlags = getLinkerFlags (config).joinIntoString (" ");
  511. if (linkerFlags.isNotEmpty())
  512. out << " " << linkerFlags;
  513. for (auto& libName : getLibraryNames (config))
  514. out << " -l" << libName;
  515. out << " $(LDFLAGS)" << newLine;
  516. }
  517. void writeTargetLines (OutputStream& out, const StringArray& packages) const
  518. {
  519. auto n = targets.size();
  520. for (int i = 0; i < n; ++i)
  521. {
  522. if (auto* target = targets.getUnchecked (i))
  523. {
  524. if (target->type == ProjectType::Target::AggregateTarget)
  525. {
  526. StringArray dependencies;
  527. MemoryOutputStream subTargetLines;
  528. for (int j = 0; j < n; ++j)
  529. {
  530. if (i == j) continue;
  531. if (auto* dependency = targets.getUnchecked (j))
  532. {
  533. if (dependency->type != ProjectType::Target::SharedCodeTarget)
  534. {
  535. auto phonyName = dependency->getPhonyName();
  536. subTargetLines << phonyName << " : " << dependency->getBuildProduct() << newLine;
  537. dependencies.add (phonyName);
  538. }
  539. }
  540. }
  541. out << "all : " << dependencies.joinIntoString (" ") << newLine << newLine;
  542. out << subTargetLines.toString() << newLine << newLine;
  543. }
  544. else
  545. {
  546. if (! getProject().isAudioPluginProject())
  547. out << "all : " << target->getBuildProduct() << newLine << newLine;
  548. target->writeTargetLine (out, packages);
  549. }
  550. }
  551. }
  552. }
  553. void writeConfig (OutputStream& out, const MakeBuildConfiguration& config) const
  554. {
  555. String buildDirName ("build");
  556. auto intermediatesDirName = buildDirName + "/intermediate/" + config.getName();
  557. auto outputDir = buildDirName;
  558. if (config.getTargetBinaryRelativePathString().isNotEmpty())
  559. {
  560. RelativePath binaryPath (config.getTargetBinaryRelativePathString(), RelativePath::projectFolder);
  561. outputDir = binaryPath.rebased (projectFolder, getTargetFolder(), RelativePath::buildTargetFolder).toUnixStyle();
  562. }
  563. out << "ifeq ($(CONFIG)," << escapeSpaces (config.getName()) << ")" << newLine
  564. << " JUCE_BINDIR := " << escapeSpaces (buildDirName) << newLine
  565. << " JUCE_LIBDIR := " << escapeSpaces (buildDirName) << newLine
  566. << " JUCE_OBJDIR := " << escapeSpaces (intermediatesDirName) << newLine
  567. << " JUCE_OUTDIR := " << escapeSpaces (outputDir) << newLine
  568. << newLine
  569. << " ifeq ($(TARGET_ARCH),)" << newLine
  570. << " TARGET_ARCH := " << getArchFlags (config) << newLine
  571. << " endif" << newLine
  572. << newLine;
  573. writeCppFlags (out, config);
  574. for (auto target : targets)
  575. {
  576. auto lines = target->getTargetSettings (config);
  577. if (lines.size() > 0)
  578. out << " " << lines.joinIntoString ("\n ") << newLine;
  579. out << newLine;
  580. }
  581. out << " JUCE_CFLAGS += $(JUCE_CPPFLAGS) $(TARGET_ARCH)";
  582. auto cflags = getCFlags (config).joinIntoString (" ");
  583. if (cflags.isNotEmpty())
  584. out << " " << cflags;
  585. out << " $(CFLAGS)" << newLine;
  586. out << " JUCE_CXXFLAGS += $(JUCE_CFLAGS)";
  587. auto cxxflags = getCXXFlags().joinIntoString (" ");
  588. if (cxxflags.isNotEmpty())
  589. out << " " << cxxflags;
  590. out << " $(CXXFLAGS)" << newLine;
  591. writeLinkerFlags (out, config);
  592. out << newLine;
  593. out << " CLEANCMD = rm -rf $(JUCE_OUTDIR)/$(TARGET) $(JUCE_OBJDIR)" << newLine
  594. << "endif" << newLine
  595. << newLine;
  596. }
  597. void writeIncludeLines (OutputStream& out) const
  598. {
  599. auto n = targets.size();
  600. for (int i = 0; i < n; ++i)
  601. {
  602. if (auto* target = targets.getUnchecked (i))
  603. {
  604. if (target->type == ProjectType::Target::AggregateTarget)
  605. continue;
  606. out << "-include $(OBJECTS_" << target->getTargetVarName()
  607. << ":%.o=%.d)" << newLine;
  608. }
  609. }
  610. }
  611. static String getCompilerFlagSchemeVariableName (const String& schemeName) { return "JUCE_COMPILERFLAGSCHEME_" + schemeName; }
  612. void findAllFilesToCompile (const Project::Item& projectItem, Array<std::pair<File, String>>& results) const
  613. {
  614. if (projectItem.isGroup())
  615. {
  616. for (int i = 0; i < projectItem.getNumChildren(); ++i)
  617. findAllFilesToCompile (projectItem.getChild (i), results);
  618. }
  619. else
  620. {
  621. if (projectItem.shouldBeCompiled())
  622. {
  623. auto f = projectItem.getFile();
  624. if (shouldFileBeCompiledByDefault (f))
  625. {
  626. auto scheme = projectItem.getCompilerFlagSchemeString();
  627. auto flags = compilerFlagSchemesMap[scheme].get().toString();
  628. if (scheme.isNotEmpty() && flags.isNotEmpty())
  629. results.add ({ f, scheme });
  630. else
  631. results.add ({ f, {} });
  632. }
  633. }
  634. }
  635. }
  636. void writeCompilerFlagSchemes (OutputStream& out, const Array<std::pair<File, String>>& filesToCompile) const
  637. {
  638. StringArray schemesToWrite;
  639. for (auto& f : filesToCompile)
  640. if (f.second.isNotEmpty())
  641. schemesToWrite.addIfNotAlreadyThere (f.second);
  642. if (! schemesToWrite.isEmpty())
  643. {
  644. for (auto& s : schemesToWrite)
  645. out << getCompilerFlagSchemeVariableName (s) << " := "
  646. << compilerFlagSchemesMap[s].get().toString() << newLine;
  647. out << newLine;
  648. }
  649. }
  650. void writeMakefile (OutputStream& out) const
  651. {
  652. out << "# Automatically generated makefile, created by the Projucer" << newLine
  653. << "# Don't edit this file! Your changes will be overwritten when you re-save the Projucer project!" << newLine
  654. << newLine;
  655. out << "# build with \"V=1\" for verbose builds" << newLine
  656. << "ifeq ($(V), 1)" << newLine
  657. << "V_AT =" << newLine
  658. << "else" << newLine
  659. << "V_AT = @" << newLine
  660. << "endif" << newLine
  661. << newLine;
  662. out << "# (this disables dependency generation if multiple architectures are set)" << newLine
  663. << "DEPFLAGS := $(if $(word 2, $(TARGET_ARCH)), , -MMD)" << newLine
  664. << newLine;
  665. out << "ifndef STRIP" << newLine
  666. << " STRIP=strip" << newLine
  667. << "endif" << newLine
  668. << newLine;
  669. out << "ifndef AR" << newLine
  670. << " AR=ar" << newLine
  671. << "endif" << newLine
  672. << newLine;
  673. out << "ifndef CONFIG" << newLine
  674. << " CONFIG=" << escapeSpaces (getConfiguration(0)->getName()) << newLine
  675. << "endif" << newLine
  676. << newLine;
  677. out << "JUCE_ARCH_LABEL := $(shell uname -m)" << newLine
  678. << newLine;
  679. for (ConstConfigIterator config (*this); config.next();)
  680. writeConfig (out, dynamic_cast<const MakeBuildConfiguration&> (*config));
  681. Array<std::pair<File, String>> filesToCompile;
  682. for (int i = 0; i < getAllGroups().size(); ++i)
  683. findAllFilesToCompile (getAllGroups().getReference (i), filesToCompile);
  684. writeCompilerFlagSchemes (out, filesToCompile);
  685. auto getFilesForTarget = [] (const Array<std::pair<File, String>>& files, MakefileTarget* target, const Project& p) -> Array<std::pair<File, String>>
  686. {
  687. Array<std::pair<File, String>> targetFiles;
  688. auto targetType = (p.isAudioPluginProject() ? target->type : MakefileTarget::SharedCodeTarget);
  689. for (auto& f : files)
  690. if (p.getTargetTypeFromFilePath (f.first, true) == targetType)
  691. targetFiles.add (f);
  692. return targetFiles;
  693. };
  694. for (auto target : targets)
  695. target->writeObjects (out, getFilesForTarget (filesToCompile, target, project));
  696. out << getPhonyTargetLine() << newLine << newLine;
  697. writeTargetLines (out, getPackages());
  698. for (auto target : targets)
  699. target->addFiles (out, getFilesForTarget (filesToCompile, target, project));
  700. out << "clean:" << newLine
  701. << "\t@echo Cleaning " << projectName << newLine
  702. << "\t$(V_AT)$(CLEANCMD)" << newLine
  703. << newLine;
  704. out << "strip:" << newLine
  705. << "\t@echo Stripping " << projectName << newLine
  706. << "\t-$(V_AT)$(STRIP) --strip-unneeded $(JUCE_OUTDIR)/$(TARGET)" << newLine
  707. << newLine;
  708. writeIncludeLines (out);
  709. }
  710. String getArchFlags (const BuildConfiguration& config) const
  711. {
  712. if (auto* makeConfig = dynamic_cast<const MakeBuildConfiguration*> (&config))
  713. return makeConfig->getArchitectureTypeString();
  714. return "-march=native";
  715. }
  716. String getObjectFileFor (const RelativePath& file) const
  717. {
  718. return file.getFileNameWithoutExtension()
  719. + "_" + String::toHexString (file.toUnixStyle().hashCode()) + ".o";
  720. }
  721. String getPhonyTargetLine() const
  722. {
  723. MemoryOutputStream phonyTargetLine;
  724. phonyTargetLine << ".PHONY: clean all strip";
  725. if (! getProject().isAudioPluginProject())
  726. return phonyTargetLine.toString();
  727. for (auto target : targets)
  728. if (target->type != ProjectType::Target::SharedCodeTarget
  729. && target->type != ProjectType::Target::AggregateTarget)
  730. phonyTargetLine << " " << target->getPhonyName();
  731. return phonyTargetLine.toString();
  732. }
  733. friend class CLionProjectExporter;
  734. OwnedArray<MakefileTarget> targets;
  735. JUCE_DECLARE_NON_COPYABLE (MakefileProjectExporter)
  736. };