Skip to content

Commit 365dabb

Browse files
keith-packardAshe Connor
authored andcommitted
Add automatic configuration of compiler to get large file support (commonmark#138)
Use CheckFileOffsetBits.cmake to get correct defines for LFS. This is necessary on 32-bit machines to avoid having trouble with file whose size requires more than 32-bits to represent. Signed-off-by: Keith Packard <[email protected]>
1 parent 2a9996f commit 365dabb

File tree

4 files changed

+61
-0
lines changed

4 files changed

+61
-0
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ endif()
99
project(cmark-gfm)
1010

1111
include("FindAsan.cmake")
12+
include("CheckFileOffsetBits.cmake")
1213

1314
if("${CMAKE_SOURCE_DIR}" STREQUAL "${CMAKE_BINARY_DIR}")
1415
message(FATAL_ERROR "Do not build in-source.\nPlease remove CMakeCache.txt and the CMakeFiles/ directory.\nThen: mkdir build ; cd build ; cmake .. ; make")

CheckFileOffsetBits.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include <sys/types.h>
2+
3+
#define KB ((off_t)1024)
4+
#define MB ((off_t)1024 * KB)
5+
#define GB ((off_t)1024 * MB)
6+
#define TB ((off_t)1024 * GB)
7+
int t2[(((64 * GB -1) % 671088649) == 268434537)
8+
&& (((TB - (64 * GB -1) + 255) % 1792151290) == 305159546)? 1: -1];
9+
10+
int main()
11+
{
12+
;
13+
return 0;
14+
}

CheckFileOffsetBits.cmake

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# - Check if _FILE_OFFSET_BITS macro needed for large files
2+
# CHECK_FILE_OFFSET_BITS ()
3+
#
4+
# The following variables may be set before calling this macro to
5+
# modify the way the check is run:
6+
#
7+
# CMAKE_REQUIRED_FLAGS = string of compile command line flags
8+
# CMAKE_REQUIRED_DEFINITIONS = list of macros to define (-DFOO=bar)
9+
# CMAKE_REQUIRED_INCLUDES = list of include directories
10+
# Copyright (c) 2009, Michihiro NAKAJIMA
11+
#
12+
# Redistribution and use is allowed according to the terms of the BSD license.
13+
# For details see the accompanying COPYING-CMAKE-SCRIPTS file.
14+
15+
#INCLUDE(CheckCSourceCompiles)
16+
17+
GET_FILENAME_COMPONENT(_selfdir_CheckFileOffsetBits
18+
"${CMAKE_CURRENT_LIST_FILE}" PATH)
19+
20+
MACRO (CHECK_FILE_OFFSET_BITS)
21+
IF(NOT DEFINED _FILE_OFFSET_BITS)
22+
MESSAGE(STATUS "Checking _FILE_OFFSET_BITS for large files")
23+
TRY_COMPILE(__WITHOUT_FILE_OFFSET_BITS_64
24+
${CMAKE_CURRENT_BINARY_DIR}
25+
${_selfdir_CheckFileOffsetBits}/CheckFileOffsetBits.c
26+
COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS})
27+
IF(NOT __WITHOUT_FILE_OFFSET_BITS_64)
28+
TRY_COMPILE(__WITH_FILE_OFFSET_BITS_64
29+
${CMAKE_CURRENT_BINARY_DIR}
30+
${_selfdir_CheckFileOffsetBits}/CheckFileOffsetBits.c
31+
COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS} -D_FILE_OFFSET_BITS=64)
32+
ENDIF(NOT __WITHOUT_FILE_OFFSET_BITS_64)
33+
34+
IF(NOT __WITHOUT_FILE_OFFSET_BITS_64 AND __WITH_FILE_OFFSET_BITS_64)
35+
SET(_FILE_OFFSET_BITS 64 CACHE INTERNAL "_FILE_OFFSET_BITS macro needed for large files")
36+
MESSAGE(STATUS "Checking _FILE_OFFSET_BITS for large files - needed")
37+
ELSE(NOT __WITHOUT_FILE_OFFSET_BITS_64 AND __WITH_FILE_OFFSET_BITS_64)
38+
SET(_FILE_OFFSET_BITS "" CACHE INTERNAL "_FILE_OFFSET_BITS macro needed for large files")
39+
MESSAGE(STATUS "Checking _FILE_OFFSET_BITS for large files - not needed")
40+
ENDIF(NOT __WITHOUT_FILE_OFFSET_BITS_64 AND __WITH_FILE_OFFSET_BITS_64)
41+
ENDIF(NOT DEFINED _FILE_OFFSET_BITS)
42+
43+
ENDMACRO (CHECK_FILE_OFFSET_BITS)

src/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmark-gfm_version.h.in
7474

7575
include (GenerateExportHeader)
7676

77+
include("../CheckFileOffsetBits.cmake")
78+
CHECK_FILE_OFFSET_BITS()
79+
7780
add_executable(${PROGRAM} ${PROGRAM_SOURCES})
7881
add_compiler_export_flags()
7982

0 commit comments

Comments
 (0)