]> git.ipfire.org Git - thirdparty/zlib-ng.git/commitdiff
typedef ptrdiff_t when stddef.h does not provide it
authorSebastian Pop <s.pop@samsung.com>
Thu, 13 Dec 2018 15:58:08 +0000 (09:58 -0600)
committerHans Kristian Rosbach <hk-github@circlestorm.org>
Thu, 17 Jan 2019 07:55:02 +0000 (08:55 +0100)
CMakeLists.txt
Makefile.in
configure
zconf-ng.h.in
zconf.h.in

index 4d691e1d05da004a9382f2e028748029b613ca5b..53dfd71c31d5758c6829d4d8d662b6c5004263ea 100644 (file)
@@ -330,6 +330,29 @@ if(HAVE_BUILTIN_CTZL)
     add_definitions(-DHAVE_BUILTIN_CTZL)
 endif()
 
+#
+# check for ptrdiff_t support
+#
+check_c_source_compiles(
+    "#include <stddef.h>
+     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)
index 6ff524c2dd0453ba7e4b6bb0380b6f5cd5d6f6c2..d4e3153a0b2a7da73cf40cba3af468d76a758772 100644 (file)
@@ -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
index 8a8143b72bdb878338b4d99d5a3893a2f6635949..393f5c6a0554f54d5c75d571425f9483dbc9bf33 100755 (executable)
--- 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 <<EOF
+#include <stddef.h>
+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 <<EOF
+#include <stdint.h>
+#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 <<EOF
+#include <stdint.h>
+#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
index 73150255248456258ade877fbbd08884e604efb2..5d0928de56fb9fc9f53af09d0f9d4b3000854002 100644 (file)
@@ -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 <sys/types.h>      /* for off_t */
 #include <stdarg.h>         /* for va_list */
 
index fd019ba828d433b1bb5620addb5f36838da1768f..79e4cc49c5fdd5977c8d615ce1a560f8249edae4 100644 (file)
@@ -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 <sys/types.h>      /* for off_t */
 #include <stdarg.h>         /* for va_list */