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.

483 lines
20KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. class MakefileProjectExporter : public ProjectExporter
  18. {
  19. public:
  20. //==============================================================================
  21. static const char* getNameLinux() { return "Linux Makefile"; }
  22. static const char* getValueTreeTypeName() { return "LINUX_MAKE"; }
  23. Value getExtraPkgConfig() { return getSetting (Ids::linuxExtraPkgConfig); }
  24. String getExtraPkgConfigString() const { return getSettingString (Ids::linuxExtraPkgConfig); }
  25. static MakefileProjectExporter* createForSettings (Project& project, const ValueTree& settings)
  26. {
  27. if (settings.hasType (getValueTreeTypeName()))
  28. return new MakefileProjectExporter (project, settings);
  29. return nullptr;
  30. }
  31. //==============================================================================
  32. MakefileProjectExporter (Project& p, const ValueTree& t) : ProjectExporter (p, t)
  33. {
  34. name = getNameLinux();
  35. if (getTargetLocationString().isEmpty())
  36. getTargetLocationValue() = getDefaultBuildsRootFolder() + "LinuxMakefile";
  37. initialiseDependencyPathValues();
  38. }
  39. //==============================================================================
  40. bool canLaunchProject() override { return false; }
  41. bool launchProject() override { return false; }
  42. bool usesMMFiles() const override { return false; }
  43. bool canCopeWithDuplicateFiles() override { return false; }
  44. bool supportsUserDefinedConfigurations() const override { return true; }
  45. bool isXcode() const override { return false; }
  46. bool isVisualStudio() const override { return false; }
  47. bool isCodeBlocks() const override { return false; }
  48. bool isMakefile() const override { return true; }
  49. bool isAndroidStudio() const override { return false; }
  50. bool isAndroidAnt() const override { return false; }
  51. bool isAndroid() const override { return false; }
  52. bool isWindows() const override { return false; }
  53. bool isLinux() const override { return true; }
  54. bool isOSX() const override { return false; }
  55. bool isiOS() const override { return false; }
  56. bool supportsVST() const override { return true; }
  57. bool supportsVST3() const override { return false; }
  58. bool supportsAAX() const override { return false; }
  59. bool supportsRTAS() const override { return false; }
  60. bool supportsAU() const override { return false; }
  61. bool supportsAUv3() const override { return false; }
  62. bool supportsStandalone() const override { return false; }
  63. Value getCppStandardValue() { return getSetting (Ids::cppLanguageStandard); }
  64. String getCppStandardString() const { return settings[Ids::cppLanguageStandard]; }
  65. void createExporterProperties (PropertyListBuilder& properties) override
  66. {
  67. static const char* cppStandardNames[] = { "C++03", "C++11", "C++14", nullptr };
  68. static const char* cppStandardValues[] = { "-std=c++03", "-std=c++11", "-std=c++14", nullptr };
  69. properties.add (new ChoicePropertyComponent (getCppStandardValue(),
  70. "C++ standard to use",
  71. StringArray (cppStandardNames),
  72. Array<var> (cppStandardValues)),
  73. "The C++ standard to specify in the makefile");
  74. properties.add (new TextPropertyComponent (getExtraPkgConfig(), "pkg-config libraries", 8192, false),
  75. "Extra pkg-config libraries for you application. Each package should be space separated.");
  76. }
  77. //==============================================================================
  78. void create (const OwnedArray<LibraryModule>&) const override
  79. {
  80. Array<RelativePath> files;
  81. for (int i = 0; i < getAllGroups().size(); ++i)
  82. findAllFilesToCompile (getAllGroups().getReference(i), files);
  83. MemoryOutputStream mo;
  84. writeMakefile (mo, files);
  85. overwriteFileIfDifferentOrThrow (getTargetFolder().getChildFile ("Makefile"), mo);
  86. }
  87. //==============================================================================
  88. void addPlatformSpecificSettingsForProjectType (const ProjectType& type) override
  89. {
  90. if (type.isStaticLibrary())
  91. makefileTargetSuffix = ".a";
  92. else if (type.isDynamicLibrary())
  93. makefileTargetSuffix = ".so";
  94. else if (type.isAudioPlugin())
  95. makefileIsDLL = true;
  96. }
  97. protected:
  98. //==============================================================================
  99. class MakeBuildConfiguration : public BuildConfiguration
  100. {
  101. public:
  102. MakeBuildConfiguration (Project& p, const ValueTree& settings, const ProjectExporter& e)
  103. : BuildConfiguration (p, settings, e)
  104. {
  105. }
  106. Value getArchitectureType() { return getValue (Ids::linuxArchitecture); }
  107. var getArchitectureTypeVar() const { return config [Ids::linuxArchitecture]; }
  108. var getDefaultOptimisationLevel() const override { return var ((int) (isDebug() ? gccO0 : gccO3)); }
  109. void createConfigProperties (PropertyListBuilder& props) override
  110. {
  111. addGCCOptimisationProperty (props);
  112. static const char* const archNames[] = { "(Default)", "<None>", "32-bit (-m32)", "64-bit (-m64)", "ARM v6", "ARM v7" };
  113. const var archFlags[] = { var(), var (String()), "-m32", "-m64", "-march=armv6", "-march=armv7" };
  114. props.add (new ChoicePropertyComponent (getArchitectureType(), "Architecture",
  115. StringArray (archNames, numElementsInArray (archNames)),
  116. Array<var> (archFlags, numElementsInArray (archFlags))));
  117. }
  118. };
  119. BuildConfiguration::Ptr createBuildConfig (const ValueTree& tree) const override
  120. {
  121. return new MakeBuildConfiguration (project, tree, *this);
  122. }
  123. private:
  124. //==============================================================================
  125. void findAllFilesToCompile (const Project::Item& projectItem, Array<RelativePath>& results) const
  126. {
  127. if (projectItem.isGroup())
  128. {
  129. for (int i = 0; i < projectItem.getNumChildren(); ++i)
  130. findAllFilesToCompile (projectItem.getChild(i), results);
  131. }
  132. else
  133. {
  134. if (projectItem.shouldBeCompiled())
  135. results.add (RelativePath (projectItem.getFile(), getTargetFolder(), RelativePath::buildTargetFolder));
  136. }
  137. }
  138. void writeDefineFlags (OutputStream& out, const BuildConfiguration& config) const
  139. {
  140. StringPairArray defines;
  141. defines.set ("LINUX", "1");
  142. if (config.isDebug())
  143. {
  144. defines.set ("DEBUG", "1");
  145. defines.set ("_DEBUG", "1");
  146. }
  147. else
  148. {
  149. defines.set ("NDEBUG", "1");
  150. }
  151. out << createGCCPreprocessorFlags (mergePreprocessorDefs (defines, getAllPreprocessorDefs (config)));
  152. }
  153. void writeHeaderPathFlags (OutputStream& out, const BuildConfiguration& config) const
  154. {
  155. StringArray searchPaths (extraSearchPaths);
  156. searchPaths.addArray (config.getHeaderSearchPaths());
  157. StringArray packages;
  158. packages.addTokens (getExtraPkgConfigString(), " ", "\"'");
  159. packages.removeEmptyStrings();
  160. if (linuxPackages.size() > 0 || packages.size() > 0)
  161. {
  162. out << " $(shell pkg-config --cflags";
  163. for (int i = 0; i < linuxPackages.size(); ++i)
  164. out << " " << linuxPackages[i];
  165. for (int i = 0; i < packages.size(); ++i)
  166. out << " " << packages[i];
  167. out << ")";
  168. }
  169. if (linuxLibs.contains("pthread"))
  170. out << " -pthread";
  171. searchPaths = getCleanedStringArray (searchPaths);
  172. // Replace ~ character with $(HOME) environment variable
  173. for (int i = 0; i < searchPaths.size(); ++i)
  174. out << " -I" << escapeSpaces (FileHelpers::unixStylePath (replacePreprocessorTokens (config, searchPaths[i]))).replace ("~", "$(HOME)");
  175. }
  176. void writeCppFlags (OutputStream& out, const BuildConfiguration& config) const
  177. {
  178. out << " JUCE_CPPFLAGS := $(DEPFLAGS)";
  179. writeDefineFlags (out, config);
  180. writeHeaderPathFlags (out, config);
  181. out << newLine;
  182. }
  183. void writeLinkerFlags (OutputStream& out, const BuildConfiguration& config) const
  184. {
  185. out << " JUCE_LDFLAGS += $(LDFLAGS) $(TARGET_ARCH) -L$(JUCE_BINDIR) -L$(JUCE_LIBDIR)";
  186. {
  187. StringArray flags (makefileExtraLinkerFlags);
  188. if (makefileIsDLL)
  189. flags.add ("-shared");
  190. if (! config.isDebug())
  191. flags.add ("-fvisibility=hidden");
  192. if (flags.size() > 0)
  193. out << " " << getCleanedStringArray (flags).joinIntoString (" ");
  194. }
  195. out << config.getGCCLibraryPathFlags();
  196. StringArray packages;
  197. packages.addTokens (getExtraPkgConfigString(), " ", "\"'");
  198. packages.removeEmptyStrings();
  199. if (linuxPackages.size() > 0 || packages.size() > 0)
  200. {
  201. out << " $(shell pkg-config --libs";
  202. for (int i = 0; i < linuxPackages.size(); ++i)
  203. out << " " << linuxPackages[i];
  204. for (int i = 0; i < packages.size(); ++i)
  205. out << " " << packages[i];
  206. out << ")";
  207. }
  208. for (int i = 0; i < linuxLibs.size(); ++i)
  209. out << " -l" << linuxLibs[i];
  210. StringArray libraries;
  211. libraries.addTokens (getExternalLibrariesString(), ";", "\"'");
  212. libraries.removeEmptyStrings();
  213. if (libraries.size() != 0)
  214. out << " -l" << replacePreprocessorTokens (config, libraries.joinIntoString (" -l")).trim();
  215. out << " " << replacePreprocessorTokens (config, getExtraLinkerFlagsString()).trim()
  216. << newLine;
  217. }
  218. void writeConfig (OutputStream& out, const BuildConfiguration& config) const
  219. {
  220. const String buildDirName ("build");
  221. const String intermediatesDirName (buildDirName + "/intermediate/" + config.getName());
  222. String outputDir (buildDirName);
  223. if (config.getTargetBinaryRelativePathString().isNotEmpty())
  224. {
  225. RelativePath binaryPath (config.getTargetBinaryRelativePathString(), RelativePath::projectFolder);
  226. outputDir = binaryPath.rebased (projectFolder, getTargetFolder(), RelativePath::buildTargetFolder).toUnixStyle();
  227. }
  228. out << "ifeq ($(CONFIG)," << escapeSpaces (config.getName()) << ")" << newLine;
  229. out << " JUCE_BINDIR := " << escapeSpaces (buildDirName) << newLine
  230. << " JUCE_LIBDIR := " << escapeSpaces (buildDirName) << newLine
  231. << " JUCE_OBJDIR := " << escapeSpaces (intermediatesDirName) << newLine
  232. << " JUCE_OUTDIR := " << escapeSpaces (outputDir) << newLine
  233. << newLine
  234. << " ifeq ($(TARGET_ARCH),)" << newLine
  235. << " TARGET_ARCH := " << getArchFlags (config) << newLine
  236. << " endif" << newLine
  237. << newLine;
  238. writeCppFlags (out, config);
  239. out << " JUCE_CFLAGS += $(CFLAGS) $(JUCE_CPPFLAGS) $(TARGET_ARCH)";
  240. if (config.isDebug())
  241. out << " -g -ggdb";
  242. if (makefileIsDLL)
  243. out << " -fPIC";
  244. out << " -O" << config.getGCCOptimisationFlag()
  245. << (" " + replacePreprocessorTokens (config, getExtraCompilerFlagsString())).trimEnd()
  246. << newLine;
  247. String cppStandardToUse (getCppStandardString());
  248. if (cppStandardToUse.isEmpty())
  249. cppStandardToUse = "-std=c++11";
  250. out << " JUCE_CXXFLAGS += $(CXXFLAGS) $(JUCE_CFLAGS) "
  251. << cppStandardToUse
  252. << newLine;
  253. writeLinkerFlags (out, config);
  254. out << newLine;
  255. String targetName (replacePreprocessorTokens (config, config.getTargetBinaryNameString()));
  256. if (projectType.isStaticLibrary() || projectType.isDynamicLibrary())
  257. targetName = getLibbedFilename (targetName);
  258. else
  259. targetName = targetName.upToLastOccurrenceOf (".", false, false) + makefileTargetSuffix;
  260. out << " TARGET := " << escapeSpaces (targetName) << newLine;
  261. if (projectType.isStaticLibrary())
  262. out << " BLDCMD = $(AR) -rcs $(JUCE_OUTDIR)/$(TARGET) $(OBJECTS)" << newLine;
  263. else
  264. out << " BLDCMD = $(CXX) -o $(JUCE_OUTDIR)/$(TARGET) $(OBJECTS) $(JUCE_LDFLAGS) $(RESOURCES) $(TARGET_ARCH)" << newLine;
  265. out << " CLEANCMD = rm -rf $(JUCE_OUTDIR)/$(TARGET) $(JUCE_OBJDIR)" << newLine
  266. << "endif" << newLine
  267. << newLine;
  268. }
  269. void writeObjects (OutputStream& out, const Array<RelativePath>& files) const
  270. {
  271. out << "OBJECTS := \\" << newLine;
  272. for (int i = 0; i < files.size(); ++i)
  273. if (shouldFileBeCompiledByDefault (files.getReference(i)))
  274. out << " $(JUCE_OBJDIR)/" << escapeSpaces (getObjectFileFor (files.getReference(i))) << " \\" << newLine;
  275. out << newLine;
  276. }
  277. void writeMakefile (OutputStream& out, const Array<RelativePath>& files) const
  278. {
  279. out << "# Automatically generated makefile, created by the Projucer" << newLine
  280. << "# Don't edit this file! Your changes will be overwritten when you re-save the Projucer project!" << newLine
  281. << newLine;
  282. out << "# (this disables dependency generation if multiple architectures are set)" << newLine
  283. << "DEPFLAGS := $(if $(word 2, $(TARGET_ARCH)), , -MMD)" << newLine
  284. << newLine;
  285. out << "ifndef STRIP" << newLine
  286. << " STRIP=strip" << newLine
  287. << "endif" << newLine
  288. << newLine;
  289. out << "ifndef AR" << newLine
  290. << " AR=ar" << newLine
  291. << "endif" << newLine
  292. << newLine;
  293. out << "ifndef CONFIG" << newLine
  294. << " CONFIG=" << escapeSpaces (getConfiguration(0)->getName()) << newLine
  295. << "endif" << newLine
  296. << newLine;
  297. for (ConstConfigIterator config (*this); config.next();)
  298. writeConfig (out, *config);
  299. writeObjects (out, files);
  300. out << ".PHONY: clean" << newLine
  301. << newLine;
  302. StringArray packages;
  303. packages.addTokens (getExtraPkgConfigString(), " ", "\"'");
  304. packages.removeEmptyStrings();
  305. bool useLinuxPackages = (linuxPackages.size() > 0 || packages.size() > 0);
  306. out << "$(JUCE_OUTDIR)/$(TARGET): "
  307. << ((useLinuxPackages) ? "check-pkg-config " : "")
  308. << "$(OBJECTS) $(RESOURCES)" << newLine
  309. << "\t@echo Linking " << projectName << newLine
  310. << "\t-@mkdir -p $(JUCE_BINDIR)" << newLine
  311. << "\t-@mkdir -p $(JUCE_LIBDIR)" << newLine
  312. << "\t-@mkdir -p $(JUCE_OUTDIR)" << newLine
  313. << "\t@$(BLDCMD)" << newLine
  314. << newLine;
  315. if (useLinuxPackages)
  316. {
  317. out << "check-pkg-config:" << newLine
  318. << "\t@command -v pkg-config >/dev/null 2>&1 || "
  319. "{ echo >&2 \"pkg-config not installed. Please, install it.\"; "
  320. "exit 1; }" << newLine
  321. << "\t@pkg-config --print-errors";
  322. for (int i = 0; i < linuxPackages.size(); ++i)
  323. out << " " << linuxPackages[i];
  324. for (int i = 0; i < packages.size(); ++i)
  325. out << " " << packages[i];
  326. out << newLine << newLine;
  327. }
  328. out << "clean:" << newLine
  329. << "\t@echo Cleaning " << projectName << newLine
  330. << "\t@$(CLEANCMD)" << newLine
  331. << newLine;
  332. out << "strip:" << newLine
  333. << "\t@echo Stripping " << projectName << newLine
  334. << "\t-@$(STRIP) --strip-unneeded $(JUCE_OUTDIR)/$(TARGET)" << newLine
  335. << newLine;
  336. for (int i = 0; i < files.size(); ++i)
  337. {
  338. if (shouldFileBeCompiledByDefault (files.getReference(i)))
  339. {
  340. jassert (files.getReference(i).getRoot() == RelativePath::buildTargetFolder);
  341. out << "$(JUCE_OBJDIR)/" << escapeSpaces (getObjectFileFor (files.getReference(i)))
  342. << ": " << escapeSpaces (files.getReference(i).toUnixStyle()) << newLine
  343. << "\t-@mkdir -p $(JUCE_OBJDIR)" << newLine
  344. << "\t@echo \"Compiling " << files.getReference(i).getFileName() << "\"" << newLine
  345. << (files.getReference(i).hasFileExtension ("c;s;S") ? "\t@$(CC) $(JUCE_CFLAGS) -o \"$@\" -c \"$<\""
  346. : "\t@$(CXX) $(JUCE_CXXFLAGS) -o \"$@\" -c \"$<\"")
  347. << newLine << newLine;
  348. }
  349. }
  350. out << "-include $(OBJECTS:%.o=%.d)" << newLine;
  351. }
  352. String getArchFlags (const BuildConfiguration& config) const
  353. {
  354. if (const MakeBuildConfiguration* makeConfig = dynamic_cast<const MakeBuildConfiguration*> (&config))
  355. if (! makeConfig->getArchitectureTypeVar().isVoid())
  356. return makeConfig->getArchitectureTypeVar();
  357. return "-march=native";
  358. }
  359. String getObjectFileFor (const RelativePath& file) const
  360. {
  361. return file.getFileNameWithoutExtension()
  362. + "_" + String::toHexString (file.toUnixStyle().hashCode()) + ".o";
  363. }
  364. void initialiseDependencyPathValues()
  365. {
  366. vst3Path.referTo (Value (new DependencyPathValueSource (getSetting (Ids::vst3Folder),
  367. Ids::vst3Path,
  368. TargetOS::linux)));
  369. }
  370. JUCE_DECLARE_NON_COPYABLE (MakefileProjectExporter)
  371. };