1#!/bin/bash -ex
2#
3# Copyright 2019 Google Inc. All rights reserved.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#     http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17# Regenerate host configuration files for the current host
18
19# TODO: Ensure all required packages are installed.
20
21cd `dirname ${BASH_SOURCE[0]}`/..
22
23SRC_TOP=$(pwd)
24LOCAL_TOP=$SRC_TOP/android
25ANDROID_BUILD_TOP=$(cd ../../..; pwd)
26
27if [ $(uname) == 'Darwin' ]; then
28  DIR=darwin
29else
30  if [ $(uname -m) == 'aarch64' ]; then
31      DIR=linux_arm64
32  else
33      DIR=linux_x86_64
34  fi
35fi
36mkdir -p $LOCAL_TOP/$DIR/pyconfig
37
38export CLANG_VERSION=$(cd $ANDROID_BUILD_TOP; build/soong/scripts/get_clang_version.py)
39
40if [ $DIR == "linux_x86_64" ]; then
41  export CC="$ANDROID_BUILD_TOP/prebuilts/clang/host/linux-x86/$CLANG_VERSION/bin/clang"
42  export CFLAGS="--sysroot=$ANDROID_BUILD_TOP/prebuilts/gcc/linux-x86/host/x86_64-linux-glibc2.17-4.8/sysroot"
43  export LDFLAGS="--sysroot=$ANDROID_BUILD_TOP/prebuilts/gcc/linux-x86/host/x86_64-linux-glibc2.17-4.8/sysroot -B$ANDROID_BUILD_TOP/prebuilts/gcc/linux-x86/host/x86_64-linux-glibc2.17-4.8/lib/gcc/x86_64-linux/4.8.3 -L$ANDROID_BUILD_TOP/prebuilts/gcc/linux-x86/host/x86_64-linux-glibc2.17-4.8/lib/gcc/x86_64-linux/4.8.3 -L$ANDROID_BUILD_TOP/prebuilts/gcc/linux-x86/host/x86_64-linux-glibc2.17-4.8/x86_64-linux/lib64"
44elif [ $DIR == "linux_arm64" ]; then
45  #export CC="$ANDROID_BUILD_TOP/prebuilts/clang/host/linux-x86/$CLANG_VERSION/bin/clang"
46  export CC=clang
47  export CFLAGS="--sysroot=$ANDROID_BUILD_TOP/prebuilts/build-tools/sysroots/aarch64-linux-musl"
48  export LDFLAGS="--sysroot=$ANDROID_BUILD_TOP/prebuilts/build-tools/sysroots/aarch64-linux-musl -rtlib=compiler-rt -fuse-ld=lld --unwindlib=none"
49fi
50
51#
52# Generate pyconfig.h
53#
54mkdir -p $ANDROID_BUILD_TOP/out
55PYTHON_BUILD=$ANDROID_BUILD_TOP/out/python
56rm -rf $PYTHON_BUILD
57cp -rp $SRC_TOP $PYTHON_BUILD
58cd $PYTHON_BUILD
59./configure
60
61if [ $DIR == "darwin" ]; then
62  # preadv and pwritev are not safe on <11, which we still target
63  sed -ibak "s%#define HAVE_PREADV 1%/* #undef HAVE_PREADV */%" pyconfig.h
64  sed -ibak "s%#define HAVE_PWRITEV 1%/* #undef HAVE_PWRITEV */%" pyconfig.h
65  # mkfifoat and mknodat are not safe on <13, which we still target
66  sed -ibak "s%#define HAVE_MKNODAT 1%/* #undef HAVE_MKNODAT */%" pyconfig.h
67  sed -ibak "s%#define HAVE_MKFIFOAT 1%/* #undef HAVE_MKFIFOAT */%" pyconfig.h
68
69  if [ $(machine) != "x86_64h" ]; then
70    echo "This script expects to be run on an X86_64 machine"
71    exit 1
72  fi
73
74  # Changes to support darwin_arm64
75  sed -ibak 's%#define HAVE_FINITE 1%#ifdef __x86_64__\n#define HAVE_FINITE 1\n#endif%' pyconfig.h
76  sed -ibak 's%#define HAVE_GAMMA 1%#ifdef __x86_64__\n#define HAVE_GAMMA 1\n#endif%' pyconfig.h
77  sed -ibak 's%#define HAVE_GCC_ASM_FOR_X64 1%#ifdef __x86_64__\n#define HAVE_GCC_ASM_FOR_X64 1\n#endif%' pyconfig.h
78  sed -ibak 's%#define HAVE_GCC_ASM_FOR_X87 1%#ifdef __x86_64__\n#define HAVE_GCC_ASM_FOR_X87 1\n#endif%' pyconfig.h
79  sed -ibak 's%#define SIZEOF_LONG_DOUBLE .*%#ifdef __x86_64__\n#define SIZEOF_LONG_DOUBLE 16\n#else\n#define SIZEOF_LONG_DOUBLE 8\n#endif%' pyconfig.h
80fi
81
82if [ $DIR == "linux_x86_64" ]; then
83  mkdir -p $LOCAL_TOP/bionic/pyconfig
84  cp pyconfig.h $LOCAL_TOP/bionic/pyconfig
85  # Changes to support bionic
86  bionic_pyconfig=$LOCAL_TOP/bionic/pyconfig/pyconfig.h
87  sed -i 's%#define HAVE_CONFSTR 1%/* #undef HAVE_CONFSTR */%' $bionic_pyconfig
88  sed -i 's%#define HAVE_CRYPT_H 1%/* #undef HAVE_CRYPT_H */%' $bionic_pyconfig
89  sed -i 's%#define HAVE_CRYPT_R 1%/* #undef HAVE_CRYPT_R */%' $bionic_pyconfig
90  sed -i 's%#define HAVE_DECL_RTLD_DEEPBIND 1%/* #undef HAVE_DECL_RTLD_DEEPBIND */%' $bionic_pyconfig
91  sed -i "s%#define HAVE_GCC_ASM_FOR_X87 1%#ifdef __i386__\n#define HAVE_GCC_ASM_FOR_X87 1\n#endif%" $bionic_pyconfig
92  sed -i 's%#define HAVE_LIBINTL_H 1%/* #undef HAVE_LIBINTL_H */%' $bionic_pyconfig
93  sed -i 's%#define HAVE_STROPTS_H 1%/* #undef HAVE_STROPTS_H */%' $bionic_pyconfig
94  sed -i 's%#define HAVE_WAIT3 1%/* #undef HAVE_WAIT3 */%' $bionic_pyconfig
95
96  sed -i 's%#define SIZEOF_FPOS_T .*%#define SIZEOF_FPOS_T 8%' $bionic_pyconfig
97  sed -i 's%#define SIZEOF_LONG .*%#ifdef __LP64__\n#define SIZEOF_LONG 8\n#else\n#define SIZEOF_LONG 4\n#endif%' $bionic_pyconfig
98  sed -i 's%#define SIZEOF_LONG_DOUBLE .*%#define SIZEOF_LONG_DOUBLE (SIZEOF_LONG * 2)%' $bionic_pyconfig
99  sed -i 's%#define SIZEOF_PTHREAD_T .*%#define SIZEOF_PTHREAD_T SIZEOF_LONG%' $bionic_pyconfig
100  sed -i 's%#define SIZEOF_SIZE_T .*%#define SIZEOF_SIZE_T SIZEOF_LONG%' $bionic_pyconfig
101  sed -i 's%#define SIZEOF_TIME_T .*%#define SIZEOF_TIME_T SIZEOF_LONG%' $bionic_pyconfig
102  sed -i 's%#define SIZEOF_UINTPTR_T .*%#define SIZEOF_UINTPTR_T SIZEOF_LONG%' $bionic_pyconfig
103  sed -i 's%#define SIZEOF_VOID_P .*%#define SIZEOF_VOID_P SIZEOF_LONG%' $bionic_pyconfig
104
105  # Changes to support musl
106  sed -i "s%#define HAVE_DECL_RTLD_DEEPBIND 1%#ifdef __GLIBC__\n#define HAVE_DECL_RTLD_DEEPBIND 1\n#endif%" pyconfig.h
107fi
108
109cp pyconfig.h $LOCAL_TOP/$DIR/pyconfig/
110
111#
112# Generate frozen modules
113#
114make -j64 Python/deepfreeze/deepfreeze.c
115mkdir -p $LOCAL_TOP/Python/deepfreeze
116cp Python/deepfreeze/deepfreeze.c $LOCAL_TOP/Python/deepfreeze
117rm -rf $LOCAL_TOP/Python/frozen_modules
118cp -rp Python/frozen_modules $LOCAL_TOP/Python
119
120function generate_srcs() {
121  #
122  # Generate config.c
123  #
124  echo >Makefile.pre
125  Modules/makesetup -c Modules/config.c.in -s Modules -m Makefile.pre $LOCAL_TOP/$1/Setup.local $LOCAL_TOP/Setup.local Modules/Setup.bootstrap Modules/Setup
126  cp config.c $LOCAL_TOP/$1
127
128  #
129  # Generate module file list
130  #
131  grep '$(CC)' Makefile | sed 's/;.*//' | sed 's/.*: //' | sed 's#$(srcdir)/##' | sed 's/$(PYTHON_HEADERS)//' | sed 's/$(MODULE_.*_DEPS)//' | sed 's#Modules/config.c##' | sed 's/ \+/\n/g' | sort -u >srcs
132  (
133    echo '// Generated by android/regen.sh'
134    echo 'filegroup {'
135    echo "    name: \"py3-c-modules-$1\","
136    echo "    srcs: ["
137    for src in $(cat srcs); do
138      echo "        \"${src}\","
139    done
140    echo "    ],"
141    echo '}'
142  ) >$SRC_TOP/Android-$1.bp
143}
144
145generate_srcs $DIR
146
147if [ $DIR == "linux_x86_64" ]; then
148  generate_srcs bionic
149fi