From 74f5a59892a13855477d8710739445733af09bd9 Mon Sep 17 00:00:00 2001 From: Colin Cross Date: Tue, 14 Sep 2021 10:57:21 -0700 Subject: [PATCH] Fix libcore compiling against musl Fix various issues when compiling host modules against musl: Musl only provides the posix version of strerror_r that returns int. Update sys/poll.h and sys/signal.h includes to point to the correct locations. Bug: 190084016 Test: m USE_HOST_MUSL=true Change-Id: If6c9c31c935767e82508f8a469bdb499c7a96115 --- NativeCode.bp | 4 +++- luni/src/main/native/libcore_io_Linux.cpp | 10 ++++++++++ ojluni/src/main/native/LinuxWatchService.c | 6 ++---- ojluni/src/main/native/NativeThread.c | 4 +++- ojluni/src/main/native/Net.c | 6 +----- ojluni/src/main/native/jvm_md.h | 6 +----- ojluni/src/main/native/linux_close.cpp | 6 +----- ojluni/src/main/native/net_util_md.h | 6 +----- 8 files changed, 22 insertions(+), 26 deletions(-) diff --git a/NativeCode.bp b/NativeCode.bp index d01f3f50925..9d6f064e228 100644 --- a/NativeCode.bp +++ b/NativeCode.bp @@ -145,9 +145,11 @@ cc_defaults { "-D_LARGEFILE64_SOURCE", "-D_GNU_SOURCE", "-DLINUX", - "-D__GLIBC__", ], }, + glibc: { + cflags: ["-D__GLIBC__"], + }, android: { shared_libs: [ "libdl_android", diff --git a/luni/src/main/native/libcore_io_Linux.cpp b/luni/src/main/native/libcore_io_Linux.cpp index 425ed5d3226..a72de9b166a 100755 --- a/luni/src/main/native/libcore_io_Linux.cpp +++ b/luni/src/main/native/libcore_io_Linux.cpp @@ -57,6 +57,7 @@ #include #include #include +#include #include #include #include @@ -2650,8 +2651,17 @@ static jobject Linux_statvfs(JNIEnv* env, jobject, jstring javaPath) { static jstring Linux_strerror(JNIEnv* env, jobject, jint errnum) { char buffer[BUFSIZ]; +#ifdef ANDROID_HOST_MUSL + /* musl only provides the posix version of strerror_r that returns int */ + int ret = strerror_r(errnum, buffer, sizeof(buffer)); + if (ret != 0) { + return env->NewStringUTF(android::base::StringPrintf("Unknown error %d", errnum).c_str()); + } + return env->NewStringUTF(buffer); +#else const char* message = strerror_r(errnum, buffer, sizeof(buffer)); return env->NewStringUTF(message); +#endif } static jstring Linux_strsignal(JNIEnv* env, jobject, jint signal) { diff --git a/ojluni/src/main/native/LinuxWatchService.c b/ojluni/src/main/native/LinuxWatchService.c index 1f531d08d4b..2e688e7e002 100644 --- a/ojluni/src/main/native/LinuxWatchService.c +++ b/ojluni/src/main/native/LinuxWatchService.c @@ -32,12 +32,10 @@ #include #include #include -// Android-changed: Fuchsia: Point to correct location of header. http://b/119426171 +// Android-changed: Point to correct location of header. http://b/119426171 // #include -#if defined(__Fuchsia__) #include -#else -#include +#if !defined(__Fuchsia__) #include #endif diff --git a/ojluni/src/main/native/NativeThread.c b/ojluni/src/main/native/NativeThread.c index dc21e5dbc90..6093b985e10 100644 --- a/ojluni/src/main/native/NativeThread.c +++ b/ojluni/src/main/native/NativeThread.c @@ -34,7 +34,9 @@ #ifdef __linux__ #include - #include + // Android-changed: Use correct include for signal.h + // #include + #include // Android-changed: Bionic (and AsynchronousCloseMonitor) expects libcore to use // __SIGRTMIN + 2, not __SIGRTMAX - 2 /* Also defined in net/linux_close.c */ diff --git a/ojluni/src/main/native/Net.c b/ojluni/src/main/native/Net.c index bcf984e957b..e1da723c4b2 100644 --- a/ojluni/src/main/native/Net.c +++ b/ojluni/src/main/native/Net.c @@ -23,13 +23,9 @@ * questions. */ -// Android-changed: Fuchsia: Point to correct location of header. http://b/119426171 +// Android-changed: Point to correct location of header. http://b/119426171 // #include -#if defined(__Fuchsia__) #include -#else -#include -#endif #include #include #include diff --git a/ojluni/src/main/native/jvm_md.h b/ojluni/src/main/native/jvm_md.h index 54743c64de1..20baad90d08 100644 --- a/ojluni/src/main/native/jvm_md.h +++ b/ojluni/src/main/native/jvm_md.h @@ -65,13 +65,9 @@ #include #include #include -// Android-changed: Fuchsia: Point to correct header location. http://b/119426171 +// Android-changed: Point to correct header location. http://b/119426171 // #include -#if !defined(__Fuchsia__) -#include -#else #include -#endif /* O Flags */ diff --git a/ojluni/src/main/native/linux_close.cpp b/ojluni/src/main/native/linux_close.cpp index a4d1457062f..c2d660b8ee0 100644 --- a/ojluni/src/main/native/linux_close.cpp +++ b/ojluni/src/main/native/linux_close.cpp @@ -35,13 +35,9 @@ #include #include -// Android-changed: Fuchsia: Fix poll.h include location +// Android-changed: Fix poll.h include location // #include -#if !defined(__Fuchsia__) -#include -#else #include -#endif #include diff --git a/ojluni/src/main/native/net_util_md.h b/ojluni/src/main/native/net_util_md.h index 0bef5b99964..67a6c97f072 100644 --- a/ojluni/src/main/native/net_util_md.h +++ b/ojluni/src/main/native/net_util_md.h @@ -33,14 +33,10 @@ #include #ifndef USE_SELECT -// Android-changed: Fuchsia: Point to correct location of header. http://b/119426171 +// Android-changed: Point to correct location of header. http://b/119426171 // #include -#if !defined(__Fuchsia__) -#include -#else #include #endif -#endif // Android-changed: Fuchsia: Use the non-JVM NET_* on Fuchsia also. // #if defined(__linux__) || defined(MACOSX)