xref: /DragonOS/user/Makefile (revision 26887c6334cdca2d13ad71dec27fb69faa0a57be)
1b2614801Sfslongjinuser_sub_dirs = apps
20aec6827Sfslongjin
30aec6827SfslongjinSUBDIR_ROOTS := .
40aec6827SfslongjinDIRS := . $(shell find $(SUBDIR_ROOTS) -type d)
57285c927SloginGARBAGE_PATTERNS := *.o sys_api_lib *.a
60aec6827SfslongjinGARBAGE := $(foreach DIR,$(DIRS),$(addprefix $(DIR)/,$(GARBAGE_PATTERNS)))
70aec6827Sfslongjin
80aec6827Sfslongjin
9afeca182Sfslongjintmp_output_dir=$(ROOT_PATH)/bin/tmp/user
10afeca182Sfslongjinoutput_dir=$(ROOT_PATH)/bin/user
11afeca182Sfslongjin
122bf5ee0eSloginCFLAGS := $(GLOBAL_CFLAGS) -I $(shell pwd)/libs -I $(shell pwd)/libs/libc/src/include -I $(shell pwd)/libs/libc/src/include/export
13afeca182Sfslongjincurrent_CFLAGS := $(CFLAGS)
14b2614801Sfslongjin
15*26887c63SLoGinDADK_VERSION=$(shell dadk -V | awk 'END {print $$2}')
167285c927Slogin# 最小的DADK版本
17abf3f634SLoGinMIN_DADK_VERSION = 0.1.2
187285c927SloginDADK_CACHE_DIR = $(ROOT_PATH)/bin/dadk_cache
197285c927Slogin
207285c927Slogin# 旧版的libc安装路径
217285c927SloginOLD_LIBC_INSTALL_PATH=$(ROOT_PATH)/bin/sysroot/usr/old_libc
227285c927Slogin
23b2614801SfslongjinECHO:
24b2614801Sfslongjin	@echo "$@"
25b2614801Sfslongjin
267285c927Slogin
277285c927Slogininstall_dadk:
287285c927Slogin# 如果未安装
297285c927Sloginifeq ("$(DADK_VERSION)", "")
307285c927Slogin	@echo "\ndadk is not installed."
317285c927Slogin	@echo "Please install dadk $(MIN_DADK_VERSION) or higher version"
327285c927Slogin	@echo "\nYou can install dadk by running the following command:"
337285c927Slogin	@echo "\n\tcargo install dadk"
347285c927Slogin	@echo "\nOr you can install dadk from source by running the following command:"
357285c927Slogin	@echo "\n\tcargo install --git https://github.com/DragonOS-Community/DADK.git"
367285c927Slogin	@echo "\n"
37abf3f634SLoGin	@echo "Auto installing dadk..."
38abf3f634SLoGin	CC=gcc AS=as AR=ar LD=ld OBJCOPY=objcopy NM=nm cargo install dadk
397285c927Sloginelse
40*26887c63SLoGin# 如果DADK版本过低,则自动更新
417285c927Slogin	@echo "dadk version $(DADK_VERSION) installed"
42abf3f634SLoGin# 如果DADK版本过低,则自动更新
43*26887c63SLoGinifneq ($(shell printf '%s\n%s' "$(DADK_VERSION)" "$(MIN_DADK_VERSION)" | sort -V | head -n1), $(MIN_DADK_VERSION))
44abf3f634SLoGin	@echo "dadk version is too low, please update to $(MIN_DADK_VERSION) or higher version"
45abf3f634SLoGin	@echo "Updating dadk..."
46abf3f634SLoGin	CC=gcc AS=as AR=ar LD=ld OBJCOPY=objcopy NM=nm cargo install dadk || (echo "dadk update failed" && exit 1)
47abf3f634SLoGin	@echo "dadk updated"
48abf3f634SLoGinendif
497285c927Sloginendif
507285c927Slogin
517285c927Slogindadk_run: install_dadk
527285c927Slogin	mkdir -p $(DADK_CACHE_DIR)
537285c927Slogin# 之所以在这里临时设置ARCH为空,是因为如果要设置这个环境变量,应当在DADK的配置文件中设置
547285c927Slogin	ARCH= dadk --config-dir dadk/config --cache-dir $(DADK_CACHE_DIR) --dragonos-dir $(ROOT_PATH)/bin/sysroot build
557285c927Slogin	ARCH= dadk --config-dir dadk/config --cache-dir $(DADK_CACHE_DIR) --dragonos-dir $(ROOT_PATH)/bin/sysroot install
567285c927Slogin
577285c927Slogindadk_clean: install_dadk
587285c927Slogin	dadk --config-dir dadk/config --cache-dir $(DADK_CACHE_DIR) --dragonos-dir $(ROOT_PATH)/bin/sysroot clean
597285c927Slogin
6056528782Sfslongjin$(user_sub_dirs): ECHO sys_api_lib
61b2614801Sfslongjin
62b2614801Sfslongjin	$(MAKE) -C $@ all CFLAGS="$(CFLAGS)" tmp_output_dir="$(tmp_output_dir)" output_dir="$(output_dir)" sys_libs_dir="$(shell pwd)/libs"
63b2614801Sfslongjin
6456528782Sfslongjinapp: $(user_sub_dirs)
657285c927Slogin	$(MAKE) dadk_run
66b2614801Sfslongjin
677285c927Sloginall: install_dadk app
682224c93eSguanjinquan
69e2a59dbdSfslongjin	$(shell if [ ! -e $(tmp_output_dir) ];then mkdir -p $(tmp_output_dir); fi)
70e2a59dbdSfslongjin	$(shell if [ ! -e $(output_dir) ];then mkdir -p $(output_dir); fi)
71e2a59dbdSfslongjin
7256528782Sfslongjin	@echo 用户态程序编译完成
73e2a59dbdSfslongjin
742224c93eSguanjinquanmake_output_dir: ECHO
752224c93eSguanjinquan	mkdir -p $(ROOT_PATH)/bin/user/
762224c93eSguanjinquan	mkdir -p $(ROOT_PATH)/bin/tmp/user
772224c93eSguanjinquan	mkdir -p $(ROOT_PATH)/bin/sysroot/usr/include
782224c93eSguanjinquan	mkdir -p $(ROOT_PATH)/bin/sysroot/usr/lib
792224c93eSguanjinquan
802224c93eSguanjinquan	$(shell if [ ! -e $(tmp_output_dir) ];then mkdir -p $(tmp_output_dir); fi)
812224c93eSguanjinquan	$(shell if [ ! -e $(output_dir) ];then mkdir -p $(output_dir); fi)
82e2a59dbdSfslongjin# 系统库
832224c93eSguanjinquan
842224c93eSguanjinquansys_api_lib_stage_1: make_output_dir
852224c93eSguanjinquan	@echo Building sys_api_lib...
86b2614801Sfslongjin	$(MAKE) -C libs all CFLAGS="$(CFLAGS)" tmp_output_dir="$(tmp_output_dir)" output_dir="$(output_dir)" sys_libs_dir="$(shell pwd)/libs"
87afeca182Sfslongjin
880aec6827Sfslongjin
892224c93eSguanjinquansys_api_lib: sys_api_lib_stage_1
902bf5ee0eSlogin
912bf5ee0eSlogin# 打包系统库
922bf5ee0eSlogin	mkdir -p $(ROOT_PATH)/bin/tmp/user/sys_api_lib_build_tmp
937285c927Slogin	mkdir -p $(OLD_LIBC_INSTALL_PATH)/include
947285c927Slogin	mkdir -p $(OLD_LIBC_INSTALL_PATH)/lib
952bf5ee0eSlogin	$(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
967285c927Slogin	$(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")
972bf5ee0eSlogin	rm -rf $(ROOT_PATH)/bin/tmp/user/sys_api_lib_build_tmp
982bf5ee0eSlogin# $(shell find ./libs/* -name "*.o" | xargs -I {} cp {} $(ROOT_PATH)/bin/sysroot/usr/lib/)
997285c927Slogin	$(shell cp -r $(ROOT_PATH)/user/libs/libc/src/include/export/* $(OLD_LIBC_INSTALL_PATH)/include/)
1007285c927Slogin	$(shell cp -r $(ROOT_PATH)/user/libs/libc/src/arch/x86_64/c*.o $(OLD_LIBC_INSTALL_PATH)/lib/)
101afeca182Sfslongjin
1020aec6827Sfslongjinclean:
1030aec6827Sfslongjin	rm -rf $(GARBAGE)
1047285c927Slogin	$(MAKE) dadk_clean
105237e95c6Swwc-15172310230	$(MAKE) clean -C libs
106237e95c6Swwc-15172310230	@list='$(user_sub_dirs)'; for subdir in $$list; do \
107237e95c6Swwc-15172310230		echo "Clean in dir: $$subdir";\
108237e95c6Swwc-15172310230		cd $$subdir && $(MAKE) clean;\
109237e95c6Swwc-15172310230		cd .. ;\
110237e95c6Swwc-15172310230	done
111afeca182Sfslongjin