Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable _GNU_SOURCE for Android #850

Merged
merged 1 commit into from
Jul 10, 2019
Merged

Enable _GNU_SOURCE for Android #850

merged 1 commit into from
Jul 10, 2019

Conversation

keith
Copy link
Contributor

@keith keith commented Jul 8, 2019

When targeting the Android NDK _GNU_SOURCE is not enabled by default:

 /*
  * With bionic, you always get all C and POSIX API.
  *
  * If you want BSD and/or GNU extensions, _BSD_SOURCE and/or _GNU_SOURCE are
  * expected to be defined by callers before *any* standard header file is
  * included.
  *
  * In our header files we test against __USE_BSD and __USE_GNU.
  */
 #if defined(_GNU_SOURCE)
 #  define __USE_BSD 1
 #  define __USE_GNU 1
 #endif

Because of this pipe2 is not available:

 #if defined(__USE_GNU)
 int pipe2(int __fds[2], int __flags) __INTRODUCED_IN(9);
 #endif

The function used to check if it does exist:

CHECK_FUNCTION_EXISTS_EX(pipe2 EVENT__HAVE_PIPE2)

Just check that the linking succeeds, which it does, it's just not
visible in the import, leading to a warning (or error):

evutil.c:2637:6: error: implicit declaration of function 'pipe2' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
        if (pipe2(fd, O_NONBLOCK|O_CLOEXEC) == 0)
            ^

When targeting the NDK it should be safe to always opt into this. Clang
would pass the right flag for us automatically if the source was C++
instead of C.

When targeting the Android NDK _GNU_SOURCE is not enabled by default:

```
 /*
  * With bionic, you always get all C and POSIX API.
  *
  * If you want BSD and/or GNU extensions, _BSD_SOURCE and/or _GNU_SOURCE are
  * expected to be defined by callers before *any* standard header file is
  * included.
  *
  * In our header files we test against __USE_BSD and __USE_GNU.
  */
 #if defined(_GNU_SOURCE)
 #  define __USE_BSD 1
 #  define __USE_GNU 1
 #endif
```

Because of this `pipe2` is not available:

```
 #if defined(__USE_GNU)
 int pipe2(int __fds[2], int __flags) __INTRODUCED_IN(9);
 #endif
```

The function used to check if it does exist:

```
CHECK_FUNCTION_EXISTS_EX(pipe2 EVENT__HAVE_PIPE2)
```

Just check that the _linking_ succeeds, which it does, it's just not
visible in the import, leading to a warning (or error):

```
evutil.c:2637:6: error: implicit declaration of function 'pipe2' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
        if (pipe2(fd, O_NONBLOCK|O_CLOEXEC) == 0)
            ^
```

When targeting the NDK it should be safe to always opt into this. Clang
would pass the right flag for us automatically _if_ the source was C++
instead of C.
@coveralls
Copy link

Coverage Status

Coverage increased (+0.007%) to 81.527% when pulling 41c95ab on keith:ks/android-gnu-source into 0d7d85c on libevent:master.

@keith
Copy link
Contributor Author

keith commented Jul 8, 2019

I don't think the red CI is related to this change, but let me know if it is!

keith added a commit to keith/envoy that referenced this pull request Jul 8, 2019
The Android NDK doesn't assume `_GNU_SOURCE`, clang only assumes it for
C++ builds, not C builds. libevent uses `pipe2` which in the NDK is only
exposed in the header when these variables are set. In bazel 0.27.0
these implicit use became an error.

More details:

- envoyproxy/envoy-mobile#116
- libevent/libevent#850

Signed-off-by: Keith Smiley <[email protected]>
@azat
Copy link
Member

azat commented Jul 9, 2019

Interesting, did you look into autotools build, IOW AC_GNU_SOURCE does the right thing?

@keith
Copy link
Contributor Author

keith commented Jul 9, 2019

I'm not super familiar with autotools, but it appears to since event-config.h ends up having:

/* Enable GNU extensions on systems that have them.  */
#ifndef _GNU_SOURCE
# define _GNU_SOURCE 1
#endif

lizan pushed a commit to envoyproxy/envoy that referenced this pull request Jul 9, 2019
The Android NDK doesn't assume `_GNU_SOURCE`, clang only assumes it for
C++ builds, not C builds. libevent uses `pipe2` which in the NDK is only
exposed in the header when these variables are set. In bazel 0.27.0
these implicit use became an error.

More details:

- envoyproxy/envoy-mobile#116
- libevent/libevent#850

Signed-off-by: Keith Smiley <[email protected]>
@ploxiln
Copy link
Contributor

ploxiln commented Jul 9, 2019

related: #773 and #781

@azat azat merged commit 41c95ab into libevent:master Jul 10, 2019
@azat
Copy link
Member

azat commented Jul 10, 2019

Applied, thanks!

@keith keith deleted the ks/android-gnu-source branch July 10, 2019 22:55
@keith
Copy link
Contributor Author

keith commented Jul 10, 2019

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

4 participants