summaryrefslogtreecommitdiffstats
path: root/scripts/makepkg
blob: 2774084f42ed7611ca09aba4dbcc7e0b85dbe82f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#!/bin/bash

myver='2.0'
startdir=`pwd`

[ -f /etc/makepkg.conf ] && source /etc/makepkg.conf

strip_url() {
  echo $1 | sed 's|^.*://.*/||g'
}

msg() {
	echo $* >&2
}

if [ "$1" = "--help" -o "$1" = "-h" ]; then
  shift
  echo "makepkg version $myver"
  echo "usage: $0 [options] [build_script]"
	echo "options:"
	echo "  -c, --clean   Clean up work files after build"
	echo "  -i, --install Install package after successful build"
	echo "  -h, --help    This help"
  echo
  echo "  if build_script is not specified, makepkg will look for a PKGBUILD"
  echo "  file in the current directory."
  echo
  exit 0
fi

CLEANUP=0
INSTALL=0

if [ "$1" = "-c" -o "$1" = "--clean" ]; then
	shift
	CLEANUP=1
fi
if [ "$1" = "-i" -o "$1" = "--install" ]; then
	shift
	INSTALL=1
fi

unset pkgname pkgver pkgrel pkgdesc
unset depends conflicts backup source install build
umask 0022


# check for a download utility
if [ -x /usr/bin/wget ]; then
 	ftpagent="/usr/bin/wget --passive-ftp --tries=3 --waitretry=3"
elif [ -x /usr/bin/snarf ]; then
 	ftpagent="/usr/bin/snarf"
elif [ -x /usr/bin/lftpget -a "$proto" = "ftp" ]; then
 	ftpagent="/usr/bin/lftpget"
else
 	msg "==> ERROR:  You need an ftp client installed (snarf/lftp/wget) in /usr/bin"
 	exit 1
fi

BUILDSCRIPT="./PKGBUILD"
if [ "$1" != "" ]; then
  BUILDSCRIPT=$1
fi

if [ ! -f $BUILDSCRIPT ]; then
  msg "==> ERROR:  $BUILDSCRIPT does not exist."
  exit 1
fi

source $BUILDSCRIPT

# check for no-no's
if [ `echo $pkgver | grep '-'` ]; then
	msg "==> ERROR:  pkgver is not allowed to contain hyphens."
	exit 1
fi
if [ `echo $pkgrel | grep '-'` ]; then
	msg "==> ERROR:  pkgrel is not allowed to contain hyphens."
	exit 1
fi

if [ `type -p pacman` ]; then
	msg "==> Checking Dependencies..."
	missdep=`pacman -T ${depends[@]}`
	ret=$?
	if [ "$ret" != "0" ]; then
		if [ "$ret" = "127" ]; then
			msg "==> ERROR: Dependency Check Failed:"
			msg ""
			nl=0
			for dep in $missdep; do
				echo -ne "$dep " >&2
				if [ "$nl" = "1" ]; then
					nl=0
					echo -ne "\n" >&2
					continue
				fi
				nl=1
			done
			msg ""
		else
			msg "==> ERROR: pacman returned a fatal error."
		fi
		exit 1
	fi
else
	msg "==> WARNING: pacman was not found in PATH. skipping dependency checks."
fi

d=`date`
msg "==> Making package $pkgname  ($d)"

# extract source
msg "==> Acquiring/Extracting Sources..."
mkdir -p src
cd $startdir/src
for netfile in ${source[@]}; do
  file=`strip_url $netfile`
  if [ -f ../$file ]; then
    msg "==> Found $file in build dir"
    cp ../$file .
  elif [ -f /var/cache/pacman/src/$file ]; then
    msg "==> Using local copy of $file"
    cp /var/cache/pacman/src/$file .
  else
    proto=`echo $netfile | sed 's|://.*||'`
    if [ "$proto" != "ftp" -a "$proto" != "http" ]; then
      msg "==> ERROR:  $netfile was not found in the build directory and is not a proper URL."
      msg "==> Aborting..."
      exit 1
    fi
    msg "==> Downloading $file"
    $ftpagent $netfile 2>&1
    if [ ! -f $file ]; then
      msg "==> ERROR: Failed to download $file"
      msg "==> Aborting..."
      exit 1
    fi
    mkdir -p /var/cache/pacman/src && cp $file /var/cache/pacman/src
  fi
  case $file in
	    *.tar.gz|*.tar.Z|*.tgz)
    cmd="tar --use-compress-program=gzip -xf $file" ;;
	    *.tar.bz2)
    cmd="tar --use-compress-program=bzip2 -xf $file" ;;
		  *.tar)
	  cmd="tar -xf $file" ;;
	    *.zip)
    cmd="unzip -qq $file" ;;
	    *)
    cmd="cp ../$file ." ;;
	esac
	msg "==> $cmd"
	$cmd
done

# check for existing pkg directory
if [ -d $startdir/pkg ]; then
	msg "==> Removing existing pkg directory..."
	rm -rf $startdir/pkg
fi
mkdir -p $startdir/pkg

# build
msg "==> Building Package..."
build 2>&1
if [ $? -gt 0 ]; then
  msg "==> Build Failed.  Aborting..."
  exit 2
fi

# get some package meta info
builddate=`date -u "+%a %b %d %k:%M:%S %Y"`
if [ "$PACKAGER" != "" ]; then
	packager="$PACKAGER"
else
	packager="Arch Linux (http://www.archlinux.org)"
fi
size=`du -cb $startdir/pkg | tail -1 | awk '{print $1}'`

# write the .PKGINFO file
msg "==> Generating .PKGINFO file..."
cd $startdir/pkg
echo "# Generated by makepkg $myver" >.PKGINFO
echo -n "# " >>.PKGINFO
date >>.PKGINFO
echo "pkgname = $pkgname" >>.PKGINFO
echo "pkgver = $pkgver-$pkgrel" >>.PKGINFO
echo "pkgdesc = $pkgdesc" >>.PKGINFO
echo "builddate = $builddate" >>.PKGINFO
echo "packager = $packager" >>.PKGINFO
echo "size = $size" >>.PKGINFO

for depend in "${depends[@]}"; do
  echo "depend = $depend" >>.PKGINFO
done
for conflict in "${conflicts[@]}"; do
  echo "conflict = $conflict" >>.PKGINFO
done
for bakfile in "${backup[@]}"; do
  echo "backup = $bakfile" >>.PKGINFO
done

# check for an install script
if [ "$install" != "" ]; then
	msg "==> Copying install script..."
	cp $startdir/$install $startdir/pkg/._install
fi

# remove info/doc files
cd $startdir
rm -rf pkg/usr/info pkg/usr/share/info
rm -rf pkg/usr/doc pkg/usr/share/doc

# move /usr/share/man files to /usr/man
if [ -d pkg/usr/share/man ]; then
	mkdir -p pkg/usr/man 
	cp -a pkg/usr/share/man/* pkg/usr/man/
	rm -rf pkg/usr/share/man
fi

# strip binaries
cd $startdir
msg "==> Stripping debugging symbols from libraries..."
find pkg/{,usr,usr/local}/lib -type f -exec /usr/bin/strip --strip-debug '{}' ';' 2>&1
msg "==> Stripping symbols from binaries..."
find pkg/{,usr,usr/local}/{bin,sbin} -type f -exec /usr/bin/strip '{}' ';' 2>&1

# tar it up
msg "==> Compressing package..."
cd $startdir/pkg
if [ -f $startdir/pkg/._install ]; then
	tar czvf $startdir/$pkgname-$pkgver-$pkgrel.pkg.tar.gz .PKGINFO ._install * >../filelist
else
	tar czvf $startdir/$pkgname-$pkgver-$pkgrel.pkg.tar.gz .PKGINFO * >../filelist
fi

cd $startdir
if [ "$CLEANUP" = "1" ]; then
	msg "==> Cleaning up"
	rm -rf src pkg
fi

d=`date`
msg "==> Finished making $pkgname  ($d)"

if [ "$INSTALL" = "1" ]; then
	msg "==> Running pacman --upgrade"
	pacman --upgrade $pkgname-$pkgver-$pkgrel.pkg.tar.gz
fi

exit 0