about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorPeter H. Froehlich <peter.hans.froehlich@gmail.com>2021-10-17 06:41:36 -0400
committerPeter H. Froehlich <peter.hans.froehlich@gmail.com>2021-10-17 06:41:36 -0400
commit9087339caef73478e0156fd996c5c55c52a48dcc (patch)
tree5cec0e4dbea0231ac7f219b87750f4dc121227ba
parent4a275b2e66edd7305992fc8ec5d69e7cd43d7f2f (diff)
downloadmkgpt-9087339caef73478e0156fd996c5c55c52a48dcc.tar.gz
OpenBSD "port" of sorts, Makefile is almost completely new
-rw-r--r--Makefile53
-rw-r--r--deps.mk4
-rw-r--r--mkgpt.c9
-rwxr-xr-xtest-mkgpt.sh20
4 files changed, 58 insertions, 28 deletions
diff --git a/Makefile b/Makefile
index e982750..6d5c56f 100644
--- a/Makefile
+++ b/Makefile
@@ -1,37 +1,48 @@
-CFLAGS+=-Wall -Wextra -Wpedantic -std=c11 -D_DEFAULT_SOURCE #-D_FORTIFY_SOURCE=2
-ALL:=mkgpt
-PREFIX:=/usr/local
+.POSIX:
+CC?=cc # GNU make .POSIX fix
+SHELL=/bin/sh # paranoia
 
-dev: CFLAGS+=-g3 -Og -fsanitize=address -fsanitize=undefined
-dev: LDFLAGS+=-fsanitize=address -fsanitize=undefined
-dev: $(ALL)
+.PHONY: prod static dev sane  check clean depend format  install uninstall
+.SUFFIXES:
+.SUFFIXES: .c .o
+.c.o:
+	$(CC) $(CFLAGS) $(CPPFLAGS) -c -o $@ $<
 
-prod: CFLAGS+=-g0 -Os
-prod: LDFLAGS+=-s
-prod: $(ALL)
+CFLAGS+=-Wall -Wextra -Wpedantic -std=c11 -D_DEFAULT_SOURCE #-D_FORTIFY_SOURCE=2
+LDFLAGS+=
 
-static: LDFLAGS+=-static
-static: prod
+OBJS=mkgpt.o crc32.o guid.o part_ids.o
 
-musl-static: CC:=musl-gcc
-musl-static: static
+mkgpt: $(OBJS)
+	$(CC) $(LDFLAGS) -o $@ $(OBJS) $(LDLIBS)
 
-mkgpt: mkgpt.o crc32.o guid.o part_ids.o
+# use "make depend" to generate a new one
+-include deps.mk
 
-mkgpt.o: mkgpt.c guid.h crc32.h
-guid.o: guid.c guid.h
-crc32.o: crc32.c crc32.h
-part_ids.o: part_ids.c part_ids.h guid.h
+# different ways to build
+prod:
+	CFLAGS="-g0 -Os" LDFLAGS="-s" $(MAKE) mkgpt
+static:
+	CFLAGS="-g0 -Os" LDFLAGS="-s -static" $(MAKE) mkgpt
+dev:
+	CFLAGS="-g3 -Og" $(MAKE) mkgpt
+sane:
+	CFLAGS="-g3 -Og -fsanitize=address -fsanitize=undefined" \
+	LDFLAGS="-fsanitize=address -fsanitize=undefined" \
+	$(MAKE) mkgpt
 
-.PHONY: check clean format install uninstall  dev prod static musl-static
 check:
 	-cppcheck --enable=all --inconclusive --std=c11 .
 	-shellcheck *.sh
 clean:
-	$(RM) *.o $(ALL)
+	rm -fv $(OBJS) mkgpt
+depend:
+	$(CC) -MM *.c >deps.mk
 format:
 	-clang-format -verbose -i *.[ch]
-install: prod
+
+PREFIX=/usr/local
+install:
 	mkdir -pv $(DESTDIR)$(PREFIX)/bin
 	cp -fv mkgpt $(DESTDIR)$(PREFIX)/bin
 uninstall:
diff --git a/deps.mk b/deps.mk
new file mode 100644
index 0000000..8ad0e17
--- /dev/null
+++ b/deps.mk
@@ -0,0 +1,4 @@
+crc32.o: crc32.c crc32.h
+guid.o: guid.c guid.h unaligned.h
+mkgpt.o: mkgpt.c crc32.h guid.h part_ids.h unaligned.h
+part_ids.o: part_ids.c part_ids.h guid.h
diff --git a/mkgpt.c b/mkgpt.c
index ae71032..885aac3 100644
--- a/mkgpt.c
+++ b/mkgpt.c
@@ -33,6 +33,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <unistd.h>
 
 struct partition {
 	GUID type;
@@ -95,6 +96,14 @@ static int secondary_gpt_sect;
 int
 main(int argc, char *argv[])
 {
+#if defined(__OpenBSD__)
+	if (pledge("stdio cpath rpath wpath", NULL) != 0) {
+		fprintf(stderr, "failed to pledge\n");
+		exit(EXIT_FAILURE);
+	}
+	/* TODO call unveil on each path AHEAD of using it? */
+#endif
+
 	random_guid(&disk_guid);
 
 	if (parse_opts(argc, argv) != 0) {
diff --git a/test-mkgpt.sh b/test-mkgpt.sh
index 2ea491a..b221dab 100755
--- a/test-mkgpt.sh
+++ b/test-mkgpt.sh
@@ -9,9 +9,13 @@ if [ ! -x ./mkgpt ]; then
 	exit 1
 fi
 
-mkdir -pv ${tmpdir}
+mkdir -p ${tmpdir}
 for name in a.img b.img c.img d.img e.img; do
-	truncate --size=8M ${tmpdir}/${name}
+	if which truncate 2>/dev/null; then
+		truncate --size=8M ${tmpdir}/${name}
+	else
+		dd if=/dev/zero of=${tmpdir}/${name} bs=1M count=8
+	fi
 done
 
 # TODO something goes wrong with --sector-size 1024 or 4096
@@ -23,13 +27,15 @@ done
 	--part ${tmpdir}/d.img --type 0x82 --name 123456789012345678901234567890123456 --uuid 22222222-2222-2222-2222-222222222222 \
 	--part ${tmpdir}/e.img --type 21686148-6449-6E6F-744E-656564454649 --uuid 44444444-4444-4444-4444-444444444444
 
-fdisk -l ${tmpdir}/bla.img
-sfdisk --verify ${tmpdir}/bla.img
-sfdisk --dump ${tmpdir}/bla.img
-head -c 512 ${tmpdir}/bla.img | xxd -s 446
+if [ "$(uname)" = "Linux" ]; then
+	fdisk -l ${tmpdir}/bla.img
+	sfdisk --verify ${tmpdir}/bla.img
+	sfdisk --dump ${tmpdir}/bla.img
+fi
+dd if=${tmpdir}/bla.img bs=512 count=1 | xxd -seek 446
 
 checksum=$(md5sum ${tmpdir}/bla.img | cut -c1-32)
-if [ ! "$checksum" = "a4abc130196a29288ab9af1c4489fe24" ]; then
+if [ ! "${checksum}" = "a4abc130196a29288ab9af1c4489fe24" ]; then
 	echo "checksum didn't match, regression!"
 	exit 1
 fi