xref: /DragonOS/user/Makefile (revision 9550910ae1de900e0291a84d268e8873fa142902)
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	@echo "dadk version $(DADK_VERSION) installed"
41# 如果DADK版本过低,则自动更新
42ifeq ($(shell printf '%s\n' "$(DADK_VERSION)" "$(MIN_DADK_VERSION)" | sort -V | head -n1), $(MIN_DADK_VERSION))
43	@echo "dadk version is too low, please update to $(MIN_DADK_VERSION) or higher version"
44	@echo "Updating dadk..."
45	CC=gcc AS=as AR=ar LD=ld OBJCOPY=objcopy NM=nm cargo install dadk || (echo "dadk update failed" && exit 1)
46	@echo "dadk updated"
47endif
48endif
49
50dadk_run: install_dadk
51	mkdir -p $(DADK_CACHE_DIR)
52# 之所以在这里临时设置ARCH为空,是因为如果要设置这个环境变量,应当在DADK的配置文件中设置
53	ARCH= dadk --config-dir dadk/config --cache-dir $(DADK_CACHE_DIR) --dragonos-dir $(ROOT_PATH)/bin/sysroot build
54	ARCH= dadk --config-dir dadk/config --cache-dir $(DADK_CACHE_DIR) --dragonos-dir $(ROOT_PATH)/bin/sysroot install
55
56dadk_clean: install_dadk
57	dadk --config-dir dadk/config --cache-dir $(DADK_CACHE_DIR) --dragonos-dir $(ROOT_PATH)/bin/sysroot clean
58
59$(user_sub_dirs): ECHO sys_api_lib
60
61	$(MAKE) -C $@ all CFLAGS="$(CFLAGS)" tmp_output_dir="$(tmp_output_dir)" output_dir="$(output_dir)" sys_libs_dir="$(shell pwd)/libs"
62
63app: $(user_sub_dirs)
64	$(MAKE) dadk_run
65
66all: install_dadk app
67
68	$(shell if [ ! -e $(tmp_output_dir) ];then mkdir -p $(tmp_output_dir); fi)
69	$(shell if [ ! -e $(output_dir) ];then mkdir -p $(output_dir); fi)
70
71	@echo 用户态程序编译完成
72
73make_output_dir: ECHO
74	mkdir -p $(ROOT_PATH)/bin/user/
75	mkdir -p $(ROOT_PATH)/bin/tmp/user
76	mkdir -p $(ROOT_PATH)/bin/sysroot/usr/include
77	mkdir -p $(ROOT_PATH)/bin/sysroot/usr/lib
78
79	$(shell if [ ! -e $(tmp_output_dir) ];then mkdir -p $(tmp_output_dir); fi)
80	$(shell if [ ! -e $(output_dir) ];then mkdir -p $(output_dir); fi)
81# 系统库
82
83sys_api_lib_stage_1: make_output_dir
84	@echo Building sys_api_lib...
85	$(MAKE) -C libs all CFLAGS="$(CFLAGS)" tmp_output_dir="$(tmp_output_dir)" output_dir="$(output_dir)" sys_libs_dir="$(shell pwd)/libs"
86
87
88sys_api_lib: sys_api_lib_stage_1
89
90# 打包系统库
91	mkdir -p $(ROOT_PATH)/bin/tmp/user/sys_api_lib_build_tmp
92	mkdir -p $(OLD_LIBC_INSTALL_PATH)/include
93	mkdir -p $(OLD_LIBC_INSTALL_PATH)/lib
94	$(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
95	$(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")
96	rm -rf $(ROOT_PATH)/bin/tmp/user/sys_api_lib_build_tmp
97# $(shell find ./libs/* -name "*.o" | xargs -I {} cp {} $(ROOT_PATH)/bin/sysroot/usr/lib/)
98	$(shell cp -r $(ROOT_PATH)/user/libs/libc/src/include/export/* $(OLD_LIBC_INSTALL_PATH)/include/)
99	$(shell cp -r $(ROOT_PATH)/user/libs/libc/src/arch/x86_64/c*.o $(OLD_LIBC_INSTALL_PATH)/lib/)
100
101clean:
102	rm -rf $(GARBAGE)
103	$(MAKE) dadk_clean
104	$(MAKE) clean -C libs
105	@list='$(user_sub_dirs)'; for subdir in $$list; do \
106		echo "Clean in dir: $$subdir";\
107		cd $$subdir && $(MAKE) clean;\
108		cd .. ;\
109	done
110