1 # Copyright 1999-2015 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
5 # @ECLASS: ruby-ng.eclass
7 # Ruby herd <ruby@gentoo.org>
9 # Author: Diego E. Pettenò <flameeyes@gentoo.org>
10 # Author: Alex Legler <a3li@gentoo.org>
11 # Author: Hans de Graaff <graaff@gentoo.org>
12 # @BLURB: An eclass for installing Ruby packages with proper support for multiple Ruby slots.
14 # The Ruby eclass is designed to allow an easier installation of Ruby packages
15 # and their incorporation into the Gentoo Linux system.
17 # Currently available targets are:
18 # * ruby18 - Ruby (MRI) 1.8.x
19 # * ruby19 - Ruby (MRI) 1.9.x
20 # * ruby20 - Ruby (MRI) 2.0.x
21 # * ruby21 - Ruby (MRI) 2.1.x
22 # * ruby22 - Ruby (MRI) 2.2.x
23 # * ree18 - Ruby Enterprise Edition 1.8.x
27 # This eclass does not define the implementation of the configure,
28 # compile, test, or install phases. Instead, the default phases are
29 # used. Specific implementations of these phases can be provided in
30 # the ebuild either to be run for each Ruby implementation, or for all
31 # Ruby implementations, as follows:
33 # * each_ruby_configure
34 # * all_ruby_configure
36 # @ECLASS-VARIABLE: USE_RUBY
40 # This variable contains a space separated list of targets (see above) a package
41 # is compatible to. It must be set before the `inherit' call. There is no
42 # default. All ebuilds are expected to set this variable.
44 # @ECLASS-VARIABLE: RUBY_PATCHES
47 # A String or Array of filenames of patches to apply to all implementations.
49 # @ECLASS-VARIABLE: RUBY_OPTIONAL
51 # Set the value to "yes" to make the dependency on a Ruby interpreter
52 # optional and then ruby_implementations_depend() to help populate
55 # @ECLASS-VARIABLE: RUBY_S
58 # If defined this variable determines the source directory name after
59 # unpacking. This defaults to the name of the package. Note that this
60 # variable supports a wildcard mechanism to help with github tarballs
61 # that contain the commit hash as part of the directory name.
63 # @ECLASS-VARIABLE: RUBY_QA_ALLOWED_LIBS
66 # If defined this variable contains a whitelist of shared objects that
67 # are allowed to exist even if they don't link to libruby. This avoids
68 # the QA check that makes this mandatory. This is most likely not what
69 # you are looking for if you get the related "Missing links" QA warning,
70 # since the proper fix is almost always to make sure the shared object
71 # is linked against libruby. There are cases were this is not the case
72 # and the shared object is generic code to be used in some other way
73 # (e.g. selenium's firefox driver extension). When set this argument is
74 # passed to "grep -E" to remove reporting of these shared objects.
76 inherit eutils java-utils-2 multilib toolchain-funcs ruby-utils
78 EXPORT_FUNCTIONS src_unpack src_prepare src_configure src_compile src_test src_install pkg_setup
82 die "Unsupported EAPI=${EAPI} (too old) for ruby-ng.eclass" ;;
85 # S is no longer automatically assigned when it doesn't exist.
89 die "Unknown EAPI=${EAPI} for ruby-ng.eclass"
92 # @FUNCTION: ruby_implementation_depend
93 # @USAGE: target [comparator [version]]
94 # @RETURN: Package atom of a Ruby implementation to be used in dependencies.
96 # This function returns the formal package atom for a Ruby implementation.
98 # `target' has to be one of the valid values for USE_RUBY (see above)
100 # Set `comparator' and `version' to include a comparator (=, >=, etc.) and a
101 # version string to the returned string
102 ruby_implementation_depend() {
103 _ruby_implementation_depend $1
106 # @FUNCTION: ruby_samelib
107 # @RETURN: use flag string with current ruby implementations
109 # Convenience function to output the use dependency part of a
110 # dependency. Used as a building block for ruby_add_rdepend() and
111 # ruby_add_bdepend(), but may also be useful in an ebuild to specify
112 # more complex dependencies.
115 for _ruby_implementation in $USE_RUBY; do
116 has -${_ruby_implementation} $@ || \
117 res="${res}ruby_targets_${_ruby_implementation}?,"
123 _ruby_atoms_samelib_generic() {
124 eshopts_push -o noglob
128 "||" | "(" | ")" | *"?")
131 echo "${token%[*}[RUBYTARGET,${token/*[}" ;;
133 echo "${token}[RUBYTARGET]" ;;
140 # @FUNCTION: ruby_implementation_command
141 # @RETURN: the path to the given ruby implementation
143 # Not all implementations have the same command basename as the
144 # target; namely Ruby Enterprise 1.8 uses ree18 and rubyee18
145 # respectively. This function translate between the two
146 ruby_implementation_command() {
149 # Add all USE_RUBY values where the flag name diverts from the binary here
156 echo $(type -p ${_ruby_name} 2>/dev/null)
159 _ruby_atoms_samelib() {
160 local atoms=$(_ruby_atoms_samelib_generic "$*")
162 for _ruby_implementation in $USE_RUBY; do
163 echo "${atoms//RUBYTARGET/ruby_targets_${_ruby_implementation}}"
167 _ruby_wrap_conditions() {
168 local conditions="$1"
171 for condition in $conditions; do
172 atoms="${condition}? ( ${atoms} )"
178 # @FUNCTION: ruby_add_rdepend
179 # @USAGE: dependencies
181 # Adds the specified dependencies, with use condition(s) to RDEPEND,
182 # taking the current set of ruby targets into account. This makes sure
183 # that all ruby dependencies of the package are installed for the same
184 # ruby targets. Use this function for all ruby dependencies instead of
185 # setting RDEPEND yourself. The list of atoms uses the same syntax as
186 # normal dependencies.
188 # Note: runtime dependencies are also added as build-time test
194 [[ "${GENTOO_DEV}" == "yes" ]] && eqawarn "You can now use the usual syntax in ruby_add_rdepend for $CATEGORY/$PF"
195 ruby_add_rdepend "$(_ruby_wrap_conditions "$1" "$2")"
199 die "bad number of arguments to $0"
203 local dependency=$(_ruby_atoms_samelib "$1")
205 RDEPEND="${RDEPEND} $dependency"
207 # Add the dependency as a test-dependency since we're going to
208 # execute the code during test phase.
209 DEPEND="${DEPEND} test? ( ${dependency} )"
210 has test "$IUSE" || IUSE="${IUSE} test"
213 # @FUNCTION: ruby_add_bdepend
214 # @USAGE: dependencies
216 # Adds the specified dependencies, with use condition(s) to DEPEND,
217 # taking the current set of ruby targets into account. This makes sure
218 # that all ruby dependencies of the package are installed for the same
219 # ruby targets. Use this function for all ruby dependencies instead of
220 # setting DEPEND yourself. The list of atoms uses the same syntax as
221 # normal dependencies.
226 [[ "${GENTOO_DEV}" == "yes" ]] && eqawarn "You can now use the usual syntax in ruby_add_bdepend for $CATEGORY/$PF"
227 ruby_add_bdepend "$(_ruby_wrap_conditions "$1" "$2")"
231 die "bad number of arguments to $0"
235 local dependency=$(_ruby_atoms_samelib "$1")
237 DEPEND="${DEPEND} $dependency"
241 # @FUNCTION: ruby_get_use_implementations
243 # Gets an array of ruby use targets enabled by the user
244 ruby_get_use_implementations() {
245 local i implementation
246 for implementation in ${USE_RUBY}; do
247 use ruby_targets_${implementation} && i+=" ${implementation}"
252 # @FUNCTION: ruby_get_use_targets
254 # Gets an array of ruby use targets that the ebuild sets
255 ruby_get_use_targets() {
256 local t implementation
257 for implementation in ${USE_RUBY}; do
258 t+=" ruby_targets_${implementation}"
263 # @FUNCTION: ruby_implementations_depend
264 # @RETURN: Dependencies suitable for injection into DEPEND and RDEPEND.
266 # Produces the dependency string for the various implementations of ruby
267 # which the package is being built against. This should not be used when
268 # RUBY_OPTIONAL is unset but must be used if RUBY_OPTIONAL=yes. Do not
269 # confuse this function with ruby_implementation_depend().
277 # DEPEND="ruby? ( $(ruby_implementations_depend) )"
278 # RDEPEND="${DEPEND}"
279 ruby_implementations_depend() {
281 for _ruby_implementation in ${USE_RUBY}; do
282 depend="${depend}${depend+ }ruby_targets_${_ruby_implementation}? ( $(ruby_implementation_depend $_ruby_implementation) )"
287 IUSE+=" $(ruby_get_use_targets)"
288 # If you specify RUBY_OPTIONAL you also need to take care of
289 # ruby useflag and dependency.
290 if [[ ${RUBY_OPTIONAL} != yes ]]; then
291 DEPEND="${DEPEND} $(ruby_implementations_depend)"
292 RDEPEND="${RDEPEND} $(ruby_implementations_depend)"
296 REQUIRED_USE+=" || ( $(ruby_get_use_targets) )"
301 _ruby_invoke_environment() {
305 if [ -z "${RUBY_S}" ]; then
312 sub_S=${S#${WORKDIR}/}
316 # Special case, for the always-lovely GitHub fetches. With this,
317 # we allow the star glob to just expand to whatever directory it's
319 if [[ "${sub_S}" = *"*"* ]]; then
322 #The old method of setting S depends on undefined package
323 # manager behaviour, so encourage upgrading to EAPI=4.
324 eqawarn "Using * expansion of S is deprecated. Use EAPI and RUBY_S instead."
327 pushd "${WORKDIR}"/all &>/dev/null || die
328 sub_S=$(eval ls -d "${sub_S}" 2>/dev/null)
329 popd &>/dev/null || die
332 environment=$1; shift
334 my_WORKDIR="${WORKDIR}"/${environment}
335 S="${my_WORKDIR}"/"${sub_S}"
337 if [[ -d "${S}" ]]; then
338 pushd "$S" &>/dev/null || die
339 elif [[ -d "${my_WORKDIR}" ]]; then
340 pushd "${my_WORKDIR}" &>/dev/null || die
342 pushd "${WORKDIR}" &>/dev/null || die
345 ebegin "Running ${_PHASE:-${EBUILD_PHASE}} phase for $environment"
347 popd &>/dev/null || die
352 _ruby_each_implementation() {
354 for _ruby_implementation in ${USE_RUBY}; do
355 # only proceed if it's requested
356 use ruby_targets_${_ruby_implementation} || continue
358 RUBY=$(ruby_implementation_command ${_ruby_implementation})
361 if [[ -n "$1" ]]; then
362 _ruby_invoke_environment ${_ruby_implementation} "$@"
368 if [[ ${invoked} == "no" ]]; then
369 eerror "You need to select at least one compatible Ruby installation target via RUBY_TARGETS in make.conf."
370 eerror "Compatible targets for this package are: ${USE_RUBY}"
372 eerror "See https://www.gentoo.org/proj/en/prog_lang/ruby/index.xml#doc_chap3 for more information."
374 die "No compatible Ruby target selected."
378 # @FUNCTION: ruby-ng_pkg_setup
380 # Check whether at least one ruby target implementation is present.
381 ruby-ng_pkg_setup() {
382 # This only checks that at least one implementation is present
383 # before doing anything; by leaving the parameters empty we know
384 # it's a special case.
385 _ruby_each_implementation
387 has ruby_targets_jruby ${IUSE} && use ruby_targets_jruby && java-pkg_setup-vm
390 # @FUNCTION: ruby-ng_src_unpack
392 # Unpack the source archive.
393 ruby-ng_src_unpack() {
394 mkdir "${WORKDIR}"/all
395 pushd "${WORKDIR}"/all &>/dev/null || die
397 # We don't support an each-unpack, it's either all or nothing!
398 if type all_ruby_unpack &>/dev/null; then
399 _ruby_invoke_environment all all_ruby_unpack
401 [[ -n ${A} ]] && unpack ${A}
404 popd &>/dev/null || die
407 _ruby_apply_patches() {
408 for patch in "${RUBY_PATCHES[@]}"; do
409 if [ -f "${patch}" ]; then
411 elif [ -f "${FILESDIR}/${patch}" ]; then
412 epatch "${FILESDIR}/${patch}"
414 die "Cannot find patch ${patch}"
418 # This is a special case: instead of executing just in the special
419 # "all" environment, this will actually copy the effects on _all_
420 # the other environments, and is thus executed before the copy
421 type all_ruby_prepare &>/dev/null && all_ruby_prepare
424 _ruby_source_copy() {
425 # Until we actually find a reason not to, we use hardlinks, this
426 # should reduce the amount of disk space that is wasted by this.
427 cp -prlP all ${_ruby_implementation} \
428 || die "Unable to copy ${_ruby_implementation} environment"
431 # @FUNCTION: ruby-ng_src_prepare
433 # Apply patches and prepare versions for each ruby target
434 # implementation. Also carry out common clean up tasks.
435 ruby-ng_src_prepare() {
436 # Way too many Ruby packages are prepared on OSX without removing
437 # the extra data forks, we do it here to avoid repeating it for
438 # almost every other ebuild.
439 find . -name '._*' -delete
441 _ruby_invoke_environment all _ruby_apply_patches
443 _PHASE="source copy" \
444 _ruby_each_implementation _ruby_source_copy
446 if type each_ruby_prepare &>/dev/null; then
447 _ruby_each_implementation each_ruby_prepare
451 # @FUNCTION: ruby-ng_src_configure
453 # Configure the package.
454 ruby-ng_src_configure() {
455 if type each_ruby_configure &>/dev/null; then
456 _ruby_each_implementation each_ruby_configure
459 type all_ruby_configure &>/dev/null && \
460 _ruby_invoke_environment all all_ruby_configure
463 # @FUNCTION: ruby-ng_src_compile
465 # Compile the package.
466 ruby-ng_src_compile() {
467 if type each_ruby_compile &>/dev/null; then
468 _ruby_each_implementation each_ruby_compile
471 type all_ruby_compile &>/dev/null && \
472 _ruby_invoke_environment all all_ruby_compile
475 # @FUNCTION: ruby-ng_src_test
477 # Run tests for the package.
479 if type each_ruby_test &>/dev/null; then
480 _ruby_each_implementation each_ruby_test
483 type all_ruby_test &>/dev/null && \
484 _ruby_invoke_environment all all_ruby_test
487 _each_ruby_check_install() {
488 local scancmd=scanelf
489 # we have a Mach-O object here
490 [[ ${CHOST} == *-darwin ]] && scancmd=scanmacho
492 has "${EAPI}" 2 && ! use prefix && EPREFIX=
494 local libruby_basename=$(${RUBY} -rrbconfig -e 'puts RbConfig::CONFIG["LIBRUBY_SO"]')
495 local libruby_soname=$(basename $(${scancmd} -F "%S#F" -qS "${EPREFIX}/usr/$(get_libdir)/${libruby_basename}") 2>/dev/null)
496 local sitedir=$(${RUBY} -rrbconfig -e 'puts RbConfig::CONFIG["sitedir"]')
497 local sitelibdir=$(${RUBY} -rrbconfig -e 'puts RbConfig::CONFIG["sitelibdir"]')
499 # Look for wrong files in sitedir
500 # if [[ -d "${D}${sitedir}" ]]; then
501 # local f=$(find "${D}${sitedir}" -mindepth 1 -maxdepth 1 -not -wholename "${D}${sitelibdir}")
502 # if [[ -n ${f} ]]; then
503 # eerror "Found files in sitedir, outsite sitelibdir:"
505 # die "Misplaced files in sitedir"
509 # The current implementation lacks libruby (i.e.: jruby)
510 [[ -z ${libruby_soname} ]] && return 0
512 # Check also the gems directory, since we could be installing compiled
513 # extensions via ruby-fakegem; make sure to check only in sitelibdir, since
514 # that's what changes between two implementations (otherwise you'd get false
515 # positives now that Ruby 1.9.2 installs with the same sitedir as 1.8)
516 ${scancmd} -qnR "${D}${sitelibdir}" "${D}${sitelibdir/site_ruby/gems}" \
517 | fgrep -v "${libruby_soname}" \
518 | grep -E -v "${RUBY_QA_ALLOWED_LIBS}" \
519 > "${T}"/ruby-ng-${_ruby_implementation}-mislink.log
521 if [[ -s "${T}"/ruby-ng-${_ruby_implementation}-mislink.log ]]; then
522 ewarn "Extensions installed for ${_ruby_implementation} with missing links to ${libruby_soname}"
523 ewarn $(< "${T}"/ruby-ng-${_ruby_implementation}-mislink.log )
524 die "Missing links to ${libruby_soname}"
528 # @FUNCTION: ruby-ng_src_install
530 # Install the package for each ruby target implementation.
531 ruby-ng_src_install() {
532 if type each_ruby_install &>/dev/null; then
533 _ruby_each_implementation each_ruby_install
536 type all_ruby_install &>/dev/null && \
537 _ruby_invoke_environment all all_ruby_install
539 _PHASE="check install" \
540 _ruby_each_implementation _each_ruby_check_install
543 # @FUNCTION: ruby_rbconfig_value
544 # @USAGE: rbconfig item
545 # @RETURN: Returns the value of the given rbconfig item of the Ruby interpreter in ${RUBY}.
546 ruby_rbconfig_value() {
547 echo $(${RUBY} -rrbconfig -e "puts RbConfig::CONFIG['$1']")
551 # @USAGE: file [file...]
553 # Installs the specified file(s) into the sitelibdir of the Ruby interpreter in ${RUBY}.
555 [[ -z ${RUBY} ]] && die "\$RUBY is not set"
556 has "${EAPI}" 2 && ! use prefix && EPREFIX=
557 ( # don't want to pollute calling env
558 sitelibdir=$(ruby_rbconfig_value 'sitelibdir')
559 insinto ${sitelibdir#${EPREFIX}}
562 ) || die "failed to install $@"
565 # @FUNCTION: ruby_get_libruby
566 # @RETURN: The location of libruby*.so belonging to the Ruby interpreter in ${RUBY}.
568 ${RUBY} -rrbconfig -e 'puts File.join(RbConfig::CONFIG["libdir"], RbConfig::CONFIG["LIBRUBY"])'
571 # @FUNCTION: ruby_get_hdrdir
572 # @RETURN: The location of the header files belonging to the Ruby interpreter in ${RUBY}.
574 local rubyhdrdir=$(ruby_rbconfig_value 'rubyhdrdir')
576 if [[ "${rubyhdrdir}" = "nil" ]] ; then
577 rubyhdrdir=$(ruby_rbconfig_value 'archdir')
583 # @FUNCTION: ruby_get_version
584 # @RETURN: The version of the Ruby interpreter in ${RUBY}, or what 'ruby' points to.
586 local ruby=${RUBY:-$(type -p ruby 2>/dev/null)}
588 echo $(${ruby} -e 'puts RUBY_VERSION')
591 # @FUNCTION: ruby_get_implementation
592 # @RETURN: The implementation of the Ruby interpreter in ${RUBY}, or what 'ruby' points to.
593 ruby_get_implementation() {
594 local ruby=${RUBY:-$(type -p ruby 2>/dev/null)}
596 case $(${ruby} --version) in
612 # @FUNCTION: ruby-ng_rspec <arguments>
614 # This is simply a wrapper around the rspec command (executed by $RUBY})
615 # which also respects TEST_VERBOSE and NOCOLOR environment variables.
616 # Optionally takes arguments to pass on to the rspec invocation. The
617 # environment variable RSPEC_VERSION can be used to control the specific
618 # rspec version that must be executed. It defaults to 2 for historical
621 local version=${RSPEC_VERSION-2}
624 # Explicitly pass the expected spec directory since the versioned
625 # rspec wrappers don't handle this automatically.
626 if [ ${#@} -eq 0 ]; then
630 if [[ ${DEPEND} != *"dev-ruby/rspec"* ]]; then
631 ewarn "Missing dev-ruby/rspec in \${DEPEND}"
637 rspec_params+=" --no-color"
640 rspec_params+=" --color"
644 case ${TEST_VERBOSE} in
646 rspec_params+=" --format documentation"
649 rspec_params+=" --format progress"
653 ${RUBY} -S rspec-${version} ${rspec_params} ${files} || die "rspec failed"
656 # @FUNCTION: ruby-ng_cucumber
658 # This is simply a wrapper around the cucumber command (executed by $RUBY})
659 # which also respects TEST_VERBOSE and NOCOLOR environment variables.
661 if [[ ${DEPEND} != *"dev-util/cucumber"* ]]; then
662 ewarn "Missing dev-util/cucumber in \${DEPEND}"
665 local cucumber_params=
668 cucumber_params+=" --no-color"
671 cucumber_params+=" --color"
675 case ${TEST_VERBOSE} in
677 cucumber_params+=" --format pretty"
680 cucumber_params+=" --format progress"
684 if [[ ${RUBY} == *jruby ]]; then
685 ewarn "Skipping cucumber tests on JRuby (unsupported)."
689 ${RUBY} -S cucumber ${cucumber_params} "$@" || die "cucumber failed"
692 # @FUNCTION: ruby-ng_testrb-2
694 # This is simply a replacement for the testrb command that load the test
695 # files and execute them, with test-unit 2.x. This actually requires
696 # either an old test-unit-2 version or 2.5.1-r1 or later, as they remove
697 # their script and we installed a broken wrapper for a while.
698 # This also respects TEST_VERBOSE and NOCOLOR environment variables.
700 if [[ ${DEPEND} != *"dev-ruby/test-unit"* ]]; then
701 ewarn "Missing dev-ruby/test-unit in \${DEPEND}"
707 testrb_params+=" --no-use-color"
710 testrb_params+=" --use-color=auto"
714 case ${TEST_VERBOSE} in
716 testrb_params+=" --verbose=verbose"
719 testrb_params+=" --verbose=normal"
723 ${RUBY} -S testrb-2 ${testrb_params} "$@" || die "testrb-2 failed"