|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# Copyright (c) The Bitcoin Core developers |
| 4 | +# Distributed under the MIT software license, see the accompanying |
| 5 | +# file COPYING or https://opensource.org/license/mit/. |
| 6 | +# |
| 7 | +# Package cross-compiled Windows binaries + runtime DLLs into a directory |
| 8 | +# suitable for uploading as a workflow artifact. Must be run inside the |
| 9 | +# nix shell produced by `ci/configs/windows.bash` so the mingw toolchain |
| 10 | +# (objdump, g++) is on PATH. |
| 11 | + |
| 12 | +export LC_ALL=C.UTF-8 |
| 13 | + |
| 14 | +set -o errexit -o nounset -o pipefail -o xtrace |
| 15 | + |
| 16 | +readonly SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)" |
| 17 | +readonly REPO_DIR="$(cd -- "${SCRIPT_DIR}/../.." && pwd)" |
| 18 | + |
| 19 | +[ "${CI_CONFIG+x}" ] && source "$CI_CONFIG" |
| 20 | +: "${CI_DIR:=build-windows}" |
| 21 | + |
| 22 | +readonly BUILD_DIR="${REPO_DIR}/${CI_DIR}" |
| 23 | +readonly ARTIFACT_DIR="${REPO_DIR}/windows-cross-artifact" |
| 24 | + |
| 25 | +# Nix's cross stdenv exports these; fall back to the canonical names if not. |
| 26 | +: "${CXX:=x86_64-w64-mingw32-g++}" |
| 27 | +: "${OBJDUMP:=x86_64-w64-mingw32-objdump}" |
| 28 | + |
| 29 | +copy_artifact_file() { |
| 30 | + install -D -m 0755 "$1" "${ARTIFACT_DIR}/$2" |
| 31 | +} |
| 32 | + |
| 33 | +# Runtime DLLs for cross-compiled dependencies live in the sibling bin/ |
| 34 | +# directory of each -L path the cross stdenv adds to NIX_LDFLAGS (e.g. |
| 35 | +# capnproto, mcfgthreads). Collect those bin/ dirs once for dll lookup. |
| 36 | +EXTRA_DLL_DIRS=() |
| 37 | +for flag in ${NIX_LDFLAGS:-}; do |
| 38 | + case "${flag}" in |
| 39 | + -L*) |
| 40 | + candidate="${flag#-L}/../bin" |
| 41 | + [[ -d "${candidate}" ]] && EXTRA_DLL_DIRS+=("${candidate}") |
| 42 | + ;; |
| 43 | + esac |
| 44 | +done |
| 45 | + |
| 46 | +copy_runtime_dlls() { |
| 47 | + local exe="$1" |
| 48 | + local dll dll_path dir |
| 49 | + while read -r dll; do |
| 50 | + [[ -n "${dll}" ]] || continue |
| 51 | + # Skip DLLs that ship with Windows / Wine. |
| 52 | + case "${dll}" in |
| 53 | + ADVAPI32.dll|COMBASE.dll|COMCTL32.dll|GDI32.dll|KERNEL32.dll|OLE32.dll|OLEAUT32.dll|RPCRT4.dll|SHELL32.dll|UCRTBASE.dll|USER32.dll|WS2_32.dll) |
| 54 | + continue |
| 55 | + ;; |
| 56 | + esac |
| 57 | + dll_path="$(${CXX} -print-file-name="${dll}")" |
| 58 | + if [[ "${dll_path}" == "${dll}" || ! -f "${dll_path}" ]]; then |
| 59 | + dll_path="" |
| 60 | + for dir in "${EXTRA_DLL_DIRS[@]+"${EXTRA_DLL_DIRS[@]}"}"; do |
| 61 | + if [[ -f "${dir}/${dll}" ]]; then |
| 62 | + dll_path="${dir}/${dll}" |
| 63 | + break |
| 64 | + fi |
| 65 | + done |
| 66 | + fi |
| 67 | + if [[ -z "${dll_path}" || ! -f "${dll_path}" ]]; then |
| 68 | + case "${dll}" in |
| 69 | + lib*.dll) |
| 70 | + echo "Could not locate runtime DLL ${dll}." >&2 |
| 71 | + exit 1 |
| 72 | + ;; |
| 73 | + *) |
| 74 | + continue |
| 75 | + ;; |
| 76 | + esac |
| 77 | + fi |
| 78 | + install -D -m 0755 "${dll_path}" "${ARTIFACT_DIR}/${dll}" |
| 79 | + done < <(${OBJDUMP} -p "${exe}" | awk '/DLL Name: / {print $3}' | sort -u) |
| 80 | +} |
| 81 | + |
| 82 | +rm -rf "${ARTIFACT_DIR}" |
| 83 | + |
| 84 | +readonly EXES=( |
| 85 | + test/mptest.exe |
| 86 | + example/mpexample.exe |
| 87 | + example/mpcalculator.exe |
| 88 | + example/mpprinter.exe |
| 89 | +) |
| 90 | + |
| 91 | +for exe in "${EXES[@]}"; do |
| 92 | + copy_artifact_file "${BUILD_DIR}/${exe}" "${exe}" |
| 93 | +done |
| 94 | + |
| 95 | +for exe in "${EXES[@]}"; do |
| 96 | + copy_runtime_dlls "${BUILD_DIR}/${exe}" |
| 97 | +done |
0 commit comments