From 11696e4c91adcc27457ace5636e354364bde9a50 Mon Sep 17 00:00:00 2001 From: Michel Hidalgo Date: Thu, 16 Jul 2020 18:57:08 -0300 Subject: [PATCH 1/5] Force _GNU_SOURCE if glibc is used. Signed-off-by: Michel Hidalgo --- CMakeLists.txt | 10 ++++++ .../rcutils_check_c_compiler_uses_glibc.cmake | 36 +++++++++++++++++++ src/process.c | 2 ++ 3 files changed, 48 insertions(+) create mode 100644 cmake/rcutils_check_c_compiler_uses_glibc.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 0f7429cf..1075bc0d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,6 +18,16 @@ find_package(ament_cmake_ros REQUIRED) ament_python_install_package(${PROJECT_NAME}) +if(UNIX) + include(cmake/rcutils_check_c_compiler_uses_glibc.cmake) + rcutils_check_c_compiler_uses_glibc(USES_GLIBC) + if (USES_GLIBC) + # Ensure GNU extended libc API is used, as C++ test code will. + # See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=2082. + add_definitions(-D_GNU_SOURCE) + endif() +endif() + if(NOT WIN32) add_compile_options(-Wall -Wextra -Wpedantic) endif() diff --git a/cmake/rcutils_check_c_compiler_uses_glibc.cmake b/cmake/rcutils_check_c_compiler_uses_glibc.cmake new file mode 100644 index 00000000..6e83ea7a --- /dev/null +++ b/cmake/rcutils_check_c_compiler_uses_glibc.cmake @@ -0,0 +1,36 @@ +# Copyright 2020 Open Source Robotics Foundation, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# +# Checks if the C compiler uses glibc. +# +# @private +# +function(rcutils_check_c_compiler_uses_glibc result_variable) + include(CheckCSourceCompiles) + + set(GLIBC_TEST_CODE [====[ + #include + + int main() { + void * buffer[1]; + int size = sizeof(buffer) / sizeof(void *); + backtrace(&buffer, size); + return 0; + } + ]====]) + + check_c_source_compiles("${GLIBC_TEST_CODE}" ${result_variable}) + set(${result_variable} ${result_variable} PARENT_SCOPE) +endfunction() diff --git a/src/process.c b/src/process.c index 832c3d1a..cf987cf6 100644 --- a/src/process.c +++ b/src/process.c @@ -17,7 +17,9 @@ extern "C" { #endif +#ifndef _GNU_SOURCE #define _GNU_SOURCE +#endif #include #include #include From b20b46d1dbe01c90f2f70e155fa0d7d174b2853c Mon Sep 17 00:00:00 2001 From: Michel Hidalgo Date: Fri, 17 Jul 2020 10:49:21 -0300 Subject: [PATCH 2/5] Please lint_cmake Signed-off-by: Michel Hidalgo --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1075bc0d..e75d8906 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -21,7 +21,7 @@ ament_python_install_package(${PROJECT_NAME}) if(UNIX) include(cmake/rcutils_check_c_compiler_uses_glibc.cmake) rcutils_check_c_compiler_uses_glibc(USES_GLIBC) - if (USES_GLIBC) + if(USES_GLIBC) # Ensure GNU extended libc API is used, as C++ test code will. # See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=2082. add_definitions(-D_GNU_SOURCE) From 6bb8833696cdf3bcb386532cf4159847a57b9f8b Mon Sep 17 00:00:00 2001 From: Michel Hidalgo Date: Fri, 17 Jul 2020 16:46:25 -0300 Subject: [PATCH 3/5] Drop _GNU_SOURCE define in process.c Signed-off-by: Michel Hidalgo --- src/process.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/process.c b/src/process.c index cf987cf6..e821120d 100644 --- a/src/process.c +++ b/src/process.c @@ -17,9 +17,6 @@ extern "C" { #endif -#ifndef _GNU_SOURCE -#define _GNU_SOURCE -#endif #include #include #include From b86881ecb7b49bbdafbe447bfc71bf3f60216c1b Mon Sep 17 00:00:00 2001 From: Michel Hidalgo Date: Fri, 17 Jul 2020 16:48:33 -0300 Subject: [PATCH 4/5] Rename glibc checking cmake function. Signed-off-by: Michel Hidalgo --- CMakeLists.txt | 4 ++-- ...ler_uses_glibc.cmake => check_c_compiler_uses_glibc.cmake} | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) rename cmake/{rcutils_check_c_compiler_uses_glibc.cmake => check_c_compiler_uses_glibc.cmake} (94%) diff --git a/CMakeLists.txt b/CMakeLists.txt index e75d8906..9aad20e7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,8 +19,8 @@ find_package(ament_cmake_ros REQUIRED) ament_python_install_package(${PROJECT_NAME}) if(UNIX) - include(cmake/rcutils_check_c_compiler_uses_glibc.cmake) - rcutils_check_c_compiler_uses_glibc(USES_GLIBC) + include(cmake/check_c_compiler_uses_glibc.cmake) + check_c_compiler_uses_glibc(USES_GLIBC) if(USES_GLIBC) # Ensure GNU extended libc API is used, as C++ test code will. # See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=2082. diff --git a/cmake/rcutils_check_c_compiler_uses_glibc.cmake b/cmake/check_c_compiler_uses_glibc.cmake similarity index 94% rename from cmake/rcutils_check_c_compiler_uses_glibc.cmake rename to cmake/check_c_compiler_uses_glibc.cmake index 6e83ea7a..46755bec 100644 --- a/cmake/rcutils_check_c_compiler_uses_glibc.cmake +++ b/cmake/check_c_compiler_uses_glibc.cmake @@ -17,7 +17,7 @@ # # @private # -function(rcutils_check_c_compiler_uses_glibc result_variable) +function(check_c_compiler_uses_glibc result_variable) include(CheckCSourceCompiles) set(GLIBC_TEST_CODE [====[ From ea6b5c7f301105c14dc9964f5cf9728a7877942d Mon Sep 17 00:00:00 2001 From: Michel Hidalgo Date: Mon, 20 Jul 2020 09:36:41 -0300 Subject: [PATCH 5/5] Do not check for glibc on Mac OS X. Signed-off-by: Michel Hidalgo --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9aad20e7..a7ffe516 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,7 +18,7 @@ find_package(ament_cmake_ros REQUIRED) ament_python_install_package(${PROJECT_NAME}) -if(UNIX) +if(UNIX AND NOT APPLE) include(cmake/check_c_compiler_uses_glibc.cmake) check_c_compiler_uses_glibc(USES_GLIBC) if(USES_GLIBC)