1 # SPDX-License-Identifier: 0BSD
3 #############################################################################
5 # tuklib_large_file_support.cmake
7 # If off_t is less than 64 bits by default and -D_FILE_OFFSET_BITS=64
8 # makes off_t become 64-bit, the CMake option LARGE_FILE_SUPPORT is
9 # provided (ON by default) and -D_FILE_OFFSET_BITS=64 is added to
10 # the compile definitions if LARGE_FILE_SUPPORT is ON.
12 # Author: Lasse Collin
14 #############################################################################
16 include("${CMAKE_CURRENT_LIST_DIR}/tuklib_common.cmake")
17 include(CheckCSourceCompiles)
19 function(tuklib_large_file_support TARGET_OR_ALL)
20 # MSVC must be handled specially in the C code.
25 set(TUKLIB_LARGE_FILE_SUPPORT_TEST
26 "#include <sys/types.h>
27 int foo[sizeof(off_t) >= 8 ? 1 : -1];
28 int main(void) { return 0; }")
30 check_c_source_compiles("${TUKLIB_LARGE_FILE_SUPPORT_TEST}"
31 TUKLIB_LARGE_FILE_SUPPORT_BY_DEFAULT)
33 if(NOT TUKLIB_LARGE_FILE_SUPPORT_BY_DEFAULT)
34 cmake_push_check_state()
36 list(APPEND CMAKE_REQUIRED_DEFINITIONS "-D_FILE_OFFSET_BITS=64")
37 check_c_source_compiles("${TUKLIB_LARGE_FILE_SUPPORT_TEST}"
38 TUKLIB_LARGE_FILE_SUPPORT_WITH_FOB64)
39 cmake_pop_check_state()
42 if(TUKLIB_LARGE_FILE_SUPPORT_WITH_FOB64)
43 # Show the option only when _FILE_OFFSET_BITS=64 affects sizeof(off_t).
44 option(LARGE_FILE_SUPPORT
45 "Use -D_FILE_OFFSET_BITS=64 to support files larger than 2 GiB."
48 if(LARGE_FILE_SUPPORT)
49 # This must not use -D.
50 tuklib_add_definitions("${TARGET_OR_ALL}" "_FILE_OFFSET_BITS=64")