Переписан поиск флагов для поддержки больших файлов

This commit is contained in:
Andrei Astafev 2019-02-15 07:43:50 +03:00
parent 8b1bfefdb3
commit c247c359f9
10 changed files with 200 additions and 134 deletions

View File

@ -1,5 +1,3 @@
include(CMLibLargeFiles)
set(COMMON_CXX_FEATURES set(COMMON_CXX_FEATURES
cxx_alias_templates cxx_nullptr cxx_override) cxx_alias_templates cxx_nullptr cxx_override)
@ -10,10 +8,6 @@ function(common_target_properties Name)
set(__interface 1) set(__interface 1)
set(__visibility INTERFACE) set(__visibility INTERFACE)
endif() 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_compile_features(${Name} ${__visibility} ${COMMON_CXX_FEATURES})
target_include_directories(${Name} PUBLIC target_include_directories(${Name} PUBLIC
$<INSTALL_INTERFACE:include> $<INSTALL_INTERFACE:include>

View File

@ -1,4 +1,7 @@
function(cmlib_config_hpp_generate) function(cmlib_config_hpp_generate)
include(CMLibLargeFiles)
cmlib_test_large_files(HAVE_LARGEFILES)
get_property(VERSION_INT GLOBAL PROPERTY VERSION_INT) get_property(VERSION_INT GLOBAL PROPERTY VERSION_INT)
get_property(VERSION_STR GLOBAL PROPERTY VERSION_STR) get_property(VERSION_STR GLOBAL PROPERTY VERSION_STR)
get_property(ORGANIZATION_NAME GLOBAL PROPERTY ORGANIZATION_NAME) get_property(ORGANIZATION_NAME GLOBAL PROPERTY ORGANIZATION_NAME)

View File

@ -1,107 +1,162 @@
macro(test_large_files VARIABLE USE_64_SUFFIX) #
if(NOT DEFINED ${VARIABLE}) # This file is part of the GROMACS molecular simulation package.
message(STATUS "Checking if large (64-bit) file support is available...") #
# 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) # Forked from Gromacs
set(SUFFIX_64 "64")
else(USE_64_SUFFIX)
set(SUFFIX_64 "")
endif(USE_64_SUFFIX)
# First try without any macros defined # - Define macro to check large file support
try_compile(LARGE_FILES_SUPPORTED "${CMAKE_BINARY_DIR}" #
"${CMLIB_MODULE_DIR}/tests/LargeFiles${SUFFIX_64}.c" # cmlib_test_large_files(VARIABLE)
OUTPUT_VARIABLE TRY_LARGE_FILES_OUTPUT) #
# 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) include(CheckTypeSize)
message(STATUS "Large file output (no special flags):\n${TRY_LARGE_FILES_OUTPUT}")
endif(VERBOSE_LARGE_FILES)
if(NOT LARGE_FILES_SUPPORTED) MACRO(cmlib_test_large_files VARIABLE)
# Try with C macro _FILE_OFFSET_BITS=64 IF(NOT DEFINED ${VARIABLE})
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)
if(VERBOSE_LARGE_FILES) # On most platforms it is probably overkill to first test the flags for 64-bit off_t,
message(STATUS "Large file output (_FILE_OFFSET_BITS=64):\n${TRY_LARGE_FILES_OUTPUT}") # and then separately fseeko. However, in the future we might have 128-bit filesystems
endif(VERBOSE_LARGE_FILES) # (ZFS), so it might be dangerous to indiscriminately set e.g. _FILE_OFFSET_BITS=64.
if(LARGE_FILES_SUPPORTED) MESSAGE(STATUS "Checking for 64-bit off_t")
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)
if(NOT LARGE_FILES_SUPPORTED) # First check without any special flags
# Try with C macro _LARGEFILE_SOURCE 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}" if(NOT FILE64_OK)
"${CMLIB_MODULE_DIR}/tests/LargeFiles${SUFFIX_64}.c" # Test with _FILE_OFFSET_BITS=64
COMPILE_DEFINITIONS "-D_LARGEFILE${SUFFIX_64}_SOURCE" TRY_COMPILE(FILE64_OK "${CMAKE_BINARY_DIR}"
OUTPUT_VARIABLE TRY_LARGE_FILES_OUTPUT) "${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) if(NOT FILE64_OK)
message(STATUS "Large file output (_LARGEFILE${SUFFIX_64}_SOURCE):\n${TRY_LARGE_FILES_OUTPUT}") # Test with _LARGE_FILES
endif(VERBOSE_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) if(NOT FILE64_OK)
set(_LARGEFILE${SUFFIX_64}_SOURCE=1 CACHE INTERNAL "C macro _LARGEFILE${SUFFIX_64}_SOURCE is required for 64-bit file support") # Test with _LARGEFILE_SOURCE
endif(LARGE_FILES_SUPPORTED) TRY_COMPILE(FILE64_OK "${CMAKE_BINARY_DIR}"
endif(NOT LARGE_FILES_SUPPORTED) "${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) if(NOT FILE64_OK)
# Try with both C macro _FILE_OFFSET_BITS=64 and _LARGEFILE_SOURCE MESSAGE(STATUS "Checking for 64-bit off_t - not present")
try_compile(LARGE_FILES_SUPPORTED "${CMAKE_BINARY_DIR}" else()
"${CMLIB_MODULE_DIR}/tests/LargeFiles${SUFFIX_64}.c" # 64-bit off_t found. Now check that ftello/fseeko is available.
COMPILE_DEFINITIONS "-D_FILE_OFFSET_BITS=64" "-D_LARGEFILE${SUFFIX_64}_SOURCE"
OUTPUT_VARIABLE TRY_LARGE_FILES_OUTPUT)
if(VERBOSE_LARGE_FILES) # Set the flags we might have determined to be required above
message(STATUS "Large file output (_FILE_OFFSET_BITS=64 and _LARGEFILE${SUFFIX_64}_SOURCE):\n${TRY_LARGE_FILES_OUTPUT}") configure_file("${CMLIB_MODULE_DIR}/tests/TestLargeFiles.c.in"
endif(VERBOSE_LARGE_FILES) "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/TestLargeFiles.c")
if(LARGE_FILES_SUPPORTED) MESSAGE(STATUS "Checking for fseeko/ftello")
set(_FILE_OFFSET_BITS=64 CACHE INTERNAL "C macro _FILE_OFFSET_BITS=64 is required for 64-bit file support") # Test if ftello/fseeko are available
set(_LARGEFILE${SUFFIX_64}_SOURCE=1 CACHE INTERNAL "C macro _LARGEFILE${SUFFIX_64}_SOURCE is required for 64-bit file support") TRY_COMPILE(FSEEKO_COMPILE_OK "${CMAKE_BINARY_DIR}"
endif(LARGE_FILES_SUPPORTED) "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/TestLargeFiles.c")
endif(NOT LARGE_FILES_SUPPORTED) if(FSEEKO_COMPILE_OK)
MESSAGE(STATUS "Checking for fseeko/ftello - present")
endif()
if(NOT LARGE_FILES_SUPPORTED) if(NOT FSEEKO_COMPILE_OK)
# Maybe we are using the Windows C standard library # glibc 2.2 neds _LARGEFILE_SOURCE for fseeko (but not 64-bit off_t...)
try_compile(LARGE_FILES_SUPPORTED "${CMAKE_BINARY_DIR}" TRY_COMPILE(FSEEKO_COMPILE_OK "${CMAKE_BINARY_DIR}"
"${CMLIB_MODULE_DIR}/tests/LargeFilesWindows.c") "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/TestLargeFiles.c"
endif(NOT LARGE_FILES_SUPPORTED) 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) if(NOT FILE64_OK)
message(STATUS "Checking if large (64-bit) file support is available - yes") # now check for Windows stuff
set(${VARIABLE} 1 CACHE INTERNAL "Is large file support available?") TRY_COMPILE(FILE64_OK "${CMAKE_BINARY_DIR}"
else(LARGE_FILES_SUPPORTED) "${CMLIB_MODULE_DIR}/tests/TestWindowsFSeek.c")
message(STATUS "Checking if large (64-bit) file support is available - no") if(FILE64_OK)
set(${VARIABLE} 0 CACHE INTERNAL "Is large file support available?") MESSAGE(STATUS "Checking for 64-bit off_t - present with _fseeki64")
endif(LARGE_FILES_SUPPORTED) set(HAVE__FSEEKI64 1 CACHE INTERNAL "64-bit off_t requires _fseeki64")
endif(NOT DEFINED ${VARIABLE}) endif()
endmacro(test_large_files VARIABLE USE_64_SUFFIX) endif()
macro(use_large_files TARGET USE_64_SUFFIX) if(FSEEKO_COMPILE_OK)
test_large_files(USING_LARGE_FILES ${USE_64_SUFFIX}) 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) ENDIF()
if(DEFINED _FILE_OFFSET_BITS) ENDMACRO(cmlib_test_large_files VARIABLE)
set_property(TARGET ${TARGET}
APPEND PROPERTY COMPILE_DEFINITIONS "_FILE_OFFSET_BITS=${_FILE_OFFSET_BITS}")
endif(DEFINED _FILE_OFFSET_BITS)
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)

View File

@ -12,5 +12,12 @@
#cmakedefine RELEASE @RELEASE@ #cmakedefine RELEASE @RELEASE@
#cmakedefine ANALYSIS @ANALYSIS@ #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_ */ #endif /* @CMLIB_PROJECT_NAME_CANONICAL@_CONFIG_HPP_ */

View File

@ -1,15 +0,0 @@
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#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;
}

View File

@ -1,16 +0,0 @@
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#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;
}

View File

@ -1,8 +0,0 @@
#include <stdio.h>
int main()
{
__int64 off = 0;
_fseeki64(NULL, off, SEEK_SET);
return 0;
}

View File

@ -0,0 +1,11 @@
#include <sys/types.h>
/* 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;
}

24
tests/TestLargeFiles.c.in Normal file
View File

@ -0,0 +1,24 @@
#cmakedefine _LARGEFILE_SOURCE
#cmakedefine _LARGEFILE64_SOURCE
#cmakedefine _LARGE_FILES
#cmakedefine _FILE_OFFSET_BITS @_FILE_OFFSET_BITS@
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
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;
}

11
tests/TestWindowsFSeek.c Normal file
View File

@ -0,0 +1,11 @@
#include <stdio.h>
int main()
{
__int64 off=0;
_fseeki64(NULL, off, SEEK_SET);
return 0;
}