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
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. bool isDefaultFormatForCurrentOS()
  46. {
  47. #if JUCE_LINUX
  48. return true;
  49. #else
  50. return false;
  51. #endif
  52. }
  53. bool isPossibleForCurrentProject() { return true; }
  54. bool usesMMFiles() const { return false; }
  55. void launchProject()
  56. {
  57. // what to do on linux?
  58. }
  59. void createPropertyEditors (Array <PropertyComponent*>& props)
  60. {
  61. ProjectExporter::createPropertyEditors (props);
  62. }
  63. //==============================================================================
  64. void create()
  65. {
  66. Array<RelativePath> files;
  67. findAllFilesToCompile (project.getMainGroup(), files);
  68. for (int i = 0; i < juceWrapperFiles.size(); ++i)
  69. if (shouldFileBeCompiledByDefault (juceWrapperFiles.getReference(i)))
  70. files.add (juceWrapperFiles.getReference(i));
  71. const Array<RelativePath> vstFiles (getVSTFilesRequired());
  72. for (int i = 0; i < vstFiles.size(); i++)
  73. files.add (vstFiles.getReference(i));
  74. MemoryOutputStream mo;
  75. writeMakefile (mo, files);
  76. overwriteFileIfDifferentOrThrow (getTargetFolder().getChildFile ("Makefile"), mo);
  77. }
  78. private:
  79. //==============================================================================
  80. void findAllFilesToCompile (const Project::Item& projectItem, Array<RelativePath>& results)
  81. {
  82. if (projectItem.isGroup())
  83. {
  84. for (int i = 0; i < projectItem.getNumChildren(); ++i)
  85. findAllFilesToCompile (projectItem.getChild(i), results);
  86. }
  87. else
  88. {
  89. if (projectItem.shouldBeCompiled())
  90. results.add (RelativePath (projectItem.getFile(), getTargetFolder(), RelativePath::buildTargetFolder));
  91. }
  92. }
  93. void writeDefineFlags (OutputStream& out, const Project::BuildConfiguration& config)
  94. {
  95. StringPairArray defines;
  96. defines.set ("LINUX", "1");
  97. if (config.isDebug().getValue())
  98. {
  99. defines.set ("DEBUG", "1");
  100. defines.set ("_DEBUG", "1");
  101. }
  102. else
  103. {
  104. defines.set ("NDEBUG", "1");
  105. }
  106. out << createGCCPreprocessorFlags (mergePreprocessorDefs (defines, getAllPreprocessorDefs (config)));
  107. }
  108. void writeHeaderPathFlags (OutputStream& out, const Project::BuildConfiguration& config)
  109. {
  110. StringArray headerPaths (config.getHeaderSearchPaths());
  111. headerPaths.insert (0, "/usr/include/freetype2");
  112. headerPaths.insert (0, "/usr/include");
  113. if (project.shouldAddVSTFolderToPath() && getVSTFolder().toString().isNotEmpty())
  114. headerPaths.insert (0, rebaseFromProjectFolderToBuildTarget (RelativePath (getVSTFolder().toString(), RelativePath::projectFolder)).toUnixStyle());
  115. if (isVST())
  116. headerPaths.insert (0, juceWrapperFolder.toUnixStyle());
  117. for (int i = 0; i < headerPaths.size(); ++i)
  118. out << " -I " << FileHelpers::unixStylePath (replacePreprocessorTokens (config, headerPaths[i])).quoted();
  119. }
  120. void writeCppFlags (OutputStream& out, const Project::BuildConfiguration& config)
  121. {
  122. out << " CPPFLAGS := $(DEPFLAGS)";
  123. writeDefineFlags (out, config);
  124. writeHeaderPathFlags (out, config);
  125. out << newLine;
  126. }
  127. void writeLinkerFlags (OutputStream& out, const Project::BuildConfiguration& config)
  128. {
  129. out << " LDFLAGS += -L$(BINDIR) -L$(LIBDIR)";
  130. if (project.isAudioPlugin())
  131. out << " -shared";
  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 << " " << replacePreprocessorTokens (config, 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. String outputDir (buildDirName);
  153. if (config.getTargetBinaryRelativePath().toString().isNotEmpty())
  154. {
  155. RelativePath binaryPath (config.getTargetBinaryRelativePath().toString(), RelativePath::projectFolder);
  156. outputDir = binaryPath.rebased (project.getFile().getParentDirectory(), getTargetFolder(), RelativePath::buildTargetFolder).toUnixStyle();
  157. }
  158. out << "ifeq ($(CONFIG)," << escapeSpaces (config.getName().toString()) << ")" << newLine;
  159. out << " BINDIR := " << escapeSpaces (buildDirName) << newLine
  160. << " LIBDIR := " << escapeSpaces (buildDirName) << newLine
  161. << " OBJDIR := " << escapeSpaces (intermediatesDirName) << newLine
  162. << " OUTDIR := " << escapeSpaces (outputDir) << newLine;
  163. writeCppFlags (out, config);
  164. out << " CFLAGS += $(CPPFLAGS) $(TARGET_ARCH)";
  165. if (config.isDebug().getValue())
  166. out << " -g -ggdb";
  167. if (project.isAudioPlugin())
  168. out << " -fPIC";
  169. out << " -O" << config.getGCCOptimisationFlag() << newLine;
  170. out << " CXXFLAGS += $(CFLAGS) " << replacePreprocessorTokens (config, getExtraCompilerFlags().toString()).trim() << newLine;
  171. writeLinkerFlags (out, config);
  172. out << " LDDEPS :=" << newLine
  173. << " RESFLAGS := ";
  174. writeDefineFlags (out, config);
  175. writeHeaderPathFlags (out, config);
  176. out << newLine;
  177. String targetName (config.getTargetBinaryName().getValue().toString());
  178. if (project.isLibrary())
  179. targetName = getLibbedFilename (targetName);
  180. else if (isVST())
  181. targetName = targetName.upToLastOccurrenceOf (".", false, false) + ".so";
  182. out << " TARGET := " << escapeSpaces (targetName) << newLine;
  183. if (project.isLibrary())
  184. out << " BLDCMD = ar -rcs $(OUTDIR)/$(TARGET) $(OBJECTS) $(TARGET_ARCH)" << newLine;
  185. else
  186. out << " BLDCMD = $(CXX) -o $(OUTDIR)/$(TARGET) $(OBJECTS) $(LDFLAGS) $(RESOURCES) $(TARGET_ARCH)" << newLine;
  187. out << "endif" << newLine << newLine;
  188. }
  189. void writeObjects (OutputStream& out, const Array<RelativePath>& files)
  190. {
  191. out << "OBJECTS := \\" << newLine;
  192. for (int i = 0; i < files.size(); ++i)
  193. if (shouldFileBeCompiledByDefault (files.getReference(i)))
  194. out << " $(OBJDIR)/" << escapeSpaces (getObjectFileFor (files.getReference(i))) << " \\" << newLine;
  195. out << newLine;
  196. }
  197. void writeMakefile (OutputStream& out, const Array<RelativePath>& files)
  198. {
  199. out << "# Automatically generated makefile, created by the Jucer" << newLine
  200. << "# Don't edit this file! Your changes will be overwritten when you re-save the Jucer project!" << newLine
  201. << newLine;
  202. out << "ifndef CONFIG" << newLine
  203. << " CONFIG=" << escapeSpaces (project.getConfiguration(0).getName().toString()) << newLine
  204. << "endif" << newLine
  205. << newLine;
  206. if (! project.isLibrary())
  207. out << "ifeq ($(TARGET_ARCH),)" << newLine
  208. << " TARGET_ARCH := -march=native" << newLine
  209. << "endif" << newLine << newLine;
  210. out << "# (this disables dependency generation if multiple architectures are set)" << newLine
  211. << "DEPFLAGS := $(if $(word 2, $(TARGET_ARCH)), , -MMD)" << newLine
  212. << newLine;
  213. int i;
  214. for (i = 0; i < project.getNumConfigurations(); ++i)
  215. writeConfig (out, project.getConfiguration(i));
  216. writeObjects (out, files);
  217. out << ".PHONY: clean" << newLine
  218. << newLine;
  219. out << "$(OUTDIR)/$(TARGET): $(OBJECTS) $(LDDEPS) $(RESOURCES)" << newLine
  220. << "\t@echo Linking " << project.getProjectName() << newLine
  221. << "\t-@mkdir -p $(BINDIR)" << newLine
  222. << "\t-@mkdir -p $(LIBDIR)" << newLine
  223. << "\t-@mkdir -p $(OUTDIR)" << newLine
  224. << "\t@$(BLDCMD)" << newLine
  225. << newLine;
  226. out << "clean:" << newLine
  227. << "\t@echo Cleaning " << project.getProjectName() << newLine
  228. << "\t-@rm -f $(OUTDIR)/$(TARGET)" << newLine
  229. << "\t-@rm -rf $(OBJDIR)/*" << newLine
  230. << "\t-@rm -rf $(OBJDIR)" << newLine
  231. << newLine;
  232. for (i = 0; i < files.size(); ++i)
  233. {
  234. if (shouldFileBeCompiledByDefault (files.getReference(i)))
  235. {
  236. jassert (files.getReference(i).getRoot() == RelativePath::buildTargetFolder);
  237. out << "$(OBJDIR)/" << escapeSpaces (getObjectFileFor (files.getReference(i)))
  238. << ": " << escapeSpaces (files.getReference(i).toUnixStyle()) << newLine
  239. << "\t-@mkdir -p $(OBJDIR)" << newLine
  240. << "\t@echo \"Compiling " << files.getReference(i).getFileName() << "\"" << newLine
  241. << (files.getReference(i).hasFileExtension (".c") ? "\t@$(CC) $(CFLAGS) -o \"$@\" -c \"$<\""
  242. : "\t@$(CXX) $(CXXFLAGS) -o \"$@\" -c \"$<\"")
  243. << newLine << newLine;
  244. }
  245. }
  246. out << "-include $(OBJECTS:%.o=%.d)" << newLine;
  247. }
  248. const String getObjectFileFor (const RelativePath& file) const
  249. {
  250. return file.getFileNameWithoutExtension()
  251. + "_" + String::toHexString (file.toUnixStyle().hashCode()) + ".o";
  252. }
  253. JUCE_DECLARE_NON_COPYABLE (MakefileProjectExporter);
  254. };
  255. #endif // __JUCER_PROJECTEXPORT_MAKE_JUCEHEADER__