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.

316 lines
12KB

  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. #ifndef __JUCER_PROJECTEXPORT_MAKE_JUCEHEADER__
  19. #define __JUCER_PROJECTEXPORT_MAKE_JUCEHEADER__
  20. #include "jucer_ProjectExporter.h"
  21. //==============================================================================
  22. class MakefileProjectExporter : public ProjectExporter
  23. {
  24. public:
  25. //==============================================================================
  26. static const char* getNameLinux() { return "Linux Makefile"; }
  27. static const char* getValueTreeTypeName() { return "LINUX_MAKE"; }
  28. static MakefileProjectExporter* createForSettings (Project& project, const ValueTree& settings)
  29. {
  30. if (settings.hasType (getValueTreeTypeName()))
  31. return new MakefileProjectExporter (project, settings);
  32. return nullptr;
  33. }
  34. //==============================================================================
  35. MakefileProjectExporter (Project& project_, const ValueTree& settings_)
  36. : ProjectExporter (project_, settings_)
  37. {
  38. name = getNameLinux();
  39. if (getTargetLocationString().isEmpty())
  40. getTargetLocationValue() = getDefaultBuildsRootFolder() + "Linux";
  41. }
  42. //==============================================================================
  43. bool launchProject() { return false; }
  44. bool usesMMFiles() const { return false; }
  45. bool isLinux() const { return true; }
  46. bool canCopeWithDuplicateFiles() { return false; }
  47. void createExporterProperties (PropertyListBuilder&)
  48. {
  49. }
  50. //==============================================================================
  51. void create (const OwnedArray<LibraryModule>&) const
  52. {
  53. Array<RelativePath> files;
  54. for (int i = 0; i < getAllGroups().size(); ++i)
  55. findAllFilesToCompile (getAllGroups().getReference(i), files);
  56. MemoryOutputStream mo;
  57. writeMakefile (mo, files);
  58. overwriteFileIfDifferentOrThrow (getTargetFolder().getChildFile ("Makefile"), mo);
  59. }
  60. protected:
  61. //==============================================================================
  62. class MakeBuildConfiguration : public BuildConfiguration
  63. {
  64. public:
  65. MakeBuildConfiguration (Project& project, const ValueTree& settings)
  66. : BuildConfiguration (project, settings)
  67. {
  68. setValueIfVoid (getLibrarySearchPathValue(), "/usr/X11R6/lib/");
  69. }
  70. void createConfigProperties (PropertyListBuilder&)
  71. {
  72. }
  73. };
  74. BuildConfiguration::Ptr createBuildConfig (const ValueTree& settings) const
  75. {
  76. return new MakeBuildConfiguration (project, settings);
  77. }
  78. private:
  79. //==============================================================================
  80. void findAllFilesToCompile (const Project::Item& projectItem, Array<RelativePath>& results) const
  81. {
  82. if (projectItem.isGroup())
  83. {
  84. for (int i = 0; i < projectItem.getNumChildren(); ++i)
  85. findAllFilesToCompile (projectItem.getChild(i), results);
  86. }
  87. else
  88. {
  89. if (projectItem.shouldBeCompiled())
  90. results.add (RelativePath (projectItem.getFile(), getTargetFolder(), RelativePath::buildTargetFolder));
  91. }
  92. }
  93. void writeDefineFlags (OutputStream& out, const BuildConfiguration& config) const
  94. {
  95. StringPairArray defines;
  96. defines.set ("LINUX", "1");
  97. if (config.isDebug())
  98. {
  99. defines.set ("DEBUG", "1");
  100. defines.set ("_DEBUG", "1");
  101. }
  102. else
  103. {
  104. defines.set ("NDEBUG", "1");
  105. }
  106. out << createGCCPreprocessorFlags (mergePreprocessorDefs (defines, getAllPreprocessorDefs (config)));
  107. }
  108. void writeHeaderPathFlags (OutputStream& out, const BuildConfiguration& config) const
  109. {
  110. StringArray searchPaths (extraSearchPaths);
  111. searchPaths.addArray (config.getHeaderSearchPaths());
  112. searchPaths.insert (0, "/usr/include/freetype2");
  113. searchPaths.insert (0, "/usr/include");
  114. searchPaths.removeDuplicates (false);
  115. for (int i = 0; i < searchPaths.size(); ++i)
  116. out << " -I " << addQuotesIfContainsSpaces (FileHelpers::unixStylePath (replacePreprocessorTokens (config, searchPaths[i])));
  117. }
  118. void writeCppFlags (OutputStream& out, const BuildConfiguration& config) const
  119. {
  120. out << " CPPFLAGS := $(DEPFLAGS)";
  121. writeDefineFlags (out, config);
  122. writeHeaderPathFlags (out, config);
  123. out << newLine;
  124. }
  125. void writeLinkerFlags (OutputStream& out, const BuildConfiguration& config) const
  126. {
  127. out << " LDFLAGS += -L$(BINDIR) -L$(LIBDIR)";
  128. if (makefileIsDLL)
  129. out << " -shared";
  130. out << config.getGCCLibraryPathFlags();
  131. for (int i = 0; i < linuxLibs.size(); ++i)
  132. out << " -l" << linuxLibs[i];
  133. out << " " << replacePreprocessorTokens (config, getExtraLinkerFlagsString()).trim()
  134. << newLine;
  135. }
  136. void writeConfig (OutputStream& out, const BuildConfiguration& config) const
  137. {
  138. const String buildDirName ("build");
  139. const String intermediatesDirName (buildDirName + "/intermediate/" + config.getName());
  140. String outputDir (buildDirName);
  141. if (config.getTargetBinaryRelativePathString().isNotEmpty())
  142. {
  143. RelativePath binaryPath (config.getTargetBinaryRelativePathString(), RelativePath::projectFolder);
  144. outputDir = binaryPath.rebased (projectFolder, getTargetFolder(), RelativePath::buildTargetFolder).toUnixStyle();
  145. }
  146. out << "ifeq ($(CONFIG)," << escapeSpaces (config.getName()) << ")" << newLine;
  147. out << " BINDIR := " << escapeSpaces (buildDirName) << newLine
  148. << " LIBDIR := " << escapeSpaces (buildDirName) << newLine
  149. << " OBJDIR := " << escapeSpaces (intermediatesDirName) << newLine
  150. << " OUTDIR := " << escapeSpaces (outputDir) << newLine;
  151. writeCppFlags (out, config);
  152. out << " CFLAGS += $(CPPFLAGS) $(TARGET_ARCH)";
  153. if (config.isDebug())
  154. out << " -g -ggdb";
  155. if (makefileIsDLL)
  156. out << " -fPIC";
  157. out << " -O" << config.getGCCOptimisationFlag() << newLine;
  158. out << " CXXFLAGS += $(CFLAGS) " << replacePreprocessorTokens (config, getExtraCompilerFlagsString()).trim() << newLine;
  159. writeLinkerFlags (out, config);
  160. out << " LDDEPS :=" << newLine
  161. << " RESFLAGS := ";
  162. writeDefineFlags (out, config);
  163. writeHeaderPathFlags (out, config);
  164. out << newLine;
  165. String targetName (config.getTargetBinaryNameString());
  166. if (projectType.isLibrary())
  167. targetName = getLibbedFilename (targetName);
  168. else
  169. targetName = targetName.upToLastOccurrenceOf (".", false, false) + makefileTargetSuffix;
  170. out << " TARGET := " << escapeSpaces (targetName) << newLine;
  171. if (projectType.isLibrary())
  172. out << " BLDCMD = ar -rcs $(OUTDIR)/$(TARGET) $(OBJECTS) $(TARGET_ARCH)" << newLine;
  173. else
  174. out << " BLDCMD = $(CXX) -o $(OUTDIR)/$(TARGET) $(OBJECTS) $(LDFLAGS) $(RESOURCES) $(TARGET_ARCH)" << newLine;
  175. out << "endif" << newLine << newLine;
  176. }
  177. void writeObjects (OutputStream& out, const Array<RelativePath>& files) const
  178. {
  179. out << "OBJECTS := \\" << newLine;
  180. for (int i = 0; i < files.size(); ++i)
  181. if (shouldFileBeCompiledByDefault (files.getReference(i)))
  182. out << " $(OBJDIR)/" << escapeSpaces (getObjectFileFor (files.getReference(i))) << " \\" << newLine;
  183. out << newLine;
  184. }
  185. void writeMakefile (OutputStream& out, const Array<RelativePath>& files) const
  186. {
  187. out << "# Automatically generated makefile, created by the Introjucer" << newLine
  188. << "# Don't edit this file! Your changes will be overwritten when you re-save the Introjucer project!" << newLine
  189. << newLine;
  190. out << "ifndef CONFIG" << newLine
  191. << " CONFIG=" << escapeSpaces (getConfiguration(0)->getName()) << newLine
  192. << "endif" << newLine
  193. << newLine;
  194. if (! projectType.isLibrary())
  195. out << "ifeq ($(TARGET_ARCH),)" << newLine
  196. << " TARGET_ARCH := -march=native" << newLine
  197. << "endif" << newLine << newLine;
  198. out << "# (this disables dependency generation if multiple architectures are set)" << newLine
  199. << "DEPFLAGS := $(if $(word 2, $(TARGET_ARCH)), , -MMD)" << newLine
  200. << newLine;
  201. for (ConstConfigIterator config (*this); config.next();)
  202. writeConfig (out, *config);
  203. writeObjects (out, files);
  204. out << ".PHONY: clean" << newLine
  205. << newLine;
  206. out << "$(OUTDIR)/$(TARGET): $(OBJECTS) $(LDDEPS) $(RESOURCES)" << newLine
  207. << "\t@echo Linking " << projectName << newLine
  208. << "\t-@mkdir -p $(BINDIR)" << newLine
  209. << "\t-@mkdir -p $(LIBDIR)" << newLine
  210. << "\t-@mkdir -p $(OUTDIR)" << newLine
  211. << "\t@$(BLDCMD)" << newLine
  212. << newLine;
  213. out << "clean:" << newLine
  214. << "\t@echo Cleaning " << projectName << newLine
  215. << "\t-@rm -f $(OUTDIR)/$(TARGET)" << newLine
  216. << "\t-@rm -rf $(OBJDIR)/*" << newLine
  217. << "\t-@rm -rf $(OBJDIR)" << newLine
  218. << newLine;
  219. for (int i = 0; i < files.size(); ++i)
  220. {
  221. if (shouldFileBeCompiledByDefault (files.getReference(i)))
  222. {
  223. jassert (files.getReference(i).getRoot() == RelativePath::buildTargetFolder);
  224. out << "$(OBJDIR)/" << escapeSpaces (getObjectFileFor (files.getReference(i)))
  225. << ": " << escapeSpaces (files.getReference(i).toUnixStyle()) << newLine
  226. << "\t-@mkdir -p $(OBJDIR)" << newLine
  227. << "\t@echo \"Compiling " << files.getReference(i).getFileName() << "\"" << newLine
  228. << (files.getReference(i).hasFileExtension (".c") ? "\t@$(CC) $(CFLAGS) -o \"$@\" -c \"$<\""
  229. : "\t@$(CXX) $(CXXFLAGS) -o \"$@\" -c \"$<\"")
  230. << newLine << newLine;
  231. }
  232. }
  233. out << "-include $(OBJECTS:%.o=%.d)" << newLine;
  234. }
  235. String getObjectFileFor (const RelativePath& file) const
  236. {
  237. return file.getFileNameWithoutExtension()
  238. + "_" + String::toHexString (file.toUnixStyle().hashCode()) + ".o";
  239. }
  240. JUCE_DECLARE_NON_COPYABLE (MakefileProjectExporter);
  241. };
  242. #endif // __JUCER_PROJECTEXPORT_MAKE_JUCEHEADER__