Skip to content

Commit ce4b22d

Browse files
user: add static mbedtls-backed https curl
1 parent c179a54 commit ce4b22d

File tree

5 files changed

+130
-9
lines changed

5 files changed

+130
-9
lines changed

user/apps/curl/Makefile

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ curl_dir := $(build_dir)/curl-$(curl_version)
77
host := $(ARCH)-linux-musl
88
prefix := $(host)-
99
bin := $(build_dir)/curl
10+
mbedtls_root := $(abspath $(build_dir)/mbedtls-root)
11+
mbedtls_prefix := $(mbedtls_root)/usr
12+
ca_bundle_src := /etc/ssl/certs/ca-certificates.crt
13+
ca_bundle_dst := etc/ssl/certs/ca-certificates.crt
1014

1115
cc := $(prefix)gcc
1216
strip := $(prefix)strip
@@ -35,35 +39,39 @@ configure_args := \
3539
--disable-telnet \
3640
--disable-tftp \
3741
--disable-websockets \
38-
--without-ssl \
3942
--without-openssl \
4043
--without-gnutls \
41-
--without-mbedtls \
44+
--with-mbedtls=$(mbedtls_prefix) \
4245
--without-rustls \
4346
--without-bearssl \
4447
--without-wolfssl \
4548
--without-libpsl \
4649
--without-brotli \
4750
--without-zstd \
4851
--without-zlib \
49-
--without-ca-bundle \
52+
--with-ca-bundle=/$(ca_bundle_dst) \
5053
--without-ca-path
5154

5255
$(curl_tarball_path):
53-
@if ! wget "$(curl_url_primary)"; then \
56+
@if ! env -u http_proxy -u https_proxy -u HTTP_PROXY -u HTTPS_PROXY -u ALL_PROXY -u all_proxy wget "$(curl_url_primary)"; then \
5457
rm -f "$(curl_tarball_path)"; \
55-
wget "$(curl_url_fallback)"; \
58+
env -u http_proxy -u https_proxy -u HTTP_PROXY -u HTTPS_PROXY -u ALL_PROXY -u all_proxy wget "$(curl_url_fallback)"; \
5659
fi
5760

5861
$(curl_dir): $(curl_tarball_path)
5962
mkdir -p $(build_dir)
6063
tar -xJf $< -C $(build_dir)
6164

62-
$(bin): $(curl_dir)
65+
$(mbedtls_prefix)/lib/libmbedtls.a:
66+
$(MAKE) -C ../mbedtls ARCH=$(ARCH) install DADK_CURRENT_BUILD_DIR=$(mbedtls_root)
67+
68+
$(bin): $(mbedtls_prefix)/lib/libmbedtls.a $(curl_dir)
6369
cd $(curl_dir) && \
6470
PKG_CONFIG=/bin/false \
6571
CC="$(cc)" AR="$(ar)" RANLIB="$(ranlib)" STRIP="$(strip)" \
66-
CFLAGS="-Os -static" LDFLAGS="-static" \
72+
CPPFLAGS="-I$(mbedtls_prefix)/include" \
73+
CFLAGS="-Os -static" \
74+
LDFLAGS="-static -L$(mbedtls_prefix)/lib" \
6775
./configure $(configure_args)
6876
cd $(curl_dir) && \
6977
$(MAKE) CURL_LDFLAGS_BIN="-all-static" -j$(nproc)
@@ -76,7 +84,10 @@ $(bin): $(curl_dir)
7684
all: $(bin)
7785

7886
install: all
79-
cp $(bin) $(DADK_CURRENT_BUILD_DIR)/curl
87+
mkdir -p $(DADK_CURRENT_BUILD_DIR)/bin
88+
cp $(bin) $(DADK_CURRENT_BUILD_DIR)/bin/curl
89+
mkdir -p $(DADK_CURRENT_BUILD_DIR)/$(dir $(ca_bundle_dst))
90+
cp $(ca_bundle_src) $(DADK_CURRENT_BUILD_DIR)/$(ca_bundle_dst)
8091

8192
clean:
8293
rm -rf build/

user/apps/mbedtls/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
build/
2+
*tar.gz
3+
patches/

user/apps/mbedtls/Makefile

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
ARCH ?= x86_64
2+
self_dir := $(dir $(abspath $(lastword $(MAKEFILE_LIST))))
3+
mbedtls_version := 2.28.0
4+
mbedtls_tarball := mbedtls_$(mbedtls_version).orig.tar.gz
5+
mbedtls_tarball_path := $(mbedtls_tarball)
6+
build_dir := build/$(ARCH)
7+
mbedtls_dir := $(build_dir)/mbedtls-$(mbedtls_version)
8+
mbedtls_build_dir := $(build_dir)/cmake-build
9+
stage_dir := $(build_dir)/stage
10+
host := $(ARCH)-linux-musl
11+
prefix := $(host)-
12+
13+
cc := $(prefix)gcc
14+
ar := $(prefix)ar
15+
ranlib := $(prefix)ranlib
16+
gcc_ar := $(prefix)gcc-ar
17+
gcc_ranlib := $(prefix)gcc-ranlib
18+
download_env := env -u http_proxy -u https_proxy -u HTTP_PROXY -u HTTPS_PROXY -u ALL_PROXY -u all_proxy
19+
20+
mbedtls_url_primary := http://archive.ubuntu.com/ubuntu/pool/universe/m/mbedtls/$(mbedtls_tarball)
21+
mbedtls_url_fallback := https://github.com/Mbed-TLS/mbedtls/archive/refs/tags/mbedtls-$(mbedtls_version).tar.gz
22+
23+
cmake_args := \
24+
-G Ninja \
25+
-D CMAKE_SYSTEM_NAME=Linux \
26+
-D CMAKE_C_COMPILER=$(abspath $(shell command -v $(cc))) \
27+
-D CMAKE_C_COMPILER_AR=$(abspath $(shell command -v $(gcc_ar))) \
28+
-D CMAKE_C_COMPILER_RANLIB=$(abspath $(shell command -v $(gcc_ranlib))) \
29+
-D CMAKE_AR=$(abspath $(shell command -v $(ar))) \
30+
-D CMAKE_RANLIB=$(abspath $(shell command -v $(ranlib))) \
31+
-D CMAKE_C_FLAGS=-Os\ -fPIC \
32+
-D CMAKE_INSTALL_PREFIX=/usr \
33+
-D ENABLE_PROGRAMS=Off \
34+
-D ENABLE_TESTING=Off \
35+
-D USE_SHARED_MBEDTLS_LIBRARY=Off
36+
37+
$(mbedtls_tarball_path):
38+
@tmp_file="$@.tmp"; \
39+
rm -f "$$tmp_file"; \
40+
if ! $(download_env) curl -fL --retry 5 --retry-delay 1 -o "$$tmp_file" "$(mbedtls_url_primary)"; then \
41+
rm -f "$$tmp_file"; \
42+
$(download_env) curl -fL --retry 5 --retry-delay 1 -o "$$tmp_file" "$(mbedtls_url_fallback)"; \
43+
fi; \
44+
mv "$$tmp_file" "$@"
45+
46+
$(mbedtls_dir): $(mbedtls_tarball_path)
47+
mkdir -p $(build_dir)
48+
rm -rf $(mbedtls_dir)
49+
mkdir -p $(mbedtls_dir)
50+
tar -xzf $< -C $(mbedtls_dir) --strip-components=1
51+
cd $(mbedtls_dir) && patch -p1 < $(self_dir)/patches/0001-linux-musl-use-getrandom.patch
52+
53+
$(stage_dir)/usr/lib/libmbedtls.a: $(mbedtls_dir)
54+
mkdir -p $(mbedtls_build_dir)
55+
cd $(mbedtls_build_dir) && \
56+
cmake $(cmake_args) $(abspath $(mbedtls_dir))
57+
cd $(mbedtls_build_dir) && \
58+
cmake --build . -j$(nproc)
59+
rm -rf $(stage_dir)
60+
cd $(mbedtls_build_dir) && \
61+
DESTDIR=$(abspath $(stage_dir)) cmake --install .
62+
63+
.PHONY: all install clean distclean
64+
65+
all: $(stage_dir)/usr/lib/libmbedtls.a
66+
67+
install: all
68+
mkdir -p $(DADK_CURRENT_BUILD_DIR)/usr
69+
cp -a $(stage_dir)/usr/. $(DADK_CURRENT_BUILD_DIR)/usr/
70+
71+
clean:
72+
rm -rf build/
73+
74+
distclean: clean
75+
rm -f $(mbedtls_tarball_path)

user/dadk/config/all/mbedtls.toml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# 用户程序名称
2+
name = "mbedtls"
3+
# 版本号
4+
version = "2.28.0"
5+
# 用户程序描述信息
6+
description = "mbedtls static musl build"
7+
# (可选)是否只构建一次,如果为true,DADK会在构建成功后,将构建结果缓存起来,下次构建时,直接使用缓存的构建结果
8+
build-once = false
9+
# (可选) 是否只安装一次,如果为true,DADK会在安装成功后,不再重复安装
10+
install-once = false
11+
# 目标架构
12+
# 可选值:"x86_64", "aarch64", "riscv64"
13+
target-arch = ["x86_64"]
14+
# 任务源
15+
[task-source]
16+
# 构建类型
17+
# 可选值:"build-from-source", "install-from-prebuilt"
18+
type = "build-from-source"
19+
# 构建来源
20+
# "build_from_source" 可选值:"git", "local", "archive"
21+
# "install_from_prebuilt" 可选值:"local", "archive"
22+
source = "local"
23+
# 路径或URL
24+
source-path = "user/apps/mbedtls"
25+
[build]
26+
build-command = "make install"
27+
[clean]
28+
clean-command = "make distclean"
29+
# 安装相关信息
30+
[install]
31+
# (可选)安装到DragonOS的路径
32+
in-dragonos-path = "/"

user/dadk/config/curl.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,4 @@ clean-command = "make distclean"
2929
# 安装相关信息
3030
[install]
3131
# (可选)安装到DragonOS的路径
32-
in-dragonos-path = "/bin"
32+
in-dragonos-path = "/"

0 commit comments

Comments
 (0)