1 # Copyright 1999-2017 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
4 # @ECLASS: ruby-ng.eclass
6 # Ruby herd <ruby@gentoo.org>
8 # Author: Diego E. Pettenò <flameeyes@gentoo.org>
9 # Author: Alex Legler <a3li@gentoo.org>
10 # Author: Hans de Graaff <graaff@gentoo.org>
11 # @BLURB: An eclass for installing Ruby packages with proper support for multiple Ruby slots.
13 # The Ruby eclass is designed to allow an easier installation of Ruby packages
14 # and their incorporation into the Gentoo Linux system.
16 # Currently available targets are listed in ruby-utils.eclass
18 # This eclass does not define the implementation of the configure,
19 # compile, test, or install phases. Instead, the default phases are
20 # used. Specific implementations of these phases can be provided in
21 # the ebuild either to be run for each Ruby implementation, or for all
22 # Ruby implementations, as follows:
24 # * each_ruby_configure
25 # * all_ruby_configure
27 # @ECLASS-VARIABLE: USE_RUBY
31 # This variable contains a space separated list of targets (see above) a package
32 # is compatible to. It must be set before the `inherit' call. There is no
33 # default. All ebuilds are expected to set this variable.
35 # @ECLASS-VARIABLE: RUBY_PATCHES
38 # A String or Array of filenames of patches to apply to all implementations.
40 # @ECLASS-VARIABLE: RUBY_OPTIONAL
43 # Set the value to "yes" to make the dependency on a Ruby interpreter
44 # optional and then ruby_implementations_depend() to help populate
47 # @ECLASS-VARIABLE: RUBY_S
50 # If defined this variable determines the source directory name after
51 # unpacking. This defaults to the name of the package. Note that this
52 # variable supports a wildcard mechanism to help with github tarballs
53 # that contain the commit hash as part of the directory name.
55 # @ECLASS-VARIABLE: RUBY_QA_ALLOWED_LIBS
58 # If defined this variable contains a whitelist of shared objects that
59 # are allowed to exist even if they don't link to libruby. This avoids
60 # the QA check that makes this mandatory. This is most likely not what
61 # you are looking for if you get the related "Missing links" QA warning,
62 # since the proper fix is almost always to make sure the shared object
63 # is linked against libruby. There are cases were this is not the case
64 # and the shared object is generic code to be used in some other way
65 # (e.g. selenium's firefox driver extension). When set this argument is
66 # passed to "grep -E" to remove reporting of these shared objects.
75 inherit ${inherits} java-utils-2 multilib toolchain-funcs ruby-utils
77 EXPORT_FUNCTIONS src_unpack src_prepare src_configure src_compile src_test src_install pkg_setup
81 die "Unsupported EAPI=${EAPI} (too old) for ruby-ng.eclass" ;;
84 # S is no longer automatically assigned when it doesn't exist.
88 die "Unknown EAPI=${EAPI} for ruby-ng.eclass"
91 # @FUNCTION: ruby_implementation_depend
92 # @USAGE: target [comparator [version]]
93 # @RETURN: Package atom of a Ruby implementation to be used in dependencies.
95 # This function returns the formal package atom for a Ruby implementation.
97 # `target' has to be one of the valid values for USE_RUBY (see above)
99 # Set `comparator' and `version' to include a comparator (=, >=, etc.) and a
100 # version string to the returned string
101 ruby_implementation_depend() {
102 _ruby_implementation_depend $1
105 # @FUNCTION: _ruby_get_all_impls
107 # @RETURN: list of valid values in USE_RUBY
108 # Return a list of valid implementations in USE_RUBY, skipping the old
109 # implementations that are no longer supported.
110 _ruby_get_all_impls() {
112 for i in ${USE_RUBY}; do
114 # removed implementations
115 ruby19|ruby20|ruby21|jruby)
123 # @FUNCTION: ruby_samelib
124 # @RETURN: use flag string with current ruby implementations
126 # Convenience function to output the use dependency part of a
127 # dependency. Used as a building block for ruby_add_rdepend() and
128 # ruby_add_bdepend(), but may also be useful in an ebuild to specify
129 # more complex dependencies.
132 for _ruby_implementation in $(_ruby_get_all_impls); do
133 has -${_ruby_implementation} $@ || \
134 res="${res}ruby_targets_${_ruby_implementation}?,"
140 _ruby_atoms_samelib_generic() {
141 eshopts_push -o noglob
145 "||" | "(" | ")" | *"?")
148 echo "${token%[*}[RUBYTARGET,${token/*[}" ;;
150 echo "${token}[RUBYTARGET]" ;;
157 # @FUNCTION: ruby_implementation_command
158 # @RETURN: the path to the given ruby implementation
160 # Not all implementations have the same command basename as the
161 # target; This function translate between the two
162 ruby_implementation_command() {
165 # Add all USE_RUBY values where the flag name diverts from the binary here
167 echo $(type -p ${_ruby_name} 2>/dev/null)
170 _ruby_atoms_samelib() {
171 local atoms=$(_ruby_atoms_samelib_generic "$*")
173 for _ruby_implementation in $(_ruby_get_all_impls); do
174 echo "${atoms//RUBYTARGET/ruby_targets_${_ruby_implementation}}"
178 _ruby_wrap_conditions() {
179 local conditions="$1"
182 for condition in $conditions; do
183 atoms="${condition}? ( ${atoms} )"
189 # @FUNCTION: ruby_add_rdepend
190 # @USAGE: dependencies
192 # Adds the specified dependencies, with use condition(s) to RDEPEND,
193 # taking the current set of ruby targets into account. This makes sure
194 # that all ruby dependencies of the package are installed for the same
195 # ruby targets. Use this function for all ruby dependencies instead of
196 # setting RDEPEND yourself. The list of atoms uses the same syntax as
197 # normal dependencies.
199 # Note: runtime dependencies are also added as build-time test
205 [[ "${GENTOO_DEV}" == "yes" ]] && eqawarn "You can now use the usual syntax in ruby_add_rdepend for $CATEGORY/$PF"
206 ruby_add_rdepend "$(_ruby_wrap_conditions "$1" "$2")"
210 die "bad number of arguments to $0"
214 local dependency=$(_ruby_atoms_samelib "$1")
216 RDEPEND="${RDEPEND} $dependency"
218 # Add the dependency as a test-dependency since we're going to
219 # execute the code during test phase.
220 DEPEND="${DEPEND} test? ( ${dependency} )"
221 has test "$IUSE" || IUSE="${IUSE} test"
224 # @FUNCTION: ruby_add_bdepend
225 # @USAGE: dependencies
227 # Adds the specified dependencies, with use condition(s) to DEPEND,
228 # taking the current set of ruby targets into account. This makes sure
229 # that all ruby dependencies of the package are installed for the same
230 # ruby targets. Use this function for all ruby dependencies instead of
231 # setting DEPEND yourself. The list of atoms uses the same syntax as
232 # normal dependencies.
237 [[ "${GENTOO_DEV}" == "yes" ]] && eqawarn "You can now use the usual syntax in ruby_add_bdepend for $CATEGORY/$PF"
238 ruby_add_bdepend "$(_ruby_wrap_conditions "$1" "$2")"
242 die "bad number of arguments to $0"
246 local dependency=$(_ruby_atoms_samelib "$1")
248 DEPEND="${DEPEND} $dependency"
252 # @FUNCTION: ruby_get_use_implementations
254 # Gets an array of ruby use targets enabled by the user
255 ruby_get_use_implementations() {
256 local i implementation
257 for implementation in $(_ruby_get_all_impls); do
258 use ruby_targets_${implementation} && i+=" ${implementation}"
263 # @FUNCTION: ruby_get_use_targets
265 # Gets an array of ruby use targets that the ebuild sets
266 ruby_get_use_targets() {
267 local t implementation
268 for implementation in $(_ruby_get_all_impls); do
269 t+=" ruby_targets_${implementation}"
274 # @FUNCTION: ruby_implementations_depend
275 # @RETURN: Dependencies suitable for injection into DEPEND and RDEPEND.
277 # Produces the dependency string for the various implementations of ruby
278 # which the package is being built against. This should not be used when
279 # RUBY_OPTIONAL is unset but must be used if RUBY_OPTIONAL=yes. Do not
280 # confuse this function with ruby_implementation_depend().
288 # DEPEND="ruby? ( $(ruby_implementations_depend) )"
289 # RDEPEND="${DEPEND}"
290 ruby_implementations_depend() {
292 for _ruby_implementation in $(_ruby_get_all_impls); do
293 depend="${depend}${depend+ }ruby_targets_${_ruby_implementation}? ( $(ruby_implementation_depend $_ruby_implementation) )"
298 IUSE+=" $(ruby_get_use_targets)"
299 # If you specify RUBY_OPTIONAL you also need to take care of
300 # ruby useflag and dependency.
301 if [[ ${RUBY_OPTIONAL} != yes ]]; then
302 DEPEND="${DEPEND} $(ruby_implementations_depend)"
303 RDEPEND="${RDEPEND} $(ruby_implementations_depend)"
307 REQUIRED_USE+=" || ( $(ruby_get_use_targets) )"
312 _ruby_invoke_environment() {
316 if [ -z "${RUBY_S}" ]; then
323 sub_S=${S#${WORKDIR}/}
327 # Special case, for the always-lovely GitHub fetches. With this,
328 # we allow the star glob to just expand to whatever directory it's
330 if [[ "${sub_S}" = *"*"* ]]; then
333 #The old method of setting S depends on undefined package
334 # manager behaviour, so encourage upgrading to EAPI=4.
335 eqawarn "Using * expansion of S is deprecated. Use EAPI and RUBY_S instead."
338 pushd "${WORKDIR}"/all &>/dev/null || die
339 # use an array to trigger filename expansion
340 # fun fact: this expansion fails in src_unpack() but the original
341 # code did not have any checks for failed expansion, so we can't
342 # really add one now without redesigning stuff hard.
344 if [[ ${#sub_S[@]} -gt 1 ]]; then
345 die "sub_S did expand to multiple paths: ${sub_S[*]}"
347 popd &>/dev/null || die
350 environment=$1; shift
352 my_WORKDIR="${WORKDIR}"/${environment}
353 S="${my_WORKDIR}"/"${sub_S}"
355 if [[ -d "${S}" ]]; then
356 pushd "$S" &>/dev/null || die
357 elif [[ -d "${my_WORKDIR}" ]]; then
358 pushd "${my_WORKDIR}" &>/dev/null || die
360 pushd "${WORKDIR}" &>/dev/null || die
363 ebegin "Running ${_PHASE:-${EBUILD_PHASE}} phase for $environment"
365 popd &>/dev/null || die
370 _ruby_each_implementation() {
372 for _ruby_implementation in $(_ruby_get_all_impls); do
373 # only proceed if it's requested
374 use ruby_targets_${_ruby_implementation} || continue
376 RUBY=$(ruby_implementation_command ${_ruby_implementation})
379 if [[ -n "$1" ]]; then
380 _ruby_invoke_environment ${_ruby_implementation} "$@"
386 if [[ ${invoked} == "no" ]]; then
387 eerror "You need to select at least one compatible Ruby installation target via RUBY_TARGETS in make.conf."
388 eerror "Compatible targets for this package are: $(_ruby_get_all_impls)"
390 eerror "See https://www.gentoo.org/proj/en/prog_lang/ruby/index.xml#doc_chap3 for more information."
392 die "No compatible Ruby target selected."
396 # @FUNCTION: ruby-ng_pkg_setup
398 # Check whether at least one ruby target implementation is present.
399 ruby-ng_pkg_setup() {
400 # This only checks that at least one implementation is present
401 # before doing anything; by leaving the parameters empty we know
402 # it's a special case.
403 _ruby_each_implementation
405 has ruby_targets_jruby ${IUSE} && use ruby_targets_jruby && java-pkg_setup-vm
408 # @FUNCTION: ruby-ng_src_unpack
410 # Unpack the source archive.
411 ruby-ng_src_unpack() {
412 mkdir "${WORKDIR}"/all
413 pushd "${WORKDIR}"/all &>/dev/null || die
415 # We don't support an each-unpack, it's either all or nothing!
416 if type all_ruby_unpack &>/dev/null; then
417 _ruby_invoke_environment all all_ruby_unpack
419 [[ -n ${A} ]] && unpack ${A}
422 popd &>/dev/null || die
425 _ruby_apply_patches() {
428 for patch in "${RUBY_PATCHES[@]}"; do
429 if [ -f "${patch}" ]; then
431 elif [ -f "${FILESDIR}/${patch}" ]; then
432 epatch "${FILESDIR}/${patch}"
434 die "Cannot find patch ${patch}"
439 if [[ -n ${RUBY_PATCHES[@]} ]]; then
440 eqawarn "RUBY_PATCHES is no longer supported, use PATCHES instead"
445 # This is a special case: instead of executing just in the special
446 # "all" environment, this will actually copy the effects on _all_
447 # the other environments, and is thus executed before the copy
448 type all_ruby_prepare &>/dev/null && all_ruby_prepare
451 _ruby_source_copy() {
452 # Until we actually find a reason not to, we use hardlinks, this
453 # should reduce the amount of disk space that is wasted by this.
454 cp -prlP all ${_ruby_implementation} \
455 || die "Unable to copy ${_ruby_implementation} environment"
458 # @FUNCTION: ruby-ng_src_prepare
460 # Apply patches and prepare versions for each ruby target
461 # implementation. Also carry out common clean up tasks.
462 ruby-ng_src_prepare() {
463 # Way too many Ruby packages are prepared on OSX without removing
464 # the extra data forks, we do it here to avoid repeating it for
465 # almost every other ebuild.
466 find . -name '._*' -delete
468 # Handle PATCHES and user supplied patches via the default phase
471 _ruby_invoke_environment all default
475 _ruby_invoke_environment all _ruby_apply_patches
477 _PHASE="source copy" \
478 _ruby_each_implementation _ruby_source_copy
480 if type each_ruby_prepare &>/dev/null; then
481 _ruby_each_implementation each_ruby_prepare
485 # @FUNCTION: ruby-ng_src_configure
487 # Configure the package.
488 ruby-ng_src_configure() {
489 if type each_ruby_configure &>/dev/null; then
490 _ruby_each_implementation each_ruby_configure
493 type all_ruby_configure &>/dev/null && \
494 _ruby_invoke_environment all all_ruby_configure
497 # @FUNCTION: ruby-ng_src_compile
499 # Compile the package.
500 ruby-ng_src_compile() {
501 if type each_ruby_compile &>/dev/null; then
502 _ruby_each_implementation each_ruby_compile
505 type all_ruby_compile &>/dev/null && \
506 _ruby_invoke_environment all all_ruby_compile
509 # @FUNCTION: ruby-ng_src_test
511 # Run tests for the package.
513 if type each_ruby_test &>/dev/null; then
514 _ruby_each_implementation each_ruby_test
517 type all_ruby_test &>/dev/null && \
518 _ruby_invoke_environment all all_ruby_test
521 _each_ruby_check_install() {
522 local scancmd=scanelf
523 # we have a Mach-O object here
524 [[ ${CHOST} == *-darwin ]] && scancmd=scanmacho
526 has "${EAPI}" 2 && ! use prefix && EPREFIX=
528 local libruby_basename=$(${RUBY} -rrbconfig -e 'puts RbConfig::CONFIG["LIBRUBY_SO"]')
529 local libruby_soname=$(basename $(${scancmd} -F "%S#F" -qS "${EPREFIX}/usr/$(get_libdir)/${libruby_basename}") 2>/dev/null)
530 local sitedir=$(${RUBY} -rrbconfig -e 'puts RbConfig::CONFIG["sitedir"]')
531 local sitelibdir=$(${RUBY} -rrbconfig -e 'puts RbConfig::CONFIG["sitelibdir"]')
533 # Look for wrong files in sitedir
534 # if [[ -d "${D}${sitedir}" ]]; then
535 # local f=$(find "${D}${sitedir}" -mindepth 1 -maxdepth 1 -not -wholename "${D}${sitelibdir}")
536 # if [[ -n ${f} ]]; then
537 # eerror "Found files in sitedir, outsite sitelibdir:"
539 # die "Misplaced files in sitedir"
543 # The current implementation lacks libruby (i.e.: jruby)
544 [[ -z ${libruby_soname} ]] && return 0
546 # Check also the gems directory, since we could be installing compiled
547 # extensions via ruby-fakegem; make sure to check only in sitelibdir, since
548 # that's what changes between two implementations (otherwise you'd get false
549 # positives now that Ruby 1.9.2 installs with the same sitedir as 1.8)
550 ${scancmd} -qnR "${D}${sitelibdir}" "${D}${sitelibdir/site_ruby/gems}" \
551 | fgrep -v "${libruby_soname}" \
552 | grep -E -v "${RUBY_QA_ALLOWED_LIBS}" \
553 > "${T}"/ruby-ng-${_ruby_implementation}-mislink.log
555 if [[ -s "${T}"/ruby-ng-${_ruby_implementation}-mislink.log ]]; then
556 ewarn "Extensions installed for ${_ruby_implementation} with missing links to ${libruby_soname}"
557 ewarn $(< "${T}"/ruby-ng-${_ruby_implementation}-mislink.log )
558 die "Missing links to ${libruby_soname}"
562 # @FUNCTION: ruby-ng_src_install
564 # Install the package for each ruby target implementation.
565 ruby-ng_src_install() {
566 if type each_ruby_install &>/dev/null; then
567 _ruby_each_implementation each_ruby_install
570 type all_ruby_install &>/dev/null && \
571 _ruby_invoke_environment all all_ruby_install
573 _PHASE="check install" \
574 _ruby_each_implementation _each_ruby_check_install
577 # @FUNCTION: ruby_rbconfig_value
578 # @USAGE: rbconfig item
579 # @RETURN: Returns the value of the given rbconfig item of the Ruby interpreter in ${RUBY}.
580 ruby_rbconfig_value() {
581 echo $(${RUBY} -rrbconfig -e "puts RbConfig::CONFIG['$1']")
585 # @USAGE: file [file...]
587 # Installs the specified file(s) into the sitelibdir of the Ruby interpreter in ${RUBY}.
589 [[ -z ${RUBY} ]] && die "\$RUBY is not set"
590 has "${EAPI}" 2 && ! use prefix && EPREFIX=
591 ( # don't want to pollute calling env
592 sitelibdir=$(ruby_rbconfig_value 'sitelibdir')
593 insinto ${sitelibdir#${EPREFIX}}
596 ) || die "failed to install $@"
599 # @FUNCTION: ruby_get_libruby
600 # @RETURN: The location of libruby*.so belonging to the Ruby interpreter in ${RUBY}.
602 ${RUBY} -rrbconfig -e 'puts File.join(RbConfig::CONFIG["libdir"], RbConfig::CONFIG["LIBRUBY"])'
605 # @FUNCTION: ruby_get_hdrdir
606 # @RETURN: The location of the header files belonging to the Ruby interpreter in ${RUBY}.
608 local rubyhdrdir=$(ruby_rbconfig_value 'rubyhdrdir')
610 if [[ "${rubyhdrdir}" = "nil" ]] ; then
611 rubyhdrdir=$(ruby_rbconfig_value 'archdir')
617 # @FUNCTION: ruby_get_version
618 # @RETURN: The version of the Ruby interpreter in ${RUBY}, or what 'ruby' points to.
620 local ruby=${RUBY:-$(type -p ruby 2>/dev/null)}
622 echo $(${ruby} -e 'puts RUBY_VERSION')
625 # @FUNCTION: ruby_get_implementation
626 # @RETURN: The implementation of the Ruby interpreter in ${RUBY}, or what 'ruby' points to.
627 ruby_get_implementation() {
628 local ruby=${RUBY:-$(type -p ruby 2>/dev/null)}
630 case $(${ruby} --version) in
643 # @FUNCTION: ruby-ng_rspec <arguments>
645 # This is simply a wrapper around the rspec command (executed by $RUBY})
646 # which also respects TEST_VERBOSE and NOCOLOR environment variables.
647 # Optionally takes arguments to pass on to the rspec invocation. The
648 # environment variable RSPEC_VERSION can be used to control the specific
649 # rspec version that must be executed. It defaults to 2 for historical
652 local version=${RSPEC_VERSION-2}
655 # Explicitly pass the expected spec directory since the versioned
656 # rspec wrappers don't handle this automatically.
657 if [ ${#@} -eq 0 ]; then
661 if [[ ${DEPEND} != *"dev-ruby/rspec"* ]]; then
662 ewarn "Missing dev-ruby/rspec in \${DEPEND}"
668 rspec_params+=" --no-color"
671 rspec_params+=" --color"
675 case ${TEST_VERBOSE} in
677 rspec_params+=" --format documentation"
680 rspec_params+=" --format progress"
684 ${RUBY} -S rspec-${version} ${rspec_params} ${files} || die "rspec failed"
687 # @FUNCTION: ruby-ng_cucumber
689 # This is simply a wrapper around the cucumber command (executed by $RUBY})
690 # which also respects TEST_VERBOSE and NOCOLOR environment variables.
692 if [[ ${DEPEND} != *"dev-util/cucumber"* ]]; then
693 ewarn "Missing dev-util/cucumber in \${DEPEND}"
696 local cucumber_params=
699 cucumber_params+=" --no-color"
702 cucumber_params+=" --color"
706 case ${TEST_VERBOSE} in
708 cucumber_params+=" --format pretty"
711 cucumber_params+=" --format progress"
715 if [[ ${RUBY} == *jruby ]]; then
716 ewarn "Skipping cucumber tests on JRuby (unsupported)."
720 ${RUBY} -S cucumber ${cucumber_params} "$@" || die "cucumber failed"
723 # @FUNCTION: ruby-ng_testrb-2
725 # This is simply a replacement for the testrb command that load the test
726 # files and execute them, with test-unit 2.x. This actually requires
727 # either an old test-unit-2 version or 2.5.1-r1 or later, as they remove
728 # their script and we installed a broken wrapper for a while.
729 # This also respects TEST_VERBOSE and NOCOLOR environment variables.
731 if [[ ${DEPEND} != *"dev-ruby/test-unit"* ]]; then
732 ewarn "Missing dev-ruby/test-unit in \${DEPEND}"
738 testrb_params+=" --no-use-color"
741 testrb_params+=" --use-color=auto"
745 case ${TEST_VERBOSE} in
747 testrb_params+=" --verbose=verbose"
750 testrb_params+=" --verbose=normal"
754 ${RUBY} -S testrb-2 ${testrb_params} "$@" || die "testrb-2 failed"