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.

323 lines
12KB

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