#! /usr/bin/env bash
# Copyright © 2021 Andrius Štikonas

# 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/>.

# Can also be run by kaem or any other shell of your personal choice
# To run in kaem simply: kaem --verbose --strict

###########################################################
# Phase-0 Build kaem and hex0 from bootstrapped binaries  #
# This phase is run by kaem-optional-seed (kaem-micro)    #
# hex0-seed hex0_riscv64.hex0 hex0                        #
# hex0      kaem-minimal.hex0 kaem                        #
# kaem                                                    #
###########################################################

###############################
# Phase-0b Rebuild kaem-micro #
###############################
./hex0 kaem-micro.hex0 kaem-micro
# Not used but build for checksum validation

################################
# Phase-1 Build hex1 from hex0 #
################################
./hex0 hex1_riscv64.hex0 hex1
# hex1 adds support for single character labels

#################################
# Phase-1b Build catm from hex1 #
#################################
./hex1 catm_riscv64.hex1 catm
# catm removes the need for cat or shell support for redirection by providing
# equivalent functionality via catm output_file input1 input2 ... inputN

################################
# Phase-2 Build hex2 from hex1 #
################################
./hex1 hex2_riscv64.hex1 hex2-0
# hex2 adds support for multi character labels, absolute addresses
# and combining multiple Risc-V words into a single word.
# This allows it to function as an effective linker for later stages of the
# bootstrap. This is a minimal version which will be used to bootstrap
# cross-platform version in a later stage.

###############################
# Phase-3 Build M0 from hex2  #
###############################
./catm hold.hex2 ELF-riscv64.hex2 M0_riscv64.hex2
./hex2-0 hold.hex2 M0
# M0 is the architecture specific version of M1 and is by design single
# architecture only and will be replaced by the C code version of M1

#####################################
# Phase-4 Build cc_riscv64 from M0  #
#####################################
./catm hold.M1 riscv64_defs.M1 cc_riscv64.M1
./M0 hold.M1 hold.hex2
./catm hold1.hex2 ELF-riscv64.hex2 hold.hex2
./hex2-0 hold1.hex2 cc_riscv64

###########################################
# Phase-5 Build M2-Planet from cc_riscv64 #
###########################################
./catm hold \
	../M2libc/riscv64/Linux/bootstrap.c \
	../M2-Planet/cc.h \
	../M2libc/bootstrappable.c \
	../M2-Planet/cc_globals.c \
	../M2-Planet/cc_reader.c \
	../M2-Planet/cc_strings.c \
	../M2-Planet/cc_types.c \
	../M2-Planet/cc_core.c \
	../M2-Planet/cc_macro.c \
	../M2-Planet/cc.c
./cc_riscv64 hold M2.M1
./catm hold riscv64_defs.M1 libc-core.M1 M2.M1
./M0 hold temp1
./catm hold ELF-riscv64.hex2 temp1
./hex2-0 hold M2

#############################################
# Phase-6 Build blood-elf-0 from C sources  #
#############################################
./M2 --architecture riscv64 \
	-f ../M2libc/riscv64/Linux/bootstrap.c \
	-f ../M2libc/bootstrappable.c \
	-f ../mescc-tools/stringify.c \
	-f ../mescc-tools/blood-elf.c \
	--bootstrap-mode\
	-o blood-elf.M1

./catm hold ../M2libc/riscv64/riscv64_defs.M1 ../M2libc/riscv64/libc-core.M1 blood-elf.M1
./M0 hold temp1
./catm hold ../M2libc/riscv64/ELF-riscv64.hex2 temp1
./hex2-0 hold blood-elf-0
# This is the last stage where the binaries will not have debug info
# and the last piece built that isn't part of the output binaries

#####################################
# Phase-7 Build M1-0 from C sources #
#####################################
./M2 --architecture riscv64 \
	-f ../M2libc/riscv64/Linux/bootstrap.c \
	-f ../M2libc/bootstrappable.c \
	-f ../mescc-tools/stringify.c \
	-f ../mescc-tools/M1-macro.c \
	--bootstrap-mode \
	--debug \
	-o M1-macro.M1

./blood-elf-0 --64 --little-endian -f M1-macro.M1 -o M1-macro-footer.M1
./catm hold ../M2libc/riscv64/riscv64_defs.M1 ../M2libc/riscv64/libc-core.M1 M1-macro.M1 M1-macro-footer.M1
./M0 hold temp1
./catm hold ../M2libc/riscv64/ELF-riscv64-debug.hex2 temp1
./hex2-0 hold M1-0

# This is the last stage where M0 is used, as we will being using it's
# much more powerful and cross-platform version with a bunch of extra goodies.

#######################################
# Phase-8 Build hex2-1 from C sources #
#######################################
./M2 --architecture riscv64 \
	-f ../M2libc/sys/types.h \
	-f ../M2libc/riscv64/Linux/sys/stat.h \
	-f ../M2libc/stddef.h \
	-f ../M2libc/riscv64/Linux/unistd.h \
	-f ../M2libc/riscv64/Linux/fcntl.h \
	-f ../M2libc/stdlib.c \
	-f ../M2libc/stdio.c \
	-f ../M2libc/bootstrappable.c \
	-f ../mescc-tools/hex2.h \
	-f ../mescc-tools/hex2_linker.c \
	-f ../mescc-tools/hex2_word.c \
	-f ../mescc-tools/hex2.c \
	--debug \
	-o hex2_linker.M1

./blood-elf-0 --64 --little-endian -f hex2_linker.M1 -o hex2_linker-footer.M1
./M1-0 --architecture riscv64 \
	--little-endian \
	-f ../M2libc/riscv64/riscv64_defs.M1 \
	-f ../M2libc/riscv64/libc-full.M1 \
	-f hex2_linker.M1 \
	-f hex2_linker-footer.M1 \
	-o temp1

./catm hold ../M2libc/riscv64/ELF-riscv64-debug.hex2 temp1
./hex2-0 hold hex2-1

# This is the last stage where we will be using the handwritten hex2 and instead
# be using the far more powerful, cross-platform version with a bunch more goodies

###################################
# Phase-9 Build M1 from C sources #
###################################
./M2 --architecture riscv64 \
	-f ../M2libc/sys/types.h \
	-f ../M2libc/stddef.h \
	-f ../M2libc/string.c \
	-f ../M2libc/riscv64/Linux/unistd.h \
	-f ../M2libc/riscv64/Linux/fcntl.h \
	-f ../M2libc/stdlib.c \
	-f ../M2libc/stdio.c \
	-f ../M2libc/bootstrappable.c \
	-f ../mescc-tools/stringify.c \
	-f ../mescc-tools/M1-macro.c \
	--debug \
	-o M1-macro.M1

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

./M1-0 --architecture riscv64 \
	--little-endian \
	-f ../M2libc/riscv64/riscv64_defs.M1 \
	-f ../M2libc/riscv64/libc-full.M1 \
	-f M1-macro.M1 \
	-f M1-macro-footer.M1 \
	-o temp1

./hex2-1 --architecture riscv64 \
	--little-endian \
	--base-address 0x00600000 \
	-f ../M2libc/riscv64/ELF-riscv64-debug.hex2 \
	-f temp1 \
	-o ../bin/M1

######################################
# Phase-10 Build hex2 from C sources #
######################################
./M2 --architecture riscv64 \
	-f ../M2libc/sys/types.h \
	-f ../M2libc/riscv64/Linux/sys/stat.h \
	-f ../M2libc/stddef.h \
	-f ../M2libc/riscv64/Linux/unistd.h \
	-f ../M2libc/riscv64/Linux/fcntl.h \
	-f ../M2libc/stdlib.c \
	-f ../M2libc/stdio.c \
	-f ../M2libc/bootstrappable.c \
	-f ../mescc-tools/hex2.h \
	-f ../mescc-tools/hex2_linker.c \
	-f ../mescc-tools/hex2_word.c \
	-f ../mescc-tools/hex2.c \
	--debug \
	-o hex2_linker.M1

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

../bin/M1 --architecture riscv64 \
	--little-endian \
	-f ../M2libc/riscv64/riscv64_defs.M1 \
	-f ../M2libc/riscv64/libc-full.M1 \
	-f hex2_linker.M1 \
	-f hex2_linker-footer.M1 \
	-o hold

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


#####################################
# Phase-11 Build kaem from C sources#
#####################################
./M2 --architecture riscv64 \
	-f ../M2libc/sys/types.h \
	-f ../M2libc/stddef.h \
	-f ../M2libc/string.c \
	-f ../M2libc/riscv64/Linux/unistd.h \
	-f ../M2libc/riscv64/Linux/fcntl.h \
	-f ../M2libc/stdlib.c \
	-f ../M2libc/stdio.c \
	-f ../M2libc/bootstrappable.c \
	-f ../mescc-tools/Kaem/kaem.h \
	-f ../mescc-tools/Kaem/variable.c \
	-f ../mescc-tools/Kaem/kaem_globals.c \
	-f ../mescc-tools/Kaem/kaem.c \
	--debug \
	-o kaem.M1

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

../bin/M1 --architecture riscv64 \
	--little-endian \
	-f ../M2libc/riscv64/riscv64_defs.M1 \
	-f ../M2libc/riscv64/libc-full.M1 \
	-f kaem.M1 \
	-f kaem-footer.M1 \
	-o hold

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

../bin/kaem --verbose --strict --file mescc-tools-full-kaem.kaem
