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.

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