TestLargeFiles.cmake 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. # - Define macro to check large file support
  2. #
  3. # OPJ_TEST_LARGE_FILES(VARIABLE)
  4. #
  5. # VARIABLE will be set to true if off_t is 64 bits, and fseeko/ftello present.
  6. # This macro will also defines the necessary variable enable large file support, for instance
  7. # _LARGE_FILES
  8. # _LARGEFILE_SOURCE
  9. # _FILE_OFFSET_BITS 64
  10. # OPJ_HAVE_FSEEKO
  11. #
  12. # However, it is YOUR job to make sure these defines are set in a #cmakedefine so they
  13. # end up in a config.h file that is included in your source if necessary!
  14. #
  15. # Adapted from Gromacs project (http://www.gromacs.org/)
  16. # by Julien Malik
  17. #
  18. macro(OPJ_TEST_LARGE_FILES VARIABLE)
  19. if(NOT DEFINED ${VARIABLE})
  20. # On most platforms it is probably overkill to first test the flags for 64-bit off_t,
  21. # and then separately fseeko. However, in the future we might have 128-bit filesystems
  22. # (ZFS), so it might be dangerous to indiscriminately set e.g. _FILE_OFFSET_BITS=64.
  23. message(STATUS "Checking for 64-bit off_t")
  24. # First check without any special flags
  25. try_compile(FILE64_OK "${PROJECT_BINARY_DIR}"
  26. "${PROJECT_SOURCE_DIR}/cmake/TestFileOffsetBits.c")
  27. if(FILE64_OK)
  28. message(STATUS "Checking for 64-bit off_t - present")
  29. endif()
  30. if(NOT FILE64_OK)
  31. # Test with _FILE_OFFSET_BITS=64
  32. try_compile(FILE64_OK "${PROJECT_BINARY_DIR}"
  33. "${PROJECT_SOURCE_DIR}/cmake/TestFileOffsetBits.c"
  34. COMPILE_DEFINITIONS "-D_FILE_OFFSET_BITS=64" )
  35. if(FILE64_OK)
  36. message(STATUS "Checking for 64-bit off_t - present with _FILE_OFFSET_BITS=64")
  37. set(_FILE_OFFSET_BITS 64)
  38. endif()
  39. endif()
  40. if(NOT FILE64_OK)
  41. # Test with _LARGE_FILES
  42. try_compile(FILE64_OK "${PROJECT_BINARY_DIR}"
  43. "${PROJECT_SOURCE_DIR}/cmake/TestFileOffsetBits.c"
  44. COMPILE_DEFINITIONS "-D_LARGE_FILES" )
  45. if(FILE64_OK)
  46. message(STATUS "Checking for 64-bit off_t - present with _LARGE_FILES")
  47. set(_LARGE_FILES 1)
  48. endif()
  49. endif()
  50. if(NOT FILE64_OK)
  51. # Test with _LARGEFILE_SOURCE
  52. try_compile(FILE64_OK "${PROJECT_BINARY_DIR}"
  53. "${PROJECT_SOURCE_DIR}/cmake/TestFileOffsetBits.c"
  54. COMPILE_DEFINITIONS "-D_LARGEFILE_SOURCE" )
  55. if(FILE64_OK)
  56. message(STATUS "Checking for 64-bit off_t - present with _LARGEFILE_SOURCE")
  57. set(_LARGEFILE_SOURCE 1)
  58. endif()
  59. endif()
  60. #if(NOT FILE64_OK)
  61. # # now check for Windows stuff
  62. # try_compile(FILE64_OK "${PROJECT_BINARY_DIR}"
  63. # "${PROJECT_SOURCE_DIR}/cmake/TestWindowsFSeek.c")
  64. # if(FILE64_OK)
  65. # message(STATUS "Checking for 64-bit off_t - present with _fseeki64")
  66. # set(HAVE__FSEEKI64 1)
  67. # endif()
  68. #endif()
  69. if(NOT FILE64_OK)
  70. message(STATUS "Checking for 64-bit off_t - not present")
  71. endif()
  72. set(_FILE_OFFSET_BITS ${_FILE_OFFSET_BITS} CACHE INTERNAL "Result of test for needed _FILE_OFFSET_BITS=64")
  73. set(_LARGE_FILES ${_LARGE_FILES} CACHE INTERNAL "Result of test for needed _LARGE_FILES")
  74. set(_LARGEFILE_SOURCE ${_LARGEFILE_SOURCE} CACHE INTERNAL "Result of test for needed _LARGEFILE_SOURCE")
  75. # Set the flags we might have determined to be required above
  76. configure_file("${PROJECT_SOURCE_DIR}/cmake/TestLargeFiles.c.cmake.in"
  77. "${PROJECT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/TestLargeFiles.c")
  78. message(STATUS "Checking for fseeko/ftello")
  79. # Test if ftello/fseeko are available
  80. try_compile(FSEEKO_COMPILE_OK
  81. "${PROJECT_BINARY_DIR}"
  82. "${PROJECT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/TestLargeFiles.c")
  83. if(FSEEKO_COMPILE_OK)
  84. message(STATUS "Checking for fseeko/ftello - present")
  85. endif()
  86. if(NOT FSEEKO_COMPILE_OK)
  87. # glibc 2.2 needs _LARGEFILE_SOURCE for fseeko (but not for 64-bit off_t...)
  88. try_compile(FSEEKO_COMPILE_OK
  89. "${PROJECT_BINARY_DIR}"
  90. "${PROJECT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/TestLargeFiles.c"
  91. COMPILE_DEFINITIONS "-D_LARGEFILE_SOURCE" )
  92. if(FSEEKO_COMPILE_OK)
  93. message(STATUS "Checking for fseeko/ftello - present with _LARGEFILE_SOURCE")
  94. set(_LARGEFILE_SOURCE ${_LARGEFILE_SOURCE} CACHE INTERNAL "Result of test for needed _LARGEFILE_SOURCE")
  95. endif()
  96. endif()
  97. if(FSEEKO_COMPILE_OK)
  98. set(OPJ_HAVE_FSEEKO ON CACHE INTERNAL "Result of test for fseeko/ftello")
  99. else()
  100. message(STATUS "Checking for fseeko/ftello - not found")
  101. set(OPJ_HAVE_FSEEKO OFF CACHE INTERNAL "Result of test for fseeko/ftello")
  102. endif()
  103. if(FILE64_OK AND FSEEKO_COMPILE_OK)
  104. message(STATUS "Large File support - found")
  105. set(${VARIABLE} ON CACHE INTERNAL "Result of test for large file support")
  106. else()
  107. message(STATUS "Large File support - not found")
  108. set(${VARIABLE} OFF CACHE INTERNAL "Result of test for large file support")
  109. endif()
  110. endif()
  111. endmacro()