Fixes to cmake build environment to filter out test drivers from sources,
[alexxy/gromacs.git] / cmake / gmxTestLargeFiles.cmake
1 # - Define macro to check large file support
2 #
3 #  GMX_TEST_LARGE_FILES(VARIABLE)
4 #
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  
10 #  HAVE_FSEEKO
11 #
12 #  However, it is YOUR job to make sure these defines are set in a cmakedefine so they
13 #  end up in a config.h file that is included in your source if necessary!
14
15 MACRO(GMX_TEST_LARGE_FILES VARIABLE)
16     IF("HAVE_${VARIABLE}" MATCHES "^HAVE_${VARIABLE}$")
17
18         # On most platforms it is probably overkill to first test the flags for 64-bit off_t,
19         # and then separately fseeko. However, in the future we might have 128-bit filesystems
20         # (ZFS), so it might be dangerous to indiscriminately set e.g. _FILE_OFFSET_BITS=64.
21
22         MESSAGE(STATUS "Checking for 64-bit off_t")
23
24         # First check without any special flags
25         TRY_COMPILE(FILE64_OK "${CMAKE_BINARY_DIR}"    
26                     "${CMAKE_SOURCE_DIR}/cmake/TestFileOffsetBits.c")
27         if(FILE64_OK)
28             MESSAGE(STATUS "Checking for 64-bit off_t - present")                       
29         endif(FILE64_OK)
30
31         if(NOT FILE64_OK)       
32             # Test with _FILE_OFFSET_BITS=64
33             TRY_COMPILE(FILE64_OK "${CMAKE_BINARY_DIR}"
34                         "${CMAKE_SOURCE_DIR}/cmake/TestFileOffsetBits.c"
35                         COMPILE_DEFINITIONS "-D_FILE_OFFSET_BITS=64" )
36             if(FILE64_OK)
37                 MESSAGE(STATUS "Checking for 64-bit off_t - present with _FILE_OFFSET_BITS=64")
38                 set(_FILE_OFFSET_BITS 64)
39             endif(FILE64_OK)
40         endif(NOT FILE64_OK)    
41
42         if(NOT FILE64_OK)
43             # Test with _LARGE_FILES
44             TRY_COMPILE(FILE64_OK "${CMAKE_BINARY_DIR}"
45                         "${CMAKE_SOURCE_DIR}/cmake/TestFileOffsetBits.c"
46                         COMPILE_DEFINITIONS "-D_LARGE_FILES" )
47             if(FILE64_OK)
48                 MESSAGE(STATUS "Checking for 64-bit off_t - present with _LARGE_FILES")
49                 set(_LARGE_FILES 1)
50             endif(FILE64_OK)
51         endif(NOT FILE64_OK)
52         
53         if(NOT FILE64_OK)
54             # Test with _LARGEFILE_SOURCE
55             TRY_COMPILE(FILE64_OK "${CMAKE_BINARY_DIR}"
56                         "${CMAKE_SOURCE_DIR}/cmake/TestFileOffsetBits.c"
57                         COMPILE_DEFINITIONS "-D_LARGEFILE_SOURCE" )
58             if(FILE64_OK)
59                 MESSAGE(STATUS "Checking for 64-bit off_t - present with _LARGEFILE_SOURCE")
60                                set(_LARGEFILE_SOURCE 1)
61             endif(FILE64_OK)
62         endif(NOT FILE64_OK)
63
64         if(NOT FILE64_OK)
65             MESSAGE(STATUS "Checking for 64-bit off_t - not present")
66         else(NOT FILE64_OK)
67
68             # Set the flags we might have determined to be required above
69             configure_file("${CMAKE_SOURCE_DIR}/cmake/TestLargeFiles.c.cmakein" 
70                            "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/TestLargeFiles.c")
71
72             MESSAGE(STATUS "Checking for fseeko/ftello")            
73             # Test if ftello/fseeko are available
74             TRY_COMPILE(HAVE_${VARIABLE} "${CMAKE_BINARY_DIR}"
75                         "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/TestLargeFiles.c")
76             if(HAVE_${VARIABLE})
77                 MESSAGE(STATUS "Checking for fseeko/ftello - present")
78             endif(HAVE_${VARIABLE})
79
80             if(NOT HAVE_${VARIABLE})
81                 # glibc 2.2 neds _LARGEFILE_SOURCE for fseeko (but not 64-bit off_t...)
82                 TRY_COMPILE(HAVE_${VARIABLE} "${CMAKE_BINARY_DIR}"
83                             "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/TestLargeFiles.c"
84                             COMPILE_DEFINITIONS "-D_LARGEFILE_SOURCE" )
85                 if(HAVE_${VARIABLE})
86                     MESSAGE(STATUS "Checking for fseeko/ftello - present with _LARGEFILE_SOURCE")
87                     set(_LARGEFILE_SOURCE 1)
88                 endif(HAVE_${VARIABLE})
89             endif(NOT HAVE_${VARIABLE})
90
91             if(HAVE_${VARIABLE})
92                 SET(${VARIABLE} 1 CACHE INTERNAL "Result of test for large file support" FORCE)
93                 set(HAVE_FSEEKO 1)
94             else(HAVE_${VARIABLE})
95                 MESSAGE(STATUS "Checking for fseeko/ftello - not found")
96                 SET(${VARIABLE} 0 CACHE INTERNAL "Result of test for large file support" FORCE)
97             endif(HAVE_${VARIABLE})
98
99         endif(NOT FILE64_OK)
100         
101     ENDIF("HAVE_${VARIABLE}" MATCHES "^HAVE_${VARIABLE}$")
102 ENDMACRO(GMX_TEST_LARGE_FILES VARIABLE)
103
104
105