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.

965 lines
35KB

  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 Array<std::pair<File, String>>& filesToCompile) const
  149. {
  150. out << "OBJECTS_" + getTargetVarName() + String (" := \\") << newLine;
  151. for (auto& f : filesToCompile)
  152. out << " $(JUCE_OBJDIR)/" << escapeSpaces (owner.getObjectFileFor ({ f.first, owner.getTargetFolder(), RelativePath::buildTargetFolder })) << " \\" << newLine;
  153. out << newLine;
  154. }
  155. void addFiles (OutputStream& out, const Array<std::pair<File, String>>& filesToCompile)
  156. {
  157. auto cppflagsVarName = "JUCE_CPPFLAGS_" + getTargetVarName();
  158. auto cflagsVarName = "JUCE_CFLAGS_" + getTargetVarName();
  159. for (auto& f : filesToCompile)
  160. {
  161. RelativePath relativePath (f.first, owner.getTargetFolder(), RelativePath::buildTargetFolder);
  162. out << "$(JUCE_OBJDIR)/" << escapeSpaces (owner.getObjectFileFor (relativePath)) << ": " << escapeSpaces (relativePath.toUnixStyle()) << newLine
  163. << "\t-$(V_AT)mkdir -p $(JUCE_OBJDIR)" << newLine
  164. << "\t@echo \"Compiling " << relativePath.getFileName() << "\"" << newLine
  165. << (relativePath.hasFileExtension ("c;s;S") ? "\t$(V_AT)$(CC) $(JUCE_CFLAGS) " : "\t$(V_AT)$(CXX) $(JUCE_CXXFLAGS) ")
  166. << "$(" << cppflagsVarName << ") $(" << cflagsVarName << ")"
  167. << (f.second.isNotEmpty() ? " $(" + owner.getCompilerFlagSchemeVariableName (f.second) + ")" : "") << " -o \"$@\" -c \"$<\"" << newLine
  168. << newLine;
  169. }
  170. }
  171. String getBuildProduct() const
  172. {
  173. return "$(JUCE_OUTDIR)/$(JUCE_TARGET_" + getTargetVarName() + ")";
  174. }
  175. String getPhonyName() const
  176. {
  177. return String (getName()).upToFirstOccurrenceOf (" ", false, false);
  178. }
  179. void writeTargetLine (OutputStream& out, const StringArray& packages)
  180. {
  181. jassert (type != AggregateTarget);
  182. out << getBuildProduct() << " : "
  183. << "$(OBJECTS_" << getTargetVarName() << ") $(RESOURCES)";
  184. if (type != SharedCodeTarget && owner.shouldBuildTargetType (SharedCodeTarget))
  185. out << " $(JUCE_OUTDIR)/$(JUCE_TARGET_SHARED_CODE)";
  186. out << newLine;
  187. if (! packages.isEmpty())
  188. {
  189. out << "\t@command -v pkg-config >/dev/null 2>&1 || { echo >&2 \"pkg-config not installed. Please, install it.\"; exit 1; }" << newLine
  190. << "\t@pkg-config --print-errors";
  191. for (auto& pkg : packages)
  192. out << " " << pkg;
  193. out << newLine;
  194. }
  195. out << "\t@echo Linking \"" << owner.projectName << " - " << getName() << "\"" << newLine
  196. << "\t-$(V_AT)mkdir -p $(JUCE_BINDIR)" << newLine
  197. << "\t-$(V_AT)mkdir -p $(JUCE_LIBDIR)" << newLine
  198. << "\t-$(V_AT)mkdir -p $(JUCE_OUTDIR)" << newLine;
  199. if (type == UnityPlugIn)
  200. {
  201. auto scriptName = owner.getProject().getUnityScriptName();
  202. RelativePath scriptPath (owner.getProject().getGeneratedCodeFolder().getChildFile (scriptName),
  203. owner.getTargetFolder(),
  204. RelativePath::projectFolder);
  205. out << "\t-$(V_AT)cp " + scriptPath.toUnixStyle() + " $(JUCE_OUTDIR)/" + scriptName << newLine;
  206. }
  207. if (owner.projectType.isStaticLibrary() || type == SharedCodeTarget)
  208. {
  209. out << "\t$(V_AT)$(AR) -rcs " << getBuildProduct()
  210. << " $(OBJECTS_" << getTargetVarName() << ")" << newLine;
  211. }
  212. else
  213. {
  214. out << "\t$(V_AT)$(CXX) -o " << getBuildProduct()
  215. << " $(OBJECTS_" << getTargetVarName() << ") ";
  216. if (owner.shouldBuildTargetType (SharedCodeTarget))
  217. out << "$(JUCE_OUTDIR)/$(JUCE_TARGET_SHARED_CODE) ";
  218. out << "$(JUCE_LDFLAGS) ";
  219. if (getTargetFileType() == sharedLibraryOrDLL || getTargetFileType() == pluginBundle
  220. || type == GUIApp || type == StandalonePlugIn)
  221. out << "$(JUCE_LDFLAGS_" << getTargetVarName() << ") ";
  222. out << "$(RESOURCES) $(TARGET_ARCH)" << newLine;
  223. }
  224. out << newLine;
  225. }
  226. const MakefileProjectExporter& owner;
  227. };
  228. //==============================================================================
  229. static const char* getNameLinux() { return "Linux Makefile"; }
  230. static const char* getValueTreeTypeName() { return "LINUX_MAKE"; }
  231. String getExtraPkgConfigString() const { return extraPkgConfigValue.get(); }
  232. static MakefileProjectExporter* createForSettings (Project& project, const ValueTree& settings)
  233. {
  234. if (settings.hasType (getValueTreeTypeName()))
  235. return new MakefileProjectExporter (project, settings);
  236. return nullptr;
  237. }
  238. //==============================================================================
  239. MakefileProjectExporter (Project& p, const ValueTree& t)
  240. : ProjectExporter (p, t),
  241. extraPkgConfigValue (settings, Ids::linuxExtraPkgConfig, getUndoManager())
  242. {
  243. name = getNameLinux();
  244. targetLocationValue.setDefault (getDefaultBuildsRootFolder() + getTargetFolderForExporter (getValueTreeTypeName()));
  245. }
  246. //==============================================================================
  247. bool canLaunchProject() override { return false; }
  248. bool launchProject() override { return false; }
  249. bool usesMMFiles() const override { return false; }
  250. bool canCopeWithDuplicateFiles() override { return false; }
  251. bool supportsUserDefinedConfigurations() const override { return true; }
  252. bool isXcode() const override { return false; }
  253. bool isVisualStudio() const override { return false; }
  254. bool isCodeBlocks() const override { return false; }
  255. bool isMakefile() const override { return true; }
  256. bool isAndroidStudio() const override { return false; }
  257. bool isCLion() const override { return false; }
  258. bool isAndroid() const override { return false; }
  259. bool isWindows() const override { return false; }
  260. bool isLinux() const override { return true; }
  261. bool isOSX() const override { return false; }
  262. bool isiOS() const override { return false; }
  263. bool supportsTargetType (ProjectType::Target::Type type) const override
  264. {
  265. switch (type)
  266. {
  267. case ProjectType::Target::GUIApp:
  268. case ProjectType::Target::ConsoleApp:
  269. case ProjectType::Target::StaticLibrary:
  270. case ProjectType::Target::SharedCodeTarget:
  271. case ProjectType::Target::AggregateTarget:
  272. case ProjectType::Target::VSTPlugIn:
  273. case ProjectType::Target::StandalonePlugIn:
  274. case ProjectType::Target::DynamicLibrary:
  275. case ProjectType::Target::UnityPlugIn:
  276. return true;
  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 (! 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. auto extra = replacePreprocessorTokens (config, getExtraCompilerFlagsString()).trim();
  400. if (extra.isNotEmpty())
  401. result.add (extra);
  402. return result;
  403. }
  404. StringArray getCXXFlags() const
  405. {
  406. StringArray result;
  407. auto cppStandard = project.getCppStandardString();
  408. if (cppStandard == "latest")
  409. cppStandard = "17";
  410. cppStandard = "-std=" + String (shouldUseGNUExtensions() ? "gnu++" : "c++") + cppStandard;
  411. result.add (cppStandard);
  412. return result;
  413. }
  414. StringArray getHeaderSearchPaths (const BuildConfiguration& config) const
  415. {
  416. StringArray searchPaths (extraSearchPaths);
  417. searchPaths.addArray (config.getHeaderSearchPaths());
  418. searchPaths = getCleanedStringArray (searchPaths);
  419. StringArray result;
  420. for (auto& path : searchPaths)
  421. result.add (FileHelpers::unixStylePath (replacePreprocessorTokens (config, path)));
  422. return result;
  423. }
  424. StringArray getLibraryNames (const BuildConfiguration& config) const
  425. {
  426. StringArray result (linuxLibs);
  427. auto libraries = StringArray::fromTokens (getExternalLibrariesString(), ";", "\"'");
  428. libraries.removeEmptyStrings();
  429. for (auto& lib : libraries)
  430. result.add (replacePreprocessorTokens (config, lib).trim());
  431. return result;
  432. }
  433. StringArray getLibrarySearchPaths (const BuildConfiguration& config) const
  434. {
  435. auto result = getSearchPathsFromString (config.getLibrarySearchPathString());
  436. for (auto path : moduleLibSearchPaths)
  437. result.add (path + "/" + config.getModuleLibraryArchName());
  438. return result;
  439. }
  440. StringArray getLinkerFlags (const BuildConfiguration& config) const
  441. {
  442. auto result = makefileExtraLinkerFlags;
  443. if (! config.isDebug())
  444. result.add ("-fvisibility=hidden");
  445. if (config.isLinkTimeOptimisationEnabled())
  446. result.add ("-flto");
  447. auto extraFlags = getExtraLinkerFlagsString().trim();
  448. if (extraFlags.isNotEmpty())
  449. result.add (replacePreprocessorTokens (config, extraFlags));
  450. return result;
  451. }
  452. bool isWebBrowserComponentEnabled() const
  453. {
  454. static String guiExtrasModule ("juce_gui_extra");
  455. return (project.getEnabledModules().isModuleEnabled (guiExtrasModule)
  456. && project.isConfigFlagEnabled ("JUCE_WEB_BROWSER", true));
  457. }
  458. bool isLoadCurlSymbolsLazilyEnabled() const
  459. {
  460. static String juceCoreModule ("juce_core");
  461. return (project.getEnabledModules().isModuleEnabled (juceCoreModule)
  462. && project.isConfigFlagEnabled ("JUCE_LOAD_CURL_SYMBOLS_LAZILY", false));
  463. }
  464. //==============================================================================
  465. void writeDefineFlags (OutputStream& out, const MakeBuildConfiguration& config) const
  466. {
  467. out << createGCCPreprocessorFlags (mergePreprocessorDefs (getDefines (config), getAllPreprocessorDefs (config, ProjectType::Target::unspecified)));
  468. }
  469. void writePkgConfigFlags (OutputStream& out) const
  470. {
  471. auto flags = getPreprocessorPkgConfigFlags();
  472. if (flags.isNotEmpty())
  473. out << " " << flags;
  474. }
  475. void writeCPreprocessorFlags (OutputStream& out, const BuildConfiguration& config) const
  476. {
  477. auto flags = getCPreprocessorFlags (config);
  478. if (! flags.isEmpty())
  479. out << " " << flags.joinIntoString (" ");
  480. }
  481. void writeHeaderPathFlags (OutputStream& out, const BuildConfiguration& config) const
  482. {
  483. for (auto& path : getHeaderSearchPaths (config))
  484. out << " -I" << escapeSpaces (path).replace ("~", "$(HOME)");
  485. }
  486. void writeCppFlags (OutputStream& out, const MakeBuildConfiguration& config) const
  487. {
  488. out << " JUCE_CPPFLAGS := $(DEPFLAGS)";
  489. writeDefineFlags (out, config);
  490. writePkgConfigFlags (out);
  491. writeCPreprocessorFlags (out, config);
  492. writeHeaderPathFlags (out, config);
  493. out << " $(CPPFLAGS)" << newLine;
  494. }
  495. void writeLinkerFlags (OutputStream& out, const BuildConfiguration& config) const
  496. {
  497. out << " JUCE_LDFLAGS += $(TARGET_ARCH) -L$(JUCE_BINDIR) -L$(JUCE_LIBDIR)";
  498. for (auto path : getLibrarySearchPaths (config))
  499. out << " -L" << escapeSpaces (path).replace ("~", "$(HOME)");
  500. auto pkgConfigFlags = getLinkerPkgConfigFlags();
  501. if (pkgConfigFlags.isNotEmpty())
  502. out << " " << getLinkerPkgConfigFlags();
  503. auto linkerFlags = getLinkerFlags (config).joinIntoString (" ");
  504. if (linkerFlags.isNotEmpty())
  505. out << " " << linkerFlags;
  506. for (auto& libName : getLibraryNames (config))
  507. out << " -l" << libName;
  508. out << " $(LDFLAGS)" << newLine;
  509. }
  510. void writeTargetLines (OutputStream& out, const StringArray& packages) const
  511. {
  512. auto n = targets.size();
  513. for (int i = 0; i < n; ++i)
  514. {
  515. if (auto* target = targets.getUnchecked (i))
  516. {
  517. if (target->type == ProjectType::Target::AggregateTarget)
  518. {
  519. StringArray dependencies;
  520. MemoryOutputStream subTargetLines;
  521. for (int j = 0; j < n; ++j)
  522. {
  523. if (i == j) continue;
  524. if (auto* dependency = targets.getUnchecked (j))
  525. {
  526. if (dependency->type != ProjectType::Target::SharedCodeTarget)
  527. {
  528. auto phonyName = dependency->getPhonyName();
  529. subTargetLines << phonyName << " : " << dependency->getBuildProduct() << newLine;
  530. dependencies.add (phonyName);
  531. }
  532. }
  533. }
  534. out << "all : " << dependencies.joinIntoString (" ") << newLine << newLine;
  535. out << subTargetLines.toString() << newLine << newLine;
  536. }
  537. else
  538. {
  539. if (! getProject().getProjectType().isAudioPlugin())
  540. out << "all : " << target->getBuildProduct() << newLine << newLine;
  541. target->writeTargetLine (out, packages);
  542. }
  543. }
  544. }
  545. }
  546. void writeConfig (OutputStream& out, const MakeBuildConfiguration& config) const
  547. {
  548. String buildDirName ("build");
  549. auto intermediatesDirName = buildDirName + "/intermediate/" + config.getName();
  550. auto outputDir = buildDirName;
  551. if (config.getTargetBinaryRelativePathString().isNotEmpty())
  552. {
  553. RelativePath binaryPath (config.getTargetBinaryRelativePathString(), RelativePath::projectFolder);
  554. outputDir = binaryPath.rebased (projectFolder, getTargetFolder(), RelativePath::buildTargetFolder).toUnixStyle();
  555. }
  556. out << "ifeq ($(CONFIG)," << escapeSpaces (config.getName()) << ")" << newLine
  557. << " JUCE_BINDIR := " << escapeSpaces (buildDirName) << newLine
  558. << " JUCE_LIBDIR := " << escapeSpaces (buildDirName) << newLine
  559. << " JUCE_OBJDIR := " << escapeSpaces (intermediatesDirName) << newLine
  560. << " JUCE_OUTDIR := " << escapeSpaces (outputDir) << newLine
  561. << newLine
  562. << " ifeq ($(TARGET_ARCH),)" << newLine
  563. << " TARGET_ARCH := " << getArchFlags (config) << newLine
  564. << " endif" << newLine
  565. << newLine;
  566. writeCppFlags (out, config);
  567. for (auto target : targets)
  568. {
  569. auto lines = target->getTargetSettings (config);
  570. if (lines.size() > 0)
  571. out << " " << lines.joinIntoString ("\n ") << newLine;
  572. out << newLine;
  573. }
  574. out << " JUCE_CFLAGS += $(JUCE_CPPFLAGS) $(TARGET_ARCH)";
  575. auto cflags = getCFlags (config).joinIntoString (" ");
  576. if (cflags.isNotEmpty())
  577. out << " " << cflags;
  578. out << " $(CFLAGS)" << newLine;
  579. out << " JUCE_CXXFLAGS += $(JUCE_CFLAGS)";
  580. auto cxxflags = getCXXFlags().joinIntoString (" ");
  581. if (cxxflags.isNotEmpty())
  582. out << " " << cxxflags;
  583. out << " $(CXXFLAGS)" << newLine;
  584. writeLinkerFlags (out, config);
  585. out << newLine;
  586. out << " CLEANCMD = rm -rf $(JUCE_OUTDIR)/$(TARGET) $(JUCE_OBJDIR)" << newLine
  587. << "endif" << newLine
  588. << newLine;
  589. }
  590. void writeIncludeLines (OutputStream& out) const
  591. {
  592. auto n = targets.size();
  593. for (int i = 0; i < n; ++i)
  594. {
  595. if (auto* target = targets.getUnchecked (i))
  596. {
  597. if (target->type == ProjectType::Target::AggregateTarget)
  598. continue;
  599. out << "-include $(OBJECTS_" << target->getTargetVarName()
  600. << ":%.o=%.d)" << newLine;
  601. }
  602. }
  603. }
  604. static String getCompilerFlagSchemeVariableName (const String& schemeName) { return "JUCE_COMPILERFLAGSCHEME_" + schemeName; }
  605. void findAllFilesToCompile (const Project::Item& projectItem, Array<std::pair<File, String>>& results) const
  606. {
  607. if (projectItem.isGroup())
  608. {
  609. for (int i = 0; i < projectItem.getNumChildren(); ++i)
  610. findAllFilesToCompile (projectItem.getChild (i), results);
  611. }
  612. else
  613. {
  614. if (projectItem.shouldBeCompiled())
  615. {
  616. auto f = projectItem.getFile();
  617. if (shouldFileBeCompiledByDefault (f))
  618. {
  619. auto scheme = projectItem.getCompilerFlagSchemeString();
  620. auto flags = compilerFlagSchemesMap[scheme].get().toString();
  621. if (scheme.isNotEmpty() && flags.isNotEmpty())
  622. results.add ({ f, scheme });
  623. else
  624. results.add ({ f, {} });
  625. }
  626. }
  627. }
  628. }
  629. void writeCompilerFlagSchemes (OutputStream& out, const Array<std::pair<File, String>>& filesToCompile) const
  630. {
  631. StringArray schemesToWrite;
  632. for (auto& f : filesToCompile)
  633. if (f.second.isNotEmpty())
  634. schemesToWrite.addIfNotAlreadyThere (f.second);
  635. if (! schemesToWrite.isEmpty())
  636. {
  637. for (auto& s : schemesToWrite)
  638. out << getCompilerFlagSchemeVariableName (s) << " := "
  639. << compilerFlagSchemesMap[s].get().toString() << newLine;
  640. out << newLine;
  641. }
  642. }
  643. void writeMakefile (OutputStream& out) const
  644. {
  645. out << "# Automatically generated makefile, created by the Projucer" << newLine
  646. << "# Don't edit this file! Your changes will be overwritten when you re-save the Projucer project!" << newLine
  647. << newLine;
  648. out << "# build with \"V=1\" for verbose builds" << newLine
  649. << "ifeq ($(V), 1)" << newLine
  650. << "V_AT =" << newLine
  651. << "else" << newLine
  652. << "V_AT = @" << newLine
  653. << "endif" << newLine
  654. << newLine;
  655. out << "# (this disables dependency generation if multiple architectures are set)" << newLine
  656. << "DEPFLAGS := $(if $(word 2, $(TARGET_ARCH)), , -MMD)" << newLine
  657. << newLine;
  658. out << "ifndef STRIP" << newLine
  659. << " STRIP=strip" << newLine
  660. << "endif" << newLine
  661. << newLine;
  662. out << "ifndef AR" << newLine
  663. << " AR=ar" << newLine
  664. << "endif" << newLine
  665. << newLine;
  666. out << "ifndef CONFIG" << newLine
  667. << " CONFIG=" << escapeSpaces (getConfiguration(0)->getName()) << newLine
  668. << "endif" << newLine
  669. << newLine;
  670. out << "JUCE_ARCH_LABEL := $(shell uname -m)" << newLine
  671. << newLine;
  672. for (ConstConfigIterator config (*this); config.next();)
  673. writeConfig (out, dynamic_cast<const MakeBuildConfiguration&> (*config));
  674. Array<std::pair<File, String>> filesToCompile;
  675. for (int i = 0; i < getAllGroups().size(); ++i)
  676. findAllFilesToCompile (getAllGroups().getReference (i), filesToCompile);
  677. writeCompilerFlagSchemes (out, filesToCompile);
  678. auto getFilesForTarget = [] (const Array<std::pair<File, String>>& files, MakefileTarget* target, const Project& p) -> Array<std::pair<File, String>>
  679. {
  680. Array<std::pair<File, String>> targetFiles;
  681. auto targetType = (p.getProjectType().isAudioPlugin() ? target->type : MakefileTarget::SharedCodeTarget);
  682. for (auto& f : files)
  683. if (p.getTargetTypeFromFilePath (f.first, true) == targetType)
  684. targetFiles.add (f);
  685. return targetFiles;
  686. };
  687. for (auto target : targets)
  688. target->writeObjects (out, getFilesForTarget (filesToCompile, target, project));
  689. out << getPhonyTargetLine() << newLine << newLine;
  690. writeTargetLines (out, getPackages());
  691. for (auto target : targets)
  692. target->addFiles (out, getFilesForTarget (filesToCompile, target, project));
  693. out << "clean:" << newLine
  694. << "\t@echo Cleaning " << projectName << newLine
  695. << "\t$(V_AT)$(CLEANCMD)" << newLine
  696. << newLine;
  697. out << "strip:" << newLine
  698. << "\t@echo Stripping " << projectName << newLine
  699. << "\t-$(V_AT)$(STRIP) --strip-unneeded $(JUCE_OUTDIR)/$(TARGET)" << newLine
  700. << newLine;
  701. writeIncludeLines (out);
  702. }
  703. String getArchFlags (const BuildConfiguration& config) const
  704. {
  705. if (auto* makeConfig = dynamic_cast<const MakeBuildConfiguration*> (&config))
  706. return makeConfig->getArchitectureTypeString();
  707. return "-march=native";
  708. }
  709. String getObjectFileFor (const RelativePath& file) const
  710. {
  711. return file.getFileNameWithoutExtension()
  712. + "_" + String::toHexString (file.toUnixStyle().hashCode()) + ".o";
  713. }
  714. String getPhonyTargetLine() const
  715. {
  716. MemoryOutputStream phonyTargetLine;
  717. phonyTargetLine << ".PHONY: clean all strip";
  718. if (! getProject().getProjectType().isAudioPlugin())
  719. return phonyTargetLine.toString();
  720. for (auto target : targets)
  721. if (target->type != ProjectType::Target::SharedCodeTarget
  722. && target->type != ProjectType::Target::AggregateTarget)
  723. phonyTargetLine << " " << target->getPhonyName();
  724. return phonyTargetLine.toString();
  725. }
  726. friend class CLionProjectExporter;
  727. OwnedArray<MakefileTarget> targets;
  728. JUCE_DECLARE_NON_COPYABLE (MakefileProjectExporter)
  729. };