raz / src / cross.sh

69 lines · 2.4 KiB · executable · raw · history · last change b6a29ae3

1#!/bin/sh
2# A compiler that runs here and compiles for TARGET (OS-ARCH, as
3# darwin-arm64 or linux-x86_64): raz built by staging, for the target
4# is whatever files the compiler was built from (doc/raz.md, 3).
5#
6# src/cross.sh TARGET [OUT] run from the repository root
7# HOST=darwin src/cross.sh darwin-arm64 [OUT]
8#
9# The second makes the compiler that runs on a Mac and compiles for it,
10# the first Mac compiler (PLAN.md 5, step 9): the stage has the host's
11# own Host.darwin.raz, and it is built by the cross compiler of the
12# first form, which must be there, since BOOT's makes programs for this
13# system, and a stage for another host can't be run here to be built.
14#
15# Stage build/cross/TARGET/src/ holds src/'s generic files and, under
16# the plain names, the target's own: Gen.ARCH.raz as Gen.raz and
17# Exe.OS.raz as Exe.raz; and the host's, Host.OS.raz as Host.raz, the
18# host being the system the compiler will run on, which is this one.
19# With only plain names there, module lookup has one choice each. BOOT's
20# release builds the stage as the Makefile builds raz, from a directory
21# where src/Raz.raz is again at src/, so the paths a program holds are
22# the same. The compiler is OUT, build/cross/TARGET/raz (or TARGET-on-HOST) if not said;
23# its library is lib/, beside it.
24set -e
25target=$1
26case $target in
27*-*) ;;
28*) echo "usage: src/cross.sh OS-ARCH [OUT]" >&2; exit 2;;
29esac
30os=${target%%-*}
31arch=${target#*-}
32here=$(uname -s | tr A-Z a-z)
33host=${HOST:-$here} # the OS that runs the compiler
34
35# Each unit with files by target, and the file this build takes of it
36missing=
37for f in Gen.$arch.raz Exe.$os.raz Host.$host.raz; do
38 [ -f src/$f ] || missing="$missing src/$f"
39done
40if [ -n "$missing" ]; then
41 echo "cross.sh: no files for $target (host $host):$missing" >&2
42 exit 1
43fi
44[ -f build/src/Version.raz ] || make build/src/Version.raz
45[ -x build/boot/raz ] || make build/boot/raz
46
47d=build/cross/$target
48[ $host != $here ] && d=$d-on-$host
49out=${2:-$d/raz}
50rm -rf $d
51mkdir -p $d/src $d/v
52for f in src/*.raz; do
53 case $f in
54 *.*.raz) ;; # a target's file, below
55 *) cp $f $d/src/;;
56 esac
57done
58cp src/Gen.$arch.raz $d/src/Gen.raz
59cp src/Exe.$os.raz $d/src/Exe.raz
60cp src/Host.$host.raz $d/src/Host.raz
61cp build/src/Version.raz $d/v/
62ln -s ../../../lib $d/lib
63boot=$PWD/build/boot/raz
64[ $host != $here ] && boot=$PWD/build/cross/$target/raz # the cross compiler, run here
65(cd $d && $boot -Werror -I src -I v -o raz src/Raz.raz)
66case $out in
67$d/raz) ;;
68*) cp $d/raz "$out";;
69esac