build: ipkg-build use fakeroot with PKG_FILE_MODES
[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 "$SOURCE_DATE_EPOCH" ]; then
20 TIMESTAMP=$(date --date="@$SOURCE_DATE_EPOCH")
21
22 # look up date of last commit
23 elif [ -d "$TOPDIR/.git" ]; then
24 GIT="$(command -v git)"
25 TIMESTAMP=$(cd $TOPDIR; $GIT log -1 -s --format=%ci)
26 elif [ -d "$TOPDIR/.svn" ]; then
27 SVN="$(command -v svn)"
28 TIMESTAMP=$($SVN info "$TOPDIR" | sed -n "s/^Last Changed Date: \(.*\)/\1/p")
29 else
30 TIMESTAMP=$(date)
31 fi
32
33 ipkg_extract_value() {
34 sed -e "s/^[^:]*:[[:space:]]*//"
35 }
36
37 required_field() {
38 field=$1
39
40 grep "^$field:" < $CONTROL/control | ipkg_extract_value
41 }
42
43 pkg_appears_sane() {
44 local pkg_dir=$1
45
46 local owd=$PWD
47 cd $pkg_dir
48
49 PKG_ERROR=0
50 pkg=`required_field Package`
51 version=`required_field Version | sed 's/Version://; s/^.://g;'`
52 arch=`required_field Architecture`
53
54 if echo $pkg | grep '[^a-zA-Z0-9_.+-]'; then
55 echo "*** Error: Package name $name contains illegal characters, (other than [a-z0-9.+-])" >&2
56 PKG_ERROR=1;
57 fi
58
59 if [ -f $CONTROL/conffiles ]; then
60 rm -f $CONTROL/conffiles.resolved
61
62 for cf in `$FIND $(sed -e "s!^/!$pkg_dir/!" $CONTROL/conffiles) -type f`; do
63 echo "${cf#$pkg_dir}" >> $CONTROL/conffiles.resolved
64 done
65
66 rm $CONTROL/conffiles
67 if [ -f $CONTROL/conffiles.resolved ]; then
68 mv $CONTROL/conffiles.resolved $CONTROL/conffiles
69 chmod 0644 $CONTROL/conffiles
70 fi
71 fi
72
73 cd $owd
74 return $PKG_ERROR
75 }
76
77 ###
78 # ipkg-build "main"
79 ###
80 file_modes=""
81 usage="Usage: $0 [-v] [-h] [-m] <pkg_directory> [<destination_directory>]"
82 while getopts "hvm:" opt; do
83 case $opt in
84 v ) echo $version
85 exit 0
86 ;;
87 h ) echo $usage >&2 ;;
88 m ) file_modes=$OPTARG ;;
89 \? ) echo $usage >&2
90 esac
91 done
92
93
94 shift $(($OPTIND - 1))
95
96 # continue on to process additional arguments
97
98 case $# in
99 1)
100 dest_dir=$PWD
101 ;;
102 2)
103 dest_dir=$2
104 if [ "$dest_dir" = "." -o "$dest_dir" = "./" ] ; then
105 dest_dir=$PWD
106 fi
107 ;;
108 *)
109 echo $usage >&2
110 exit 1
111 ;;
112 esac
113
114 pkg_dir=$1
115
116 if [ ! -d $pkg_dir ]; then
117 echo "*** Error: Directory $pkg_dir does not exist" >&2
118 exit 1
119 fi
120
121 # CONTROL is second so that it takes precedence
122 CONTROL=
123 [ -d $pkg_dir/CONTROL ] && CONTROL=CONTROL
124 if [ -z "$CONTROL" ]; then
125 echo "*** Error: Directory $pkg_dir has no CONTROL subdirectory." >&2
126 exit 1
127 fi
128
129 if ! pkg_appears_sane $pkg_dir; then
130 echo >&2
131 echo "ipkg-build: Please fix the above errors and try again." >&2
132 exit 1
133 fi
134
135 tmp_dir=$dest_dir/IPKG_BUILD.$$
136 mkdir $tmp_dir
137
138 echo $CONTROL > $tmp_dir/tarX
139 cd $pkg_dir
140 for file_mode in $file_modes; do
141 case $file_mode in
142 /*:*:*:*)
143 ;;
144 *)
145 echo "ERROR: file modes must use absolute path and contain user:group:mode"
146 echo "$file_mode"
147 exit 1
148 ;;
149 esac
150 path=$(echo "$file_mode" | cut -d ':' -f 1)
151 user_group=$(echo "$file_mode" | cut -d ':' -f 2-3)
152 mode=$(echo "$file_mode" | cut -d ':' -f 4)
153
154 chown "$user_group" "$pkg_dir/$path"
155 chmod "$mode" "$pkg_dir/$path"
156 done
157 $TAR -X $tmp_dir/tarX --format=gnu --sort=name -cpf - --mtime="$TIMESTAMP" . | $GZIP -n - > $tmp_dir/data.tar.gz
158
159 installed_size=`stat -c "%s" $tmp_dir/data.tar.gz`
160 sed -i -e "s/^Installed-Size: .*/Installed-Size: $installed_size/" \
161 $pkg_dir/$CONTROL/control
162
163 ( cd $pkg_dir/$CONTROL && $TAR --format=gnu --sort=name -cf - --mtime="$TIMESTAMP" . | $GZIP -n - > $tmp_dir/control.tar.gz )
164 rm $tmp_dir/tarX
165
166 echo "2.0" > $tmp_dir/debian-binary
167
168 pkg_file=$dest_dir/${pkg}_${version}_${arch}.ipk
169 rm -f $pkg_file
170 ( cd $tmp_dir && $TAR --format=gnu --sort=name -cf - --mtime="$TIMESTAMP" ./debian-binary ./data.tar.gz ./control.tar.gz | $GZIP -n - > $pkg_file )
171
172 rm $tmp_dir/debian-binary $tmp_dir/data.tar.gz $tmp_dir/control.tar.gz
173 rmdir $tmp_dir
174
175 echo "Packaged contents of $pkg_dir into $pkg_file"