8.23. patch stage 2 0.99.1
Rebuild the FreeBSD-derived patch utility in the final target environment.
Input assumption: bsdpatch-v0.99.1.tar.gz is already present in /sources from the chapter 4 source staging step.
Source URL: https://github.com/chimera-linux/bsdpatch/archive/refs/tags/v0.99.1.tar.gz
Upstream build note: upstream provides a simple portable Makefile. It builds with make and supports install-time PREFIX, BINDIR, DATADIR, MANDIR, and INSTALL_NAME variables.
Licenses:
- BSD-2-Clause-FreeBSD
Dependencies:
- musl (libc)
- make
- awk
patch is a FreeBSD-derived patch utility made portable by the Chimera Linux bsdpatch project. we need it to provide the final target patch command for applying source changes during maintenance and later package work.
Extract and Enter the Source Tree
cd /sources
rm -rf bsdpatch-0.99.1
tar -xf bsdpatch-v0.99.1.tar.gz
cd bsdpatch-0.99.1
Apply musl Compatibility Edits
sed -i '' \
-e 's|dp->d_namlen|strlen(dp->d_name)|' \
backupfile.c
sed -i '' \
-e 's|optreset = optind = 1;|optind = 1;|' \
patch.c
sed -i '' \
-e 's|fgetln(|lbi_fgetln(|g' \
inp.c pch.c
sed -i '' \
'/char[[:space:]]*\*xstrdup/a\
char *lbi_fgetln(FILE *, size_t *);
' \
util.h
awk '
/\/\* Rename a file, copying it if necessary\. \*\// {
print "char *"
print "lbi_fgetln(FILE *stream, size_t *len)"
print "{"
print "\tstatic char *line;"
print "\tstatic size_t cap;"
print "\tssize_t nread;"
print ""
print "\tnread = getline(&line, &cap, stream);"
print "\tif (nread < 0)"
print "\t\treturn NULL;"
print "\t*len = (size_t)nread;"
print "\treturn line;"
print "}"
print ""
}
{ print }
' util.c > util.c.new
mv util.c.new util.c
Build patch
bsdpatch does not ship a configure script, so lbi_configure is not applicable here.
make $LWI_MAKE_FLAGS \
CC=cc \
CFLAGS="-O2 $LWI_CFLAGS" \
LDFLAGS="$LBI_CUSTOM_LDFLAGS"
Install patch
make install \
PREFIX=/system \
BINDIR=/system/binaries \
DATADIR=/system/documentation \
MANDIR=/system/documentation/man-pages/man1 \
INSTALL_NAME=patch
After this step is complete, you can remove the extracted source directory and source tarball from
/sources if you do not plan to rebuild patch again.
Command Explanations
rm -rf bsdpatch-...andtar -xf: Recreate a clean bsdpatch source tree.sed -i ... backupfile.c: Replaces BSDd_namlenusage with portablestrlen(dp->d_name).sed -i ... patch.c: Removesoptresetusage that is not portable to musl.sed -i ... fgetln: Renames local compatibility functions to avoid namespace conflicts.make $LWI_MAKE_FLAGS CC=cc: Builds patch with the target compiler and shared make parallelism.make install PREFIX=/system ... INSTALL_NAME=patch: Installs the utility under the standardpatchcommand name in the book's layout.