build: store granular timestamps in packages
[openwrt/openwrt.git] / scripts / ipkg-build
1 #!/bin/sh
2
3 # ipkg-build -- construct a .ipk from a directory
4 # Carl Worth <cworth@east.isi.edu>
5 # based on a script by Steve Redler IV, steve@sr-tech.com 5-21-2001
6 # 2003-04-25 rea@sr.unh.edu
7 # Updated to work on Familiar Pre0.7rc1, with busybox tar.
8 # Note it Requires: binutils-ar (since the busybox ar can't create)
9 # For UID debugging it needs a better "find".
10 set -e
11
12 version=1.0
13 FIND="$(command -v find)"
14 FIND="${FIND:-$(command -v gfind)}"
15 TAR="${TAR:-$(command -v tar)}"
16 GZIP="$(command -v gzip)"
17
18 # try to use fixed source epoch
19 if [ -n "$PKG_SOURCE_DATE_EPOCH" ]; then
20 TIMESTAMP=$(date --date="@$PKG_SOURCE_DATE_EPOCH")
21 elif [ -n "$SOURCE_DATE_EPOCH" ]; then
22 TIMESTAMP=$(date --date="@$SOURCE_DATE_EPOCH")
23 else
24 TIMESTAMP=$(date)
25 fi
26
27 ipkg_extract_value() {
28 sed -e "s/^[^:]*:[[:space:]]*//"
29 }
30
31 required_field() {
32 field=$1
33
34 grep "^$field:" < $CONTROL/control | ipkg_extract_value
35 }
36
37 pkg_appears_sane() {
38 local pkg_dir=$1
39
40 local owd=$PWD
41 cd $pkg_dir
42
43 PKG_ERROR=0
44 pkg=`required_field Package`
45 version=`required_field Version | sed 's/Version://; s/^.://g;'`
46 arch=`required_field Architecture`
47
48 if echo $pkg | grep '[^a-zA-Z0-9_.+-]'; then
49 echo "*** Error: Package name $name contains illegal characters, (other than [a-z0-9.+-])" >&2
50 PKG_ERROR=1;
51 fi
52
53 if [ -f $CONTROL/conffiles ]; then
54 rm -f $CONTROL/conffiles.resolved
55
56 for cf in `$FIND $(sed -e "s!^/!$pkg_dir/!" $CONTROL/conffiles) -type f`; do
57 echo "${cf#$pkg_dir}" >> $CONTROL/conffiles.resolved
58 done
59
60 rm $CONTROL/conffiles
61 if [ -f $CONTROL/conffiles.resolved ]; then
62 mv $CONTROL/conffiles.resolved $CONTROL/conffiles
63 chmod 0644 $CONTROL/conffiles
64 fi
65 fi
66
67 cd $owd
68 return $PKG_ERROR
69 }
70
71 ###
72 # ipkg-build "main"
73 ###
74 file_modes=""
75 usage="Usage: $0 [-v] [-h] [-m] <pkg_directory> [<destination_directory>]"
76 while getopts "hvm:" opt; do
77 case $opt in
78 v ) echo $version
79 exit 0
80 ;;
81 h ) echo $usage >&2 ;;
82 m ) file_modes=$OPTARG ;;
83 \? ) echo $usage >&2
84 esac
85 done
86
87
88 shift $(($OPTIND - 1))
89
90 # continue on to process additional arguments
91
92 case $# in
93 1)
94 dest_dir=$PWD
95 ;;
96 2)
97 dest_dir=$2
98 if [ "$dest_dir" = "." -o "$dest_dir" = "./" ] ; then
99 dest_dir=$PWD
100 fi
101 ;;
102 *)
103 echo $usage >&2
104 exit 1
105 ;;
106 esac
107
108 pkg_dir=$1
109
110 if [ ! -d $pkg_dir ]; then
111 echo "*** Error: Directory $pkg_dir does not exist" >&2
112 exit 1
113 fi
114
115 # CONTROL is second so that it takes precedence
116 CONTROL=
117 [ -d $pkg_dir/CONTROL ] && CONTROL=CONTROL
118 if [ -z "$CONTROL" ]; then
119 echo "*** Error: Directory $pkg_dir has no CONTROL subdirectory." >&2
120 exit 1
121 fi
122
123 if ! pkg_appears_sane $pkg_dir; then
124 echo >&2
125 echo "ipkg-build: Please fix the above errors and try again." >&2
126 exit 1
127 fi
128
129 tmp_dir=$dest_dir/IPKG_BUILD.$$
130 mkdir $tmp_dir
131
132 echo $CONTROL > $tmp_dir/tarX
133 cd $pkg_dir
134 for file_mode in $file_modes; do
135 case $file_mode in
136 /*:*:*:*)
137 ;;
138 *)
139 echo "ERROR: file modes must use absolute path and contain user:group:mode"
140 echo "$file_mode"
141 exit 1
142 ;;
143 esac
144 path=$(echo "$file_mode" | cut -d ':' -f 1)
145 user_group=$(echo "$file_mode" | cut -d ':' -f 2-3)
146 mode=$(echo "$file_mode" | cut -d ':' -f 4)
147
148 chown "$user_group" "$pkg_dir/$path"
149 chmod "$mode" "$pkg_dir/$path"
150 done
151 $TAR -X $tmp_dir/tarX --format=gnu --sort=name -cpf - --mtime="$TIMESTAMP" . | $GZIP -n - > $tmp_dir/data.tar.gz
152
153 installed_size=`stat -c "%s" $tmp_dir/data.tar.gz`
154 sed -i -e "s/^Installed-Size: .*/Installed-Size: $installed_size/" \
155 $pkg_dir/$CONTROL/control
156
157 ( cd $pkg_dir/$CONTROL && $TAR --format=gnu --sort=name -cf - --mtime="$TIMESTAMP" . | $GZIP -n - > $tmp_dir/control.tar.gz )
158 rm $tmp_dir/tarX
159
160 echo "2.0" > $tmp_dir/debian-binary
161
162 pkg_file=$dest_dir/${pkg}_${version}_${arch}.ipk
163 rm -f $pkg_file
164 ( cd $tmp_dir && $TAR --format=gnu --sort=name -cf - --mtime="$TIMESTAMP" ./debian-binary ./data.tar.gz ./control.tar.gz | $GZIP -n - > $pkg_file )
165
166 rm $tmp_dir/debian-binary $tmp_dir/data.tar.gz $tmp_dir/control.tar.gz
167 rmdir $tmp_dir
168
169 echo "Packaged contents of $pkg_dir into $pkg_file"