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.

351 lines
14KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software 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. static MakefileProjectExporter* createForSettings (Project& project, const ValueTree& settings)
  24. {
  25. if (settings.hasType (getValueTreeTypeName()))
  26. return new MakefileProjectExporter (project, settings);
  27. return nullptr;
  28. }
  29. //==============================================================================
  30. MakefileProjectExporter (Project& p, const ValueTree& t) : ProjectExporter (p, t)
  31. {
  32. name = getNameLinux();
  33. if (getTargetLocationString().isEmpty())
  34. getTargetLocationValue() = getDefaultBuildsRootFolder() + "LinuxMakefile";
  35. }
  36. //==============================================================================
  37. bool canLaunchProject() override { return false; }
  38. bool launchProject() override { return false; }
  39. bool usesMMFiles() const override { return false; }
  40. bool isLinuxMakefile() const override { return true; }
  41. bool isLinux() const override { return true; }
  42. bool canCopeWithDuplicateFiles() override { return false; }
  43. void createExporterProperties (PropertyListBuilder&) override
  44. {
  45. }
  46. //==============================================================================
  47. void create (const OwnedArray<LibraryModule>&) const override
  48. {
  49. Array<RelativePath> files;
  50. for (int i = 0; i < getAllGroups().size(); ++i)
  51. findAllFilesToCompile (getAllGroups().getReference(i), files);
  52. MemoryOutputStream mo;
  53. writeMakefile (mo, files);
  54. overwriteFileIfDifferentOrThrow (getTargetFolder().getChildFile ("Makefile"), mo);
  55. }
  56. protected:
  57. //==============================================================================
  58. class MakeBuildConfiguration : public BuildConfiguration
  59. {
  60. public:
  61. MakeBuildConfiguration (Project& p, const ValueTree& settings)
  62. : BuildConfiguration (p, settings)
  63. {
  64. setValueIfVoid (getLibrarySearchPathValue(), "/usr/X11R6/lib/");
  65. }
  66. Value getArchitectureType() { return getValue (Ids::linuxArchitecture); }
  67. var getArchitectureTypeVar() const { return config [Ids::linuxArchitecture]; }
  68. var getDefaultOptimisationLevel() const override { return var ((int) (isDebug() ? gccO0 : gccO3)); }
  69. void createConfigProperties (PropertyListBuilder& props) override
  70. {
  71. addGCCOptimisationProperty (props);
  72. static const char* const archNames[] = { "(Default)", "<None>", "32-bit (-m32)", "64-bit (-m64)", "ARM v6", "ARM v7" };
  73. const var archFlags[] = { var(), var (String()), "-m32", "-m64", "-march=armv6", "-march=armv7" };
  74. props.add (new ChoicePropertyComponent (getArchitectureType(), "Architecture",
  75. StringArray (archNames, numElementsInArray (archNames)),
  76. Array<var> (archFlags, numElementsInArray (archFlags))));
  77. }
  78. };
  79. BuildConfiguration::Ptr createBuildConfig (const ValueTree& tree) const override
  80. {
  81. return new MakeBuildConfiguration (project, tree);
  82. }
  83. private:
  84. //==============================================================================
  85. void findAllFilesToCompile (const Project::Item& projectItem, Array<RelativePath>& results) const
  86. {
  87. if (projectItem.isGroup())
  88. {
  89. for (int i = 0; i < projectItem.getNumChildren(); ++i)
  90. findAllFilesToCompile (projectItem.getChild(i), results);
  91. }
  92. else
  93. {
  94. if (projectItem.shouldBeCompiled())
  95. results.add (RelativePath (projectItem.getFile(), getTargetFolder(), RelativePath::buildTargetFolder));
  96. }
  97. }
  98. void writeDefineFlags (OutputStream& out, const BuildConfiguration& config) const
  99. {
  100. StringPairArray defines;
  101. defines.set ("LINUX", "1");
  102. if (config.isDebug())
  103. {
  104. defines.set ("DEBUG", "1");
  105. defines.set ("_DEBUG", "1");
  106. }
  107. else
  108. {
  109. defines.set ("NDEBUG", "1");
  110. }
  111. out << createGCCPreprocessorFlags (mergePreprocessorDefs (defines, getAllPreprocessorDefs (config)));
  112. }
  113. void writeHeaderPathFlags (OutputStream& out, const BuildConfiguration& config) const
  114. {
  115. StringArray searchPaths (extraSearchPaths);
  116. searchPaths.addArray (config.getHeaderSearchPaths());
  117. searchPaths.insert (0, "/usr/include/freetype2");
  118. searchPaths.insert (0, "/usr/include");
  119. searchPaths = getCleanedStringArray (searchPaths);
  120. for (int i = 0; i < searchPaths.size(); ++i)
  121. out << " -I " << escapeSpaces (FileHelpers::unixStylePath (replacePreprocessorTokens (config, searchPaths[i])));
  122. }
  123. void writeCppFlags (OutputStream& out, const BuildConfiguration& config) const
  124. {
  125. out << " CPPFLAGS := $(DEPFLAGS)";
  126. writeDefineFlags (out, config);
  127. writeHeaderPathFlags (out, config);
  128. out << newLine;
  129. }
  130. void writeLinkerFlags (OutputStream& out, const BuildConfiguration& config) const
  131. {
  132. out << " LDFLAGS += $(TARGET_ARCH) -L$(BINDIR) -L$(LIBDIR)";
  133. {
  134. StringArray flags (makefileExtraLinkerFlags);
  135. if (makefileIsDLL)
  136. flags.add ("-shared");
  137. if (! config.isDebug())
  138. flags.add ("-fvisibility=hidden");
  139. if (flags.size() > 0)
  140. out << " " << getCleanedStringArray (flags).joinIntoString (" ");
  141. }
  142. out << config.getGCCLibraryPathFlags();
  143. for (int i = 0; i < linuxLibs.size(); ++i)
  144. out << " -l" << linuxLibs[i];
  145. if (getProject().isConfigFlagEnabled ("JUCE_USE_CURL"))
  146. out << " -lcurl";
  147. StringArray libraries;
  148. libraries.addTokens (getExternalLibrariesString(), ";", "\"'");
  149. libraries.removeEmptyStrings();
  150. if (libraries.size() != 0)
  151. out << " -l" << replacePreprocessorTokens (config, libraries.joinIntoString (" -l")).trim();
  152. out << " " << replacePreprocessorTokens (config, getExtraLinkerFlagsString()).trim()
  153. << newLine;
  154. }
  155. void writeConfig (OutputStream& out, const BuildConfiguration& config) const
  156. {
  157. const String buildDirName ("build");
  158. const String intermediatesDirName (buildDirName + "/intermediate/" + config.getName());
  159. String outputDir (buildDirName);
  160. if (config.getTargetBinaryRelativePathString().isNotEmpty())
  161. {
  162. RelativePath binaryPath (config.getTargetBinaryRelativePathString(), RelativePath::projectFolder);
  163. outputDir = binaryPath.rebased (projectFolder, getTargetFolder(), RelativePath::buildTargetFolder).toUnixStyle();
  164. }
  165. out << "ifeq ($(CONFIG)," << escapeSpaces (config.getName()) << ")" << newLine;
  166. out << " BINDIR := " << escapeSpaces (buildDirName) << newLine
  167. << " LIBDIR := " << escapeSpaces (buildDirName) << newLine
  168. << " OBJDIR := " << escapeSpaces (intermediatesDirName) << newLine
  169. << " OUTDIR := " << escapeSpaces (outputDir) << newLine
  170. << newLine
  171. << " ifeq ($(TARGET_ARCH),)" << newLine
  172. << " TARGET_ARCH := " << getArchFlags (config) << newLine
  173. << " endif" << newLine
  174. << newLine;
  175. writeCppFlags (out, config);
  176. out << " CFLAGS += $(CPPFLAGS) $(TARGET_ARCH)";
  177. if (config.isDebug())
  178. out << " -g -ggdb";
  179. if (makefileIsDLL)
  180. out << " -fPIC";
  181. out << " -O" << config.getGCCOptimisationFlag()
  182. << (" " + replacePreprocessorTokens (config, getExtraCompilerFlagsString())).trimEnd()
  183. << newLine;
  184. out << " CXXFLAGS += $(CFLAGS) -std=c++11" << newLine;
  185. writeLinkerFlags (out, config);
  186. out << newLine;
  187. String targetName (replacePreprocessorTokens (config, config.getTargetBinaryNameString()));
  188. if (projectType.isStaticLibrary() || projectType.isDynamicLibrary())
  189. targetName = getLibbedFilename (targetName);
  190. else
  191. targetName = targetName.upToLastOccurrenceOf (".", false, false) + makefileTargetSuffix;
  192. out << " TARGET := " << escapeSpaces (targetName) << newLine;
  193. if (projectType.isStaticLibrary())
  194. out << " BLDCMD = ar -rcs $(OUTDIR)/$(TARGET) $(OBJECTS)" << newLine;
  195. else
  196. out << " BLDCMD = $(CXX) -o $(OUTDIR)/$(TARGET) $(OBJECTS) $(LDFLAGS) $(RESOURCES) $(TARGET_ARCH)" << newLine;
  197. out << " CLEANCMD = rm -rf $(OUTDIR)/$(TARGET) $(OBJDIR)" << newLine
  198. << "endif" << newLine
  199. << newLine;
  200. }
  201. void writeObjects (OutputStream& out, const Array<RelativePath>& files) const
  202. {
  203. out << "OBJECTS := \\" << newLine;
  204. for (int i = 0; i < files.size(); ++i)
  205. if (shouldFileBeCompiledByDefault (files.getReference(i)))
  206. out << " $(OBJDIR)/" << escapeSpaces (getObjectFileFor (files.getReference(i))) << " \\" << newLine;
  207. out << newLine;
  208. }
  209. void writeMakefile (OutputStream& out, const Array<RelativePath>& files) const
  210. {
  211. out << "# Automatically generated makefile, created by the Introjucer" << newLine
  212. << "# Don't edit this file! Your changes will be overwritten when you re-save the Introjucer project!" << newLine
  213. << newLine;
  214. out << "# (this disables dependency generation if multiple architectures are set)" << newLine
  215. << "DEPFLAGS := $(if $(word 2, $(TARGET_ARCH)), , -MMD)" << newLine
  216. << newLine;
  217. out << "ifndef CONFIG" << newLine
  218. << " CONFIG=" << escapeSpaces (getConfiguration(0)->getName()) << newLine
  219. << "endif" << newLine
  220. << newLine;
  221. for (ConstConfigIterator config (*this); config.next();)
  222. writeConfig (out, *config);
  223. writeObjects (out, files);
  224. out << ".PHONY: clean" << newLine
  225. << newLine;
  226. out << "$(OUTDIR)/$(TARGET): $(OBJECTS) $(RESOURCES)" << newLine
  227. << "\t@echo Linking " << projectName << newLine
  228. << "\t-@mkdir -p $(BINDIR)" << newLine
  229. << "\t-@mkdir -p $(LIBDIR)" << newLine
  230. << "\t-@mkdir -p $(OUTDIR)" << newLine
  231. << "\t@$(BLDCMD)" << newLine
  232. << newLine;
  233. out << "clean:" << newLine
  234. << "\t@echo Cleaning " << projectName << newLine
  235. << "\t@$(CLEANCMD)" << newLine
  236. << newLine;
  237. out << "strip:" << newLine
  238. << "\t@echo Stripping " << projectName << newLine
  239. << "\t-@strip --strip-unneeded $(OUTDIR)/$(TARGET)" << newLine
  240. << newLine;
  241. for (int i = 0; i < files.size(); ++i)
  242. {
  243. if (shouldFileBeCompiledByDefault (files.getReference(i)))
  244. {
  245. jassert (files.getReference(i).getRoot() == RelativePath::buildTargetFolder);
  246. out << "$(OBJDIR)/" << escapeSpaces (getObjectFileFor (files.getReference(i)))
  247. << ": " << escapeSpaces (files.getReference(i).toUnixStyle()) << newLine
  248. << "\t-@mkdir -p $(OBJDIR)" << newLine
  249. << "\t@echo \"Compiling " << files.getReference(i).getFileName() << "\"" << newLine
  250. << (files.getReference(i).hasFileExtension ("c;s;S") ? "\t@$(CC) $(CFLAGS) -o \"$@\" -c \"$<\""
  251. : "\t@$(CXX) $(CXXFLAGS) -o \"$@\" -c \"$<\"")
  252. << newLine << newLine;
  253. }
  254. }
  255. out << "-include $(OBJECTS:%.o=%.d)" << newLine;
  256. }
  257. String getArchFlags (const BuildConfiguration& config) const
  258. {
  259. if (const MakeBuildConfiguration* makeConfig = dynamic_cast<const MakeBuildConfiguration*> (&config))
  260. if (! makeConfig->getArchitectureTypeVar().isVoid())
  261. return makeConfig->getArchitectureTypeVar();
  262. return "-march=native";
  263. }
  264. String getObjectFileFor (const RelativePath& file) const
  265. {
  266. return file.getFileNameWithoutExtension()
  267. + "_" + String::toHexString (file.toUnixStyle().hashCode()) + ".o";
  268. }
  269. JUCE_DECLARE_NON_COPYABLE (MakefileProjectExporter)
  270. };