| 1 | #!/usr/bin/envscip-shell filesystem . . `/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 | usagescip-shell shell shell-project 0.0.0 usage().() |
| 20 | { |
| 21 | printfscip-shell system . . printf. "\n" |
| 22 | printfscip-shell system . . printf. "Usage: %s -v <libgpiod_source_version> [-o <output_directory>] [-s <source_directory>] [-cfh]\n" "$0" |
| 23 | printfscip-shell system . . printf. "\t-v Version of libgpiod sources to bundle in sdist. Overrides LIBGPIOD_VERSION\n" |
| 24 | printfscip-shell system . . printf. "\t-o Directory to store outputs\n" |
| 25 | printfscip-shell system . . printf. "\t-s Directory with python binding sources\n" |
| 26 | printfscip-shell system . . printf. "\t-c Calculate checksums for generated outputs\n" |
| 27 | printfscip-shell system . . printf. "\t-f Forcibly remove old files from output directory\n" |
| 28 | printfscip-shell system . . 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=$(pwdscip-shell system . . pwd.) # Default to putting outputs in the current directory |
| 34 | source_dir=$(pwdscip-shell system . . 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 | usagescip-shell shell shell-project 0.0.0 usage(). |
| 57 | ;; |
| 58 | esac |
| 59 | done |
| 60 | |
| 61 | if [scip-shell system . . `[`. -z "${source_dir}" ] || [scip-shell system . . `[`. ! -d "${output_dir}" ]; then |
| 62 | printfscip-shell system . . printf. "Invalid source directory %s.\n" "${source_dir}" |
| 63 | exit 1 |
| 64 | fi |
| 65 | |
| 66 | if [scip-shell system . . `[`. -z "${output_dir}" ] || [scip-shell system . . `[`. ! -w "${output_dir}" ]; then |
| 67 | printfscip-shell system . . printf. "Output directory %s is not writable.\n" "${output_dir}" |
| 68 | exit 1 |
| 69 | fi |
| 70 | |
| 71 | if [scip-shell system . . `[`. -z "${src_version}" ]; then |
| 72 | printfscip-shell system . . 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/nullscip-shell filesystem . . `/dev/null`. 2>&1; then |
| 80 | printfscip-shell system . . 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 ! python3scip-shell system . . python3. -m pip -h >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1; then |
| 86 | printfscip-shell system . . 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=$(python3scip-shell system . . python3. -m venv -h >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1 && echoscip-shell system . . echo. 1 || echoscip-shell system . . echo. 0) |
| 92 | has_virtualenv=$(python3scip-shell system . . python3. -m virtualenv -h >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1 && echoscip-shell system . . echo. 1 || echoscip-shell system . . echo. 0) |
| 93 | |
| 94 | if ! { [scip-shell system . . `[`. "${has_venv}" -eq 1 ] || [scip-shell system . . `[`. "${has_virtualenv}" -eq 1 ]; }; then |
| 95 | printfscip-shell system . . printf. "A virtual environment module is required to generate wheels.\n" |
| 96 | exit 1 |
| 97 | fi |
| 98 | |
| 99 | venv_module=$([scip-shell system . . `[`. "${has_virtualenv}" -eq 1 ] && echoscip-shell system . . echo. "virtualenv" || echoscip-shell system . . echo. "venv" ) |
| 100 | |
| 101 | # Stage the build in a temp directory. |
| 102 | cur_dir=$(pwdscip-shell system . . pwd.) |
| 103 | temp_dir=$(mktempscip-shell system . . mktemp. -d) |
| 104 | cd "${temp_dir}" || { printfscip-shell system . . printf. "Failed to enter temp directory.\n"; exit 1; } |
| 105 | |
| 106 | # Setup a virtual environment |
| 107 | python3scip-shell system . . 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=$(findscip-shell system . . 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 [scip-shell system . . `[`. "${force}" -eq 1 ]; then |
| 122 | printfscip-shell system . . printf. "\nRemoving files from %s/dist/\n" "${output_dir}" |
| 123 | rmscip-shell system . . rm. -rf "${output_dir}/dist/" |
| 124 | fi |
| 125 | |
| 126 | cpscip-shell system . . cp. -fa dist/ "${output_dir}/" |
| 127 | |
| 128 | if [scip-shell system . . `[`. "${calc_hash}" -eq 1 ]; then |
| 129 | printfscip-shell system . . printf. "\nHashes for generated outputs:\n" |
| 130 | sha256sumscip-shell system . . sha256sum. "${output_dir}/dist/"* |
| 131 | fi |
| 132 | |
| 133 | cd "${cur_dir}" || { printfscip-shell system . . printf. "Failed to return to previous working directory.\n"; exit 1; } |
| 134 | rmscip-shell system . . rm. -rf "${temp_dir}" |