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.

340 lines
13KB

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