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