diff --git a/CMakeLists.txt b/CMakeLists.txt index 10236dec..53191441 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -102,6 +102,9 @@ list(APPEND uv_cflags ${lint-no-conditional-assignment-msvc}) list(APPEND uv_cflags ${lint-no-unsafe-msvc}) list(APPEND uv_cflags ${lint-utf8-msvc} ) +check_c_compiler_flag(-fno-strict-aliasing UV_F_STRICT_ALIASING) +list(APPEND uv_cflags $<$:-fno-strict-aliasing>) + set(uv_sources src/fs-poll.c src/idna.c @@ -325,7 +328,6 @@ if(CMAKE_SYSTEM_NAME STREQUAL "QNX") src/unix/bsd-ifaddrs.c src/unix/no-proctitle.c src/unix/no-fsevents.c) - list(APPEND uv_cflags -fno-strict-aliasing) list(APPEND uv_libraries socket) endif() diff --git a/README.md b/README.md index 98007c5e..f6c73709 100644 --- a/README.md +++ b/README.md @@ -286,6 +286,16 @@ listed in `test/benchmark-list.h`. Check the [SUPPORTED_PLATFORMS file](SUPPORTED_PLATFORMS.md). +### `-fno-strict-aliasing` + +It is recommended to turn on the `-fno-strict-aliasing` compiler flag in +projects that use libuv. The use of ad hoc "inheritance" in the libuv API +may not be safe in the presence of compiler optimizations that depend on +strict aliasing. + +MSVC does not have an equivalent flag but it also does not appear to need it +at the time of writing (December 2019.) + ### AIX Notes AIX compilation using IBM XL C/C++ requires version 12.1 or greater. diff --git a/configure.ac b/configure.ac index 1a66b74d..007c07ea 100644 --- a/configure.ac +++ b/configure.ac @@ -25,6 +25,7 @@ AC_ENABLE_STATIC AC_PROG_CC AM_PROG_CC_C_O CC_FLAG_VISIBILITY #[-fvisibility=hidden] +CC_CHECK_CFLAGS_APPEND([-fno-strict-aliasing]) CC_CHECK_CFLAGS_APPEND([-g]) CC_CHECK_CFLAGS_APPEND([-std=gnu89]) CC_CHECK_CFLAGS_APPEND([-Wall])