summaryrefslogtreecommitdiffstats
path: root/CMake/FindLFS.cmake
blob: e1fcf964c6f04e434c35f029fe5515116ad990d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# CMake support for large files
#
# Copyright (C) 2016 Julian Andres Klode <jak@debian.org>.
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation files
# (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge,
# publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
# This defines the following variables
#
# LFS_DEFINITIONS - List of definitions to pass to add_definitions()
# LFS_COMPILE_OPTIONS - List of definitions to pass to add_compile_options()
# LFS_LIBRARIES - List of libraries and linker flags
# LFS_FOUND - If there is Large files support
#

include(CheckCXXSourceCompiles)
include(FindPackageHandleStandardArgs)

# Test program to check for LFS. Requires that off_t has at least 8 byte large
set(_lfs_test_source
    "
    #include <sys/types.h>
    typedef char my_static_assert[sizeof(off_t) >= 8 ? 1 : -1];
    int main(void) { return 0; }
    "
)

# Check if the given options are needed
#
# This appends to the variables _lfs_cppflags, _lfs_cflags, and _lfs_ldflags,
# it also sets LFS_FOUND to 1 if it works.
function(_lfs_check_compiler_option var options definitions libraries)
    set(CMAKE_REQUIRED_QUIET 1)
    set(CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS} ${options})
    set(CMAKE_REQUIRED_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS} ${definitions})
    set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_DEFINITIONS} ${libraries})

    message(STATUS "Looking for LFS support using ${options} ${definitions} ${libraries}")
    check_cxx_source_compiles("${_lfs_test_source}" ${var})

    if(${var})
        message(STATUS "Looking for LFS support using ${options} ${definitions} ${libraries} - found")
        set(_lfs_cppflags ${_lfs_cppflags} ${definitions} PARENT_SCOPE)
        set(_lfs_cflags ${_lfs_cflags} ${options} PARENT_SCOPE)
        set(_lfs_ldflags ${_lfs_ldflags} ${libraries} PARENT_SCOPE)
        set(LFS_FOUND TRUE PARENT_SCOPE)
    else()
        message(STATUS "Looking for LFS support using ${options} ${definitions} ${libraries} - not found")
    endif()
endfunction()

# Check for the availability of LFS.
# The cases handled are:
#
#  * Native LFS
#  * Output of getconf LFS_CFLAGS; getconf LFS_LIBS; getconf LFS_LDFLAGS
#  * Preprocessor flag -D_FILE_OFFSET_BITS=64
#  * Preprocessor flag -D_LARGE_FILES
#
function(_lfs_check)
    set(_lfs_cflags)
    set(_lfs_cppflags)
    set(_lfs_ldflags)
    set(_lfs_libs)
    set(CMAKE_REQUIRED_QUIET 1)
    message(STATUS "Looking for native LFS support")
    check_cxx_source_compiles("${_lfs_test_source}" lfs_native)
    if (lfs_native)
        message(STATUS "Looking for native LFS support - found")
        set(LFS_FOUND TRUE)
    else()
        message(STATUS "Looking for native LFS support - not found")
    endif()

    if (NOT LFS_FOUND)
        # Check using getconf. If getconf fails, don't worry, the check in
        # _lfs_check_compiler_option will fail as well.
        execute_process(COMMAND getconf LFS_CFLAGS
                        OUTPUT_VARIABLE _lfs_cflags_raw
                        OUTPUT_STRIP_TRAILING_WHITESPACE
                        ERROR_QUIET)
        execute_process(COMMAND getconf LFS_LIBS
                        OUTPUT_VARIABLE _lfs_libs_tmp
                        OUTPUT_STRIP_TRAILING_WHITESPACE
                        ERROR_QUIET)
        execute_process(COMMAND getconf LFS_LDFLAGS
                        OUTPUT_VARIABLE _lfs_ldflags_tmp
                        OUTPUT_STRIP_TRAILING_WHITESPACE
                        ERROR_QUIET)

        separate_arguments(_lfs_cflags_raw)
        separate_arguments(_lfs_ldflags_tmp)
        separate_arguments(_lfs_libs_tmp)

        # Move -D flags to the place they are supposed to be
        foreach(flag ${_lfs_cflags_raw})
            if (flag MATCHES "-D.*")
                list(APPEND _lfs_cppflags_tmp ${flag})
            else()
                list(APPEND _lfs_cflags_tmp ${flag})
            endif()
        endforeach()

        # Check if the flags we received (if any) produce working LFS support
        _lfs_check_compiler_option(lfs_getconf_works
                                   "${_lfs_cflags_tmp}"
                                   "${_lfs_cppflags_tmp}"
                                   "${_lfs_libs_tmp};${_lfs_ldflags_tmp}")
    endif()

    if(NOT LFS_FOUND)  # IRIX stuff
        _lfs_check_compiler_option(lfs_need_n32 "-n32" "" "")
    endif()
    if(NOT LFS_FOUND)  # Linux and friends
        _lfs_check_compiler_option(lfs_need_file_offset_bits "" "-D_FILE_OFFSET_BITS=64" "")
    endif()
    if(NOT LFS_FOUND)  # AIX
        _lfs_check_compiler_option(lfs_need_large_files "" "-D_LARGE_FILES=1" "")
    endif()

    set(LFS_DEFINITIONS ${_lfs_cppflags} CACHE STRING "Extra definitions for large file support")
    set(LFS_COMPILE_OPTIONS ${_lfs_cflags} CACHE STRING "Extra definitions for large file support")
    set(LFS_LIBRARIES ${_lfs_libs} ${_lfs_ldflags} CACHE STRING "Extra definitions for large file support")
    set(LFS_FOUND ${LFS_FOUND} CACHE INTERNAL "Found LFS")
endfunction()

if (NOT LFS_FOUND)
    _lfs_check()
endif()

find_package_handle_standard_args(LFS "Could not find LFS. Set LFS_DEFINITIONS, LFS_COMPILE_OPTIONS, LFS_LIBRARIES." LFS_FOUND)