google / cctz

CCTZ is a C++ library for translating between absolute and civil times using the rules of a time zone.
Apache License 2.0
597 stars 166 forks source link

Commit 6355d60 broke FreeBSD builds #270

Closed matthewjmiller1 closed 1 year ago

matthewjmiller1 commented 1 year ago

This commit regressed FreeBSD builds (I noticed this when attempting to build as part of abseil-cpp).

Attempting to compile on FreeBSD 13.1 gives:

$ freebsd-version
13.1-RELEASE-p5
$ cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_POSITION_INDEPENDENT_CODE=ON -DBUILD_TESTING=OFF  ..
-- The C compiler identification is Clang 13.0.0
-- The CXX compiler identification is Clang 13.0.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done (1.3s)
-- Generating done (0.4s)
-- Build files have been written to: /u/matthew6/tmp/google-cctz-17c7dbc/mybuild
$ cmake --build . --config Release
[  3%] Building CXX object CMakeFiles/cctz.dir/src/civil_time_detail.cc.o
[  7%] Building CXX object CMakeFiles/cctz.dir/src/time_zone_fixed.cc.o
[ 11%] Building CXX object CMakeFiles/cctz.dir/src/time_zone_format.cc.o
In file included from /u/matthew6/tmp/google-cctz-17c7dbc/src/time_zone_format.cc:27:
In file included from /u/matthew6/tmp/google-cctz-17c7dbc/include/cctz/time_zone.h:27:
In file included from /usr/include/c++/v1/string:520:
In file included from /usr/include/c++/v1/__functional_base:23:
/usr/include/c++/v1/new:317:11: error: no member named 'posix_memalign' in the global namespace
  (void)::posix_memalign(&__result, __alignment, __size);
        ~~^
1 error generated.
*** Error code 1

Stop.
make[2]: stopped in /u/matthew6/tmp/google-cctz-17c7dbc/mybuild
*** Error code 1

Stop.
make[1]: stopped in /u/matthew6/tmp/google-cctz-17c7dbc/mybuild
*** Error code 1

Stop.
make: stopped in /u/matthew6/tmp/google-cctz-17c7dbc/mybuild
$ 

However, if I set a more recent _XOPEN_SOURCE value, then the FreeBSD build succeeds:

$ cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_POSITION_INDEPENDENT_CODE=ON -DBUILD_TESTING=OFF -DCMAKE_CXX_FLAGS="-D_XOPEN_SOURCE=700"   ..
-- Configuring done (0.0s)
-- Generating done (0.4s)
-- Build files have been written to: /u/matthew6/tmp/google-cctz-17c7dbc/mybuild
$ cmake --build . --config Release
[  3%] Building CXX object CMakeFiles/cctz.dir/src/civil_time_detail.cc.o
[  7%] Building CXX object CMakeFiles/cctz.dir/src/time_zone_fixed.cc.o
[ 11%] Building CXX object CMakeFiles/cctz.dir/src/time_zone_format.cc.o
[ 14%] Building CXX object CMakeFiles/cctz.dir/src/time_zone_if.cc.o
[ 18%] Building CXX object CMakeFiles/cctz.dir/src/time_zone_impl.cc.o
[ 22%] Building CXX object CMakeFiles/cctz.dir/src/time_zone_info.cc.o
[ 25%] Building CXX object CMakeFiles/cctz.dir/src/time_zone_libc.cc.o
[ 29%] Building CXX object CMakeFiles/cctz.dir/src/time_zone_lookup.cc.o
[ 33%] Building CXX object CMakeFiles/cctz.dir/src/time_zone_posix.cc.o
[ 37%] Building CXX object CMakeFiles/cctz.dir/src/zone_info_source.cc.o
[ 40%] Linking CXX static library libcctz.a
[ 40%] Built target cctz
[ 44%] Building CXX object CMakeFiles/time_tool.dir/src/time_tool.cc.o
[ 48%] Linking CXX executable time_tool
[ 48%] Built target time_tool
[ 51%] Building CXX object examples/CMakeFiles/classic.dir/classic.cc.o
[ 55%] Linking CXX executable classic
[ 55%] Built target classic
[ 59%] Building CXX object examples/CMakeFiles/hello.dir/hello.cc.o
[ 62%] Linking CXX executable hello
[ 62%] Built target hello
[ 66%] Building CXX object examples/CMakeFiles/epoch_shift.dir/epoch_shift.cc.o
[ 70%] Linking CXX executable epoch_shift
[ 70%] Built target epoch_shift
[ 74%] Building CXX object examples/CMakeFiles/example1.dir/example1.cc.o
[ 77%] Linking CXX executable example1
[ 77%] Built target example1
[ 81%] Building CXX object examples/CMakeFiles/example2.dir/example2.cc.o
[ 85%] Linking CXX executable example2
[ 85%] Built target example2
[ 88%] Building CXX object examples/CMakeFiles/example3.dir/example3.cc.o
[ 92%] Linking CXX executable example3
[ 92%] Built target example3
[ 96%] Building CXX object examples/CMakeFiles/example4.dir/example4.cc.o
[100%] Linking CXX executable example4
[100%] Built target example4
$ 
devbww commented 1 year ago

I don't know why the FreeBSD headers are trying to use posix_memalign() when _XOPEN_SOURCE=500. It looks like it is first available in 600.

That said, I'm keen to make things work for you in any case. Perhaps take a look at #224 and see if __FreeBSD__ might deserve similar logic to __OpenBSD__. That is, does FreeBSD provide strptime() by default?

matthewjmiller1 commented 1 year ago

It seems so.

$ cat test_strptime.cc
#undef _GNU_SOURCE

#ifdef __linux__
#define _XOPEN_SOURCE 500
#endif

#include <time.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

int
main(int argc, char **argv)
{
   struct tm tm;
   char buf[255];

#ifdef _XOPEN_SOURCE
   printf("_XOPEN_SOURCE: %u\n", _XOPEN_SOURCE);
#endif
   memset(&tm, 0, sizeof(tm));
   strptime("2001-11-12 18:31:01", "%Y-%m-%d %H:%M:%S", &tm);
   strftime(buf, sizeof(buf), "%d %b %Y %H:%M", &tm);
   puts(buf);

   exit(EXIT_SUCCESS);
}
$

On Linux:

$ clang++ test_strptime.cc
$ ./a.out
_XOPEN_SOURCE: 500
12 Nov 2001 18:31
$

On FreeBSD:

$ clang++ test_strptime.cc
$ ./a.out
12 Nov 2001 18:31
$
matthewjmiller1 commented 1 year ago

It seems to build OK when adding __FreeBSD__ to that logic (and leaving _XOPEN_SOURCE undefined):

#if defined(HAS_STRPTIME) && HAS_STRPTIME
# if !defined(_XOPEN_SOURCE) && !defined(__OpenBSD__) && !defined(__FreeBSD__)
#  define _XOPEN_SOURCE 500  // Exposes definitions for SUSv2 (UNIX 98).
# endif
#endif
devbww commented 1 year ago

Thanks. #271 out for review.