xref: /DragonOS/user/Makefile (revision ab5c8ca46db8e7d4793a9791292122b0b9684274)
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.1
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	@exit 1
38else
39	@echo "dadk version $(DADK_VERSION) installed"
40endif
41
42dadk_run: install_dadk
43	mkdir -p $(DADK_CACHE_DIR)
44# 之所以在这里临时设置ARCH为空,是因为如果要设置这个环境变量,应当在DADK的配置文件中设置
45	ARCH= dadk --config-dir dadk/config --cache-dir $(DADK_CACHE_DIR) --dragonos-dir $(ROOT_PATH)/bin/sysroot build
46	ARCH= dadk --config-dir dadk/config --cache-dir $(DADK_CACHE_DIR) --dragonos-dir $(ROOT_PATH)/bin/sysroot install
47
48dadk_clean: install_dadk
49	dadk --config-dir dadk/config --cache-dir $(DADK_CACHE_DIR) --dragonos-dir $(ROOT_PATH)/bin/sysroot clean
50
51$(user_sub_dirs): ECHO sys_api_lib
52
53	$(MAKE) -C $@ all CFLAGS="$(CFLAGS)" tmp_output_dir="$(tmp_output_dir)" output_dir="$(output_dir)" sys_libs_dir="$(shell pwd)/libs"
54
55app: $(user_sub_dirs)
56	$(MAKE) dadk_run
57
58all: install_dadk app
59
60	$(shell if [ ! -e $(tmp_output_dir) ];then mkdir -p $(tmp_output_dir); fi)
61	$(shell if [ ! -e $(output_dir) ];then mkdir -p $(output_dir); fi)
62
63	@echo 用户态程序编译完成
64
65make_output_dir: ECHO
66	mkdir -p $(ROOT_PATH)/bin/user/
67	mkdir -p $(ROOT_PATH)/bin/tmp/user
68	mkdir -p $(ROOT_PATH)/bin/sysroot/usr/include
69	mkdir -p $(ROOT_PATH)/bin/sysroot/usr/lib
70
71	$(shell if [ ! -e $(tmp_output_dir) ];then mkdir -p $(tmp_output_dir); fi)
72	$(shell if [ ! -e $(output_dir) ];then mkdir -p $(output_dir); fi)
73# 系统库
74
75sys_api_lib_stage_1: make_output_dir
76	@echo Building sys_api_lib...
77	$(MAKE) -C libs all CFLAGS="$(CFLAGS)" tmp_output_dir="$(tmp_output_dir)" output_dir="$(output_dir)" sys_libs_dir="$(shell pwd)/libs"
78
79
80sys_api_lib: sys_api_lib_stage_1
81
82# 打包系统库
83	mkdir -p $(ROOT_PATH)/bin/tmp/user/sys_api_lib_build_tmp
84	mkdir -p $(OLD_LIBC_INSTALL_PATH)/include
85	mkdir -p $(OLD_LIBC_INSTALL_PATH)/lib
86	$(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
87	$(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")
88	rm -rf $(ROOT_PATH)/bin/tmp/user/sys_api_lib_build_tmp
89# $(shell find ./libs/* -name "*.o" | xargs -I {} cp {} $(ROOT_PATH)/bin/sysroot/usr/lib/)
90	$(shell cp -r $(ROOT_PATH)/user/libs/libc/src/include/export/* $(OLD_LIBC_INSTALL_PATH)/include/)
91	$(shell cp -r $(ROOT_PATH)/user/libs/libc/src/arch/x86_64/c*.o $(OLD_LIBC_INSTALL_PATH)/lib/)
92
93clean:
94	rm -rf $(GARBAGE)
95	$(MAKE) dadk_clean
96	$(MAKE) clean -C libs
97	@list='$(user_sub_dirs)'; for subdir in $$list; do \
98		echo "Clean in dir: $$subdir";\
99		cd $$subdir && $(MAKE) clean;\
100		cd .. ;\
101	done
102