Skip to content

Commit

Permalink
typedef ptrdiff_t when stddef.h does not provide it
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Pop authored and Dead2 committed Jan 17, 2019
1 parent cb5166a commit 966fdc3
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 6 deletions.
29 changes: 28 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 2 additions & 5 deletions Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
40 changes: 40 additions & 0 deletions configure
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions zconf-ng.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

Expand Down
4 changes: 4 additions & 0 deletions zconf.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

Expand Down

0 comments on commit 966fdc3

Please sign in to comment.