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