gemspec.sh (6175B)
1# 2# This helper is for templates using gemspec files from upstream or Rubygems. 3# 4do_build() { 5 : ${gem_cmd:=gem} 6 : ${gemspec:=${pkgname#ruby-}.gemspec} 7 8 # Fix packages that keep Gem.gemspec as the name instead of the proper 9 # $pkgname.gemspec 10 if [ -f Gem.gemspec ]; then 11 gemspec=Gem.gemspec 12 fi 13 14 if [ -f .gemspec ]; then 15 gemspec=.gemspec 16 fi 17 18 # Hardcode name and version just in case they try something funny like 19 # requiring RELEASE to be on the environment to not append -rc0 to version 20 # Some even forget to update the version in the gemspec after releasing 21 sed -ri "s|(\.name .*)=.*|\1 = \"${pkgname#ruby-}\"|g" $gemspec 22 sed -ri "s|(\.version .*)=.*|\1 = \"${version}\"|g" $gemspec 23 24 # Replace use of `git ls-files` with find, use printf so we can print without starting 25 # dot-slash path 26 sed -i 's|`git ls-files`|`find . -type f -printf "%P\\n"`|g' $gemspec 27 28 # Sadly ruby isn't capable of handling nullbytes in a command so we have to use 29 # -print0, then try using sed to remove the suffix 30 # The end result is: 31 # `find . -type f -print0 | sed -e "s@\\./@@g"` 32 sed -i 's|`git ls-files -z`|`find . -type f -print0 \| sed -e "s@\\\\./@@g"`|g' $gemspec 33 34 if [ "$CROSS_BUILD" ]; then 35 36 local _TARGET_PLATFORM 37 38 _TARGET_PLATFORM="$(ruby -r \ 39 $(find ${XBPS_CROSS_BASE}/usr/lib/ruby -iname rbconfig.rb) \ 40 -e 'puts RbConfig::CONFIG["arch"]' 2>/dev/null)" 41 42 # Patch all instances of extconf that use create_makefile 43 for f in $(find . -type f -name 'extconf.rb'); do 44 if [ ! -f ${f}.orig ]; then 45 # Create a .extconf file that forces the Makefile to use our environment 46 # this allows us to cross-compile like it is done with meson cross-files 47 cat<<EOF>append 48\$CPPFLAGS = ENV['CPPFLAGS'] if ENV['CPPFLAGS'] 49RbConfig::MAKEFILE_CONFIG['CC'] = ENV['CC'] if ENV['CC'] 50RbConfig::MAKEFILE_CONFIG['CXX'] = ENV['CXX'] if ENV['CXX'] 51RbConfig::MAKEFILE_CONFIG['LD'] = ENV['LD'] if ENV['LD'] 52RbConfig::MAKEFILE_CONFIG['CFLAGS'] = ENV['CFLAGS'] if ENV['CFLAGS'] 53RbConfig::MAKEFILE_CONFIG['CPPFLAGS'] = ENV['CPPFLAGS'] if ENV['CPPFLAGS'] 54RbConfig::MAKEFILE_CONFIG['CXXFLAGS'] = ENV['CXXFLAGS'] if ENV['CXXFLAGS'] 55EOF 56 cat $f > append2 57 # Use sed and enable verbose mode 58 cat<<EOF>>append2 59system("sed -i 's|^V =.*|V = 1|' Makefile") 60system("sed -i 's|^CFLAGS.*|CFLAGS = \$(CCDLFLAGS) ${VOID_TARGET_CFLAGS} \$(ARCH_FLAG)|' Makefile") 61system("sed -i 's|^topdir.*|topdir = ${XBPS_CROSS_BASE}/usr/include/ruby-\$(ruby_version)|' Makefile") 62system("sed -i 's|^hdrdir.*|hdrdir = ${XBPS_CROSS_BASE}/usr/include/ruby-\$(ruby_version)|' Makefile") 63system("sed -i 's|^arch_hdrdir.*|arch_hdrdir = ${XBPS_CROSS_BASE}/usr/include/ruby-\$(ruby_version)/\$(arch)|' Makefile") 64system("sed -i 's|^arch =.*|arch = ${_TARGET_PLATFORM}|' Makefile") 65system("sed -i 's|^dldflags =.*|dldflags = ${LDFLAGS}|' Makefile") 66EOF 67 68 # Create a backup which we will restore later 69 cp $f ${f}.orig 70 71 # Patch extconf.rb for cross compile 72 cat append append2 > $f 73 fi 74 done 75 fi 76 77 # If we are downloading a gem file then create a spec out of it 78 for f in $distfiles; do 79 if [ "${f##*.}" = "gem" ]; then 80 $gem_cmd spec \ 81 "${XBPS_SRCDISTDIR}/${pkgname}-${version}/${f##*/}" \ 82 --ruby > $gemspec 83 fi 84 done 85 86 sed 's|~>|>=|g' -i $gemspec 87 88 $gem_cmd build --verbose ${gemspec} 89 90 if [ "$CROSS_BUILD" ]; then 91 # Restore previous extconf.rb which we ship. 92 find . -type f -name 'extconf.rb.orig' | while read -r f; do 93 mv $f ${f%.*} 94 done 95 fi 96} 97 98do_install() { 99 : ${gem_cmd:=gem} 100 101 local _GEMDIR _INSTDIR 102 103 _GEMDIR=$($gem_cmd env gemdir) 104 _INSTDIR=${DESTDIR}/${_GEMDIR}/gems/${pkgname#ruby-}-${version} 105 106 # Ruby is very eager to add CFLAGS everywhere there is a compilation 107 # but we do both cross compilation of the modules and host compilation 108 # for checks, so unset CFLAGS and keep it in a separate value. 109 # We will manually pass CFLAGS as VOID_TAGET_CFLAGS to cross-compilation 110 # And ruby will use rbconfig.rb to get the proper CFLAGS for host compilation 111 VOID_TARGET_CFLAGS="$CFLAGS" 112 export VOID_TARGET_CFLAGS 113 unset CFLAGS 114 115 $gem_cmd install \ 116 --local \ 117 --install-dir ${DESTDIR}/${_GEMDIR} \ 118 --bindir ${DESTDIR}/usr/bin \ 119 --ignore-dependencies \ 120 --no-document \ 121 --verbose \ 122 "${pkgname#ruby-}-${version}.gem" \ 123 -- $configure_args 124 125 # Remove cache 126 rm -rf ${DESTDIR}/${_GEMDIR}/cache 127 128 # Remove ext directory, they are only source code and configuration 129 # The actual extensions are in a arch path guarded 130 rm -rf ${_INSTDIR}/ext 131 132 # Remove duplicated library that is available in a arch guarded 133 # extension 134 rm -rf ${_INSTDIR}/lib/*.so 135 136 # Remove installed tests and benchmarks 137 rm -rf ${_INSTDIR}/{test,tests,autotest,benchmark,benchmarks,script,examples,demo} 138 139 # Remove files shipped on the root of the gem, most of the time they are useless 140 find ${_INSTDIR} -maxdepth 1 -type f -delete 141 142 # Remove unnecessary files 143 find ${DESTDIR}/${_GEMDIR}/extensions \( -name mkmf.log -o -name gem_make.out \) -delete 144 145 # Place manpages in usr/share/man/man[0-9] 146 if [ -d ${_INSTDIR}/man ]; then 147 find ${_INSTDIR}/man -type f -name '*.[0-8n]' | while read -r m; do 148 vman ${m} 149 done 150 fi 151 152 rm -rf "${_INSTDIR}/man" 153 154 # Place executables in /usr/bin 155 if [ -d "${_INSTDIR}/bin" ]; then 156 for f in "${_INSTDIR}"/bin/*; do 157 vbin "${f}" 158 done 159 fi 160 161 rm -rf ${_INSTDIR}/bin 162 163 # Place conf files in their places 164 if [ -d ${_INSTDIR}/etc ]; then 165 find ${_INSTDIR}/etc -type f | while read -r c; do 166 vmkdir ${c%/*}/ 167 mv ${c} "${DESTDIR}/${c##*${_INSTDIR}/etc/}/" 168 done 169 fi 170 171 rm -rf ${_INSTDIR}/etc 172 173 if [ "$CROSS_BUILD" ]; then 174 175 local _TARGET_PLATFORM _TARGET_EXT_DIR 176 177 # Get arch of the target and host platform by reading the rbconfig.rb 178 # of the cross ruby 179 _TARGET_PLATFORM="$(ruby -r \ 180 $(find ${XBPS_CROSS_BASE}/usr/lib/ruby -iname rbconfig.rb) \ 181 -e 'puts RbConfig::CONFIG["arch"]' 2>/dev/null)" 182 183 # Path to the extensions on a package, ruby installs against the platform 184 # of the host, so we have to move them to the correct place 185 _TARGET_EXT_DIR="${DESTDIR}/${_GEMDIR}/extensions/${_TARGET_PLATFORM}" 186 187 find ${DESTDIR}/${_GEMDIR}/extensions -maxdepth 1 -type d \ 188 -exec mv '{}' ${_TARGET_EXT_DIR} \; 189 fi 190}