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.

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