pkg-rebar.mk 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. ################################################################################
  2. # rebar package infrastructure for Erlang packages
  3. #
  4. # This file implements an infrastructure that eases development of
  5. # package .mk files for rebar packages. It should be used for all
  6. # packages that use rebar as their build system.
  7. #
  8. # In terms of implementation, this rebar infrastructure requires the
  9. # .mk file to only specify metadata information about the package:
  10. # name, version, download URL, etc.
  11. #
  12. # We still allow the package .mk file to override what the different
  13. # steps are doing, if needed. For example, if <PKG>_BUILD_CMDS is
  14. # already defined, it is used as the list of commands to perform to
  15. # build the package, instead of the default rebar behaviour. The
  16. # package can also define some post operation hooks.
  17. #
  18. ################################################################################
  19. # Directories to store rebar dependencies in.
  20. #
  21. # These directories actually only contain symbolic links to Erlang
  22. # applications in either $(HOST_DIR) or $(STAGING_DIR). One needs
  23. # them to avoid rebar complaining about missing dependencies, as this
  24. # infrastructure tells rebar to NOT download dependencies during
  25. # the build stage.
  26. #
  27. REBAR_HOST_DEPS_DIR = $(HOST_DIR)/share/rebar/deps
  28. REBAR_TARGET_DEPS_DIR = $(STAGING_DIR)/usr/share/rebar/deps
  29. # Tell rebar where to find the dependencies
  30. #
  31. REBAR_HOST_DEPS_ENV = \
  32. ERL_COMPILER_OPTIONS='{i, "$(REBAR_HOST_DEPS_DIR)"}' \
  33. ERL_EI_LIBDIR=$(HOST_DIR)/lib/erlang/lib/erl_interface-$(ERLANG_EI_VSN)/lib
  34. REBAR_TARGET_DEPS_ENV = \
  35. ERL_COMPILER_OPTIONS='{i, "$(REBAR_TARGET_DEPS_DIR)"}' \
  36. ERL_EI_LIBDIR=$(STAGING_DIR)/usr/lib/erlang/lib/erl_interface-$(ERLANG_EI_VSN)/lib
  37. ################################################################################
  38. # Helper functions
  39. ################################################################################
  40. # Install an Erlang application from $(@D).
  41. #
  42. # i.e., define a recipe that installs the "bin ebin priv $(2)" directories
  43. # from $(@D) to $(1)/$($(PKG)_ERLANG_LIBDIR).
  44. #
  45. # argument 1 should typically be $(HOST_DIR), $(TARGET_DIR),
  46. # or $(STAGING_DIR).
  47. # argument 2 is typically empty when installing in $(TARGET_DIR) and
  48. # "include" when installing in $(HOST_DIR) or
  49. # $(STAGING_DIR).
  50. #
  51. # Note: calling this function must be done with $$(call ...) because it
  52. # expands package-related variables.
  53. #
  54. define install-erlang-directories
  55. $(INSTALL) -d $(1)/$($(PKG)_ERLANG_LIBDIR)
  56. for dir in bin ebin priv $(2); do \
  57. if test -d $(@D)/$$dir; then \
  58. cp -r $(@D)/$$dir $(1)/$($(PKG)_ERLANG_LIBDIR); \
  59. fi; \
  60. done
  61. endef
  62. # Setup a symbolic link in rebar's deps_dir to the actual location
  63. # where an Erlang application is installed.
  64. #
  65. # i.e., define a recipe that creates a symbolic link
  66. # from $($(PKG)_REBAR_DEPS_DIR)/$($(PKG)_ERLANG_APP)
  67. # to $(1)$($(PKG)_ERLANG_LIBDIR).
  68. #
  69. # For target packages for example, one uses this to setup symbolic
  70. # links from $(STAGING_DIR)/usr/share/rebar/deps/<erlang-app> to
  71. # $(STAGING_DIR)/usr/lib/erlang/lib/<erlang-app>-<version>. This
  72. # infrastructure points rebar at the former in order to tell rebar to
  73. # NOT download dependencies during the build stage, and instead use
  74. # the already available dependencies.
  75. #
  76. # Therefore,
  77. # argument 1 is $(HOST_DIR) (for host packages) or
  78. # $(STAGING_DIR) (for target packages).
  79. #
  80. # argument 2 is HOST (for host packages) or
  81. # TARGET (for target packages).
  82. #
  83. # Note: calling this function must be done with $$(call ...) because it
  84. # expands package-related variables.
  85. #
  86. define install-rebar-deps
  87. $(INSTALL) -d $(REBAR_$(2)_DEPS_DIR)
  88. ln -f -s $(1)/$($(PKG)_ERLANG_LIBDIR) \
  89. $(REBAR_$(2)_DEPS_DIR)/$($(PKG)_ERLANG_APP)
  90. endef
  91. # Remove the "deps" statement from a rebar.config file
  92. define remove-rebar-config-dependencies
  93. $(SED) '/^{deps.*}\.$$/d' -e '/^{deps/,/}\.$$/d' \
  94. $($(PKG)_DIR)/rebar.config
  95. endef
  96. ################################################################################
  97. # inner-rebar-package -- defines how the configuration, compilation
  98. # and installation of a rebar package should be done, implements a few
  99. # hooks to tune the build process according to rebar specifities, and
  100. # calls the generic package infrastructure to generate the necessary
  101. # make targets.
  102. #
  103. # argument 1 is the lowercase package name
  104. # argument 2 is the uppercase package name, including a HOST_ prefix
  105. # for host packages
  106. # argument 3 is the uppercase package name, without the HOST_ prefix
  107. # for host packages
  108. # argument 4 is the type (target or host)
  109. #
  110. ################################################################################
  111. define inner-rebar-package
  112. # Extract just the raw package name, lowercase without the leading
  113. # erlang- or host- prefix, as this is used by rebar to find the
  114. # dependencies a package specifies.
  115. #
  116. $(2)_ERLANG_APP = $(subst -,_,$(patsubst erlang-%,%,$(patsubst host-%,%,$(1))))
  117. # Path where to store the package's libs, relative to either $(HOST_DIR)
  118. # for host packages, or $(STAGING_DIR)/usr for target packages.
  119. #
  120. $(2)_ERLANG_LIBDIR = \
  121. lib/erlang/lib/$$($$(PKG)_ERLANG_APP)-$$($$(PKG)_VERSION)
  122. # If a host package, inherit <pkg>_USE_BUNDLED_REBAR from the target
  123. # package, if not explicitly defined. Otherwise, default to NO.
  124. ifndef $(2)_USE_BUNDLED_REBAR
  125. ifdef $(3)_USE_BUNDLED_REBAR
  126. $(2)_USE_BUNDLED_REBAR = $$($(3)_USE_BUNDLED_REBAR)
  127. else
  128. $(2)_USE_BUNDLED_REBAR ?= NO
  129. endif
  130. endif
  131. # If a host package, inherit <pkg>_USE_AUTOCONF from the target
  132. # package, if not explicitly defined. Otherwise, default to NO.
  133. ifndef $(2)_USE_AUTOCONF
  134. ifdef $(3)_USE_AUTOCONF
  135. $(2)_USE_AUTOCONF = $$($(3)_USE_AUTOCONF)
  136. else
  137. $(2)_USE_AUTOCONF ?= NO
  138. endif
  139. endif
  140. # Define the build and install commands
  141. #
  142. ifeq ($(4),target)
  143. # Target packages need the erlang interpreter on the target
  144. $(2)_DEPENDENCIES += erlang
  145. # Used only if the package uses autotools underneath; otherwise, ignored
  146. $(2)_CONF_ENV += $$(REBAR_TARGET_DEPS_ENV)
  147. ifndef $(2)_BUILD_CMDS
  148. define $(2)_BUILD_CMDS
  149. (cd $$(@D); \
  150. CC="$$(TARGET_CC)" \
  151. CXX="$$(TARGET_CXX)" \
  152. CFLAGS="$$(TARGET_CFLAGS)" \
  153. CXXFLAGS="$$(TARGET_CXXFLAGS)" \
  154. LDFLAGS="$$(TARGET_LDFLAGS)" \
  155. $$(REBAR_TARGET_DEPS_ENV) \
  156. $$(TARGET_MAKE_ENV) \
  157. $$($$(PKG)_REBAR_ENV) $$($$(PKG)_REBAR) deps_dir=$$(REBAR_TARGET_DEPS_DIR) compile \
  158. )
  159. endef
  160. endif
  161. # We need to double-$ the 'call' because it wants to expand
  162. # package-related variables
  163. ifndef $(2)_INSTALL_STAGING_CMDS
  164. define $(2)_INSTALL_STAGING_CMDS
  165. $$(call install-erlang-directories,$$(STAGING_DIR)/usr,include)
  166. $$(call install-rebar-deps,$$(STAGING_DIR)/usr,TARGET)
  167. endef
  168. endif
  169. # We need to double-$ the 'call' because it wants to expand
  170. # package-related variables
  171. ifndef $(2)_INSTALL_TARGET_CMDS
  172. define $(2)_INSTALL_TARGET_CMDS
  173. $$(call install-erlang-directories,$$(TARGET_DIR)/usr)
  174. endef
  175. endif
  176. else # !target
  177. # Host packages need the erlang interpreter on the host
  178. $(2)_DEPENDENCIES += host-erlang
  179. # Used only if the package uses autotools underneath; otherwise, ignored
  180. $(2)_CONF_ENV += $$(REBAR_HOST_DEPS_ENV)
  181. ifndef $(2)_BUILD_CMDS
  182. define $(2)_BUILD_CMDS
  183. (cd $$(@D); \
  184. CC="$$(HOSTCC)" \
  185. CFLAGS="$$(HOST_CFLAGS)" \
  186. LDFLAGS="$$(HOST_LDFLAGS)" \
  187. $$(REBAR_HOST_DEPS_ENV) \
  188. $$(HOST_MAKE_ENV) \
  189. $$($$(PKG)_REBAR_ENV) $$($$(PKG)_REBAR) deps_dir=$$(REBAR_HOST_DEPS_DIR) compile \
  190. )
  191. endef
  192. endif
  193. # We need to double-$ the 'call' because it wants to expand
  194. # package-related variables
  195. ifndef $(2)_INSTALL_CMDS
  196. define $(2)_INSTALL_CMDS
  197. $$(call install-erlang-directories,$$(HOST_DIR),include)
  198. $$(call install-rebar-deps,$$(HOST_DIR),HOST)
  199. endef
  200. endif
  201. endif # !target
  202. # Whether to use the generic rebar or the package's bundled rebar
  203. #
  204. ifeq ($$($(2)_USE_BUNDLED_REBAR),YES)
  205. $(2)_REBAR = ./rebar
  206. else
  207. $(2)_REBAR = rebar
  208. $(2)_DEPENDENCIES += host-erlang-rebar
  209. endif
  210. $(2)_KEEP_DEPENDENCIES ?= NO
  211. # Remove dependencies listed in rebar.config unless the package says
  212. # otherwise
  213. ifeq ($$($(2)_KEEP_DEPENDENCIES),NO)
  214. $(2)_POST_PATCH_HOOKS += remove-rebar-config-dependencies
  215. endif
  216. # The package sub-infra to use
  217. #
  218. ifeq ($$($(2)_USE_AUTOCONF),YES)
  219. $(call inner-autotools-package,$(1),$(2),$(3),$(4))
  220. else
  221. $(call inner-generic-package,$(1),$(2),$(3),$(4))
  222. endif
  223. endef # inner-rebar-package
  224. rebar-package = $(call inner-rebar-package,$(pkgname),$(call UPPERCASE,$(pkgname)),$(call UPPERCASE,$(pkgname)),target)
  225. host-rebar-package = $(call inner-rebar-package,host-$(pkgname),$(call UPPERCASE,host-$(pkgname)),$(call UPPERCASE,$(pkgname)),host)