| 
 | 1 | +# CMake support for large files  | 
 | 2 | +#  | 
 | 3 | +# Copyright (C) 2016 Julian Andres Klode <[email protected]>.  | 
 | 4 | +#  | 
 | 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:  | 
 | 12 | +#  | 
 | 13 | +# The above copyright notice and this permission notice shall be  | 
 | 14 | +# included in all copies or substantial portions of the Software.  | 
 | 15 | +#  | 
 | 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  | 
 | 23 | +# SOFTWARE.  | 
 | 24 | +#  | 
 | 25 | +# This defines the following variables  | 
 | 26 | +#  | 
 | 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  | 
 | 31 | +#  | 
 | 32 | + | 
 | 33 | +include(CheckCSourceCompiles)  | 
 | 34 | +include(FindPackageHandleStandardArgs)  | 
 | 35 | +include(CMakePushCheckState)  | 
 | 36 | + | 
 | 37 | +# Test program to check for LFS. Requires that off_t has at least 8 byte large  | 
 | 38 | +set(_lfs_test_source  | 
 | 39 | +    "  | 
 | 40 | +    #include <sys/types.h>  | 
 | 41 | +    typedef char my_static_assert[sizeof(off_t) >= 8 ? 1 : -1];  | 
 | 42 | +    int main(void) { return 0; }  | 
 | 43 | +    "  | 
 | 44 | +)  | 
 | 45 | + | 
 | 46 | +# Check if the given options are needed  | 
 | 47 | +#  | 
 | 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})  | 
 | 56 | + | 
 | 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()  | 
 | 60 | + | 
 | 61 | +    if(${var})  | 
 | 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)  | 
 | 67 | +    else()  | 
 | 68 | +        message(STATUS "Looking for LFS support using ${options} ${definitions} ${libraries} - not found")  | 
 | 69 | +    endif()  | 
 | 70 | +endfunction()  | 
 | 71 | + | 
 | 72 | +# Check for the availability of LFS.  | 
 | 73 | +# The cases handled are:  | 
 | 74 | +#  | 
 | 75 | +#  * Native LFS  | 
 | 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  | 
 | 79 | +#  | 
 | 80 | +function(_lfs_check)  | 
 | 81 | +    set(_lfs_cflags)  | 
 | 82 | +    set(_lfs_cppflags)  | 
 | 83 | +    set(_lfs_ldflags)  | 
 | 84 | +    set(_lfs_libs)  | 
 | 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()  | 
 | 90 | +    if (lfs_native)  | 
 | 91 | +        message(STATUS "Looking for native LFS support - found")  | 
 | 92 | +        set(LFS_FOUND TRUE)  | 
 | 93 | +    else()  | 
 | 94 | +        message(STATUS "Looking for native LFS support - not found")  | 
 | 95 | +    endif()  | 
 | 96 | + | 
 | 97 | +    if (NOT LFS_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  | 
 | 103 | +                        ERROR_QUIET)  | 
 | 104 | +        execute_process(COMMAND getconf LFS_LIBS  | 
 | 105 | +                        OUTPUT_VARIABLE _lfs_libs_tmp  | 
 | 106 | +                        OUTPUT_STRIP_TRAILING_WHITESPACE  | 
 | 107 | +                        ERROR_QUIET)  | 
 | 108 | +        execute_process(COMMAND getconf LFS_LDFLAGS  | 
 | 109 | +                        OUTPUT_VARIABLE _lfs_ldflags_tmp  | 
 | 110 | +                        OUTPUT_STRIP_TRAILING_WHITESPACE  | 
 | 111 | +                        ERROR_QUIET)  | 
 | 112 | + | 
 | 113 | +        separate_arguments(_lfs_cflags_raw)  | 
 | 114 | +        separate_arguments(_lfs_ldflags_tmp)  | 
 | 115 | +        separate_arguments(_lfs_libs_tmp)  | 
 | 116 | + | 
 | 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})  | 
 | 121 | +            else()  | 
 | 122 | +                list(APPEND _lfs_cflags_tmp ${flag})  | 
 | 123 | +            endif()  | 
 | 124 | +        endforeach()  | 
 | 125 | + | 
 | 126 | +        # Check if the flags we received (if any) produce working LFS support  | 
 | 127 | +        _lfs_check_compiler_option(lfs_getconf_works  | 
 | 128 | +                                   "${_lfs_cflags_tmp}"  | 
 | 129 | +                                   "${_lfs_cppflags_tmp}"  | 
 | 130 | +                                   "${_lfs_libs_tmp};${_lfs_ldflags_tmp}")  | 
 | 131 | +    endif()  | 
 | 132 | + | 
 | 133 | +    if(NOT LFS_FOUND)  # IRIX stuff  | 
 | 134 | +        _lfs_check_compiler_option(lfs_need_n32 "-n32" "" "")  | 
 | 135 | +    endif()  | 
 | 136 | +    if(NOT LFS_FOUND)  # Linux and friends  | 
 | 137 | +        _lfs_check_compiler_option(lfs_need_file_offset_bits "" "-D_FILE_OFFSET_BITS=64" "")  | 
 | 138 | +    endif()  | 
 | 139 | +    if(NOT LFS_FOUND)  # AIX  | 
 | 140 | +        _lfs_check_compiler_option(lfs_need_large_files "" "-D_LARGE_FILES=1" "")  | 
 | 141 | +    endif()  | 
 | 142 | + | 
 | 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")  | 
 | 147 | +endfunction()  | 
 | 148 | + | 
 | 149 | +if (NOT LFS_FOUND)  | 
 | 150 | +    _lfs_check()  | 
 | 151 | +endif()  | 
 | 152 | + | 
 | 153 | +find_package_handle_standard_args(LFS "Could not find LFS. Set LFS_DEFINITIONS, LFS_COMPILE_OPTIONS, LFS_LIBRARIES." LFS_FOUND)  | 
0 commit comments