newapkbuild.in 6.83 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 62 63 64 65 66 67 68 69 70 71
# and likewise CMAKE_FIND_ROOT_PATH and a few other details.

	sed -i -e 's/^\(makedepends="\)/\1cmake /' APKBUILD
	cat >>APKBUILD<<__EOF__
	if [ "$CBUILD" != "$CHOST" ]
	then
		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" \\
72
		-DCMAKE_C_FLAGS="\$CFLAGS" \\
73 74
		${CMAKE_CROSSOPTS}
	make
75 76 77
__EOF__
}

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

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

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

98 99 100 101
package_autotools() {
	package_make
}

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

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

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

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

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

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

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

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

164
	# Copy init.d scripts if requested
165
	if [ -n "$cpinitd" ]; then
166 167 168 169
		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
170 171
		install="\$pkgname.pre-install \$pkgname.post-install"
		source="$source
172 173
	$pkgname.initd
	$pkgname.confd
Natanael Copa's avatar
Natanael Copa committed
174 175 176
	"
	fi

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

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

206 207
	# 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.
208
	find "$sdir" -name "*.h" -o -name "*.hpp" -maxdepth 3 2>/dev/null \
209 210 211
		| head -n 1 | grep -q ".*" \
		|| sed -i -e '/^depends_dev=.*/d' -e 's/\$depends_dev\s*//' APKBUILD

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

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

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

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

__EOF__

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

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

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

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

__EOF__
283
	abuild -f checksum
284 285 286
}

usage() {
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
	cat >&2 <<-__EOF__
		$program $program_version - generate a new APKBUILD
		Usage: $program [-cfh] [-d DESC] [-l LICENSE] [-n NAME] [-u URL]
		       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