T1: fix BYPASS/LAZY, TERMALL/RESTART and PTERM/ERTERM encoding modes. (#674)
[openjpeg.git] / cmake / TestLargeFiles.cmake
1 # - Define macro to check large file support
2 #
3 #  OPJ_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 defines the necessary variable enable large file support, for instance
7 #  _LARGE_FILES
8 #  _LARGEFILE_SOURCE
9 #  _FILE_OFFSET_BITS 64
10 #  OPJ_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 #  Adapted from Gromacs project (http://www.gromacs.org/)
16 #  by Julien Malik
17 #
18
19 macro(OPJ_TEST_LARGE_FILES VARIABLE)
20     if(NOT DEFINED ${VARIABLE})
21
22         # On most platforms it is probably overkill to first test the flags for 64-bit off_t,
23         # and then separately fseeko. However, in the future we might have 128-bit filesystems
24         # (ZFS), so it might be dangerous to indiscriminately set e.g. _FILE_OFFSET_BITS=64.
25
26         message(STATUS "Checking for 64-bit off_t")
27
28         # First check without any special flags
29         try_compile(FILE64_OK "${PROJECT_BINARY_DIR}"
30                     "${PROJECT_SOURCE_DIR}/cmake/TestFileOffsetBits.c")
31         if(FILE64_OK)
32           message(STATUS "Checking for 64-bit off_t - present")
33         endif()
34
35         if(NOT FILE64_OK)
36             # Test with _FILE_OFFSET_BITS=64
37             try_compile(FILE64_OK "${PROJECT_BINARY_DIR}"
38                         "${PROJECT_SOURCE_DIR}/cmake/TestFileOffsetBits.c"
39                         COMPILE_DEFINITIONS "-D_FILE_OFFSET_BITS=64" )
40             if(FILE64_OK)
41                 message(STATUS "Checking for 64-bit off_t - present with _FILE_OFFSET_BITS=64")
42                 set(_FILE_OFFSET_BITS 64)
43             endif()
44         endif()
45
46         if(NOT FILE64_OK)
47             # Test with _LARGE_FILES
48             try_compile(FILE64_OK "${PROJECT_BINARY_DIR}"
49                         "${PROJECT_SOURCE_DIR}/cmake/TestFileOffsetBits.c"
50                         COMPILE_DEFINITIONS "-D_LARGE_FILES" )
51             if(FILE64_OK)
52                 message(STATUS "Checking for 64-bit off_t - present with _LARGE_FILES")
53                 set(_LARGE_FILES 1)
54             endif()
55         endif()
56         
57         if(NOT FILE64_OK)
58             # Test with _LARGEFILE_SOURCE
59             try_compile(FILE64_OK "${PROJECT_BINARY_DIR}"
60                         "${PROJECT_SOURCE_DIR}/cmake/TestFileOffsetBits.c"
61                         COMPILE_DEFINITIONS "-D_LARGEFILE_SOURCE" )
62             if(FILE64_OK)
63                 message(STATUS "Checking for 64-bit off_t - present with _LARGEFILE_SOURCE")
64                 set(_LARGEFILE_SOURCE 1)
65             endif()
66         endif()
67
68
69         #if(NOT FILE64_OK)
70         #    # now check for Windows stuff
71         #    try_compile(FILE64_OK "${PROJECT_BINARY_DIR}"
72         #                "${PROJECT_SOURCE_DIR}/cmake/TestWindowsFSeek.c")
73         #    if(FILE64_OK)
74         #        message(STATUS "Checking for 64-bit off_t - present with _fseeki64")
75         #        set(HAVE__FSEEKI64 1)
76         #    endif()
77         #endif()
78
79         if(NOT FILE64_OK)
80             message(STATUS "Checking for 64-bit off_t - not present")
81         endif()
82
83         set(_FILE_OFFSET_BITS ${_FILE_OFFSET_BITS} CACHE INTERNAL "Result of test for needed _FILE_OFFSET_BITS=64")
84         set(_LARGE_FILES      ${_LARGE_FILES}      CACHE INTERNAL "Result of test for needed _LARGE_FILES")
85         set(_LARGEFILE_SOURCE ${_LARGEFILE_SOURCE} CACHE INTERNAL "Result of test for needed _LARGEFILE_SOURCE")
86
87         # Set the flags we might have determined to be required above
88         configure_file("${PROJECT_SOURCE_DIR}/cmake/TestLargeFiles.c.cmake.in"
89                        "${PROJECT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/TestLargeFiles.c")
90
91         message(STATUS "Checking for fseeko/ftello")
92
93             # Test if ftello/fseeko are available
94             try_compile(FSEEKO_COMPILE_OK
95                         "${PROJECT_BINARY_DIR}"
96                     "${PROJECT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/TestLargeFiles.c")
97         
98             if(FSEEKO_COMPILE_OK)
99             message(STATUS "Checking for fseeko/ftello - present")
100         endif()
101
102         if(NOT FSEEKO_COMPILE_OK)
103                 # glibc 2.2 needs _LARGEFILE_SOURCE for fseeko (but not for 64-bit off_t...)
104                 try_compile(FSEEKO_COMPILE_OK
105                             "${PROJECT_BINARY_DIR}"
106                             "${PROJECT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/TestLargeFiles.c"
107                             COMPILE_DEFINITIONS "-D_LARGEFILE_SOURCE" )
108
109                 if(FSEEKO_COMPILE_OK)
110                     message(STATUS "Checking for fseeko/ftello - present with _LARGEFILE_SOURCE")
111                     set(_LARGEFILE_SOURCE ${_LARGEFILE_SOURCE} CACHE INTERNAL "Result of test for needed _LARGEFILE_SOURCE")
112                 endif()
113         endif()
114
115             if(FSEEKO_COMPILE_OK)
116                 set(OPJ_HAVE_FSEEKO ON CACHE INTERNAL "Result of test for fseeko/ftello")
117         else()
118                 message(STATUS "Checking for fseeko/ftello - not found")
119                 set(OPJ_HAVE_FSEEKO OFF CACHE INTERNAL "Result of test for fseeko/ftello")
120         endif()
121
122             if(FILE64_OK AND FSEEKO_COMPILE_OK)
123                 message(STATUS "Large File support - found")
124                 set(${VARIABLE} ON CACHE INTERNAL "Result of test for large file support")
125         else()
126                 message(STATUS "Large File support - not found")
127                 set(${VARIABLE} OFF CACHE INTERNAL "Result of test for large file support")
128         endif()
129
130     endif()
131 endmacro()
132
133
134