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.

334 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. if (project.isAudioPlugin())
  134. out << " -shared";
  135. {
  136. Array<RelativePath> libraryPaths;
  137. libraryPaths.add (RelativePath ("/usr/X11R6/lib/", RelativePath::unknown));
  138. libraryPaths.add (getJucePathFromTargetFolder().getChildFile ("bin"));
  139. for (int i = 0; i < libraryPaths.size(); ++i)
  140. out << " -L" << libraryPaths.getReference(i).toUnixStyle().quoted();
  141. }
  142. const char* defaultLibs[] = { "freetype", "pthread", "rt", "X11", "GL", "GLU", "Xinerama", "asound", 0 };
  143. StringArray libs (defaultLibs);
  144. if (project.getJuceLinkageMode() == Project::useLinkedJuce)
  145. libs.add ("juce");
  146. for (int i = 0; i < libs.size(); ++i)
  147. out << " -l" << libs[i];
  148. out << " " << getExtraLinkerFlags().toString().trim()
  149. << newLine;
  150. }
  151. void writeConfig (OutputStream& out, const Project::BuildConfiguration& config)
  152. {
  153. const String buildDirName ("build");
  154. const String intermediatesDirName (buildDirName + "/intermediate/" + config.getName().toString());
  155. out << "ifeq ($(CONFIG)," << escapeSpaces (config.getName().toString()) << ")" << newLine;
  156. out << " BINDIR := " << escapeSpaces (buildDirName) << newLine
  157. << " LIBDIR := " << escapeSpaces (buildDirName) << newLine
  158. << " OBJDIR := " << escapeSpaces (intermediatesDirName) << newLine
  159. << " OUTDIR := " << escapeSpaces (buildDirName) << newLine;
  160. writeCppFlags (out, config);
  161. out << " CFLAGS += $(CPPFLAGS) $(TARGET_ARCH)";
  162. if (config.isDebug().getValue())
  163. out << " -g -ggdb";
  164. if (project.isAudioPlugin())
  165. out << " -fPIC";
  166. out << " -O" << config.getGCCOptimisationFlag() << newLine;
  167. out << " CXXFLAGS += $(CFLAGS) " << getExtraCompilerFlags().toString().trim() << newLine;
  168. writeLinkerFlags (out, config);
  169. out << " LDDEPS :=" << newLine
  170. << " RESFLAGS := ";
  171. writeDefineFlags (out, config);
  172. writeHeaderPathFlags (out, config);
  173. out << newLine;
  174. String targetName (config.getTargetBinaryName().getValue().toString());
  175. if (project.isLibrary())
  176. targetName = getLibbedFilename (targetName);
  177. out << " TARGET := " << escapeSpaces (targetName) << newLine;
  178. if (project.isLibrary())
  179. out << " BLDCMD = ar -rcs $(OUTDIR)/$(TARGET) $(OBJECTS) $(TARGET_ARCH)" << newLine;
  180. else
  181. out << " BLDCMD = $(CXX) -o $(OUTDIR)/$(TARGET) $(OBJECTS) $(LDFLAGS) $(RESOURCES) $(TARGET_ARCH)" << newLine;
  182. out << "endif" << newLine << newLine;
  183. }
  184. void writeObjects (OutputStream& out, const Array<RelativePath>& files)
  185. {
  186. out << "OBJECTS := \\" << newLine;
  187. for (int i = 0; i < files.size(); ++i)
  188. if (shouldFileBeCompiledByDefault (files.getReference(i)))
  189. out << " $(OBJDIR)/" << escapeSpaces (getObjectFileFor (files.getReference(i))) << " \\" << newLine;
  190. out << newLine;
  191. }
  192. void writeMakefile (OutputStream& out, const Array<RelativePath>& files)
  193. {
  194. out << "# Automatically generated makefile, created by the Jucer" << newLine
  195. << "# Don't edit this file! Your changes will be overwritten when you re-save the Jucer project!" << newLine
  196. << newLine;
  197. out << "ifndef CONFIG" << newLine
  198. << " CONFIG=" << escapeSpaces (project.getConfiguration(0).getName().toString()) << newLine
  199. << "endif" << newLine
  200. << newLine;
  201. if (! project.isLibrary())
  202. out << "ifeq ($(TARGET_ARCH),)" << newLine
  203. << " TARGET_ARCH := -march=native" << newLine
  204. << "endif" << newLine << newLine;
  205. out << "# (this disables dependency generation if multiple architectures are set)" << newLine
  206. << "DEPFLAGS := $(if $(word 2, $(TARGET_ARCH)), , -MMD)" << newLine
  207. << newLine;
  208. int i;
  209. for (i = 0; i < project.getNumConfigurations(); ++i)
  210. writeConfig (out, project.getConfiguration(i));
  211. writeObjects (out, files);
  212. out << ".PHONY: clean" << newLine
  213. << newLine;
  214. out << "$(OUTDIR)/$(TARGET): $(OBJECTS) $(LDDEPS) $(RESOURCES)" << newLine
  215. << "\t@echo Linking " << project.getProjectName() << newLine
  216. << "\t-@mkdir -p $(BINDIR)" << newLine
  217. << "\t-@mkdir -p $(LIBDIR)" << newLine
  218. << "\t-@mkdir -p $(OUTDIR)" << newLine
  219. << "\t@$(BLDCMD)" << newLine
  220. << newLine;
  221. out << "clean:" << newLine
  222. << "\t@echo Cleaning " << project.getProjectName() << newLine
  223. << "\t-@rm -f $(OUTDIR)/$(TARGET)" << newLine
  224. << "\t-@rm -rf $(OBJDIR)/*" << newLine
  225. << "\t-@rm -rf $(OBJDIR)" << newLine
  226. << newLine;
  227. for (i = 0; i < files.size(); ++i)
  228. {
  229. if (shouldFileBeCompiledByDefault (files.getReference(i)))
  230. {
  231. jassert (files.getReference(i).getRoot() == RelativePath::buildTargetFolder);
  232. out << "$(OBJDIR)/" << escapeSpaces (getObjectFileFor (files.getReference(i)))
  233. << ": " << escapeSpaces (files.getReference(i).toUnixStyle()) << newLine
  234. << "\t-@mkdir -p $(OBJDIR)" << newLine
  235. << "\t@echo \"Compiling " << files.getReference(i).getFileName() << "\"" << newLine
  236. << (files.getReference(i).hasFileExtension (".c") ? "\t@$(CC) $(CFLAGS) -o \"$@\" -c \"$<\""
  237. : "\t@$(CXX) $(CXXFLAGS) -o \"$@\" -c \"$<\"")
  238. << newLine << newLine;
  239. }
  240. }
  241. out << "-include $(OBJECTS:%.o=%.d)" << newLine;
  242. }
  243. static const String escapeSpaces (const String& s)
  244. {
  245. return s.replace (" ", "\\ ");
  246. }
  247. const String getObjectFileFor (const RelativePath& file) const
  248. {
  249. return file.getFileNameWithoutExtension()
  250. + "_" + String::toHexString (file.toUnixStyle().hashCode()) + ".o";
  251. }
  252. MakefileProjectExporter (const MakefileProjectExporter&);
  253. MakefileProjectExporter& operator= (const MakefileProjectExporter&);
  254. };
  255. #endif // __JUCER_PROJECTEXPORT_MAKE_JUCEHEADER__