10d163575Sopenharmony_ci# Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
20d163575Sopenharmony_ci# Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
30d163575Sopenharmony_ci#
40d163575Sopenharmony_ci# Redistribution and use in source and binary forms, with or without modification,
50d163575Sopenharmony_ci# are permitted provided that the following conditions are met:
60d163575Sopenharmony_ci#
70d163575Sopenharmony_ci# 1. Redistributions of source code must retain the above copyright notice, this list of
80d163575Sopenharmony_ci#    conditions and the following disclaimer.
90d163575Sopenharmony_ci#
100d163575Sopenharmony_ci# 2. Redistributions in binary form must reproduce the above copyright notice, this list
110d163575Sopenharmony_ci#    of conditions and the following disclaimer in the documentation and/or other materials
120d163575Sopenharmony_ci#    provided with the distribution.
130d163575Sopenharmony_ci#
140d163575Sopenharmony_ci# 3. Neither the name of the copyright holder nor the names of its contributors may be used
150d163575Sopenharmony_ci#    to endorse or promote products derived from this software without specific prior written
160d163575Sopenharmony_ci#    permission.
170d163575Sopenharmony_ci#
180d163575Sopenharmony_ci# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
190d163575Sopenharmony_ci# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
200d163575Sopenharmony_ci# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
210d163575Sopenharmony_ci# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
220d163575Sopenharmony_ci# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
230d163575Sopenharmony_ci# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
240d163575Sopenharmony_ci# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
250d163575Sopenharmony_ci# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
260d163575Sopenharmony_ci# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
270d163575Sopenharmony_ci# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
280d163575Sopenharmony_ci# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
290d163575Sopenharmony_ci
300d163575Sopenharmony_ciLITEOSTOPDIR := $(realpath $(dir $(lastword $(MAKEFILE_LIST))))
310d163575Sopenharmony_ciexport LITEOSTOPDIR
320d163575Sopenharmony_ci
330d163575Sopenharmony_ciAPPS = apps
340d163575Sopenharmony_ciROOTFS = rootfs
350d163575Sopenharmony_ciLITEOS_TARGET = liteos
360d163575Sopenharmony_ciLITEOS_LIBS_TARGET = libs
370d163575Sopenharmony_ciKCONFIG_CMDS := $(notdir $(wildcard $(dir $(shell which menuconfig))*config))
380d163575Sopenharmony_ci
390d163575Sopenharmony_ciohos_kernel ?= liteos_a
400d163575Sopenharmony_ci$(foreach line,$(shell hb env | sed 's/\[OHOS INFO\]/ohos/g;s/ /_/g;s/:_/=/g' || true),$(eval $(line)))
410d163575Sopenharmony_ciifneq ($(ohos_kernel),liteos_a)
420d163575Sopenharmony_ci$(error The selected product ($(ohos_product)) is not a liteos_a kernel type product)
430d163575Sopenharmony_ciendif
440d163575Sopenharmony_ci
450d163575Sopenharmony_ciifeq ($(PRODUCT_PATH),)
460d163575Sopenharmony_ciPRODUCT_PATH:=$(ohos_product_path)
470d163575Sopenharmony_ciendif
480d163575Sopenharmony_ci
490d163575Sopenharmony_ciifeq ($(DEVICE_PATH),)
500d163575Sopenharmony_ciDEVICE_PATH:=$(ohos_device_path)
510d163575Sopenharmony_ciendif
520d163575Sopenharmony_ci
530d163575Sopenharmony_ciifeq ($(TEE:1=y),y)
540d163575Sopenharmony_citee = _tee
550d163575Sopenharmony_ciendif
560d163575Sopenharmony_ciifeq ($(RELEASE:1=y),y)
570d163575Sopenharmony_ciCONFIG ?= $(PRODUCT_PATH)/kernel_configs/release$(tee).config
580d163575Sopenharmony_cielse
590d163575Sopenharmony_ciCONFIG ?= $(PRODUCT_PATH)/kernel_configs/debug$(tee).config
600d163575Sopenharmony_ciendif
610d163575Sopenharmony_ci
620d163575Sopenharmony_ciKCONFIG_CONFIG ?= $(CONFIG)
630d163575Sopenharmony_ciSYSROOT_PATH ?= $(OUT)/sysroot
640d163575Sopenharmony_ci
650d163575Sopenharmony_ci# export subdir Makefile related environment variables
660d163575Sopenharmony_ciexport SYSROOT_PATH
670d163575Sopenharmony_ciexport PRODUCT_PATH
680d163575Sopenharmony_ciexport DEVICE_PATH
690d163575Sopenharmony_ci
700d163575Sopenharmony_ci# export kconfig related environment variables
710d163575Sopenharmony_ciexport CONFIG_=LOSCFG_
720d163575Sopenharmony_ciexport srctree=$(LITEOSTOPDIR)
730d163575Sopenharmony_ci
740d163575Sopenharmony_ciinclude $(LITEOSTOPDIR)/config.mk
750d163575Sopenharmony_ci
760d163575Sopenharmony_ciifeq ($(LOSCFG_STORAGE_SPINOR), y)
770d163575Sopenharmony_ciFSTYPE = jffs2
780d163575Sopenharmony_ciendif
790d163575Sopenharmony_ciifeq ($(LOSCFG_STORAGE_EMMC), y)
800d163575Sopenharmony_ciFSTYPE = vfat
810d163575Sopenharmony_ciendif
820d163575Sopenharmony_ciifeq ($(LOSCFG_STORAGE_SPINAND), y)
830d163575Sopenharmony_ciFSTYPE = yaffs2
840d163575Sopenharmony_ciendif
850d163575Sopenharmony_ciROOTFS_DIR = $(OUT)/rootfs
860d163575Sopenharmony_ciROOTFS_ZIP = $(OUT)/rootfs.zip
870d163575Sopenharmony_ci
880d163575Sopenharmony_cidefine HELP =
890d163575Sopenharmony_ciUsage: make [TARGET]... [PARAMETER=VALUE]...
900d163575Sopenharmony_ci
910d163575Sopenharmony_ciTargets:
920d163575Sopenharmony_ci    help:       display this help and exit
930d163575Sopenharmony_ci    clean:      clean compiled objects
940d163575Sopenharmony_ci    cleanall:   clean all build outputs
950d163575Sopenharmony_ci    all:        make liteos kernel image and rootfs image (Default target)
960d163575Sopenharmony_ci    $(APPS):       build all apps
970d163575Sopenharmony_ci    $(ROOTFS):     make an original rootfs image
980d163575Sopenharmony_ci    $(LITEOS_LIBS_TARGET):       compile all kernel modules (libraries)
990d163575Sopenharmony_ci    $(LITEOS_TARGET):     make liteos kernel image
1000d163575Sopenharmony_ci    update_config:  update product kernel config (use menuconfig)
1010d163575Sopenharmony_ci    xxconfig:   invoke xxconfig command of kconfiglib (xxconfig is one of $(KCONFIG_CMDS))
1020d163575Sopenharmony_ci
1030d163575Sopenharmony_ciParameters:
1040d163575Sopenharmony_ci    FSTYPE:     value should be one of (jffs2 vfat yaffs2)
1050d163575Sopenharmony_ci    TEE:        boolean value(1 or y for true), enable tee
1060d163575Sopenharmony_ci    RELEASE:    boolean value(1 or y for true), build release version
1070d163575Sopenharmony_ci    CONFIG:     kernel config file to be use
1080d163575Sopenharmony_ci    args:       arguments for xxconfig command
1090d163575Sopenharmony_ciendef
1100d163575Sopenharmony_ciexport HELP
1110d163575Sopenharmony_ci
1120d163575Sopenharmony_ciall: $(LITEOS_TARGET) $(ROOTFS)
1130d163575Sopenharmony_ci
1140d163575Sopenharmony_cihelp:
1150d163575Sopenharmony_ci	$(HIDE)echo "$$HELP"
1160d163575Sopenharmony_ci
1170d163575Sopenharmony_cisysroot:
1180d163575Sopenharmony_ci	$(HIDE)echo "sysroot:" $(abspath $(SYSROOT_PATH))
1190d163575Sopenharmony_ciifeq ($(origin SYSROOT_PATH),file)
1200d163575Sopenharmony_ci	$(HIDE)mkdir -p $(SYSROOT_PATH)/build && cd $(SYSROOT_PATH)/build && \
1210d163575Sopenharmony_ci	ln -snf $(LITEOSTOPDIR)/../../third_party/musl/scripts/build_lite/Makefile && \
1220d163575Sopenharmony_ci	$(MAKE) TARGETS=liteos_a_user \
1230d163575Sopenharmony_ci		ARCH=$(ARCH) \
1240d163575Sopenharmony_ci		TARGET=$(LOSCFG_LLVM_TARGET) \
1250d163575Sopenharmony_ci		ARCH_CFLAGS="$(LITEOS_CORE_COPTS) -w" \
1260d163575Sopenharmony_ci		TOPDIR="$(LITEOSTOPDIR)/../.." \
1270d163575Sopenharmony_ci		SYSROOTDIR="$(SYSROOT_PATH)" \
1280d163575Sopenharmony_ci		$(if $(LOSCFG_COMPILER_CLANG_LLVM),CLANG="$(LITEOS_COMPILER_PATH)clang",GCC="$(CC)") \
1290d163575Sopenharmony_ci		BUILD_DEBUG=$(if $(patsubst y,,$(or $(RELEASE:1=y),n)),true,false)
1300d163575Sopenharmony_ciendif
1310d163575Sopenharmony_ci
1320d163575Sopenharmony_ci$(filter-out menuconfig,$(KCONFIG_CMDS)):
1330d163575Sopenharmony_ci	$(HIDE)$@ $(args)
1340d163575Sopenharmony_ci
1350d163575Sopenharmony_ci$(LITEOS_CONFIG_FILE): $(KCONFIG_CONFIG)
1360d163575Sopenharmony_ci	$(HIDE)env KCONFIG_CONFIG=$< genconfig --config-out $@ --header-path $(LITEOS_MENUCONFIG_H)
1370d163575Sopenharmony_ci
1380d163575Sopenharmony_ciupdate_config menuconfig:
1390d163575Sopenharmony_ci	$(HIDE)test -f "$(CONFIG)" && cp -v "$(CONFIG)" .config && menuconfig $(args) && savedefconfig --out "$(CONFIG)"
1400d163575Sopenharmony_ci
1410d163575Sopenharmony_ci$(LITEOS_LIBS_TARGET): sysroot
1420d163575Sopenharmony_ci	$(HIDE)for dir in $(LIB_SUBDIRS); do $(MAKE) -C $$dir all || exit 1; done
1430d163575Sopenharmony_ci
1440d163575Sopenharmony_ci$(LITEOS_TARGET): $(OUT)/$(LITEOS_TARGET)
1450d163575Sopenharmony_ci$(LITEOS_TARGET): $(OUT)/$(LITEOS_TARGET).map
1460d163575Sopenharmony_ci#$(LITEOS_TARGET): $(OUT)/$(LITEOS_TARGET).objsize
1470d163575Sopenharmony_ci$(LITEOS_TARGET): $(OUT)/$(LITEOS_TARGET).bin
1480d163575Sopenharmony_ci$(LITEOS_TARGET): $(OUT)/$(LITEOS_TARGET).sym.sorted
1490d163575Sopenharmony_ci$(LITEOS_TARGET): $(OUT)/$(LITEOS_TARGET).asm
1500d163575Sopenharmony_ci#$(LITEOS_TARGET): $(OUT)/$(LITEOS_TARGET).size
1510d163575Sopenharmony_ci
1520d163575Sopenharmony_ci$(OUT)/$(LITEOS_TARGET): $(LITEOS_LIBS_TARGET)
1530d163575Sopenharmony_ci	$(LD) $(LITEOS_LDFLAGS) $(LITEOS_TABLES_LDFLAGS) -Map=$@.map -o $@ --start-group $(LITEOS_LIBDEP) --end-group
1540d163575Sopenharmony_ci$(OUT)/$(LITEOS_TARGET).map: $(OUT)/$(LITEOS_TARGET)
1550d163575Sopenharmony_ci$(OUT)/$(LITEOS_TARGET).objsize: $(LITEOS_LIBS_TARGET)
1560d163575Sopenharmony_ci	$(SIZE) -t --common $(OUT)/lib/*.a >$@
1570d163575Sopenharmony_ci$(OUT)/$(LITEOS_TARGET).bin: $(OUT)/$(LITEOS_TARGET)
1580d163575Sopenharmony_ci	$(OBJCOPY) -O binary $< $@
1590d163575Sopenharmony_ci$(OUT)/$(LITEOS_TARGET).sym.sorted: $(OUT)/$(LITEOS_TARGET)
1600d163575Sopenharmony_ci	$(OBJDUMP) -t $< |sort >$@
1610d163575Sopenharmony_ci$(OUT)/$(LITEOS_TARGET).asm: $(OUT)/$(LITEOS_TARGET)
1620d163575Sopenharmony_ci	$(OBJDUMP) -d $< >$@
1630d163575Sopenharmony_ci$(OUT)/$(LITEOS_TARGET).size: $(OUT)/$(LITEOS_TARGET)
1640d163575Sopenharmony_ci	$(NM) -S --size-sort $< >$@
1650d163575Sopenharmony_ci
1660d163575Sopenharmony_ci$(APPS): sysroot
1670d163575Sopenharmony_ci	$(HIDE)$(MAKE) -C apps all
1680d163575Sopenharmony_ci
1690d163575Sopenharmony_ci$(ROOTFS): $(APPS)
1700d163575Sopenharmony_ci	$(HIDE)mkdir -p $(OUT)/musl
1710d163575Sopenharmony_ciifeq ($(LOSCFG_COMPILER_CLANG_LLVM), y)
1720d163575Sopenharmony_ci	$(HIDE)cp -fp $$($(CC) $(LITEOS_CFLAGS) -print-file-name=libc.so) $(OUT)/musl
1730d163575Sopenharmony_ci	$(HIDE)cp -fp $$($(GPP) $(LITEOS_CXXFLAGS) -print-file-name=libc++.so) $(OUT)/musl
1740d163575Sopenharmony_cielse
1750d163575Sopenharmony_ci	$(HIDE)cp -fp $$($(CC) $(LITEOS_CFLAGS) -print-file-name=libc.so) $(OUT)/musl
1760d163575Sopenharmony_ci	$(HIDE)cp -fp $$($(CC) $(LITEOS_CFLAGS) -print-file-name=libgcc_s.so.1) $(OUT)/musl
1770d163575Sopenharmony_ci	$(HIDE)cp -fp $$($(GPP) $(LITEOS_CXXFLAGS) -print-file-name=libstdc++.so.6) $(OUT)/musl
1780d163575Sopenharmony_ciendif
1790d163575Sopenharmony_ci	$(HIDE)$(LITEOS_SCRIPTPATH)/make_rootfs/rootfsdir.sh $(OUT) $(ROOTFS_DIR)
1800d163575Sopenharmony_ci	$(HIDE)shopt -s nullglob && $(STRIP) $(ROOTFS_DIR)/bin/* $(ROOTFS_DIR)/lib/*
1810d163575Sopenharmony_ciifneq ($(VERSION),)
1820d163575Sopenharmony_ci	$(HIDE)$(LITEOS_SCRIPTPATH)/make_rootfs/releaseinfo.sh "$(VERSION)" $(ROOTFS_DIR)
1830d163575Sopenharmony_ciendif
1840d163575Sopenharmony_ci	$(HIDE)$(LITEOS_SCRIPTPATH)/make_rootfs/rootfsimg.sh $(ROOTFS_DIR) $(FSTYPE)
1850d163575Sopenharmony_ci	$(HIDE)cd $(ROOTFS_DIR)/.. && zip -r $(ROOTFS_ZIP) $(ROOTFS)
1860d163575Sopenharmony_ci
1870d163575Sopenharmony_ciclean:
1880d163575Sopenharmony_ci	$(HIDE)if [ -d $(SYSROOT_PATH)/build ]; then $(MAKE) -C $(SYSROOT_PATH)/build clean; fi
1890d163575Sopenharmony_ci	$(HIDE)for dir in $(LIB_SUBDIRS) apps; do $(MAKE) -C $$dir clean || exit 1; done
1900d163575Sopenharmony_ci	$(HIDE)$(RM) $(LITEOS_MENUCONFIG_H)
1910d163575Sopenharmony_ci	$(HIDE)echo "clean $(LOSCFG_PLATFORM) finish"
1920d163575Sopenharmony_ci
1930d163575Sopenharmony_cicleanall: clean
1940d163575Sopenharmony_ci	$(HIDE)$(RM) $(LITEOSTOPDIR)/out $(LITEOS_CONFIG_FILE)
1950d163575Sopenharmony_ci	$(HIDE)echo "clean all done"
1960d163575Sopenharmony_ci
1970d163575Sopenharmony_ci.PHONY: all clean cleanall sysroot help update_config
1980d163575Sopenharmony_ci.PHONY: $(LITEOS_TARGET) $(ROOTFS) $(APPS) $(KCONFIG_CMDS) $(LITEOS_LIBS_TARGET) $(KCONFIG_CONFIG)
199