cmake: Only provide USE_NSEC if struct stat members are avilable. This allows us to remove OS checks from source code, instead relying on CMake to detect whether or not `struct stat` has the nanoseconds members we rely on.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 5790a03..cb1f1b8 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -19,9 +19,13 @@ CMAKE_POLICY(SET CMP0015 NEW)
SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules/")
INCLUDE(CheckLibraryExists)
+INCLUDE(CheckStructHasMember)
INCLUDE(AddCFlagIfSupported)
INCLUDE(FindPkgConfig)
+CHECK_STRUCT_HAS_MEMBER("struct stat" st_mtim.tv_nsec sys/stat.h
+ HAVE_STRUCT_STAT_NSEC LANGUAGE C)
+
# Build options
#
OPTION( SONAME "Set the (SO)VERSION of the target" ON )
@@ -37,7 +41,9 @@ OPTION( LIBGIT2_FILENAME "Name of the produced binary" OFF )
OPTION( USE_ICONV "Link with and use iconv library" OFF )
OPTION( USE_SSH "Link with libssh to enable SSH support" ON )
OPTION( USE_GSSAPI "Link with libgssapi for SPNEGO auth" OFF )
-OPTION( USE_NSEC "Care about sub-second file mtimes and ctimes" OFF )
+IF(HAVE_STRUCT_STAT_NSEC)
+ OPTION( USE_NSEC "Care about sub-second file mtimes and ctimes" OFF )
+ENDIF()
OPTION( VALGRIND "Configure build for valgrind" OFF )
OPTION( CURL "User curl for HTTP if available" ON)
diff --git a/src/index.c b/src/index.c
index 54a8b1e..bd56cc7 100644
--- a/src/index.c
+++ b/src/index.c
@@ -858,8 +858,7 @@ void git_index_entry__init_from_stat(
{
entry->ctime.seconds = (git_time_t)st->st_ctime;
entry->mtime.seconds = (git_time_t)st->st_mtime;
-#if !defined(GIT_WIN32) && !defined(__APPLE__)
- /* Apple and Windows doesn't provide these struct stat fields. */
+#if defined(GIT_USE_NSEC)
entry->mtime.nanoseconds = st->st_mtim.tv_nsec;
entry->ctime.nanoseconds = st->st_ctim.tv_nsec;
#endif
diff --git a/tests/merge/workdir/dirty.c b/tests/merge/workdir/dirty.c
index ed402bd..f168963 100644
--- a/tests/merge/workdir/dirty.c
+++ b/tests/merge/workdir/dirty.c
@@ -164,8 +164,7 @@ static void hack_index(char *files[])
entry->ctime.seconds = (git_time_t)statbuf.st_ctime;
entry->mtime.seconds = (git_time_t)statbuf.st_mtime;
-#if !defined(GIT_WIN32) && !defined(__APPLE__)
- /* Apple and Windows doesn't provide these struct stat fields. */
+#if defined(GIT_USE_NSEC)
entry->ctime.nanoseconds = statbuf.st_ctim.tv_nsec;
entry->mtime.nanoseconds = statbuf.st_mtim.tv_nsec;
#else