xref: /aosp_15_r20/external/protobuf/kokoro/release/python/linux/config.sh (revision 1b3f573f81763fcece89efc2b6a5209149e44ab8)
1# Define custom utilities
2# Test for OSX with [ -n "$IS_OSX" ]
3
4function pre_build {
5    # Any stuff that you need to do before you start building the wheels
6    # Runs in the root directory of this repository.
7    pushd protobuf
8
9    if [ "$PLAT" == "aarch64" ]
10    then
11      local configure_host_flag="--host=aarch64"
12    fi
13
14    # Build protoc and libprotobuf
15    ./autogen.sh
16    CXXFLAGS="-fPIC -g -O2" ./configure $configure_host_flag
17    make -j8
18
19    if [ "$PLAT" == "aarch64" ]
20    then
21      # we are crosscompiling for aarch64 while running on x64
22      # the simplest way for build_py command to be able to generate
23      # the protos is by running the protoc process under
24      # an emulator. That way we don't have to build a x64 version
25      # of protoc. The qemu-arm emulator is already included
26      # in the dockcross docker image.
27      # Running protoc under an emulator is fast as protoc doesn't
28      # really do much.
29
30      # create a simple shell wrapper that runs crosscompiled protoc under qemu
31      echo '#!/bin/bash' >protoc_qemu_wrapper.sh
32      echo 'exec qemu-aarch64 "../src/protoc" "$@"' >>protoc_qemu_wrapper.sh
33      chmod ugo+x protoc_qemu_wrapper.sh
34
35      # PROTOC variable is by build_py step that runs under ./python directory
36      export PROTOC=../protoc_qemu_wrapper.sh
37    fi
38
39    # Generate python dependencies.
40    pushd python
41    python setup.py build_py
42    popd
43
44    popd
45}
46
47function bdist_wheel_cmd {
48    # Builds wheel with bdist_wheel, puts into wheelhouse
49    #
50    # It may sometimes be useful to use bdist_wheel for the wheel building
51    # process.  For example, versioneer has problems with versions which are
52    # fixed with bdist_wheel:
53    # https://github.com/warner/python-versioneer/issues/121
54    local abs_wheelhouse=$1
55
56    # Modify build version
57    pwd
58    ls
59
60    if [ "$PLAT" == "aarch64" ]
61    then
62      # when crosscompiling for aarch64, --plat-name needs to be set explicitly
63      # to end up with correctly named wheel file
64      # the value should be manylinuxABC_ARCH and dockcross docker image
65      # conveniently provides the value in the AUDITWHEEL_PLAT env
66      local plat_name_flag="--plat-name=$AUDITWHEEL_PLAT"
67
68      # override the value of EXT_SUFFIX to make sure the crosscompiled .so files in the wheel have the correct filename suffix
69      export PROTOCOL_BUFFERS_OVERRIDE_EXT_SUFFIX="$(python -c 'import sysconfig; print(sysconfig.get_config_var("EXT_SUFFIX").replace("-x86_64-linux-gnu.so", "-aarch64-linux-gnu.so"))')"
70    fi
71
72    python setup.py bdist_wheel --cpp_implementation --compile_static_extension $plat_name_flag
73    cp dist/*.whl $abs_wheelhouse
74}
75
76function build_wheel {
77    build_wheel_cmd "bdist_wheel_cmd" $@
78}
79
80function run_tests {
81    # Runs tests on installed distribution from an empty directory
82    python --version
83    python -c "from google.protobuf.pyext import _message;"
84}
85
86if [ "$PLAT" == "aarch64" ]
87then
88  # when crosscompiling for aarch64, override the default multibuild's repair_wheelhouse logic
89  # since "auditwheel repair" doesn't work for crosscompiled wheels
90  function repair_wheelhouse {
91      echo "Skipping repair_wheelhouse since auditwheel requires build architecture to match wheel architecture."
92  }
93fi
94