#!/usr/bin/env bash
# Copyright © 2017,2019 Jan Nieuwenhuizen <janneke@gnu.org>
# Copyright © 2017,2019 Jeremiah Orians
# Copyright © 2020 Sanne Wouda
#
# This file is part of stage0.
#
# stage0 is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or (at
# your option) any later version.
#
# stage0 is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with stage0.  If not, see <http://www.gnu.org/licenses/>.

#################################################
# Phase-12 Build final blood-elf from C sources #
#################################################
./M2 --architecture aarch64 \
	-f ../M2libc/sys/types.h \
	-f ../M2libc/stddef.h \
	-f ../M2libc/aarch64/Linux/unistd.h \
	-f ../M2libc/aarch64/Linux/fcntl.h \
	-f ../M2libc/stdlib.c \
	-f ../M2libc/stdio.c \
	-f ../M2libc/bootstrappable.c \
	-f ../mescc-tools/stringify.c \
	-f ../mescc-tools/blood-elf.c \
	--debug \
	-o blood-elf.M1

./blood-elf-0 --64 --little-endian -f blood-elf.M1 -o blood-elf-footer.M1

../bin/M1 --architecture aarch64 \
	--little-endian \
	-f ../M2libc/aarch64/aarch64_defs.M1 \
	-f ../M2libc/aarch64/libc-full.M1 \
	-f blood-elf.M1 \
	-f blood-elf-footer.M1 \
	-o hold

../bin/hex2 --architecture aarch64 \
	--little-endian \
	--base-address 0x00600000 \
	-f ../M2libc/aarch64/ELF-aarch64-debug.hex2 \
	-f hold \
	-o ../bin/blood-elf

# Now we have our shipping debuggable blood-elf, the rest will be down hill from
# here as we have ALL of the core pieces of compiling and assembling debuggable
# programs in a debuggable form with corresponding C source code.

#############################################
# Phase-13 Build get_machine from C sources #
#############################################
./M2 --architecture aarch64 \
	-f ../M2libc/sys/types.h \
	-f ../M2libc/stddef.h \
	-f ../M2libc/aarch64/Linux/unistd.h \
	-f ../M2libc/aarch64/Linux/fcntl.h \
	-f ../M2libc/stdlib.c \
	-f ../M2libc/stdio.c \
	-f ../M2libc/bootstrappable.c \
	-f ../mescc-tools/get_machine.c \
	--debug \
	-o get_machine.M1

../bin/blood-elf --64 --little-endian -f get_machine.M1 -o get_machine-footer.M1

../bin/M1 --architecture aarch64 \
	--little-endian \
	-f ../M2libc/aarch64/aarch64_defs.M1 \
	-f ../M2libc/aarch64/libc-full.M1 \
	-f get_machine.M1 \
	-f get_machine-footer.M1 \
	-o hold

../bin/hex2 --architecture aarch64 \
	--little-endian \
	--base-address 0x00600000 \
	-f ../M2libc/aarch64/ELF-aarch64-debug.hex2 \
	-f hold \
	-o ../bin/get_machine

############################################
# Phase-14 Build M2-Planet from M2-Planet  #
############################################

./M2 --architecture aarch64 \
	-f ../M2libc/sys/types.h \
	-f ../M2libc/stddef.h \
	-f ../M2libc/aarch64/Linux/unistd.h \
	-f ../M2libc/aarch64/Linux/fcntl.h \
	-f ../M2libc/stdlib.c \
	-f ../M2libc/stdio.c \
	-f ../M2libc/bootstrappable.c \
	-f ../M2-Planet/cc.h \
	-f ../M2-Planet/cc_globals.c \
	-f ../M2-Planet/cc_reader.c \
	-f ../M2-Planet/cc_strings.c \
	-f ../M2-Planet/cc_types.c \
	-f ../M2-Planet/cc_core.c \
	-f ../M2-Planet/cc_macro.c \
	-f ../M2-Planet/cc.c \
	--debug \
	-o M2.M1

../bin/blood-elf --64 --little-endian -f M2.M1 -o M2-footer.M1

../bin/M1 --architecture aarch64 \
	--little-endian \
	-f ../M2libc/aarch64/aarch64_defs.M1 \
	-f ../M2libc/aarch64/libc-full.M1 \
	-f M2.M1 \
	-f M2-footer.M1 \
	-o hold

../bin/hex2 --architecture aarch64 \
	--little-endian \
	--base-address 0x00600000 \
	-f ../M2libc/aarch64/ELF-aarch64-debug.hex2 \
	-f hold \
	-o ../bin/M2-Planet
