1#!/usr/bin/env bash 2#===----------------------------------------------------------------------===## 3# 4# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5# See https://llvm.org/LICENSE.txt for license information. 6# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7# 8#===----------------------------------------------------------------------===## 9 10# 11# This script builds picolibc (https://github.com/picolibc/picolibc) from 12# source to facilitate building libc++ against it. 13# 14 15set -e 16 17PROGNAME="$(basename "${0}")" 18 19function error() { printf "error: %s\n" "$*" >&2; exit 1; } 20 21function usage() { 22cat <<EOF 23Usage: 24${PROGNAME} [options] 25 26[-h|--help] Display this help and exit. 27 28--build-dir <DIR> Path to the directory to use for building. 29 30--install-dir <DIR> Path to the directory to install the library to. 31EOF 32} 33 34while [[ $# -gt 0 ]]; do 35 case ${1} in 36 -h|--help) 37 usage 38 exit 0 39 ;; 40 --build-dir) 41 build_dir="${2}" 42 shift; shift 43 ;; 44 --install-dir) 45 install_dir="${2}" 46 shift; shift 47 ;; 48 --target) 49 target="${2}" 50 shift; shift 51 ;; 52 *) 53 error "Unknown argument '${1}'" 54 ;; 55 esac 56done 57 58for arg in build_dir install_dir target; do 59 if [ -z ${!arg+x} ]; then 60 error "Missing required argument '--${arg//_/-}'" 61 elif [ "${!arg}" == "" ]; then 62 error "Argument to --${arg//_/-} must not be empty" 63 fi 64done 65 66 67echo "--- Downloading picolibc" 68picolibc_source_dir="${build_dir}/picolibc-source" 69picolibc_build_dir="${build_dir}/picolibc-build" 70mkdir -p "${picolibc_source_dir}" 71mkdir -p "${picolibc_build_dir}" 72# Download the version of picolibc that was the latest at the time this script was written. 73# Following changes are required and were introduced after version 1.8.5: 74# - updated semihost arguments handling, 75# - added missing macros in stdio.h 76# - external linkage for isblank 77# Version following 1.8.5, was not released by the time of writing. 78picolibc_commit="04a90c56d7aac61880f205ec29b3dce6a9de0342" 79curl -L "https://github.com/picolibc/picolibc/archive/${picolibc_commit}.zip" --output "${picolibc_source_dir}/picolibc.zip" 80unzip -q "${picolibc_source_dir}/picolibc.zip" -d "${picolibc_source_dir}" 81mv "${picolibc_source_dir}/picolibc-${picolibc_commit}"/* "${picolibc_source_dir}" 82rm -rf "${picolibc_source_dir}/picolibc-${picolibc_commit}" 83 84cat <<EOF > "${picolibc_build_dir}/meson-cross-build.txt" 85[binaries] 86c = ['${CC:-cc}', '--target=${target}', '-mfloat-abi=soft', '-nostdlib'] 87ar = 'llvm-ar' 88as = 'llvm-as' 89ld = 'lld' 90strip = 'llvm-strip' 91[host_machine] 92system = 'none' 93cpu_family = 'arm' 94cpu = 'arm' 95endian = 'little' 96[properties] 97skip_sanity_check = true 98EOF 99 100venv_dir="${build_dir}/meson-venv" 101python3 -m venv "${venv_dir}" 102# Install the version of meson that was the latest at the time this script was written. 103"${venv_dir}/bin/pip" install "meson==1.1.1" 104 105"${venv_dir}/bin/meson" setup \ 106 -Dincludedir=include -Dlibdir=lib -Dspecsdir=none -Dmultilib=false -Dpicoexit=false \ 107 --prefix "${install_dir}" \ 108 --cross-file "${picolibc_build_dir}/meson-cross-build.txt" \ 109 "${picolibc_build_dir}" \ 110 "${picolibc_source_dir}" 111 112"${venv_dir}/bin/meson" install -C "${picolibc_build_dir}" 113