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.

322 lines
12KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-10 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 0;
  33. }
  34. //==============================================================================
  35. MakefileProjectExporter (Project& project_, const ValueTree& settings_)
  36. : ProjectExporter (project_, settings_)
  37. {
  38. name = getNameLinux();
  39. if (getTargetLocation().toString().isEmpty())
  40. getTargetLocation() = getDefaultBuildsRootFolder() + "Linux";
  41. }
  42. //==============================================================================
  43. int getLaunchPreferenceOrderForCurrentOS()
  44. {
  45. #if JUCE_LINUX
  46. return 1;
  47. #else
  48. return 0;
  49. #endif
  50. }
  51. bool isPossibleForCurrentProject() { return true; }
  52. bool usesMMFiles() const { return false; }
  53. bool isLinux() const { return true; }
  54. void launchProject()
  55. {
  56. // what to do on linux?
  57. }
  58. void createPropertyEditors (Array <PropertyComponent*>& props)
  59. {
  60. ProjectExporter::createPropertyEditors (props);
  61. }
  62. //==============================================================================
  63. void create()
  64. {
  65. Array<RelativePath> files;
  66. for (int i = 0; i < groups.size(); ++i)
  67. findAllFilesToCompile (groups.getReference(i), files);
  68. MemoryOutputStream mo;
  69. writeMakefile (mo, files);
  70. overwriteFileIfDifferentOrThrow (getTargetFolder().getChildFile ("Makefile"), mo);
  71. }
  72. private:
  73. //==============================================================================
  74. void findAllFilesToCompile (const Project::Item& projectItem, Array<RelativePath>& results)
  75. {
  76. if (projectItem.isGroup())
  77. {
  78. for (int i = 0; i < projectItem.getNumChildren(); ++i)
  79. findAllFilesToCompile (projectItem.getChild(i), results);
  80. }
  81. else
  82. {
  83. if (projectItem.shouldBeCompiled())
  84. results.add (RelativePath (projectItem.getFile(), getTargetFolder(), RelativePath::buildTargetFolder));
  85. }
  86. }
  87. void writeDefineFlags (OutputStream& out, const Project::BuildConfiguration& config)
  88. {
  89. StringPairArray defines;
  90. defines.set ("LINUX", "1");
  91. if (config.isDebug().getValue())
  92. {
  93. defines.set ("DEBUG", "1");
  94. defines.set ("_DEBUG", "1");
  95. }
  96. else
  97. {
  98. defines.set ("NDEBUG", "1");
  99. }
  100. out << createGCCPreprocessorFlags (mergePreprocessorDefs (defines, getAllPreprocessorDefs (config)));
  101. }
  102. void writeHeaderPathFlags (OutputStream& out, const Project::BuildConfiguration& config)
  103. {
  104. StringArray headerPaths (config.getHeaderSearchPaths());
  105. headerPaths.insert (0, "/usr/include/freetype2");
  106. headerPaths.insert (0, "/usr/include");
  107. for (int i = 0; i < libraryModules.size(); ++i)
  108. libraryModules.getUnchecked(i)->addExtraSearchPaths (*this, headerPaths);
  109. for (int i = 0; i < headerPaths.size(); ++i)
  110. out << " -I " << FileHelpers::unixStylePath (replacePreprocessorTokens (config, headerPaths[i])).quoted();
  111. }
  112. void writeCppFlags (OutputStream& out, const Project::BuildConfiguration& config)
  113. {
  114. out << " CPPFLAGS := $(DEPFLAGS)";
  115. writeDefineFlags (out, config);
  116. writeHeaderPathFlags (out, config);
  117. out << newLine;
  118. }
  119. void writeLinkerFlags (OutputStream& out, const Project::BuildConfiguration& config)
  120. {
  121. out << " LDFLAGS += -L$(BINDIR) -L$(LIBDIR)";
  122. if (makefileIsDLL)
  123. out << " -shared";
  124. {
  125. Array<RelativePath> libraryPaths;
  126. libraryPaths.add (RelativePath ("/usr/X11R6/lib/", RelativePath::unknown));
  127. libraryPaths.add (getJucePathFromTargetFolder().getChildFile ("bin"));
  128. for (int i = 0; i < libraryPaths.size(); ++i)
  129. out << " -L" << libraryPaths.getReference(i).toUnixStyle().quoted();
  130. }
  131. const char* defaultLibs[] = { "freetype", "pthread", "rt", "X11", "GL", "GLU", "Xinerama", "asound", 0 };
  132. StringArray libs (defaultLibs);
  133. if (project.getJuceLinkageMode() == Project::useLinkedJuce)
  134. libs.add ("juce");
  135. for (int i = 0; i < libs.size(); ++i)
  136. out << " -l" << libs[i];
  137. out << " " << replacePreprocessorTokens (config, getExtraLinkerFlags().toString()).trim()
  138. << newLine;
  139. }
  140. void writeConfig (OutputStream& out, const Project::BuildConfiguration& config)
  141. {
  142. const String buildDirName ("build");
  143. const String intermediatesDirName (buildDirName + "/intermediate/" + config.getName().toString());
  144. String outputDir (buildDirName);
  145. if (config.getTargetBinaryRelativePath().toString().isNotEmpty())
  146. {
  147. RelativePath binaryPath (config.getTargetBinaryRelativePath().toString(), RelativePath::projectFolder);
  148. outputDir = binaryPath.rebased (projectFolder, getTargetFolder(), RelativePath::buildTargetFolder).toUnixStyle();
  149. }
  150. out << "ifeq ($(CONFIG)," << escapeSpaces (config.getName().toString()) << ")" << newLine;
  151. out << " BINDIR := " << escapeSpaces (buildDirName) << newLine
  152. << " LIBDIR := " << escapeSpaces (buildDirName) << newLine
  153. << " OBJDIR := " << escapeSpaces (intermediatesDirName) << newLine
  154. << " OUTDIR := " << escapeSpaces (outputDir) << newLine;
  155. writeCppFlags (out, config);
  156. out << " CFLAGS += $(CPPFLAGS) $(TARGET_ARCH)";
  157. if (config.isDebug().getValue())
  158. out << " -g -ggdb";
  159. if (makefileIsDLL)
  160. out << " -fPIC";
  161. out << " -O" << config.getGCCOptimisationFlag() << newLine;
  162. out << " CXXFLAGS += $(CFLAGS) " << replacePreprocessorTokens (config, getExtraCompilerFlags().toString()).trim() << newLine;
  163. writeLinkerFlags (out, config);
  164. out << " LDDEPS :=" << newLine
  165. << " RESFLAGS := ";
  166. writeDefineFlags (out, config);
  167. writeHeaderPathFlags (out, config);
  168. out << newLine;
  169. String targetName (config.getTargetBinaryName().getValue().toString());
  170. if (projectType.isLibrary())
  171. targetName = getLibbedFilename (targetName);
  172. else
  173. targetName = targetName.upToLastOccurrenceOf (".", false, false) + makefileTargetSuffix;
  174. out << " TARGET := " << escapeSpaces (targetName) << newLine;
  175. if (projectType.isLibrary())
  176. out << " BLDCMD = ar -rcs $(OUTDIR)/$(TARGET) $(OBJECTS) $(TARGET_ARCH)" << newLine;
  177. else
  178. out << " BLDCMD = $(CXX) -o $(OUTDIR)/$(TARGET) $(OBJECTS) $(LDFLAGS) $(RESOURCES) $(TARGET_ARCH)" << newLine;
  179. out << "endif" << newLine << newLine;
  180. }
  181. void writeObjects (OutputStream& out, const Array<RelativePath>& files)
  182. {
  183. out << "OBJECTS := \\" << newLine;
  184. for (int i = 0; i < files.size(); ++i)
  185. if (shouldFileBeCompiledByDefault (files.getReference(i)))
  186. out << " $(OBJDIR)/" << escapeSpaces (getObjectFileFor (files.getReference(i))) << " \\" << newLine;
  187. out << newLine;
  188. }
  189. void writeMakefile (OutputStream& out, const Array<RelativePath>& files)
  190. {
  191. out << "# Automatically generated makefile, created by the Jucer" << newLine
  192. << "# Don't edit this file! Your changes will be overwritten when you re-save the Jucer project!" << newLine
  193. << newLine;
  194. out << "ifndef CONFIG" << newLine
  195. << " CONFIG=" << escapeSpaces (configs.getReference(0).getName().toString()) << newLine
  196. << "endif" << newLine
  197. << newLine;
  198. if (! projectType.isLibrary())
  199. out << "ifeq ($(TARGET_ARCH),)" << newLine
  200. << " TARGET_ARCH := -march=native" << newLine
  201. << "endif" << newLine << newLine;
  202. out << "# (this disables dependency generation if multiple architectures are set)" << newLine
  203. << "DEPFLAGS := $(if $(word 2, $(TARGET_ARCH)), , -MMD)" << newLine
  204. << newLine;
  205. int i;
  206. for (i = 0; i < configs.size(); ++i)
  207. writeConfig (out, configs.getReference(i));
  208. writeObjects (out, files);
  209. out << ".PHONY: clean" << newLine
  210. << newLine;
  211. out << "$(OUTDIR)/$(TARGET): $(OBJECTS) $(LDDEPS) $(RESOURCES)" << newLine
  212. << "\t@echo Linking " << projectName << newLine
  213. << "\t-@mkdir -p $(BINDIR)" << newLine
  214. << "\t-@mkdir -p $(LIBDIR)" << newLine
  215. << "\t-@mkdir -p $(OUTDIR)" << newLine
  216. << "\t@$(BLDCMD)" << newLine
  217. << newLine;
  218. out << "clean:" << newLine
  219. << "\t@echo Cleaning " << projectName << newLine
  220. << "\t-@rm -f $(OUTDIR)/$(TARGET)" << newLine
  221. << "\t-@rm -rf $(OBJDIR)/*" << newLine
  222. << "\t-@rm -rf $(OBJDIR)" << newLine
  223. << newLine;
  224. for (i = 0; i < files.size(); ++i)
  225. {
  226. if (shouldFileBeCompiledByDefault (files.getReference(i)))
  227. {
  228. jassert (files.getReference(i).getRoot() == RelativePath::buildTargetFolder);
  229. out << "$(OBJDIR)/" << escapeSpaces (getObjectFileFor (files.getReference(i)))
  230. << ": " << escapeSpaces (files.getReference(i).toUnixStyle()) << newLine
  231. << "\t-@mkdir -p $(OBJDIR)" << newLine
  232. << "\t@echo \"Compiling " << files.getReference(i).getFileName() << "\"" << newLine
  233. << (files.getReference(i).hasFileExtension (".c") ? "\t@$(CC) $(CFLAGS) -o \"$@\" -c \"$<\""
  234. : "\t@$(CXX) $(CXXFLAGS) -o \"$@\" -c \"$<\"")
  235. << newLine << newLine;
  236. }
  237. }
  238. out << "-include $(OBJECTS:%.o=%.d)" << newLine;
  239. }
  240. String getObjectFileFor (const RelativePath& file) const
  241. {
  242. return file.getFileNameWithoutExtension()
  243. + "_" + String::toHexString (file.toUnixStyle().hashCode()) + ".o";
  244. }
  245. JUCE_DECLARE_NON_COPYABLE (MakefileProjectExporter);
  246. };
  247. #endif // __JUCER_PROJECTEXPORT_MAKE_JUCEHEADER__