1 # CMake support for large files
3 # Copyright (C) 2016 Julian Andres Klode <jak@debian.org>.
5 # Permission is hereby granted, free of charge, to any person
6 # obtaining a copy of this software and associated documentation files
7 # (the "Software"), to deal in the Software without restriction,
8 # including without limitation the rights to use, copy, modify, merge,
9 # publish, distribute, sublicense, and/or sell copies of the Software,
10 # and to permit persons to whom the Software is furnished to do so,
11 # subject to the following conditions:
13 # The above copyright notice and this permission notice shall be
14 # included in all copies or substantial portions of the Software.
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20 # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 # This defines the following variables
27 # LFS_DEFINITIONS - List of definitions to pass to add_definitions()
28 # LFS_COMPILE_OPTIONS - List of definitions to pass to add_compile_options()
29 # LFS_LIBRARIES - List of libraries and linker flags
30 # LFS_FOUND - If there is Large files support
33 include(CheckCSourceCompiles)
34 include(FindPackageHandleStandardArgs)
35 include(CMakePushCheckState)
37 # Test program to check for LFS. Requires that off_t has at least 8 byte large
40 #include <sys/types.h>
41 typedef char my_static_assert[sizeof(off_t) >= 8 ? 1 : -1];
42 int main(void) { return 0; }
46 # Check if the given options are needed
48 # This appends to the variables _lfs_cppflags, _lfs_cflags, and _lfs_ldflags,
49 # it also sets LFS_FOUND to 1 if it works.
50 function(_lfs_check_compiler_option var options definitions libraries)
51 cmake_push_check_state()
52 set(CMAKE_REQUIRED_QUIET 1)
53 set(CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS} ${options})
54 set(CMAKE_REQUIRED_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS} ${definitions})
55 set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_DEFINITIONS} ${libraries})
57 message(STATUS "Looking for LFS support using ${options} ${definitions} ${libraries}")
58 check_c_source_compiles("${_lfs_test_source}" ${var})
59 cmake_pop_check_state()
62 message(STATUS "Looking for LFS support using ${options} ${definitions} ${libraries} - found")
63 set(_lfs_cppflags ${_lfs_cppflags} ${definitions} PARENT_SCOPE)
64 set(_lfs_cflags ${_lfs_cflags} ${options} PARENT_SCOPE)
65 set(_lfs_ldflags ${_lfs_ldflags} ${libraries} PARENT_SCOPE)
66 set(LFS_FOUND TRUE PARENT_SCOPE)
68 message(STATUS "Looking for LFS support using ${options} ${definitions} ${libraries} - not found")
72 # Check for the availability of LFS.
73 # The cases handled are:
76 # * Output of getconf LFS_CFLAGS; getconf LFS_LIBS; getconf LFS_LDFLAGS
77 # * Preprocessor flag -D_FILE_OFFSET_BITS=64
78 # * Preprocessor flag -D_LARGE_FILES
85 cmake_push_check_state()
86 set(CMAKE_REQUIRED_QUIET 1)
87 message(STATUS "Looking for native LFS support")
88 check_c_source_compiles("${_lfs_test_source}" lfs_native)
89 cmake_pop_check_state()
91 message(STATUS "Looking for native LFS support - found")
94 message(STATUS "Looking for native LFS support - not found")
98 # Check using getconf. If getconf fails, don't worry, the check in
99 # _lfs_check_compiler_option will fail as well.
100 execute_process(COMMAND getconf LFS_CFLAGS
101 OUTPUT_VARIABLE _lfs_cflags_raw
102 OUTPUT_STRIP_TRAILING_WHITESPACE
104 execute_process(COMMAND getconf LFS_LIBS
105 OUTPUT_VARIABLE _lfs_libs_tmp
106 OUTPUT_STRIP_TRAILING_WHITESPACE
108 execute_process(COMMAND getconf LFS_LDFLAGS
109 OUTPUT_VARIABLE _lfs_ldflags_tmp
110 OUTPUT_STRIP_TRAILING_WHITESPACE
113 separate_arguments(_lfs_cflags_raw)
114 separate_arguments(_lfs_ldflags_tmp)
115 separate_arguments(_lfs_libs_tmp)
117 # Move -D flags to the place they are supposed to be
118 foreach(flag ${_lfs_cflags_raw})
119 if (flag MATCHES "-D.*")
120 list(APPEND _lfs_cppflags_tmp ${flag})
122 list(APPEND _lfs_cflags_tmp ${flag})
126 # Check if the flags we received (if any) produce working LFS support
127 _lfs_check_compiler_option(lfs_getconf_works
129 "${_lfs_cppflags_tmp}"
130 "${_lfs_libs_tmp};${_lfs_ldflags_tmp}")
133 if(NOT LFS_FOUND) # IRIX stuff
134 _lfs_check_compiler_option(lfs_need_n32 "-n32" "" "")
136 if(NOT LFS_FOUND) # Linux and friends
137 _lfs_check_compiler_option(lfs_need_file_offset_bits "" "-D_FILE_OFFSET_BITS=64" "")
139 if(NOT LFS_FOUND) # AIX
140 _lfs_check_compiler_option(lfs_need_large_files "" "-D_LARGE_FILES=1" "")
143 set(LFS_DEFINITIONS ${_lfs_cppflags} CACHE STRING "Extra definitions for large file support")
144 set(LFS_COMPILE_OPTIONS ${_lfs_cflags} CACHE STRING "Extra definitions for large file support")
145 set(LFS_LIBRARIES ${_lfs_libs} ${_lfs_ldflags} CACHE STRING "Extra definitions for large file support")
146 set(LFS_FOUND ${LFS_FOUND} CACHE INTERNAL "Found LFS")
153 find_package_handle_standard_args(LFS "Could not find LFS. Set LFS_DEFINITIONS, LFS_COMPILE_OPTIONS, LFS_LIBRARIES." LFS_FOUND)