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.

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