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
13KB

  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. //==============================================================================
  45. int getLaunchPreferenceOrderForCurrentOS()
  46. {
  47. #if JUCE_LINUX
  48. return 1;
  49. #else
  50. return 0;
  51. #endif
  52. }
  53. bool isPossibleForCurrentProject() { return true; }
  54. bool usesMMFiles() const { return false; }
  55. bool isLinux() const { return true; }
  56. void launchProject()
  57. {
  58. // what to do on linux?
  59. }
  60. void createPropertyEditors (Array <PropertyComponent*>& props)
  61. {
  62. ProjectExporter::createPropertyEditors (props);
  63. }
  64. //==============================================================================
  65. void create()
  66. {
  67. Array<RelativePath> files;
  68. findAllFilesToCompile (project.getMainGroup(), files);
  69. for (int i = 0; i < generatedGroups.size(); ++i)
  70. findAllFilesToCompile (generatedGroups.getReference(i), files);
  71. MemoryOutputStream mo;
  72. writeMakefile (mo, files);
  73. overwriteFileIfDifferentOrThrow (getTargetFolder().getChildFile ("Makefile"), mo);
  74. }
  75. private:
  76. //==============================================================================
  77. void findAllFilesToCompile (const Project::Item& projectItem, Array<RelativePath>& results)
  78. {
  79. if (projectItem.isGroup())
  80. {
  81. for (int i = 0; i < projectItem.getNumChildren(); ++i)
  82. findAllFilesToCompile (projectItem.getChild(i), results);
  83. }
  84. else
  85. {
  86. if (projectItem.shouldBeCompiled())
  87. results.add (RelativePath (projectItem.getFile(), getTargetFolder(), RelativePath::buildTargetFolder));
  88. }
  89. }
  90. void writeDefineFlags (OutputStream& out, const Project::BuildConfiguration& config)
  91. {
  92. StringPairArray defines;
  93. defines.set ("LINUX", "1");
  94. if (config.isDebug().getValue())
  95. {
  96. defines.set ("DEBUG", "1");
  97. defines.set ("_DEBUG", "1");
  98. }
  99. else
  100. {
  101. defines.set ("NDEBUG", "1");
  102. }
  103. out << createGCCPreprocessorFlags (mergePreprocessorDefs (defines, getAllPreprocessorDefs (config)));
  104. }
  105. void writeHeaderPathFlags (OutputStream& out, const Project::BuildConfiguration& config)
  106. {
  107. StringArray headerPaths (config.getHeaderSearchPaths());
  108. headerPaths.insert (0, "/usr/include/freetype2");
  109. headerPaths.insert (0, "/usr/include");
  110. for (int i = 0; i < libraryModules.size(); ++i)
  111. libraryModules.getUnchecked(i)->addExtraSearchPaths (*this, headerPaths);
  112. for (int i = 0; i < headerPaths.size(); ++i)
  113. out << " -I " << FileHelpers::unixStylePath (replacePreprocessorTokens (config, headerPaths[i])).quoted();
  114. }
  115. void writeCppFlags (OutputStream& out, const Project::BuildConfiguration& config)
  116. {
  117. out << " CPPFLAGS := $(DEPFLAGS)";
  118. writeDefineFlags (out, config);
  119. writeHeaderPathFlags (out, config);
  120. out << newLine;
  121. }
  122. void writeLinkerFlags (OutputStream& out, const Project::BuildConfiguration& config)
  123. {
  124. out << " LDFLAGS += -L$(BINDIR) -L$(LIBDIR)";
  125. if (project.getProjectType().isAudioPlugin())
  126. out << " -shared";
  127. {
  128. Array<RelativePath> libraryPaths;
  129. libraryPaths.add (RelativePath ("/usr/X11R6/lib/", RelativePath::unknown));
  130. libraryPaths.add (getJucePathFromTargetFolder().getChildFile ("bin"));
  131. for (int i = 0; i < libraryPaths.size(); ++i)
  132. out << " -L" << libraryPaths.getReference(i).toUnixStyle().quoted();
  133. }
  134. const char* defaultLibs[] = { "freetype", "pthread", "rt", "X11", "GL", "GLU", "Xinerama", "asound", 0 };
  135. StringArray libs (defaultLibs);
  136. if (project.getJuceLinkageMode() == Project::useLinkedJuce)
  137. libs.add ("juce");
  138. for (int i = 0; i < libs.size(); ++i)
  139. out << " -l" << libs[i];
  140. out << " " << replacePreprocessorTokens (config, getExtraLinkerFlags().toString()).trim()
  141. << newLine;
  142. }
  143. void writeConfig (OutputStream& out, const Project::BuildConfiguration& config)
  144. {
  145. const String buildDirName ("build");
  146. const String intermediatesDirName (buildDirName + "/intermediate/" + config.getName().toString());
  147. String outputDir (buildDirName);
  148. if (config.getTargetBinaryRelativePath().toString().isNotEmpty())
  149. {
  150. RelativePath binaryPath (config.getTargetBinaryRelativePath().toString(), RelativePath::projectFolder);
  151. outputDir = binaryPath.rebased (project.getFile().getParentDirectory(), getTargetFolder(), RelativePath::buildTargetFolder).toUnixStyle();
  152. }
  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 (outputDir) << newLine;
  158. writeCppFlags (out, config);
  159. out << " CFLAGS += $(CPPFLAGS) $(TARGET_ARCH)";
  160. if (config.isDebug().getValue())
  161. out << " -g -ggdb";
  162. if (project.getProjectType().isAudioPlugin())
  163. out << " -fPIC";
  164. out << " -O" << config.getGCCOptimisationFlag() << newLine;
  165. out << " CXXFLAGS += $(CFLAGS) " << replacePreprocessorTokens (config, getExtraCompilerFlags().toString()).trim() << newLine;
  166. writeLinkerFlags (out, config);
  167. out << " LDDEPS :=" << newLine
  168. << " RESFLAGS := ";
  169. writeDefineFlags (out, config);
  170. writeHeaderPathFlags (out, config);
  171. out << newLine;
  172. String targetName (config.getTargetBinaryName().getValue().toString());
  173. if (project.getProjectType().isLibrary())
  174. targetName = getLibbedFilename (targetName);
  175. else if (isVST())
  176. targetName = targetName.upToLastOccurrenceOf (".", false, false) + ".so";
  177. out << " TARGET := " << escapeSpaces (targetName) << newLine;
  178. if (project.getProjectType().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.getProjectType().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. String getObjectFileFor (const RelativePath& file) const
  244. {
  245. return file.getFileNameWithoutExtension()
  246. + "_" + String::toHexString (file.toUnixStyle().hashCode()) + ".o";
  247. }
  248. JUCE_DECLARE_NON_COPYABLE (MakefileProjectExporter);
  249. };
  250. #endif // __JUCER_PROJECTEXPORT_MAKE_JUCEHEADER__