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