1#!/bin/sh
2# $FreeBSD$
3#
4# Usage: cd /usr/src/contrib/jemalloc
5#        ./FREEBSD-upgrade <command> [args]
6#
7# At least the following ports are required when importing jemalloc:
8# - devel/autoconf
9# - devel/git
10# - devel/gmake
11# - textproc/docbook-xsl
12# - textproc/libxslt
13#
14# The normal workflow for importing a new release is:
15#
16#   cd /usr/src/contrib/jemalloc
17#
18# Merge local changes that were made since the previous import:
19#
20#   ./FREEBSD-upgrade merge-changes
21#   ./FREEBSD-upgrade rediff
22#
23# Extract latest jemalloc release.
24#
25#   ./FREEBSD-upgrade extract
26#
27# Fix patch conflicts as necessary, then regenerate diffs to update line
28# offsets:
29#
30#   ./FREEBSD-upgrade rediff
31#   ./FREEBSD-upgrade extract
32#
33# Do multiple buildworld/installworld rounds.  If problems arise and patches
34# are needed, edit the code in ${work} as necessary, then:
35#
36#   ./FREEBSD-upgrade rediff
37#   ./FREEBSD-upgrade extract
38#
39# The rediff/extract order is important because rediff saves the local
40# changes, then extract blows away the work tree and re-creates it with the
41# diffs applied.
42#
43# Finally, to clean up:
44#
45#  ./FREEBSD-upgrade clean
46
47set -e
48
49if [ ! -x "FREEBSD-upgrade" ] ; then
50  echo "Run from within src/contrib/jemalloc/" >&2
51  exit 1
52fi
53
54src=`pwd`
55workname="jemalloc.git"
56work="${src}/../${workname}" # merge-changes expects ${workname} in "..".
57changes="${src}/FREEBSD-changes"
58
59do_extract() {
60  local rev=$1
61  # Clone.
62  rm -rf ${work}
63  git clone git://canonware.com/jemalloc.git ${work}
64  (
65    cd ${work}
66    if [ "x${rev}" != "x" ] ; then
67      # Use optional rev argument to check out a revision other than HEAD on
68      # master.
69      git checkout ${rev}
70    fi
71    # Apply diffs before generating files.
72    patch -p1 < "${src}/FREEBSD-diffs"
73    find . -name '*.orig' -delete
74    # Generate various files.
75    ./autogen.sh --enable-cc-silence --enable-dss --enable-xmalloc \
76      --enable-utrace --with-xslroot=/usr/local/share/xsl/docbook \
77      --with-private-namespace=__jemalloc_
78    gmake dist
79  )
80}
81
82do_diff() {
83  (cd ${work}; git add -A; git diff --cached) > FREEBSD-diffs
84}
85
86command=$1
87shift
88case "${command}" in
89  merge-changes) # Merge local changes that were made since the previous import.
90    rev=`cat VERSION |tr 'g' ' ' |awk '{print $2}'`
91    # Extract code corresponding to most recent import.
92    do_extract ${rev}
93    # Compute local differences to the upstream+patches and apply them.
94    (
95      cd ..
96      diff -ru -X ${src}/FREEBSD-Xlist ${workname} jemalloc > ${changes} || true
97    )
98    (
99      cd ${work}
100      patch -p1 < ${changes}
101      find . -name '*.orig' -delete
102    )
103    # Update diff.
104    do_diff
105    ;;
106  extract) # Extract upstream sources, apply patches, copy to contrib/jemalloc.
107    rev=$1
108    do_extract ${rev}
109    # Delete existing files so that cruft doesn't silently remain.
110    rm -rf ChangeLog COPYING VERSION doc include src
111    # Copy files over.
112    tar cf - -C ${work} -X FREEBSD-Xlist . |tar xvf -
113    ;;
114  rediff) # Regenerate diffs based on working tree.
115    do_diff
116    ;;
117  clean) # Remove working tree and temporary files.
118    rm -rf ${work} ${changes}
119    ;;
120  *)
121    echo "Unsupported command: \"${command}\"" >&2
122    exit 1
123    ;;
124esac
125