From bf98d3c1de524ca50cbc09f825a4b0648e353624 Mon Sep 17 00:00:00 2001
From: John Crispin <john@phrozen.org>
Date: Fri, 13 Aug 2021 08:47:11 +0200
Subject: [PATCH 25/27] apk: backport package

Signed-off-by: John Crispin <john@phrozen.org>
---
 package/system/apk/Makefile                   | 93 +++++++++++++++++++
 ...vel@lists.alpinelinux.org-4a6a0840.rsa.pub |  9 ++
 ...vel@lists.alpinelinux.org-5243ef4b.rsa.pub |  9 ++
 ...vel@lists.alpinelinux.org-5261cecb.rsa.pub |  9 ++
 package/system/apk/files/alpine-repositories  |  3 +
 .../apk/patches/000-Makefile-version.patch    | 11 +++
 .../patches/0001-remove-doc-generation.patch  | 21 +++++
 package/system/apk/patches/100-link.patch     | 16 ++++
 package/system/apk/patches/100-phtread.patch  | 12 +++
 ...ude-limits.h-to-fix-build-with-glibc.patch | 20 ++++
 package/system/apk/test.sh                    |  9 ++
 11 files changed, 212 insertions(+)
 create mode 100644 package/system/apk/Makefile
 create mode 100644 package/system/apk/files/alpine-keys/alpine-devel@lists.alpinelinux.org-4a6a0840.rsa.pub
 create mode 100644 package/system/apk/files/alpine-keys/alpine-devel@lists.alpinelinux.org-5243ef4b.rsa.pub
 create mode 100644 package/system/apk/files/alpine-keys/alpine-devel@lists.alpinelinux.org-5261cecb.rsa.pub
 create mode 100644 package/system/apk/files/alpine-repositories
 create mode 100644 package/system/apk/patches/000-Makefile-version.patch
 create mode 100644 package/system/apk/patches/0001-remove-doc-generation.patch
 create mode 100644 package/system/apk/patches/100-link.patch
 create mode 100644 package/system/apk/patches/100-phtread.patch
 create mode 100644 package/system/apk/patches/100-tar-include-limits.h-to-fix-build-with-glibc.patch
 create mode 100644 package/system/apk/test.sh

diff --git a/package/system/apk/Makefile b/package/system/apk/Makefile
new file mode 100644
index 0000000000..335f50c155
--- /dev/null
+++ b/package/system/apk/Makefile
@@ -0,0 +1,93 @@
+include $(TOPDIR)/rules.mk
+
+PKG_NAME:=apk
+PKG_VERSION:=3.0.0_pre0
+
+PKG_SOURCE_PROTO:=git
+PKG_SOURCE_URL:=https://git.alpinelinux.org/apk-tools.git
+PKG_SOURCE_DATE:=2021-08-17
+PKG_SOURCE_VERSION:=a46043bcc4cc15b456ef1eac5c5f9d93bd905d53
+PKG_MIRROR_HASH:=e16fd04b18043e78a177acd8c6958fa03fd1484b62c879c2dd0bed8ce9c50625
+PKG_SOURCE_SUBDIR:=$(PKG_NAME)-$(PKG_SOURCE_DATE)-$(call version_abbrev,$(PKG_SOURCE_VERSION))
+PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_SOURCE_SUBDIR)
+HOST_BUILD_DIR:=$(BUILD_DIR_HOST)/$(PKG_SOURCE_SUBDIR)
+PKG_SOURCE:=$(PKG_SOURCE_SUBDIR).tar.xz
+PKG_RELEASE:=r$(PKG_SOURCE_DATE)-$(call version_abbrev,$(PKG_SOURCE_VERSION))-$(AUTORELEASE)
+
+PKG_MAINTAINER:=Paul Spooren <mail@aparcar.org>
+PKG_LICENSE:=GPL-2.0-only
+PKG_LICENSE_FILES:=LICENSE
+
+PKG_INSTALL:=1
+PKG_BUILD_PARALLEL:=1
+
+HOST_BUILD_DEPENDS:=lua/host lua-lzlib/host
+PKG_BUILD_DEPENDS:=$(HOST_BUILD_DEPENDS)
+
+include $(INCLUDE_DIR)/package.mk
+include $(INCLUDE_DIR)/host-build.mk
+
+define Package/apk
+  SECTION:=utils
+  CATEGORY:=Utilities
+  TITLE:=apk package manager
+  DEPENDS:=+liblua +libopenssl +zlib @!arc
+  URL:=$(PKG_SOURCE_URL)
+endef
+
+define Package/alpine-keys
+  SECTION:=utils
+  CATEGORY:=Utilities
+  TITLE:=Alpine apk public signing keys
+  DEPENDS:=apk
+endef
+
+define Package/alpine-repositories
+  SECTION:=utils
+  CATEGORY:=Utilities
+  TITLE:=Official Alpine repositories
+  DEPENDS:=apk
+endef
+
+MAKE_FLAGS += \
+	LUA=$(STAGING_DIR_HOSTPKG)/bin/lua \
+	LUA_VERSION=5.1 \
+	LUA_PC=lua
+
+HOST_MAKE_FLAGS += \
+	LUA=$(STAGING_DIR_HOSTPKG)/bin/lua \
+	LUA_VERSION=5.1 \
+	DESTDIR=$(STAGING_DIR_HOSTPKG) \
+	SBINDIR=/bin \
+	PREFIX=
+
+HOST_LDFLAGS+=-Wl,-rpath=$(STAGING_DIR_HOSTPKG)/lib -lpthread
+
+define Package/apk/install
+	$(INSTALL_DIR) $(1)/lib/apk/db
+
+	$(INSTALL_DIR) $(1)/bin
+	$(INSTALL_BIN) $(PKG_INSTALL_DIR)/sbin/apk $(1)/bin/apk
+
+	$(INSTALL_DIR) $(1)/usr/lib
+	$(CP) $(PKG_INSTALL_DIR)/lib/*.so.* $(1)/usr/lib/
+
+	$(INSTALL_DIR) $(1)/etc/apk/
+	echo $(ARCH) > $(1)/etc/apk/arch
+	touch $(1)/etc/apk/world
+endef
+
+define Package/alpine-keys/install
+	$(INSTALL_DIR) $(1)/etc/apk/keys
+	$(INSTALL_DATA) ./files/alpine-keys/* $(1)/etc/apk/keys
+endef
+
+define Package/alpine-repositories/install
+	$(INSTALL_DIR) $(1)/etc/apk/keys
+	$(INSTALL_DATA) ./files/alpine-repositories $(1)/etc/apk/repositories
+endef
+
+$(eval $(call BuildPackage,apk))
+$(eval $(call BuildPackage,alpine-keys))
+$(eval $(call BuildPackage,alpine-repositories))
+$(eval $(call HostBuild))
diff --git a/package/system/apk/files/alpine-keys/alpine-devel@lists.alpinelinux.org-4a6a0840.rsa.pub b/package/system/apk/files/alpine-keys/alpine-devel@lists.alpinelinux.org-4a6a0840.rsa.pub
new file mode 100644
index 0000000000..bb4bdc80fd
--- /dev/null
+++ b/package/system/apk/files/alpine-keys/alpine-devel@lists.alpinelinux.org-4a6a0840.rsa.pub
@@ -0,0 +1,9 @@
+-----BEGIN PUBLIC KEY-----
+MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA1yHJxQgsHQREclQu4Ohe
+qxTxd1tHcNnvnQTu/UrTky8wWvgXT+jpveroeWWnzmsYlDI93eLI2ORakxb3gA2O
+Q0Ry4ws8vhaxLQGC74uQR5+/yYrLuTKydFzuPaS1dK19qJPXB8GMdmFOijnXX4SA
+jixuHLe1WW7kZVtjL7nufvpXkWBGjsfrvskdNA/5MfxAeBbqPgaq0QMEfxMAn6/R
+L5kNepi/Vr4S39Xvf2DzWkTLEK8pcnjNkt9/aafhWqFVW7m3HCAII6h/qlQNQKSo
+GuH34Q8GsFG30izUENV9avY7hSLq7nggsvknlNBZtFUcmGoQrtx3FmyYsIC8/R+B
+ywIDAQAB
+-----END PUBLIC KEY-----
diff --git a/package/system/apk/files/alpine-keys/alpine-devel@lists.alpinelinux.org-5243ef4b.rsa.pub b/package/system/apk/files/alpine-keys/alpine-devel@lists.alpinelinux.org-5243ef4b.rsa.pub
new file mode 100644
index 0000000000..6cbfad7441
--- /dev/null
+++ b/package/system/apk/files/alpine-keys/alpine-devel@lists.alpinelinux.org-5243ef4b.rsa.pub
@@ -0,0 +1,9 @@
+-----BEGIN PUBLIC KEY-----
+MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvNijDxJ8kloskKQpJdx+
+mTMVFFUGDoDCbulnhZMJoKNkSuZOzBoFC94omYPtxnIcBdWBGnrm6ncbKRlR+6oy
+DO0W7c44uHKCFGFqBhDasdI4RCYP+fcIX/lyMh6MLbOxqS22TwSLhCVjTyJeeH7K
+aA7vqk+QSsF4TGbYzQDDpg7+6aAcNzg6InNePaywA6hbT0JXbxnDWsB+2/LLSF2G
+mnhJlJrWB1WGjkz23ONIWk85W4S0XB/ewDefd4Ly/zyIciastA7Zqnh7p3Ody6Q0
+sS2MJzo7p3os1smGjUF158s6m/JbVh4DN6YIsxwl2OjDOz9R0OycfJSDaBVIGZzg
+cQIDAQAB
+-----END PUBLIC KEY-----
diff --git a/package/system/apk/files/alpine-keys/alpine-devel@lists.alpinelinux.org-5261cecb.rsa.pub b/package/system/apk/files/alpine-keys/alpine-devel@lists.alpinelinux.org-5261cecb.rsa.pub
new file mode 100644
index 0000000000..83f0658e9c
--- /dev/null
+++ b/package/system/apk/files/alpine-keys/alpine-devel@lists.alpinelinux.org-5261cecb.rsa.pub
@@ -0,0 +1,9 @@
+-----BEGIN PUBLIC KEY-----
+MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAwlzMkl7b5PBdfMzGdCT0
+cGloRr5xGgVmsdq5EtJvFkFAiN8Ac9MCFy/vAFmS8/7ZaGOXoCDWbYVLTLOO2qtX
+yHRl+7fJVh2N6qrDDFPmdgCi8NaE+3rITWXGrrQ1spJ0B6HIzTDNEjRKnD4xyg4j
+g01FMcJTU6E+V2JBY45CKN9dWr1JDM/nei/Pf0byBJlMp/mSSfjodykmz4Oe13xB
+Ca1WTwgFykKYthoLGYrmo+LKIGpMoeEbY1kuUe04UiDe47l6Oggwnl+8XD1MeRWY
+sWgj8sF4dTcSfCMavK4zHRFFQbGp/YFJ/Ww6U9lA3Vq0wyEI6MCMQnoSMFwrbgZw
+wwIDAQAB
+-----END PUBLIC KEY-----
diff --git a/package/system/apk/files/alpine-repositories b/package/system/apk/files/alpine-repositories
new file mode 100644
index 0000000000..5babbb23b4
--- /dev/null
+++ b/package/system/apk/files/alpine-repositories
@@ -0,0 +1,3 @@
+https://dl-cdn.alpinelinux.org/alpine/edge/main
+https://dl-cdn.alpinelinux.org/alpine/edge/community
+
diff --git a/package/system/apk/patches/000-Makefile-version.patch b/package/system/apk/patches/000-Makefile-version.patch
new file mode 100644
index 0000000000..2e7f5b0f15
--- /dev/null
+++ b/package/system/apk/patches/000-Makefile-version.patch
@@ -0,0 +1,11 @@
+--- a/Makefile
++++ b/Makefile
+@@ -4,7 +4,7 @@
+ -include config.mk
+ 
+ PACKAGE := apk-tools
+-VERSION := 2.12.0
++VERSION := 3.0.0_pre0
+ 
+ export VERSION
+ 
diff --git a/package/system/apk/patches/0001-remove-doc-generation.patch b/package/system/apk/patches/0001-remove-doc-generation.patch
new file mode 100644
index 0000000000..dee05c56f2
--- /dev/null
+++ b/package/system/apk/patches/0001-remove-doc-generation.patch
@@ -0,0 +1,21 @@
+From b05a93c48fdbb50f0c464310dc2ce45777d32ea2 Mon Sep 17 00:00:00 2001
+From: Paul Spooren <mail@aparcar.org>
+Date: Fri, 2 Oct 2020 14:08:52 -1000
+Subject: [PATCH] remove doc generation
+
+Signed-off-by: Paul Spooren <mail@aparcar.org>
+---
+ Makefile | 2 +-
+ 1 file changed, 1 insertion(+), 1 deletion(-)
+
+--- a/Makefile
++++ b/Makefile
+@@ -25,7 +25,7 @@ export DESTDIR SBINDIR LIBDIR CONFDIR MA
+ ##
+ # Top-level subdirs
+ 
+-subdirs		:= libfetch/ src/ doc/
++subdirs		:= libfetch/ src/
+ 
+ ##
+ # Include all rules and stuff
diff --git a/package/system/apk/patches/100-link.patch b/package/system/apk/patches/100-link.patch
new file mode 100644
index 0000000000..9cae2787d9
--- /dev/null
+++ b/package/system/apk/patches/100-link.patch
@@ -0,0 +1,16 @@
+diff -urN apk-2021-08-17-a46043bc.orig/src/Makefile apk-2021-08-17-a46043bc/src/Makefile
+--- apk-2021-08-17-a46043bc.orig/src/Makefile	2021-08-17 14:21:04.117760513 +0200
++++ apk-2021-08-17-a46043bc/src/Makefile	2021-08-17 14:21:16.653830180 +0200
+@@ -65,7 +65,11 @@
+ 	app_convdb.o app_convndx.o app_del.o app_dot.o app_extract.o app_fetch.o \
+ 	app_fix.o app_index.o app_info.o app_list.o app_manifest.o app_mkndx.o \
+ 	app_mkpkg.o app_policy.o app_update.o app_upgrade.o app_search.o \
+-	app_stats.o app_verify.o app_version.o app_vertest.o applet.o
++	app_stats.o app_verify.o app_version.o app_vertest.o applet.o \
++	adb.o adb_comp.o adb_walk_adb.o adb_walk_genadb.o adb_walk_gentext.o adb_walk_text.o apk_adb.o \
++	atom.o blob.o commit.o common.o context.o crypto_openssl.o database.o hash.o \
++	extract.o extract_v2.o extract_v3.o io.o io_gunzip.o io_url.o tar.o \
++	package.o pathbuilder.o print.o solver.o trust.o version.o
+ 
+ ifeq ($(ADB),y)
+ libapk.so.$(libapk_soname)-objs += apk_adb.o
diff --git a/package/system/apk/patches/100-phtread.patch b/package/system/apk/patches/100-phtread.patch
new file mode 100644
index 0000000000..c252e14dc1
--- /dev/null
+++ b/package/system/apk/patches/100-phtread.patch
@@ -0,0 +1,12 @@
+diff -urN apk-2021-07-23-3d203e8f.orig/src/Makefile apk-2021-07-23-3d203e8f/src/Makefile
+--- apk-2021-07-23-3d203e8f.orig/src/Makefile	2021-07-25 12:55:05.576564663 +0200
++++ apk-2021-07-23-3d203e8f/src/Makefile	2021-07-25 12:55:48.660862181 +0200
+@@ -87,7 +87,7 @@
+ apk.static-libs		:= $(apk-static-libs)
+ LDFLAGS_apk.static	:= -static
+ LIBS_apk.static		:= -Wl,--as-needed -ldl -Wl,--no-as-needed
+-LDFLAGS_apk		+= -L$(obj)
++LDFLAGS_apk		+= -L$(obj) -pthread
+ LDFLAGS_apk-test	+= -L$(obj)
+ 
+ CFLAGS_ALL		+= $(OPENSSL_CFLAGS) $(ZLIB_CFLAGS)
diff --git a/package/system/apk/patches/100-tar-include-limits.h-to-fix-build-with-glibc.patch b/package/system/apk/patches/100-tar-include-limits.h-to-fix-build-with-glibc.patch
new file mode 100644
index 0000000000..43ec7d5f1a
--- /dev/null
+++ b/package/system/apk/patches/100-tar-include-limits.h-to-fix-build-with-glibc.patch
@@ -0,0 +1,20 @@
+From c72ea983e287ec1d8b1f2b3aab1bf40aa7a30b03 Mon Sep 17 00:00:00 2001
+From: Daniel Golle <daniel@makrotopia.org>
+Date: Wed, 4 Aug 2021 21:37:40 +0100
+Subject: [PATCH] tar: include <limits.h> to fix build with glibc
+
+Signed-off-by: Daniel Golle <daniel@makrotopia.org>
+---
+ src/tar.c | 1 +
+ 1 file changed, 1 insertion(+)
+
+--- a/src/tar.c
++++ b/src/tar.c
+@@ -9,6 +9,7 @@
+ 
+ #include <sys/stat.h>
+ #include <sys/sysmacros.h>
++#include <limits.h> /* for SSIZE_MAX with glibc */
+ 
+ #include "apk_defines.h"
+ #include "apk_tar.h"
diff --git a/package/system/apk/test.sh b/package/system/apk/test.sh
new file mode 100644
index 0000000000..814777fd70
--- /dev/null
+++ b/package/system/apk/test.sh
@@ -0,0 +1,9 @@
+#!/bin/sh
+
+case "$1" in
+    "apk")
+        apk --version | grep "${2/-r*/}"
+        ;;
+    *)
+        return 0;
+esac
-- 
2.25.1

