newapkbuild.in 7.04 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 36 37 38 39
build_make() {
	cat >>APKBUILD<<__EOF__
	make || return 1
__EOF__
}

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

Isaac Dunham's avatar
Isaac Dunham committed
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
build_cmake() {
# References:
# http://www.cmake.org/Wiki/CMake_Useful_Variables
# http://www.vtk.org/Wiki/CMake_Cross_Compiling 
# This is incomplete: CMAKE_{HOST_,}SYSTEM_PROCESSOR needs to be set, 
# 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_COMPILER="\${CXX:-g++}" \\
		-DCMAKE_C_COMPILER="\${CC:-gcc}" \\
		-DCMAKE_CXX_FLAGS="\$CXXFLAGS" \\
		-DCMAKE_CXX_FLAGS="\$CFLAGS" \\
		${CMAKE_CROSSOPTS} \\
		|| return 1
	make || return 1
__EOF__
}

83
build_perl() {
84 85
	cat >>APKBUILD<<__EOF__
	PERL_MM_USE_DEFAULT=1 perl Makefile.PL INSTALLDIRS=vendor || return 1
86 87 88 89
	make || return 1
__EOF__
}

90
build_python() {
91 92
	cat >>APKBUILD<<__EOF__
	python setup.py build || return 1
93 94 95
__EOF__
}

96
# Package sections
97
package_make() {
98 99 100 101 102 103
	cat >>APKBUILD<<__EOF__
	make DESTDIR="\$pkgdir" install || return 1
	rm -f "\$pkgdir"/usr/lib/*.la
__EOF__
}

104 105 106 107
package_autotools() {
	package_make
}

108
package_perl() {
109
	cat >>APKBUILD<<__EOF__
110
	make DESTDIR="\$pkgdir" install || return 1
111 112 113 114
	find "\$pkgdir" \\( -name perllocal.pod -o -name .packlist \\) -delete
__EOF__
}

115 116 117 118 119 120 121
package_python() {
	cat >>APKBUILD<<__EOF__
	python setup.py install --prefix=/usr --root="\$pkgdir" || return 1
__EOF__
}

# Create new aport from templates
122
newaport() {
123
	local newname="${1##*/}"
124 125
	local pn=${newname%-[0-9]*}
	local pv
126 127 128
	local source=
	is_url "$1" && source="$1"

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

151 152 153 154
	if [ -z "$source" ] && [ -n "$sourceforge" ]; then
		source="http://downloads.sourceforge.net/$pn/$pn-$pv.tar.gz"
	fi

155
	if [ -z "$depends" ] &&[ "$buildtype" = "python" ]; then
156 157 158
		depends="python"
	fi

159
	if [ -z "$makedepends" ] &&[ "$buildtype" = "python" ]; then
160 161 162 163 164 165
		makedepends="python-dev"
	else
        makedepends="\$depends_dev"
	fi

	# Replace pkgver in $source
166
	if [ -n "$source" ]; then
167
		source=$(echo "$source" | sed "s/$pv/\$pkgver/g")
168
	fi
Natanael Copa's avatar
Natanael Copa committed
169

170
	# Copy init.d scripts if requested
171
	if [ -n "$cpinitd" ]; then
172 173 174 175
		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
176 177
		install="\$pkgname.pre-install \$pkgname.post-install"
		source="$source
178 179
	$pkgname.initd
	$pkgname.confd
Natanael Copa's avatar
Natanael Copa committed
180 181 182
	"
	fi

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

__EOF__

	abuild -f fetch unpack
204
	# Figure out the _builddir
Natanael Copa's avatar
Natanael Copa committed
205 206 207 208 209 210 211 212 213
	for i in src/*; do
		if [ -d "$i" ]; then
			sdir=$i
			_builddir=$(echo ${i#*/} | sed "s/$pv/\$pkgver/g")
			_builddir="\"\$srcdir\"/$_builddir"
		fi
	done
	echo "_builddir=$_builddir" >> APKBUILD

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


232
	# Create the prepare() template
Natanael Copa's avatar
Natanael Copa committed
233 234 235 236 237 238 239 240 241 242 243 244 245
	cat >>APKBUILD<<__EOF__
prepare() {
	local i
	cd "\$_builddir"
	for i in \$source; do
		case \$i in
		*.patch) msg \$i; patch -p1 -i "\$srcdir"/\$i || return 1;;
		esac
	done
}

__EOF__

246
	# Create build() function
Natanael Copa's avatar
Natanael Copa committed
247 248 249 250
	cat >>APKBUILD<<__EOF__
build() {
	cd "\$_builddir"
__EOF__
251 252

	case "$buildtype" in
253 254
	make)
		build_make;;
Isaac Dunham's avatar
Isaac Dunham committed
255 256
	cmake)
		build_cmake;;
257
	autotools)
258
		build_autotools;;
259
	perl)
260
		build_perl;;
261
	python)
262
		build_python;;
263 264
	esac

Natanael Copa's avatar
Natanael Copa committed
265 266 267 268 269
	cat >>APKBUILD<<__EOF__
}

__EOF__

270
	# Create package() function
Natanael Copa's avatar
Natanael Copa committed
271 272
	cat >>APKBUILD<<__EOF__
package() {
273
	cd "\$_builddir"
Natanael Copa's avatar
Natanael Copa committed
274
__EOF__
275 276

	case "$buildtype" in
277 278
	make)
		package_make;;
279 280 281 282 283 284
	autotools)
		package_autotools;;
	perl)
		package_perl;;
	python)
		package_python;;
285 286
	esac

287 288 289 290 291 292 293 294 295
	if [ -n "$cpinitd" ]; then
		cat >>APKBUILD<<__EOF__

	install -m755 -D "\$srcdir"/\$pkgname.initd \\
		"\$pkgdir"/etc/init.d/\$pkgname || return 1
	install -m644 -D "\$srcdir"/\$pkgname.confd \\
		"\$pkgdir"/etc/conf.d/\$pkgname || return 1
__EOF__
	fi
Natanael Copa's avatar
Natanael Copa committed
296 297 298 299
	cat >>APKBUILD<<__EOF__
}

__EOF__
300
	abuild -f checksum
301 302 303
}

usage() {
304
	cat >&2 <<__EOF__
305
$program $program_version - generate a new APKBUILD
306 307
Usage: $program [-cfh] [-d DESC] [-l LICENSE] [-n NAME] [-u URL]
		PKGNAME[-PKGVER]|SRCURL
308 309 310 311 312 313
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 ...)
Isaac Dunham's avatar
Isaac Dunham committed
314
  -C  Create CMake pakckage (Assume cmake/ is there)
315 316 317 318 319 320 321 322
  -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__
323 324
}

325
while getopts "acd:fhl:n:pyu:s" opt; do
326
	case $opt in
327
		'a') buildtype="autotools";;
328
		'c') cpinitd=1;;
Isaac Dunham's avatar
Isaac Dunham committed
329
		'C') buildtype="cmake";;
Natanael Copa's avatar
Natanael Copa committed
330
		'd') pkgdesc="$OPTARG";;
331
		'f') force=1;;
332
		'h') usage; exit;;
Natanael Copa's avatar
Natanael Copa committed
333
		'l') license="$OPTARG";;
334
		'n') pkgname="$OPTARG";;
335
		'p') buildtype="perl";;
336
		'y') buildtype="python";;
Natanael Copa's avatar
Natanael Copa committed
337
		'u') url="$OPTARG";;
338
		's') sourceforge=1;;
339 340 341 342
	esac
done
shift $(( $OPTIND - 1 ))

343
while [ $# -gt 0 ]; do
344 345 346 347
	newaport $1 || exit 1
	shift
done