Merge release-4-6 into master
[gromacs.git] / cmake / gmxTestLargeFiles.cmake
blob1c3681254b34878c9019a903167a5e0bb5467ad2
1 # - Define macro to check large file support
3 #  GMX_TEST_LARGE_FILES(VARIABLE)
5 #  VARIABLE will be set to true if off_t is 64 bits, and fseeko/ftello present.
6 #  This macro will also set defines necessary enable large file support, for instance
7 #  _LARGE_FILES
8 #  _LARGEFILE_SOURCE
9 #  _FILE_OFFSET_BITS 64  
11 #  However, it is YOUR job to make sure these defines are set in a cmakedefine so they
12 #  end up in a config.h file that is included in your source if necessary!
14 MACRO(GMX_TEST_LARGE_FILES VARIABLE)
15     IF(NOT DEFINED ${VARIABLE})
17         # On most platforms it is probably overkill to first test the flags for 64-bit off_t,
18         # and then separately fseeko. However, in the future we might have 128-bit filesystems
19         # (ZFS), so it might be dangerous to indiscriminately set e.g. _FILE_OFFSET_BITS=64.
21         MESSAGE(STATUS "Checking for 64-bit off_t")
23         # First check without any special flags
24         TRY_COMPILE(FILE64_OK "${CMAKE_BINARY_DIR}"    
25                     "${CMAKE_SOURCE_DIR}/cmake/TestFileOffsetBits.c")
26         if(FILE64_OK)
27             MESSAGE(STATUS "Checking for 64-bit off_t - present")                       
28         endif(FILE64_OK)
30         if(NOT FILE64_OK)       
31             # Test with _FILE_OFFSET_BITS=64
32             TRY_COMPILE(FILE64_OK "${CMAKE_BINARY_DIR}"
33                         "${CMAKE_SOURCE_DIR}/cmake/TestFileOffsetBits.c"
34                         COMPILE_DEFINITIONS "-D_FILE_OFFSET_BITS=64" )
35             if(FILE64_OK)
36                 MESSAGE(STATUS "Checking for 64-bit off_t - present with _FILE_OFFSET_BITS=64")
37                 set(_FILE_OFFSET_BITS 64 CACHE INTERNAL "64-bit off_t requires _FILE_OFFSET_BITS=64")
38             endif(FILE64_OK)
39         endif(NOT FILE64_OK)    
41         if(NOT FILE64_OK)
42             # Test with _LARGE_FILES
43             TRY_COMPILE(FILE64_OK "${CMAKE_BINARY_DIR}"
44                         "${CMAKE_SOURCE_DIR}/cmake/TestFileOffsetBits.c"
45                         COMPILE_DEFINITIONS "-D_LARGE_FILES" )
46             if(FILE64_OK)
47                 MESSAGE(STATUS "Checking for 64-bit off_t - present with _LARGE_FILES")
48                 set(_LARGE_FILES 1 CACHE INTERNAL "64-bit off_t requires _LARGE_FILES")
49             endif(FILE64_OK)
50         endif(NOT FILE64_OK)
51         
52         if(NOT FILE64_OK)
53             # Test with _LARGEFILE_SOURCE
54             TRY_COMPILE(FILE64_OK "${CMAKE_BINARY_DIR}"
55                         "${CMAKE_SOURCE_DIR}/cmake/TestFileOffsetBits.c"
56                         COMPILE_DEFINITIONS "-D_LARGEFILE_SOURCE" )
57             if(FILE64_OK)
58                 MESSAGE(STATUS "Checking for 64-bit off_t - present with _LARGEFILE_SOURCE")
59                 set(_LARGEFILE_SOURCE 1 CACHE INTERNAL "64-bit off_t requires _LARGEFILE_SOURCE")
60             endif(FILE64_OK)
61         endif(NOT FILE64_OK)
63         if(NOT FILE64_OK)
64             # now check for Windows stuff
65             TRY_COMPILE(FILE64_OK "${CMAKE_BINARY_DIR}"
66                         "${CMAKE_SOURCE_DIR}/cmake/TestWindowsFSeek.c")
67             if(FILE64_OK)
68                 MESSAGE(STATUS "Checking for 64-bit off_t - present with _fseeki64")
69                 set(HAVE__FSEEKI64 1 CACHE INTERNAL "64-bit off_t requires _fseeki64")
70             endif(FILE64_OK)
71         endif(NOT FILE64_OK)
73         if(NOT FILE64_OK)
74             MESSAGE(STATUS "Checking for 64-bit off_t - not present")
75         else(NOT FILE64_OK)
77             # Set the flags we might have determined to be required above
78             configure_file("${CMAKE_SOURCE_DIR}/cmake/TestLargeFiles.c.cmakein" 
79                            "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/TestLargeFiles.c")
81             MESSAGE(STATUS "Checking for fseeko/ftello")            
82             # Test if ftello/fseeko are available
83             TRY_COMPILE(FSEEKO_COMPILE_OK "${CMAKE_BINARY_DIR}"
84                         "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/TestLargeFiles.c")
85             if(FSEEKO_COMPILE_OK)
86                 MESSAGE(STATUS "Checking for fseeko/ftello - present")
87             endif(FSEEKO_COMPILE_OK)
89             if(NOT FSEEKO_COMPILE_OK)
90                 # glibc 2.2 neds _LARGEFILE_SOURCE for fseeko (but not 64-bit off_t...)
91                 TRY_COMPILE(FSEEKO_COMPILE_OK "${CMAKE_BINARY_DIR}"
92                             "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/TestLargeFiles.c"
93                             COMPILE_DEFINITIONS "-D_LARGEFILE_SOURCE" )
94                 if(FSEEKO_COMPILE_OK)
95                     MESSAGE(STATUS "Checking for fseeko/ftello - present with _LARGEFILE_SOURCE")
96                     set(_LARGEFILE_SOURCE 1 CACHE INTERNAL "64-bit fseeko requires _LARGEFILE_SOURCE")
97                 endif(FSEEKO_COMPILE_OK)
98             endif(NOT FSEEKO_COMPILE_OK)
100         endif(NOT FILE64_OK)
102         if(FSEEKO_COMPILE_OK)
103             SET(${VARIABLE} 1 CACHE INTERNAL "Result of test for large file support" FORCE)
104             set(HAVE_FSEEKO 1 CACHE INTERNAL "64bit fseeko is available" FORCE)
105         else(FSEEKO_COMPILE_OK)
106             if (HAVE__FSEEKI64)
107                 SET(${VARIABLE} 1 CACHE INTERNAL "Result of test for large file support" FORCE)
108                 SET(HAVE__FSEEKI64 1 CACHE INTERNAL "Windows 64-bit fseek" FORCE)
109             else (HAVE__FSEEKI64)
110                 MESSAGE(STATUS "Checking for fseeko/ftello - not found")
111                 SET(${VARIABLE} 0 CACHE INTERNAL "Result of test for large file support" FORCE)
112             endif (HAVE__FSEEKI64)
113         endif(FSEEKO_COMPILE_OK)
115     ENDIF(NOT DEFINED ${VARIABLE})
116 ENDMACRO(GMX_TEST_LARGE_FILES VARIABLE)