|
| 1 | +#!/bin/sh |
| 2 | + |
| 3 | +set -eu |
| 4 | + |
| 5 | +[ -z "${ARCH+x}" ] && ARCH=universal |
| 6 | + |
| 7 | +case "${ARCH}" in |
| 8 | + arm64) |
| 9 | + # Build for arm64, either as a same-architecture or cross-architecture |
| 10 | + # build. |
| 11 | + CONFIGURE_HOST_ARG="--host=aarch64-apple-darwin" |
| 12 | + CC_ARCH_ARG="-arch ${ARCH}" |
| 13 | + |
| 14 | + # Don't collide with macosx, which has traditionally been used for x86_64 |
| 15 | + # code. |
| 16 | + ARCH_DIR=mac-arm64 |
| 17 | + ;; |
| 18 | + universal) |
| 19 | + # Build for x86_64 and arm64 simultaneously, putting the results into |
| 20 | + # "universal" or "fat" binaries. |
| 21 | + CONFIGURE_HOST_ARG= |
| 22 | + CC_ARCH_ARG="-arch x86_64 -arch arm64" |
| 23 | + |
| 24 | + # It's fine to put universal code into macosx, traditionally used for x86_64 |
| 25 | + # code, because it will contain x86_64 code and will run without trouble on |
| 26 | + # x86_64 systems. Using the same directory for universal files means that |
| 27 | + # these tools will remain at a stable path across a transition between |
| 28 | + # x86_64 and arm64. |
| 29 | + ARCH_DIR=macosx |
| 30 | + ;; |
| 31 | + x86_64) |
| 32 | + # Build for x86_64, either as a same-architecture or cross-architecture |
| 33 | + # build. |
| 34 | + CONFIGURE_HOST_ARG="--host=${ARCH}-apple-darwin" |
| 35 | + CC_ARCH_ARG="-arch ${ARCH}" |
| 36 | + |
| 37 | + ARCH_DIR=macosx |
| 38 | + ;; |
| 39 | + *) |
| 40 | + # Build for some other ${ARCH}, either as a same-architecture or |
| 41 | + # cross-architecture build. |
| 42 | + CONFIGURE_HOST_ARG="--host=${ARCH}-apple-darwin" |
| 43 | + CC_ARCH_ARG="-arch ${ARCH}" |
| 44 | + |
| 45 | + ARCH_DIR="mac-${ARCH}" |
| 46 | + ;; |
| 47 | +esac |
| 48 | + |
| 49 | +if [ -z "${MIN_OS_VERSION+x}" ]; then |
| 50 | + case "${ARCH}" in |
| 51 | + arm64) |
| 52 | + # macOS 11 was the first to support arm64. |
| 53 | + MIN_OS_VERSION=11.0 |
| 54 | + ;; |
| 55 | + universal | x86_64) |
| 56 | + # Although this could be set lower, the xPack tools declare support for |
| 57 | + # macOS 10.13, so it's unlikely that anyone would be using anything older. |
| 58 | + # |
| 59 | + # In a universal build, the toolchain will use this value for the x86_64 |
| 60 | + # build, but will automatically increase the minimum to 11.0 for the arm64 |
| 61 | + # build, because that was the first OS version to support arm64. |
| 62 | + MIN_OS_VERSION=10.13 |
| 63 | + ;; |
| 64 | + esac |
| 65 | +fi |
| 66 | +if [ -n "${MIN_OS_VERSION+x}" ]; then |
| 67 | + CC_MIN_OS_VERSION_ARG="-mmacosx-version-min=${MIN_OS_VERSION}" |
| 68 | +fi |
| 69 | + |
| 70 | +MAKE_J_ARG="-j$(sysctl -n hw.ncpu)" |
| 71 | +BASE_CFLAGS="-Os -flto" |
| 72 | +BASE_LDFLAGS="-dead_strip" |
| 73 | + |
| 74 | +LIBUSB_VERSION=1.0.27 |
| 75 | +DFU_UTIL_VERSION=0.11 |
| 76 | +STM32_HID_BOOTLOADER_VERSION=2.2.2 |
| 77 | + |
| 78 | +set -x |
| 79 | + |
| 80 | +cd "$(dirname "${0}")/.." |
| 81 | + |
| 82 | +( |
| 83 | + cd .. |
| 84 | + |
| 85 | + # dfu-util depends on libusb, which isn't part of the operating system. |
| 86 | + # Provide a build of libusb to satisfy that dependency. |
| 87 | + [ ! -d libusb ] && git clone https://github.com/libusb/libusb.git |
| 88 | + cd libusb |
| 89 | + git checkout "v${LIBUSB_VERSION}" |
| 90 | + # git clean -fdx |
| 91 | + # git checkout -- . |
| 92 | + sh bootstrap.sh |
| 93 | + CC="clang ${CC_ARCH_ARG} ${CC_MIN_OS_VERSION_ARG}" \ |
| 94 | + CFLAGS="${BASE_CFLAGS}" \ |
| 95 | + LDFLAGS="${BASE_LDFLAGS} -Wl,-source_version,${LIBUSB_VERSION}" \ |
| 96 | + sh configure ${CONFIGURE_HOST_ARG} |
| 97 | + make clean |
| 98 | + make ${MAKE_J_ARG} |
| 99 | + |
| 100 | + # Rewrite the LC_ID_DYLIB in libusb-1.0.0.dylib so that other modules that |
| 101 | + # link against it will expect to find it in the same directory that they are |
| 102 | + # located in (@loader_path). Later, libusb-1.0.0.dylib will be copied to the |
| 103 | + # same directory as dfu-util and the other executables that rely on it. |
| 104 | + install_name_tool \ |
| 105 | + -id @loader_path/libusb-1.0.0.dylib \ |
| 106 | + libusb/.libs/libusb-1.0.0.dylib |
| 107 | + |
| 108 | + cd .. |
| 109 | + |
| 110 | + [ ! -d dfu-util ] && git clone git://git.code.sf.net/p/dfu-util/dfu-util |
| 111 | + cd dfu-util |
| 112 | + git checkout "v${DFU_UTIL_VERSION}" |
| 113 | + # git clean -fdx |
| 114 | + # git checkout -- . |
| 115 | + sh autogen.sh |
| 116 | + CC="clang ${CC_ARCH_ARG} ${CC_MIN_OS_VERSION_ARG}" \ |
| 117 | + CFLAGS="${BASE_CFLAGS} -fvisibility=hidden" \ |
| 118 | + LDFLAGS="${BASE_LDFLAGS} -Wl,-source_version,${DFU_UTIL_VERSION}" \ |
| 119 | + USB_CFLAGS="-I$(pwd)/../libusb/libusb" \ |
| 120 | + USB_LIBS="-L$(pwd)/../libusb/libusb/.libs -lusb-1.0.0" \ |
| 121 | + sh configure ${CONFIGURE_HOST_ARG} |
| 122 | + make clean |
| 123 | + make ${MAKE_J_ARG} |
| 124 | + cd .. |
| 125 | + |
| 126 | + [ ! -d STM32_HID_Bootloader ] && |
| 127 | + git clone https://github.com/Serasidis/STM32_HID_Bootloader.git |
| 128 | + cd STM32_HID_Bootloader |
| 129 | + git checkout "${STM32_HID_BOOTLOADER_VERSION}" |
| 130 | + # git clean -fdx |
| 131 | + # git checkout -- . |
| 132 | + git checkout -- cli/main.c |
| 133 | + |
| 134 | + # Isolate and apply the patch from |
| 135 | + # https://github.com/Serasidis/STM32_HID_Bootloader/issues/68#issuecomment-2009105851 |
| 136 | + curl --silent \ |
| 137 | + https://api.github.com/repos/Serasidis/STM32_HID_Bootloader/issues/comments/2009105851 | |
| 138 | + jq --raw-output .body | |
| 139 | + sed -e 's/\r//g' -e '1,/^```/d' -e '/^```$/,$d' | |
| 140 | + patch cli/main.c |
| 141 | + |
| 142 | + make -C cli clean |
| 143 | + make -C cli ${MAKE_J_ARG} \ |
| 144 | + CC="clang ${CC_ARCH_ARG} ${CC_MIN_OS_VERSION_ARG}" \ |
| 145 | + CFLAGS="${BASE_CFLAGS} -fvisibility=hidden -Wall -Werror -c" \ |
| 146 | + LDFLAGS="${BASE_LDFLAGS} -Wl,-source_version,${STM32_HID_BOOTLOADER_VERSION}" |
| 147 | + cd .. |
| 148 | +) |
| 149 | + |
| 150 | +mkdir -p "${ARCH_DIR}" |
| 151 | + |
| 152 | +cp ../libusb/libusb/.libs/libusb-1.0.0.dylib \ |
| 153 | + ../dfu-util/src/dfu-prefix \ |
| 154 | + ../dfu-util/src/dfu-suffix \ |
| 155 | + ../dfu-util/src/dfu-util \ |
| 156 | + ../STM32_HID_Bootloader/cli/hid-flash \ |
| 157 | + "${ARCH_DIR}/" |
| 158 | +chmod -x "${ARCH_DIR}/libusb-1.0.0.dylib" |
| 159 | + |
| 160 | +# It would be nice to include -Wl,-source_version here, but that's tricky to do |
| 161 | +# for code that's in this repository, and probably won't be tagged with a |
| 162 | +# version until after this tool is built. |
| 163 | +clang \ |
| 164 | + ${CC_ARCH_ARG} \ |
| 165 | + ${CC_MIN_OS_VERSION_ARG} \ |
| 166 | + ${BASE_CFLAGS} \ |
| 167 | + -fvisibility=hidden \ |
| 168 | + ${BASE_LDFLAGS} \ |
| 169 | + -Wall \ |
| 170 | + -Werror \ |
| 171 | + -o "${ARCH_DIR}/upload_reset" \ |
| 172 | + src/upload_reset/unix/upload_reset.c |
0 commit comments