FindLFS.cmake 5.9 KB

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