| 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. |
| 24 | set -e |
| 25 | target=$1 |
| 26 | case $target in |
| 27 | *-*) ;; |
| 28 | *) echo "usage: src/cross.sh OS-ARCH [OUT]" >&2; exit 2;; |
| 29 | esac |
| 30 | os=${target%%-*} |
| 31 | arch=${target#*-} |
| 32 | here=$(uname -s | tr A-Z a-z) |
| 33 | host=${HOST:-$here} # the OS that runs the compiler |
| 34 | |
| 35 | # Each unit with files by target, and the file this build takes of it |
| 36 | missing= |
| 37 | for f in Gen.$arch.raz Exe.$os.raz Host.$host.raz; do |
| 38 | [ -f src/$f ] || missing="$missing src/$f" |
| 39 | done |
| 40 | if [ -n "$missing" ]; then |
| 41 | echo "cross.sh: no files for $target (host $host):$missing" >&2 |
| 42 | exit 1 |
| 43 | fi |
| 44 | [ -f build/src/Version.raz ] || make build/src/Version.raz |
| 45 | [ -x build/boot/raz ] || make build/boot/raz |
| 46 | |
| 47 | d=build/cross/$target |
| 48 | [ $host != $here ] && d=$d-on-$host |
| 49 | out=${2:-$d/raz} |
| 50 | rm -rf $d |
| 51 | mkdir -p $d/src $d/v |
| 52 | for f in src/*.raz; do |
| 53 | case $f in |
| 54 | *.*.raz) ;; # a target's file, below |
| 55 | *) cp $f $d/src/;; |
| 56 | esac |
| 57 | done |
| 58 | cp src/Gen.$arch.raz $d/src/Gen.raz |
| 59 | cp src/Exe.$os.raz $d/src/Exe.raz |
| 60 | cp src/Host.$host.raz $d/src/Host.raz |
| 61 | cp build/src/Version.raz $d/v/ |
| 62 | ln -s ../../../lib $d/lib |
| 63 | boot=$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) |
| 66 | case $out in |
| 67 | $d/raz) ;; |
| 68 | *) cp $d/raz "$out";; |
| 69 | esac |