Guest User

Untitled

a guest
Jul 22nd, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.52 KB | None | 0 0
  1. #!/bin/bash -ex
  2.  
  3. # Fetch the old CHOST from make.conf or profile.
  4. OLD_CHOST=$(portageq envvar CHOST)
  5.  
  6. # The new CHOST needs to end in hf. The vendor part doesn't really
  7. # matter as long as it's not softfloat or softfp but the convention is
  8. # to replace hardfloat with unknown.
  9. CHOST=${OLD_CHOST/-hardfloat-/-unknown-}hf
  10.  
  11. if [[ ${OLD_CHOST} == *hf ]]; then
  12. echo "You already have the new *hf CHOST? Either you've already successful run this or you're doing something crazy."
  13. exit 1
  14. fi
  15.  
  16. # This function is used to skip parts of this script if it is being
  17. # retried following a failure. It checks whether the given atom has
  18. # been built against the new CHOST.
  19. atom_new_chost() {
  20. local path=${VDB}/${1}/CHOST
  21. [[ ! -f ${path} ]] && return 1
  22. [[ $(cat "${path}") == ${CHOST} ]]
  23. }
  24.  
  25. # This script supports prefixed systems.
  26. EPREFIX=$(portageq envvar EPREFIX)
  27.  
  28. # The VDB path is needed for atom_new_chost above.
  29. VDB=$(portageq vdb_path)
  30.  
  31. # Fetch the old profile and convert to the new profile. The new
  32. # profile may already be selected if this script is being retried so
  33. # convert both ways just in case. Note that some profiles like
  34. # hardened are not versioned so these will not change. That is okay.
  35. OLD_PROFILE=$(eselect --brief profile show)
  36. OLD_PROFILE=${OLD_PROFILE/17.0/13.0}
  37. NEW_PROFILE=${OLD_PROFILE/13.0/17.0}
  38.  
  39. # What are the latest binutils and gcc versions we can install?
  40. BINUTILS_PF=$(portageq best_visible "${EPREFIX}"/ sys-devel/binutils)
  41. GCC_PF=$(portageq best_visible "${EPREFIX}"/ sys-devel/gcc)
  42.  
  43. # We need the new binutils and gcc SLOTs for some operations below.
  44. BINUTILS_SLOT=$(portageq metadata "${EPREFIX}"/ ebuild "${BINUTILS_PF}" SLOT)
  45. GCC_SLOT=$(portageq metadata "${EPREFIX}"/ ebuild "${GCC_PF}" SLOT)
  46.  
  47. # What libc does this profile use, what is the latest we can install?
  48. LIBC=$(portageq expand_virtual "${EPREFIX}"/ virtual/libc)
  49. LIBC_PF=$(portageq best_visible "${EPREFIX}"/ "${LIBC}")
  50.  
  51. # What are the latest libtool and perl versions we can install?
  52. LIBTOOL_PF=$(portageq best_visible "${EPREFIX}"/ sys-devel/libtool)
  53. PERL_PF=$(portageq best_visible "${EPREFIX}"/ dev-lang/perl)
  54.  
  55. # Ensure portage, sandbox, and portage-utils are up to date. Older
  56. # sandbox can block newer libtool. portage-utils is needed for qfile.
  57. emerge -v1u sys-apps/portage sys-apps/sandbox app-portage/portage-utils
  58.  
  59. # Backup the old toolchain, just in case.
  60. atom_new_chost "${BINUTILS_PF}" ||
  61. quickpkg sys-devel/binutils sys-devel/gcc "${LIBC}"
  62.  
  63. # Select the new profile.
  64. eselect profile set ${NEW_PROFILE}
  65.  
  66. # If we modify make.conf with the new CHOST now then we won't be able
  67. # to reliably determine the old CHOST when retrying this script. Just
  68. # override with this variable and modify make.conf at the end.
  69. export CHOST
  70.  
  71. # Build binutils with the new CHOST.
  72. atom_new_chost "${BINUTILS_PF}" ||
  73. emerge -v1 "=${BINUTILS_PF}"
  74.  
  75. # Remove old binutils versions to avoid a mix-up.
  76. ! emerge -C "<${BINUTILS_PF}"
  77.  
  78. # Explicitly select the newly built binutils.
  79. binutils-config "${CHOST}-${BINUTILS_SLOT}"
  80.  
  81. # Build gcc with the new CHOST.
  82. atom_new_chost "${GCC_PF}" ||
  83. emerge -v1 "=${GCC_PF}"
  84.  
  85. # Remove old gcc versions to avoid a mix-up.
  86. ! emerge -C "<${GCC_PF}"
  87.  
  88. # Explicitly select the newly built gcc.
  89. gcc-config -f "${CHOST}-${GCC_SLOT}"
  90.  
  91. # Delete old CHOST files from env.d.
  92. find "${EPREFIX}"/etc/env.d/ -regex ".*\\b${OLD_CHOST}\\b.*" -exec rm -v {} +
  93.  
  94. # Delete old CHOST files from ld.so.conf.d, which may not exist.
  95. [[ -d "${EPREFIX}"/etc/ld.so.conf.d ]] &&
  96. find "${EPREFIX}"/etc/ld.so.conf.d/ -regex ".*\\b${OLD_CHOST}\\b.*" -exec rm -v {} +
  97.  
  98. # Refresh the environment.
  99. env-update
  100. source "${EPREFIX}"/etc/profile
  101. ldconfig
  102.  
  103. # Build libc with the new CHOST.
  104. atom_new_chost "${LIBC_PF}" ||
  105. emerge -v1 "=${LIBC_PF}"
  106.  
  107. # Rebuild libtool and binutils as required for a PIE migration.
  108. if ! atom_new_chost "${LIBTOOL_PF}"; then
  109. emerge -v1 "=${BINUTILS_PF}"
  110. emerge -v1 "=${LIBTOOL_PF}"
  111. fi
  112.  
  113. # fix_libtool_files.sh is sometimes needed after changing CHOST.
  114. "${EPREFIX}"/usr/share/gcc-data/"${CHOST}/${GCC_SLOT}"/fix_libtool_files.sh "${GCC_SLOT}" --oldarch "${OLD_CHOST}"
  115.  
  116. # Delete any orphaned files under /usr/bin that are prefixed with the
  117. # old CHOST. This is mainly to clean up files created by gcc-config.
  118. qfile -o "${EPREFIX}"/usr/bin/"${OLD_CHOST}"-* | xargs rm -vf
  119.  
  120. # Rename any remaining files under /usr/bin that are prefixed with the
  121. # old CHOST so that they now have the new CHOST instead.
  122. find "${EPREFIX}"/usr/bin -name "${OLD_CHOST}-*" -execdir sh -c "X='{}'; mv -v \"\${X}\" \"\${X/${OLD_CHOST}/${CHOST}}\"" \;
  123.  
  124. # The previous operation will have broken symlinks such as xml2-config
  125. # so replace the old CHOST with the new CHOST in such symlinks.
  126. find "${EPREFIX}"/usr/bin -lname "${OLD_CHOST}-*" -execdir sh -c "X='{}'; ln -snfv \"${CHOST}-\${X##*/}\" \"\${X}\"" \;
  127.  
  128. # Safely delete broken symlinks and empty directories from the old
  129. # toolchain including the main top-level directory, if possible.
  130. ! find "${EPREFIX}/usr/${OLD_CHOST}"/ -xtype l -delete
  131. ! find "${EPREFIX}/usr/${OLD_CHOST}"/ -type d -empty -delete
  132.  
  133. # Update make.conf with the new CHOST. We do this before rebuilding
  134. # Perl below because Perl is a prime candidate for package blockers,
  135. # which you are free to resolve manually at this point.
  136. sed -i "/CHOST=/s/${OLD_CHOST}/${CHOST}/" "${EPREFIX}"/etc/portage/make.conf
  137.  
  138. # Perl is sensitive to a CHOST change so rebuild.
  139. if portageq has_version "${EPREFIX}"/ dev-lang/perl && ! atom_new_chost "${PERL_PF}"; then
  140. emerge -v1 "=${PERL_PF}"
  141. fi
  142.  
  143. echo "All done! :)"
  144.  
  145. [[ -d "${EPREFIX}/usr/${OLD_CHOST}" ]] &&
  146. echo "Tried to delete ${EPREFIX}/usr/${OLD_CHOST} but it wasn't empty. Please check and delete it manually."
Add Comment
Please, Sign In to add comment