From acc0ac84d9ddee95f4e38d7a9db2831a61d33205 Mon Sep 17 00:00:00 2001 From: Vladimir Tamara Date: Thu, 26 Mar 2020 13:19:32 -0500 Subject: [PATCH] Uses CXX17 if it can compile test program using timespec_get and TIME_UTC, otherwise by default uses CXX11. Fixes #115 --- configure | 40 ++++++++++++++++++++++++++++++++++++++-- src/Makevars.in | 3 +-- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/configure b/configure index 85c118c..28ccab3 100755 --- a/configure +++ b/configure @@ -3,7 +3,7 @@ echo "Running configure script" # Find compiler -CC=$("${R_HOME}"/bin/R CMD config CC) +CC=`"${R_HOME}"/bin/R CMD config CC` # Detect whether -latomic is needed during linking. This is needed on some # platforms, notably ARM (Raspberry Pi). @@ -21,8 +21,44 @@ else fi +CXX=`"${R_HOME}"/bin/R CMD config CXX` +CXX11STD=`"${R_HOME}"/bin/R CMD config CXX11STD` +CXX11FLAGS=`"${R_HOME}"/bin/R CMD config CXX11FLAGS` +CXX_STD=CXX11 +if [ "$CXX" != "" -a "$CXX11FLAGS" != "" -a "$CXX11STD" != "" ]; then + # Detect if it can use c++17 + ptimespec='#include +#include +// Example adapted from https://en.cppreference.com/w/cpp/chrono/c/timespec_get + +namespace std { + + int main() + { + timespec ts; + timespec_get(&ts, TIME_UTC); + char buf[100]; + strftime(buf, sizeof buf, "%D %T", gmtime(&ts.tv_sec)); + printf("Current time: %s.%09ld UTC", buf, ts.tv_nsec); + return 0; + } + +}' + CXX17FLAGS=`"${R_HOME}"/bin/R CMD config CXX17FLAGS` + CXX17STD=`"${R_HOME}"/bin/R CMD config CXX17STD` + echo "$ptimespec" | ${CXX} -x c++ -c $CXX17FLAGS $CXX17STD - -o /dev/null > /dev/null 2>&1 + if [ $? -eq 0 ]; then + # See https://github.com/r-lib/later/issues/115 + echo "Using c++17." + CXX_STD=CXX17 + else + echo "Cannot use c++17 using default configuration for c++11" + fi +fi + + # Write to Makevars -sed -e "s|@extra_pkg_libs@|$EXTRA_PKG_LIBS|" src/Makevars.in > src/Makevars +sed -e "s|@extra_pkg_libs@|$EXTRA_PKG_LIBS|;s|@cxx_std@|$CXX_STD|" src/Makevars.in > src/Makevars # Success exit 0 diff --git a/src/Makevars.in b/src/Makevars.in index f3d2a50..9227f2e 100644 --- a/src/Makevars.in +++ b/src/Makevars.in @@ -1,5 +1,4 @@ -# Use C++11 if available -CXX_STD=CXX11 +CXX_STD=@cxx_std@ PKG_CPPFLAGS = -pthread -DSTRICT_R_HEADERS PKG_LIBS = -pthread @extra_pkg_libs@