1# Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. 2# Copyright (c) 2020-2022 Huawei Device Co., Ltd. All rights reserved. 3# 4# Redistribution and use in source and binary forms, with or without modification, 5# are permitted provided that the following conditions are met: 6# 7# 1. Redistributions of source code must retain the above copyright notice, this list of 8# conditions and the following disclaimer. 9# 10# 2. Redistributions in binary form must reproduce the above copyright notice, this list 11# of conditions and the following disclaimer in the documentation and/or other materials 12# provided with the distribution. 13# 14# 3. Neither the name of the copyright holder nor the names of its contributors may be used 15# to endorse or promote products derived from this software without specific prior written 16# permission. 17# 18# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 20# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 22# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 23# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 24# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 25# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 26# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 27# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 28# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 30ARCH = arm 31ifneq ($(GCC),) 32TARGET = 33CROSS_COMPILE = $(GCC:%gcc=%) 34CC = $(GCC) $(ARCH_CFLAGS) 35MULTILIB = $(patsubst %.,%,$(shell $(CC) -print-multi-directory)) 36else 37TARGET = $(ARCH)-liteos-ohos 38CLANG ?= clang 39CROSS_COMPILE = $(CLANG:%clang=%llvm-) 40CC = $(CLANG) --target=$(TARGET) $(ARCH_CFLAGS) 41MULTILIB = $(patsubst $(dir $(shell $(filter-out $(ARCH_CFLAGS),$(CC)) -print-libgcc-file-name))%,/%,$(dir $(shell $(CC) -print-libgcc-file-name))) 42endif 43MUSLBUILDDIR = build_$(or $(TARGET),$(ARCH))$(subst /,_,$(MULTILIB:%/=%)) 44HIDE = @ 45BUILD_DEBUG = false 46SED_ARGS = -e '/install-libs:/s/if/and/g' 47 48TOPDIR = $(shell pwd)/../../../.. 49MUSLDIR = $(TOPDIR)/third_party/musl 50LINUXKERNELDIR = $(TOPDIR)/kernel/linux/linux-5.10 51OPTRTDIR = $(TOPDIR)/third_party/optimized-routines 52NUTTXDIR = $(TOPDIR)/third_party/NuttX 53SYSROOTDIR = $(TOPDIR)/third_party/musl 54LITEOSADIR = $(TOPDIR)/kernel/liteos_a 55LINUXDIR = $(TOPDIR)/kernel/linux/linux-4.19 56LINUXHDRDIR = $(PREBUILTLINUXHDRDIR) 57 58TARGETS = $(if $(wildcard $(LITEOSADIR)),liteos_a_user,) 59TARGETS += $(if $(wildcard $(LINUXDIR)),linux_user,) 60 61define LINUX_TYPES_H 62/* Auto generated file at $(shell date), do NOT edit! */ 63#ifndef _LINUX_TYPES_H 64#define _LINUX_TYPES_H 65#include <stdint.h> 66typedef uint32_t __u32, __le32; 67#endif 68endef 69export LINUX_TYPES_H 70 71ifeq ($(ARCH),arm) 72ARCH_CFLAGS = -mfloat-abi=softfp -mcpu=cortex-a7 -mfpu=neon-vfpv4 73else 74$(warning *** warning: ARCH $(ARCH) has not been tested yet, use with cautions!) 75ARCH_CFLAGS = 76endif 77 78CFLAGS = -fstack-protector-strong -D_FORTIFY_SOURCE=2 -Wl,-z,relro,-z,now,-z,noexecstack -Wl,--build-id=sha1 79 80.PHONY: $(TARGETS:%=musl_copy_for_%) 81.PHONY: $(TARGETS:%=musl_patch_for_%) 82.PHONY: $(TARGETS:%=musl_install_for_%) 83.PHONY: $(TARGETS:%=linux_header_install_for_%) 84.PHONY: $(TARGETS:%=nuttx_header_install_for_%) 85.PHONY: $(TARGETS:%=optimized_routines_install_for_%) 86.PHONY: all clean distclean 87 88all: $(TARGETS:%=musl_install_for_%) 89 90$(TARGETS:%=musl_copy_for_%): 91 $(HIDE) mkdir -p $@ 92 $(HIDE) cp -rfu $(MUSLDIR)/[^p]* $@ 93 94optimized_routines_install_for_liteos_a_user: musl_copy_for_liteos_a_user 95ifneq ($(ARCH),) 96 $(HIDE) cp -rfp $(OPTRTDIR)/string/$(ARCH)/* $</src/string/$(ARCH)/ 97 $(HIDE) cp -rfp $(OPTRTDIR)/string/asmdefs.h $</src/string/asmdefs.h 98ifeq ($(ARCH),arm) 99 $(HIDE) rm -f $</src/string/arm/memcpy.c 100 $(HIDE) rm -f $</src/string/arm/memcpy_le.S 101 $(HIDE) rm -f $</src/string/arm/memset.S 102 $(HIDE) rm -f $</src/string/arm/strcpy.c 103 $(HIDE) true >> $</src/string/arm/strlen.S 104 105CFLAGS += \ 106 -D__strlen_armv6t2=strlen \ 107 -D__strcpy_arm=strcpy \ 108 -D__strcmp_arm=strcmp \ 109 -D__memcpy_arm=memcpy \ 110 -D__memchr_arm=memchr 111 112endif 113endif 114 115nuttx_header_install_for_liteos_a_user: musl_copy_for_liteos_a_user 116 $(HIDE) sed '/^#include/d' $(NUTTXDIR)/include/nuttx/video/fb.h | sed 's,FAR ,,g' | sed 's,LosVmMapRegion,void,g' > $</include/fb.h 117 118linux_header_install_for_liteos_a_user: musl_copy_for_liteos_a_user 119 $(HIDE) make -sj headers_install ARCH=$(ARCH) O=$(shell pwd)/$@ -C $(LINUXKERNELDIR) 120 $(HIDE) install -p -m 644 -D $@/usr/include/linux/capability.h $</include/linux/capability.h 121 $(HIDE) echo "$$LINUX_TYPES_H" > $</include/linux/types.h 122 123musl_patch_for_liteos_a_user: nuttx_header_install_for_liteos_a_user 124musl_patch_for_liteos_a_user: linux_header_install_for_liteos_a_user 125musl_patch_for_liteos_a_user: optimized_routines_install_for_liteos_a_user 126musl_patch_for_liteos_a_user: musl_copy_for_liteos_a_user 127 $(HIDE) cp -rfp $(MUSLDIR)/porting/liteos_a/user/* $</ 128 129musl_patch_for_liteos_a_user_debug: musl_patch_for_liteos_a_user 130musl_patch_for_liteos_a_user_debug: musl_copy_for_liteos_a_user 131 $(HIDE) cp -rfp $(MUSLDIR)/porting/liteos_a/user_debug/* $</ 132 133ifeq ($(BUILD_DEBUG),true) 134ifeq ($(GCC),) 135musl_install_for_liteos_a_user: LDFLAGS = $(shell $(CC) -print-file-name=libunwind.a) -Wl,--no-dependent-libraries 136musl_install_for_liteos_a_user: SED_ARGS += -e 's,$$(AR) rc $$@ $$(AOBJS)$$,cp $$(shell $$(CC) -print-file-name=libunwind.a) $$@ \&\& &,g' 137endif 138musl_install_for_liteos_a_user: SED_ARGS += -e 's,CFLAGS_ALL += -DCRT$$,& -fno-unwind-tables -fno-asynchronous-unwind-tables,g' 139musl_install_for_liteos_a_user: CFLAGS += -funwind-tables -fasynchronous-unwind-tables -rdynamic -I $(shell $(CC) "-print-file-name=include") 140musl_install_for_liteos_a_user: musl_patch_for_liteos_a_user_debug 141endif 142 143musl_install_for_liteos_a_user: musl_patch_for_liteos_a_user 144 $(HIDE) cd musl_copy_for_liteos_a_user && mkdir -p $(MUSLBUILDDIR) && cd $(MUSLBUILDDIR) && \ 145 ../configure --prefix=$(SYSROOTDIR)/usr --target=$(TARGET) \ 146 --includedir=$(SYSROOTDIR)/usr/include/$(TARGET) \ 147 --libdir=$(SYSROOTDIR)/usr/lib/$(TARGET)/$(MULTILIB) \ 148 $(if $(LDFLAGS),LDFLAGS="$(LDFLAGS)",) \ 149 CC="$(CC)" CROSS_COMPILE="$(CROSS_COMPILE)" CFLAGS="$(CFLAGS)" >/dev/null && \ 150 sed $(SED_ARGS) Makefile | make -f- -sj install 151 152ifeq ($(wildcard $(LINUXHDRDIR)),) 153LINUXHDRDIR = $(shell pwd)/linux_header_install_for_linux_user/usr/include 154linux_header_install_for_linux_user: 155 $(HIDE) make -sj headers_install ARCH=$(ARCH) O=$(shell pwd)/$@ -C $(LINUXDIR) 156musl_patch_for_linux_user: linux_header_install_for_linux_user 157endif 158 159musl_patch_for_linux_user: musl_copy_for_linux_user 160 $(HIDE) cp -rfp $(MUSLDIR)/porting/linux/user/* $</ 161 162musl_install_for_linux_user: LDFLAGS = $(shell $(CC) -print-file-name=libunwind.a) -Wl,--no-dependent-libraries 163musl_install_for_linux_user: SED_ARGS += -e 's,$$(AR) rc $$@ $$(AOBJS)$$,cp $$(shell $$(CC) -print-file-name=libunwind.a) $$@ \&\& &,g' 164musl_install_for_linux_user: CFLAGS += -fno-omit-frame-pointer -funwind-tables -fasynchronous-unwind-tables -rdynamic -I $(LINUXHDRDIR) 165ifeq ($(GCC),) 166musl_install_for_linux_user: TARGET = $(ARCH)-linux-ohos 167endif 168musl_install_for_linux_user: musl_patch_for_linux_user 169 $(HIDE) cd musl_copy_for_linux_user && mkdir -p $(MUSLBUILDDIR) && cd $(MUSLBUILDDIR) && \ 170 ../configure --prefix=$(SYSROOTDIR)/usr --target=$(TARGET) \ 171 --includedir=$(SYSROOTDIR)/usr/include/$(TARGET) \ 172 --libdir=$(SYSROOTDIR)/usr/lib/$(TARGET)/$(MULTILIB) \ 173 $(if $(LDFLAGS),LDFLAGS="$(LDFLAGS)",) \ 174 CC="$(CC)" CROSS_COMPILE="$(CROSS_COMPILE)" CFLAGS="$(CFLAGS)" >/dev/null && \ 175 sed $(SED_ARGS) Makefile | make -f- -sj install 176 $(HIDE) cp -rfp $(LINUXHDRDIR)/* $(SYSROOTDIR)/usr/include/$(TARGET) 177 $(HIDE) if [ -d $(LINUXHDRDIR)/asm-$(ARCH)/asm ]; then ln -snf asm-$(ARCH)/asm $(SYSROOTDIR)/usr/include/$(TARGET)/; fi 178 179clean: 180 $(HIDE) rm -rf musl_copy_for_* linux_header_install_for_* 181 182distclean: clean 183 $(HIDE) rm -rf $(SYSROOTDIR)/lib $(SYSROOTDIR)/usr 184