1 # $NetBSD: gem.mk,v 1.14 2012/03/20 06:14:30 taca Exp $
3 # This Makefile fragment is intended to be included by packages that build
4 # and install Ruby gems.
6 # Package-settable variables:
9 # Minimum version of required rubygems. Ruby base packages contain:
12 # ruby193-base: 1.8.11
14 # If newer version of rubygems is resuiqred, set RUBYGEMS_REQD to
17 # Default: not defined
20 # Fix version of depending gem or modify files in gemspec.
22 # (1) Specify as gem and dependency pattern as usual pkgsrc's one.
25 # When gemspec contains "json~>1.4.7" as runtime dependency
26 # (i.e. json>=1.4.7<1.5) and if you want to relax it to
27 # "json>=1.4.6" then use:
29 # OVERRIDE_GEMSPEC+= json>=1.4.6
31 # If you want to change depending gem to "json_pure>=1.4.6"
34 # OVERRIDE_GEMSPEC+= json:json_pure>=1.4.6
36 # You can also remove dependency:
38 # OVERRIDE_GEMSPEC+= json:
40 # (2) Modify files in gemspec.
43 # Remove files (a.rb and b.rb) from 'files':
45 # OVERRIDE_GEMSPEC+= :files a.rb= b.rb=
48 # Add a file (exec.rb) to 'executables':
50 # OVERRIDE_GEMSPEC+= :executables exec.rb
53 # Rename a file (from 'ruby' to 'ruby193') in 'files':
55 # OVERRIDE_GEMSPEC+= :files ruby=ruby193
57 # Note: Because of limited parser, argumetns for (1) must preceed to (2).
62 # The Rakefile target that creates a local gem if using the
63 # ``rake'' GEM_BUILD method.
68 # The method used to build the local gem.
70 # Possible: gemspec, rake
74 # A list of shell globs representing files to remove from the
75 # gem installed in the installation root. The file is removed
76 # if the path matches the glob and is not in ${WRKSRC}. The
81 # GEM_CLEANBUILD= *.o *.${RUBY_DLEXT} mkmf.log
84 # The name of the gem to install. The default value is ${DISTNAME}.
87 # The path to the gemspec file to use when building a gem using
88 # the ``gemspec'' GEM_BUILD method. It defaults to
89 # ${WRKDIR}/${DISTNAME}.gemspec.
92 # Optional parameter to pass to gem on install stage.
95 # Variables defined in this file:
98 # The relative path from PREFIX to the directory in the local gem
99 # repository that holds the documentation for the installed gem.
102 # The relative path from PREFIX to the directory in the local gem
103 # repository that holds the contents of the installed gem.
106 # The path to the rubygems ``gem'' script.
108 .if !defined(_RUBYGEM_MK)
109 _RUBYGEM_MK= # defined
111 # By default, assume that gems are capable of user-destdir installation.
112 PKG_DESTDIR_SUPPORT?= user-destdir
114 # replace interpeter bin default
115 REPLACE_RUBY_DIRS?= bin
118 # Default to using rake to build the local gem from the unpacked files.
121 OVERRIDE_GEMSPEC?= # default is empty
123 RUBYGEM_LANG?= en_US.UTF-8
124 RUBYGEM_ENV?= LANG=${RUBYGEM_LANG} LC_CTYPE=${RUBYGEM_LANG}
126 .if !empty(OVERRIDE_GEMSPEC)
127 UPDATE_GEMSPEC= ../../lang/ruby/files/update-gemspec.rb
130 .if ${GEM_BUILD} == "rake"
134 # print-PLIST support
135 PRINT_PLIST_AWK+= /${GEM_NAME}\.(gem|gemspec)$$/ \
136 { gsub(/${GEM_NAME}\.gem/, "$${GEM_NAME}.gem"); }
137 PRINT_PLIST_AWK+= /${GEM_NAME:S/./[.]/g}[.](gem|gemspec)$$/ \
138 { gsub(/${PKGVERSION_NOREV:S|/|\\/|g}[.]gem/, "$${PKGVERSION}.gem"); }
139 PRINT_PLIST_AWK+= /^${GEM_LIBDIR:S|/|\\/|g}/ \
140 { gsub(/${GEM_LIBDIR:S|/|\\/|g}/, "$${GEM_LIBDIR}"); print; next; }
141 PRINT_PLIST_AWK+= /^${GEM_DOCDIR:S|/|\\/|g}/ \
143 PRINT_PLIST_AWK+= /^${GEM_HOME:S|/|\\/|g}/ \
144 { gsub(/${GEM_HOME:S|/|\\/|g}/, "$${GEM_HOME}"); \
146 PRINT_PLIST_AWK+= /^${RUBY_GEM_BASE:S|/|\\/|g}/ \
147 { gsub(/${RUBY_GEM_BASE:S|/|\\/|g}/, "$${RUBY_GEM_BASE}"); \
150 # Include this early in case some of its target are needed
151 .include "../../lang/ruby/modules.mk"
153 # Build and run-time dependencies for Ruby prior to 1.9.
155 # We need rubygems>=1.1.0 to actually build the package, but the
156 # resulting installed gem can run with older versions of rubygems.
158 # If we're using rake to build the local gem, then include it as a
162 .if ${RUBY_VER} == "18"
163 BUILD_DEPENDS+= ${RUBY_PKGPREFIX}-rubygems>=1.1.0:../../misc/rubygems
164 DEPENDS+= ${RUBY_PKGPREFIX}-rubygems>=1.0.1:../../misc/rubygems
166 . if defined(RUBYGEMS_REQD)
168 RUBY193_RUBYGEMS_VERS= 1.8.11
170 _RUBYGEMS_REQD_MAJOR= ${RUBYGEMS_REQD:C/\.[0-9\.]+$//}
171 _RUBYGEMS_REQD_MINORS= ${RUBYGEMS_REQD:C/^([0-9]+)\.*//}
173 . if ${RUBY_VER} == "193"
174 _RUBYGEMS_MAJOR= ${RUBY193_RUBYGEMS_VERS:C/\.[0-9\.]+$//}
175 _RUBYGEMS_MINORS= ${RUBY193_RUBYGEMS_VERS:C/^([0-9]+)\.*//}
177 PKG_FAIL_REASON+= "Unknown Ruby version specified: ${RUBY_VER}."
182 . if ${_RUBYGEMS_REQD_MAJOR} > ${_RUBYGEMS_MAJOR}
184 . elif ${_RUBYGEMS_REQD_MAJOR} == ${_RUBYGEMS_MAJOR}
185 . if !empty(_RUBYGEMS_MINORS) && ${_RUBYGEMS_REQD_MINORS} > ${_RUBYGEMS_MINORS}
190 . if empty(_RUBYGEMS_REQD:M[nN][oO])
191 DEPENDS+= ${RUBY_PKGPREFIX}-rubygems>=${RUBYGEMS_REQD}:../../misc/rubygems
197 MASTER_SITES?= http://rubygems.org/gems/ http://gems.rubyforge.org/gems/
200 DISTFILES?= ${DISTNAME}${EXTRACT_SUFX}
202 # If any of the DISTFILES are gems, then skip the normal do-extract actions
203 # and extract them ourselves in gem-extract.
205 .if !empty(DISTFILES:M*.gem)
206 EXTRACT_ONLY?= # empty
209 # Base directory for Gems
210 MAKE_ENV+= GEM_PATH=${PREFIX}/${GEM_HOME}
212 # Directory for the Gem to install
213 GEM_NAME?= ${DISTNAME}
214 GEM_LIBDIR= ${GEM_HOME}/gems/${GEM_NAME}
215 GEM_DOCDIR= ${GEM_HOME}/doc/${GEM_NAME}
216 GEM_CACHEDIR= ${GEM_HOME}/cache
218 # Installed gems have wrapper scripts that call the right interpreter,
219 # regardless of the #! line at the head of a script, so we can skip
220 # the interpreter path check for gems. ANd it is also true for files'
223 CHECK_INTERPRETER_SKIP+= ${GEM_LIBDIR}/*
224 CHECK_PERMS_SKIP+= ${GEM_LIBDIR}/*
226 # RUBYGEM holds the path to RubyGems' gem command
227 EVAL_PREFIX+= RUBYGEM_PREFIX=${RUBYGEM_NAME}
228 RUBYGEM= ${RUBYGEM_PREFIX}/bin/${RUBYGEM_NAME}
231 PLIST_SUBST+= GEM_NAME=${GEM_NAME}
232 PLIST_SUBST+= GEM_LIBDIR=${GEM_LIBDIR}
233 PLIST_SUBST+= GEM_DOCDIR=${GEM_DOCDIR}
238 ### The gem-extract target extracts a standard gem file. It is an
239 ### automatic dependency for the post-extract target so it doesn't
240 ### disturb the usual do-extract actions.
242 GEM_SPECFILE?= ${WRKDIR}/${DISTNAME}.gemspec
245 post-extract: gem-extract
246 .if !target(gem-extract)
247 gem-extract: fake-home
248 . for _gem_ in ${DISTFILES:M*.gem}
249 ${RUN} cd ${WRKDIR} && ${SETENV} ${MAKE_ENV} ${RUBYGEM_ENV} \
250 ${RUBYGEM} unpack ${RUBYGEM_INSTALL_ROOT_OPTION} \
251 ${_DISTDIR:Q}/${_gem_:Q}
252 ${RUN} cd ${WRKDIR} && \
253 ${SETENV} ${MAKE_ENV} TZ=UTC ${RUBYGEM_ENV} \
254 ${RUBYGEM} spec ${_DISTDIR:Q}/${_gem_:Q} > ${_gem_}spec
261 ### The gem-build target builds a new local gem from the extracted gem's
262 ### contents. The new gem as created as ${WRKSRC}/${GEM_NAME}.gem.
263 ### The local gem is then installed into a special build root under
264 ### ${WRKDIR} (${RUBYGEM_INSTALL_ROOT}), possibly compiling any extensions.
266 GEM_CLEANBUILD?= ext/*
268 .if !empty(GEM_CLEANBUILD:M/*) || !empty(GEM_CLEANBUILD:M*../*)
269 PKG_FAIL_REASON= "GEM_CLEANBUILD must be relative to "${PREFIX}/${GEM_LIBDIR:Q}"."
273 do-build: _gem-pre-build gem-build
276 .if !empty(OVERRIDE_GEMSPEC)
277 @${STEP_MSG} Override gemspec dependency
278 @${RUBY} ${.CURDIR}/${UPDATE_GEMSPEC} ${WRKDIR}/${GEM_NAME}.gemspec \
279 ${OVERRIDE_GEMSPEC:Q}
281 @${STEP_MSG} "Removing backup files of patch before build"
282 @find ${WRKSRC} -name \*.orig -exec rm {} \;
284 gem-build: _gem-${GEM_BUILD}-build
286 .PHONY: _gem-gemspec-build
288 ${RUN} cd ${WRKSRC} && ${SETENV} ${MAKE_ENV} ${RUBYGEM_ENV} \
289 ${RUBYGEM} build ${GEM_SPECFILE}
290 ${RUN} ${TEST} -f ${WRKSRC}/${GEM_NAME}.gem || \
291 ${FAIL_MSG} "Build of ${GEM_NAME}.gem failed."
295 .PHONY: _gem-rake-build
297 ${RUN} cd ${WRKSRC} && ${SETENV} ${MAKE_ENV} ${RAKE} ${BUILD_TARGET}
298 ${RUN} cd ${WRKSRC} && rm -f ${GEM_NAME}.gem
299 ${RUN} cd ${WRKSRC} && find . -name ${GEM_NAME}.gem -print | \
300 while read file; do \
301 ln -fs "$$file" ${GEM_NAME}.gem; \
305 RUBYGEM_INSTALL_ROOT= ${WRKDIR}/.inst
306 _RUBYGEM_OPTIONS= --no-update-sources # don't cache the gem index
307 _RUBYGEM_OPTIONS+= --install-dir ${PREFIX}/${GEM_HOME}
308 _RUBYGEM_OPTIONS+= ${RUBYGEM_INSTALL_ROOT_OPTION}
309 _RUBYGEM_OPTIONS+= --ignore-dependencies
310 _RUBYGEM_OPTIONS+= --local ${WRKSRC}/${GEM_NAME}.gem
311 .if !empty(RUBY_BUILD_RI:M[nN][oO])
312 _RUBYGEM_OPTIONS+= --no-ri
314 .if !empty(RUBY_BUILD_RDOC:M[nN][oO])
315 _RUBYGEM_OPTIONS+= --no-rdoc
317 .if !empty(CONFIGURE_ARGS)
318 _RUBYGEM_OPTIONS+= -- --build-args ${CONFIGURE_ARGS}
321 RUBYGEM_INSTALL_ROOT_OPTION= --install-root ${RUBYGEM_INSTALL_ROOT}
323 .PHONY: _gem-build-install-root
324 _gem-build-install-root:
325 @${STEP_MSG} "Installing gem into installation root"
326 ${RUN} ${SETENV} ${MAKE_ENV} ${RUBYGEM_ENV} \
327 ${RUBYGEM} install ${RUBYGEM_OPTIONS} ${_RUBYGEM_OPTIONS}
329 # The ``gem'' command doesn't exit with a non-zero result even if the
330 # install of the gem failed, so we do the check and return the proper exit
333 .PHONY: _gem-build-install-root-check
334 _gem-build-install-root-check:
335 ${RUN} ${TEST} -f ${RUBYGEM_INSTALL_ROOT}${PREFIX}/${GEM_CACHEDIR}/${GEM_NAME}.gem || \
336 ${FAIL_MSG} "Installing ${GEM_NAME}.gem into installation root failed."
338 .if !empty(GEM_CLEANBUILD)
339 .PHONY: _gem-build-cleanbuild
340 _gem-build-cleanbuild:
341 @${STEP_MSG} "Cleaning intermediate gem build files"
342 ${RUN} cd ${RUBYGEM_INSTALL_ROOT}${PREFIX}/${GEM_LIBDIR} && \
343 find . -print | sort -r | \
344 while read file; do \
346 ${GEM_CLEANBUILD:@.p.@./${.p.}) ;;@} \
349 [ ! -e ${WRKSRC:Q}"/$$file" ] || continue; \
350 if [ -d "$$file" ]; then \
351 ${ECHO} "rmdir "${GEM_NAME}"/$$file"; \
354 ${ECHO} "rm "${GEM_NAME}"/$$file"; \
363 ### The gem-install target installs the gem in ${_RUBY_INSTALL_ROOT} into
364 ### the actual gem repository.
366 GENERATE_PLIST+= ${RUBYGEM_GENERATE_PLIST}
367 RUBYGEM_GENERATE_PLIST= \
368 ${ECHO} "@comment The following lines are automatically generated." && \
369 ( cd ${RUBYGEM_INSTALL_ROOT}${PREFIX} && test -d ${GEM_DOCDIR} && \
370 ${FIND} ${GEM_DOCDIR} \! -type d -print | ${SORT} ) || true;
372 _GEM_INSTALL_TARGETS= _gem-build-install-root
373 _GEM_INSTALL_TARGETS+= _gem-build-install-root-check
374 .if !empty(GEM_CLEANBUILD)
375 _GEM_INSTALL_TARGETS+= _gem-build-cleanbuild
377 _GEM_INSTALL_TARGETS+= _gem-install
379 .ORDER: ${_GEM_INSTALL_TARGETS}
383 do-install: ${_GEM_INSTALL_TARGETS}
387 @${STEP_MSG} "gem install"
388 ${RUN} cd ${RUBYGEM_INSTALL_ROOT}${PREFIX} && \
389 pax -rwpe . ${DESTDIR}${PREFIX}