Permalink
Browse files

Rewrite SIZEOF_* and HAVE_* feature detection.

pyconfig.h shouldn't hard-code values from a 64-bit machine!  Include
_build/detected-config.h instead.

It now compiles on ubuntu32, but it crashes!  There are a few more
hard-coded sizeof() definitions.
  • Loading branch information...
Andy Chu
Andy Chu committed Sep 8, 2017
1 parent 85f64d7 commit a3cd7968580b75212b60ca76cb6c49ca68917e3e
Showing with 165 additions and 50 deletions.
  1. +8 −47 Python-2.7.13/pyconfig.h
  2. +132 −3 configure
  3. +25 −0 scripts/ovm.sh
View
@@ -1,5 +1,11 @@
/* pyconfig.h. Generated from pyconfig.h.in by configure. */
/* pyconfig.h.in. Generated from configure.ac by autoheader. */
/* pyconfig.h.
*
* OVM Change: Instead of being generated by autoconf, pyconfig.h was frozen
* from a run of Python's ./configure on Alpine Linux, then deleted lines
* and modified it to work on many platforms. */
/* From our own ./configure */
#include "../_build/detected-config.h"
#ifndef Py_PYCONFIG_H
@@ -104,9 +110,6 @@
/* Define if `unsetenv` does not return an int. */
/* #undef HAVE_BROKEN_UNSETENV */
/* Define this if you have the type _Bool. */
#define HAVE_C99_BOOL 1
/* Define to 1 if you have the 'chflags' function. */
/* #undef HAVE_CHFLAGS */
@@ -452,12 +455,6 @@
/* Define to 1 if you have the `log1p' function. */
#define HAVE_LOG1P 1
/* Define this if you have the type long double. */
#define HAVE_LONG_DOUBLE 1
/* Define this if you have the type long long. */
#define HAVE_LONG_LONG 1
/* Define to 1 if you have the `lstat' function. */
#define HAVE_LSTAT 1
@@ -996,57 +993,21 @@
/* Define if i>>j for signed int i does not extend the sign bit when i < 0 */
/* #undef SIGNED_RIGHT_SHIFT_ZERO_FILLS */
/* The size of `double', as computed by sizeof. */
#define SIZEOF_DOUBLE 8
/* The size of `float', as computed by sizeof. */
#define SIZEOF_FLOAT 4
/* The size of `fpos_t', as computed by sizeof. */
#define SIZEOF_FPOS_T 16
/* The size of `int', as computed by sizeof. */
#define SIZEOF_INT 4
/* The size of `long', as computed by sizeof. */
#define SIZEOF_LONG 8
/* The size of `long double', as computed by sizeof. */
#define SIZEOF_LONG_DOUBLE 16
/* The size of `long long', as computed by sizeof. */
#define SIZEOF_LONG_LONG 8
/* The size of `off_t', as computed by sizeof. */
#define SIZEOF_OFF_T 8
/* The size of `pid_t', as computed by sizeof. */
#define SIZEOF_PID_T 4
/* The size of `pthread_t', as computed by sizeof. */
#define SIZEOF_PTHREAD_T 8
/* The size of `short', as computed by sizeof. */
#define SIZEOF_SHORT 2
/* The size of `size_t', as computed by sizeof. */
#define SIZEOF_SIZE_T 8
/* The size of `time_t', as computed by sizeof. */
#define SIZEOF_TIME_T 8
/* The size of `uintptr_t', as computed by sizeof. */
#define SIZEOF_UINTPTR_T 8
/* The size of `void *', as computed by sizeof. */
#define SIZEOF_VOID_P 8
/* The size of `wchar_t', as computed by sizeof. */
#define SIZEOF_WCHAR_T 4
/* The size of `_Bool', as computed by sizeof. */
#define SIZEOF__BOOL 1
/* Define to 1 if you have the ANSI C header files. */
#define STDC_HEADERS 1
View
135 configure
@@ -12,6 +12,9 @@
# Other settings: LTO, PGO? Consider moving prefix, LTO, PGO to build and
# install steps.
TMP=${TMP:-/tmp} # Assume that any system has $TMP set or /tmp exists
readonly TMP # POSIX sh supports 'readonly'
log() {
echo "$0: $@" 1>&2
}
@@ -85,6 +88,33 @@ cc_quiet() {
cc "$@" -o /dev/null >/dev/null 2>&1
}
cc_or_die() {
if ! cc "$@" >$TMP/cc.log 2>&1; then
log "Error running 'cc $@':"
cat $TMP/cc.log
die "Fatal compile error running feature test"
fi
}
# Check if a given program compiles
cc_statement() {
local pp_var="$1"
local prog="$2"
cat >$TMP/cc_statement.c <<EOF
int main() {
$prog
}
EOF
# Return exit code of compiler
if cc_quiet $TMP/cc_statement.c; then
echo "#define $pp_var 1"
return 0
else
return 1
fi
}
# Write a shell script to standard out with variables, or fail.
detect_readline() {
if cc_quiet build/detect-readline.c -l readline; then
@@ -106,6 +136,84 @@ detect_and_echo_vars() {
echo "PREFIX=$FLAG_prefix"
}
# c.m4 AC_LANG_INT_SAVE
cc_print_expr() {
local c_expr="$1"
cat >$TMP/print_expr.c <<EOF
#include <stdio.h>
#include <sys/types.h> /* size_t, pid_t */
int main() {
printf("%lu", $c_expr);
}
EOF
cc_or_die -o $TMP/print_expr $TMP/print_expr.c
$TMP/print_expr > $TMP/print_expr.out
}
# Shell note:
# - local is not POSIX, but most shells have it.
# C note:
# - autoconf uses ac_fn_compute_int (in sh) aka AC_COMPUTE_INT (in m4).
# - it uses different tests when cross compiling.
# - cross-compiling does binary search?
# - other one does AC_LANG_INT_SAVE
# - generates a C program that outputs to conftest.val!
# - well why not use exit code?
# - QEMU configure doesn't do any tests
# Hm, don't bother with cross compiling case for now.
# Check if the size of a type is greater than a certain integer.
check_sizeof() {
local pp_var="$1"
local c_type="$2"
local min_bytes="$3"
cc_print_expr "sizeof($c_type)"
local actual_bytes
actual_bytes=$(cat $TMP/print_expr.out)
if test "$actual_bytes" -lt "$min_bytes"; then
die "sizeof($c_type) should be at least $min_bytes; got $actual_bytes"
fi
# Echo to stdout!
echo "#define $pp_var $actual_bytes"
}
detect_c_language() {
# This is the equivalent of AC_CHECK_SIZEOF(int, 4)
check_sizeof SIZEOF_INT 'int' 4
check_sizeof SIZEOF_LONG 'long' 4
check_sizeof SIZEOF_VOID_P 'void *' 4
check_sizeof SIZEOF_SHORT 'short' 2
check_sizeof SIZEOF_FLOAT 'float' 4
check_sizeof SIZEOF_DOUBLE 'double' 8
check_sizeof SIZEOF_SIZE_T 'size_t' 4
# NOTE: This might only be relevant for large file support, which we don't
# have.
check_sizeof SIZEOF_FPOS_T 'fpos_t' 4
check_sizeof SIZEOF_PID_T 'pid_t' 4
if cc_statement HAVE_LONG_LONG 'long long x; x = (long long)0;'
then
check_sizeof SIZEOF_LONG_LONG 'long long' 8
fi
if cc_statement HAVE_LONG_DOUBLE 'long double x; x = (long double)0;'
then
check_sizeof SIZEOF_LONG_DOUBLE 'long double' 8
fi
if cc_statement HAVE_C99_BOOL '_Bool x; x = (_Bool)0;'
then
# NOTE: this is mainly used in ctypes.h, which we might not need.
check_sizeof SIZEOF__BOOL '_Bool' 1
fi
}
# Another way of working: set detected-config.mk ?
# And set the default target as oil_readline, oil_no_readline, oil_lto,
# oil_pgo, etc.?
@@ -117,9 +225,30 @@ main() {
# The shell build actions will 'source _build/detected-config.sh'. And then
# adjust flags to compiler (-D, -l, etc.)
mkdir -p _build
local out=_build/detected-config.sh
detect_and_echo_vars > $out
log "Wrote $out"
local sh_out=_build/detected-config.sh
local c_out=_build/detected-config.h
detect_and_echo_vars > $sh_out
detect_c_language > $c_out
log "Wrote $sh_out and $c_out"
}
unittest() {
cc_print_expr 'sizeof(int)'
local actual
actual=$(cat $TMP/print_expr.out)
test "$actual" = 4 || die "Expected 4, got $actual"
check_sizeof SIZEOF_INT 'int' 4 || die "FAILED"
# failing test
#check_sizeof SIZEOF_INT 'int' 8
cc_statement HAVE_INT 'int x = (int)0;' || die "FAILED"
cc_statement HAVE_FOO 'foo x = (foo)0;' && die "Expected to fail"
#detect_c_language
}
main "$@"
#unittest "$@"
View
@@ -0,0 +1,25 @@
#!/bin/bash
#
# Miscellaneous scripts for figuring out OVM.
#
# Usage:
# ./ovm.sh <function name>
set -o nounset
set -o pipefail
set -o errexit
source build/common.sh # $PY27
grep-python() {
find $PY27 -type f | xargs grep "$@"
}
# https://stackoverflow.com/questions/2224334/gcc-dump-preprocessor-defines
# 493 variables.
pp-vars() {
gcc -E -dM - < $PY27/pyconfig.h
}
"$@"

0 comments on commit a3cd796

Please sign in to comment.