75facd55a3a37ceb48945fddf48c96667db1889f
[packages/trusty/cirros-testvm.git] / cirros-testvm / src-cirros / buildroot-2015.05 / package / pkg-rebar.mk
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
20 # Directories to store rebar dependencies in.
21 #
22 # These directories actually only contain symbolic links to Erlang
23 # applications in either $(HOST_DIR) or $(STAGING_DIR).  One needs
24 # them to avoid rebar complaining about missing dependencies, as this
25 # infrastructure tells rebar to NOT download dependencies during
26 # the build stage.
27 #
28 REBAR_HOST_DEPS_DIR = $(HOST_DIR)/usr/share/rebar/deps
29 REBAR_TARGET_DEPS_DIR = $(STAGING_DIR)/usr/share/rebar/deps
30
31 # Tell rebar where to find the dependencies
32 #
33 REBAR_HOST_DEPS_ENV = \
34         ERL_COMPILER_OPTIONS='{i, "$(REBAR_HOST_DEPS_DIR)"}' \
35         ERL_EI_LIBDIR=$(HOST_DIR)/usr/lib/erlang/lib/erl_interface-$(ERLANG_EI_VSN)/lib
36 REBAR_TARGET_DEPS_ENV = \
37         ERL_COMPILER_OPTIONS='{i, "$(REBAR_TARGET_DEPS_DIR)"}' \
38         ERL_EI_LIBDIR=$(STAGING_DIR)/usr/lib/erlang/lib/erl_interface-$(ERLANG_EI_VSN)/lib
39
40 ################################################################################
41 # Helper functions
42 ################################################################################
43
44 # Install an Erlang application from $(@D).
45 #
46 # i.e., define a recipe that installs the "bin ebin priv $(2)" directories
47 # from $(@D) to $(1)$($(PKG)_ERLANG_LIBDIR).
48 #
49 #  argument 1 should typically be $(HOST_DIR), $(TARGET_DIR),
50 #             or $(STAGING_DIR).
51 #  argument 2 is typically empty when installing in $(TARGET_DIR) and
52 #             "include" when installing in $(HOST_DIR) or
53 #             $(STAGING_DIR).
54 #
55 # Note: calling this function must be done with $$(call ...) because it
56 # expands package-related variables.
57 #
58 define install-erlang-directories
59         $(INSTALL) -d $(1)/$($(PKG)_ERLANG_LIBDIR)
60         for dir in bin ebin priv $(2); do                               \
61                 if test -d $(@D)/$$dir; then                            \
62                         cp -r $(@D)/$$dir $(1)$($(PKG)_ERLANG_LIBDIR);  \
63                 fi;                                                     \
64         done
65 endef
66
67 # Setup a symbolic link in rebar's deps_dir to the actual location
68 # where an Erlang application is installed.
69 #
70 # i.e., define a recipe that creates a symbolic link
71 # from $($(PKG)_REBAR_DEPS_DIR)/$($(PKG)_ERLANG_APP)
72 # to $(1)$($(PKG)_ERLANG_LIBDIR).
73 #
74 # For target packages for example, one uses this to setup symbolic
75 # links from $(STAGING_DIR)/usr/share/rebar/deps/<erlang-app> to
76 # $(STAGING_DIR)/usr/lib/erlang/lib/<erlang-app>-<version>. This
77 # infrastructure points rebar at the former in order to tell rebar to
78 # NOT download dependencies during the build stage, and instead use
79 # the already available dependencies.
80 #
81 # Therefore,
82 #  argument 1 is $(HOST_DIR) (for host packages) or
83 #             $(STAGING_DIR) (for target packages).
84 #
85 #  argument 2 is HOST (for host packages) or
86 #             TARGET (for target packages).
87 #
88 # Note: calling this function must be done with $$(call ...) because it
89 # expands package-related variables.
90 #
91 define install-rebar-deps
92         $(INSTALL) -d $(REBAR_$(2)_DEPS_DIR)
93         ln -f -s $(1)/$($(PKG)_ERLANG_LIBDIR) \
94                 $(REBAR_$(2)_DEPS_DIR)/$($(PKG)_ERLANG_APP)
95 endef
96
97 ################################################################################
98 # inner-rebar-package -- defines how the configuration, compilation
99 # and installation of a rebar package should be done, implements a few
100 # hooks to tune the build process according to rebar specifities, and
101 # calls the generic package infrastructure to generate the necessary
102 # make targets.
103 #
104 #  argument 1 is the lowercase package name
105 #  argument 2 is the uppercase package name, including a HOST_ prefix
106 #             for host packages
107 #  argument 3 is the uppercase package name, without the HOST_ prefix
108 #             for host packages
109 #  argument 4 is the type (target or host)
110 #
111 ################################################################################
112
113 define inner-rebar-package
114
115 # Extract just the raw package name, lowercase without the leading
116 # erlang- or host- prefix, as this is used by rebar to find the
117 # dependencies a package specifies.
118 #
119 $(2)_ERLANG_APP = $(subst -,_,$(patsubst erlang-%,%,$(patsubst host-%,%,$(1))))
120
121 # Path where to store the package's libs, relative to either $(HOST_DIR)
122 # for host packages, or $(STAGING_DIR) for target packages.
123 #
124 $(2)_ERLANG_LIBDIR = \
125         /usr/lib/erlang/lib/$$($$(PKG)_ERLANG_APP)-$$($$(PKG)_VERSION)
126
127 # If a host package, inherit <pkg>_USE_BUNDLED_REBAR from the target
128 # package, if not explicitly defined. Otherwise, default to NO.
129 ifndef $(2)_USE_BUNDLED_REBAR
130  ifdef $(3)_USE_BUNDLED_REBAR
131   $(2)_USE_BUNDLED_REBAR = $$($(3)_USE_BUNDLED_REBAR)
132  else
133   $(2)_USE_BUNDLED_REBAR ?= NO
134  endif
135 endif
136
137 # If a host package, inherit <pkg>_USE_AUTOCONF from the target
138 # package, if not explicitly defined. Otherwise, default to NO.
139 ifndef $(2)_USE_AUTOCONF
140  ifdef $(3)_USE_AUTOCONF
141   $(2)_USE_AUTOCONF = $$($(3)_USE_AUTOCONF)
142  else
143   $(2)_USE_AUTOCONF ?= NO
144  endif
145 endif
146
147 # Define the build and install commands
148 #
149 ifeq ($(4),target)
150
151 # Target packages need the erlang interpreter on the target
152 $(2)_DEPENDENCIES += erlang
153
154 # Used only if the package uses autotools underneath; otherwise, ignored
155 $(2)_CONF_ENV += $$(REBAR_TARGET_DEPS_ENV)
156
157 ifndef $(2)_BUILD_CMDS
158 define $(2)_BUILD_CMDS
159         (cd $$(@D); \
160                 CC="$$(TARGET_CC)" \
161                 CFLAGS="$$(TARGET_CFLAGS)" \
162                 LDFLAGS="$$(TARGET_LDFLAGS)" \
163                 $$(REBAR_TARGET_DEPS_ENV) \
164                 $$(TARGET_MAKE_ENV) \
165                 $$($$(PKG)_REBAR_ENV) $$($$(PKG)_REBAR) deps_dir=$$(REBAR_TARGET_DEPS_DIR) compile \
166         )
167 endef
168 endif
169
170 # We need to double-$ the 'call' because it wants to expand
171 # package-related variables
172 ifndef $(2)_INSTALL_STAGING_CMDS
173 define $(2)_INSTALL_STAGING_CMDS
174         $$(call install-erlang-directories,$$(STAGING_DIR),include)
175         $$(call install-rebar-deps,$$(STAGING_DIR),TARGET)
176 endef
177 endif
178
179 # We need to double-$ the 'call' because it wants to expand
180 # package-related variables
181 ifndef $(2)_INSTALL_TARGET_CMDS
182 define $(2)_INSTALL_TARGET_CMDS
183         $$(call install-erlang-directories,$$(TARGET_DIR))
184 endef
185 endif
186
187 else # !target
188
189 ifeq ($$($(2)_USE_AUTOCONF),YES)
190 # This must be repeated from inner-autotools-package, otherwise we get
191 # an empty _DEPENDENCIES if _AUTORECONF is YES or _USE_BUNDLED_REBAR
192 # is NO.  Also filter the result of _AUTORECONF and _GETTEXTIZE away
193 # from the non-host rule
194 $(2)_DEPENDENCIES ?= $$(filter-out host-automake host-autoconf host-libtool \
195                                 host-gettext host-toolchain host-erlang-rebar $(1),\
196     $$(patsubst host-host-%,host-%,$$(addprefix host-,$$($(3)_DEPENDENCIES))))
197 else
198 # Same deal, if _USE_BUNDLED_REBAR is NO.
199 $(2)_DEPENDENCIES ?= $$(filter-out  host-toolchain host-erlang-rebar $(1),\
200         $$(patsubst host-host-%,host-%,$$(addprefix host-,$$($(3)_DEPENDENCIES))))
201 endif
202
203 # Host packages need the erlang interpreter on the host
204 $(2)_DEPENDENCIES += host-erlang
205
206 # Used only if the package uses autotools underneath; otherwise, ignored
207 $(2)_CONF_ENV += $$(REBAR_HOST_DEPS_ENV)
208
209 ifndef $(2)_BUILD_CMDS
210 define $(2)_BUILD_CMDS
211         (cd $$(@D); \
212                 CC="$$(HOSTCC)" \
213                 CFLAGS="$$(HOST_CFLAGS)" \
214                 LDFLAGS="$$(HOST_LDFLAGS)" \
215                 $$(REBAR_HOST_DEPS_ENV) \
216                 $$(HOST_MAKE_ENV) \
217                 $$($$(PKG)_REBAR_ENV) $$($$(PKG)_REBAR) deps_dir=$$(REBAR_HOST_DEPS_DIR) compile \
218         )
219 endef
220 endif
221
222 # We need to double-$ the 'call' because it wants to expand
223 # package-related variables
224 ifndef $(2)_INSTALL_CMDS
225 define $(2)_INSTALL_CMDS
226         $$(call install-erlang-directories,$$(HOST_DIR),include)
227         $$(call install-rebar-deps,$$(HOST_DIR),HOST)
228 endef
229 endif
230
231 endif # !target
232
233 # Whether to use the generic rebar or the package's bundled rebar
234 #
235 ifeq ($$($(2)_USE_BUNDLED_REBAR),YES)
236 $(2)_REBAR = ./rebar
237 else
238 $(2)_REBAR = rebar
239 $(2)_DEPENDENCIES += host-erlang-rebar
240 endif
241
242 # The package sub-infra to use
243 #
244 ifeq ($$($(2)_USE_AUTOCONF),YES)
245 $(call inner-autotools-package,$(1),$(2),$(3),$(4))
246 else
247 $(call inner-generic-package,$(1),$(2),$(3),$(4))
248 endif
249
250 endef # inner-rebar-package
251
252 rebar-package = $(call inner-rebar-package,$(pkgname),$(call UPPERCASE,$(pkgname)),$(call UPPERCASE,$(pkgname)),target)
253 host-rebar-package = $(call inner-rebar-package,host-$(pkgname),$(call UPPERCASE,host-$(pkgname)),$(call UPPERCASE,$(pkgname)),host)