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.

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