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.

325 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. //==============================================================================
  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. for (int i = 0; i < groups.size(); ++i)
  69. findAllFilesToCompile (groups.getReference(i), files);
  70. MemoryOutputStream mo;
  71. writeMakefile (mo, files);
  72. overwriteFileIfDifferentOrThrow (getTargetFolder().getChildFile ("Makefile"), mo);
  73. }
  74. private:
  75. //==============================================================================
  76. void findAllFilesToCompile (const Project::Item& projectItem, Array<RelativePath>& results)
  77. {
  78. if (projectItem.isGroup())
  79. {
  80. for (int i = 0; i < projectItem.getNumChildren(); ++i)
  81. findAllFilesToCompile (projectItem.getChild(i), results);
  82. }
  83. else
  84. {
  85. if (projectItem.shouldBeCompiled())
  86. results.add (RelativePath (projectItem.getFile(), getTargetFolder(), RelativePath::buildTargetFolder));
  87. }
  88. }
  89. void writeDefineFlags (OutputStream& out, const Project::BuildConfiguration& config)
  90. {
  91. StringPairArray defines;
  92. defines.set ("LINUX", "1");
  93. if (config.isDebug().getValue())
  94. {
  95. defines.set ("DEBUG", "1");
  96. defines.set ("_DEBUG", "1");
  97. }
  98. else
  99. {
  100. defines.set ("NDEBUG", "1");
  101. }
  102. out << createGCCPreprocessorFlags (mergePreprocessorDefs (defines, getAllPreprocessorDefs (config)));
  103. }
  104. void writeHeaderPathFlags (OutputStream& out, const Project::BuildConfiguration& config)
  105. {
  106. StringArray headerPaths (config.getHeaderSearchPaths());
  107. headerPaths.insert (0, "/usr/include/freetype2");
  108. headerPaths.insert (0, "/usr/include");
  109. for (int i = 0; i < libraryModules.size(); ++i)
  110. libraryModules.getUnchecked(i)->addExtraSearchPaths (*this, headerPaths);
  111. for (int i = 0; i < headerPaths.size(); ++i)
  112. out << " -I " << FileHelpers::unixStylePath (replacePreprocessorTokens (config, headerPaths[i])).quoted();
  113. }
  114. void writeCppFlags (OutputStream& out, const Project::BuildConfiguration& config)
  115. {
  116. out << " CPPFLAGS := $(DEPFLAGS)";
  117. writeDefineFlags (out, config);
  118. writeHeaderPathFlags (out, config);
  119. out << newLine;
  120. }
  121. void writeLinkerFlags (OutputStream& out, const Project::BuildConfiguration& config)
  122. {
  123. out << " LDFLAGS += -L$(BINDIR) -L$(LIBDIR)";
  124. if (makefileIsDLL)
  125. out << " -shared";
  126. {
  127. Array<RelativePath> libraryPaths;
  128. libraryPaths.add (RelativePath ("/usr/X11R6/lib/", RelativePath::unknown));
  129. libraryPaths.add (getJucePathFromTargetFolder().getChildFile ("bin"));
  130. for (int i = 0; i < libraryPaths.size(); ++i)
  131. out << " -L" << libraryPaths.getReference(i).toUnixStyle().quoted();
  132. }
  133. const char* defaultLibs[] = { "freetype", "pthread", "rt", "X11", "GL", "GLU", "Xinerama", "asound", 0 };
  134. StringArray libs (defaultLibs);
  135. if (project.getJuceLinkageMode() == Project::useLinkedJuce)
  136. libs.add ("juce");
  137. for (int i = 0; i < libs.size(); ++i)
  138. out << " -l" << libs[i];
  139. out << " " << replacePreprocessorTokens (config, getExtraLinkerFlags().toString()).trim()
  140. << newLine;
  141. }
  142. void writeConfig (OutputStream& out, const Project::BuildConfiguration& config)
  143. {
  144. const String buildDirName ("build");
  145. const String intermediatesDirName (buildDirName + "/intermediate/" + config.getName().toString());
  146. String outputDir (buildDirName);
  147. if (config.getTargetBinaryRelativePath().toString().isNotEmpty())
  148. {
  149. RelativePath binaryPath (config.getTargetBinaryRelativePath().toString(), RelativePath::projectFolder);
  150. outputDir = binaryPath.rebased (projectFolder, getTargetFolder(), RelativePath::buildTargetFolder).toUnixStyle();
  151. }
  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 (outputDir) << newLine;
  157. writeCppFlags (out, config);
  158. out << " CFLAGS += $(CPPFLAGS) $(TARGET_ARCH)";
  159. if (config.isDebug().getValue())
  160. out << " -g -ggdb";
  161. if (makefileIsDLL)
  162. out << " -fPIC";
  163. out << " -O" << config.getGCCOptimisationFlag() << newLine;
  164. out << " CXXFLAGS += $(CFLAGS) " << replacePreprocessorTokens (config, getExtraCompilerFlags().toString()).trim() << newLine;
  165. writeLinkerFlags (out, config);
  166. out << " LDDEPS :=" << newLine
  167. << " RESFLAGS := ";
  168. writeDefineFlags (out, config);
  169. writeHeaderPathFlags (out, config);
  170. out << newLine;
  171. String targetName (config.getTargetBinaryName().getValue().toString());
  172. if (projectType.isLibrary())
  173. targetName = getLibbedFilename (targetName);
  174. else
  175. targetName = targetName.upToLastOccurrenceOf (".", false, false) + makefileTargetSuffix;
  176. out << " TARGET := " << escapeSpaces (targetName) << newLine;
  177. if (projectType.isLibrary())
  178. out << " BLDCMD = ar -rcs $(OUTDIR)/$(TARGET) $(OBJECTS) $(TARGET_ARCH)" << newLine;
  179. else
  180. out << " BLDCMD = $(CXX) -o $(OUTDIR)/$(TARGET) $(OBJECTS) $(LDFLAGS) $(RESOURCES) $(TARGET_ARCH)" << newLine;
  181. out << "endif" << newLine << newLine;
  182. }
  183. void writeObjects (OutputStream& out, const Array<RelativePath>& files)
  184. {
  185. out << "OBJECTS := \\" << newLine;
  186. for (int i = 0; i < files.size(); ++i)
  187. if (shouldFileBeCompiledByDefault (files.getReference(i)))
  188. out << " $(OBJDIR)/" << escapeSpaces (getObjectFileFor (files.getReference(i))) << " \\" << newLine;
  189. out << newLine;
  190. }
  191. void writeMakefile (OutputStream& out, const Array<RelativePath>& files)
  192. {
  193. out << "# Automatically generated makefile, created by the Jucer" << newLine
  194. << "# Don't edit this file! Your changes will be overwritten when you re-save the Jucer project!" << newLine
  195. << newLine;
  196. out << "ifndef CONFIG" << newLine
  197. << " CONFIG=" << escapeSpaces (configs.getReference(0).getName().toString()) << newLine
  198. << "endif" << newLine
  199. << newLine;
  200. if (! projectType.isLibrary())
  201. out << "ifeq ($(TARGET_ARCH),)" << newLine
  202. << " TARGET_ARCH := -march=native" << newLine
  203. << "endif" << newLine << newLine;
  204. out << "# (this disables dependency generation if multiple architectures are set)" << newLine
  205. << "DEPFLAGS := $(if $(word 2, $(TARGET_ARCH)), , -MMD)" << newLine
  206. << newLine;
  207. int i;
  208. for (i = 0; i < configs.size(); ++i)
  209. writeConfig (out, configs.getReference(i));
  210. writeObjects (out, files);
  211. out << ".PHONY: clean" << newLine
  212. << newLine;
  213. out << "$(OUTDIR)/$(TARGET): $(OBJECTS) $(LDDEPS) $(RESOURCES)" << newLine
  214. << "\t@echo Linking " << projectName << newLine
  215. << "\t-@mkdir -p $(BINDIR)" << newLine
  216. << "\t-@mkdir -p $(LIBDIR)" << newLine
  217. << "\t-@mkdir -p $(OUTDIR)" << newLine
  218. << "\t@$(BLDCMD)" << newLine
  219. << newLine;
  220. out << "clean:" << newLine
  221. << "\t@echo Cleaning " << projectName << newLine
  222. << "\t-@rm -f $(OUTDIR)/$(TARGET)" << newLine
  223. << "\t-@rm -rf $(OBJDIR)/*" << newLine
  224. << "\t-@rm -rf $(OBJDIR)" << newLine
  225. << newLine;
  226. for (i = 0; i < files.size(); ++i)
  227. {
  228. if (shouldFileBeCompiledByDefault (files.getReference(i)))
  229. {
  230. jassert (files.getReference(i).getRoot() == RelativePath::buildTargetFolder);
  231. out << "$(OBJDIR)/" << escapeSpaces (getObjectFileFor (files.getReference(i)))
  232. << ": " << escapeSpaces (files.getReference(i).toUnixStyle()) << newLine
  233. << "\t-@mkdir -p $(OBJDIR)" << newLine
  234. << "\t@echo \"Compiling " << files.getReference(i).getFileName() << "\"" << newLine
  235. << (files.getReference(i).hasFileExtension (".c") ? "\t@$(CC) $(CFLAGS) -o \"$@\" -c \"$<\""
  236. : "\t@$(CXX) $(CXXFLAGS) -o \"$@\" -c \"$<\"")
  237. << newLine << newLine;
  238. }
  239. }
  240. out << "-include $(OBJECTS:%.o=%.d)" << newLine;
  241. }
  242. String getObjectFileFor (const RelativePath& file) const
  243. {
  244. return file.getFileNameWithoutExtension()
  245. + "_" + String::toHexString (file.toUnixStyle().hashCode()) + ".o";
  246. }
  247. JUCE_DECLARE_NON_COPYABLE (MakefileProjectExporter);
  248. };
  249. #endif // __JUCER_PROJECTEXPORT_MAKE_JUCEHEADER__