Skip to content

Commit

Permalink
Fix libcore compiling against musl
Browse files Browse the repository at this point in the history
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
  • Loading branch information
colincross committed Sep 20, 2021
1 parent d4a531e commit 74f5a59
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 26 deletions.
4 changes: 3 additions & 1 deletion NativeCode.bp
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,11 @@ cc_defaults {
"-D_LARGEFILE64_SOURCE",
"-D_GNU_SOURCE",
"-DLINUX",
"-D__GLIBC__",
],
},
glibc: {
cflags: ["-D__GLIBC__"],
},
android: {
shared_libs: [
"libdl_android",
Expand Down
10 changes: 10 additions & 0 deletions luni/src/main/native/libcore_io_Linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
#include <android-base/file.h>
#include <android-base/logging.h>
#include <android-base/macros.h>
#include <android-base/stringprintf.h>
#include <android-base/strings.h>
#include <log/log.h>
#include <nativehelper/JNIPlatformHelp.h>
Expand Down Expand Up @@ -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) {
Expand Down
6 changes: 2 additions & 4 deletions ojluni/src/main/native/LinuxWatchService.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,10 @@
#include <dlfcn.h>
#include <sys/types.h>
#include <sys/socket.h>
// Android-changed: Fuchsia: Point to correct location of header. http://b/119426171
// Android-changed: Point to correct location of header. http://b/119426171
// #include <sys/poll.h>
#if defined(__Fuchsia__)
#include <poll.h>
#else
#include <sys/poll.h>
#if !defined(__Fuchsia__)
#include <sys/inotify.h>
#endif

Expand Down
4 changes: 3 additions & 1 deletion ojluni/src/main/native/NativeThread.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@

#ifdef __linux__
#include <pthread.h>
#include <sys/signal.h>
// Android-changed: Use correct include for signal.h
// #include <sys/signal.h>
#include <signal.h>
// Android-changed: Bionic (and AsynchronousCloseMonitor) expects libcore to use
// __SIGRTMIN + 2, not __SIGRTMAX - 2
/* Also defined in net/linux_close.c */
Expand Down
6 changes: 1 addition & 5 deletions ojluni/src/main/native/Net.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 <sys/poll.h>
#if defined(__Fuchsia__)
#include <poll.h>
#else
#include <sys/poll.h>
#endif
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
Expand Down
6 changes: 1 addition & 5 deletions ojluni/src/main/native/jvm_md.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,9 @@
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
// Android-changed: Fuchsia: Point to correct header location. http://b/119426171
// Android-changed: Point to correct header location. http://b/119426171
// #include <sys/signal.h>
#if !defined(__Fuchsia__)
#include <sys/signal.h>
#else
#include <signal.h>
#endif

/* O Flags */

Expand Down
6 changes: 1 addition & 5 deletions ojluni/src/main/native/linux_close.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,9 @@
#include <unistd.h>
#include <errno.h>

// Android-changed: Fuchsia: Fix poll.h include location
// Android-changed: Fix poll.h include location
// #include <sys/poll.h>
#if !defined(__Fuchsia__)
#include <sys/poll.h>
#else
#include <poll.h>
#endif

#include <AsynchronousCloseMonitor.h>

Expand Down
6 changes: 1 addition & 5 deletions ojluni/src/main/native/net_util_md.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,10 @@
#include <unistd.h>

#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 <sys/poll.h>
#if !defined(__Fuchsia__)
#include <sys/poll.h>
#else
#include <poll.h>
#endif
#endif

// Android-changed: Fuchsia: Use the non-JVM NET_* on Fuchsia also.
// #if defined(__linux__) || defined(MACOSX)
Expand Down

0 comments on commit 74f5a59

Please sign in to comment.