xref: /DragonOS/user/Makefile (revision bb0e4d4131046a69bbccdc0cdf1d5db51a2c6126)
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.3
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
51.PHONY: dadk_run
52dadk_run: install_dadk
53	mkdir -p $(DADK_CACHE_DIR)
54# 之所以在这里临时设置ARCH为空,是因为如果要设置这个环境变量,应当在DADK的配置文件中设置
55	ARCH= dadk --config-dir dadk/config --cache-dir $(DADK_CACHE_DIR) --dragonos-dir $(ROOT_PATH)/bin/sysroot build
56	ARCH= dadk --config-dir dadk/config --cache-dir $(DADK_CACHE_DIR) --dragonos-dir $(ROOT_PATH)/bin/sysroot install
57
58.PHONY: dadk_clean
59dadk_clean: install_dadk
60# 不运行dadk clean的原因是,把clean的工作交给应用程序自己去做,这样可以节省编译时间
61#dadk --config-dir dadk/config --cache-dir $(DADK_CACHE_DIR) --dragonos-dir $(ROOT_PATH)/bin/sysroot clean src
62#dadk --config-dir dadk/config --cache-dir $(DADK_CACHE_DIR) --dragonos-dir $(ROOT_PATH)/bin/sysroot clean target
63
64$(user_sub_dirs): ECHO sys_api_lib
65
66	$(MAKE) -C $@ all CFLAGS="$(CFLAGS)" tmp_output_dir="$(tmp_output_dir)" output_dir="$(output_dir)" sys_libs_dir="$(shell pwd)/libs"
67
68app: $(user_sub_dirs) dadk_run
69
70
71all: make_output_dir
72
73	$(MAKE) app
74
75	@echo 用户态程序编译完成
76
77make_output_dir: ECHO
78	mkdir -p $(ROOT_PATH)/bin/user/
79	mkdir -p $(ROOT_PATH)/bin/tmp/user
80	mkdir -p $(ROOT_PATH)/bin/sysroot/usr/include
81	mkdir -p $(ROOT_PATH)/bin/sysroot/usr/lib
82
83	$(shell if [ ! -e $(tmp_output_dir) ];then mkdir -p $(tmp_output_dir); fi)
84	$(shell if [ ! -e $(output_dir) ];then mkdir -p $(output_dir); fi)
85# 系统库
86
87sys_api_lib_stage_1: make_output_dir
88	@echo Building sys_api_lib...
89	$(MAKE) -C libs all CFLAGS="$(CFLAGS)" tmp_output_dir="$(tmp_output_dir)" output_dir="$(output_dir)" sys_libs_dir="$(shell pwd)/libs"
90
91
92sys_api_lib: sys_api_lib_stage_1
93
94# 打包系统库
95	mkdir -p $(ROOT_PATH)/bin/tmp/user/sys_api_lib_build_tmp
96	mkdir -p $(OLD_LIBC_INSTALL_PATH)/include
97	mkdir -p $(OLD_LIBC_INSTALL_PATH)/lib
98	$(AR) x libs/libc/target/x86_64-unknown-none/release/liblibc.a --output=$(ROOT_PATH)/bin/tmp/user/sys_api_lib_build_tmp
99	$(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")
100	rm -rf $(ROOT_PATH)/bin/tmp/user/sys_api_lib_build_tmp
101# $(shell find ./libs/* -name "*.o" | xargs -I {} cp {} $(ROOT_PATH)/bin/sysroot/usr/lib/)
102	$(shell cp -r $(ROOT_PATH)/user/libs/libc/src/include/export/* $(OLD_LIBC_INSTALL_PATH)/include/)
103	$(shell cp -r $(ROOT_PATH)/user/libs/libc/src/arch/x86_64/c*.o $(OLD_LIBC_INSTALL_PATH)/lib/)
104
105.PHONY: clean
106clean:
107	rm -rf $(GARBAGE)
108	$(MAKE) dadk_clean
109	$(MAKE) clean -C libs
110	@list='$(user_sub_dirs)'; for subdir in $$list; do \
111		echo "Clean in dir: $$subdir";\
112		cd $$subdir && $(MAKE) clean;\
113		cd .. ;\
114	done
115
116.PHONY: fmt
117fmt:
118	FMT_CHECK=$(FMT_CHECK) $(MAKE) -C libs
119	FMT_CHECK=$(FMT_CHECK) $(MAKE) -C apps
120