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 81ifeq ($(IS_LLVM_BUILD),true) 82CFLAGS += -D__IS_LLVM_BUILD 83endif 84 85.PHONY: $(TARGETS:%=musl_copy_for_%) 86.PHONY: $(TARGETS:%=musl_patch_for_%) 87.PHONY: $(TARGETS:%=musl_install_for_%) 88.PHONY: $(TARGETS:%=linux_header_install_for_%) 89.PHONY: $(TARGETS:%=nuttx_header_install_for_%) 90.PHONY: $(TARGETS:%=optimized_routines_install_for_%) 91.PHONY: all clean distclean 92 93all: $(TARGETS:%=musl_install_for_%) 94 95$(TARGETS:%=musl_copy_for_%): 96 $(HIDE) mkdir -p $@ 97 $(HIDE) cp -rfu $(MUSLDIR)/[^p]* $@ 98 $(HIDE) cp -rfu $(MUSLDIR)/../FreeBSD/sys/sys/queue.h $@/include/sys 99 100optimized_routines_install_for_liteos_a_user: musl_copy_for_liteos_a_user 101ifneq ($(ARCH),) 102 $(HIDE) cp -rfp $(OPTRTDIR)/string/$(ARCH)/* $</src/string/$(ARCH)/ 103 $(HIDE) cp -rfp $(OPTRTDIR)/string/$(ARCH)/asmdefs.h $</src/string/$(ARCH)/asmdefs.h 104ifeq ($(ARCH),arm) 105 $(HIDE) rm -f $</src/string/arm/memcpy.c 106 $(HIDE) rm -f $</src/string/arm/memcpy_le.S 107 $(HIDE) rm -f $</src/string/arm/memset.S 108 $(HIDE) rm -f $</src/string/arm/strcpy.c 109 $(HIDE) true >> $</src/string/arm/strlen.S 110 111CFLAGS += \ 112 -D__strlen_armv6t2=strlen \ 113 -D__strcpy_arm=strcpy \ 114 -D__strcmp_arm=strcmp \ 115 -D__memcpy_arm=memcpy \ 116 -D__memchr_arm=memchr 117 118endif 119endif 120 121nuttx_header_install_for_liteos_a_user: musl_copy_for_liteos_a_user 122 $(HIDE) sed '/^#include/d' $(NUTTXDIR)/include/nuttx/video/fb.h | sed 's,FAR ,,g' | sed 's,LosVmMapRegion,void,g' > $</include/fb.h 123 124linux_header_install_for_liteos_a_user: musl_copy_for_liteos_a_user 125 $(HIDE) make -sj headers_install ARCH=$(ARCH) O=$(shell pwd)/$@ -C $(LINUXKERNELDIR) 126 $(HIDE) install -p -m 644 -D $@/usr/include/linux/capability.h $</include/linux/capability.h 127 $(HIDE) echo "$$LINUX_TYPES_H" > $</include/linux/types.h 128 129musl_patch_for_liteos_a_user: nuttx_header_install_for_liteos_a_user 130musl_patch_for_liteos_a_user: linux_header_install_for_liteos_a_user 131musl_patch_for_liteos_a_user: optimized_routines_install_for_liteos_a_user 132musl_patch_for_liteos_a_user: musl_copy_for_liteos_a_user 133 $(HIDE) cd musl_copy_for_liteos_a_user && \ 134 mv -f arch/arm/bits/liteos_a/* arch/arm/bits/ && \ 135 mv -f src/linux/liteos_a/* src/linux/ && \ 136 mv -f src/misc/liteos_a/* src/misc/ && \ 137 mv -f src/signal/liteos_a/* src/signal/ && \ 138 mv -f src/signal/arm/liteos_a/* src/signal/arm/ && \ 139 mv -f src/stdlib/liteos_a/* src/stdlib/ && \ 140 mv -f src/string/arm/liteos_a/* src/string/arm/ && \ 141 mv -f src/thread/liteos_a/* src/thread/ && \ 142 mv -f src/unistd/liteos_a/* src/unistd/ && \ 143 mv -f src/network/liteos_a/* src/network/ && \ 144 cd .. 145 146musl_patch_for_liteos_a_user_debug: musl_patch_for_liteos_a_user 147musl_patch_for_liteos_a_user_debug: musl_copy_for_liteos_a_user 148 $(HIDE) cd musl_copy_for_liteos_a_user && \ 149 mv -f include/liteos_a/user_debug/* include/ && \ 150 mv -f src/malloc/liteos_a/user_debug/* src/malloc/ && \ 151 cd .. 152 153ifeq ($(BUILD_DEBUG),true) 154ifeq ($(GCC),) 155musl_install_for_liteos_a_user: LDFLAGS = $(shell $(CC) -print-file-name=libunwind.a) -Wl,--no-dependent-libraries 156musl_install_for_liteos_a_user: SED_ARGS += -e 's,$$(AR) rc $$@ $$(AOBJS)$$,cp $$(shell $$(CC) -print-file-name=libunwind.a) $$@ \&\& &,g' 157endif 158musl_install_for_liteos_a_user: SED_ARGS += -e 's,CFLAGS_ALL += -DCRT$$,& -fno-unwind-tables -fno-asynchronous-unwind-tables,g' 159musl_install_for_liteos_a_user: CFLAGS += -funwind-tables -fasynchronous-unwind-tables -rdynamic -D__LITEOS_DEBUG__ -I $(shell $(CC) "-print-file-name=include") 160musl_install_for_liteos_a_user: musl_patch_for_liteos_a_user_debug 161endif 162 163musl_install_for_liteos_a_user: CFLAGS += -D__LITEOS_A__ -DFEATURE_PTHREAD_CANCEL 164 165musl_header_install_for_liteos_a_user: musl_patch_for_liteos_a_user 166 $(HIDE) cd musl_copy_for_liteos_a_user && mkdir -p $(MUSLBUILDDIR) && cd $(MUSLBUILDDIR) && \ 167 ../configure --prefix=$(SYSROOTDIR)/usr --target=$(TARGET) \ 168 --includedir=$(SYSROOTDIR)/usr/include/$(TARGET) \ 169 --libdir=$(SYSROOTDIR)/usr/lib/$(TARGET)/$(MULTILIB) \ 170 --with-malloc=oldmalloc \ 171 $(if $(LDFLAGS),LDFLAGS="$(LDFLAGS)",) \ 172 CC="$(CC)" CROSS_COMPILE="$(CROSS_COMPILE)" CFLAGS="$(CFLAGS)" >/dev/null && \ 173 sed $(SED_ARGS) Makefile | make -sj install-headers 174 175musl_install_for_liteos_a_user: musl_patch_for_liteos_a_user 176 $(HIDE) cd musl_copy_for_liteos_a_user && mkdir -p $(MUSLBUILDDIR) && cd $(MUSLBUILDDIR) && \ 177 ../configure --prefix=$(SYSROOTDIR)/usr --target=$(TARGET) \ 178 --includedir=$(SYSROOTDIR)/usr/include/$(TARGET) \ 179 --libdir=$(SYSROOTDIR)/usr/lib/$(TARGET)/$(MULTILIB) \ 180 --with-malloc=oldmalloc \ 181 $(if $(LDFLAGS),LDFLAGS="$(LDFLAGS)",) \ 182 CC="$(CC)" CROSS_COMPILE="$(CROSS_COMPILE)" CFLAGS="$(CFLAGS)" >/dev/null && \ 183 sed $(SED_ARGS) Makefile | make -f- -sj install 184 185ifeq ($(wildcard $(LINUXHDRDIR)),) 186LINUXHDRDIR = $(shell pwd)/linux_header_install_for_linux_user/usr/include 187linux_header_install_for_linux_user: 188 $(HIDE) make -sj headers_install ARCH=$(ARCH) O=$(shell pwd)/$@ -C $(LINUXDIR) 189musl_patch_for_linux_user: linux_header_install_for_linux_user 190endif 191 192musl_patch_for_linux_user: musl_copy_for_linux_user 193 $(HIDE) cp -rfp $(MUSLDIR)/scripts/linux/* $</ && \ 194 cd musl_copy_for_linux_user && \ 195 mv -f crt/linux/* crt/ && \ 196 mv -f include/fortify/linux/* include/fortify/ && \ 197 mv -f include/info/linux/* include/info/ && \ 198 mv -f include/linux/* include/ && \ 199 mv -f include/sys/linux/* include/sys/ && \ 200 mv -f include/trace/linux/* include/trace/ && \ 201 mv -f ldso/linux/* ldso/ && \ 202 mv -f src/exit/linux/* src/exit/ && \ 203 mv -f src/fortify/linux/* src/fortify/ && \ 204 mv -f src/gwp_asan/linux/* src/gwp_asan/ && \ 205 mv -f src/hilog/linux/* src/hilog/ && \ 206 mv -f src/hook/linux/* src/hook/ && \ 207 mv -f src/info/linux/* src/info/ && \ 208 mv -f src/internal/linux/* src/internal/ && \ 209 mv -f src/ldso/aarch64/linux/* src/ldso/aarch64/ && \ 210 mv -f src/ldso/arm/linux/* src/ldso/arm/ && \ 211 mv -f src/ldso/riscv64/linux/* src/ldso/riscv64/ && \ 212 mv -f src/ldso/x86_64/linux/* src/ldso/x86_64/ && \ 213 mv -f src/linux/aarch64/linux/* src/linux/aarch64/ && \ 214 mv -f src/linux/arm/linux/* src/linux/arm/ && \ 215 mv -f src/linux/linux/* src/linux/ && \ 216 mv -f src/linux/x86_64/linux/* src/linux/x86_64/ && \ 217 mv -f src/malloc/jemalloc/linux/* src/malloc/jemalloc/ && \ 218 mv -f src/malloc/linux/* src/malloc/ && \ 219 mv -f src/misc/aarch64/linux/* src/misc/aarch64/ && \ 220 mv -f src/sigchain/linux/* src/sigchain/ && \ 221 mv -f src/trace/linux/* src/trace/ && \ 222 mv -f src/network/linux/* src/network/ && \ 223 mv -f src/signal/linux/* src/signal/ && \ 224 mv -f src/thread/linux/* src/thread/ && \ 225 mv -f src/fdsan/linux/* src/fdsan/ && \ 226 cd .. 227 228LIBCXXDIR = $(TOPDIR)/prebuilts/clang/ohos/linux-x86_64/llvm/lib/clang/15.0.4/include 229musl_install_for_linux_user: LDFLAGS = $(shell $(CC) -print-file-name=libunwind.a) -Wl,--no-dependent-libraries 230musl_install_for_linux_user: SED_ARGS += -e 's,$$(AR) rc $$@ $$(AOBJS)$$,cp $$(shell $$(CC) -print-file-name=libunwind.a) $$@ \&\& &,g' 231musl_install_for_linux_user: CFLAGS += -fno-omit-frame-pointer -funwind-tables -fasynchronous-unwind-tables -rdynamic -D__HISPARK_LINUX__ -I $(LINUXHDRDIR) -I $(LIBCXXDIR) 232ifeq ($(GCC),) 233musl_install_for_linux_user: TARGET = $(ARCH)-linux-ohos 234endif 235musl_install_for_linux_user: musl_patch_for_linux_user 236 $(HIDE) cd musl_copy_for_linux_user && mkdir -p $(MUSLBUILDDIR) && cd $(MUSLBUILDDIR) && \ 237 ../configure --prefix=$(SYSROOTDIR)/usr --target=$(TARGET) \ 238 --includedir=$(SYSROOTDIR)/usr/include/$(TARGET) \ 239 --libdir=$(SYSROOTDIR)/usr/lib/$(TARGET)/$(MULTILIB) \ 240 --with-malloc=oldmalloc \ 241 $(if $(LDFLAGS),LDFLAGS="$(LDFLAGS)",) \ 242 CC="$(CC)" CROSS_COMPILE="$(CROSS_COMPILE)" CFLAGS="$(CFLAGS)" >/dev/null && \ 243 sed $(SED_ARGS) Makefile | make -f- -sj install 244 $(HIDE) cp -rfp $(LINUXHDRDIR)/* $(SYSROOTDIR)/usr/include/$(TARGET) 245 $(HIDE) if [ -d $(LINUXHDRDIR)/asm-$(ARCH)/asm ]; then ln -snf asm-$(ARCH)/asm $(SYSROOTDIR)/usr/include/$(TARGET)/; fi 246ifeq ($(ARCH),aarch64) 247 $(HIDE) if [ -d $(LINUXHDRDIR)/asm-arm64/asm ]; then ln -snf asm-arm64/asm $(SYSROOTDIR)/usr/include/$(TARGET)/; fi 248endif 249 250clean: 251 $(HIDE) rm -rf musl_copy_for_* linux_header_install_for_* 252 253distclean: clean 254 $(HIDE) rm -rf $(SYSROOTDIR)/lib $(SYSROOTDIR)/usr 255