From: Sebastian Pop Date: Thu, 13 Dec 2018 15:58:08 +0000 (-0600) Subject: typedef ptrdiff_t when stddef.h does not provide it X-Git-Tag: 1.9.9-b1~554 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=966fdc372c8c7268dd914fb54baa99bb24492102;p=thirdparty%2Fzlib-ng.git typedef ptrdiff_t when stddef.h does not provide it --- diff --git a/CMakeLists.txt b/CMakeLists.txt index 4d691e1d0..53dfd71c3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -330,6 +330,29 @@ if(HAVE_BUILTIN_CTZL) add_definitions(-DHAVE_BUILTIN_CTZL) endif() +# +# check for ptrdiff_t support +# +check_c_source_compiles( + "#include + int main() { ptrdiff_t *a; return 0; }" + HAVE_PTRDIFF_T +) +if(NOT HAVE_PTRDIFF_T) + set(NEED_PTRDIFF_T 1) + + check_type_size("void *" SIZEOF_DATA_PTR) + message(STATUS "sizeof(void *) is ${SIZEOF_DATA_PTR} bytes") + + if(${SIZEOF_DATA_PTR} MATCHES "4") + set(PTRDIFF_TYPE "uint32_t") + elseif(${SIZEOF_DATA_PTR} MATCHES "8") + set(PTRDIFF_TYPE "uint64_t") + else() + message(FATAL_ERROR "sizeof(void *) is neither 32 nor 64 bit") + endif() +endif() + # Macro to check if source compiles when cross-compiling # or runs when compiling natively macro(check_c_source_compile_or_run source flag) @@ -561,8 +584,12 @@ macro(generate_cmakein input output) foreach(_line IN LISTS _lines) file(APPEND ${output} "${_line}\n") - if (_line STREQUAL "#define ZCONF_H") + if (_line STREQUAL "#define ZCONF_H" OR _line STREQUAL "#define ZCONFNG_H") file(APPEND ${output} "#cmakedefine Z_HAVE_UNISTD_H\n") + if(NOT HAVE_PTRDIFF_T) + file(APPEND ${output} "#cmakedefine NEED_PTRDIFF_T\n") + file(APPEND ${output} "#cmakedefine PTRDIFF_TYPE ${PTRDIFF_TYPE}\n") + endif() endif() endforeach() endmacro(generate_cmakein) diff --git a/Makefile.in b/Makefile.in index 6ff524c2d..d4e3153a0 100644 --- a/Makefile.in +++ b/Makefile.in @@ -364,11 +364,8 @@ distclean: clean # Reset zconf.h and zconf.h.cmakein if building inside source tree @if [ -f zconf.h.in ]; then \ cp -p $(SRCDIR)/zconf.h.in zconf.h ; \ - TEMPFILE=zconfh_$$ ; \ - echo "/#define ZCONF_H/ a\\\n#cmakedefine Z_HAVE_UNISTD_H\\n" >> $$TEMPFILE &&\ - sed -f $$TEMPFILE $(SRCDIR)/zconf.h.in > zconf.h.cmakein &&\ - touch -r $(SRCDIR)/zconf.h.in zconf.h.cmakein &&\ - rm $$TEMPFILE ; fi + grep -v '^#cmakedefine' $(SRCDIR)/zconf.h.in > zconf.h.cmakein &&\ + touch -r $(SRCDIR)/zconf.h.in zconf.h.cmakein ; fi # Cleanup these files if building outside source tree @if [ ! -f zlib.3 ]; then rm -f zlib.3.pdf Makefile; fi # Remove arch and test directory if building outside source tree diff --git a/configure b/configure index 8a8143b72..393f5c6a0 100755 --- a/configure +++ b/configure @@ -712,6 +712,46 @@ fi echo >> configure.log +# check for ptrdiff_t and save result in zconf.h +echo -n "Checking for ptrdiff_t... " | tee -a configure.log +cat > $test.c < +int fun(ptrdiff_t *a) { return 0; } +EOF +if try $CC -c $CFLAGS $test.c; then + echo "Yes." | tee -a configure.log +else + echo "No." | tee -a configure.log + sed < zconf${SUFFIX}.h "/^#ifdef NEED_PTRDIFF_T.* may be/s/def NEED_PTRDIFF_T\(.*\) may be/ 1\1 was/" > zconf${SUFFIX}.temp.h + mv zconf${SUFFIX}.temp.h zconf${SUFFIX}.h + + echo -n "Checking for sizeof(void *)... " | tee -a configure.log + cat > $test.c < +#define COMPILE_TIME_ASSERT(pred) struct s { int x: (pred) ? 1 : -1; }; +COMPILE_TIME_ASSERT(sizeof(int32_t) == sizeof(void *)); +EOF + if try $CC -c $CFLAGS $test.c; then + echo "sizeof(int32_t)." | tee -a configure.log + sed < zconf${SUFFIX}.h "s/^typedef PTRDIFF_TYPE ptrdiff_t;/typedef int32_t ptrdiff_t;/g" > zconf${SUFFIX}.temp.h + mv zconf${SUFFIX}.temp.h zconf${SUFFIX}.h + else + cat > $test.c < +#define COMPILE_TIME_ASSERT(pred) struct s { int x: (pred) ? 1 : -1; }; +COMPILE_TIME_ASSERT(sizeof(int64_t) == sizeof(void *)); +EOF + if try $CC -c $CFLAGS $test.c; then + echo "sizeof(int64_t)." | tee -a configure.log + sed < zconf${SUFFIX}.h "s/^typedef PTRDIFF_TYPE ptrdiff_t;/typedef int64_t ptrdiff_t;/g" > zconf${SUFFIX}.temp.h + mv zconf${SUFFIX}.temp.h zconf${SUFFIX}.h + else + echo "unknown." | tee -a configure.log + exit 1 + fi + fi +fi + # if --zlib-compat was requested if test $compat -eq 1; then gzfileops=1 diff --git a/zconf-ng.h.in b/zconf-ng.h.in index 731502552..5d0928de5 100644 --- a/zconf-ng.h.in +++ b/zconf-ng.h.in @@ -111,6 +111,10 @@ typedef void *voidp; # define Z_HAVE_UNISTD_H #endif +#ifdef NEED_PTRDIFF_T /* may be set to #if 1 by ./configure */ +typedef PTRDIFF_TYPE ptrdiff_t; +#endif + #include /* for off_t */ #include /* for va_list */ diff --git a/zconf.h.in b/zconf.h.in index fd019ba82..79e4cc49c 100644 --- a/zconf.h.in +++ b/zconf.h.in @@ -111,6 +111,10 @@ typedef void *voidp; # define Z_HAVE_UNISTD_H #endif +#ifdef NEED_PTRDIFF_T /* may be set to #if 1 by ./configure */ +typedef PTRDIFF_TYPE ptrdiff_t; +#endif + #include /* for off_t */ #include /* for va_list */