newapkbuild.in 8.94 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
# and likewise CMAKE_FIND_ROOT_PATH and a few other details.

	cat >>APKBUILD<<__EOF__
61
	if [ "\$CBUILD" != "\$CHOST" ]; then
62 63 64 65 66 67 68 69
		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" \\
70
		-DCMAKE_C_FLAGS="\$CFLAGS" \\
71
		\${CMAKE_CROSSOPTS}
72
	make
73 74 75
__EOF__
}

76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
build_meson() {
# References:
# http://mesonbuild.com/Reference-manual.html
# http://mesonbuild.com/Cross-compilation.html
# TODO For cross compilation a cross_file needs to be created.

	cat >>APKBUILD<<__EOF__
	meson \\
		--prefix=/usr \\
		--sysconfdir=/etc \\
		--mandir=/usr/share/man \\
		--localstatedir=/var \\
		--buildtype=release \\
		. output
	ninja -C output
__EOF__
}

94
build_perl() {
95
	cat >>APKBUILD<<__EOF__
96 97
	PERL_MM_USE_DEFAULT=1 perl Makefile.PL INSTALLDIRS=vendor
	make
98 99 100
__EOF__
}

101
build_python() {
102
	cat >>APKBUILD<<__EOF__
103
	python3 setup.py build
104 105 106
__EOF__
}

107 108 109 110 111 112 113
build_empty() {
	cat >>APKBUILD<<__EOF__
	# Replace with proper build command(s)
	:
__EOF__
}

114 115 116 117 118 119 120 121
check_make() {
	cat >>APKBUILD<<__EOF__
	make check
__EOF__
}

check_python() {
	cat >>APKBUILD<<__EOF__
122
	python3 setup.py test
123 124 125
__EOF__
}

126 127 128 129 130 131 132
check_empty() {
	cat >>APKBUILD<<__EOF__
	# Replace with proper check command(s)
	:
__EOF__
}

133
# Package sections
134
package_make() {
135
	cat >>APKBUILD<<__EOF__
136
	make DESTDIR="\$pkgdir" install
137 138 139
__EOF__
}

140 141 142 143
package_autotools() {
	package_make
}

144 145
package_meson() {
	cat >>APKBUILD<<__EOF__
146
	DESTDIR="\$pkgdir" ninja -C output install
147 148 149
__EOF__
}

150
package_perl() {
151
	cat >>APKBUILD<<__EOF__
152
	make DESTDIR="\$pkgdir" install
153 154 155 156
	find "\$pkgdir" \\( -name perllocal.pod -o -name .packlist \\) -delete
__EOF__
}

157 158
package_python() {
	cat >>APKBUILD<<__EOF__
159
	python3 setup.py install --prefix=/usr --root="\$pkgdir"
160 161 162
__EOF__
}

163 164 165 166 167 168 169
package_empty() {
	cat >>APKBUILD<<__EOF__
	# Replace with proper package command(s)
	:
__EOF__
}

170
# Create new aport from templates
171
newaport() {
172
	local newname="${1##*/}"
173 174
	local pn=${newname%-[0-9]*}
	local pv
175 176 177
	local source=
	is_url "$1" && source="$1"

178 179 180 181 182 183 184 185 186
	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
187
		pv=${newname#$pn-}
188
		pv=${pv%.t*} #strip .tar.gz .tgz .tar.bz2 etc
189
	fi
190 191 192 193
	if [ -z "$pkgname" ]; then
		pkgname=$pn
	fi
	if [ -e "$pkgname"/APKBUILD ] && [ -z "$force" ]; then
A. Wilcox's avatar
A. Wilcox committed
194
		error "$pkgname/APKBUILD already exists"
195 196
		return 1
	fi
197 198
	mkdir -p "$pkgname"
	cd "$pkgname"
Natanael Copa's avatar
Natanael Copa committed
199

200
	if [ -z "$source" ] && [ -n "$sourceforge" ]; then
201
		source="https://downloads.sourceforge.net/$pn/$pn-$pv.tar.gz"
202 203
	fi

204
	if [ -z "$depends" ] &&[ "$buildtype" = "python" ]; then
205
		depends="python3"
206 207
	fi

208
	case "$buildtype" in
209
	python) makedepends="python3-dev";;
210 211 212 213
	cmake)  makedepends="cmake";;
	meson)  makedepends="meson";;
	*)      makedepends="\$depends_dev";;
	esac
214 215

	# Replace pkgver in $source
216
	if [ -n "$source" ]; then
217
		source=$(echo "$source" | sed "s/$pv/\$pkgver/g")
218
	fi
Natanael Copa's avatar
Natanael Copa committed
219

220
	# Copy init.d scripts if requested
221
	if [ -n "$cpinitd" ]; then
222 223 224 225
		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
226 227
		install="\$pkgname.pre-install \$pkgname.post-install"
		source="$source
228 229
	$pkgname.initd
	$pkgname.confd
Natanael Copa's avatar
Natanael Copa committed
230 231 232
	"
	fi

233
	# Generate header with standard variables
Natanael Copa's avatar
Natanael Copa committed
234 235 236
	cat >APKBUILD<<__EOF__
# Contributor:${PACKAGER:+" "}${PACKAGER}
# Maintainer:${MAINTAINER:+" "}${MAINTAINER}
237
pkgname=$pkgname
Natanael Copa's avatar
Natanael Copa committed
238 239 240 241 242 243
pkgver=$pv
pkgrel=0
pkgdesc="$pkgdesc"
url="$url"
arch="all"
license="$license"
244 245 246
depends="$depends"
depends_dev=""
makedepends="$makedepends"
Natanael Copa's avatar
Natanael Copa committed
247 248 249 250 251
install="$install"
subpackages="\$pkgname-dev \$pkgname-doc"
source="$source"
__EOF__

252
	abuild -f fetch checksum unpack
253
	# Figure out the builddir
Natanael Copa's avatar
Natanael Copa committed
254 255 256
	for i in src/*; do
		if [ -d "$i" ]; then
			sdir=$i
257
			builddir=$(echo ${i#*/} | sed "s/$pv/\$pkgver/g")
Natanael Copa's avatar
Natanael Copa committed
258 259
		fi
	done
260
	printf 'builddir="$srcdir/%s"\n\n' "$builddir" >> APKBUILD
Natanael Copa's avatar
Natanael Copa committed
261

262 263
	# 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.
264
	find "$sdir" -name "*.h" -o -name "*.hpp" -maxdepth 3 2>/dev/null \
265 266 267
		| head -n 1 | grep -q ".*" \
		|| sed -i -e '/^depends_dev=.*/d' -e 's/\$depends_dev\s*//' APKBUILD

268
	# Try to autodetect the buildtype
269 270 271
	if [ -z "$buildtype" ]; then
		if [ -x "$sdir"/configure ]; then
			buildtype="autotools"
272
		elif [ -r "$sdir"/Makefile.PL ] || [ "${pn#perl-}" != "$pn" ]; then
273 274 275
			buildtype="perl"
		elif [ -r "$sdir"/waf ]; then
			buildtype="waf"
276 277
		elif [ -r "$sdir"/meson.build ]; then
			buildtype="meson"
278
		elif [ -d "$sdir"/cmake ] || [ -r "$sdir/CMakeLists.txt" ]; then
279 280 281
			buildtype="cmake"
		elif [ -r "$sdir"/Makefile ]; then
			buildtype="make"
282 283
		elif [ -r "$sdir"/setup.py ]; then
			buildtype="python"
284 285 286
		fi
	fi

287
	# Create build() function
Natanael Copa's avatar
Natanael Copa committed
288 289 290
	cat >>APKBUILD<<__EOF__
build() {
__EOF__
291 292

	case "$buildtype" in
293 294
	make)
		build_make;;
295 296
	cmake)
		build_cmake;;
297 298
	meson)
		build_meson;;
299
	autotools)
300
		build_autotools;;
301
	perl)
302
		build_perl;;
303
	python)
304
		build_python;;
305 306
	*)
		build_empty;;
307 308
	esac

Natanael Copa's avatar
Natanael Copa committed
309 310 311
	cat >>APKBUILD<<__EOF__
}

312 313 314 315 316 317 318 319 320 321 322 323
__EOF__

	# Create check() function
	cat >>APKBUILD<<__EOF__
check() {
__EOF__

	case "$buildtype" in
	make|cmake|autotools|perl)
		check_make;;
	python)
		check_python;;
324 325
	*)
		check_empty;;
326 327 328 329 330
	esac

	cat >>APKBUILD<<__EOF__
}

Natanael Copa's avatar
Natanael Copa committed
331 332
__EOF__

333
	# Create package() function
Natanael Copa's avatar
Natanael Copa committed
334 335 336
	cat >>APKBUILD<<__EOF__
package() {
__EOF__
337 338

	case "$buildtype" in
339
	make|cmake)
340
		package_make;;
341 342
	autotools)
		package_autotools;;
343 344
	meson)
		package_meson;;
345 346 347 348
	perl)
		package_perl;;
	python)
		package_python;;
349 350
	*)
		package_empty;;
351 352
	esac

353 354 355 356
	if [ -n "$cpinitd" ]; then
		cat >>APKBUILD<<__EOF__

	install -m755 -D "\$srcdir"/\$pkgname.initd \\
357
		"\$pkgdir"/etc/init.d/\$pkgname
358
	install -m644 -D "\$srcdir"/\$pkgname.confd \\
359
		"\$pkgdir"/etc/conf.d/\$pkgname
360 361
__EOF__
	fi
Natanael Copa's avatar
Natanael Copa committed
362 363 364 365
	cat >>APKBUILD<<__EOF__
}

__EOF__
366 367 368

	# regenerate checksum so they end last in APKBUILD
	abuild -f checksum
369 370 371
}

usage() {
372 373
	cat >&2 <<-__EOF__
		$program $program_version - generate a new APKBUILD
374 375 376
		Usage: $program [-n PKGNAME] [-d PKGDESC] [-l LICENSE] [-u URL]
		       [-a | -C | -m | -p | -y] [-s] [-c] [-f] [-h]
		       PKGNAME[-PKGVER] | SRCURL
377
		Options:
378 379 380 381
		  -n  Set package name to PKGNAME (only use with SRCURL)
		  -d  Set package description to PKGDESC
		  -l  Set package license to LICENSE, use identifiers from:
		      <https://spdx.org/licenses/>
382
		  -u  Set package URL
383
		  -a  Create autotools package (use ./configure ...)
Oliver Smith's avatar
Oliver Smith committed
384
		  -C  Create CMake package (Assume cmake/ is there)
385
		  -m  Create meson package (Assume meson.build is there)
386 387 388
		  -p  Create perl package (Assume Makefile.PL is there)
		  -y  Create python package (Assume setup.py is there)
		  -s  Use sourceforge source URL
389 390
		  -c  Copy a sample init.d, conf.d, and install script
		  -f  Force even if directory already exists
391 392
		  -h  Show this help
	__EOF__
393 394
}

395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
set_buildtype() {
	if [ -n "$buildtype" ]; then
		error "More than one buildtype flag specified ($buildtype and $1)"
		exit 1
	fi
	buildtype="$1"
}

check_arguments() {
	if [ $# -eq 0 ]; then
		error "Missing required argument: PKGNAME[-PKGVER] | SRCURL"
		exit 1
	fi
	if [ $# -gt 1 ]; then
		shift
		error "Unrecognized arguments: $*"
		exit 1
	fi
	if ! is_url "$1" && [ -n "$pkgname" ]; then
		error "-n is only allowed when using SRCURL as last argument"
		exit 1
	fi
	if is_url "$1" && [ -n "$sourceforge" ]; then
		error "-s is only allowed when using PKGNAME as last argument"
		exit 1
	fi
}

423
while getopts "acCmd:fhl:n:pyu:s" opt; do
424
	case $opt in
425
		'a') set_buildtype "autotools";;
426
		'c') cpinitd=1;;
427 428
		'C') set_buildtype "cmake";;
		'm') set_buildtype "meson";;
Natanael Copa's avatar
Natanael Copa committed
429
		'd') pkgdesc="$OPTARG";;
430
		'f') force=1;;
431
		'h') usage; exit;;
Natanael Copa's avatar
Natanael Copa committed
432
		'l') license="$OPTARG";;
433
		'n') pkgname="$OPTARG";;
434 435
		'p') set_buildtype "perl";;
		'y') set_buildtype "python";;
Natanael Copa's avatar
Natanael Copa committed
436
		'u') url="$OPTARG";;
437
		's') sourceforge=1;;
438 439 440 441
	esac
done
shift $(( $OPTIND - 1 ))

442 443
check_arguments "$@"
newaport $1