newapkbuild.in 6.88 KB
Newer Older
1
#!/bin/sh
2
#
3
# newapkbuild - generate a new APKBUILD
4 5 6 7 8
# Copyright (c) 2009 Natanael Copa <natanael.copa@gmail.com>
#
# Distributed under GPL-2
#

9
program_version=@VERSION@
10 11
datadir=@datadir@

12 13 14 15 16
if ! [ -f "$datadir/functions.sh" ]; then
	echo "$datadir/functions.sh: not found" >&2
	exit 1
fi
. "$datadir/functions.sh"
17 18


19 20
is_url() {
	case "$1" in
21
	http://*|https://*|ftp://*) return 0;;
22
	esac
23
	return 1
24 25
}

26 27 28 29 30 31 32
is_github_url() {
	case $1 in
	https://github.com/*/*/archive/*.tar.gz) return 0;;
	esac
	return 1
}

33
# Build sections
34 35
build_make() {
	cat >>APKBUILD<<__EOF__
36
	make
37 38 39
__EOF__
}

40
build_autotools() {
41
	cat >>APKBUILD<<__EOF__
42 43 44 45
	./configure \\
		--build=\$CBUILD \\
		--host=\$CHOST \\
		--prefix=/usr \\
46 47
		--sysconfdir=/etc \\
		--mandir=/usr/share/man \\
48 49
		--localstatedir=/var
	make
50 51 52
__EOF__
}

53 54 55
build_cmake() {
# References:
# http://www.cmake.org/Wiki/CMake_Useful_Variables
56 57
# http://www.vtk.org/Wiki/CMake_Cross_Compiling
# This is incomplete: CMAKE_{HOST_,}SYSTEM_PROCESSOR needs to be set,
58 59 60 61
# and likewise CMAKE_FIND_ROOT_PATH and a few other details.

	sed -i -e 's/^\(makedepends="\)/\1cmake /' APKBUILD
	cat >>APKBUILD<<__EOF__
62
	if [ "\$CBUILD" != "\$CHOST" ]; then
63 64 65 66 67 68 69 70
		CMAKE_CROSSOPTS="-DCMAKE_SYSTEM_NAME=Linux -DCMAKE_HOST_SYSTEM_NAME=Linux"
	fi
	cmake \\
		-DCMAKE_INSTALL_PREFIX=/usr \\
		-DCMAKE_INSTALL_LIBDIR=lib \\
		-DBUILD_SHARED_LIBS=True \\
		-DCMAKE_BUILD_TYPE=Release \\
		-DCMAKE_CXX_FLAGS="\$CXXFLAGS" \\
71
		-DCMAKE_C_FLAGS="\$CFLAGS" \\
72
		\${CMAKE_CROSSOPTS}
73
	make
74 75 76
__EOF__
}

77
build_perl() {
78
	cat >>APKBUILD<<__EOF__
79 80
	PERL_MM_USE_DEFAULT=1 perl Makefile.PL INSTALLDIRS=vendor
	make
81 82 83
__EOF__
}

84
build_python() {
85
	cat >>APKBUILD<<__EOF__
86
	python setup.py build
87 88 89
__EOF__
}

90
# Package sections
91
package_make() {
92
	cat >>APKBUILD<<__EOF__
93
	make DESTDIR="\$pkgdir" install
94 95 96
__EOF__
}

97 98 99 100
package_autotools() {
	package_make
}

101
package_perl() {
102
	cat >>APKBUILD<<__EOF__
103
	make DESTDIR="\$pkgdir" install
104 105 106 107
	find "\$pkgdir" \\( -name perllocal.pod -o -name .packlist \\) -delete
__EOF__
}

108 109
package_python() {
	cat >>APKBUILD<<__EOF__
110
	python setup.py install --prefix=/usr --root="\$pkgdir"
111 112 113 114
__EOF__
}

# Create new aport from templates
115
newaport() {
116
	local newname="${1##*/}"
117 118
	local pn=${newname%-[0-9]*}
	local pv
119 120 121
	local source=
	is_url "$1" && source="$1"

122 123 124 125 126 127 128 129 130
	if is_github_url $source; then
		if [ -z "$pkgname" ]; then
			pkgname=${source%/archive/*}
			pkgname=${pkgname##*/}
		fi
		pv=${newname%.t*} #strip .tar.gz .tgz .tar.bz2 etc
		pv=${pv#*[a-z]}
		source="$pkgname-$pv.tar.gz::$source"
	elif [ "$pn" != "$newname" ]; then
131
		pv=${newname#$pn-}
132
		pv=${pv%.t*} #strip .tar.gz .tgz .tar.bz2 etc
133
	fi
134 135 136 137 138
	if [ -z "$pkgname" ]; then
		pkgname=$pn
	fi
	if [ -e "$pkgname"/APKBUILD ] && [ -z "$force" ]; then
		error "$pkgname/APKBUILD already exist"
139 140
		return 1
	fi
141 142
	mkdir -p "$pkgname"
	cd "$pkgname"
Natanael Copa's avatar
Natanael Copa committed
143

144 145 146 147
	if [ -z "$source" ] && [ -n "$sourceforge" ]; then
		source="http://downloads.sourceforge.net/$pn/$pn-$pv.tar.gz"
	fi

148
	if [ -z "$depends" ] &&[ "$buildtype" = "python" ]; then
149 150 151
		depends="python"
	fi

152
	if [ -z "$makedepends" ] &&[ "$buildtype" = "python" ]; then
153 154
		makedepends="python-dev"
	else
155
		makedepends="\$depends_dev"
156 157 158
	fi

	# Replace pkgver in $source
159
	if [ -n "$source" ]; then
160
		source=$(echo "$source" | sed "s/$pv/\$pkgver/g")
161
	fi
Natanael Copa's avatar
Natanael Copa committed
162

163
	# Copy init.d scripts if requested
164
	if [ -n "$cpinitd" ]; then
165 166 167 168
		cp "$datadir"/sample.initd $pkgname.initd
		cp "$datadir"/sample.confd $pkgname.confd
		cp "$datadir"/sample.pre-install $pkgname.pre-install
		cp "$datadir"/sample.post-install $pkgname.post-install
Natanael Copa's avatar
Natanael Copa committed
169 170
		install="\$pkgname.pre-install \$pkgname.post-install"
		source="$source
171 172
	$pkgname.initd
	$pkgname.confd
Natanael Copa's avatar
Natanael Copa committed
173 174 175
	"
	fi

176
	# Generate header with standard variables
Natanael Copa's avatar
Natanael Copa committed
177 178 179
	cat >APKBUILD<<__EOF__
# Contributor:${PACKAGER:+" "}${PACKAGER}
# Maintainer:${MAINTAINER:+" "}${MAINTAINER}
180
pkgname=$pkgname
Natanael Copa's avatar
Natanael Copa committed
181 182 183 184 185 186
pkgver=$pv
pkgrel=0
pkgdesc="$pkgdesc"
url="$url"
arch="all"
license="$license"
187 188 189
depends="$depends"
depends_dev=""
makedepends="$makedepends"
Natanael Copa's avatar
Natanael Copa committed
190 191 192 193 194 195
install="$install"
subpackages="\$pkgname-dev \$pkgname-doc"
source="$source"
__EOF__

	abuild -f fetch unpack
196
	# Figure out the builddir
Natanael Copa's avatar
Natanael Copa committed
197 198 199
	for i in src/*; do
		if [ -d "$i" ]; then
			sdir=$i
200
			builddir=$(echo ${i#*/} | sed "s/$pv/\$pkgver/g")
Natanael Copa's avatar
Natanael Copa committed
201 202
		fi
	done
203
	printf 'builddir="$srcdir/%s"\n\n' "$builddir" >> APKBUILD
Natanael Copa's avatar
Natanael Copa committed
204

205 206
	# Subpackage -dev is usually required only for C/C++. Since depends_dev
	# confuses a lot people, remove it if there's no .h or .hpp file.
207
	find "$sdir" -name "*.h" -o -name "*.hpp" -maxdepth 3 2>/dev/null \
208 209 210
		| head -n 1 | grep -q ".*" \
		|| sed -i -e '/^depends_dev=.*/d' -e 's/\$depends_dev\s*//' APKBUILD

211
	# Check if its autotools
212 213 214
	if [ -z "$buildtype" ]; then
		if [ -x "$sdir"/configure ]; then
			buildtype="autotools"
215
		elif [ -r "$sdir"/Makefile.PL ] || [ "${pn#perl-}" != "$pn" ]; then
216 217 218
			buildtype="perl"
		elif [ -r "$sdir"/waf ]; then
			buildtype="waf"
219
		elif [ -d "$sdir"/cmake ] || [ -r "$sdir/CMakeLists.txt" ]; then
220 221 222
			buildtype="cmake"
		elif [ -r "$sdir"/Makefile ]; then
			buildtype="make"
223 224
		elif [ -r "$sdir"/setup.py ]; then
			buildtype="python"
225 226 227
		fi
	fi

228
	# Create build() function
Natanael Copa's avatar
Natanael Copa committed
229 230
	cat >>APKBUILD<<__EOF__
build() {
231
	cd "\$builddir"
Natanael Copa's avatar
Natanael Copa committed
232
__EOF__
233 234

	case "$buildtype" in
235 236
	make)
		build_make;;
237 238
	cmake)
		build_cmake;;
239
	autotools)
240
		build_autotools;;
241
	perl)
242
		build_perl;;
243
	python)
244
		build_python;;
245 246
	esac

Natanael Copa's avatar
Natanael Copa committed
247 248 249 250 251
	cat >>APKBUILD<<__EOF__
}

__EOF__

252
	# Create package() function
Natanael Copa's avatar
Natanael Copa committed
253 254
	cat >>APKBUILD<<__EOF__
package() {
255
	cd "\$builddir"
Natanael Copa's avatar
Natanael Copa committed
256
__EOF__
257 258

	case "$buildtype" in
259 260
	make)
		package_make;;
261 262 263 264 265 266
	autotools)
		package_autotools;;
	perl)
		package_perl;;
	python)
		package_python;;
267 268
	esac

269 270 271 272
	if [ -n "$cpinitd" ]; then
		cat >>APKBUILD<<__EOF__

	install -m755 -D "\$srcdir"/\$pkgname.initd \\
273
		"\$pkgdir"/etc/init.d/\$pkgname
274
	install -m644 -D "\$srcdir"/\$pkgname.confd \\
275
		"\$pkgdir"/etc/conf.d/\$pkgname
276 277
__EOF__
	fi
Natanael Copa's avatar
Natanael Copa committed
278 279 280 281
	cat >>APKBUILD<<__EOF__
}

__EOF__
282
	abuild -f checksum
283 284 285
}

usage() {
286 287
	cat >&2 <<-__EOF__
		$program $program_version - generate a new APKBUILD
A. Wilcox's avatar
A. Wilcox committed
288 289
		Usage: $program [-n NAME] [-d DESC] [-l LICENSE] [-u URL]
		       [-aCpy] [-s] [-cfh]
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
		       PKGNAME[-PKGVER]|SRCURL
		Options:
		  -n  Set package name to NAME
		  -d  Set package description (pkgdesc) to DESC
		  -l  Set package license to LICENSE
		  -u  Set package URL
		  -a  Create autotools (use ./configure ...)
		  -C  Create CMake pakckage (Assume cmake/ is there)
		  -p  Create perl package (Assume Makefile.PL is there)
		  -y  Create python package (Assume setup.py is there)
		  -s  Use sourceforge source URL
		  -c  Copy a sample init.d, conf.d, and install script to new directory
		  -f  Force even if directory already exist
		  -h  Show this help

	__EOF__
306 307
}

308
while getopts "acd:fhl:n:pyu:s" opt; do
309
	case $opt in
310
		'a') buildtype="autotools";;
311
		'c') cpinitd=1;;
312
		'C') buildtype="cmake";;
Natanael Copa's avatar
Natanael Copa committed
313
		'd') pkgdesc="$OPTARG";;
314
		'f') force=1;;
315
		'h') usage; exit;;
Natanael Copa's avatar
Natanael Copa committed
316
		'l') license="$OPTARG";;
317
		'n') pkgname="$OPTARG";;
318
		'p') buildtype="perl";;
319
		'y') buildtype="python";;
Natanael Copa's avatar
Natanael Copa committed
320
		'u') url="$OPTARG";;
321
		's') sourceforge=1;;
322 323 324 325
	esac
done
shift $(( $OPTIND - 1 ))

326
while [ $# -gt 0 ]; do
327 328 329
	newaport $1 || exit 1
	shift
done