| 1 | #!/usr/bin/env sh |
| 2 | # SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | # SPDX-FileCopyrightText: 2024 Vincent Fazio <[email protected]> |
| 4 | # |
| 5 | # This is a script to generate an sdist and wheels for publishing to PyPI. |
| 6 | # |
| 7 | # This script requires: |
| 8 | # * Python3 + venv or virtualenv + pip |
| 9 | # * Docker or Podman (https://cibuildwheel.pypa.io/en/stable/options/#container-engine) |
| 10 | # * binfmt support and qemu-user-static for AArch64 registered as Fixed |
| 11 | # (https://docs.kernel.org/admin-guide/binfmt-misc.html) |
| 12 | # |
| 13 | # On Debian based systems, AArch64 binfmt support can be checked via: |
| 14 | # cat /proc/sys/fs/binfmt_misc/qemu-aarch64 |
| 15 | # |
| 16 | # The entry should be enabled and "F" should be in the list of flags. |
| 17 | # |
| 18 | |
| 19 | usage() |
| 20 | { |
| 21 | printf "\n" |
| 22 | printf "Usage: %s -v <libgpiod_source_version> [-o <output_directory>] [-s <source_directory>] [-cfh]\n" "$0" |
| 23 | printf "\t-v Version of libgpiod sources to bundle in sdist. Overrides LIBGPIOD_VERSION\n" |
| 24 | printf "\t-o Directory to store outputs\n" |
| 25 | printf "\t-s Directory with python binding sources\n" |
| 26 | printf "\t-c Calculate checksums for generated outputs\n" |
| 27 | printf "\t-f Forcibly remove old files from output directory\n" |
| 28 | printf "\t-h Show this help output\n" |
| 29 | exit 1 |
| 30 | } |
| 31 | |
| 32 | src_version=${LIBGPIOD_VERSION:-} # Default to environment specified library version |
| 33 | output_dir=$(pwd) # Default to putting outputs in the current directory |
| 34 | source_dir=$(pwd) # Assume the current directory has the python binding sources |
| 35 | calc_hash=0 # Do not calculate hashes by default |
| 36 | force=0 # Do not forcibly remove files by default |
| 37 | |
| 38 | while getopts :hv:o:s:cf value; do |
| 39 | case ${value} in |
| 40 | c) |
| 41 | calc_hash=1 |
| 42 | ;; |
| 43 | f) |
| 44 | force=1 |
| 45 | ;; |
| 46 | o) |
| 47 | output_dir=${OPTARG} |
| 48 | ;; |
| 49 | s) |
| 50 | source_dir=${OPTARG} |
| 51 | ;; |
| 52 | v) |
| 53 | src_version=${OPTARG} |
| 54 | ;; |
| 55 | h | *) |
| 56 | usage |
| 57 | ;; |
| 58 | esac |
| 59 | done |
| 60 | |
| 61 | if [ -z "${source_dir}" ] || [ ! -d "${output_dir}" ]; then |
| 62 | printf "Invalid source directory %s.\n" "${source_dir}" |
| 63 | exit 1 |
| 64 | fi |
| 65 | |
| 66 | if [ -z "${output_dir}" ] || [ ! -w "${output_dir}" ]; then |
| 67 | printf "Output directory %s is not writable.\n" "${output_dir}" |
| 68 | exit 1 |
| 69 | fi |
| 70 | |
| 71 | if [ -z "${src_version}" ]; then |
| 72 | printf "The libgpiod source version must be specified.\n" |
| 73 | exit 1 |
| 74 | fi |
| 75 | |
| 76 | shift $((OPTIND-1)) |
| 77 | |
| 78 | # We require Python3 for building artifacts |
| 79 | if ! command -v python3 >/dev/null 2>&1; then |
| 80 | printf "Python3 is required to generate PyPI artifacts.\n" |
| 81 | exit 1 |
| 82 | fi |
| 83 | |
| 84 | # Pip is necessary for installing build dependencies |
| 85 | if ! python3 -m pip -h >/dev/null 2>&1; then |
| 86 | printf "The pip module is required to generate wheels.\n" |
| 87 | exit 1 |
| 88 | fi |
| 89 | |
| 90 | # Check for a virtual environment tool to not pollute user installed packages |
| 91 | has_venv=$(python3 -m venv -h >/dev/null 2>&1 && echo 1 || echo 0) |
| 92 | has_virtualenv=$(python3 -m virtualenv -h >/dev/null 2>&1 && echo 1 || echo 0) |
| 93 | |
| 94 | if ! { [ "${has_venv}" -eq 1 ] || [ "${has_virtualenv}" -eq 1 ]; }; then |
| 95 | printf "A virtual environment module is required to generate wheels.\n" |
| 96 | exit 1 |
| 97 | fi |
| 98 | |
| 99 | venv_module=$([ "${has_virtualenv}" -eq 1 ] && echo "virtualenv" || echo "venv" ) |
| 100 | |
| 101 | # Stage the build in a temp directory. |
| 102 | cur_dir=$(pwd) |
| 103 | temp_dir=$(mktemp -d) |
| 104 | cd "${temp_dir}" || { printf "Failed to enter temp directory.\n"; exit 1; } |
| 105 | |
| 106 | # Setup a virtual environment |
| 107 | python3 -m "${venv_module}" .venv |
| 108 | venv_python="${temp_dir}/.venv/bin/python" |
| 109 | |
| 110 | # Install build dependencies |
| 111 | # cibuildwheel 2.18.1 pins the toolchain containers to 2024-05-13-0983f6f |
| 112 | ${venv_python} -m pip install build==1.2.1 cibuildwheel==2.18.1 |
| 113 | |
| 114 | LIBGPIOD_VERSION=${src_version} ${venv_python} -m build --sdist --outdir ./dist "${source_dir}" |
| 115 | sdist=$(find ./dist -name '*.tar.gz') |
| 116 | |
| 117 | # Target only CPython and X86_64 + AArch64 Linux wheels unless specified otherwise via environment variables |
| 118 | CIBW_BUILD=${CIBW_BUILD:-"cp*"} CIBW_ARCHS=${CIBW_ARCHS:-"x86_64,aarch64"} \ |
| 119 | ${venv_python} -m cibuildwheel --platform linux "${sdist}" --output-dir dist/ |
| 120 | |
| 121 | if [ "${force}" -eq 1 ]; then |
| 122 | printf "\nRemoving files from %s/dist/\n" "${output_dir}" |
| 123 | rm -rf "${output_dir}/dist/" |
| 124 | fi |
| 125 | |
| 126 | cp -fa dist/ "${output_dir}/" |
| 127 | |
| 128 | if [ "${calc_hash}" -eq 1 ]; then |
| 129 | printf "\nHashes for generated outputs:\n" |
| 130 | sha256sum "${output_dir}/dist/"* |
| 131 | fi |
| 132 | |
| 133 | cd "${cur_dir}" || { printf "Failed to return to previous working directory.\n"; exit 1; } |
| 134 | rm -rf "${temp_dir}" |