FindLFS.cmake 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. # CMake support for large files
  2. #
  3. # Copyright (C) 2016 Julian Andres Klode <jak@debian.org>.
  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. include(CheckCSourceCompiles)
  33. include(FindPackageHandleStandardArgs)
  34. include(CMakePushCheckState)
  35. # Test program to check for LFS. Requires that off_t has at least 8 byte large
  36. set(_lfs_test_source
  37. "
  38. #include <sys/types.h>
  39. typedef char my_static_assert[sizeof(off_t) >= 8 ? 1 : -1];
  40. int main(void) { return 0; }
  41. "
  42. )
  43. # Check if the given options are needed
  44. #
  45. # This appends to the variables _lfs_cppflags, _lfs_cflags, and _lfs_ldflags,
  46. # it also sets LFS_FOUND to 1 if it works.
  47. function(_lfs_check_compiler_option var options definitions libraries)
  48. cmake_push_check_state()
  49. set(CMAKE_REQUIRED_QUIET 1)
  50. set(CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS} ${options})
  51. set(CMAKE_REQUIRED_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS} ${definitions})
  52. set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_DEFINITIONS} ${libraries})
  53. message(STATUS "Looking for LFS support using ${options} ${definitions} ${libraries}")
  54. check_c_source_compiles("${_lfs_test_source}" ${var})
  55. cmake_pop_check_state()
  56. if(${var})
  57. message(STATUS "Looking for LFS support using ${options} ${definitions} ${libraries} - found")
  58. set(_lfs_cppflags ${_lfs_cppflags} ${definitions} PARENT_SCOPE)
  59. set(_lfs_cflags ${_lfs_cflags} ${options} PARENT_SCOPE)
  60. set(_lfs_ldflags ${_lfs_ldflags} ${libraries} PARENT_SCOPE)
  61. set(LFS_FOUND TRUE PARENT_SCOPE)
  62. else()
  63. message(STATUS "Looking for LFS support using ${options} ${definitions} ${libraries} - not found")
  64. endif()
  65. endfunction()
  66. # Check for the availability of LFS.
  67. # The cases handled are:
  68. #
  69. # * Native LFS
  70. # * Output of getconf LFS_CFLAGS; getconf LFS_LIBS; getconf LFS_LDFLAGS
  71. # * Preprocessor flag -D_FILE_OFFSET_BITS=64
  72. # * Preprocessor flag -D_LARGE_FILES
  73. #
  74. function(_lfs_check)
  75. set(_lfs_cflags)
  76. set(_lfs_cppflags)
  77. set(_lfs_ldflags)
  78. set(_lfs_libs)
  79. cmake_push_check_state()
  80. set(CMAKE_REQUIRED_QUIET 1)
  81. message(STATUS "Looking for native LFS support")
  82. check_c_source_compiles("${_lfs_test_source}" lfs_native)
  83. cmake_pop_check_state()
  84. if (lfs_native)
  85. message(STATUS "Looking for native LFS support - found")
  86. set(LFS_FOUND TRUE)
  87. else()
  88. message(STATUS "Looking for native LFS support - not found")
  89. endif()
  90. if (NOT LFS_FOUND)
  91. # Check using getconf. If getconf fails, don't worry, the check in
  92. # _lfs_check_compiler_option will fail as well.
  93. execute_process(COMMAND getconf LFS_CFLAGS
  94. OUTPUT_VARIABLE _lfs_cflags_raw
  95. OUTPUT_STRIP_TRAILING_WHITESPACE
  96. ERROR_QUIET)
  97. execute_process(COMMAND getconf LFS_LIBS
  98. OUTPUT_VARIABLE _lfs_libs_tmp
  99. OUTPUT_STRIP_TRAILING_WHITESPACE
  100. ERROR_QUIET)
  101. execute_process(COMMAND getconf LFS_LDFLAGS
  102. OUTPUT_VARIABLE _lfs_ldflags_tmp
  103. OUTPUT_STRIP_TRAILING_WHITESPACE
  104. ERROR_QUIET)
  105. separate_arguments(_lfs_cflags_raw)
  106. separate_arguments(_lfs_ldflags_tmp)
  107. separate_arguments(_lfs_libs_tmp)
  108. # Move -D flags to the place they are supposed to be
  109. foreach(flag ${_lfs_cflags_raw})
  110. if (flag MATCHES "-D.*")
  111. list(APPEND _lfs_cppflags_tmp ${flag})
  112. else()
  113. list(APPEND _lfs_cflags_tmp ${flag})
  114. endif()
  115. endforeach()
  116. # Check if the flags we received (if any) produce working LFS support
  117. _lfs_check_compiler_option(lfs_getconf_works
  118. "${_lfs_cflags_tmp}"
  119. "${_lfs_cppflags_tmp}"
  120. "${_lfs_libs_tmp};${_lfs_ldflags_tmp}")
  121. endif()
  122. if(NOT LFS_FOUND) # IRIX stuff
  123. _lfs_check_compiler_option(lfs_need_n32 "-n32" "" "")
  124. endif()
  125. if(NOT LFS_FOUND) # Linux and friends
  126. _lfs_check_compiler_option(lfs_need_file_offset_bits "" "-D_FILE_OFFSET_BITS=64" "")
  127. endif()
  128. if(NOT LFS_FOUND) # AIX
  129. _lfs_check_compiler_option(lfs_need_large_files "" "-D_LARGE_FILES=1" "")
  130. endif()
  131. set(LFS_DEFINITIONS ${_lfs_cppflags} CACHE STRING "Extra definitions for large file support")
  132. set(LFS_COMPILE_OPTIONS ${_lfs_cflags} CACHE STRING "Extra definitions for large file support")
  133. set(LFS_LIBRARIES ${_lfs_libs} ${_lfs_ldflags} CACHE STRING "Extra definitions for large file support")
  134. set(LFS_FOUND ${LFS_FOUND} CACHE INTERNAL "Found LFS")
  135. endfunction()
  136. if (NOT LFS_FOUND)
  137. _lfs_check()
  138. endif()
  139. find_package_handle_standard_args(LFS "Could not find LFS. Set LFS_DEFINITIONS, LFS_COMPILE_OPTIONS, LFS_LIBRARIES." LFS_FOUND)