Fixup fromcvs/togit conversion
[pkgsrcv2.git] / lang / ruby / gem.mk
1 # $NetBSD: gem.mk,v 1.14 2012/03/20 06:14:30 taca Exp $
2 #
3 # This Makefile fragment is intended to be included by packages that build
4 # and install Ruby gems.
5 #
6 # Package-settable variables:
7 #
8 # RUBYGEMS_REQD
9 #       Minimum version of required rubygems.  Ruby base packages contain:
10 #
11 #               ruby18-base:    none
12 #               ruby193-base:   1.8.11
13 #
14 #       If newer version of rubygems is resuiqred, set RUBYGEMS_REQD to
15 #       minimum version.
16 #
17 #       Default: not defined
18 #
19 # OVERRIDE_GEMSPEC
20 #       Fix version of depending gem or modify files in gemspec.
21 #
22 #       (1) Specify as gem and dependency pattern as usual pkgsrc's one.
23 #
24 #               Example:
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:
28 #       
29 #                       OVERRIDE_GEMSPEC+= json>=1.4.6
30 #       
31 #                   If you want to change depending gem to "json_pure>=1.4.6"
32 #                   then use:
33 #       
34 #                       OVERRIDE_GEMSPEC+= json:json_pure>=1.4.6
35 #       
36 #                   You can also remove dependency:
37 #       
38 #                       OVERRIDE_GEMSPEC+= json:
39 #
40 #       (2) Modify files in gemspec.
41 #
42 #               Example:
43 #                       Remove files (a.rb and b.rb) from 'files':
44 #
45 #                       OVERRIDE_GEMSPEC+= :files a.rb= b.rb=
46 #
47 #               Example:
48 #                       Add a file (exec.rb) to 'executables':
49 #
50 #                       OVERRIDE_GEMSPEC+= :executables exec.rb
51 #
52 #               Example:
53 #                       Rename a file (from 'ruby' to 'ruby193') in 'files':
54 #
55 #                       OVERRIDE_GEMSPEC+= :files ruby=ruby193
56 #
57 #       Note: Because of limited parser, argumetns for (1) must preceed to (2).
58 #
59 #       Default: (empty)
60 #
61 # BUILD_TARGET
62 #       The Rakefile target that creates a local gem if using the
63 #       ``rake'' GEM_BUILD method.
64 #
65 #       Default: gem
66 #
67 # GEM_BUILD
68 #       The method used to build the local gem.
69 #
70 #       Possible: gemspec, rake
71 #       Default: gemspec
72 #
73 # GEM_CLEANBUILD
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
77 #       default is "ext/*"
78 #
79 #       Example:
80 #
81 #           GEM_CLEANBUILD=     *.o *.${RUBY_DLEXT} mkmf.log    
82 #
83 # GEM_NAME
84 #       The name of the gem to install.  The default value is ${DISTNAME}.
85 #
86 # GEM_SPECFILE
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.
90 #
91 # RUBYGEM_OPTIONS
92 #       Optional parameter to pass to gem on install stage.
93 #
94 #
95 # Variables defined in this file:
96 #
97 # GEM_DOCDIR
98 #       The relative path from PREFIX to the directory in the local gem
99 #       repository that holds the documentation for the installed gem.
100 #
101 # GEM_LIBDIR
102 #       The relative path from PREFIX to the directory in the local gem
103 #       repository that holds the contents of the installed gem.
104 #
105 # RUBYGEM
106 #       The path to the rubygems ``gem'' script.
107 #
108 .if !defined(_RUBYGEM_MK)
109 _RUBYGEM_MK=    # defined
110
111 # By default, assume that gems are capable of user-destdir installation.
112 PKG_DESTDIR_SUPPORT?=   user-destdir
113
114 # replace interpeter bin default
115 REPLACE_RUBY_DIRS?=     bin
116 REPLACE_RUBY_PAT?=      *
117
118 # Default to using rake to build the local gem from the unpacked files.
119 GEM_BUILD?=     gemspec
120
121 OVERRIDE_GEMSPEC?=      # default is empty
122
123 RUBYGEM_LANG?=  en_US.UTF-8
124 RUBYGEM_ENV?=   LANG=${RUBYGEM_LANG} LC_CTYPE=${RUBYGEM_LANG}
125
126 .if !empty(OVERRIDE_GEMSPEC)
127 UPDATE_GEMSPEC=         ../../lang/ruby/files/update-gemspec.rb
128 .endif
129
130 .if ${GEM_BUILD} == "rake"
131 USE_RAKE?=              YES
132 .endif
133
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}/ \
142                         { next; }
143 PRINT_PLIST_AWK+=       /^${GEM_HOME:S|/|\\/|g}/ \
144                         { gsub(/${GEM_HOME:S|/|\\/|g}/, "$${GEM_HOME}"); \
145                         print; next; }
146 PRINT_PLIST_AWK+=       /^${RUBY_GEM_BASE:S|/|\\/|g}/ \
147                 { gsub(/${RUBY_GEM_BASE:S|/|\\/|g}/, "$${RUBY_GEM_BASE}"); \
148                         print; next; }
149
150 # Include this early in case some of its target are needed
151 .include "../../lang/ruby/modules.mk"
152
153 # Build and run-time dependencies for Ruby prior to 1.9.
154 #
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.
157 #
158 # If we're using rake to build the local gem, then include it as a
159 # build tool.
160 #
161
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
165 .else # !ruby18
166 . if defined(RUBYGEMS_REQD)
167
168 RUBY193_RUBYGEMS_VERS=  1.8.11
169
170 _RUBYGEMS_REQD_MAJOR=   ${RUBYGEMS_REQD:C/\.[0-9\.]+$//}
171 _RUBYGEMS_REQD_MINORS=  ${RUBYGEMS_REQD:C/^([0-9]+)\.*//}
172
173 .  if ${RUBY_VER} == "193"
174 _RUBYGEMS_MAJOR=        ${RUBY193_RUBYGEMS_VERS:C/\.[0-9\.]+$//}
175 _RUBYGEMS_MINORS=       ${RUBY193_RUBYGEMS_VERS:C/^([0-9]+)\.*//}
176 .  else
177 PKG_FAIL_REASON+= "Unknown Ruby version specified: ${RUBY_VER}."
178 .  endif
179
180 _RUBYGEMS_REQD= NO
181
182 .  if ${_RUBYGEMS_REQD_MAJOR} > ${_RUBYGEMS_MAJOR}
183 _RUBYGEMS_REQD= YES
184 .  elif ${_RUBYGEMS_REQD_MAJOR} == ${_RUBYGEMS_MAJOR}
185 .   if !empty(_RUBYGEMS_MINORS) && ${_RUBYGEMS_REQD_MINORS} > ${_RUBYGEMS_MINORS}
186 _RUBYGEMS_REQD= YES
187 .   endif
188 .  endif
189
190 .  if empty(_RUBYGEMS_REQD:M[nN][oO])
191 DEPENDS+=       ${RUBY_PKGPREFIX}-rubygems>=${RUBYGEMS_REQD}:../../misc/rubygems
192 .  endif
193 . endif
194 .endif # !ruby18
195
196 CATEGORIES+=    ruby
197 MASTER_SITES?=  http://rubygems.org/gems/ http://gems.rubyforge.org/gems/
198
199 EXTRACT_SUFX?=  .gem
200 DISTFILES?=     ${DISTNAME}${EXTRACT_SUFX}
201
202 # If any of the DISTFILES are gems, then skip the normal do-extract actions
203 # and extract them ourselves in gem-extract.
204 #
205 .if !empty(DISTFILES:M*.gem)
206 EXTRACT_ONLY?=  # empty
207 .endif
208
209 # Base directory for Gems
210 MAKE_ENV+=      GEM_PATH=${PREFIX}/${GEM_HOME}
211
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
217
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'
221 # permission.
222 #
223 CHECK_INTERPRETER_SKIP+=        ${GEM_LIBDIR}/*
224 CHECK_PERMS_SKIP+=              ${GEM_LIBDIR}/*
225
226 # RUBYGEM holds the path to RubyGems' gem command
227 EVAL_PREFIX+=   RUBYGEM_PREFIX=${RUBYGEM_NAME}
228 RUBYGEM=        ${RUBYGEM_PREFIX}/bin/${RUBYGEM_NAME}
229
230 # PLIST support
231 PLIST_SUBST+=           GEM_NAME=${GEM_NAME}
232 PLIST_SUBST+=           GEM_LIBDIR=${GEM_LIBDIR}
233 PLIST_SUBST+=           GEM_DOCDIR=${GEM_DOCDIR}
234
235 ###
236 ### gem-extract
237 ###
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.
241 ###
242 GEM_SPECFILE?=  ${WRKDIR}/${DISTNAME}.gemspec
243
244 .PHONY: gem-extract
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
255 .  endfor
256 .endif
257
258 ###
259 ### gem-build
260 ###
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.
265 ###
266 GEM_CLEANBUILD?=        ext/*
267
268 .if !empty(GEM_CLEANBUILD:M/*) || !empty(GEM_CLEANBUILD:M*../*)
269 PKG_FAIL_REASON=        "GEM_CLEANBUILD must be relative to "${PREFIX}/${GEM_LIBDIR:Q}"."
270 .endif
271
272 .PHONY: gem-build
273 do-build: _gem-pre-build gem-build
274
275 _gem-pre-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}
280 .endif
281         @${STEP_MSG} "Removing backup files of patch before build"
282         @find ${WRKSRC} -name \*.orig -exec rm {} \;
283
284 gem-build: _gem-${GEM_BUILD}-build
285
286 .PHONY: _gem-gemspec-build
287 _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."
292
293 BUILD_TARGET?=  gem
294
295 .PHONY: _gem-rake-build
296 _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; \
302                 exit 0; \
303         done
304
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
313 .endif
314 .if !empty(RUBY_BUILD_RDOC:M[nN][oO])
315 _RUBYGEM_OPTIONS+=      --no-rdoc
316 .endif
317 .if !empty(CONFIGURE_ARGS)
318 _RUBYGEM_OPTIONS+=      -- --build-args ${CONFIGURE_ARGS}
319 .endif
320
321 RUBYGEM_INSTALL_ROOT_OPTION=    --install-root ${RUBYGEM_INSTALL_ROOT}
322
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}
328
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
331 # code ourselves.
332 #
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."
337
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                                             \
345                 case $$file in                                          \
346                 ${GEM_CLEANBUILD:@.p.@./${.p.}) ;;@}                    \
347                 *)      continue ;;                                     \
348                 esac;                                                   \
349                 [ ! -e ${WRKSRC:Q}"/$$file" ] || continue;              \
350                 if [ -d "$$file" ]; then                                \
351                         ${ECHO} "rmdir "${GEM_NAME}"/$$file";           \
352                         rmdir $$file;                                   \
353                 else                                                    \
354                         ${ECHO} "rm "${GEM_NAME}"/$$file";              \
355                         rm -f $$file;                                   \
356                 fi;                                                     \
357         done
358 .endif
359
360 ###
361 ### gem-install
362 ###
363 ### The gem-install target installs the gem in ${_RUBY_INSTALL_ROOT} into
364 ### the actual gem repository.
365 ###
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;
371
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
376 .endif
377 _GEM_INSTALL_TARGETS+=  _gem-install
378
379 .ORDER: ${_GEM_INSTALL_TARGETS}
380
381 USE_TOOLS+=     pax
382
383 do-install: ${_GEM_INSTALL_TARGETS}
384
385 .PHONY: _gem-install
386 _gem-install:
387         @${STEP_MSG} "gem install"
388         ${RUN} cd ${RUBYGEM_INSTALL_ROOT}${PREFIX} && \
389                 pax -rwpe . ${DESTDIR}${PREFIX}
390
391 .endif