From 23c15e2536d0fa5ed81c5bdfe7623d3370dca997 Mon Sep 17 00:00:00 2001
From: Paul Spooren <mail@aparcar.org>
Date: Fri, 2 Oct 2020 23:30:30 -1000
Subject: [PATCH 21/27] build: create APK files parrallel to IPK

Create APK files based on the folder and control files of IPK packages.

Signed-off-by: Paul Spooren <mail@aparcar.org>
---
 include/package-ipkg.mk     | 48 +++++++++++++++++++++++++------------
 package/Makefile            |  2 ++
 package/base-files/Makefile |  4 ++++
 rules.mk                    |  2 ++
 scripts/apk-make-index.sh   | 20 ++++++++++++++++
 5 files changed, 61 insertions(+), 15 deletions(-)
 create mode 100755 scripts/apk-make-index.sh

diff --git a/include/package-ipkg.mk b/include/package-ipkg.mk
index e972b7de0b..5c31b1774f 100644
--- a/include/package-ipkg.mk
+++ b/include/package-ipkg.mk
@@ -102,6 +102,7 @@ ifeq ($(DUMP),)
     ABIV_$(1):=$(call FormatABISuffix,$(1),$(ABI_VERSION))
     PDIR_$(1):=$(call FeedPackageDir,$(1))
     IPKG_$(1):=$$(PDIR_$(1))/$(1)$$(ABIV_$(1))_$(VERSION)_$(PKGARCH).ipk
+    APK_$(1):=$$(PDIR_$(1))/$(1)$$(ABIV_$(1))_$(VERSION)_$(PKGARCH).apk
     IDIR_$(1):=$(PKG_BUILD_DIR)/ipkg-$(PKGARCH)/$(1)
     KEEP_$(1):=$(strip $(call Package/$(1)/conffiles))
 
@@ -200,7 +201,7 @@ $(_endef)
     $(PKG_INFO_DIR)/$(1).provides $$(IPKG_$(1)): $(STAMP_BUILT) $(INCLUDE_DIR)/package-ipkg.mk
 	@rm -rf $$(IDIR_$(1)); \
 		$$(call remove_ipkg_files,$(1),$$(call opkg_package_files,$(call gen_ipkg_wildcard,$(1))))
-	mkdir -p $(PACKAGE_DIR) $$(IDIR_$(1))/CONTROL $(PKG_INFO_DIR)
+	mkdir -p $(PACKAGE_DIR) $$(IDIR_$(1)) $(PKG_INFO_DIR)
 	$(call Package/$(1)/install,$$(IDIR_$(1)))
 	$(if $(Package/$(1)/install-overlay),mkdir -p $(PACKAGE_DIR) $$(IDIR_$(1))/rootfs-overlay)
 	$(call Package/$(1)/install-overlay,$$(IDIR_$(1))/rootfs-overlay)
@@ -226,6 +227,37 @@ $(_endef)
 		) || true \
 	)
     endif
+
+    ifneq ($$(KEEP_$(1)),)
+		@( \
+			keepfiles=""; \
+			for x in $$(KEEP_$(1)); do \
+				[ -f "$$(IDIR_$(1))/$$$$x" ] || keepfiles="$$$${keepfiles:+$$$$keepfiles }$$$$x"; \
+			done; \
+			[ -z "$$$$keepfiles" ] || { \
+				mkdir -p $$(IDIR_$(1))/lib/upgrade/keep.d; \
+				for x in $$$$keepfiles; do echo $$$$x >> $$(IDIR_$(1))/lib/upgrade/keep.d/$(1); done; \
+			}; \
+		)
+    endif
+
+	$(INSTALL_DIR) $$(PDIR_$(1))
+
+	$(FAKEROOT) apk mkpkg \
+	  --info "name:$(1)" \
+	  --info "version:$(VERSION)" \
+	  --info "description:$()" \
+	  --info "arch:$(PKGARCH)" \
+	  --info "license:$(LICENSE)" \
+	  --info "origin:$(SOURCE)" \
+	  --info "maintainer:$(MAINTAINER)" \
+	  $$(foreach dep,$$(Package/$(1)/DEPENDS),--info "depends:$$(subst $$(comma),,$$(dep))") \
+	  --files "$$(IDIR_$(1))" \
+	  --output "$$(APK_$(1))" \
+	  --sign "$(BUILD_KEY_APK_SEC)"
+
+	mkdir -p $$(IDIR_$(1))/CONTROL
+
 	(cd $$(IDIR_$(1))/CONTROL; \
 		( \
 			echo "$$$$CONTROL"; \
@@ -249,20 +281,6 @@ $(_endef)
 		$($(1)_COMMANDS) \
 	)
 
-    ifneq ($$(KEEP_$(1)),)
-		@( \
-			keepfiles=""; \
-			for x in $$(KEEP_$(1)); do \
-				[ -f "$$(IDIR_$(1))/$$$$x" ] || keepfiles="$$$${keepfiles:+$$$$keepfiles }$$$$x"; \
-			done; \
-			[ -z "$$$$keepfiles" ] || { \
-				mkdir -p $$(IDIR_$(1))/lib/upgrade/keep.d; \
-				for x in $$$$keepfiles; do echo $$$$x >> $$(IDIR_$(1))/lib/upgrade/keep.d/$(1); done; \
-			}; \
-		)
-    endif
-
-	$(INSTALL_DIR) $$(PDIR_$(1))
 	$(FAKEROOT) $(SCRIPT_DIR)/ipkg-build -m "$(FILE_MODES)" $$(IDIR_$(1)) $$(PDIR_$(1))
 	@[ -f $$(IPKG_$(1)) ]
 
diff --git a/package/Makefile b/package/Makefile
index ec503dc527..18a19fff13 100644
--- a/package/Makefile
+++ b/package/Makefile
@@ -60,6 +60,7 @@ $(curdir)/merge-index: $(curdir)/merge
 
 ifndef SDK
   $(curdir)/compile: $(curdir)/system/opkg/host/compile
+  $(patsubst %,$(curdir)/%/compile,$(filter-out %/apk/host,$($(curdir)/builddirs))): $(curdir)/system/apk/host/compile
 endif
 
 $(curdir)/install: $(TMP_DIR)/.build $(curdir)/merge $(if $(CONFIG_TARGET_PER_DEVICE_ROOTFS),$(curdir)/merge-index)
@@ -84,6 +85,7 @@ $(curdir)/index: FORCE
 	@for d in $(PACKAGE_SUBDIRS); do ( \
 		mkdir -p $$d; \
 		cd $$d || continue; \
+		$(SCRIPT_DIR)/apk-make-index.sh . 2>&1; \
 		$(SCRIPT_DIR)/ipkg-make-index.sh . 2>&1 > Packages.manifest; \
 		grep -vE '^(Maintainer|LicenseFiles|Source|SourceName|Require|SourceDateEpoch)' Packages.manifest > Packages; \
 		case "$$(((64 + $$(stat -L -c%s Packages)) % 128))" in 110|111) \
diff --git a/package/base-files/Makefile b/package/base-files/Makefile
index 8a1ddf96f5..9db4812981 100644
--- a/package/base-files/Makefile
+++ b/package/base-files/Makefile
@@ -107,6 +107,10 @@ ifdef CONFIG_SIGNED_PACKAGES
 	[ -s $(BUILD_KEY).ucert ] || \
 		$(STAGING_DIR_HOST)/bin/ucert -I -c $(BUILD_KEY).ucert -p $(BUILD_KEY).pub -s $(BUILD_KEY)
 
+	[ -s $(BUILD_KEY_APK_SEC) -a -s $(BUILD_KEY_APK_PUB) ] || \
+		openssl ecparam -name prime256v1 -genkey -noout -out $(BUILD_KEY_APK_SEC); \
+		openssl ec -in $(BUILD_KEY_APK_SEC) -pubout > $(BUILD_KEY_APK_PUB)
+
   endef
 
 ifndef CONFIG_BUILDBOT
diff --git a/rules.mk b/rules.mk
index f31d9bb113..de81b65d46 100644
--- a/rules.mk
+++ b/rules.mk
@@ -258,6 +258,8 @@ else
 endif
 
 BUILD_KEY=$(TOPDIR)/key-build
+BUILD_KEY_APK_SEC=$(TOPDIR)/private-key.pem
+BUILD_KEY_APK_PUB=$(TOPDIR)/public-key.pem
 
 FAKEROOT:=$(STAGING_DIR_HOST)/bin/fakeroot
 
diff --git a/scripts/apk-make-index.sh b/scripts/apk-make-index.sh
new file mode 100755
index 0000000000..df1f1a2e2b
--- /dev/null
+++ b/scripts/apk-make-index.sh
@@ -0,0 +1,20 @@
+#!/usr/bin/env bash
+set -e
+
+pkg_dir=$1
+
+if [ -z "$pkg_dir" ] || [ ! -d "$pkg_dir" ]; then
+	echo "Usage: apk-make-index <package_directory>" >&2
+	exit 1
+fi
+
+(
+	cd "$pkg_dir" || exit 1
+	GLOBIGNORE="kernel*:libc*"
+	set -- *.apk
+	if [ "$1" = '*.apk' ]; then
+		echo "No APK packages found"
+	fi
+	apk index --output APKINDEX.tar.gz "$@"
+	unset GLOBIGNORE
+)
-- 
2.25.1

