Skip to content

Commit

Permalink
apk: add package
Browse files Browse the repository at this point in the history
APK (Alpine Package Keeper) is the package manager of Alpine Linux and
has multiple advantages over OPKG. While Alpine uses APK version 2, this
commit adds version 3 with a heavily optimised database structure and
additional feature making it suitable for OpenWrt.

This commit will be followed by many more to add APK build capabilities
to the OpenWrt build system, firstly enabling side by side builds of APK
and OPKG packages, later replacing OPKG entirely.

Signed-off-by: Paul Spooren <[email protected]>
  • Loading branch information
aparcar committed Mar 18, 2024
1 parent f7275de commit 74eaade
Show file tree
Hide file tree
Showing 4 changed files with 201 additions and 0 deletions.
64 changes: 64 additions & 0 deletions package/system/apk/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
include $(TOPDIR)/rules.mk

PKG_NAME:=apk
PKG_RELEASE:=1

PKG_SOURCE_DATE:=2024-03-16
PKG_SOURCE_VERSION:=f2f10d7e851ed3ff2a239c89c1a6a3612adbd63f
PKG_HASH:=964bbc71462630a5418a20ccd3e725eb0b065a6232d4aee16db745d95a04c1ff

PKG_VERSION:=3.0.0_pre0
PKG_SOURCE:=apk-tools-v$(PKG_SOURCE_VERSION).tar.gz
PKG_SOURCE_URL:=https://gitlab.alpinelinux.org/alpine/apk-tools/-/archive/$(PKG_SOURCE_VERSION)
PKG_BUILD_DIR:=$(BUILD_DIR)/apk-tools-$(PKG_SOURCE_VERSION)
HOST_BUILD_DIR:=$(BUILD_DIR_HOST)/apk-tools-$(PKG_SOURCE_VERSION)

PKG_MAINTAINER:=Paul Spooren <[email protected]>
PKG_LICENSE:=GPL-2.0-only
PKG_LICENSE_FILES:=LICENSE
PKG_INSTALL:=1

HOST_BUILD_PREFIX:=$(STAGING_DIR_HOST)
HOST_BUILD_DEPENDS:=lua/host
PKG_BUILD_DEPENDS:=$(HOST_BUILD_DEPENDS)

include $(INCLUDE_DIR)/package.mk
include $(INCLUDE_DIR)/host-build.mk
include $(INCLUDE_DIR)/meson.mk

define Package/apk
SECTION:=base
CATEGORY:=Base system
TITLE:=apk package manager
DEPENDS:=+zlib +libopenssl +libpthread @!arc
URL:=$(PKG_SOURCE_URL)
endef

MESON_HOST_ARGS += \
-Dlua_version=5.1 \
-Dcompressed-help=false \
-Ddocs=disabled \
-Dzstd=False
MESON_ARGS += \
-Dlua_version=5.1 \
-Dcompressed-help=false \
-Ddocs=disabled \
-Dzstd=False

HOST_LDFLAGS += \
-lpthread

HOST_CFLAGS += $(HOST_FPIC)

define Package/apk/install
$(INSTALL_DIR) $(1)/lib/apk/db

$(INSTALL_DIR) $(1)/usr/bin
$(INSTALL_BIN) $(PKG_INSTALL_DIR)/usr/bin/apk $(1)/usr/bin/apk

$(INSTALL_DIR) $(1)/usr/lib
$(CP) $(PKG_INSTALL_DIR)/usr/lib/libapk.so.* $(1)/usr/lib/
endef

$(eval $(call BuildPackage,apk))
$(eval $(call HostBuild))
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
From 9918c683fcc2f148328332d58d030ec5750a1473 Mon Sep 17 00:00:00 2001
From: Paul Spooren <[email protected]>
Date: Sat, 19 Feb 2022 17:20:37 +0100
Subject: [PATCH 1/4] openwrt: move layer db to temp folder

Signed-off-by: Paul Spooren <[email protected]>
---
src/database.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

--- a/src/database.c
+++ b/src/database.c
@@ -1604,7 +1604,7 @@ const char *apk_db_layer_name(int layer)
{
switch (layer) {
case APK_DB_LAYER_ROOT: return "lib/apk/db";
- case APK_DB_LAYER_UVOL: return "lib/apk/db-uvol";
+ case APK_DB_LAYER_UVOL: return "tmp/run/uvol/.meta/apk";
default:
assert("invalid layer");
return 0;
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
From 091488909354d3f6aabb3fe98379be9025ad04c1 Mon Sep 17 00:00:00 2001
From: Paul Spooren <[email protected]>
Date: Tue, 1 Feb 2022 09:30:20 +0100
Subject: [PATCH 2/4] change naming to <name>_<version>_<arch>.apk

OpenWrt allows dashes in package names but no underlines. The current
cleanup mechanism in the build system uses those underlines to find
suiting packages to cleanup via `<pkgname>_*.apk`.

Also add the package architecture to package filenames. This allows a
better overview when downloading packages manually i.e. from a CI.

Signed-off-by: Paul Spooren <[email protected]>
---
src/apk_package.h | 4 ++--
src/app_mkpkg.c | 2 +-
src/database.c | 6 ++++--
3 files changed, 7 insertions(+), 5 deletions(-)

--- a/src/apk_package.h
+++ b/src/apk_package.h
@@ -103,8 +103,8 @@ APK_ARRAY(apk_package_array, struct apk_
#define APK_PROVIDER_FROM_PACKAGE(pkg) (struct apk_provider){(pkg),(pkg)->version}
#define APK_PROVIDER_FROM_PROVIDES(pkg,p) (struct apk_provider){(pkg),(p)->version}

-#define PKG_VER_FMT "%s-" BLOB_FMT
-#define PKG_VER_PRINTF(pkg) pkg->name->name, BLOB_PRINTF(*pkg->version)
+#define PKG_VER_FMT "%s_" BLOB_FMT "_" BLOB_FMT
+#define PKG_VER_PRINTF(pkg) pkg->name->name, BLOB_PRINTF(*pkg->version), BLOB_PRINTF(*pkg->arch)
#define PKG_FILE_FMT PKG_VER_FMT ".apk"
#define PKG_FILE_PRINTF(pkg) PKG_VER_PRINTF(pkg)

--- a/src/app_mkpkg.c
+++ b/src/app_mkpkg.c
@@ -283,7 +283,7 @@ static char *pkgi_filename(struct adb_ob
{
apk_blob_t to = APK_BLOB_PTR_LEN(buf, n);
apk_blob_push_blob(&to, adb_ro_blob(pkgi, ADBI_PI_NAME));
- apk_blob_push_blob(&to, APK_BLOB_STR("-"));
+ apk_blob_push_blob(&to, APK_BLOB_STR("_"));
apk_blob_push_blob(&to, adb_ro_blob(pkgi, ADBI_PI_VERSION));
apk_blob_push_blob(&to, APK_BLOB_STR(".apk"));
apk_blob_push_blob(&to, APK_BLOB_PTR_LEN("", 1));
--- a/src/database.c
+++ b/src/database.c
@@ -589,10 +589,12 @@ struct apk_package *apk_db_pkg_add(struc

static int apk_pkg_format_cache_pkg(apk_blob_t to, struct apk_package *pkg)
{
- /* pkgname-1.0_alpha1.12345678.apk */
+ /* pkgname_1.0_alpha1.12345678.apk */
apk_blob_push_blob(&to, APK_BLOB_STR(pkg->name->name));
- apk_blob_push_blob(&to, APK_BLOB_STR("-"));
+ apk_blob_push_blob(&to, APK_BLOB_STR("_"));
apk_blob_push_blob(&to, *pkg->version);
+ apk_blob_push_blob(&to, APK_BLOB_STR("_"));
+ apk_blob_push_blob(&to, *pkg->arch);
apk_blob_push_blob(&to, APK_BLOB_STR("."));
apk_blob_push_hexdump(&to, APK_BLOB_PTR_LEN((char *) pkg->csum.data,
APK_CACHE_CSUM_BYTES));
56 changes: 56 additions & 0 deletions package/system/apk/patches/0003-fix-macOS-compilation.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
From f3875f6ec84c3f7c704a8d9cc050e8a496893b1c Mon Sep 17 00:00:00 2001
From: Paul Spooren <[email protected]>
Date: Sat, 16 Mar 2024 18:20:46 +0100
Subject: [PATCH] fix macOS compilation

Pasting these bits together makes it work on macOS again.

Signed-off-by: Paul Spooren <[email protected]>
---
libfetch/common.h | 6 ++++++
src/adb.c | 12 ++++++++++++
2 files changed, 18 insertions(+)

diff --git a/libfetch/common.h b/libfetch/common.h
index 2c16bf7..42a8f0b 100644
--- a/libfetch/common.h
+++ b/libfetch/common.h
@@ -32,6 +32,12 @@
#ifndef _COMMON_H_INCLUDED
#define _COMMON_H_INCLUDED

+#ifndef SOCK_NONBLOCK
+#include <fcntl.h>
+#define SOCK_NONBLOCK O_NONBLOCK
+#define SOCK_CLOEXEC O_CLOEXEC
+#endif
+
#define FTP_DEFAULT_PORT 21
#define HTTP_DEFAULT_PORT 80
#define HTTPS_DEFAULT_PORT 443
diff --git a/src/adb.c b/src/adb.c
index fe614af..70ce75b 100644
--- a/src/adb.c
+++ b/src/adb.c
@@ -952,6 +952,18 @@ adb_val_t adb_wo_arr(struct adb_obj *o, unsigned i, struct adb_obj *no)
return adb_wo_val(o, i, adb_w_arr(no));
}

+#ifndef MUL_NO_OVERFLOW
+#define MUL_NO_OVERFLOW ((size_t)1 << (sizeof(size_t) * 4))
+#endif
+void *reallocarray(void *optr, size_t nmemb, size_t size) {
+ if ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
+ nmemb > 0 && SIZE_MAX / nmemb < size) {
+ errno = ENOMEM;
+ return NULL;
+ }
+ return realloc(optr, size * nmemb);
+}
+
adb_val_t adb_wa_append(struct adb_obj *o, adb_val_t v)
{
assert(o->schema->kind == ADB_KIND_ARRAY);
--
2.44.0

0 comments on commit 74eaade

Please sign in to comment.