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:
17 # * ruby19 - Ruby (MRI) 1.9.x
18 # * ruby20 - Ruby (MRI) 2.0.x
19 # * ruby21 - Ruby (MRI) 2.1.x
20 # * ruby22 - Ruby (MRI) 2.2.x
21 # * ruby23 - Ruby (MRI) 2.3.x
22 # * ruby24 - Ruby (MRI) 2.4.x
26 # This eclass does not define the implementation of the configure,
27 # compile, test, or install phases. Instead, the default phases are
28 # used. Specific implementations of these phases can be provided in
29 # the ebuild either to be run for each Ruby implementation, or for all
30 # Ruby implementations, as follows:
32 # * each_ruby_configure
33 # * all_ruby_configure
35 # @ECLASS-VARIABLE: USE_RUBY
39 # This variable contains a space separated list of targets (see above) a package
40 # is compatible to. It must be set before the `inherit' call. There is no
41 # default. All ebuilds are expected to set this variable.
43 # @ECLASS-VARIABLE: RUBY_PATCHES
46 # A String or Array of filenames of patches to apply to all implementations.
48 # @ECLASS-VARIABLE: RUBY_OPTIONAL
50 # Set the value to "yes" to make the dependency on a Ruby interpreter
51 # optional and then ruby_implementations_depend() to help populate
54 # @ECLASS-VARIABLE: RUBY_S
57 # If defined this variable determines the source directory name after
58 # unpacking. This defaults to the name of the package. Note that this
59 # variable supports a wildcard mechanism to help with github tarballs
60 # that contain the commit hash as part of the directory name.
62 # @ECLASS-VARIABLE: RUBY_QA_ALLOWED_LIBS
65 # If defined this variable contains a whitelist of shared objects that
66 # are allowed to exist even if they don't link to libruby. This avoids
67 # the QA check that makes this mandatory. This is most likely not what
68 # you are looking for if you get the related "Missing links" QA warning,
69 # since the proper fix is almost always to make sure the shared object
70 # is linked against libruby. There are cases were this is not the case
71 # and the shared object is generic code to be used in some other way
72 # (e.g. selenium's firefox driver extension). When set this argument is
73 # passed to "grep -E" to remove reporting of these shared objects.
82 inherit ${inherits} java-utils-2 multilib toolchain-funcs ruby-utils
84 EXPORT_FUNCTIONS src_unpack src_prepare src_configure src_compile src_test src_install pkg_setup
88 die "Unsupported EAPI=${EAPI} (too old) for ruby-ng.eclass" ;;
91 # S is no longer automatically assigned when it doesn't exist.
95 die "Unknown EAPI=${EAPI} for ruby-ng.eclass"
98 # @FUNCTION: ruby_implementation_depend
99 # @USAGE: target [comparator [version]]
100 # @RETURN: Package atom of a Ruby implementation to be used in dependencies.
102 # This function returns the formal package atom for a Ruby implementation.
104 # `target' has to be one of the valid values for USE_RUBY (see above)
106 # Set `comparator' and `version' to include a comparator (=, >=, etc.) and a
107 # version string to the returned string
108 ruby_implementation_depend() {
109 _ruby_implementation_depend $1
112 # @FUNCTION: ruby_samelib
113 # @RETURN: use flag string with current ruby implementations
115 # Convenience function to output the use dependency part of a
116 # dependency. Used as a building block for ruby_add_rdepend() and
117 # ruby_add_bdepend(), but may also be useful in an ebuild to specify
118 # more complex dependencies.
121 for _ruby_implementation in $USE_RUBY; do
122 has -${_ruby_implementation} $@ || \
123 res="${res}ruby_targets_${_ruby_implementation}?,"
129 _ruby_atoms_samelib_generic() {
130 eshopts_push -o noglob
134 "||" | "(" | ")" | *"?")
137 echo "${token%[*}[RUBYTARGET,${token/*[}" ;;
139 echo "${token}[RUBYTARGET]" ;;
146 # @FUNCTION: ruby_implementation_command
147 # @RETURN: the path to the given ruby implementation
149 # Not all implementations have the same command basename as the
150 # target; This function translate between the two
151 ruby_implementation_command() {
154 # 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 # use an array to trigger filename expansion
329 # fun fact: this expansion fails in src_unpack() but the original
330 # code did not have any checks for failed expansion, so we can't
331 # really add one now without redesigning stuff hard.
333 if [[ ${#sub_S[@]} -gt 1 ]]; then
334 die "sub_S did expand to multiple paths: ${sub_S[*]}"
336 popd &>/dev/null || die
339 environment=$1; shift
341 my_WORKDIR="${WORKDIR}"/${environment}
342 S="${my_WORKDIR}"/"${sub_S}"
344 if [[ -d "${S}" ]]; then
345 pushd "$S" &>/dev/null || die
346 elif [[ -d "${my_WORKDIR}" ]]; then
347 pushd "${my_WORKDIR}" &>/dev/null || die
349 pushd "${WORKDIR}" &>/dev/null || die
352 ebegin "Running ${_PHASE:-${EBUILD_PHASE}} phase for $environment"
354 popd &>/dev/null || die
359 _ruby_each_implementation() {
361 for _ruby_implementation in ${USE_RUBY}; do
362 # only proceed if it's requested
363 use ruby_targets_${_ruby_implementation} || continue
365 RUBY=$(ruby_implementation_command ${_ruby_implementation})
368 if [[ -n "$1" ]]; then
369 _ruby_invoke_environment ${_ruby_implementation} "$@"
375 if [[ ${invoked} == "no" ]]; then
376 eerror "You need to select at least one compatible Ruby installation target via RUBY_TARGETS in make.conf."
377 eerror "Compatible targets for this package are: ${USE_RUBY}"
379 eerror "See https://www.gentoo.org/proj/en/prog_lang/ruby/index.xml#doc_chap3 for more information."
381 die "No compatible Ruby target selected."
385 # @FUNCTION: ruby-ng_pkg_setup
387 # Check whether at least one ruby target implementation is present.
388 ruby-ng_pkg_setup() {
389 # This only checks that at least one implementation is present
390 # before doing anything; by leaving the parameters empty we know
391 # it's a special case.
392 _ruby_each_implementation
394 has ruby_targets_jruby ${IUSE} && use ruby_targets_jruby && java-pkg_setup-vm
397 # @FUNCTION: ruby-ng_src_unpack
399 # Unpack the source archive.
400 ruby-ng_src_unpack() {
401 mkdir "${WORKDIR}"/all
402 pushd "${WORKDIR}"/all &>/dev/null || die
404 # We don't support an each-unpack, it's either all or nothing!
405 if type all_ruby_unpack &>/dev/null; then
406 _ruby_invoke_environment all all_ruby_unpack
408 [[ -n ${A} ]] && unpack ${A}
411 popd &>/dev/null || die
414 _ruby_apply_patches() {
417 for patch in "${RUBY_PATCHES[@]}"; do
418 if [ -f "${patch}" ]; then
420 elif [ -f "${FILESDIR}/${patch}" ]; then
421 epatch "${FILESDIR}/${patch}"
423 die "Cannot find patch ${patch}"
428 if [[ -n ${RUBY_PATCHES[@]} ]]; then
429 eqawarn "RUBY_PATCHES is no longer supported, use PATCHES instead"
434 # This is a special case: instead of executing just in the special
435 # "all" environment, this will actually copy the effects on _all_
436 # the other environments, and is thus executed before the copy
437 type all_ruby_prepare &>/dev/null && all_ruby_prepare
440 _ruby_source_copy() {
441 # Until we actually find a reason not to, we use hardlinks, this
442 # should reduce the amount of disk space that is wasted by this.
443 cp -prlP all ${_ruby_implementation} \
444 || die "Unable to copy ${_ruby_implementation} environment"
447 # @FUNCTION: ruby-ng_src_prepare
449 # Apply patches and prepare versions for each ruby target
450 # implementation. Also carry out common clean up tasks.
451 ruby-ng_src_prepare() {
452 # Way too many Ruby packages are prepared on OSX without removing
453 # the extra data forks, we do it here to avoid repeating it for
454 # almost every other ebuild.
455 find . -name '._*' -delete
457 # Handle PATCHES and user supplied patches via the default phase
460 _ruby_invoke_environment all default
464 _ruby_invoke_environment all _ruby_apply_patches
466 _PHASE="source copy" \
467 _ruby_each_implementation _ruby_source_copy
469 if type each_ruby_prepare &>/dev/null; then
470 _ruby_each_implementation each_ruby_prepare
474 # @FUNCTION: ruby-ng_src_configure
476 # Configure the package.
477 ruby-ng_src_configure() {
478 if type each_ruby_configure &>/dev/null; then
479 _ruby_each_implementation each_ruby_configure
482 type all_ruby_configure &>/dev/null && \
483 _ruby_invoke_environment all all_ruby_configure
486 # @FUNCTION: ruby-ng_src_compile
488 # Compile the package.
489 ruby-ng_src_compile() {
490 if type each_ruby_compile &>/dev/null; then
491 _ruby_each_implementation each_ruby_compile
494 type all_ruby_compile &>/dev/null && \
495 _ruby_invoke_environment all all_ruby_compile
498 # @FUNCTION: ruby-ng_src_test
500 # Run tests for the package.
502 if type each_ruby_test &>/dev/null; then
503 _ruby_each_implementation each_ruby_test
506 type all_ruby_test &>/dev/null && \
507 _ruby_invoke_environment all all_ruby_test
510 _each_ruby_check_install() {
511 local scancmd=scanelf
512 # we have a Mach-O object here
513 [[ ${CHOST} == *-darwin ]] && scancmd=scanmacho
515 has "${EAPI}" 2 && ! use prefix && EPREFIX=
517 local libruby_basename=$(${RUBY} -rrbconfig -e 'puts RbConfig::CONFIG["LIBRUBY_SO"]')
518 local libruby_soname=$(basename $(${scancmd} -F "%S#F" -qS "${EPREFIX}/usr/$(get_libdir)/${libruby_basename}") 2>/dev/null)
519 local sitedir=$(${RUBY} -rrbconfig -e 'puts RbConfig::CONFIG["sitedir"]')
520 local sitelibdir=$(${RUBY} -rrbconfig -e 'puts RbConfig::CONFIG["sitelibdir"]')
522 # Look for wrong files in sitedir
523 # if [[ -d "${D}${sitedir}" ]]; then
524 # local f=$(find "${D}${sitedir}" -mindepth 1 -maxdepth 1 -not -wholename "${D}${sitelibdir}")
525 # if [[ -n ${f} ]]; then
526 # eerror "Found files in sitedir, outsite sitelibdir:"
528 # die "Misplaced files in sitedir"
532 # The current implementation lacks libruby (i.e.: jruby)
533 [[ -z ${libruby_soname} ]] && return 0
535 # Check also the gems directory, since we could be installing compiled
536 # extensions via ruby-fakegem; make sure to check only in sitelibdir, since
537 # that's what changes between two implementations (otherwise you'd get false
538 # positives now that Ruby 1.9.2 installs with the same sitedir as 1.8)
539 ${scancmd} -qnR "${D}${sitelibdir}" "${D}${sitelibdir/site_ruby/gems}" \
540 | fgrep -v "${libruby_soname}" \
541 | grep -E -v "${RUBY_QA_ALLOWED_LIBS}" \
542 > "${T}"/ruby-ng-${_ruby_implementation}-mislink.log
544 if [[ -s "${T}"/ruby-ng-${_ruby_implementation}-mislink.log ]]; then
545 ewarn "Extensions installed for ${_ruby_implementation} with missing links to ${libruby_soname}"
546 ewarn $(< "${T}"/ruby-ng-${_ruby_implementation}-mislink.log )
547 die "Missing links to ${libruby_soname}"
551 # @FUNCTION: ruby-ng_src_install
553 # Install the package for each ruby target implementation.
554 ruby-ng_src_install() {
555 if type each_ruby_install &>/dev/null; then
556 _ruby_each_implementation each_ruby_install
559 type all_ruby_install &>/dev/null && \
560 _ruby_invoke_environment all all_ruby_install
562 _PHASE="check install" \
563 _ruby_each_implementation _each_ruby_check_install
566 # @FUNCTION: ruby_rbconfig_value
567 # @USAGE: rbconfig item
568 # @RETURN: Returns the value of the given rbconfig item of the Ruby interpreter in ${RUBY}.
569 ruby_rbconfig_value() {
570 echo $(${RUBY} -rrbconfig -e "puts RbConfig::CONFIG['$1']")
574 # @USAGE: file [file...]
576 # Installs the specified file(s) into the sitelibdir of the Ruby interpreter in ${RUBY}.
578 [[ -z ${RUBY} ]] && die "\$RUBY is not set"
579 has "${EAPI}" 2 && ! use prefix && EPREFIX=
580 ( # don't want to pollute calling env
581 sitelibdir=$(ruby_rbconfig_value 'sitelibdir')
582 insinto ${sitelibdir#${EPREFIX}}
585 ) || die "failed to install $@"
588 # @FUNCTION: ruby_get_libruby
589 # @RETURN: The location of libruby*.so belonging to the Ruby interpreter in ${RUBY}.
591 ${RUBY} -rrbconfig -e 'puts File.join(RbConfig::CONFIG["libdir"], RbConfig::CONFIG["LIBRUBY"])'
594 # @FUNCTION: ruby_get_hdrdir
595 # @RETURN: The location of the header files belonging to the Ruby interpreter in ${RUBY}.
597 local rubyhdrdir=$(ruby_rbconfig_value 'rubyhdrdir')
599 if [[ "${rubyhdrdir}" = "nil" ]] ; then
600 rubyhdrdir=$(ruby_rbconfig_value 'archdir')
606 # @FUNCTION: ruby_get_version
607 # @RETURN: The version of the Ruby interpreter in ${RUBY}, or what 'ruby' points to.
609 local ruby=${RUBY:-$(type -p ruby 2>/dev/null)}
611 echo $(${ruby} -e 'puts RUBY_VERSION')
614 # @FUNCTION: ruby_get_implementation
615 # @RETURN: The implementation of the Ruby interpreter in ${RUBY}, or what 'ruby' points to.
616 ruby_get_implementation() {
617 local ruby=${RUBY:-$(type -p ruby 2>/dev/null)}
619 case $(${ruby} --version) in
632 # @FUNCTION: ruby-ng_rspec <arguments>
634 # This is simply a wrapper around the rspec command (executed by $RUBY})
635 # which also respects TEST_VERBOSE and NOCOLOR environment variables.
636 # Optionally takes arguments to pass on to the rspec invocation. The
637 # environment variable RSPEC_VERSION can be used to control the specific
638 # rspec version that must be executed. It defaults to 2 for historical
641 local version=${RSPEC_VERSION-2}
644 # Explicitly pass the expected spec directory since the versioned
645 # rspec wrappers don't handle this automatically.
646 if [ ${#@} -eq 0 ]; then
650 if [[ ${DEPEND} != *"dev-ruby/rspec"* ]]; then
651 ewarn "Missing dev-ruby/rspec in \${DEPEND}"
657 rspec_params+=" --no-color"
660 rspec_params+=" --color"
664 case ${TEST_VERBOSE} in
666 rspec_params+=" --format documentation"
669 rspec_params+=" --format progress"
673 ${RUBY} -S rspec-${version} ${rspec_params} ${files} || die "rspec failed"
676 # @FUNCTION: ruby-ng_cucumber
678 # This is simply a wrapper around the cucumber command (executed by $RUBY})
679 # which also respects TEST_VERBOSE and NOCOLOR environment variables.
681 if [[ ${DEPEND} != *"dev-util/cucumber"* ]]; then
682 ewarn "Missing dev-util/cucumber in \${DEPEND}"
685 local cucumber_params=
688 cucumber_params+=" --no-color"
691 cucumber_params+=" --color"
695 case ${TEST_VERBOSE} in
697 cucumber_params+=" --format pretty"
700 cucumber_params+=" --format progress"
704 if [[ ${RUBY} == *jruby ]]; then
705 ewarn "Skipping cucumber tests on JRuby (unsupported)."
709 ${RUBY} -S cucumber ${cucumber_params} "$@" || die "cucumber failed"
712 # @FUNCTION: ruby-ng_testrb-2
714 # This is simply a replacement for the testrb command that load the test
715 # files and execute them, with test-unit 2.x. This actually requires
716 # either an old test-unit-2 version or 2.5.1-r1 or later, as they remove
717 # their script and we installed a broken wrapper for a while.
718 # This also respects TEST_VERBOSE and NOCOLOR environment variables.
720 if [[ ${DEPEND} != *"dev-ruby/test-unit"* ]]; then
721 ewarn "Missing dev-ruby/test-unit in \${DEPEND}"
727 testrb_params+=" --no-use-color"
730 testrb_params+=" --use-color=auto"
734 case ${TEST_VERBOSE} in
736 testrb_params+=" --verbose=verbose"
739 testrb_params+=" --verbose=normal"
743 ${RUBY} -S testrb-2 ${testrb_params} "$@" || die "testrb-2 failed"