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 79CFLAGS += -Wno-int-conversion 80 81.PHONY: $(TARGETS:%=musl_copy_for_%) 82.PHONY: $(TARGETS:%=musl_patch_for_%) 83.PHONY: $(TARGETS:%=musl_install_for_%) 84.PHONY: $(TARGETS:%=linux_header_install_for_%) 85.PHONY: $(TARGETS:%=nuttx_header_install_for_%) 86.PHONY: $(TARGETS:%=optimized_routines_install_for_%) 87.PHONY: all clean distclean 88 89all: $(TARGETS:%=musl_install_for_%) 90 91$(TARGETS:%=musl_copy_for_%): 92 $(HIDE) mkdir -p $@ 93 $(HIDE) cp -rfu $(MUSLDIR)/[^p]* $@ 94 $(HIDE) cp -rfu $(MUSLDIR)/../FreeBSD/sys/sys/queue.h $@/include/sys 95 96optimized_routines_install_for_liteos_a_user: musl_copy_for_liteos_a_user 97ifneq ($(ARCH),) 98 $(HIDE) cp -rfp $(OPTRTDIR)/string/$(ARCH)/* $</src/string/$(ARCH)/ 99 $(HIDE) cp -rfp $(OPTRTDIR)/string/asmdefs.h $</src/string/asmdefs.h 100ifeq ($(ARCH),arm) 101 $(HIDE) rm -f $</src/string/arm/memcpy.c 102 $(HIDE) rm -f $</src/string/arm/memcpy_le.S 103 $(HIDE) rm -f $</src/string/arm/memset.S 104 $(HIDE) rm -f $</src/string/arm/strcpy.c 105 $(HIDE) true >> $</src/string/arm/strlen.S 106 107CFLAGS += \ 108 -D__strlen_armv6t2=strlen \ 109 -D__strcpy_arm=strcpy \ 110 -D__strcmp_arm=strcmp \ 111 -D__memcpy_arm=memcpy \ 112 -D__memchr_arm=memchr 113 114endif 115endif 116 117nuttx_header_install_for_liteos_a_user: musl_copy_for_liteos_a_user 118 $(HIDE) sed '/^#include/d' $(NUTTXDIR)/include/nuttx/video/fb.h | sed 's,FAR ,,g' | sed 's,LosVmMapRegion,void,g' > $</include/fb.h 119 120linux_header_install_for_liteos_a_user: musl_copy_for_liteos_a_user 121 $(HIDE) make -sj headers_install ARCH=$(ARCH) O=$(shell pwd)/$@ -C $(LINUXKERNELDIR) 122 $(HIDE) install -p -m 644 -D $@/usr/include/linux/capability.h $</include/linux/capability.h 123 $(HIDE) echo "$$LINUX_TYPES_H" > $</include/linux/types.h 124 125musl_patch_for_liteos_a_user: nuttx_header_install_for_liteos_a_user 126musl_patch_for_liteos_a_user: linux_header_install_for_liteos_a_user 127musl_patch_for_liteos_a_user: optimized_routines_install_for_liteos_a_user 128musl_patch_for_liteos_a_user: musl_copy_for_liteos_a_user 129 $(HIDE) cp -rfp $(MUSLDIR)/porting/liteos_a/user/* $</ 130 131musl_patch_for_liteos_a_user_debug: musl_patch_for_liteos_a_user 132musl_patch_for_liteos_a_user_debug: musl_copy_for_liteos_a_user 133 $(HIDE) cp -rfp $(MUSLDIR)/porting/liteos_a/user_debug/* $</ 134 135ifeq ($(BUILD_DEBUG),true) 136ifeq ($(GCC),) 137musl_install_for_liteos_a_user: LDFLAGS = $(shell $(CC) -print-file-name=libunwind.a) -Wl,--no-dependent-libraries 138musl_install_for_liteos_a_user: SED_ARGS += -e 's,$$(AR) rc $$@ $$(AOBJS)$$,cp $$(shell $$(CC) -print-file-name=libunwind.a) $$@ \&\& &,g' 139endif 140musl_install_for_liteos_a_user: SED_ARGS += -e 's,CFLAGS_ALL += -DCRT$$,& -fno-unwind-tables -fno-asynchronous-unwind-tables,g' 141musl_install_for_liteos_a_user: CFLAGS += -funwind-tables -fasynchronous-unwind-tables -rdynamic -I $(shell $(CC) "-print-file-name=include") 142musl_install_for_liteos_a_user: musl_patch_for_liteos_a_user_debug 143endif 144 145musl_header_install_for_liteos_a_user: musl_patch_for_liteos_a_user 146 $(HIDE) cd musl_copy_for_liteos_a_user && mkdir -p $(MUSLBUILDDIR) && cd $(MUSLBUILDDIR) && \ 147 ../configure --prefix=$(SYSROOTDIR)/usr --target=$(TARGET) \ 148 --includedir=$(SYSROOTDIR)/usr/include/$(TARGET) \ 149 --libdir=$(SYSROOTDIR)/usr/lib/$(TARGET)/$(MULTILIB) \ 150 --with-malloc=oldmalloc \ 151 $(if $(LDFLAGS),LDFLAGS="$(LDFLAGS)",) \ 152 CC="$(CC)" CROSS_COMPILE="$(CROSS_COMPILE)" CFLAGS="$(CFLAGS)" >/dev/null && \ 153 sed $(SED_ARGS) Makefile | make -sj install-headers 154 155musl_install_for_liteos_a_user: musl_patch_for_liteos_a_user 156 $(HIDE) cd musl_copy_for_liteos_a_user && mkdir -p $(MUSLBUILDDIR) && cd $(MUSLBUILDDIR) && \ 157 ../configure --prefix=$(SYSROOTDIR)/usr --target=$(TARGET) \ 158 --includedir=$(SYSROOTDIR)/usr/include/$(TARGET) \ 159 --libdir=$(SYSROOTDIR)/usr/lib/$(TARGET)/$(MULTILIB) \ 160 --with-malloc=oldmalloc \ 161 $(if $(LDFLAGS),LDFLAGS="$(LDFLAGS)",) \ 162 CC="$(CC)" CROSS_COMPILE="$(CROSS_COMPILE)" CFLAGS="$(CFLAGS)" >/dev/null && \ 163 sed $(SED_ARGS) Makefile | make -f- -sj install 164 165ifeq ($(wildcard $(LINUXHDRDIR)),) 166LINUXHDRDIR = $(shell pwd)/linux_header_install_for_linux_user/usr/include 167linux_header_install_for_linux_user: 168 $(HIDE) make -sj headers_install ARCH=$(ARCH) O=$(shell pwd)/$@ -C $(LINUXDIR) 169musl_patch_for_linux_user: linux_header_install_for_linux_user 170endif 171 172musl_patch_for_linux_user: musl_copy_for_linux_user 173 $(HIDE) cp -rfp $(MUSLDIR)/porting/linux/user/* $</ 174 175musl_install_for_linux_user: LDFLAGS = $(shell $(CC) -print-file-name=libunwind.a) -Wl,--no-dependent-libraries 176musl_install_for_linux_user: SED_ARGS += -e 's,$$(AR) rc $$@ $$(AOBJS)$$,cp $$(shell $$(CC) -print-file-name=libunwind.a) $$@ \&\& &,g' 177musl_install_for_linux_user: CFLAGS += -fno-omit-frame-pointer -funwind-tables -fasynchronous-unwind-tables -rdynamic -I $(LINUXHDRDIR) 178ifeq ($(GCC),) 179musl_install_for_linux_user: TARGET = $(ARCH)-linux-ohos 180endif 181musl_install_for_linux_user: musl_patch_for_linux_user 182 $(HIDE) cd musl_copy_for_linux_user && mkdir -p $(MUSLBUILDDIR) && cd $(MUSLBUILDDIR) && \ 183 ../configure --prefix=$(SYSROOTDIR)/usr --target=$(TARGET) \ 184 --includedir=$(SYSROOTDIR)/usr/include/$(TARGET) \ 185 --libdir=$(SYSROOTDIR)/usr/lib/$(TARGET)/$(MULTILIB) \ 186 --with-malloc=oldmalloc \ 187 $(if $(LDFLAGS),LDFLAGS="$(LDFLAGS)",) \ 188 CC="$(CC)" CROSS_COMPILE="$(CROSS_COMPILE)" CFLAGS="$(CFLAGS)" >/dev/null && \ 189 sed $(SED_ARGS) Makefile | make -f- -sj install 190 $(HIDE) cp -rfp $(LINUXHDRDIR)/* $(SYSROOTDIR)/usr/include/$(TARGET) 191 $(HIDE) if [ -d $(LINUXHDRDIR)/asm-$(ARCH)/asm ]; then ln -snf asm-$(ARCH)/asm $(SYSROOTDIR)/usr/include/$(TARGET)/; fi 192 193clean: 194 $(HIDE) rm -rf musl_copy_for_* linux_header_install_for_* 195 196distclean: clean 197 $(HIDE) rm -rf $(SYSROOTDIR)/lib $(SYSROOTDIR)/usr 198