Differences between version 9 and previous revision of KnowledgeBase/FreeBSD/buildpkgs.sh.

Other diffs: Previous Major Revision, Previous Author

Newer page: version 9 Last edited on Saturday, 21 August 2010 0:58:08 by CyberLeo Revert
Older page: version 8 Last edited on Saturday, 21 August 2010 0:38:39 by CyberLeo Revert
@@ -73,9 +73,9 @@
  mount -t devfs devfs "${chroot_dir}/dev" || return 1 
  mkdir -p "${chroot_dir}/usr/ports" || return 1 
  mount -t nullfs -r /usr/ports "${chroot_dir}/usr/ports" || return 1 
  mkdir -p "${chroot_dir}/var/ports/distfiles" || return 1 
- mount- t nullfs -w "${dist_dir}" "${chroot_dir}/var/ports/distfiles" || return 1 
+ mount -t nullfs -w "${dist_dir}" "${chroot_dir}/var/ports/distfiles" || return 1 
  
  # Chroot configuration 
  cp -f /etc/resolv.conf "${chroot_dir}/etc/resolv.conf" || return 1 
  
@@ -92,9 +92,8 @@
 
  
 # Evaluate a command line within the chroot 
 cheval() { 
- meh "cheval ${*}"  
  chroot "${chroot_dir}" env -i ${chroot_env} /bin/sh -c "cd ${chroot_pkg_dir}; ${*}" 
 
  
 # Run chrooted make 
@@ -113,9 +112,8 @@
 # Translate port origin to package name 
 port2pkg() { 
  while [ "${1}" ] 
  do 
- meh "port2pkg ${1}"  
  chmake "${1}" -V PKGNAME 
  shift 
  done 
 
@@ -123,9 +121,8 @@
 # Build dependencies (shallow, recursive, package) 
 port_bdeps() { 
  while [ "${1}" ] 
  do 
- meh "bdeps ${1}"  
  chmake "${1}" build-depends-list | sed -e 's#^/usr/ports/##' 
  shift 
  done 
 
@@ -144,9 +141,8 @@
 # Runtime dependencies (shallow, recursive, package) 
 port_rdeps() { 
  while [ "${1}" ] 
  do 
- meh "rdeps ${1}"  
  chmake "${1}" run-depends-list | sed -e 's#^/usr/ports/##' 
  shift 
  done 
 
@@ -164,9 +160,8 @@
 # All dependencies (shallow, recursive, package) 
 port_deps() { 
  while [ "${1}" ] 
  do 
- meh "deps ${1}"  
  chmake "${1}" all-depends-list | sed -e 's#^/usr/ports/##' 
  shift 
  done 
 
@@ -190,9 +185,9 @@
 # Run make config on a list of ports 
 port_config() { 
  while [ "${1}" ] 
  do 
- meh "config ${1}" 
+ meh "port config ${1}" 
  chmake "${1}" config < /dev/tty 
  shift 
  done 
 
@@ -207,9 +202,9 @@
  # if the config is already complete. Also use a cache to avoid re-config 
  # and re-recurse on previously handled port branches. 
  if echo "${_port_config_recursive_cache}" | grep -qv " ${1} " 
  then 
- meh "config-recursive ${1}" 
+ meh "port config-recursive ${1}" 
  chmake "${1}" config-conditional < /dev/tty 
  port_config_recursive $(port_deps "${1}") 
  _port_config_recursive_cache="${_port_config_recursive_cache} ${1} " 
  fi 
@@ -220,35 +215,35 @@
 # Remove saved config for a list of ports 
 port_rmconfig() { 
  while [ "${1}" ] 
  do 
- meh "rmconfig ${1}" 
+ meh "port rmconfig ${1}" 
  chmake "${1}" rmconfig 
  shift 
  done 
 
  
 # Remove saved config for a list of ports and all dependencies 
 port_rmconfig_recursive() { 
- meh "rmconfig-recursive ${*}" 
+ meh "port rmconfig-recursive ${*}" 
  port_rmconfig $(echo "${@}" $(port_all_deps "${@}") | sort | uniq) 
 
  
 # Obliterate saved configuration for all ports 
 port_rmconfig_all() { 
- meh "rmconfig-all" 
+ meh "port rmconfig-all" 
  cheval "cd /var/db/ports; find . -name 'options' -delete; find . -type d | xargs rmdir 2>/dev/null" 
 
  
 # Restore port build options from cpio 
 port_load_config() { 
- meh "load-config" 
+ meh "port load-config" 
  cheval "cd /var/db/ports; cpio -iv" 
 
  
 # Dump port build options to cpio 
 port_save_config() { 
- meh "save-config" 
+ meh "port save-config" 
  cheval "cd /var/db/ports; find . -type d -o -type f -name options | cpio -ovHnewc" 
 
  
 ######## 
@@ -280,27 +275,30 @@
  cp -f "${final_bdeps_dir}/${pkg}.tbz" "${bdeps_dir}" 2>/dev/null && meh "Loading dependent ${pkg}" 
  done 
  if ls "${bdeps_dir}"/*.tbz >/dev/null 2>&1 
  then 
+ meh "Installing dependencies"  
  cheval "cd ${chroot_bdeps_dir}; pkg_add -F *" || wtf "port_load_deps ${port} failed" 
  fi 
 
  
 # Build and install a port 
 port_build() { 
  local port="${1}" 
+ meh "Building ${port}"  
  chmake "${port}" clean build install clean || wtf "port_build ${port} failed" 
 
  
 # Package a port 
 port_package() { 
  local port="${1}" 
+ meh "Creating rdep package tree for ${port}"  
  cheval "pkg_create -Rvb $(port2pkg "${port}")" || wtf "port_package ${port} failed" 
 
  
 # Package port build dependencies, unless they're already run deps 
 port_stash_bdeps() { 
- meh "Stashing bdeps" 
+ meh "Stashing unsaved bdeps" 
  # This doesn't work well, because there can be bdeps that aren't listed as bdeps (bison) 
  #for pkg in $(pkg_bdeps "${1}") 
  #do 
  # [ ! -f "${pkg_dir}/${pkg}.tbz" ] && cheval "cd ${chroot_bdeps_dir}; pkg_create -vb ${pkg}" 
@@ -308,9 +306,9 @@
 
  # Instead, just save everything that's not already in rdeps as bdeps 
  for pkg in $(cheval pkg_info | awk '{print $1}') 
  do 
- if [ ! -f "${pkg_dir}/${pkg}.tbz" ] 
+ if [ ! -f "${pkg_dir}/${pkg}.tbz" -a ! -f "${bdeps _dir}/${pkg}.tbz" ] 
  then 
  cheval "cd ${chroot_bdeps_dir}; pkg_create -vb ${pkg}" || wtf "port_stash_bdeps failed" 
  fi 
  done 
@@ -326,8 +324,9 @@
 
  
 # Delete all installed packages (hope you saved them first) 
 pkg_delete_all() { 
+ meh "Clearing out installed packages"  
  cheval "pkg_delete -f \*" || wtf "pkg_delete_all failed" 
 
  
 ######## 

version 9

# Dump a list of leaf ports from a working system
# Leaf ports are ports that have nothing depending upon them
# These are generally the top-level ports; everything else should
# be pulled in by them
leaf_ports() {
  ( cd /var/db/pkg; ls -1 ) | while read pkg
  do
    pkg_info -Rq "${pkg}" | grep -q '' || pkg_info -oq "${pkg}"
  done | sort
}

########
#
# Configuration variable setup
#
########

# Base directory for everything
base_dir="/usr/home/cyberleo/vitanitest"

# Directory where distfiles will be stored between builds
dist_dir="${base_dir}/distfiles"

# Final directory for built packages (Outside chroot)
final_pkg_dir="${base_dir}/pkg"
final_bdeps_dir="${base_dir}/bdeps"

# Chroot directory
chroot_dir="${base_dir}/root"

# Package directories, must be under ${chroot_dir}
pkg_dir="${chroot_dir}/pkg"
bdeps_dir="${pkg_dir}/bdeps"

# Compute in-chroot pkg and bdeps dirs
chroot_pkg_dir="${pkg_dir##${chroot_dir}}"
chroot_bdeps_dir="${bdeps_dir##${chroot_dir}}"

# Chroot environment
chroot_env="
USER=root
HOME=/root
LOGNAME=root
PATH=:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
SHELL=/bin/sh
TERM=${TERM}
"

# Tempvars
unset _port_config_recursive_cache

########
#
# Boilerplate functions
#
########

meh() { printf " \033[1;32m*\033[0m %s\n" "${*}" >&2; }
omg() { printf " \033[1;33m*\033[0m %s\n" "${*}" >&2; }
wtf() { printf " \033[1;31m*\033[0m %s\n" "${*}" >&2; exit 1; }

########
#
# Chroot handling
#
########

# Setup a chroot
chsetup() {
  # Necessary mountpoints
  mkdir -p "${chroot_dir}/dev" || return 1
  mount -t devfs devfs "${chroot_dir}/dev" || return 1
  mkdir -p "${chroot_dir}/usr/ports" || return 1
  mount -t nullfs -r /usr/ports "${chroot_dir}/usr/ports" || return 1
  mkdir -p "${chroot_dir}/var/ports/distfiles" || return 1
  mount -t nullfs -w "${dist_dir}" "${chroot_dir}/var/ports/distfiles" || return 1

  # Chroot configuration
  cp -f /etc/resolv.conf "${chroot_dir}/etc/resolv.conf" || return 1

  # Target package directories
  mkdir -p "${pkg_dir}" || return 1
  mkdir -p "${bdeps_dir}" || return 1
}

# Tear down a chroot
chteardown() {
  umount "${chroot_dir}/var/ports/distfiles"
  umount "${chroot_dir}/usr/ports"
  umount "${chroot_dir}/dev"
}

# Evaluate a command line within the chroot
cheval() {
  chroot "${chroot_dir}" env -i ${chroot_env} /bin/sh -c "cd ${chroot_pkg_dir}; ${*}"
}

# Run chrooted make
chmake() {
  local port="/usr/ports/${1##/usr/ports/}"
  shift
  cheval "make -C ${port} ${*}"
}

########
#
# Port Dependency Tracking
#
########

# Translate port origin to package name
port2pkg() {
  while [ "${1}" ]
  do
    chmake "${1}" -V PKGNAME
    shift
  done
}

# Build dependencies (shallow, recursive, package)
port_bdeps() {
  while [ "${1}" ]
  do
    chmake "${1}" build-depends-list | sed -e 's#^/usr/ports/##'
    shift
  done
}
port_all_bdeps() {
  # rdeps for rdeps are rdeps, rdeps for bdeps are bdeps; thus:
  port_all_rdeps "${@}" | while read port
  do
    port_all_bdeps "${port}"
    port_all_rdeps "${port}"
  done | sort | uniq
}
pkg_bdeps() {
  port2pkg $(port_all_bdeps "${@}")
}

# Runtime dependencies (shallow, recursive, package)
port_rdeps() {
  while [ "${1}" ]
  do
    chmake "${1}" run-depends-list | sed -e 's#^/usr/ports/##'
    shift
  done
}
port_all_rdeps() {
  port_rdeps "${@}" | while read port
  do
    echo "${port}"
    port_all_rdeps "${port}"
  done | sort | uniq
}
pkg_rdeps() {
  port2pkg $(port_all_rdeps "${@}")
}

# All dependencies (shallow, recursive, package)
port_deps() {
  while [ "${1}" ]
  do
    chmake "${1}" all-depends-list | sed -e 's#^/usr/ports/##'
    shift
  done
}
port_all_deps() {
  port_deps "${@}" | while read port
  do
    echo "${port}"
    port_deps "${port}"
  done | sort | uniq
}
pkg_deps() {
  port2pkg $(port_all_deps "${@}")
}

########
#
# Port Configuration Handling
#
########

# Run make config on a list of ports
port_config() {
  while [ "${1}" ]
  do
    meh "port config ${1}"
    chmake "${1}" config < /dev/tty
    shift
  done
}

# Make config-conditional for a list of ports, and all dependencies
port_config_recursive() {
  while [ "${1}" ]
  do
    # Do not use config-recursive because it computes the depchain first;
    # instead, manually compute the depchain after each config to ensure the
    # proper depchain is followed. Use config-conditional to avoid dialog
    # if the config is already complete. Also use a cache to avoid re-config
    # and re-recurse on previously handled port branches.
    if echo "${_port_config_recursive_cache}" | grep -qv " ${1} "
    then
      meh "port config-recursive ${1}"
      chmake "${1}" config-conditional < /dev/tty
      port_config_recursive $(port_deps "${1}")
      _port_config_recursive_cache="${_port_config_recursive_cache} ${1} "
    fi
    shift
  done
}

# Remove saved config for a list of ports
port_rmconfig() {
  while [ "${1}" ]
  do
    meh "port rmconfig ${1}"
    chmake "${1}" rmconfig
    shift
  done
}

# Remove saved config for a list of ports and all dependencies
port_rmconfig_recursive() {
  meh "port rmconfig-recursive ${*}"
  port_rmconfig $(echo "${@}" $(port_all_deps "${@}") | sort | uniq)
}

# Obliterate saved configuration for all ports
port_rmconfig_all() {
  meh "port rmconfig-all"
  cheval "cd /var/db/ports; find . -name 'options' -delete; find . -type d | xargs rmdir 2>/dev/null"
}

# Restore port build options from cpio
port_load_config() {
  meh "port load-config"
  cheval "cd /var/db/ports; cpio -iv"
}

# Dump port build options to cpio
port_save_config() {
  meh "port save-config"
  cheval "cd /var/db/ports; find . -type d -o -type f -name options | cpio -ovHnewc"
}

########
#
# Port distfile handling
#
########

# Recursively retrieve distfiles
port_fetch_recursive() {
  while [ "${1}" ]
  do
    meh "fetch-recursive ${1}"
    chmake "${1}" fetch-recursive
  done
}

########
#
# Port building and packaging
#
########

# Copy in and install dependency packages
port_load_deps() {
  local port="${1}"
  for pkg in $(port2pkg $(port_all_deps "${port}"))
  do
    cp -f "${final_bdeps_dir}/${pkg}.tbz" "${bdeps_dir}" 2>/dev/null && meh "Loading dependent ${pkg}"
  done
  if ls "${bdeps_dir}"/*.tbz >/dev/null 2>&1
  then
    meh "Installing dependencies"
    cheval "cd ${chroot_bdeps_dir}; pkg_add -F *" || wtf "port_load_deps ${port} failed"
  fi
}

# Build and install a port
port_build() {
  local port="${1}"
  meh "Building ${port}"
  chmake "${port}" clean build install clean || wtf "port_build ${port} failed"
}

# Package a port
port_package() {
  local port="${1}"
  meh "Creating rdep package tree for ${port}"
  cheval "pkg_create -Rvb $(port2pkg "${port}")" || wtf "port_package ${port} failed"
}

# Package port build dependencies, unless they're already run deps
port_stash_bdeps() {
  meh "Stashing unsaved bdeps"
  # This doesn't work well, because there can be bdeps that aren't listed as bdeps (bison)
  #for pkg in $(pkg_bdeps "${1}")
  #do
  #  [ ! -f "${pkg_dir}/${pkg}.tbz" ] && cheval "cd ${chroot_bdeps_dir}; pkg_create -vb ${pkg}"
  #done
  #
  # Instead, just save everything that's not already in rdeps as bdeps
  for pkg in $(cheval pkg_info | awk '{print $1}')
  do
    if [ ! -f "${pkg_dir}/${pkg}.tbz" -a ! -f "${bdeps_dir}/${pkg}.tbz" ]
    then
      cheval "cd ${chroot_bdeps_dir}; pkg_create -vb ${pkg}" || wtf "port_stash_bdeps failed"
    fi
  done
}

# Copy generated packages out of tree
pkg_final() {
  meh "Moving created packages to repo"
  ls "${pkg_dir}"/*.tbz >/dev/null 2>&1 && mv -f "${pkg_dir}"/*.tbz "${final_pkg_dir}"
  ls "${bdeps_dir}"/*.tbz >/dev/null 2>&1 && mv -f "${bdeps_dir}"/*.tbz "${final_bdeps_dir}"
  # link everything into ${bdeps_dir} so we can find it easily later
  ( cd "${final_pkg_dir}"; find . -type f | cpio -plu --quiet "${final_bdeps_dir}" )
}

# Delete all installed packages (hope you saved them first)
pkg_delete_all() {
  meh "Clearing out installed packages"
  cheval "pkg_delete -f \*" || wtf "pkg_delete_all failed"
}

########
#
# All of the above?
#
########

# Execute a complete port build, using prebuilt packages to fulfill dependencies when available
# Be sure to chsetup and populate your config before running!
chport() {
  local port="${1}"
  port_load_deps "${port}"
  port_build "${port}"
  port_package "${port}"
  port_stash_bdeps
  pkg_final
  pkg_delete_all
}