#! /usr/bin/env bash
# Mes --- Maxwell Equations of Software
# Copyright © 2017,2019 Jan Nieuwenhuizen <janneke@gnu.org>
# Copyright © 2017,2019 Jeremiah Orians
#
# This file is part of Mes.
#
# Mes 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.
#
# Mes 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 Mes.  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
# Warning all binaries prior to the use of blood-elf will not be readable by
# Objdump, you may need to use ndism or gdb to view the assembly in the binary.


#################################
# Phase-1 Build hex1 from hex0  #
#################################
./hex0 hex1_x86.hex0 hex1
# hex1 adds support for single character labels and is available in various forms
# in mescc-tools/x86_bootstrap to allow you various ways to verify correctness

#################################
# Phase-1b Build catm from hex0 #
#################################
./hex0 catm_x86.hex0 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_x86.hex1 hex2-0
# hex2 adds support for long labels and absolute addresses thus allowing it
# to function as an effective linker for later stages of the bootstrap
# This is a minimal version which will be used to bootstrap a much more advanced
# version in a later stage.

###############################
# Phase-3 Build M0 from hex2  #
###############################
./catm hold ELF-i386.hex2 M0_x86.hex2
./hex2-0 hold 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_x86 from M0    #
###################################
./M0 cc_x86.M1 temp1
./catm hold ELF-i386.hex2 temp1
./hex2-0 hold cc_x86


#########################################
# Phase-5 Build M2-Planet from cc_x86   #
#########################################
./catm hold \
	../M2libc/x86/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_x86 hold M2.M1
./catm hold x86_defs.M1 libc-core.M1 M2.M1
./M0 hold temp1
./catm hold ELF-i386.hex2 temp1
./hex2-0 hold M2

#############################################
# Phase-6 Build blood-elf-0 from C sources  #
#############################################
./M2 --architecture x86 \
	-f ../M2libc/x86/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/x86/x86_defs.M1  ../M2libc/x86/libc-core.M1 blood-elf.M1
./M0 hold temp1
./catm hold ../M2libc/x86/ELF-x86.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 x86 \
	-f ../M2libc/x86/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 -f M1-macro.M1 --little-endian -o M1-macro-footer.M1
./catm hold ../M2libc/x86/x86_defs.M1  ../M2libc/x86/libc-core.M1 M1-macro.M1 M1-macro-footer.M1
./M0 hold temp1
./catm hold ../M2libc/x86/ELF-x86-debug.hex2 temp1
./hex2-0 hold M1-0

# This is the last stage where catm will need to be used and 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 x86 \
	-f ../M2libc/sys/types.h \
	-f ../M2libc/x86/Linux/sys/stat.h \
	-f ../M2libc/stddef.h \
	-f ../M2libc/x86/Linux/unistd.h \
	-f ../M2libc/x86/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 -f hex2_linker.M1 --little-endian -o hex2_linker-footer.M1

./M1-0 --architecture x86 \
	--little-endian \
	-f ../M2libc/x86/x86_defs.M1 \
	-f ../M2libc/x86/libc-full.M1 \
	-f hex2_linker.M1 \
	-f hex2_linker-footer.M1 \
	-o temp1

./catm hold ../M2libc/x86/ELF-x86-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 x86 \
	-f ../M2libc/sys/types.h \
	-f ../M2libc/stddef.h \
	-f ../M2libc/string.c \
	-f ../M2libc/x86/Linux/unistd.h \
	-f ../M2libc/x86/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 -f M1-macro.M1 --little-endian -o M1-macro-footer.M1

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

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

######################################
# Phase-10 Build hex2 from C sources #
######################################
./M2 --architecture x86 \
	-f ../M2libc/sys/types.h \
	-f ../M2libc/x86/Linux/sys/stat.h \
	-f ../M2libc/stddef.h \
	-f ../M2libc/x86/Linux/unistd.h \
	-f ../M2libc/x86/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 -f hex2_linker.M1 --little-endian -o hex2_linker-footer.M1

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

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


#####################################
# Phase-11 Build kaem from C sources#
#####################################
./M2 --architecture x86 \
	-f ../M2libc/sys/types.h \
	-f ../M2libc/stddef.h \
	-f ../M2libc/string.c \
	-f ../M2libc/x86/Linux/unistd.h \
	-f ../M2libc/x86/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 -f kaem.M1 --little-endian -o kaem-footer.M1

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

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