1# 2# Copyright (c) 2022, Google, Inc. All rights reserved 3# 4# Permission is hereby granted, free of charge, to any person obtaining 5# a copy of this software and associated documentation files 6# (the "Software"), to deal in the Software without restriction, 7# including without limitation the rights to use, copy, modify, merge, 8# publish, distribute, sublicense, and/or sell copies of the Software, 9# and to permit persons to whom the Software is furnished to do so, 10# subject to the following conditions: 11# 12# The above copyright notice and this permission notice shall be 13# included in all copies or substantial portions of the Software. 14# 15# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 18# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 19# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 20# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 21# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22# 23 24LOCAL_DIR := $(GET_LOCAL_DIR) 25 26MODULE := $(LOCAL_DIR) 27 28MODULE_DEPS := \ 29 lib/io 30 31ifndef WITH_CUSTOM_MALLOC 32MODULE_DEPS += lib/heap 33endif 34 35# Clang currently generates incorrect code when it simplifies calls to libc 36# and then inlines them. The simplification pass does not set a calling 37# convention on the new call, leading to problems when inlining. 38# Avoid this bug by disabling LTO for libc. See: b/161257552 39MODULE_DISABLE_LTO := true 40 41MUSL_DIR := external/trusty/musl 42LK_DIR := $(LKROOT) 43LIBC_TRUSTY_DIR := trusty/user/base/lib/libc-trusty 44 45MODULE_INCLUDES += \ 46 $(MUSL_DIR)/src/internal \ 47 $(MUSL_DIR)/src/include \ 48 49MODULE_EXPORT_COMPILEFLAGS += \ 50 -isystem $(MUSL_DIR)/arch/$(STANDARD_ARCH_NAME) \ 51 -isystem $(MUSL_DIR)/arch/generic \ 52 -isystem $(MUSL_DIR)/include \ 53 54MODULE_EXPORT_INCLUDES += \ 55 $(LK_DIR)/lib/libc/include_common \ 56 57# Musl is scrupulous about exposing prototypes and defines based on what 58# standard is requested. When compiling C++ code, however, Clang defines 59# _GNU_SOURCE because libcxx's header files depend on prototypes that are only 60# available with _GNU_SOURCE specified. To avoid skew where prototypes are 61# defined for C++ but not C, turn everything on always. 62MODULE_EXPORT_COMPILEFLAGS += -D_ALL_SOURCE 63 64# Musl declares global variables with names like "index" that can conflict with 65# function names when _ALL_SOURCE is turned on. Compile Musl as it expects to be 66# compiled. 67MODULE_COMPILEFLAGS += -U_ALL_SOURCE -D_XOPEN_SOURCE=700 68 69# Musl's source is not warning clean. Suppress warnings we know about. 70MODULE_COMPILEFLAGS += \ 71 -Wno-parentheses \ 72 -Wno-sign-compare \ 73 -Wno-incompatible-pointer-types-discards-qualifiers \ 74 -Wno-string-plus-int \ 75 -Wno-missing-braces \ 76 -Wno-implicit-fallthrough \ 77 -Wno-unused-but-set-variable \ 78 79# Musl is generally not strict about its function prototypes. 80# This could be fixed, except for "main". The prototype for main is deliberately 81# ill-defined. 82MODULE_CFLAGS += -Wno-strict-prototypes 83 84# Musl will do something like this: 85# weak_alias(a, b); weak_alias(b, c); 86# But it appears the second statement will get eagerly evaluated to: 87# weak_alias(a, c); 88# and overriding b will not affect c. This is likely not intended behavior, but 89# it does not matter for us so ignore it. 90MODULE_COMPILEFLAGS += \ 91 -Wno-ignored-attributes \ 92 93# The are compares that make sense in 64-bit but do not make sense in 32-bit. 94MODULE_COMPILEFLAGS += \ 95 -Wno-tautological-constant-compare 96 97# NOTE eabi_unwind_stubs.c because libgcc pulls in unwinding stuff. 98MODULE_SRCS := \ 99 $(LOCAL_DIR)/abort.c \ 100 $(LOCAL_DIR)/close.c \ 101 $(LOCAL_DIR)/fflush.c \ 102 $(LOCAL_DIR)/libc_state.c \ 103 $(LOCAL_DIR)/writev.c \ 104 $(LK_DIR)/lib/libc/atoi.c \ 105 $(LK_DIR)/lib/libc/eabi.c \ 106 $(LK_DIR)/lib/libc/eabi_unwind_stubs.c \ 107 $(LK_DIR)/lib/libc/io_handle.c \ 108 $(LK_DIR)/lib/libc/printf.c \ 109 $(LK_DIR)/lib/libc/stdio.c \ 110 $(LK_DIR)/lib/libc/strtol.c \ 111 $(LK_DIR)/lib/libc/strtoll.c \ 112 113# These sources are only necessary to support C++ 114MODULE_SRCS += \ 115 $(LIBC_TRUSTY_DIR)/locale_stubs.c \ 116 $(LK_DIR)/lib/libc/atexit.c \ 117 $(LK_DIR)/lib/libc/pure_virtual.cpp 118 119MODULE_DEPS += \ 120 $(LK_DIR)/lib/libc/rand 121 122# These stubs are only needed because binder uses libutils which uses pthreads mutex directly 123MODULE_SRCS += \ 124 $(LIBC_TRUSTY_DIR)/pthreads.c 125 126# Musl 127MODULE_SRCS += \ 128 $(MUSL_DIR)/src/ctype/isalnum.c \ 129 $(MUSL_DIR)/src/ctype/isalpha.c \ 130 $(MUSL_DIR)/src/ctype/isascii.c \ 131 $(MUSL_DIR)/src/ctype/isblank.c \ 132 $(MUSL_DIR)/src/ctype/iscntrl.c \ 133 $(MUSL_DIR)/src/ctype/isdigit.c \ 134 $(MUSL_DIR)/src/ctype/isgraph.c \ 135 $(MUSL_DIR)/src/ctype/islower.c \ 136 $(MUSL_DIR)/src/ctype/isprint.c \ 137 $(MUSL_DIR)/src/ctype/ispunct.c \ 138 $(MUSL_DIR)/src/ctype/isspace.c \ 139 $(MUSL_DIR)/src/ctype/isupper.c \ 140 $(MUSL_DIR)/src/ctype/isxdigit.c \ 141 $(MUSL_DIR)/src/ctype/toascii.c \ 142 $(MUSL_DIR)/src/ctype/tolower.c \ 143 $(MUSL_DIR)/src/ctype/toupper.c \ 144 $(MUSL_DIR)/src/locale/c_locale.c \ 145 $(MUSL_DIR)/src/stdlib/abs.c \ 146 $(MUSL_DIR)/src/stdlib/bsearch.c \ 147 $(MUSL_DIR)/src/stdlib/div.c \ 148 $(MUSL_DIR)/src/stdlib/imaxabs.c \ 149 $(MUSL_DIR)/src/stdlib/imaxdiv.c \ 150 $(MUSL_DIR)/src/stdlib/labs.c \ 151 $(MUSL_DIR)/src/stdlib/ldiv.c \ 152 $(MUSL_DIR)/src/stdlib/llabs.c \ 153 $(MUSL_DIR)/src/stdlib/lldiv.c \ 154 $(MUSL_DIR)/src/stdlib/qsort.c \ 155 $(MUSL_DIR)/src/string/explicit_bzero.c \ 156 $(MUSL_DIR)/src/string/bcmp.c \ 157 $(MUSL_DIR)/src/string/memccpy.c \ 158 $(MUSL_DIR)/src/string/memmem.c \ 159 $(MUSL_DIR)/src/string/mempcpy.c \ 160 $(MUSL_DIR)/src/string/memrchr.c \ 161 $(MUSL_DIR)/src/string/stpcpy.c \ 162 $(MUSL_DIR)/src/string/stpncpy.c \ 163 $(MUSL_DIR)/src/string/strcasecmp.c \ 164 $(MUSL_DIR)/src/string/strcasestr.c \ 165 $(MUSL_DIR)/src/string/strchrnul.c \ 166 $(MUSL_DIR)/src/string/strcspn.c \ 167 $(MUSL_DIR)/src/string/strerror_r.c \ 168 $(MUSL_DIR)/src/string/strncasecmp.c \ 169 $(MUSL_DIR)/src/string/strndup.c \ 170 $(MUSL_DIR)/src/string/strsep.c \ 171 $(MUSL_DIR)/src/string/strtok_r.c \ 172 $(MUSL_DIR)/src/string/strverscmp.c \ 173 $(MUSL_DIR)/src/string/swab.c \ 174 175# We use the lk implementation of stdio but export 176# the musl stdio.h headers, which expose different 177# definitions of stdin/out/err. The files below contain 178# the musl definitions of those FILE structures, along 179# with their dependencies. 180MODULE_SRCS += \ 181 $(MUSL_DIR)/src/stdio/stderr.c \ 182 $(MUSL_DIR)/src/stdio/stdin.c \ 183 $(MUSL_DIR)/src/stdio/stdout.c \ 184 $(MUSL_DIR)/src/stdio/__stdio_close.c \ 185 $(MUSL_DIR)/src/stdio/__stdio_read.c \ 186 $(MUSL_DIR)/src/stdio/__stdio_write.c \ 187 $(MUSL_DIR)/src/stdio/__stdio_seek.c \ 188 189# These sources are only necessary to support C++ 190MODULE_SRCS += \ 191 $(MUSL_DIR)/src/ctype/__ctype_get_mb_cur_max.c \ 192 $(MUSL_DIR)/src/multibyte/internal.c \ 193 $(MUSL_DIR)/src/multibyte/mbtowc.c \ 194 $(MUSL_DIR)/src/multibyte/wcrtomb.c \ 195 196include $(LK_DIR)/lib/libc/string/rules.mk 197 198include make/library.mk 199