diff --git a/CMLibCommonTargetProperties.cmake b/CMLibCommonTargetProperties.cmake index eafa7a8..af9c322 100644 --- a/CMLibCommonTargetProperties.cmake +++ b/CMLibCommonTargetProperties.cmake @@ -1,5 +1,3 @@ -include(CMLibLargeFiles) - set(COMMON_CXX_FEATURES cxx_alias_templates cxx_nullptr cxx_override) @@ -10,10 +8,6 @@ function(common_target_properties Name) set(__interface 1) set(__visibility INTERFACE) endif() - set(_FILE_OFFSET_BITS ON) - set(_LARGEFILE_SOURCE ON) - set(_LARGEFILE64_SOURCE ON) - use_large_files(${Name} ON) target_compile_features(${Name} ${__visibility} ${COMMON_CXX_FEATURES}) target_include_directories(${Name} PUBLIC $ diff --git a/CMLibConfigHPPGenerate.cmake b/CMLibConfigHPPGenerate.cmake index 427e360..2ba7b31 100644 --- a/CMLibConfigHPPGenerate.cmake +++ b/CMLibConfigHPPGenerate.cmake @@ -1,4 +1,7 @@ function(cmlib_config_hpp_generate) + include(CMLibLargeFiles) + cmlib_test_large_files(HAVE_LARGEFILES) + get_property(VERSION_INT GLOBAL PROPERTY VERSION_INT) get_property(VERSION_STR GLOBAL PROPERTY VERSION_STR) get_property(ORGANIZATION_NAME GLOBAL PROPERTY ORGANIZATION_NAME) diff --git a/CMLibLargeFiles.cmake b/CMLibLargeFiles.cmake index 5ca9ddf..18a15f0 100644 --- a/CMLibLargeFiles.cmake +++ b/CMLibLargeFiles.cmake @@ -1,107 +1,162 @@ -macro(test_large_files VARIABLE USE_64_SUFFIX) - if(NOT DEFINED ${VARIABLE}) - message(STATUS "Checking if large (64-bit) file support is available...") +# +# This file is part of the GROMACS molecular simulation package. +# +# Copyright (c) 2009,2010,2012,2014,2019, by the GROMACS development team, led by +# Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl, +# and including many others, as listed in the AUTHORS file in the +# top-level source directory and at http://www.gromacs.org. +# +# GROMACS is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public License +# as published by the Free Software Foundation; either version 2.1 +# of the License, or (at your option) any later version. +# +# GROMACS is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with GROMACS; if not, see +# http://www.gnu.org/licenses, or write to the Free Software Foundation, +# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +# +# If you want to redistribute modifications to GROMACS, please +# consider that scientific software is very special. Version +# control is crucial - bugs must be traceable. We will be happy to +# consider code for inclusion in the official distribution, but +# derived work must not be called official GROMACS. Details are found +# in the README & COPYING files - if they are missing, get the +# official version at http://www.gromacs.org. +# +# To help us fund GROMACS development, we humbly ask that you cite +# the research papers on the package. Check out http://www.gromacs.org. - if(USE_64_SUFFIX) - set(SUFFIX_64 "64") - else(USE_64_SUFFIX) - set(SUFFIX_64 "") - endif(USE_64_SUFFIX) +# Forked from Gromacs - # First try without any macros defined - try_compile(LARGE_FILES_SUPPORTED "${CMAKE_BINARY_DIR}" - "${CMLIB_MODULE_DIR}/tests/LargeFiles${SUFFIX_64}.c" - OUTPUT_VARIABLE TRY_LARGE_FILES_OUTPUT) +# - Define macro to check large file support +# +# cmlib_test_large_files(VARIABLE) +# +# VARIABLE will be set to true if off_t is 64 bits, and fseeko/ftello present. +# This macro will also set defines necessary enable large file support, for instance +# _LARGE_FILES +# _LARGEFILE_SOURCE +# _FILE_OFFSET_BITS 64 +# HAVE_FSEEKO +# HAVE__FSEEKI64 +# +# However, it is YOUR job to make sure these defines are set in a cmakedefine so they +# end up in a config.h file that is included in your source if necessary! - if(VERBOSE_LARGE_FILES) - message(STATUS "Large file output (no special flags):\n${TRY_LARGE_FILES_OUTPUT}") - endif(VERBOSE_LARGE_FILES) +include(CheckTypeSize) - if(NOT LARGE_FILES_SUPPORTED) - # Try with C macro _FILE_OFFSET_BITS=64 - try_compile(LARGE_FILES_SUPPORTED "${CMAKE_BINARY_DIR}" - "${CMLIB_MODULE_DIR}/tests/LargeFiles${SUFFIX_64}.c" - COMPILE_DEFINITIONS "-D_FILE_OFFSET_BITS=64" - OUTPUT_VARIABLE TRY_LARGE_FILES_OUTPUT) +MACRO(cmlib_test_large_files VARIABLE) + IF(NOT DEFINED ${VARIABLE}) - if(VERBOSE_LARGE_FILES) - message(STATUS "Large file output (_FILE_OFFSET_BITS=64):\n${TRY_LARGE_FILES_OUTPUT}") - endif(VERBOSE_LARGE_FILES) + # On most platforms it is probably overkill to first test the flags for 64-bit off_t, + # and then separately fseeko. However, in the future we might have 128-bit filesystems + # (ZFS), so it might be dangerous to indiscriminately set e.g. _FILE_OFFSET_BITS=64. - if(LARGE_FILES_SUPPORTED) - set(_FILE_OFFSET_BITS=64 CACHE INTERNAL "C macro _FILE_OFFSET_BITS=64 is required for 64-bit file support") - endif(LARGE_FILES_SUPPORTED) - endif(NOT LARGE_FILES_SUPPORTED) + MESSAGE(STATUS "Checking for 64-bit off_t") - if(NOT LARGE_FILES_SUPPORTED) - # Try with C macro _LARGEFILE_SOURCE + # First check without any special flags + TRY_COMPILE(FILE64_OK "${CMAKE_BINARY_DIR}" + "${CMLIB_MODULE_DIR}/tests/TestFileOffsetBits.c") + if(FILE64_OK) + MESSAGE(STATUS "Checking for 64-bit off_t - present") + endif() - try_compile(LARGE_FILES_SUPPORTED "${CMAKE_BINARY_DIR}" - "${CMLIB_MODULE_DIR}/tests/LargeFiles${SUFFIX_64}.c" - COMPILE_DEFINITIONS "-D_LARGEFILE${SUFFIX_64}_SOURCE" - OUTPUT_VARIABLE TRY_LARGE_FILES_OUTPUT) + if(NOT FILE64_OK) + # Test with _FILE_OFFSET_BITS=64 + TRY_COMPILE(FILE64_OK "${CMAKE_BINARY_DIR}" + "${CMLIB_MODULE_DIR}/tests/TestFileOffsetBits.c" + COMPILE_DEFINITIONS "-D_FILE_OFFSET_BITS=64" ) + if(FILE64_OK) + MESSAGE(STATUS "Checking for 64-bit off_t - present with _FILE_OFFSET_BITS=64") + set(_FILE_OFFSET_BITS 64 CACHE INTERNAL "64-bit off_t requires _FILE_OFFSET_BITS=64") + endif() + endif() - if(VERBOSE_LARGE_FILES) - message(STATUS "Large file output (_LARGEFILE${SUFFIX_64}_SOURCE):\n${TRY_LARGE_FILES_OUTPUT}") - endif(VERBOSE_LARGE_FILES) + if(NOT FILE64_OK) + # Test with _LARGE_FILES + TRY_COMPILE(FILE64_OK "${CMAKE_BINARY_DIR}" + "${CMLIB_MODULE_DIR}/tests/TestFileOffsetBits.c" + COMPILE_DEFINITIONS "-D_LARGE_FILES" ) + if(FILE64_OK) + MESSAGE(STATUS "Checking for 64-bit off_t - present with _LARGE_FILES") + set(_LARGE_FILES 1 CACHE INTERNAL "64-bit off_t requires _LARGE_FILES") + endif() + endif() - if(LARGE_FILES_SUPPORTED) - set(_LARGEFILE${SUFFIX_64}_SOURCE=1 CACHE INTERNAL "C macro _LARGEFILE${SUFFIX_64}_SOURCE is required for 64-bit file support") - endif(LARGE_FILES_SUPPORTED) - endif(NOT LARGE_FILES_SUPPORTED) + if(NOT FILE64_OK) + # Test with _LARGEFILE_SOURCE + TRY_COMPILE(FILE64_OK "${CMAKE_BINARY_DIR}" + "${CMLIB_MODULE_DIR}/tests/TestFileOffsetBits.c" + COMPILE_DEFINITIONS "-D_LARGEFILE_SOURCE" ) + if(FILE64_OK) + MESSAGE(STATUS "Checking for 64-bit off_t - present with _LARGEFILE_SOURCE") + set(_LARGEFILE_SOURCE 1 CACHE INTERNAL "64-bit off_t requires _LARGEFILE_SOURCE") + endif() + endif() - if(NOT LARGE_FILES_SUPPORTED) - # Try with both C macro _FILE_OFFSET_BITS=64 and _LARGEFILE_SOURCE - try_compile(LARGE_FILES_SUPPORTED "${CMAKE_BINARY_DIR}" - "${CMLIB_MODULE_DIR}/tests/LargeFiles${SUFFIX_64}.c" - COMPILE_DEFINITIONS "-D_FILE_OFFSET_BITS=64" "-D_LARGEFILE${SUFFIX_64}_SOURCE" - OUTPUT_VARIABLE TRY_LARGE_FILES_OUTPUT) + if(NOT FILE64_OK) + MESSAGE(STATUS "Checking for 64-bit off_t - not present") + else() + # 64-bit off_t found. Now check that ftello/fseeko is available. - if(VERBOSE_LARGE_FILES) - message(STATUS "Large file output (_FILE_OFFSET_BITS=64 and _LARGEFILE${SUFFIX_64}_SOURCE):\n${TRY_LARGE_FILES_OUTPUT}") - endif(VERBOSE_LARGE_FILES) + # Set the flags we might have determined to be required above + configure_file("${CMLIB_MODULE_DIR}/tests/TestLargeFiles.c.in" + "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/TestLargeFiles.c") - if(LARGE_FILES_SUPPORTED) - set(_FILE_OFFSET_BITS=64 CACHE INTERNAL "C macro _FILE_OFFSET_BITS=64 is required for 64-bit file support") - set(_LARGEFILE${SUFFIX_64}_SOURCE=1 CACHE INTERNAL "C macro _LARGEFILE${SUFFIX_64}_SOURCE is required for 64-bit file support") - endif(LARGE_FILES_SUPPORTED) - endif(NOT LARGE_FILES_SUPPORTED) + MESSAGE(STATUS "Checking for fseeko/ftello") + # Test if ftello/fseeko are available + TRY_COMPILE(FSEEKO_COMPILE_OK "${CMAKE_BINARY_DIR}" + "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/TestLargeFiles.c") + if(FSEEKO_COMPILE_OK) + MESSAGE(STATUS "Checking for fseeko/ftello - present") + endif() - if(NOT LARGE_FILES_SUPPORTED) - # Maybe we are using the Windows C standard library - try_compile(LARGE_FILES_SUPPORTED "${CMAKE_BINARY_DIR}" - "${CMLIB_MODULE_DIR}/tests/LargeFilesWindows.c") - endif(NOT LARGE_FILES_SUPPORTED) + if(NOT FSEEKO_COMPILE_OK) + # glibc 2.2 neds _LARGEFILE_SOURCE for fseeko (but not 64-bit off_t...) + TRY_COMPILE(FSEEKO_COMPILE_OK "${CMAKE_BINARY_DIR}" + "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/TestLargeFiles.c" + COMPILE_DEFINITIONS "-D_LARGEFILE_SOURCE" ) + if(FSEEKO_COMPILE_OK) + MESSAGE(STATUS "Checking for fseeko/ftello - present with _LARGEFILE_SOURCE") + set(_LARGEFILE_SOURCE 1 CACHE INTERNAL "64-bit fseeko requires _LARGEFILE_SOURCE") + else() + set(FILE64_OK 0) + message(STATUS "64-bit off_t present but fseeko/ftello not found!") + endif() + endif() + endif() - if(LARGE_FILES_SUPPORTED) - message(STATUS "Checking if large (64-bit) file support is available - yes") - set(${VARIABLE} 1 CACHE INTERNAL "Is large file support available?") - else(LARGE_FILES_SUPPORTED) - message(STATUS "Checking if large (64-bit) file support is available - no") - set(${VARIABLE} 0 CACHE INTERNAL "Is large file support available?") - endif(LARGE_FILES_SUPPORTED) - endif(NOT DEFINED ${VARIABLE}) -endmacro(test_large_files VARIABLE USE_64_SUFFIX) + if(NOT FILE64_OK) + # now check for Windows stuff + TRY_COMPILE(FILE64_OK "${CMAKE_BINARY_DIR}" + "${CMLIB_MODULE_DIR}/tests/TestWindowsFSeek.c") + if(FILE64_OK) + MESSAGE(STATUS "Checking for 64-bit off_t - present with _fseeki64") + set(HAVE__FSEEKI64 1 CACHE INTERNAL "64-bit off_t requires _fseeki64") + endif() + endif() -macro(use_large_files TARGET USE_64_SUFFIX) - test_large_files(USING_LARGE_FILES ${USE_64_SUFFIX}) + if(FSEEKO_COMPILE_OK) + SET(${VARIABLE} 1 CACHE INTERNAL "Result of test for large file support" FORCE) + set(HAVE_FSEEKO 1 CACHE INTERNAL "64bit fseeko is available" FORCE) + elseif(HAVE__FSEEKI64) + SET(${VARIABLE} 1 CACHE INTERNAL "Result of test for large file support" FORCE) + SET(HAVE__FSEEKI64 1 CACHE INTERNAL "Windows 64-bit fseek" FORCE) + else() + check_type_size("long int" SIZEOF_LONG_INT) + if(SIZEOF_LONG_INT EQUAL 8) #standard fseek is OK for 64bit + SET(${VARIABLE} 1 CACHE INTERNAL "Result of test for large file support" FORCE) + else() + MESSAGE(FATAL_ERROR "Checking for 64bit file support failed.") + endif() + endif() - if(USING_LARGE_FILES) - if(DEFINED _FILE_OFFSET_BITS) - set_property(TARGET ${TARGET} - APPEND PROPERTY COMPILE_DEFINITIONS "_FILE_OFFSET_BITS=${_FILE_OFFSET_BITS}") - endif(DEFINED _FILE_OFFSET_BITS) + ENDIF() +ENDMACRO(cmlib_test_large_files VARIABLE) - if(DEFINED _LARGEFILE_SOURCE) - set_property(TARGET ${TARGET} - APPEND PROPERTY COMPILE_DEFINITIONS "_LARGEFILE_SOURCE") - endif(DEFINED _LARGEFILE_SOURCE) - - if(DEFINED _LARGEFILE64_SOURCE) - set_property(TARGET ${TARGET} - APPEND PROPERTY COMPILE_DEFINITIONS "_LARGEFILE64_SOURCE") - endif(DEFINED _LARGEFILE64_SOURCE) - else(USING_LARGE_FILES) - message(FATAL_ERROR "Large file support not available") - endif(USING_LARGE_FILES) -endmacro(use_large_files TARGET USE_64_SUFFIX) diff --git a/hpp/config.hpp.in b/hpp/config.hpp.in index 2df7009..b457498 100644 --- a/hpp/config.hpp.in +++ b/hpp/config.hpp.in @@ -12,5 +12,12 @@ #cmakedefine RELEASE @RELEASE@ #cmakedefine ANALYSIS @ANALYSIS@ +#cmakedefine _LARGEFILE_SOURCE +#cmakedefine _LARGE_FILES +#cmakedefine _FILE_OFFSET_BITS @_FILE_OFFSET_BITS@ +#cmakedefine HAVE_FSEEKO +#cmakedefine HAVE__FSEEKI64 +#cmakedefine HAVE_LARGEFILES @HAVE_LARGEFILES@ + #endif /* @CMLIB_PROJECT_NAME_CANONICAL@_CONFIG_HPP_ */ diff --git a/tests/LargeFiles.c b/tests/LargeFiles.c deleted file mode 100644 index d11ea97..0000000 --- a/tests/LargeFiles.c +++ /dev/null @@ -1,15 +0,0 @@ -#include -#include -#include - -#define LARGE_OFF_T (((off_t) 1 << 62) - 1 + ((off_t) 1 << 62)) -#define VALID_ARRAY_LENGTH 1 -#define INVALID_ARRAY_LENGTH -1 - -int main(int argc, const char **argv) -{ - int a[(LARGE_OFF_T % 2147483629 == 721 && LARGE_OFF_T % 2147483647 == 1) ? VALID_ARRAY_LENGTH : INVALID_ARRAY_LENGTH]; - off_t offset = ftello(NULL); - fseeko(NULL, offset, SEEK_SET); - return 0; -} diff --git a/tests/LargeFiles64.c b/tests/LargeFiles64.c deleted file mode 100644 index 4953392..0000000 --- a/tests/LargeFiles64.c +++ /dev/null @@ -1,16 +0,0 @@ -#include -#include -#include -#include - -#define LARGE_OFF_T (((off_t) 1 << 62) - 1 + ((off_t) 1 << 62)) -#define VALID_ARRAY_LENGTH 1 -#define INVALID_ARRAY_LENGTH -1 - -int main(int argc, const char **argv) -{ - int a[(LARGE_OFF_T % 2147483629 == 721 && LARGE_OFF_T % 2147483647 == 1) ? VALID_ARRAY_LENGTH : INVALID_ARRAY_LENGTH]; - off64_t offset = ftello64(NULL); - fseeko64(NULL, offset, SEEK_SET); - return 0; -} diff --git a/tests/LargeFilesWindows.c b/tests/LargeFilesWindows.c deleted file mode 100644 index 85fadd8..0000000 --- a/tests/LargeFilesWindows.c +++ /dev/null @@ -1,8 +0,0 @@ -#include - -int main() -{ - __int64 off = 0; - _fseeki64(NULL, off, SEEK_SET); - return 0; -} diff --git a/tests/TestFileOffsetBits.c b/tests/TestFileOffsetBits.c new file mode 100644 index 0000000..cd679c1 --- /dev/null +++ b/tests/TestFileOffsetBits.c @@ -0,0 +1,11 @@ +#include + +/* Cause a compile-time error if off_t is smaller than 64 bits */ +#define LARGE_OFF_T (((off_t) 1 << 62) - 1 + ((off_t) 1 << 62)) +int off_t_is_large[ (LARGE_OFF_T % 2147483629 == 721 && LARGE_OFF_T % 2147483647 == 1) ? 1 : -1 ]; + +int main(int argc, char **argv) +{ + return 0; +} + diff --git a/tests/TestLargeFiles.c.in b/tests/TestLargeFiles.c.in new file mode 100644 index 0000000..3c8baa0 --- /dev/null +++ b/tests/TestLargeFiles.c.in @@ -0,0 +1,24 @@ +#cmakedefine _LARGEFILE_SOURCE +#cmakedefine _LARGEFILE64_SOURCE +#cmakedefine _LARGE_FILES +#cmakedefine _FILE_OFFSET_BITS @_FILE_OFFSET_BITS@ + +#include +#include +#include + +int main(int argc, char **argv) +{ + /* Cause a compile-time error if off_t is smaller than 64 bits, + * and make sure we have ftello / fseeko. + */ +#define LARGE_OFF_T (((off_t) 1 << 62) - 1 + ((off_t) 1 << 62)) + int off_t_is_large[ (LARGE_OFF_T % 2147483629 == 721 && LARGE_OFF_T % 2147483647 == 1) ? 1 : -1 ]; + FILE *fp = fopen(argv[0],"r"); + off_t offset = ftello( fp ); + + fseeko( fp, offset, SEEK_CUR ); + fclose(fp); + return off_t_is_large[0] || argc; +} + diff --git a/tests/TestWindowsFSeek.c b/tests/TestWindowsFSeek.c new file mode 100644 index 0000000..ad9f0be --- /dev/null +++ b/tests/TestWindowsFSeek.c @@ -0,0 +1,11 @@ + +#include + +int main() +{ + __int64 off=0; + + _fseeki64(NULL, off, SEEK_SET); + + return 0; +}