xref: /DragonOS/user/Makefile (revision 26887c6334cdca2d13ad71dec27fb69faa0a57be)
1user_sub_dirs = apps
2
3SUBDIR_ROOTS := .
4DIRS := . $(shell find $(SUBDIR_ROOTS) -type d)
5GARBAGE_PATTERNS := *.o sys_api_lib *.a
6GARBAGE := $(foreach DIR,$(DIRS),$(addprefix $(DIR)/,$(GARBAGE_PATTERNS)))
7
8
9tmp_output_dir=$(ROOT_PATH)/bin/tmp/user
10output_dir=$(ROOT_PATH)/bin/user
11
12CFLAGS := $(GLOBAL_CFLAGS) -I $(shell pwd)/libs -I $(shell pwd)/libs/libc/src/include -I $(shell pwd)/libs/libc/src/include/export
13current_CFLAGS := $(CFLAGS)
14
15DADK_VERSION=$(shell dadk -V | awk 'END {print $$2}')
16# 最小的DADK版本
17MIN_DADK_VERSION = 0.1.2
18DADK_CACHE_DIR = $(ROOT_PATH)/bin/dadk_cache
19
20# 旧版的libc安装路径
21OLD_LIBC_INSTALL_PATH=$(ROOT_PATH)/bin/sysroot/usr/old_libc
22
23ECHO:
24	@echo "$@"
25
26
27install_dadk:
28# 如果未安装
29ifeq ("$(DADK_VERSION)", "")
30	@echo "\ndadk is not installed."
31	@echo "Please install dadk $(MIN_DADK_VERSION) or higher version"
32	@echo "\nYou can install dadk by running the following command:"
33	@echo "\n\tcargo install dadk"
34	@echo "\nOr you can install dadk from source by running the following command:"
35	@echo "\n\tcargo install --git https://github.com/DragonOS-Community/DADK.git"
36	@echo "\n"
37	@echo "Auto installing dadk..."
38	CC=gcc AS=as AR=ar LD=ld OBJCOPY=objcopy NM=nm cargo install dadk
39else
40# 如果DADK版本过低,则自动更新
41	@echo "dadk version $(DADK_VERSION) installed"
42# 如果DADK版本过低,则自动更新
43ifneq ($(shell printf '%s\n%s' "$(DADK_VERSION)" "$(MIN_DADK_VERSION)" | sort -V | head -n1), $(MIN_DADK_VERSION))
44	@echo "dadk version is too low, please update to $(MIN_DADK_VERSION) or higher version"
45	@echo "Updating dadk..."
46	CC=gcc AS=as AR=ar LD=ld OBJCOPY=objcopy NM=nm cargo install dadk || (echo "dadk update failed" && exit 1)
47	@echo "dadk updated"
48endif
49endif
50
51dadk_run: install_dadk
52	mkdir -p $(DADK_CACHE_DIR)
53# 之所以在这里临时设置ARCH为空,是因为如果要设置这个环境变量,应当在DADK的配置文件中设置
54	ARCH= dadk --config-dir dadk/config --cache-dir $(DADK_CACHE_DIR) --dragonos-dir $(ROOT_PATH)/bin/sysroot build
55	ARCH= dadk --config-dir dadk/config --cache-dir $(DADK_CACHE_DIR) --dragonos-dir $(ROOT_PATH)/bin/sysroot install
56
57dadk_clean: install_dadk
58	dadk --config-dir dadk/config --cache-dir $(DADK_CACHE_DIR) --dragonos-dir $(ROOT_PATH)/bin/sysroot clean
59
60$(user_sub_dirs): ECHO sys_api_lib
61
62	$(MAKE) -C $@ all CFLAGS="$(CFLAGS)" tmp_output_dir="$(tmp_output_dir)" output_dir="$(output_dir)" sys_libs_dir="$(shell pwd)/libs"
63
64app: $(user_sub_dirs)
65	$(MAKE) dadk_run
66
67all: install_dadk app
68
69	$(shell if [ ! -e $(tmp_output_dir) ];then mkdir -p $(tmp_output_dir); fi)
70	$(shell if [ ! -e $(output_dir) ];then mkdir -p $(output_dir); fi)
71
72	@echo 用户态程序编译完成
73
74make_output_dir: ECHO
75	mkdir -p $(ROOT_PATH)/bin/user/
76	mkdir -p $(ROOT_PATH)/bin/tmp/user
77	mkdir -p $(ROOT_PATH)/bin/sysroot/usr/include
78	mkdir -p $(ROOT_PATH)/bin/sysroot/usr/lib
79
80	$(shell if [ ! -e $(tmp_output_dir) ];then mkdir -p $(tmp_output_dir); fi)
81	$(shell if [ ! -e $(output_dir) ];then mkdir -p $(output_dir); fi)
82# 系统库
83
84sys_api_lib_stage_1: make_output_dir
85	@echo Building sys_api_lib...
86	$(MAKE) -C libs all CFLAGS="$(CFLAGS)" tmp_output_dir="$(tmp_output_dir)" output_dir="$(output_dir)" sys_libs_dir="$(shell pwd)/libs"
87
88
89sys_api_lib: sys_api_lib_stage_1
90
91# 打包系统库
92	mkdir -p $(ROOT_PATH)/bin/tmp/user/sys_api_lib_build_tmp
93	mkdir -p $(OLD_LIBC_INSTALL_PATH)/include
94	mkdir -p $(OLD_LIBC_INSTALL_PATH)/lib
95	$(AR) x $(ROOT_PATH)/user/libs/libc/target/x86_64-unknown-none/release/liblibc.a --output=$(ROOT_PATH)/bin/tmp/user/sys_api_lib_build_tmp
96	$(AR) crvs $(OLD_LIBC_INSTALL_PATH)/lib/libc.a $(shell find ./libs/* -name "*.o") $(shell find $(ROOT_PATH)/bin/tmp/user/sys_api_lib_build_tmp/* -name "*.o")
97	rm -rf $(ROOT_PATH)/bin/tmp/user/sys_api_lib_build_tmp
98# $(shell find ./libs/* -name "*.o" | xargs -I {} cp {} $(ROOT_PATH)/bin/sysroot/usr/lib/)
99	$(shell cp -r $(ROOT_PATH)/user/libs/libc/src/include/export/* $(OLD_LIBC_INSTALL_PATH)/include/)
100	$(shell cp -r $(ROOT_PATH)/user/libs/libc/src/arch/x86_64/c*.o $(OLD_LIBC_INSTALL_PATH)/lib/)
101
102clean:
103	rm -rf $(GARBAGE)
104	$(MAKE) dadk_clean
105	$(MAKE) clean -C libs
106	@list='$(user_sub_dirs)'; for subdir in $$list; do \
107		echo "Clean in dir: $$subdir";\
108		cd $$subdir && $(MAKE) clean;\
109		cd .. ;\
110	done
111