# Makefile

# Gujin is a bootloader, it loads a Linux kernel from cold boot or DOS.
# Copyright (C) 1999-2013 Etienne Lorrain, fingerprint (2D3AF3EA):
#   2471 DF64 9DEE 41D4 C8DB 9667 E448 FF8C 2D3A F3EA
# E-Mail: etienne@gujin.org
# This work is registered with the UK Copyright Service: Registration No:299755
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License,
# version 2, as published by the Free Software Foundation.
#
# This program 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 this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

###################################
# This is not really a Makefile configuration variable, just the
# default parameter for "./gujin", it is easier to put it here.
# Normally, you should not give here the "ide[0-9]=0x..." fields
# that you may give usually to other bootloaders, it is automatically
# added at startup by Gujin.
# --cmdline="" max size is 63 bytes.
# Just start without definning it, and come back here if needed or
# directly type the complete command line (./gujin --xxx).
#
#INSTBOOT_DEFAULT_PARAMETER=--cmdline="hdg=ide-scsi" --default_ide_password=abc
#INSTBOOT_DEFAULT_PARAMETER=--cmdline="ide0=0xb400,0xb802 ide1=0x170,0x376 root=/dev/hdc2"
INSTBOOT_DEFAULT_PARAMETER=-w=s

###################################
#### DO NOT FORGET to do 'make clean', if you modify any of the
####  following configuration values !
####
#### GCC and BINUTILS need to be built before everything - if you want
#### or need to use other version than those installed on your computer,
#### type 'make toolchain' first having all tar.gz in the parent directory.
#### make proper will not clean the toolchain.

###################################
# This is the general setup, you describe here in this "make
# variable" what you want to do by ORing the different constants:
#
#  - The kind of assembly primitives you want to acces memory over 64Kb,
#  mostly video memory, select by ORing with one (and only one) of those:
#  ASSEMBLY_NONE, ASSEMBLY_CISC, ASSEMBLY_RISC, ASSEMBLY_DSES
#  This is mostly an optimisation matter, it does not fundamentaly
#  changes the features of the bootloader.
#  Here is the description of each of them:
#    ASSEMBLY_NONE :
#       use C equivalent as much as possible, hopes that the
#       optimiser is perfect... The inline functions gpeek*()
#       and gpoke*() using segment %gs are used everywhere.
#    ASSEMBLY_CISC :
#       flavour complex addressing mode to reduce the number
#       of instructions, use segment %gs.
#    ASSEMBLY_RISC :
#       flavour simple addressing mode even if there is more
#       instructions than the strict minimum, use segment %gs.
#    ASSEMBLY_DSES :
#       This is the only choice where segments "%gs" and "%fs"
#       are not used - and segment doors for all segments are
#       open very early in the boot process - not only when a
#       VESA2 video mode is selected. It requests a little bit
#       more instructions (to manage offsets) but can be quicker.
#  NOTE: the first three options do not need to open the
#    segment doors for VESA1 modes (modes with a window in
#    the first 1024 Kb) - do not forget that the 80x86 is
#    in real mode here and cannot access more than 1 Mbyte
#    without openning the segment doors!
#    If a DOS bootloader is produced, you cannot open segment
#    doors when the processor is in virtual mode, i.e. after
#    loading Winblows or in DOSEMU.
#
#  - The position of the pool of memory managed by malloc functions,
#  it can be either in between the last data of the software and
#  the bottom of the stack, of after the stack. In the latter
#  case, the pool size can be bigger (up to 640 Kbytes), but the
#  software needs then to have segment doors open, and as said
#  before will not run in virtual mode. The default is to keep the
#  malloc pool below the stack (approx 45 Kbytes can be malloced),
#  else you OR the value with BIG_MALLOC.
#
#  - If you want to support all the language possible, you OR
#  that variable with MULTILINGUAL, else only English is supported,
#  having only a single language is better for DEBUG because of the
#  reduced available size to store strings.
#
#  -If you want more than 2 or 3 languages, the "const" memory area
#  becomes too small (mostly for non tiny version, which have a lot more
#  messages), so you can move all those strings into a new segment
#  by ORing the value with XSTRING_SEGMENT.
#
#  - If you want to support more than 256 char fonts, you OR
#  that variable with UNICODE_FONT, should not be enabled with DEBUG.
#
#  - By default the total size (code+const+variable) is 64 Kb
#  because everything is loaded in only one segment, i.e.
#  registers cs = ds = es. It is easy to change cs segment,
#  that is the segment of the code, to point to some other
#  place - and this adds just few bytes of code, so you can
#  OR the value with CODE_SEGMENT safely.
#  For the DOS bootloader "gujin.com", it does not change its
#  maximal size of 64 Kb because it is loaded by DOS, but the
#  bootloader "gujin.exe" and the BIOS bootloader (floppy or
#  HD sector 0) is then limited to 64Kb of code _plus_ 64Kb
#  of data (including stack and malloc pool) with this define.
#
#  - If there is large arrays accessed only in few cases (i.e. only
#  with farptr), like character fonts, they can be placed in an extra
#  data segment located in between code and real data, just before
#  the real data. Its maximum size is one ix86 segment, i.e. 64Kb.
#  This option is only available if CODE_SEGMENT is defined, and
#  is named XDATA_SEGMENT.
#  I am considering this segment read-only for now, i.e. for const
#  in the "C" meaning. Maybe one day I will try to compress this
#  segment (and decompress it by the loader) so I am not including
#  the gunzip constants in it.
#
#  - To reduce the size of the code segment, one way is to move some
#  functions in an extra code segment. If you OR the previous define
#  with XCODE_SEGMENT, a new i8086 segment is used to store the
#  gzlib functions (gzlib.o), functions which use them (fs.c),
#  and the disk access (disk.c).
#  This does _not_ need to have segment doors opened, but you need
#  to have CODE_SEGMENT OR'ed (it is not interresting without).
#
#  - To be able to store some information (default video mode, VGA mode
#  valid if your video board is not VESA compliant...) in between boots,
#  a special sector part can be updated on the floppy disk (the first
#  sector of the file called boot-xxx.sys). This shall be disabled when
#  you do not want to write to _any_ disk (after making unsafe modification
#  to the source), then the software do not contains the code to write
#  to anything near a FD/HD/IDE. The part written (structure gujin_param)
#  is protected by checksum, so do not modify this with an hexadecimal
#  editor!
#
#  - To save few bytes of code, you can OR with BIOS_ONLY. Then, you
#  can no more use "instboot" to generate an executable "gujin.exe" or
#  "gujin.com".
#
#  - If a DOS bootloader is produced, HIMEM.SYS (and so EMM386.EXE) presence
#  is checked and this memory interface is then used. If such a memory driver
#  does not exist or a BIOS bootfloppy is done, there is two solutions to
#  manage memory over 1 Mb: use BIOS service INT15/ah=0x87 or open segment
#  doors and use long pointers. The first BIOS one is selected when
#  SETUP is OR'ed with USE_INT1587 - the bootloader is then completely
#  compatible (up to vmlinuz_start() function) but maybe a little bit
#  slower and nobody knows if this documented interrupt is working on every
#  PCs. Else (USE_INT1587 not OR'ed), the segment doors are open when
#  the vmlinux/kernel file is loaded.
#
#  - If you really want Gujin to be quite (try to not display anything),
#  and so save constant (string) space, like the "verbose" flags at compile
#  time, you can OR with QUITE
#
#  - If you do not want to retry a read operation in Gujin 1st stage
#  bootloader (while loading Gujin 2) but instead call INT 0x19 BIOS
#  service which say "try next bootable device" - for instance when
#  the CDROM is not bootable, you do not want to enter an infinite
#  loop but try the hard disk - then do an OR with BOOT_NEVER_RETRY
#  It is now the default because I have never seen a floppy not
#  correctly reading a sector at the first time (officially you have
#  to retry three times) - unless there is a real error and then the
#  same erroneous data is read and re-read in an infinite loop.
#  Same (and worse) for CDROM because the data (with checksum error)
#  is cached anyways in the drive...
#  Gujin-1.6: Do not define BOOT_NEVER_RETRY here but use instboot
#  "--read_retry" command line switch.
#
#
# Simply select your choice, and if the software is too big for what you
# selected, the linker will complain because it cannot fit everything in
# the "ram" memory area:

# Intermediate variable:
#SETUP_STD_APPLI=CODE_SEGMENT|XCODE_SEGMENT|XDATA_SEGMENT|ASSEMBLY_DSES
#SETUP_STD_APPLI=CODE_SEGMENT|XCODE_SEGMENT|XDATA_SEGMENT|ASSEMBLY_RISC
#SETUP_STD_APPLI=CODE_SEGMENT|XCODE_SEGMENT|XDATA_SEGMENT|ASSEMBLY_NONE
SETUP_STD_APPLI=CODE_SEGMENT|XCODE_SEGMENT|XDATA_SEGMENT|ASSEMBLY_CISC
#SETUP_STD_APPLI=CODE_SEGMENT|XCODE_SEGMENT|ASSEMBLY_CISC

ifndef SETUP	# default
SETUP=$(SETUP_STD_APPLI)|UPDATE_BOOTPARAM|USE_INT1587|MULTILINGUAL|XSTRING_SEGMENT|UNICODE_FONT
endif

###################################
# Define here the user interfaces supported by the bootloader:
# The list of option is: (bigger => slower to load from floppy)
#
# VGA_SUPPORT		support of the VGA modes
# VGA_EXTENDED		modes > 0x13 can be probed/used
# VGA_MEMORY		direct memory access, now only for text modes
# VGA_USEINT1013	use _VGA_writestring, reduces code size
# VGA_INT1013_BG	use background color for 256 color modes, do not work
#
# VESA_WINDOW		VESA 1 compatible (slows VESA2 execution)
# VESA_STRANGE_WINDOW	window without 2^x bytes granularity, slow
# VESA_WINFUNCTION	optimise by using function instead of interrupt
# VESA_HARDWINDOW	analyse first code to change window(s)
# VESA_2WINDOWS		use separate read & write window if present
#
# VESA_LINEAR		VESA 2 only (cannot handle VESA1 only boards
#				with modes having more than 64K/screen)
# VESA_EDID		interrogate screen with EDID
# VESA_PMINFO		Get the I/O ports used by the graphic card
# VESA_RECALC		Correct number of visible lines if EGA compatible
#
# VESA_4BPP_EGA 	4 Bits Per Pixel	(big code size)
# VESA_4BPP_TEXT	text modes, 16 colors
# VESA_1BPP		1 Bit Per Pixel (monochrome graphic)
# VESA_8BPP		8 Bits Per Pixel
# VESA_4BPP		4 Bits Per Pixel with only one plane (ununsual)
# VESA_16BPP		15 or 16 Bits Per Pixel
# VESA_24BPP		24 Bits Per Pixel
# VESA_32BPP		32 Bits Per Pixel
#
# SERIAL_VT100		only VT100 (restriction not implemented)
# SERIAL_VT420		VT420 mode (36 and 48 lines/screen)
#
# BIOS_MOUSE_SUPPORT	for BIOS mouses (mostly PS2): INT0x15/ah=0xC2.
# SERIAL_MOUSE_SUPPORT  standard serial mouse driver on one of the first
#			4 COM ports declared to BIOS
# JOYSTICK_SUPPORT	self explanatory, not tested on much hardware.
#
# I know there is too many parameter to configure this subsystem,
# so there is few pre-build choice at end.
#

# Intermediate variables:
# only fast modes:
VESAMODE_FEW = VESA_16BPP|VESA_32BPP

# without palette management:

# forget 1BPP : VESAMODE_STD = $(VESAMODE_FEW)|VESA_1BPP|VESA_24BPP
# forget 1BPP : 1 BPP & 1 plane does not work on my Vodoo3 (640x200) because CGA pixel memory
# forget 1BPP : organisation, do not know about 1 BPP & 4 planes (640x350 & 640x480).
VESAMODE_STD = $(VESAMODE_FEW)|VESA_1BPP|VESA_24BPP

VESAMODE_ALL = $(VESAMODE_STD)|VESA_4BPP_EGA|VESA_4BPP_TEXT|VESA_8BPP|VESA_4BPP

# without EGA & 24BPP & 1BPP:
VESAMODE_ALT = $(VESAMODE_FEW)|VESA_4BPP_TEXT|VESA_8BPP


VESATYPE_2   = VESA_LINEAR|VESA_EDID

VESATYPE_1   = VESA_WINDOW|VESA_WINFUNCTION

VESATYPE_FEW = $(VESATYPE_1)|$(VESATYPE_2)|VESA_RECALC

VESATYPE_STD = $(VESATYPE_FEW)|VESA_2WINDOWS|VESA_HARDWINDOW

VESATYPE_ALL = $(VESATYPE_STD)|VESA_PMINFO

VESATYPE_ALT = $(VESATYPE_STD)|VESA_STRANGE_WINDOW

VESATYPE_ALL1 = $(VESATYPE_1)|VESA_2WINDOWS|VESA_HARDWINDOW|VESA_EDID

VGA_STD  = VGA_SUPPORT|VGA_EXTENDED
VGA_HARD = VGA_SUPPORT|VGA_EXTENDED|VGA_MEMORY
VGA_ALT  = VGA_SUPPORT|VGA_EXTENDED|VGA_USEINT1013

SERIAL = SERIAL_VT100|SERIAL_VT420

MOUSE = BIOS_MOUSE_SUPPORT|SERIAL_MOUSE_SUPPORT|JOYSTICK_SUPPORT

# Full:
USER_FULL=$(VGA_STD)|$(VESATYPE_ALL)|$(VESAMODE_ALL)|$(SERIAL)|$(MOUSE)

# Full but with VGA_USEINT1013, no background color in VGA 256 colors:
#USER_FULL=$(VGA_ALT)|$(VESATYPE_ALL)|$(VESAMODE_ALL)|$(SERIAL)|$(MOUSE)

# Full but no serial:
USER_FULL_NOSERIAL=$(VGA_STD)|$(VESATYPE_ALL)|$(VESAMODE_ALL)|$(MOUSE)

# Full but only VESA1:
USER_FULLVESA1=$(VGA_STD)|$(VESATYPE_ALL1)|$(VESAMODE_ALL)|$(SERIAL)|$(MOUSE)

# The hardware VGA and only VESA2 one:
USER_HVGA_VESA2=$(VGA_HARD)|$(VESATYPE_2)|$(VESAMODE_ALL)|$(SERIAL)|$(MOUSE)

# The alternate VGA one:
USER_ALTVGA=$(VGA_ALT)|$(VESATYPE_FEW)|$(VESAMODE_ALL)|$(SERIAL)|$(MOUSE)

# Without VGA, not optimised support of two window, only few VESA modes:
USER_SMALL=$(VESATYPE_FEW)|$(VESAMODE_FEW)|$(SERIAL)

# The "no serial" one:
USER_SCREEN=$(VGA_STD)|$(VESATYPE_STD)|$(VESAMODE_ALL)|$(MOUSE)

# A short one, to leave space for DEBUG disk & FS:
USER_STD=$(VGA_STD)|$(VESATYPE_STD)|$(VESAMODE_FEW)|BIOS_MOUSE_SUPPORT
USER_STD_NOMOUSE=$(VGA_STD)|$(VESATYPE_STD)|$(VESAMODE_FEW)

# The only VGA one:
USER_VGA=$(VGA_STD)

# The only VESA one:
USER_VESA=$(VESATYPE_STD)|$(VESAMODE_ALL)|$(MOUSE)

# The VESA and serial, but no VGA one:
USER_VESA_SERIAL=$(VESATYPE_STD)|$(VESAMODE_ALL)|$(SERIAL)|$(MOUSE)

# The small VESA one:
USER_FEW=$(VESATYPE_FEW)|$(VESAMODE_ALT)

# None, keep the basic interface (default minimum conf for a bootfloppy):
USER_NONE=0

ifndef USER_SUPPORT	# default
USER_SUPPORT=$(USER_FULL)
#USER_SUPPORT=$(USER_FULL_NOSERIAL)
# Just if you do not have enought in .text (debug disk/fs/load)
#USER_SUPPORT=$(VGA_STD)
endif

###################################
# Define here the disk interfaces supported by the bootloader:
# If any of BIOS_SUPPORT, EBIOS_SUPPORT, IDE_SUPPORT, DOS_SUPPORT,
# CDBIOS_SUPPORT, ATAPI_SUPPORT are not in this list,
# the corresponding functions are not linked.
#
# BIOS_SUPPORT: INT 0x13, %ah=0x02,0x03 / 0x21,0x22 (size limited)
# EBIOS_SUPPORT: INT 0x13, %ah=0x42, 0x43 (linear i.e. LBA access)
# IDE_SUPPORT: Direct I/O access to IDE using ATA
# DOS_SUPPORT: INT 0x21 (for "gujin.exe" or "gujin.com")
# CDBIOS_SUPPORT: INT0x13, %ah=0x4A,0x4B,0x4C,0x4D bootable CDROM in "noemul" mode
# ATAPI_SUPPORT: Direct I/O access to IDE using ATAPI (ATA PACKET Interface)
#
# You can define DOS_SUPPORT even on a BIOS only bootloader, it will
# not be used but there will be only one "boot.bin" result file which
# can be installed (by ./gujin) everywhere, on a floppy or as gujin.exe.
# Same for IDE_SUPPORT: it will _not_ be used when running on DOS or
# windows, but can be defined.
#
# We also add there the filesystem/bootsector probing, i.e. what
# to display on the menu: you may want to remove E2FS and bootsectors
# if you are making a boot floppy - it will decrease the size a lot.
# If E2FS is not supported, do not complain that the root filesystem
# is not automagically detected! So just OR with some of:
# E2FS_PROBE, FAT12_PROBE, FAT16_PROBE, FAT32_PROBE, ISOFS_PROBE,
# BOOTSECT_PROBE.
# Note that selecting only one of FAT12_PROBE, FAT16_PROBE, FAT32_PROBE,
# is interresting because the C compiler can then optimise the code a lot.
#
# The last things are to save code space: if you want to exclude
# the BIOS floppy support (and probing) because it takes quite some
# code space to take care of 64 Kb boundary for DMA, request no more
# than 18 sectors for 1.68Mb floppy, no request crossing the "head",
# then OR with NO_FLOPPY_SUPPORT (EBIOS floppy support should still
# work if available on the PC).
# If you want to _only_ probe the BIOS bootdevice (in %dl before the
# bootloader is called) and forget about any other disk, for the
# TINY configuration, then OR with PROBE_ONLY_BIOSBOOTDEVICE.
# If you want to _not_ probe for partitions (on the BIOS bootdevice),
# i.e. the TINY config is on a standard floppy, not a LS120 or a ZIP
# floppy, OR with NO_PROBE_PARTITIONS.

DISK_FULL_SUPPORT=BIOS_SUPPORT|EBIOS_SUPPORT|IDE_SUPPORT|DOS_SUPPORT|CDBIOS_SUPPORT
DISK_BASE_PROBE=E2FS_PROBE|FAT12_PROBE|FAT16_PROBE|FAT32_PROBE
DISK_FULL_PROBE=$(DISK_BASE_PROBE)|BOOTSECT_PROBE

# For a full featured bootloader:
DISK_FULL=$(DISK_FULL_SUPPORT)|ATAPI_SUPPORT|$(DISK_FULL_PROBE)|ISOFS_PROBE
# For a full featured bootloader:
DISK_FULL_NOCDROM=$(DISK_FULL_SUPPORT)|$(DISK_FULL_PROBE)
# For a full featured bootloader running only on DOS (dbg*.exe):
DISK_FULL_DOS=$(DISK_FULL_SUPPORT)|ATAPI_SUPPORT|$(DISK_BASE_PROBE)|ISOFS_PROBE
# For DBG_DISK, we save space (keep E2FS_PROBE:see make.h) by removing FAT support,
# note that now we can no more completely run a kernel with DBG_DISK:
DISK_DBG=$(DISK_FULL_SUPPORT)|ATAPI_SUPPORT|E2FS_PROBE|ISOFS_PROBE|BOOTSECT_PROBE
# For a minimum floppy bootloader on floppy or USB-FLOPPY thumb drive, tiny_img:
DISK_MIN=BIOS_SUPPORT|FAT12_PROBE|FAT16_PROBE|PROBE_ONLY_BIOSBOOTDEVICE|NO_PROBE_PARTITIONS
# For a ZIP or LS120 or small HD bootloader:
DISK_USB=BIOS_SUPPORT|EBIOS_SUPPORT|FAT12_PROBE|FAT16_PROBE|FAT32_PROBE|PROBE_ONLY_BIOSBOOTDEVICE
# For a PC with single Linux distribution:
DISK_EXT4=BIOS_SUPPORT|EBIOS_SUPPORT|E2FS_PROBE|PROBE_ONLY_BIOSBOOTDEVICE
# For a CDROM with autoload linux and no menu
DISK_CDROM=EBIOS_SUPPORT|CDBIOS_SUPPORT|ISOFS_PROBE|PROBE_ONLY_BIOSBOOTDEVICE

ifndef DISK_SUPPORT	# default
DISK_SUPPORT=$(DISK_FULL)
#DISK_SUPPORT=$(DISK_FULL_NOCDROM)
endif

###################################
# If you want to see debugging messages of boot level 2
# using serial port, define this:
#	DEBUG = -DDEBUG=COM4	# for COM4 (fourth serial port)
# If you want to see those debugging messages on a local printer,
# define this:
#	DEBUG = -DDEBUG=LPT1	# for LPT1 (first printer for BIOS)
# If you want to see those on the screen, define this:
#	DEBUG = -DDEBUG=SCREEN	# ONLY if current video mode is
#				# supported by BIOS INT0x10/ah=0x0E
# If you want to store those in DOS file "dbg" (only for DOS bootloader
# gujin.com/gujin.exe), define this:
#	DEBUG = -DDEBUG=DOSFILE
# Possible values are ONE of:
#   COM1, COM2, COM3, COM4, LPT1, LPT2, LPT3, LPT4, SCREEN, DOSFILE
#
# Take care, DEBUG on SCREEN is using INT0x10/ah=0x0E, so will maybe
# not work for graphic and/or VESA modes - use text mode to see those
# debug messages.
#
# Take care, COM4 will exist only with 3 previous ports, due to BIOS
# leaving no gap, on a real PC (w/o DOSEMU) - same for LPT ports.
# Take also care, using the wrong COM/LPT port will nearly stop boot2
# execution because of the harware handshake/long timeout if the
# serial/parallel port exists but is not connected to anything.
#
# To select which messages you want to see (and so reduce the
# size of the bootloader file) you OR the previous define with:

# DEBUG_ADDR, DEBUG_STACK, DEBUG_DISK, DEBUG_VIDEO,
# DEBUG_SERIAL, DEBUG_FS, DEBUG_MOUSE, DEBUG_LOADER (includes A20 debugging).
#
# If only the ouput device is defined, there is still few
# "general" debug messages emitted (those in macro "DBG()").
#
# DEBUG_ADDR is used in "checkptr()" to test C pointers, and
# DEBUG_STACK is used in "bound_stack()" to check stack overflow,
# they can be standalone (without output device) because their error
# messages goes to the default output interface.
#
#  Uncomment maximum one of those lines:
#
#DEBUG=DEBUG_STACK	# still test stack overflow
#DEBUG=DEBUG_ADDR	# still test few pointers
#DEBUG=DEBUG_STACK|DEBUG_ADDR	# Two previous tests
#
#DEBUG=SCREEN	# Just "general" messages
#DEBUG=SCREEN|DEBUG_STACK
#DEBUG=SCREEN|DEBUG_SERIAL	# for SERIAL terminal I/O
#DEBUG=SCREEN|DEBUG_STACK|DEBUG_MOUSE
#DEBUG=SCREEN|DEBUG_STACK|DEBUG_FS|DEBUG_LOADER
#DEBUG=SCREEN|DEBUG_STACK|DEBUG_FS
#DEBUG=SCREEN|DEBUG_STACK|DEBUG_FS
#DEBUG=COM1|DEBUG_STACK|DEBUG_LOADER|DEBUG_FS
#DEBUG=COM1|DEBUG_STACK|DEBUG_LOADER
#DEBUG=COM1|DEBUG_STACK|DEBUG_FS
#DEBUG=COM1|DEBUG_STACK|DEBUG_VIDEO|DEBUG_MOUSE
#DEBUG=COM1|DEBUG_STACK|DEBUG_VIDEO
#DEBUG=COM1|DEBUG_STACK|DEBUG_MOUSE
#DEBUG=COM1|DEBUG_STACK|DEBUG_DISK
#DEBUG=COM1|DEBUG_STACK|DEBUG_LOADER
#DEBUG=LPT1|DEBUG_STACK|DEBUG_DISK
#DEBUG=LPT1|DEBUG_STACK|DEBUG_VIDEO
#DEBUG=DOSFILE|DEBUG_STACK|DEBUG_FS|DEBUG_LOADER
#DEBUG=DOSFILE|DEBUG_STACK|DEBUG_LOADER
#DEBUG=DOSFILE|DEBUG_STACK|DEBUG_DISK
#DEBUG=DOSFILE|DEBUG_STACK|DEBUG_ADDR|DEBUG_VIDEO|DEBUG_SERIAL|DEBUG_MOUSE
#DEBUG=DOSFILE|DEBUG_STACK|DEBUG_FS|DEBUG_DISK
#
# Those may generate too big an executable:
#DEBUG=COM1  |DEBUG_VIDEO |DEBUG_STACK|DEBUG_ADDR|\
	DEBUG_DISK|DEBUG_FS|DEBUG_MOUSE|DEBUG_LOADER
#DEBUG=SCREEN|DEBUG_SERIAL|DEBUG_STACK|DEBUG_ADDR|\
	DEBUG_DISK|DEBUG_FS|DEBUG_MOUSE|DEBUG_LOADER

ifndef DEBUG	# default
DEBUG=DEBUG_STACK
#DEBUG=COM1|DEBUG_DISK
endif

###################################
####   You should no more configure anything here,
####  but continue reading the few following lines...
####
#### Now, define few variables which describe the tools you
#### may or have to download. To upgrade, just download the
#### new version and save the *.tar.gz in the parent directory.
####

# You need at least binutils-2.9.5.0.33.tar.gz, get the latest
# available in ftp://ftp.gnu.org/gnu/binutils/binutils-2.22.tar.bz2
# and put this file in the parent directory - then type "make toolchain"
# in current directory - this Makefile will rebuild it.
# I am using binutils-2.22
# The name of the file has to be the name of the main directory created
# by "tar -x[zj]f", i.e. binutils-020505.tar.{gz,bz2} for binutils-020505/*

LIST_BINUTILS := $(wildcard ../binutils-*.tar.gz ../binutils-*.tar.bz2)
ifneq ($(strip $(LIST_BINUTILS)),)
LAST_BINUTILS := $(word $(words $(LIST_BINUTILS)), $(LIST_BINUTILS))
endif

# If you want to test with the latest GCC, download "gcc-core-*.tar.gz"
# Its new optimisation may reduce the size of Gujin; to locate it, use:
# http://gcc.gnu.org/mirrors.html or:
# http://www.mirrorservice.org/sites/ftp.gnu.org/gnu/gcc/
# If you want to try it, copy the file gcc-core-4.6.2.tar.bz2
# in the upper directory and type "make toolchain" in current directory.
# Note 1:
#   If you have a fast enough PC, consider replacing line "make" with
#   "make bootstrap-lean"
# I am using a BOOTSTRAPPED gcc-4.6.2/gmp-5.0.1/mpfr-3.0.1/mpc-0.9/binutils-2.22
# now - please report if there is a problem with other versions.
# GCC with version >= 4.3 needs the gmp and mpfr libraries, download
# files gmp-5.0.1.tar.bz2, mpc-0.9.tar.gz and mpfr-3.0.1.tar.xz in the same directory
# to build/install them locally before building gcc, if you need them
# (i.e. too old version installed on your distribution).
# You may need "flex" to recompile the compiler, "apt-get install flex".
# I had to "ls -s ../toolchain/lib/libgmp.so.10 /lib/libgmp.so.10" to
# get some configure script help detect what the object extension is...
# Same for libmpc.so.2 and libmpfr.so.4

LIST_GCC := $(wildcard ../gcc-*.tar.gz ../gcc-*.tar.bz2 ../gcc-*.tar.xz)
ifneq ($(strip $(LIST_GCC)),)
LAST_GCC := $(word $(words $(LIST_GCC)), $(LIST_GCC))
endif

LIST_GMP := $(wildcard ../gmp-*.tar.gz ../gmp-*.tar.bz2 ../gmp-*.tar.xz)
ifneq ($(strip $(LIST_GMP)),)
LAST_GMP := $(word $(words $(LIST_GMP)), $(LIST_GMP))
endif

LIST_MPFR := $(wildcard ../mpfr-*.tar.gz ../mpfr-*.tar.bz2 ../mpfr-*.tar.xz)
ifneq ($(strip $(LIST_MPFR)),)
LAST_MPFR := $(word $(words $(LIST_MPFR)), $(LIST_MPFR))
endif

LIST_MPC := $(wildcard ../mpc-*.tar.gz ../mpc-*.tar.bz2 ../mpc-*.tar.xz)
ifneq ($(strip $(LIST_MPC)),)
LAST_MPC := $(word $(words $(LIST_MPC)), $(LIST_MPC))
endif

# These are just for testing, if someone needs them, download from:
# http://www.cs.unipr.it/ppl/Download/ftp/releases/0.10.2/ppl-0.10.2.tar.bz2 
# ftp://gcc.gnu.org/pub/gcc/infrastructure/cloog-ppl-0.15.3.tar.gz 

LIST_PPL := $(wildcard ../ppl-*.tar.gz ../ppl-*.tar.bz2 ../ppl-*.tar.xz)
ifneq ($(strip $(LIST_PPL)),)
LAST_PPL := $(word $(words $(LIST_PPL)), $(LIST_PPL))
endif

LIST_CLOOG_PPL := $(wildcard ../cloog-ppl-*.tar.gz ../cloog-ppl-*.tar.bz2 ../cloog-ppl-*.tar.xz)
ifneq ($(strip $(LIST_CLOOG_PPL)),)
LAST_CLOOG_PPL := $(word $(words $(LIST_CLOOG_PPL)), $(LIST_CLOOG_PPL))
endif

# Just about objdump:
# dd if=/dev/hdb of=copy_hdb bs=512 count=1
# objdump -D -m i8086 -b binary copy_hdb | less
# objdump -D -m i8086 -b binary --start-address=0x3E copy_hdb | less
# Would it be better for objdump to accept its input from a device,
# i.e. 'objdump -D -m i8086 -b binary /dev/hdb | less', as you
# can do with 'hexdump /dev/hdb1 | less' ?

###################################
####
####  The following is 'less' configurable...
####  If I correctly done it, you can forget to read it,
####  but maybe for targets 'dosemu:' and 'floppyimage:'

#
# A good habit is to never have a comment at the end of the
# line of a Makefile, because that adds spacing to variable
# declaration, and for instance if the variable is a
# part of a path, that make life really complex removing spaces.
#

PWD := $(shell pwd)
# Parent directory, do not add comment at the end of this line:
#P_PWD := `echo "$(PWD)/.." | sed 's:/[^/]*/\.\.::g'`
P_PWD := $(shell echo `pwd`/.. | sed 's:/[^/]*/\.\.::g')
#P_PWD := $(PWD)/..

ifeq ($(shell [ -x $(P_PWD)/toolchain/bin/gcc ] || echo FALSE ),)
GCC = $(P_PWD)/toolchain/bin/gcc
ifdef LAST_MPC # GCC-4.5.0
export LD_LIBRARY_PATH=$(P_PWD)/toolchain/lib
endif
else
ifeq ($(shell [ -x $(firstword $(wildcard /usr/bin/gcc* )) ] || echo FALSE ),)
ifeq ($(shell [ -x /usr/bin/gcc ] || echo FALSE ),)
GCC = /usr/bin/gcc
else
GCC = $(firstword $(wildcard /usr/bin/gcc* ))
endif
else
GCC = gcc
endif
endif

ifeq ($(shell [ -x $(P_PWD)/toolchain/bin/ld ] || echo FALSE ),)
OBJDUMP = $(P_PWD)/toolchain/bin/objdump
OBJCOPY = $(P_PWD)/toolchain/bin/objcopy
AS = $(P_PWD)/toolchain/bin/as
LD = $(P_PWD)/toolchain/bin/ld
#AR = $(P_PWD)/toolchain/bin/ar
else
OBJDUMP = objdump
OBJCOPY = objcopy
endif

MV = mv -f
RM = rm -f
CP = cp -f
CDRECORD = wodim
GENISOIMAGE = genisoimage

# The HOST CC, i.e. maybe 64 bits if Linux64, for installer:
ifeq ($(shell [ -x /usr/bin/cc ] || echo FALSE ),)
CC = /usr/bin/cc
else
ifeq ($(shell [ -x /usr/bin/gcc ] || echo FALSE ),)
CC = /usr/bin/gcc
else
ifeq ($(shell [ -x $(firstword $(wildcard /usr/bin/gcc* )) ] || echo FALSE ),)
CC = $(firstword $(wildcard /usr/bin/gcc* ))
else
CC = gcc
endif
endif
endif

# If rebuilt compiler cannot compile 64 bits application (hello_64*)
# then call: make GCC64=/usr/bin/gcc install.tar.gz
ifeq ($(GCC64),)
GCC64 = $(GCC)
endif

TEST_CC = $(GCC) -c -x c /dev/null % -o null.o 2>&1 ; $(RM) null.o
TEST_LD = $(GCC) -c -x c /dev/null -o null.o ; $(LD) null.o --defsym _start=0 % -o null.exe 2>&1 ; $(RM) null.exe
TEST_HOSTLD = $(CC) -c -x c /dev/null -o null.o ; ld null.o --defsym _start=0 % -o null.exe 2>&1 ; $(RM) null.exe

SETUPFLAGS = $(strip -DDEBUG="($(DEBUG))" -DUSER_SUPPORT="($(USER_SUPPORT))" \
		-DDISK_SUPPORT="($(DISK_SUPPORT))" -DSETUP="($(SETUP))")

WARNFLAGS = -Wall -Wno-main -Wstrict-prototypes -Wundef -Wpointer-arith \
	-Wcast-align -Wwrite-strings -Wsign-compare -Wno-format -Wno-multichar
# echo "unsigned short grave_a = 'à'; // '\xa0';" > test.c && gcc -S test.c
# test.c:1:26: warning: multi-character character constant
# -finput-charset=ISO8859-1 : warning: large integer implicitly truncated to unsigned type
# -fno-short-wchar -fexec-charset=UTF-8 -finput-charset=UTF-8 -fwide-exec-charset=UTF-8

ifeq ($(shell $(subst %,-Wdisabled-optimization,$(TEST_CC))),)
WARNFLAGS += -Wdisabled-optimization
endif

#WARNFLAGS = -Wall -Wstrict-prototypes -Wno-format -Wno-main \
#	-Wundef -Wpointer-arith -Wsign-compare -Wwrite-strings \
#	-Winline -Wredundant-decls -Waggregate-return -Wconversion \
#	-Wcast-qual -Wbad-function-cast
# -Winline
# -Wshadow gives buggy messages -W

#ifeq ($(shell $(subst %,-falign-loops,$(TEST_CC))),)
ALIGNFLAGS = -falign-loops=1 -falign-jumps=1 \
		 -falign-functions=2 # 2 for --function-section
#else
#ALIGNFLAGS = -malign-loops=1 -malign-jumps=1 \
	     -malign-functions=2 # 2 for --function-section
#endif
#ifeq ($(shell $(subst %,-mpreferred-stack-boundary=2,$(TEST_CC))),)
ALIGNFLAGS += -mpreferred-stack-boundary=2
#endif
ALIGNFLAGS += -mno-align-double

ifeq ($(shell $(subst %,-mpreferred-stack-boundary=2,$(TEST_CC))),)
ALIGNFLAGS += -mpreferred-stack-boundary=2
endif

ifeq ($(shell $(subst %,-falign-label=1,$(TEST_CC))),)
ALIGNFLAGS += -falign-label=1
endif

# aliasing optimisation does not seem to save a byte of code...
ifeq ($(shell $(subst %,-fno-strict-aliasing,$(TEST_CC))),)
ALIASFLAGS = -fno-strict-aliasing
# -fargument-alias or -fargument-noalias or -fargument-noalias-global ??
endif

# Note: we keep CFLAGS, ASFLAGS and LDFLAGS unmodified, they may have
# been given on make command line, like (to get source in mouse.lis):
#      make mouse.o ASFLAGS=-a=l CFLAGS=-g
ifeq ($(shell [ -x /usr/bin/dpkg-buildflags ] || echo FALSE ),)
CFLAGS:=$(shell dpkg-buildflags --get CPPFLAGS) $(shell dpkg-buildflags --get CFLAGS)
#LDFLAGS:=$(shell dpkg-buildflags --get LDFLAGS)
endif
# So we use GCCFLAGS, CPPFLAGS, GASFLAGS and GLDFLAGS.
# I add now "-funsigned-char" because char are used as index of font arrays,
# in font.c, else I get: (FIXME: should no more be needed)
# font.c:1641: duplicate array index in initializer
# font.c:1641: (near initialization for `font8x8')
#
# use -gstabs to stop generating ".cfi_offset 3, -20" which errors the assembler with
# "boot.s:1102: Error: register save offset not a multiple of 8" on debian-64 (make debian)
# Also debian 64 assembler errors with -gstabs, use -g1
#GCCFLAGS = -funsigned-char -fomit-frame-pointer -mrtd -march=i386 -fno-builtin -fverbose-asm -g1
GCCFLAGS = -fomit-frame-pointer -mrtd -march=i386 -fno-builtin -fverbose-asm -g1
# -fno-force-addr -fno-function-cse -g1 -fverbose-asm -fschedule-insns -fno-tree-sra
# -fcprop-registers ?

# We assume GCC -finput-charset=UTF-8 which is GCC default

ifeq ($(shell $(subst %,-fno-stack-protector,$(TEST_CC))),)
GCCFLAGS += -fno-stack-protector
endif

ifeq ($(shell $(subst %,-fno-optimize-sibling-calls,$(TEST_CC))),)
GCCFLAGS += -fno-optimize-sibling-calls
endif

ifeq ($(shell $(subst %,-m32,$(TEST_CC))),)
GCCFLAGS += -m32
endif

# to no have calls to memset but "rep stos" assembly instruction on some GCC versions:
ifeq ($(shell $(subst %,-minline-all-stringops -mno-align-stringops,$(TEST_CC))),)
GCCFLAGS += -minline-all-stringops -mno-align-stringops # -Dinline_all_stringops
endif

#ifeq ($(shell $(subst %,-finline-limit=10000,$(TEST_CC))),)
## -finline-limit= because the inlined function should be in the same segment...
#GCCFLAGS += -finline-limit=10000
#endif

ifeq ($(shell $(subst %,-fno-function-cse,$(TEST_CC))),)
GCCFLAGS += -fno-function-cse
endif

#ifeq ($(shell $(subst %,-foptimize-register-move,$(TEST_CC))),)
#GCCFLAGS += -foptimize-register-move
#endif

#ifeq ($(shell $(subst %,-fbranch-count-reg,$(TEST_CC))),)
#GCCFLAGS += -fbranch-count-reg
#endif

# This is by default:
#ifeq ($(shell $(subst %,-mpush-args,$(TEST_CC))),)
#GCCFLAGS += -mpush-args
#endif

ifeq ($(shell $(subst %,-Os,$(TEST_CC))),)
GCCFLAGS += -Os
else
GCCFLAGS += -O2
endif

# Assembler options:
#GASFLAGS         = -acdls=$*.lis -32
GASFLAGS	 = -acdhls=$*.lis -32
# 32

# Link options:
GLDFLAGS	 = -nostdlib -Tboot.lnk -Map=boot.map \
		--sort-common --cref --warn-section-align -m elf_i386
# --oformat=elf32-i386

# Sections overlaps, I know...
ifeq ($(shell $(subst %,--no-check-sections,$(TEST_LD))),)
GLDFLAGS += --no-check-sections
endif

ifeq ($(shell $(subst %,-z noexecstack,$(TEST_LD))),)
GLDFLAGS += -z noexecstack
endif

ifeq ($(shell $(subst %,--hash-style=both,$(TEST_HOSTLD))),)
XLDFLAGS += -Wl,--hash-style=both
endif

ifeq ($(shell $(subst %,-z relro -z now,$(TEST_HOSTLD))),)
XLDFLAGS += -Wl,-z,relro,-z,now
endif

# The only changes when -ffunction-sections is used, i.e. always now:
#ifneq ($(findstring GC_SECTION, $(SETUP)),)
GCCFLAGS += -ffunction-sections
GLDFLAGS += --gc-sections
#endif

# -Wa,-a -fconserve-space -fuse-loop ??? -fschedule-insns ???
#  -Oss ?
# -mregparm=1 : increase size of executable: 47048 to 47432
# -fforce-addr : increase size of executable: 47048 to 47144
# -fno-force-mem : increase size of executable: 47048 to 47208
# -mreg-alloc="dacbSDB"	: increase size of executable by 416 bytes.
# -mreg-alloc="dcabSDB"	: increase size of executable by 320 bytes.
# -mreg-alloc="adcbSDB"	is the default
# -finline-functions -finline-limit-50	: increases size of executable.
# -mwide-multiply -freg-struct-return
# -fstack-check ?? -fcall-used-<reg> / -fcall-saved-<reg> ??
# -fno-data-sections -ffunction-sections and -Wl,--gc-sections ?
# -fno-keep-static-consts : only for C++ ?

# Preprocessor flags:
CPPFLAGS = -include code16.h $(SETUPFLAGS) $(EXTRA_CPPFLAGS)

# Keep boot.o as the _first_ file in this list, see boot.ld:
OBJS = boot.o user.o debug.o library.o disk.o util.o gzlib.o \
	kbd.o fs.o vmlinuz.o mouse.o main.o font.o

INCS = boot.h user.h debug.h library.h disk.h util.h gzlib.h \
	kbd.h fs.h vmlinuz.h mouse.h font.h \
	make.h gmem.h e2fs.h instboot.h messages.h \
	vesabios.h vgabios.h ide.h bios.h dos.h ega.h xms.h

PNGS = bochs.png cmd.png color16.png color4.png main.png minicom.png mode0.png \
	setup.png setup2.png setup-fr.png simple.png start.png ttys0.png

DOCS = gujin-scfrg.htm README.htm install.txt gpg_sign.txt gpl.txt \
	toolchain.txt gujin.8 changelog.wri

MISCFILE = Makefile boot.ld awk.cmd cleandisk.ld cleandisk_bin.ld gujin.spec gujin.cmd gujin.sh

# Note that we define $(OBJS) and deduce $(SRCS) from it because
# we do not have assembler files, only C files.
# All real sources, even the ones not compiled:
ALLSRCS := $(OBJS:o=c) instboot.c gzcopy.c fntbin2h.c fnthex2h.c addhole.c \
	showmap.c cleandisk.c showmbr.c gujin-cmdline.c hello_32bits.c hello_64bits.c hello_bios.c hello_gpl.c

ALLINCS := $(INCS)

ALLFILES = $(ALLSRCS) $(ALLINCS) $(PNGS) $(DOCS) $(MISCFILE)

.SUFFIXES:	.i .com
.PHONY:		dep clean proper all tar full install.tar.gz standard.tar.gz \
		save floppyimage dosemu toolchain $(wildcard /dev/fd*) \
		testdisk testuser testappli testdebug testall \
		dbgvideo dbgdisk dbgload dbgfs dbgdisk_screen dbgload_screen dbgfs_screen \
		dbgvideo_com1 dbgdisk_com1 dbgload_com1 dbgfs_com1

empty :=
tab   := $(empty)	$(empty)
space := $(empty) $(empty)
dot   := $(empty).$(empty)
comma := $(empty),$(empty)
cr    := $(empty)
$(empty)

# cancel shortcut to compile directly from .c to .o :
%.o: %.c
#boot.o: boot.c # now only for file "boot.c" : remove ALL ".align 32"

# always cancel .S -> .o rule (if it exists)
%.o: %.S

%.S: %.c
	$(GCC) $(CFLAGS) $(CPPFLAGS) $(GCCFLAGS) $(ALIASFLAGS) $(ALIGNFLAGS) $(WARNFLAGS) -S -o $*.S $<

%.s:%.S
ifneq (,$(findstring XCODE_SEGMENT,$(SETUP)))
# GCC-4.5.0 reorganize those "awk farptr" at the end of the file...
#	awk -f awk.cmd < $< > $*.s
	(grep "awk farptr" $< ; grep -v "awk farptr" $<) | awk -f awk.cmd > $*.s
else
	sed '/.align 32/d' < $< > $*.s
endif

%.o: %.s
	$(AS) $(ASFLAGS) $(GASFLAGS) -o $*.o $<

%.i: %.c
	$(GCC) $(CPPFLAGS) -E $< > $@

# For some special debug, keep the assembler files:
#.PRECIOUS: library.s disk.s debug.s malloc.s main.s vmlinuz.s fs.s user.s boot.s

# THE default target:
# "gujin" executable is approximately the same as instboot but contains
# the usual sources images as internal constants:
gujin build binary binary-ia32 binary-amd64: instboot.c $(ALLSRCS) $(ALLINCS)
	rm -rf gujin_images
	mkdir gujin_images
	@echo "# generating gujin_images/boot.bin, GCCFLAGS=$(GCCFLAGS)."
	make -s DISK_SUPPORT="$(DISK_FULL)" USER_SUPPORT="$(USER_FULL)" SETUP="$(SETUP_STD_APPLI)|UPDATE_BOOTPARAM|USE_INT1587|MULTILINGUAL|XSTRING_SEGMENT|UNICODE_FONT" INSTBOOT_DEFAULT_PARAMETER='--cmdline=""' clean dep boot.bin
	mv boot.bin gujin_images/boot.bin
	@echo "# generating gujin_images/tinystd.bin, no disk EBIOS, no partitions (FAT floppies)! GCCFLAGS=$(GCCFLAGS)"
	make -s DISK_SUPPORT="$(DISK_MIN)" USER_SUPPORT=0 SETUP="CODE_SEGMENT|ASSEMBLY_DSES|BIOS_ONLY|QUITE" DEBUG=0 clean dep boot.bin
	mv boot.bin gujin_images/tinystd.bin
	@echo "# generating gujin_images/tinyusb.bin, EBIOS and VFAT partitions, GCCFLAGS=$(GCCFLAGS)"
	make -s DISK_SUPPORT="$(DISK_USB)" USER_SUPPORT=0 SETUP="CODE_SEGMENT|ASSEMBLY_DSES|BIOS_ONLY|QUITE" DEBUG=0 clean dep boot.bin
	mv boot.bin gujin_images/tinyusb.bin
	@echo "# generating gujin_images/tinyext4.bin, EBIOS and Ext4FS partitions, GCCFLAGS=$(GCCFLAGS)"
	make -s DISK_SUPPORT="$(DISK_EXT4)" USER_SUPPORT=0 SETUP="CODE_SEGMENT|ASSEMBLY_CISC|BIOS_ONLY|QUITE|USE_INT1587" DEBUG=0 clean dep boot.bin
	mv boot.bin gujin_images/tinyext4.bin
	@echo "# generating gujin_images/tinycdrom.bin, GCCFLAGS=$(GCCFLAGS)"
	make -s DISK_SUPPORT="$(DISK_CDROM)" USER_SUPPORT=0 SETUP="CODE_SEGMENT|ASSEMBLY_DSES|BIOS_ONLY|QUITE" DEBUG=0 clean dep boot.bin
	mv boot.bin gujin_images/tinycdrom.bin
	@echo "# generating gujin_images/tinyexe.bin, GCCFLAGS=$(GCCFLAGS)"
	make -s DISK_SUPPORT=DOS_SUPPORT USER_SUPPORT=0 SETUP="CODE_SEGMENT|ASSEMBLY_CISC|USE_INT1587|QUITE|MULTILINGUAL" DEBUG=0 clean dep boot.bin
	mv boot.bin gujin_images/tinyexe.bin
	$(CC) $(CFLAGS) -DWITH_INPUT_FILES -Wall -fno-strict-aliasing -O2 $(XLDFLAGS) -g instboot.c -o gujin
	rm -rf gujin_images
	@echo '# "yum install gcc rpmdevtools rpmlint" to produce package on fedora'
	@echo '# "apt-get install gcc devscripts debhelper genisoimage" on Debian/Ubuntu'
	@echo '# Type "make debian", make "ubuntu" or "make fedora" to produce package (i386/amd64)'

install:
	gzip -c gujin.8 > gujin.8.gz
	install -Dps ./gujin $(DESTDIR)/sbin/gujin
	install -m644 -Dp gujin.8.gz $(DESTDIR)/usr/share/man/man8/gujin.8.gz
	rm -f gujin.8.gz

install-disk:
	$(DESTDIR)/sbin/gujin /boot/gujin.ebios

uninstall:
	rm -f /sbin/gujin $(DESTDIR)/usr/share/man/man8/gujin.8.gz

purge: uninstall
	rm -f /boot/gujin.cmd
	
uninstall-disk:
	$(DESTDIR)/sbin/gujin --remove=/boot/gujin.ebios

toolpath:
	echo GCC=$(GCC) , building the binary images
	echo CC=$(CC) , building the installer
	echo GCC64=$(GCC64) , building the installer
	echo AS=$(AS)
	echo LD=$(LD)
	echo OBJCOPY=$(OBJCOPY)

#release
#cd /home/groups/g/gu/gujin/htdocs/ && cat adoc.txt
#scp *.png etienne_lorrain@shell.sourceforge.net:/home/groups/g/gu/gujin/htdocs
##scp gujin-scfrg.htm etienne_lorrain,gujin@web.sourceforge.net:/home/groups/g/gu/gujin/htdocs/index.php

.PHONY: debian
debian ubuntu:
	debuild -us -uc
	rm -rf ./pkg
	mkdir ./pkg
	mv -f ../gujin_2* ./pkg

fedora:
	rpmdev-setuptree
	make proper
	tar czf ~/rpmbuild/SOURCES/gujin-2.8.7.tar.gz ../gujin	# VERSION
	rpmbuild -ba --clean gujin.spec
	rpmlint gujin.spec ~/rpmbuild/RPMS/*/gujin-2*.rpm ~/rpmbuild/SRPMS/gujin-2*.src.rpm
	rm -f ~/rpmbuild/RPMS/*/gujin-debuginfo-2*.rpm
	rm -rf ./pkg
	mkdir ./pkg
	mv -v ~/rpmbuild/RPMS/*/gujin-2*.rpm ./pkg
	mv -v ~/rpmbuild/SRPMS/gujin-2*.src.rpm ./pkg

# dependancies:
dep:	code16.h
	echo '##### Dependencies' >> Makefile # if it is not there...
	sed '/^[# ]*Dependencies/q' < Makefile > Makefile.tmp
	# Take care, no dot, i.e. ".o", for instboot/gzcopy dependency:
	$(CPP) $(CPPFLAGS) -MM $(OBJS:o=c) | sed 's/\.o/.[oisS]/' \
						>> Makefile.tmp
	$(CPP) -MM instboot.c | sed 's/\.o//'	>> Makefile.tmp
	$(CPP) -MM gzcopy.c | sed 's/\.o//'	>> Makefile.tmp
	$(CPP) -MM fntbin2h.c | sed 's/\.o//'	>> Makefile.tmp
	$(CPP) -MM fnthex2h.c | sed 's/\.o//'	>> Makefile.tmp
	$(MV) Makefile.tmp Makefile
	@echo
	@echo GCC: `$(GCC) --version | head -1`, Binutils: `$(LD) --version | head -1`
	@echo : dependencies rebuilt, depending on Makefile setup.

extradoc:
	@echo ""
	@echo "Now you can type:"
	@echo "	* to build and install on a floppy:"
	@echo "  make /dev/fd0     (or make /dev/fd1 or make /dev/fd0H1440)"
	@echo "	   NOTE THAT:"
	@echo "	   - ALL FILES WILL BE ERASED ON THIS FLOPPY"
	@echo "	   - the format of the floppy (360K, 1.2M, 720K, 1.44M ...)"
	@echo "	   is automagically recognised, it could work for 1.68M floppy."
	@echo "	* to build the DOS bootloader:"
	@echo "  make gujin.exe    (make gujin.com possible if size < 64K)"
	@echo "	* to install a \"serial interface\" gujin bootloader"
	@echo "  make /dev/fd0.com1      (in fact make /dev/fd[01].com[1-4])"
	@echo "	 or try: ./instboot --serial=COM1,9600,n,8,1 boot.bin /dev/fd0"
	@echo "	* make gzcopy or testgzlib or testgzcopy to build/test the GZIP utilities"
	@echo "	* make standard.tar.gz install.tar.gz to rebuild the distribution files"
	@echo "	* make dbgdisk_screen or dbgvideo_com1 for debug on screen/serial line"
	@echo "	* See in file Makefile for all other targets, like:"
	@echo " CDnoemul, dosemu , floppyimage ; clean , proper , tar , boot.asm"
	@echo " and type \"make instboot ; ./instboot --help\" to install on HD"
	@echo

# use the standard cc here, without GCCFLAGS.
# instboot do not depend on the compilations switches - it is the same
# executable for all configurations.
# It is easy to forget to do "make dep" before those two, so
# just add explicit dependancy.
instboot: instboot.c
	$(CC) $(CFLAGS) -Wall -fno-strict-aliasing -O2 $(XLDFLAGS) -g instboot.c -o instboot

addhole: addhole.c
	$(CC) $(CFLAGS) -Wall -fno-strict-aliasing -O2 $(XLDFLAGS) -s addhole.c -o addhole

showmap: showmap.c
	$(CC) $(CFLAGS) -Wall -fno-strict-aliasing -O2 $(XLDFLAGS) -s showmap.c -o showmap

showmbr: showmbr.c
	$(CC) $(CFLAGS) -Wall -fno-strict-aliasing -O2 $(XLDFLAGS) -s showmbr.c -o showmbr

gujin-cmdline: gujin-cmdline.c
	$(CC) $(CFLAGS) -Wall -fno-strict-aliasing -O2 $(XLDFLAGS) -s gujin-cmdline.c -o gujin-cmdline

#
# Maybe the name field of a kernel.img.gz should be something like:
# ./gzcopy -n=`mount | grep '/[^a-zA-Z]' | \
#	awk '{print $1}'`/boot/kernel-2.4.0.img.gz \
#	kernel.img.gz /boot/kernel-2.4.0.img.gz
# i.e. ./gzcopy -n kernel-2.4.0.img.gz
# "kernel-2.4.0.img.gz" original name: "/dev/hdc5//boot/kernel-2.4.0.img.gz"
#
gzcopy: gzcopy.c
	$(CC) $(CFLAGS) -Wall -O2 $(XLDFLAGS) -s gzcopy.c -o gzcopy

testgzlib: gzcopy
	find /mnt/cdrom/ -name "*.tgz" -o -name "*.gz" \
		-exec ./gzcopy -t {} \; 2>&1 | tee log

testgzlib2: gzcopy	# double $$ in Makefiles:
	for name in ../gz/* ; do ./gzcopy -t $$name ; done

testgzcopy-comment:
	./gzcopy gzcp.cgz gzcp-name1.cgz -a="that is a comment"
	./gzcopy gzcp.cgz gzcp-name0.cgz -c=
	diff gzcp.cgz gzcp-name0.cgz
	./gzcopy gzcp.cgz gzcp-name2.cgz -c= -a="that " -a="is a" -a=" comment"
	./gzcopy gzcp.cgz gzcp-name3.cgz -c= -as="that" -as="is a " -as="comment"
	./gzcopy gzcp.cgz gzcp-name4.cgz -p=" comment" -p="is a" -p="that "
	./gzcopy gzcp.cgz gzcp-name5.cgz -ps=" comment" -ps="is a" -ps="that"
	./gzcopy gzcp.cgz gzcp-name6.cgz -a="" -a="that is a comment" -p=""
	./gzcopy gzcp.cgz gzcp-name7.cgz -p="" -p="that is a comment" -a=""
	./gzcopy gzcp.cgz gzcp-name8.cgz -as="" -as="that is a comment" -ps=""
	./gzcopy gzcp.cgz gzcp-name9.cgz -ps="" -ps="that is a comment" -as=""
	@echo `./gzcopy -c gzcp-name1.cgz` == \"`./gzcopy -c0 gzcp-name1.cgz`\"
	@echo `./gzcopy -c gzcp-name2.cgz` == \"`./gzcopy -c0 gzcp-name2.cgz`\"
	@echo `./gzcopy -c gzcp-name3.cgz` == \"`./gzcopy -c0 gzcp-name3.cgz`\"
	@echo `./gzcopy -c gzcp-name4.cgz` == \"`./gzcopy -c0 gzcp-name4.cgz`\"
	@echo `./gzcopy -c gzcp-name5.cgz` == \"`./gzcopy -c0 gzcp-name5.cgz`\"
	@echo `./gzcopy -c gzcp-name6.cgz` == \"`./gzcopy -c0 gzcp-name6.cgz`\"
	@echo `./gzcopy -c gzcp-name7.cgz` == \"`./gzcopy -c0 gzcp-name7.cgz`\"
	@echo `./gzcopy -c gzcp-name8.cgz` == \"`./gzcopy -c0 gzcp-name8.cgz`\"
	@echo `./gzcopy -c gzcp-name9.cgz` == \"`./gzcopy -c0 gzcp-name9.cgz`\"
	diff gzcp-name1.cgz gzcp-name2.cgz
	diff gzcp-name1.cgz gzcp-name3.cgz
	diff gzcp-name1.cgz gzcp-name4.cgz
	diff gzcp-name1.cgz gzcp-name5.cgz
	diff gzcp-name1.cgz gzcp-name6.cgz
	diff gzcp-name1.cgz gzcp-name7.cgz
	diff gzcp-name1.cgz gzcp-name8.cgz
	diff gzcp-name1.cgz gzcp-name9.cgz

testgzcopy-name:
	./gzcopy -n gzcp.cgz
	./gzcopy gzcp.cgz gzcpa.cgz -n=
	./gzcopy -n gzcpa.cgz
	./gzcopy -n=`./gzcopy -n0 gzcp.cgz` gzcpa.cgz gzcpb.cgz
	diff gzcp.cgz gzcpb.cgz
	./gzcopy -n=`./gzcopy -n0 gzcp.cgz` -f gzcpb.cgz
	diff gzcp.cgz gzcpb.cgz

testgzcopy-textflag:
	./gzcopy -b gzcp.cgz
	./gzcopy gzcp.cgz gzcp0.cgz -b=1
	./gzcopy -b gzcp0.cgz
	./gzcopy -b=0 gzcp0.cgz gzcp1.cgz
	diff gzcp.cgz gzcp1.cgz

testgzcopy: gzcopy
	cp gzcopy.c gzcp.c
	gzip -9 -f gzcp.c
	./gzcopy gzcp.c.gz gzcp.cgz
	diff gzcp.c.gz gzcp.cgz
	make testgzcopy-name
	make testgzcopy-comment
	make testgzcopy-textflag
	gzip -t gzcp-name[0-9].cgz
	$(RM) gzcp-name[0-9].cgz
	gzip -t gzcp[ab].cgz
	$(RM) gzcp[ab].cgz
	gzip -t gzcp[01].cgz
	$(RM) gzcp[01].cgz
	./gzcopy -k=1 -f gzcp.c.gz gzcp.cgz
	make testgzcopy-name
	make testgzcopy-comment
	make testgzcopy-textflag
	$(RM) gzcp-name[0-9].cgz
	$(RM) gzcp[ab].cgz
	$(RM) gzcp[01].cgz
	$(RM) gzcp.cgz gzcp.c.gz

fntbin2h: fntbin2h.c
	$(CC) $(CFLAGS) -Wall -O2 $(XLDFLAGS) -s -funsigned-char fntbin2h.c -o fntbin2h

fnthex2h: fnthex2h.c
	$(CC) $(CFLAGS) -Wall -O2 $(XLDFLAGS) -s -funsigned-char fnthex2h.c -o fnthex2h

# Produces the standard include to have 16 bits code & page size of *.lst:
code16.h:
	/bin/echo -e "asm(\"	.code16gcc \");" > code16.h
	/bin/echo -e "asm(\"	.psize 0 \");" >> code16.h

# Produces the linker command file for this config:
boot.lnk: boot.ld Makefile
ifneq ($(shell $(LD) --version | grep "GNU gold"),)
	echo "ERROR: gold do not recognise MEMORY, NOCROSSREFS and overlapping sections, cannot use!"
	exit 1
endif
	$(GCC) $(SETUPFLAGS) -P -E -x c boot.ld \
		| sed "/^[$(space)$(tab)]*$$/d" > boot.lnk

# Intermediate ELF file, needed for "ld --gc-sections":
boot.elf: $(OBJS) boot.lnk
	$(LD) $(OBJS) $(LIBS) $(subst -Wl$(comma),,$(LDFLAGS)) $(GLDFLAGS) -o boot.elf

# Intermediate binary file, whatever the FD/HD format:
boot.bin: boot.elf
	$(OBJCOPY) --output-target=binary boot.elf boot.bin
	@echo "-------------------------"
	grep "__sizeof_" boot.map | sed 's/        / /g;s/0x0000/0x/;s/=.*//'
	@echo "-------------------------"

# To have the text file boot.asm containning everything:
#boot.asm: boot.elf
#	$(OBJDUMP) --headers --disassemble-all -m i8086 boot.elf > boot.asm
boot.asm:
	make -s DISK_SUPPORT="$(DISK_FULL)" USER_SUPPORT="$(USER_FULL)" SETUP="$(SETUP_STD_APPLI)|UPDATE_BOOTPARAM|USE_INT1587|MULTILINGUAL|XSTRING_SEGMENT|UNICODE_FONT" INSTBOOT_DEFAULT_PARAMETER='--cmdline=""' clean dep boot.elf
	$(OBJDUMP) --headers --disassemble-all -m i8086 boot.elf > boot.asm

# only warnings, the resulting code size is too big:
warn:
	make -k clean dep $(OBJS) DEBUG="DOSFILE|DEBUG_ADDR|DEBUG_STACK|DEBUG_DISK|DEBUG_VIDEO|DEBUG_SERIAL|DEBUG_FS|DEBUG_MOUSE|DEBUG_LOADER" > log

floppy.144.gz: gujin
	./gujin --full floppy.144 $(INSTBOOT_DEFAULT_PARAMETER)
	gzip -9 -f floppy.144

floppy.720.gz: gujin
	./gujin --full floppy.720 $(INSTBOOT_DEFAULT_PARAMETER)
	gzip -9 -f floppy.720

# Standard usual targets:
/dev/fd[0-9dhDH]*: gujin
	./gujin --full $@ $(INSTBOOT_DEFAULT_PARAMETER)

/dev/fd0.com1 /dev/fd0.com2 /dev/fd0.com3 /dev/fd0.com4 \
/dev/fd1.com1 /dev/fd1.com2 /dev/fd1.com3 /dev/fd1.com4: gujin
	./gujin --full $(basename $@) --serial=$(subst .,,$(suffix $@)),9600,n,8,1 $(INSTBOOT_DEFAULT_PARAMETER)

gujin.exe gujin.com gujin.pic gujin.bcd: gujin
	./gujin $@ $(INSTBOOT_DEFAULT_PARAMETER)

boot.144 boot.288 boot.168 boot.720 boot.120 boot.360: gujin
	./gujin --full $@ $(INSTBOOT_DEFAULT_PARAMETER)

# just for fun and to test mtools, 2048 bytes/sect, only floppyimage.
# 1.44 Mb floppy cannot be done with 18 / 4 = 4.5 sectors per track
boot2048: gujin
	./gujin boot2048 --full -d=BIOS:0x00 --geometry=1440,0,2,9,2048
	mdir -a -i boot2048 ::

#
# That is to build some standard target as a demonstration,
# I am not sure to build the one you really need:
# Note that the "*.img" files are 1.44 Mb floppy images, so
# they include the FAT and root directory: the executable size
# is not as big as the file size.
standard.tar.gz:
	make full INSTBOOT_DEFAULT_PARAMETER='--cmdline=""'
	make medium INSTBOOT_DEFAULT_PARAMETER='--cmdline=""'
	make small INSTBOOT_DEFAULT_PARAMETER='--cmdline=""'
	make tiny_img INSTBOOT_DEFAULT_PARAMETER='--cmdline=""'
	make mingujin.exe INSTBOOT_DEFAULT_PARAMETER='--cmdline=""'
	make lotiny_exe INSTBOOT_DEFAULT_PARAMETER='--cmdline=""'
	make lodtiny_exe INSTBOOT_DEFAULT_PARAMETER='--cmdline=""'
	make dbgvideo INSTBOOT_DEFAULT_PARAMETER='--cmdline=""'
	make dbgload INSTBOOT_DEFAULT_PARAMETER='--cmdline=""'
	make dbgfs INSTBOOT_DEFAULT_PARAMETER='--cmdline=""'
	make dbgdisk INSTBOOT_DEFAULT_PARAMETER='--cmdline=""'
	$(RM) standard.tar.gz
	tar -czf standard.tar.gz full.img.gz full.exe \
				medium.img.gz medium.exe \
				small.img.gz small.exe \
				tiny.144 mingujin.exe lotiny.exe lodtiny.exe \
				dbgvideo.exe dbgdisk.exe dbgload.exe dbgfs.exe
	$(RM) full.img.gz full.exe full.asm
	$(RM) medium.img.gz medium.exe medium.asm
	$(RM) small.img.gz small.exe small.asm
	$(RM) tiny.144 mingujin.exe
	$(RM) dbgvideo.exe dbgdisk.exe dbgload.exe dbgfs.exe
	make clean	# just in case...

# full: the full one.
# medium: Removes the "mini-debug" and serial (VT420) support, and remove
#     the analysis of the I/O ports used by the video card.
# small: Revove the mouse support, the support of 4 BPP (16 color) graphic
#     and 24 bit VESA modes, the VESA_HARDWINDOW analysis which cannot
#     be used right now by Linux.
#     Remove also the VGA support, note that even some 486 refuse to boot
#     if the video card is not VESA 1.0+ compliant, so it should be quite
#     safe. This version defines "QUITE", so it is a lot less verbose
#     - nearly nothing is displayed.
# dbg*:  These are the "debug" versions, outputing information on a DOS file
#     to be useable by everybody. Just create a DOS bootable floppy
#     ("format /s a:" under DOS) and copy the executable to this floppy.
#     Then, you boot out of this floppy and execute the file. It will do
#     the same as the bootloader but also create a file named "a:\DBG" to
#     store a lot of information.
#     If you have a problem with Gujin, I need this DBG file to debug!
#     Note that you need to either exit Gujin (^C or ^D will do) or run
#     a kernel to flush and close the file properly: do not reboot.
#     There is different executable because of size restrictions, and
#     readability.
#   dbgvideo:  video subsystem
#   dbgdisk:   disk subsystem
#   dbgload:   parameters passed to Linux
#   dbgfs:     filesystems analysis (FAT*, E2FS, DOS)
#
# dbgdisk_screen: same as dbgdisk but debug info goes to the screen,
#   it is messy screen but can help to locate a BIOS problem.
#
# dbg*_com1: exactly the same as previous ones, but the debug information
#     is going to the serial line COM1, i.e. /dev/ttyS0, you can read it
#     (and capture it) using the "minicom" software.
#   dbgvideo_com1:  video subsystem
#   dbgdisk_com1:   disk subsystem
#   dbgload_com1:   parameters passed to Linux
#   dbgfs_com1:     filesystems analysis (FAT*, E2FS, DOS)


full:
	make DISK_SUPPORT="$(DISK_FULL)" USER_SUPPORT="$(USER_FULL)" \
		SETUP="$(SETUP_STD_APPLI)|UPDATE_BOOTPARAM|USE_INT1587|MULTILINGUAL|XSTRING_SEGMENT|UNICODE_FONT" \
		clean dep
	# pause to reread the file Makefile
	make  DISK_SUPPORT="$(DISK_FULL)" USER_SUPPORT="$(USER_FULL)" \
		SETUP="$(SETUP_STD_APPLI)|UPDATE_BOOTPARAM|USE_INT1587|MULTILINGUAL|XSTRING_SEGMENT|UNICODE_FONT" \
		boot.asm boot.bin instboot
	$(MV) boot.asm $@.asm
	./instboot boot.bin $@.exe
	./instboot boot.bin floppy.144
	gzip floppy.144
	$(MV) floppy.144.gz $@.img.gz

medium:
	make DISK_SUPPORT="$(DISK_FULL)" USER_SUPPORT="$(USER_SCREEN)" \
		SETUP="$(SETUP_STD_APPLI)|UPDATE_BOOTPARAM|USE_INT1587" \
		clean dep
	# pause to reread the file Makefile
	make DISK_SUPPORT="$(DISK_FULL)" USER_SUPPORT="$(USER_SCREEN)" \
		SETUP="$(SETUP_STD_APPLI)|UPDATE_BOOTPARAM|USE_INT1587" \
		boot.asm boot.bin instboot
	$(MV) boot.asm $@.asm
	./instboot boot.bin $@.exe
	./instboot boot.bin floppy.144
	gzip floppy.144
	$(MV) floppy.144.gz $@.img.gz

small:
	make DISK_SUPPORT="$(DISK_FULL)" USER_SUPPORT="$(USER_FEW)" \
		SETUP="$(SETUP_STD_APPLI)|UPDATE_BOOTPARAM|USE_INT1587|QUITE" \
		clean dep
	# pause to reread the file Makefile
	make DISK_SUPPORT="$(DISK_FULL)" USER_SUPPORT="$(USER_FEW)" \
		SETUP="$(SETUP_STD_APPLI)|UPDATE_BOOTPARAM|USE_INT1587|QUITE" \
		boot.asm boot.bin instboot
	$(MV) boot.asm $@.asm
	./instboot boot.bin $@.exe
	./instboot boot.bin floppy.144
	gzip floppy.144
	$(MV) floppy.144.gz $@.img.gz

myconf:
	make DISK_SUPPORT="$(DISK_FULL)" USER_SUPPORT="$(USER_FULL)" \
		SETUP="CODE_SEGMENT|XCODE_SEGMENT|ASSEMBLY_DSES|BIG_MALLOC" \
		clean dep
	# pause to reread the file Makefile
	make DISK_SUPPORT="$(DISK_FULL)" USER_SUPPORT="$(USER_FULL)" \
		SETUP="CODE_SEGMENT|XCODE_SEGMENT|ASSEMBLY_DSES|BIG_MALLOC" \
		boot.asm boot.bin instboot
	$(MV) boot.asm $@.asm
	./instboot boot.bin $@.exe
	./instboot boot.bin floppy.144
	gzip floppy.144
	$(MV) floppy.144.gz $@.img.gz

# Those cannot be something else than DOS executable, because they are
# configured to log information in a DOS file (\DBG):
dbgvideo dbgvideo.exe:
	make SETUP="$(SETUP_STD_APPLI)|USE_INT1587" DISK_SUPPORT="$(DISK_FULL_DOS)" \
		USER_SUPPORT="$(USER_SCREEN)" DEBUG="DOSFILE|DEBUG_STACK|DEBUG_VIDEO" clean dep
	# pause to reread the file Makefile
	make SETUP="$(SETUP_STD_APPLI)|USE_INT1587" DISK_SUPPORT="$(DISK_FULL_DOS)" \
		USER_SUPPORT="$(USER_SCREEN)" DEBUG="DOSFILE|DEBUG_STACK|DEBUG_VIDEO" \
		boot.bin instboot
	./instboot boot.bin dbgvideo.exe

dbgdisk dbgdisk.exe:
	make SETUP="$(SETUP_STD_APPLI)|USE_INT1587" DISK_SUPPORT="$(DISK_DBG)" \
		USER_SUPPORT="$(USER_STD)" DEBUG="DOSFILE|DEBUG_STACK|DEBUG_DISK" clean dep
	# pause to reread the file Makefile
	make SETUP="$(SETUP_STD_APPLI)|USE_INT1587" DISK_SUPPORT="$(DISK_DBG)" \
		USER_SUPPORT="$(USER_STD)" DEBUG="DOSFILE|DEBUG_STACK|DEBUG_DISK" \
		boot.bin instboot
	./instboot boot.bin dbgdisk.exe

dbgload dbgload.exe:
	make SETUP="$(SETUP_STD_APPLI)|USE_INT1587" DISK_SUPPORT="$(DISK_DBG)" \
		USER_SUPPORT="$(USER_STD_NOMOUSE)" DEBUG="DOSFILE|DEBUG_STACK|DEBUG_LOADER" clean dep
	# pause to reread the file Makefile
	make SETUP="$(SETUP_STD_APPLI)|USE_INT1587" DISK_SUPPORT="$(DISK_DBG)" \
		USER_SUPPORT="$(USER_STD_NOMOUSE)" DEBUG="DOSFILE|DEBUG_STACK|DEBUG_LOADER" \
		boot.bin instboot
	./instboot boot.bin dbgload.exe

dbgfs dbgfs.exe:
	make SETUP="$(SETUP_STD_APPLI)|USE_INT1587" DISK_SUPPORT="$(DISK_FULL_DOS)" \
		USER_SUPPORT="$(USER_STD)" DEBUG="DOSFILE|DEBUG_STACK|DEBUG_FS" clean dep
	# pause to reread the file Makefile
	make SETUP="$(SETUP_STD_APPLI)|USE_INT1587" DISK_SUPPORT="$(DISK_FULL_DOS)" \
		USER_SUPPORT="$(USER_STD)" DEBUG="DOSFILE|DEBUG_STACK|DEBUG_FS" \
		boot.bin instboot
	./instboot boot.bin dbgfs.exe

# A modified dbgdisk to debug the IDE timings - the (big) log file shall
# not be on a IDE disk: keep this DOS executable on a floppy:
# We remove DOS_SUPPORT from DISK_SUPPORT because of size restrictions...
# We do not have BOOTSECT_PROBE because we have a DOS executable.
# $(DISK_FULL): BIOS_SUPPORT|EBIOS_SUPPORT|IDE_SUPPORT|DOS_SUPPORT|E2FS_PROBE|FAT12_PROBE|FAT16_PROBE|FAT32_PROBE|BOOTSECT_PROBE
dbgide dbgide.exe:
	make SETUP="$(SETUP_STD_APPLI)|USE_INT1587" EXTRA_CPPFLAGS=-DDEBUG_IDE_TIMMING \
		DISK_SUPPORT="BIOS_SUPPORT|EBIOS_SUPPORT|IDE_SUPPORT|E2FS_PROBE|FAT12_PROBE|FAT16_PROBE|FAT32_PROBE" \
		USER_SUPPORT="$(USER_STD)" DEBUG="DOSFILE|DEBUG_STACK|DEBUG_DISK" clean dep
	# pause to reread the file Makefile
	make SETUP="$(SETUP_STD_APPLI)|USE_INT1587" EXTRA_CPPFLAGS=-DDEBUG_IDE_TIMMING \
		DISK_SUPPORT="BIOS_SUPPORT|EBIOS_SUPPORT|IDE_SUPPORT|E2FS_PROBE|FAT12_PROBE|FAT16_PROBE|FAT32_PROBE" \
		USER_SUPPORT="$(USER_STD)" DEBUG="DOSFILE|DEBUG_STACK|DEBUG_DISK" \
		boot.bin instboot
	./instboot boot.bin dbgide.exe

# Debug the mouse, on a dos file:
dbgmouse dbgmouse.exe:
	make SETUP="$(SETUP_STD_APPLI)|USE_INT1587" DISK_SUPPORT="$(DISK_FULL)" \
		USER_SUPPORT="$(USER_STD)|SERIAL_MOUSE_SUPPORT|JOYSTICK_SUPPORT" DEBUG="DOSFILE|DEBUG_STACK|DEBUG_MOUSE" clean dep
	# pause to reread the file Makefile
	make SETUP="$(SETUP_STD_APPLI)|USE_INT1587" DISK_SUPPORT="$(DISK_FULL)" \
		USER_SUPPORT="$(USER_STD)|SERIAL_MOUSE_SUPPORT|JOYSTICK_SUPPORT" DEBUG="DOSFILE|DEBUG_STACK|DEBUG_MOUSE" \
		boot.bin instboot
	./instboot boot.bin dbgmouse.exe

# If you have a problem which make logging to a floppy not possible, you can try:
# These can be either DOS executable or floppy images:

dbgdisk_screen:
	make SETUP="$(SETUP_STD_APPLI)|USE_INT1587" DISK_SUPPORT="$(DISK_DBG)" \
		USER_SUPPORT="$(USER_STD)" DEBUG="SCREEN|DEBUG_STACK|DEBUG_DISK" clean dep
	# pause to reread the file Makefile
	make SETUP="$(SETUP_STD_APPLI)|USE_INT1587" DISK_SUPPORT="$(DISK_DBG)" \
		USER_SUPPORT="$(USER_STD)" DEBUG="SCREEN|DEBUG_STACK|DEBUG_DISK" \
		boot.bin instboot
	./instboot boot.bin $@.exe
	./instboot boot.bin $@.144

dbgload_screen:
	make SETUP="$(SETUP_STD_APPLI)|USE_INT1587" DISK_SUPPORT="$(DISK_DBG)" \
		USER_SUPPORT="$(USER_STD_NOMOUSE)" DEBUG="SCREEN|DEBUG_STACK|DEBUG_LOADER" clean dep
	# pause to reread the file Makefile
	make SETUP="$(SETUP_STD_APPLI)|USE_INT1587" DISK_SUPPORT="$(DISK_DBG)" \
		USER_SUPPORT="$(USER_STD_NOMOUSE)" DEBUG="SCREEN|DEBUG_STACK|DEBUG_LOADER" \
		boot.bin instboot
	./instboot boot.bin $@.exe
	./instboot boot.bin $@.144

dbgfs_screen:
	make SETUP="$(SETUP_STD_APPLI)|USE_INT1587" DISK_SUPPORT="$(DISK_FULL)" \
		USER_SUPPORT="$(USER_STD_NOMOUSE)" DEBUG="SCREEN|DEBUG_STACK|DEBUG_FS" clean dep
	# pause to reread the file Makefile
	make SETUP="$(SETUP_STD_APPLI)|USE_INT1587" DISK_SUPPORT="$(DISK_FULL)" \
		USER_SUPPORT="$(USER_STD_NOMOUSE)" DEBUG="SCREEN|DEBUG_STACK|DEBUG_FS" \
		boot.bin instboot
	./instboot boot.bin $@.exe
	./instboot boot.bin $@.144

dbgvideo_com1:
	make SETUP="$(SETUP_STD_APPLI)|USE_INT1587" DISK_SUPPORT="$(DISK_FULL)" \
		USER_SUPPORT="$(USER_SCREEN)" DEBUG="COM1|DEBUG_STACK|DEBUG_VIDEO" clean dep
	# pause to reread the file Makefile
	make SETUP="$(SETUP_STD_APPLI)|USE_INT1587" DISK_SUPPORT="$(DISK_FULL)" \
		USER_SUPPORT="$(USER_SCREEN)" DEBUG="COM1|DEBUG_STACK|DEBUG_VIDEO" \
		boot.bin instboot
	./instboot boot.bin $@.exe
	./instboot boot.bin $@.144

dbgdisk_com1:
	make SETUP="$(SETUP_STD_APPLI)|USE_INT1587" DISK_SUPPORT="$(DISK_DBG)" \
		USER_SUPPORT="$(USER_STD)" DEBUG="COM1|DEBUG_STACK|DEBUG_DISK" clean dep
	# pause to reread the file Makefile
	make SETUP="$(SETUP_STD_APPLI)|USE_INT1587" DISK_SUPPORT="$(DISK_DBG)" \
		USER_SUPPORT="$(USER_STD)" DEBUG="COM1|DEBUG_STACK|DEBUG_DISK" \
		boot.bin instboot
	./instboot boot.bin $@.exe
	./instboot boot.bin $@.144

dbgdisk_com2:
	make SETUP="$(SETUP_STD_APPLI)|USE_INT1587" DISK_SUPPORT="$(DISK_DBG)" \
		USER_SUPPORT="$(USER_STD)" DEBUG="COM2|DEBUG_STACK|DEBUG_DISK" clean dep
	# pause to reread the file Makefile
	make SETUP="$(SETUP_STD_APPLI)|USE_INT1587" DISK_SUPPORT="$(DISK_DBG)" \
		USER_SUPPORT="$(USER_STD)" DEBUG="COM2|DEBUG_STACK|DEBUG_DISK" \
		boot.bin instboot
	./instboot boot.bin $@.exe
	./instboot boot.bin $@.144

dbgload_com1:
	make SETUP="$(SETUP_STD_APPLI)|USE_INT1587" DISK_SUPPORT="$(DISK_DBG)" \
		USER_SUPPORT="$(USER_STD_NOMOUSE)" DEBUG="COM1|DEBUG_STACK|DEBUG_LOADER" clean dep
	# pause to reread the file Makefile
	make SETUP="$(SETUP_STD_APPLI)|USE_INT1587" DISK_SUPPORT="$(DISK_DBG)" \
		USER_SUPPORT="$(USER_STD_NOMOUSE)" DEBUG="COM1|DEBUG_STACK|DEBUG_LOADER" \
		boot.bin instboot
	./instboot boot.bin $@.exe
	./instboot boot.bin $@.144

dbgload_com2:
	make SETUP="$(SETUP_STD_APPLI)|USE_INT1587" DISK_SUPPORT="$(DISK_DBG)" \
		USER_SUPPORT="$(USER_STD_NOMOUSE)" DEBUG="COM2|DEBUG_STACK|DEBUG_LOADER" clean dep
	# pause to reread the file Makefile
	make SETUP="$(SETUP_STD_APPLI)|USE_INT1587" DISK_SUPPORT="$(DISK_DBG)" \
		USER_SUPPORT="$(USER_STD_NOMOUSE)" DEBUG="COM2|DEBUG_STACK|DEBUG_LOADER" \
		boot.bin instboot
	./instboot boot.bin $@.exe
	./instboot boot.bin $@.144

dbgfs_com1:
	make SETUP="$(SETUP_STD_APPLI)|USE_INT1587" DISK_SUPPORT="$(DISK_FULL)" \
		USER_SUPPORT="$(USER_STD)" DEBUG="COM1|DEBUG_STACK|DEBUG_FS" clean dep
	# pause to reread the file Makefile
	make SETUP="$(SETUP_STD_APPLI)|USE_INT1587" DISK_SUPPORT="$(DISK_FULL)" \
		USER_SUPPORT="$(USER_STD)" DEBUG="COM1|DEBUG_STACK|DEBUG_FS" \
		boot.bin instboot
	./instboot boot.bin $@.exe
	./instboot boot.bin $@.144

dbgfs_com2:
	make SETUP="$(SETUP_STD_APPLI)|USE_INT1587" DISK_SUPPORT="$(DISK_FULL)" \
		USER_SUPPORT="$(USER_STD)" DEBUG="COM2|DEBUG_STACK|DEBUG_FS" clean dep
	# pause to reread the file Makefile
	make SETUP="$(SETUP_STD_APPLI)|USE_INT1587" DISK_SUPPORT="$(DISK_FULL)" \
		USER_SUPPORT="$(USER_STD)" DEBUG="COM2|DEBUG_STACK|DEBUG_FS" \
		boot.bin instboot
	./instboot boot.bin $@.exe
	./instboot boot.bin $@.144

dbgmouse_com1:
	make SETUP="$(SETUP_STD_APPLI)|USE_INT1587" DISK_SUPPORT="$(DISK_FULL)" \
		USER_SUPPORT="$(USER_STD)" DEBUG="COM1|DEBUG_STACK|DEBUG_MOUSE" clean dep
	# pause to reread the file Makefile
	make SETUP="$(SETUP_STD_APPLI)|USE_INT1587" DISK_SUPPORT="$(DISK_FULL)" \
		USER_SUPPORT="$(USER_STD)" DEBUG="COM1|DEBUG_STACK|DEBUG_MOUSE" \
		boot.bin instboot
	./instboot boot.bin $@.exe
	./instboot boot.bin $@.144

# Debug the serial terminal interface, on the PC screen:
dbgserial:
	make SETUP="$(SETUP_STD_APPLI)|USE_INT1587" DISK_SUPPORT="$(DISK_FULL)" \
		USER_SUPPORT="$(SERIAL)" DEBUG="SCREEN|DEBUG_STACK|DEBUG_SERIAL" clean dep
	# pause to reread the file Makefile
	make SETUP="$(SETUP_STD_APPLI)|USE_INT1587" DISK_SUPPORT="$(DISK_FULL)" \
		USER_SUPPORT="$(SERIAL)" DEBUG="SCREEN|DEBUG_STACK|DEBUG_SERIAL" \
		boot.bin instboot
	./instboot boot.bin $@.exe
	./instboot boot.bin $@.144

# Remove the E2FS and FAT 16 support (so can only load a kernel/initrd on
# a floppy, will not probe HDs). The menu is removed also, so it tries to load
# every kernel found (will run the first one sucessfully loaded) without asking
# anything: you must have a "vmlinuz" file on the floppy, and maybe an "initrd".
# If no kernel can be loaded, the INT 0x19 is executed (to tell "failed boot"
# to the BIOS, so it may try network or HD boots).
# The keyboard is never accessed here.
# BOOTSECT_PROBE is removed because you do not want to load a bootsector here,
# and UPDATE_BOOTPARAM is not needed because you do not want to save
# parameters in between boots.
# Take care, nothing here recognises the long filename on FAT12/FAT16,
# copy your kernel file as a 8+3 name, "vmlinuz" will be fine.
#
# It removes also the DOS support with BIOS_ONLY, so we can no more build the
# DOS bootloader "gujin.exe"/"gujin.com". Stack overflow checking (and all other
# exceptions like invalid instruction) are no more treated - finger crossed here!
tiny_img:
	make DISK_SUPPORT="$(DISK_MIN)" USER_SUPPORT=0 \
		SETUP="CODE_SEGMENT|ASSEMBLY_DSES|BIOS_ONLY|QUITE" \
		DEBUG=0 clean dep
	# pause to reread the file Makefile
	make DISK_SUPPORT="$(DISK_MIN)" USER_SUPPORT=0 \
		SETUP="CODE_SEGMENT|ASSEMBLY_DSES|BIOS_ONLY|QUITE" \
		DEBUG=0 boot.bin instboot
	./instboot boot.bin tiny.144

lotiny_img:
	make DISK_SUPPORT="$(DISK_MIN)" USER_SUPPORT=0 \
		SETUP="CODE_SEGMENT|ASSEMBLY_DSES|BIOS_ONLY|QUITE" \
		DEBUG="SCREEN|DEBUG_LOADER" clean dep
	# pause to reread the file Makefile
	make DISK_SUPPORT="$(DISK_MIN)" USER_SUPPORT=0 \
		SETUP="CODE_SEGMENT|ASSEMBLY_DSES|BIOS_ONLY|QUITE" \
		DEBUG="SCREEN|DEBUG_LOADER" \
		boot.bin instboot
	./instboot boot.bin $@.144

# This just adds partition probing, to install on a USB thumb drive,
# it will loads the first vmlinuz/initrd present on the thumb drive partition
tinyusb:
	make DISK_SUPPORT="$(DISK_USB)" USER_SUPPORT=0 \
		SETUP="CODE_SEGMENT|ASSEMBLY_DSES|BIOS_ONLY|QUITE" \
		DEBUG=0 clean dep
	# pause to reread the file Makefile
	make DISK_SUPPORT="$(DISK_USB)" USER_SUPPORT=0 \
		SETUP="CODE_SEGMENT|ASSEMBLY_DSES|BIOS_ONLY|QUITE" \
		DEBUG=0 boot.bin
	$(MV) boot.bin $@.bin

lodtinyusb:
	make DISK_SUPPORT="$(DISK_USB)" USER_SUPPORT=0 \
		SETUP="CODE_SEGMENT|ASSEMBLY_DSES|BIOS_ONLY|QUITE" \
		DEBUG="SCREEN|DEBUG_STACK|DEBUG_LOADER" clean dep
	# pause to reread the file Makefile
	make DISK_SUPPORT="$(DISK_USB)" USER_SUPPORT=0 \
		SETUP="CODE_SEGMENT|ASSEMBLY_DSES|BIOS_ONLY|QUITE" \
		DEBUG="SCREEN|DEBUG_STACK|DEBUG_LOADER" boot.bin
	$(MV) boot.bin $@.bin

# That is an alternative if something is not working with ASSEMBLY_DSES, but the
# former seems to work like it is, and is smaller.
tinyusb_alt:
	make DISK_SUPPORT="$(DISK_USB)" USER_SUPPORT=0 \
		SETUP="CODE_SEGMENT|ASSEMBLY_CISC|BIOS_ONLY|QUITE|USE_INT1587" \
		DEBUG=0 clean dep
	# pause to reread the file Makefile
	make DISK_SUPPORT="$(DISK_USB)" USER_SUPPORT=0 \
		SETUP="CODE_SEGMENT|ASSEMBLY_CISC|BIOS_ONLY|QUITE|USE_INT1587" \
		DEBUG=0 boot.bin
	$(MV) boot.bin $@.bin

lodtinyusb_alt:
	make DISK_SUPPORT="$(DISK_USB)" USER_SUPPORT=0 \
		SETUP="CODE_SEGMENT|ASSEMBLY_CISC|BIOS_ONLY|QUITE|USE_INT1587" \
		DEBUG="SCREEN|DEBUG_STACK|DEBUG_LOADER" clean dep
	# pause to reread the file Makefile
	make DISK_SUPPORT="$(DISK_USB)" USER_SUPPORT=0 \
		SETUP="CODE_SEGMENT|ASSEMBLY_CISC|BIOS_ONLY|QUITE|USE_INT1587" \
		DEBUG="SCREEN|DEBUG_STACK|DEBUG_LOADER" boot.bin
	$(MV) boot.bin $@.bin

# This only handle ISO9660 filesystem
# it will loads the first vmlinuz/initrd present on the CDROM/DVD
tinycdrom:
	make DISK_SUPPORT="$(DISK_CDROM)" USER_SUPPORT=0 \
		SETUP="CODE_SEGMENT|ASSEMBLY_DSES|BIOS_ONLY|QUITE" \
		DEBUG=0 clean dep
	# pause to reread the file Makefile
	make DISK_SUPPORT="$(DISK_CDROM)" USER_SUPPORT=0 \
		SETUP="CODE_SEGMENT|ASSEMBLY_DSES|BIOS_ONLY|QUITE" \
		DEBUG=0 boot.bin
	$(MV) boot.bin $@.bin

tinycdrom_dbgl:
	make DISK_SUPPORT="$(DISK_CDROM)" USER_SUPPORT=0 \
		SETUP="CODE_SEGMENT|ASSEMBLY_DSES|BIOS_ONLY|QUITE" \
		DEBUG="SCREEN|DEBUG_STACK|DEBUG_LOADER" clean dep
	# pause to reread the file Makefile
	make DISK_SUPPORT="$(DISK_CDROM)" USER_SUPPORT=0 \
		SETUP="CODE_SEGMENT|ASSEMBLY_DSES|BIOS_ONLY|QUITE" \
		DEBUG="SCREEN|DEBUG_STACK|DEBUG_LOADER" boot.bin
	$(MV) boot.bin $@.bin

tinycdrom_dbgd:
	make DISK_SUPPORT="$(DISK_CDROM)" USER_SUPPORT=0 \
		SETUP="CODE_SEGMENT|ASSEMBLY_DSES|BIOS_ONLY|QUITE" \
		DEBUG="SCREEN|DEBUG_STACK|DEBUG_DISK" clean dep
	# pause to reread the file Makefile
	make DISK_SUPPORT="$(DISK_CDROM)" USER_SUPPORT=0 \
		SETUP="CODE_SEGMENT|ASSEMBLY_DSES|BIOS_ONLY|QUITE" \
		DEBUG="SCREEN|DEBUG_STACK|DEBUG_DISK" boot.bin
	$(MV) boot.bin $@.bin

tinycdrom_dbgf:
	make DISK_SUPPORT="$(DISK_CDROM)" USER_SUPPORT=0 \
		SETUP="CODE_SEGMENT|ASSEMBLY_DSES|BIOS_ONLY|QUITE" \
		DEBUG="SCREEN|DEBUG_STACK|DEBUG_FS" clean dep
	# pause to reread the file Makefile
	make DISK_SUPPORT="$(DISK_CDROM)" USER_SUPPORT=0 \
		SETUP="CODE_SEGMENT|ASSEMBLY_DSES|BIOS_ONLY|QUITE" \
		DEBUG="SCREEN|DEBUG_STACK|DEBUG_FS" boot.bin
	$(MV) boot.bin $@.bin

# This is the "opposite" of tiny_img, it produces only a simple "mingujin.exe"
# which do not probe hard disks at all and has no graphic interface:
# it has to be used a la loadlin.exe, under DOS, like:
# mingujin.exe c:\dir1\dir2\vmlinuz c:\dir3\initrd.gz opt1=val1 opt2=val2
mingujin.exe:
	make DISK_SUPPORT=DOS_SUPPORT USER_SUPPORT=0 \
		SETUP="CODE_SEGMENT|ASSEMBLY_CISC|USE_INT1587|QUITE|MULTILINGUAL" \
		DEBUG=0 clean dep
	# pause to reread the file Makefile
	make DISK_SUPPORT=DOS_SUPPORT USER_SUPPORT=0 \
		SETUP="CODE_SEGMENT|ASSEMBLY_CISC|USE_INT1587|QUITE|MULTILINGUAL" \
		DEBUG=0 boot.bin instboot
	./instboot boot.bin mingujin.exe

# Some DEBUG to dos file "DBG" version:
fstiny_exe:
	make DISK_SUPPORT=DOS_SUPPORT USER_SUPPORT=0 \
		SETUP="CODE_SEGMENT|ASSEMBLY_CISC|USE_INT1587|QUITE" \
		DEBUG="DOSFILE|DEBUG_STACK|DEBUG_FS" clean dep
	# pause to reread the file Makefile
	make DISK_SUPPORT=DOS_SUPPORT USER_SUPPORT=0 \
		SETUP="CODE_SEGMENT|ASSEMBLY_CISC|USE_INT1587|QUITE" \
		DEBUG="DOSFILE|DEBUG_STACK|DEBUG_FS" boot.bin instboot
	./instboot boot.bin fstiny.exe

lotiny_exe:
	make DISK_SUPPORT=DOS_SUPPORT USER_SUPPORT=0 \
		SETUP="CODE_SEGMENT|ASSEMBLY_CISC|USE_INT1587|QUITE" \
		DEBUG="DOSFILE|DEBUG_STACK|DEBUG_LOADER" clean dep
	# pause to reread the file Makefile
	make DISK_SUPPORT=DOS_SUPPORT USER_SUPPORT=0 \
		SETUP="CODE_SEGMENT|ASSEMBLY_CISC|USE_INT1587|QUITE" \
		DEBUG="DOSFILE|DEBUG_STACK|DEBUG_LOADER" boot.bin instboot
	./instboot boot.bin lotiny.exe

lodtiny_exe:
	make DISK_SUPPORT=DOS_SUPPORT USER_SUPPORT=0 \
		SETUP="CODE_SEGMENT|ASSEMBLY_CISC|USE_INT1587|QUITE" \
		DEBUG="SCREEN|DEBUG_STACK|DEBUG_LOADER" clean dep
	# pause to reread the file Makefile
	make DISK_SUPPORT=DOS_SUPPORT USER_SUPPORT=0 \
		SETUP="CODE_SEGMENT|ASSEMBLY_CISC|USE_INT1587|QUITE" \
		DEBUG="SCREEN|DEBUG_STACK|DEBUG_LOADER" boot.bin instboot
	./instboot boot.bin lodtiny.exe


ditiny_exe:
	make DISK_SUPPORT=DOS_SUPPORT USER_SUPPORT=0 \
		SETUP="CODE_SEGMENT|ASSEMBLY_CISC|USE_INT1587|QUITE" \
		DEBUG="DOSFILE|DEBUG_STACK|DEBUG_DISK" clean dep
	# pause to reread the file Makefile
	make DISK_SUPPORT=DOS_SUPPORT USER_SUPPORT=0 \
		SETUP="CODE_SEGMENT|ASSEMBLY_CISC|USE_INT1587|QUITE" \
		DEBUG="DOSFILE|DEBUG_STACK|DEBUG_DISK" boot.bin instboot
	./instboot boot.bin ditiny.exe

# Just temporary...
tiny_dbg_exe:
	make DISK_SUPPORT=DOS_SUPPORT USER_SUPPORT=0 \
		SETUP="CODE_SEGMENT|ASSEMBLY_CISC|USE_INT1587|QUITE" \
		DEBUG="SCREEN|DEBUG_STACK|DEBUG_LOADER" clean dep
	# pause to reread the file Makefile
	make DISK_SUPPORT=DOS_SUPPORT USER_SUPPORT=0 \
		SETUP="CODE_SEGMENT|ASSEMBLY_CISC|USE_INT1587|QUITE" \
		DEBUG="SCREEN|DEBUG_STACK|DEBUG_LOADER" boot.bin instboot
	./instboot boot.bin tiny_dbg.exe

# Let's do the install pack, install.tar.gz in a local directory:
install.tar.gz:
	$(RM) -r ./install install.tar.gz
	mkdir ./install
	make cleandisk.kgz hello_bios.kgz hello_32bits.kgz hello_32bits_r.kgz hello_gpl.kgz
	$(MV) cleandisk.kgz hello_bios.kgz hello_32bits.kgz hello_32bits_r.kgz hello_gpl.kgz ./install
	# Fedora32 cannot compile that, use a set compiled by Debian:
	make hello_64bits.kgz hello_64bits_r.kgz || make GCC64=/usr/bin/gcc hello_64bits.kgz hello_64bits_r.kgz || cp ../hello_64bits.kgz ../hello_64bits_r.kgz .
	$(MV) hello_64bits.kgz hello_64bits_r.kgz ./install
	# This one is compiled on a 64 bits host:
	( [ -f ../gujin64 ] && cp ../gujin64 ./install ) || touch ./install/gujin64
	make clean
	make gujin
	$(MV) gujin install
	# for users only using windows, do the most usefull images:
	install/gujin tinystd.bin install/boot.144 --cmdline="" --full
	install/gujin install/gujin.bcd --cmdline=""
	install/gujin -t install/minigujin.bcd --stop_emulation=0 --cmdline=""
	install/gujin install/gujin.exe --cmdline=""
	install/gujin -t install/mingujin.exe --cmdline=""
	mkdir install/tmpiso
	install/gujin install/tmpiso/gujin.bcd --cmdline="" --force_textmode=1 --full
	cp gujin.cmd install/tmpiso/
	cp install/cleandisk.kgz install/tmpiso/
	cp install/hello_bios.kgz install/tmpiso/
	cp install/hello_32bits.kgz install/tmpiso/
	cp install/hello_32bits_r.kgz install/tmpiso/
	cp install/hello_64bits.kgz install/tmpiso/
	cp install/hello_64bits_r.kgz install/tmpiso/
	cp install/hello_gpl.kgz install/tmpiso/
	cp install/boot.144 install/tmpiso/floppyimage.bdi
	# disk simulated on ATAPI with 2048 bytes per sectors (gujin loading itself from EBIOS simulation)
	install/gujin install/tmpiso/hd2048bps.img --geometry=2000,0,16,63,2048 --disk=EBIOS:0x80,auto
	$(GENISOIMAGE) -R -no-emul-boot -boot-load-size 4 -b gujin.bcd -o install/gujin.iso install/tmpiso
	rm -rf install/tmpiso
	# same as USB1Gb.img: # shall we --quickboot= or --timeout_autoload= ?
	# Those geometry will be with sectors/track=32, use multiple of 32 for the total size for mtools:
	install/gujin install/USB1Gb.img --geometry=1953120 --disk=EBIOS:0x00,auto --probe_ide_disk=0 --cmdline="quiet"
	install/gujin install/USB2Gb.img --geometry=3906240 --disk=EBIOS:0x00,auto --probe_ide_disk=0 --cmdline="quiet"
	install/gujin install/USB4Gb.img --geometry=7812480 --disk=EBIOS:0x00,auto --probe_ide_disk=0 --cmdline="quiet"
	install/gujin install/USB8Gb.img --geometry=15624992 --disk=EBIOS:0x00,auto --probe_ide_disk=0 --cmdline="quiet"
	# install/gujin -t install/USB1GbTiny.img --geometry=1953120 --disk=BIOS:0x80,auto --cmdline="quiet"
	$(CP) install.txt ./install/install.txt
	$(CP) gujin.cmd ./install/gujin.cmd
	chmod a-wx ./install/*
	make gzcopy addhole showmap gujin-cmdline
	$(MV) gzcopy addhole showmap gujin-cmdline ./install/
	chmod a-w,a+x ./install/gzcopy ./install/addhole ./install/showmap ./install/gujin-cmdline
	chmod a-w,a+x ./install/gujin ./install/gujin64
	tar -cvzf install.tar.gz install
	$(RM) -r ./install
	make clean

# For this one, you need to do, as root (using DOSEMU-1.0):
# touch /var/lib/dosemu/floppyimage
# chmod a+w /var/lib/dosemu/floppyimage
# and edit /etc/dosemu.users to add your connection name
# and edit /etc/dosemu.conf to add:
#   $_vbootfloppy = "floppyimage"
# and _only_ if you have unmounted partitions:
#   $_hdimage = "hdimage.first /dev/hda1:ro /dev/hda5:ro "
# Then run DOSEMU (as a user) like: dos 2> /dev/null
# To boot again from the hdimage, type "dos -C"
# It does not work for non 1.44M floppies, I do not know why.
# Note that it is just faster than making a real boot floppy
# and type "dos -A" - use it just if you do not have a floppy
# or a floppy driver...
floppyimage: gujin
	./gujin /var/lib/dosemu/$@ -g=floppy:144 $(INSTBOOT_DEFAULT_PARAMETER)

# Yes, sometimes DOSEMU is cool, so try it:
# Unfortunately, you will have to be root to use "mount"
# You can then run DOSEMU as a user like "dos -C 2>/dev/null"
# and type "boot" at the command prompt. Do _not_ run DOSEMU
# before unmounting the (loop) file. You have to create directory
# /mnt/dosemu and may need to adjust the name of the hdimage.
# You could also use "mcopy gujin.exe n:", if mtools is correctly
# configured, see file "/etc/mtools.conf"
dosemu: gujin.exe
	mount -o loop=/dev/loop0,offset=0x2280 -o sync \
		/var/lib/dosemu/hdimage.freedos -t vfat /mnt/dosemu
	cp gujin.exe /mnt/dosemu
	umount /mnt/dosemu

# To save floppy space, you can modify the filesystem parameters:
# first execute ./gujin to print default used:
#   $ ./gujin boot.144 -w=r
#   Reprint: gujin boot.bin boot.144  \
#   Reprint:    --geometry=2880,0,2,18,512,2880,0,2,18,512 \
#   Reprint:    --disk=BIOS:0x0 --fs=FAT:2880,1,14,1,2,12,0x0,0xF0 \
#   Finishing (flushing buffers)...
# Now cut and paste the fs line and modify the Sector/Cluster and NRS:
#   $ ./gujin boot.144 --fs=FAT:2880,8,2
# Remember the --fs format:
#   --fs=FAT:nbSectorInFS,Sector/Cluster,NRS,RES,NF,FS,PDN,MD
# Abreviations: NRS: nb Root Sectors <14>   (default is for 224 root entries)
#     RES: nb FAT reserved sectors <1>, NF: nbFat <2>, FS: Fat size <12|16>,
#     PDN: physical drive nb <0x00|0x80>, MD: media descriptor <0xF0>
# If you change the number of FAT (NF), then the floppy cannot be read
# by MSDOS or Windows but mtools works perfectly and it should boot
# for every accepted configuration (./gujin exiting without error)
# If NRS is 2, you can have only 2 * 512 / 32 = 32 files slots on the root
# directory - one file slot is one short (8+3) filename.
# Increasing Sector/Cluster reduces the size of the FAT,
# but more sectors will be unused at end of each file.
bf144.img.gz:
	make SETUP=$(SETUP_MIN_APPLI) DISK_SUPPORT=$(DISK_MIN) \
		USER_SUPPORT=0 DEBUG=DEBUG_STACK instboot boot.bin
	./instboot boot.bin bf144.img --geometry=floppy:144 \
		--fs=FAT:2880,8,2  $(INSTBOOT_DEFAULT_PARAMETER)
	gzip -9 bf144.img

##
## CDROM targets:
##

# What I am doing to generate and test bootable CDROMs:
# First generate FD emulations, then HD emulations:
# Note that cdrecord can be preceded by minus to ignore the error if
#   the CD is just CDR and not CDRW (so cannot erase).
# Note that if the directory /tmp/mnt_cdrom exists, you can do as root:
#   mount -o loop -t vfat /tmp/cdrom/boot.120 /tmp/mnt_cdrom
# (replace boot.120 by the file you have generated, 120 is for 1.2 Mbytes
#  floppies) before doing the "rm -rf /tmp/cdrom".
# NOTE: cdrecord may require to be root / suid for security reasons
# NOTE: What was working is no more, now I suppose you have something
# like "ln -s /dev/hdc /dev/cdrom" instead of giving param "dev=ATAPI:0,0"
# to each "cdrecord".
# All those use Gujin default for floppy "--disk=BIOS:0x0".
CD120FD: gujin
	rm -rf /tmp/cdrom
	mkdir /tmp/cdrom
	./gujin /tmp/cdrom/boot.120 --full
	- $(CDRECORD) -v blank=fast
	$(GENISOIMAGE) -R -b boot.120 /tmp/cdrom | $(CDRECORD) -tao -v -
	rm -rf /tmp/cdrom

CD144FD: gujin
	rm -rf /tmp/cdrom
	mkdir /tmp/cdrom
	./gujin /tmp/cdrom/boot.144 --full
	- $(CDRECORD) -v blank=fast
	$(GENISOIMAGE) -R -b boot.144 /tmp/cdrom | $(CDRECORD) -tao -v -
	rm -rf /tmp/cdrom

CD288FD: gujin
	rm -rf /tmp/cdrom
	mkdir /tmp/cdrom
	./gujin /tmp/cdrom/boot.288 --full
	- $(CDRECORD) -v blank=fast
	$(GENISOIMAGE) -R -b boot.288 /tmp/cdrom | $(CDRECORD) -tao -v -
	rm -rf /tmp/cdrom

# This is used to put a kernel/initrd on the floppy and so Gujin will stop the
# floppy emulation after loading kernel or before jumping to a MBR.
CD288FD-late: gujin
	rm -rf /tmp/cdrom
	mkdir /tmp/cdrom
	./gujin /tmp/cdrom/boot.288 --full --stop_emulation=2
	# do here "mcopy kernel&initrd" to the drive /tmp/cdrom/boot.288 in /etc/mtools
	- $(CDRECORD) -v blank=fast
	$(GENISOIMAGE) -R -b boot.288 /tmp/cdrom | $(CDRECORD) -tao -v -
	rm -rf /tmp/cdrom

# For this one, --stop_emulation= defaults to 1 so early stop CDROM
# emulation, just after Gujin is loaded and before it scans the disks:
# The kernel to load cannot be inside the simulated HD.
# Note the offset given to the mount, 32256 = 63 * 16 * 32 does match
# the sector/track * heads * cylinder...
# Also take care if EBIOS is supported and if the boot disk is given
# in DX (works for me so ",auto") ... see CDHD[1-6] target later
# to test each possible configuration...
# Theoretically the C/H/S of the simulated disk is given by the CDROM BIOS
# but Gujin cannot adapt itself to changing C/H/S at run time.
CDsmallHD: gujin
	rm -rf /tmp/cdrom
	mkdir /tmp/cdrom
	./gujin /tmp/cdrom/cd16Mb --full --mbr -d=EBIOS:0x80,auto \
		--geometry=32193,63,16,63,512,32256,0
	/sbin/fdisk -b 512 -S 63 -H 16 -C 32 /tmp/cdrom/cd16Mb -lu
	#root# rm -rf /tmp/mnt_cdrom
	#root# mkdir  /tmp/mnt_cdrom
	#root# mount -t vfat /tmp/cdrom/cd16Mb /tmp/mnt_cdrom -o loop,offset=32256 -o umask=0
	#root# ls -l  /tmp/mnt_cdrom
	#root# umount /tmp/mnt_cdrom
	#root# rm -rf /tmp/mnt_cdrom
	- $(CDRECORD) -v blank=fast
	$(GENISOIMAGE) -R -hard-disk-boot -b cd16Mb /tmp/cdrom | $(CDRECORD) -tao -
	rm -rf /tmp/cdrom

# Tests - need Gujin itself to load the simulated HD of the CDROM, because
# most motherboard BIOSes can boot from a simulated HD but they do
# not provide EBIOS. Moreover here we use the fact that Gujin will analyse
# the MBR containning 0x29 signature and reduce PhysicaldriveNb found
# to have contiguous BIOS hard disk number (so the simulated HD does not
# hide nor change BIOS disk numbers).
# --stop_emulation=0 so never terminate the emulation so that the BIOS disk
# is still available _after_ booting for instance FreeDOS, maybe to check dbgdisk.exe...
CDsmallHD-late: gujin
	rm -rf /tmp/cdrom
	mkdir /tmp/cdrom
	./gujin /tmp/cdrom/cd16Mb --full --mbr -d=EBIOS:0x80,auto --stop_emulation=0 \
		--geometry=32193,63,16,63,512,32256,0
	/sbin/fdisk -b 512 -S 63 -H 16 -C 32 /tmp/cdrom/cd16Mb -lu
	- $(CDRECORD) -v blank=fast
	$(GENISOIMAGE) -R -hard-disk-boot -b cd16Mb /tmp/cdrom | $(CDRECORD) -tao -
	rm -rf /tmp/cdrom

# For this one, --stop_emulation=2 so the simulated HD is scanned for kernels and
# bootable partition(s). The simulated HD is still removed before running the real
# kernel (in fact before linux_get_param() call):
CDbigHD: gujin
	rm -rf /tmp/cdrom
	mkdir /tmp/cdrom
	./gujin /tmp/cdrom/boot.cd --full --mbr -d=EBIOS:0x80 \
		--geometry=1032129,63,16,63,512,1032192,0 \
		--stop_emulation=2
	/sbin/fdisk -b 512 -S 63 -H 16 -C 1024 /tmp/cdrom/boot.cd -lu
	#root# rm -rf /tmp/mnt_cdrom
	#root# mkdir  /tmp/mnt_cdrom
	#root# mount -t vfat /tmp/cdrom/boot.cd /tmp/mnt_cdrom -o loop,offset=32256 -o umask=0
	#root# ls -l  /tmp/mnt_cdrom
	#root# umount /tmp/mnt_cdrom
	#root# rm -rf /tmp/mnt_cdrom
	- $(CDRECORD) -v blank=fast
	$(GENISOIMAGE) -R -hard-disk-boot -b boot.cd /tmp/cdrom | $(CDRECORD) -tao -
	rm -rf /tmp/cdrom

# Test which BIOS you have, one of them should boot:
# Another way is to recompile CDsmallHD but un-commenting in boot.c the line "#define ONLY_PRINT_GEOMETRY_ALL_DISKS"
# you can then see which BIOS number (in DX before) will reply to interrupts and its geometry (in 2nd DX/CX/BX)
CDHD1: gujin
	rm -rf /tmp/cdrom
	mkdir /tmp/cdrom
	./gujin /tmp/cdrom/cd16Mb --full --mbr -d=BIOS:0x80,0x00 --geometry=32193,63,16,63,512,32256,0
	- $(CDRECORD) -v blank=fast
	$(GENISOIMAGE) -R -hard-disk-boot -b cd16Mb /tmp/cdrom | $(CDRECORD) -tao -
	rm -rf /tmp/cdrom

CDHD2: gujin
	rm -rf /tmp/cdrom
	mkdir /tmp/cdrom
	./gujin /tmp/cdrom/cd16Mb --full --mbr -d=BIOS:0x80,0x80 --geometry=32193,63,16,63,512,32256,0
	- $(CDRECORD) -v blank=fast
	$(GENISOIMAGE) -R -hard-disk-boot -b cd16Mb /tmp/cdrom | $(CDRECORD) -tao -
	rm -rf /tmp/cdrom

CDHD3: gujin
	rm -rf /tmp/cdrom
	mkdir /tmp/cdrom
	./gujin /tmp/cdrom/cd16Mb --full --mbr -d=BIOS:0x80,auto --geometry=32193,63,16,63,512,32256,0
	- $(CDRECORD) -v blank=fast
	$(GENISOIMAGE) -R -hard-disk-boot -b cd16Mb /tmp/cdrom | $(CDRECORD) -tao -
	rm -rf /tmp/cdrom

CDHD4: gujin
	rm -rf /tmp/cdrom
	mkdir /tmp/cdrom
	./gujin /tmp/cdrom/cd16Mb --full --mbr -d=EBIOS:0x80,0x00 --geometry=32193,63,16,63,512,32256,0
	- $(CDRECORD) -v blank=fast
	$(GENISOIMAGE) -R -hard-disk-boot -b cd16Mb /tmp/cdrom | $(CDRECORD) -tao -
	rm -rf /tmp/cdrom

CDHD5: gujin
	rm -rf /tmp/cdrom
	mkdir /tmp/cdrom
	./gujin /tmp/cdrom/cd16Mb --full --mbr -d=EBIOS:0x80,0x80 --geometry=32193,63,16,63,512,32256,0
	- $(CDRECORD) -v blank=fast
	$(GENISOIMAGE) -R -hard-disk-boot -b cd16Mb /tmp/cdrom | $(CDRECORD) -tao -
	rm -rf /tmp/cdrom

CDHD6: gujin
	rm -rf /tmp/cdrom
	mkdir /tmp/cdrom
	./gujin /tmp/cdrom/cd16Mb --full --mbr -d=EBIOS:0x80,auto --geometry=32193,63,16,63,512,32256,0
	- $(CDRECORD) -v blank=fast
	$(GENISOIMAGE) -R -hard-disk-boot -b cd16Mb /tmp/cdrom | $(CDRECORD) -tao -
	rm -rf /tmp/cdrom

CDHD7: gujin
	rm -rf /tmp/cdrom
	mkdir /tmp/cdrom
	./gujin /tmp/cdrom/cd16Mb --full --mbr -d=BIOS:0x80,auto --geometry=32130,63,15,63,512,32193,0
	- $(CDRECORD) -v blank=fast
	$(GENISOIMAGE) -R -hard-disk-boot -b cd16Mb /tmp/cdrom | $(CDRECORD) -tao -
	rm -rf /tmp/cdrom

# This should generate a bootable CD where you can select with the mouse
# the emulation you want... but mkisofs just generate a file boot.cat (which
# shall be the El-Torito directory) with only the boot method of the
# last "-b" argument: $ hexdump /mnt/disk/boot.cat
# 0000000 0001 0000 0000 0000 0000 0000 0000 0000
# 0000010 0000 0000 0000 0000 0000 0000 55aa aa55
# 0000020 0288 0000 0000 0001 001a 0000 0000 0000
# 0000030 0000 0000 0000 0000 0000 0000 0000 0000
# *
# 0000800
CDallEmul: gujin
	rm -rf /tmp/cdrom
	mkdir /tmp/cdrom
	./gujin /tmp/cdrom/boot.120 --full
	./gujin /tmp/cdrom/boot-late.120 --full --stop_emulation=2
	./gujin /tmp/cdrom/boot.144 --full
	./gujin /tmp/cdrom/boot-late.144 --full --stop_emulation=2
	./gujin /tmp/cdrom/boot.288 --full
	./gujin /tmp/cdrom/boot-late.288 --full --stop_emulation=2
	./gujin /tmp/cdrom/boot-2048 --full --stop_emulation=0 -d=BIOS:0x00 \
		--geometry=1440,0,2,9,2048	# 2.88Mb, 2048 bytes/sect, can be read/write on CDRW
	#./gujin /tmp/cdrom/cd16Mb --full --mbr --stop_emulation=0 -d=BIOS:0x80,auto \
	#	--geometry=32193,63,16,63,512,32256,0
	#./gujin /tmp/cdrom/cd16Mb --full --mbr --stop_emulation=0 -d=EBIOS:0x80,auto \
	#	--geometry=32193,63,16,63,512,32256,0
	#./gujin /tmp/cdrom/cd16Mb --full --mbr --stop_emulation=0 -d=EBIOS:0xFF,auto \
	#	--geometry=32193,63,16,63,512,32256,0
	- $(CDRECORD) -v blank=fast
	$(GENISOIMAGE) -R -b boot.120 -b boot.144 -b boot.288 -b boot-late.120 -b boot-late.144 -b boot-late.288 \
		-b boot-2048 /tmp/cdrom | $(CDRECORD) -tao -v -
	#	-b boot-2048 -b cd16Mb_bios -b cd16Mb_ebios -b cd16Mb_auto /tmp/cdrom | $(CDRECORD) -tao -v -
	rm -rf /tmp/cdrom

# No emulation mode of CDROM, i.e. the file is loaded in memory (at a segment
# boundary, so real mode memory, below 1 Mb) by someone else and control is
# tranfered at the first instruction. For CDROM no-emulation mode we keep the
# same base address of cs:ip = 0x7C0:0 - some BIOSes do not work otherwise.
# The main problem is that most (all?) BIOS are buggy and have a (very) small
# limit in the file size they can load - unlike Rock Ridge Bootable description
# which say the full 640 Kb shall be loadable.
#
# The CDnoemul-notpatched target create what should work on a bug free BIOS,
# i.e. a Position Independant Code, and the CDnoemul (i.e. patched) target
# try to detect the CDROM disk BIOS number and offset of the file and try to load
# the end of the file (it is not so different than a floppy/hard disk CDROM
# emulation - just the complete CDROM should be accessible using 2048 bytes
# sectors). Usually the CDROM BIOS number is not consecutive with other
# hard disks (mine is 0x9F, on another PC with 1 HD it is 0x82) and is not
# present if the PC did not boot from the CDROM.
#
# I do not use "-boot-info-table" option of mkisofs because the table written
# in the first sector is really big (full of undefined/reserved zero words) and
# erases some of the code of Gujin1 (_start is at address 0x3E, -boot-info-table
# creates a table of 56 = 0x38 bytes starting at 0x08). I have never seen a BIOS
# needing it anyway - please send me the reference of a BIOS which will not boot
# with the current code (make CDnoemul) but will boot with the table (make CDnoemul0)
# Using the target CDnoemul0, i.e. with -boot-info-table, is working on my
# PC but display an ignored checksum error on BOOT1 and does not display
# correctly the first line before the copyright.
#
# It would be very nice to have an option of mkisofs to just set the LBA at
# the usual offset, i.e. "bootbefore.part1.NbHiddensector" in /mnt/cdrom/gujin.bcd
# file, when the 0x29 signature is present. You can see where is the first LBA
# of gujin.bcd when using "-boot-info-table" like this (having the CDROM mounted
# on "/mnt/cdrom/"):
# hexdump /mnt/cdrom/gujin.bcd | head -2 # 32 bits at offset 0x0C
# 0000000 ebfa 473b 6a75 6e69 0010 0000 001a 0000
# 0000010 3800 0002 f4ee a405 0000 0000 0000 0000
# So boot.cdr start at sector 26, 0x1A with 2048 bytes/sect,
# so at offset 0x00d000.
#
# "hexdump -cx /dev/cdrom | less" with or without "-boot-info-table" would
# also show where the file begins (search for a line containning "Gujin1")
#  - it is usually at 0xd000, i.e. at 26 sectors of 2048 bytes, with those
#  Makefile targets (very few files on CDROM so filetable really small).
#
# Note that this target 'CDnoemul-notpatched' produces the file "gujin.pic" which
# is the same kind of file than the DOS file "IO.SYS" i.e. you could hope
# to generate a floppy like this using DOS:
# C:\> format /s /u a:
# C:\> attrib -h a:MSDOS.SYS
# C:\> attrib -h a:DBLSPACE.BIN
# C:\> del a:MSDOS.SYS
# C:\> del a:DBLSPACE.BIN
# C:\> del a:COMMAND.COM
# C:\> copy GUJIN.PIC a:IO.SYS
# C:\> echo X > a:MSDOS.SYS
# C:\> attrib +h a:MSDOS.SYS
# C:\> attrib +h a:IO.SYS
# C:\> attrib +s a:MSDOS.SYS
# C:\> attrib +s a:IO.SYS
# And reboot with such a floppy (that uses the DOS floppy bootsector to
# load IO.SYS which is now our own GUJIN.PIC). Unfortunately this boot
# record is also limited in the size it can load - at least the one I
# tried (real MSDOS 6.2). If you find one bootrecord able to load
# Gujin completely - please say so! (shall display the complete line of
# dot when checking the checksums).
#
CDnoemul-notpatched: gujin
	rm -rf /tmp/cdrom
	mkdir /tmp/cdrom
	# Create a real position independant file, but base address aligned at 0x10 boundary:
	#./gujin /tmp/cdrom/gujin.pic
	# -g with head = 0 means max load/checksum coded in sect/track= 16 sectors
	# 16 sector loaded max, each 512 bytes so 8 kb loaded (and checksumed) per gujin1 star
	# so you can see how long is really loaded with the number of stars
	# 2880 (partition size/disk size) not used but shall be big enough to not trigger
	# an error in ./gujin.
	./gujin /tmp/cdrom/gujin.pic --geometry=2880,0,0,16,512 --disk=PIC -w
	# objdump -D -m i8086 --start-address=0 -b binary /tmp/cdrom/gujin.pic | less
	- $(CDRECORD) blank=fast
	$(GENISOIMAGE) -R -no-emul-boot -b gujin.pic /tmp/cdrom | $(CDRECORD) -tao -
	# $(GENISOIMAGE) -R -no-emul-boot -boot-load-seg 0x4000 -b gujin.pic /tmp/cdrom | $(CDRECORD) -tao -
	rm -rf /tmp/cdrom

# use the default ./gujin for BCD: "--geometry=10000000,0,0,16,2048 --disk=BCD",
#  so will load and checksum 32 Kb per star displayed on 1st line.
# Minus before cdrecord to ignore error like erasing a CD-R
# To see the assembly instructions:
#  objdump -D --start-address=0x3E -m i8086 -b binary /tmp/cdrom/gujin.bcd | less
CDnoemul: gujin
	rm -rf /tmp/cdrom
	mkdir /tmp/cdrom
	./gujin /tmp/cdrom/gujin.bcd
	- $(CDRECORD) blank=fast
	# -boot-load-size 4 is hopefully working everywhere - four because it is the
	#  size of a CDROM sector: 2048 bytes (could be more but not less):
	# FIXME: it is really 4*2048 and not 4*512 - Gujin just need one 2048 sector loaded.
	# My BIOS does not boot with "-boot-load-size 1", mkisofs problem?
	# check that mkisofs display "Size of boot image is 4 sectors -> No emulation"
	$(GENISOIMAGE) -R -no-emul-boot -boot-load-size 4 -b gujin.bcd /tmp/cdrom | $(CDRECORD) -tao -
	rm -rf /tmp/cdrom

# Test if it works even when "mkisofs -boot-info-table" is given, then some code
# and data are erased and the first line (Gujin1, Gujin2... OK) does not look nice.
CDnoemul0: gujin
	rm -rf /tmp/cdrom
	mkdir /tmp/cdrom
	./gujin /tmp/cdrom/gujin.bcd
	- $(CDRECORD) blank=fast
	# check that mkisofs display "Size of boot image is 4 sectors -> No emulation"
	$(GENISOIMAGE) -R -no-emul-boot -boot-load-size 4 -boot-info-table -b gujin.bcd /tmp/cdrom | $(CDRECORD) -tao -
	rm -rf /tmp/cdrom

# Try another segment loading address by curiosity (works on _some_ of my PC):
CDnoemul1: gujin
	rm -rf /tmp/cdrom
	mkdir /tmp/cdrom
	./gujin /tmp/cdrom/gujin.bcd
	- $(CDRECORD) blank=fast
	$(GENISOIMAGE) -R -no-emul-boot -boot-load-size 4 -b gujin.bcd -boot-load-seg 0x1000 /tmp/cdrom | $(CDRECORD) -tao -
	rm -rf /tmp/cdrom

# Test multiple sessions (they are treated like multiple partitions):
CDall: gujin
	rm -rf /tmp/cdrom
	mkdir /tmp/cdrom
	- $(CDRECORD) blank=fast -s
	./gujin /tmp/cdrom/boot.120 --full
	mkdir /tmp/cdrom/boot
	-cp /boot/* /tmp/cdrom/boot
	$(GENISOIMAGE) -R -b boot.120 /tmp/cdrom | $(CDRECORD) -tao -multi gracetime=0 -s -
	rm -f /tmp/cdrom/boot.120
	rm -rf /tmp/cdrom/boot
	./gujin /tmp/cdrom/boot.144 --full
	$(GENISOIMAGE) -R -b boot.144 /tmp/cdrom | $(CDRECORD) -tao -multi gracetime=0 -s -
	rm -f /tmp/cdrom/boot.144
	./gujin /tmp/cdrom/boot.288 --full
	$(GENISOIMAGE) -R -b boot.288 /tmp/cdrom | $(CDRECORD) -tao -multi gracetime=0 -s -
	rm -f /tmp/cdrom/boot.288
	./gujin /tmp/cdrom/cd16Mb --full --mbr -d=EBIOS:0x80,auto --geometry=32193,63,16,63,512,32256,0
	$(GENISOIMAGE) -R -hard-disk-boot -b cd16Mb /tmp/cdrom | $(CDRECORD) -tao -multi gracetime=0 -s -
	rm -f /tmp/cdrom/cd16Mb
	./gujin /tmp/cdrom/gujin.bcd
	$(GENISOIMAGE) -R -no-emul-boot -boot-load-size 4 -b gujin.bcd /tmp/cdrom | $(CDRECORD) -tao gracetime=0 -s -
	rm -rf /tmp/cdrom

# Test CD + BDI files to create a menu system of bootable floppies:
# We are simulating 320 Kbytes floppies in the CDROM boot_menu directory because we do not need more space,
# that is a made-up format not taking too many indexes in bootchain[] array.
# Directory names needs to be less than 16 chars long (--bootdir=).
# Just populate root directories boot_disk, boot_partition and boot_flash with bootable floppy images
# with filename using extension BDI. The size is not important, as long as the MBR of this image
# contains the correct description (./gujin -t can create whatever size you want).
# Create the floppy image from a real floppy by simply "cat /dev/fd0 > /tmp/floppy.bdi"
# By default, --stop_emulation=1 , that is stop emulation early.
# Note that in Gujin, pressing ESC calls INT 0x18 so fails the boot (BIOS shall try next boot device) .
CD_BDI_SETUP=--verbose=0 --probe_bios_floppy_disk=0 --probe_bios_hard_disk=0 --probe_ide_disk=0 \
	--search_disk_mbr=0 --search_part_mbr=0 --hide_unhide_partitions=0 --search_topdir_files=0 \
	--menu_with_disk=0 --menu_with_parttype=0 --menu_with_partition=0 --disk_write_enable=0

CD_BDI: gujin
	rm -rf /tmp/cdrom
	mkdir /tmp/cdrom
	- $(CDRECORD) blank=fast -s
	mkdir /tmp/cdrom/boot_menu
	./gujin /tmp/cdrom/gujin.bcd $(CD_BDI_SETUP) --search_el_torito=0 --full --bootdir=boot_menu
	mkdir /tmp/cdrom/boot_disk
	mkdir /tmp/cdrom/boot_partition
	mkdir /tmp/cdrom/boot_flash
	./gujin /tmp/cdrom/boot_menu/boot_disk.bdi     --geometry=floppy:120 $(CD_BDI_SETUP) --full --bootdir=boot_disk
	- [ -e /boot/hal91.img ] && cp /boot/hal91.img /tmp/cdrom/boot_disk/hal91.bdi		# replace by your own files
	./gujin /tmp/cdrom/boot_menu/boot_partition.bdi --geometry=floppy:120 $(CD_BDI_SETUP) --full --bootdir=boot_partition
	- [ -e /boot/rescue-0.4.1.img ] && cp /boot/rescue-0.4.1.img /tmp/cdrom/boot_partition/rescue-0.4.1.bdi	# replace by your own files
	./gujin /tmp/cdrom/boot_menu/boot_flash.bdi     --geometry=floppy:120 $(CD_BDI_SETUP) --full --bootdir=boot_flash
	- [ -e /boot/memtest* ] && cp /boot/memtest* /tmp/cdrom/boot_flash/memtest.bdi		# replace by your own files
	$(GENISOIMAGE) -R -no-emul-boot -boot-load-size 4 -b gujin.bcd /tmp/cdrom | $(CDRECORD) -tao gracetime=0 -

# Just autoload without menu the first vmlinuz/initrd or *.kgz
CDtiny: gujin
	rm -rf /tmp/cdrom
	mkdir /tmp/cdrom
	./gujin -t /tmp/cdrom/tiny.bcd --stop_emulation=0 --cmdline="root=LABEL=myCD"
	cp /boot/vmlinuz* /boot/initr* /boot/gujin.cmd /tmp/cdrom
	- $(CDRECORD) blank=fast
	$(GENISOIMAGE) -R -V myCD -no-emul-boot -boot-load-size 4 -b tiny.bcd /tmp/cdrom | $(CDRECORD) -tao -
	rm -rf /tmp/cdrom

CDtinyDBGl: gujin
	make tinycdrom_dbgl
	rm -rf /tmp/cdrom
	mkdir /tmp/cdrom
	./gujin tinycdrom_dbgl.bin /tmp/cdrom/tiny.bcd --stop_emulation=0 --cmdline="root=LABEL=myCD"
	cp /boot/vmlinuz* /boot/initr* /tmp/cdrom
	- $(CDRECORD) blank=fast
	$(GENISOIMAGE) -R -V myCD -no-emul-boot -boot-load-size 4 -b tiny.bcd /tmp/cdrom | $(CDRECORD) -tao -
	rm -rf /tmp/cdrom

CDtinyDBGd: gujin
	make tinycdrom_dbgd
	rm -rf /tmp/cdrom
	mkdir /tmp/cdrom
	./gujin tinycdrom_dbgd.bin /tmp/cdrom/tiny.bcd --stop_emulation=0 --cmdline="root=LABEL=myCD"
	cp /boot/vmlinuz* /boot/initr* /tmp/cdrom
	- $(CDRECORD) blank=fast
	$(GENISOIMAGE) -R -V myCD -no-emul-boot -boot-load-size 4 -b tiny.bcd /tmp/cdrom | $(CDRECORD) -tao -
	rm -rf /tmp/cdrom

CDtinyDBGf: gujin
	make tinycdrom_dbgf
	rm -rf /tmp/cdrom
	mkdir /tmp/cdrom
	./gujin tinycdrom_dbgf.bin /tmp/cdrom/tiny.bcd --stop_emulation=0 --cmdline="root=LABEL=myCD"
	cp /boot/vmlinuz* /boot/initr* /tmp/cdrom
	- $(CDRECORD) blank=fast
	$(GENISOIMAGE) -R -V myCD -no-emul-boot -boot-load-size 4 -b tiny.bcd /tmp/cdrom | $(CDRECORD) -tao -
	rm -rf /tmp/cdrom

##
## Standard images a bit smaller than 1Gb 2Gb 4Gb 8Gb USB keys:
##

USB1Gb.img: gujin
	./gujin $@ --geometry=1953125 --disk=BIOS:0x80,auto --cmdline="quiet"

USB2Gb.img: gujin
	./gujin $@ --geometry=3906250 --disk=BIOS:0x80,auto --cmdline="quiet"

USB4Gb.img: gujin
	./gujin $@ --geometry=7812500 --disk=BIOS:0x80,auto --cmdline="quiet"

USB8Gb.img: gujin
	./gujin $@ --geometry=15625000 --disk=BIOS:0x80,auto --cmdline="quiet"

##
## Images to test Gujin simulating disks with 1024/2048/4096 bytes per sectors
## using EBIOS as base disks, and Gujin loading itself from those disks:
## Nice to check that saving Gujin parameters and uninstall would work for them.
## --stop_emulation=2 is needed to save parameters, else disk has disappeared!
## Their write only flags (for user in e2fs) will change the result to writing them in Gujin...
boothdimg: gujin
	-rm /boot/hd2048bps1.img /boot/hd2048bps.img /boot/hd1024bps.img /boot/hd4096bps.img /boot/hd512bps.img
	# limit number of sector read by second fields (NbSectorpertrack != 0 && geo->heads == 0) - see instboot.c:
	# because Gujin will not retry a reading error with less sectors (32*2048 -> 128*512 sector reads generate BIOS error)
	./gujin /boot/hd2048bps1.img --geometry=2000,0,16,63,2048,2000,0,0,31,2048 --disk=EBIOS:0x80,auto --stop_emulation=2
	./gujin /boot/hd2048bps.img  --geometry=2000,0,0,0,2048,2000,0,0,31,2048 --disk=EBIOS:0x80,auto --stop_emulation=2
	./gujin /boot/hd4096bps.img  --geometry=2000,0,0,0,4096,2000,0,0,15,4096 --disk=EBIOS:0x80,auto --stop_emulation=2
	./gujin /boot/hd1024bps.img  --geometry=2000,0,0,0,1024,2000,0,0,63,1024 --disk=EBIOS:0x80,auto --stop_emulation=2
	./gujin /boot/hd512bps.img   --geometry=2000,0,0,0,512,2000,0,0,127,512 --disk=EBIOS:0x80,auto --stop_emulation=2

##
## Standalone KGZ utilities:
##
cleandisk.o:  cleandisk.c cleandisk.ld
	$(GCC) -m32 -Wall -Os -fomit-frame-pointer -mrtd -march=i386 -fno-gcse -fgcse-lm -fno-builtin \
		-fno-optimize-sibling-calls -fno-stack-protector -S cleandisk.c -o cleandisk.S
	@ echo "Greeping indirect calls through 32 bits register, there shall be none (lcallw replacement impossible):"
	@ !(grep 'call	\*%e' cleandisk.S) || echo "*** ERROR: the application will not work, did you remove -fno-gcse?"
	sed 's/call\t\(.*_far_.*\)/lcallw\t\1/;s/call\t\(*(%ed.)\)/lcallw\t\1/' < cleandisk.S > cleandisk.s
	as -32 cleandisk.s -o cleandisk.o

cleandisk_bin.kgz cleandisk_bin:	cleandisk.o gzcopy
	ld -T cleandisk_bin.ld -m elf_i386 -nostdlib -Map=cleandisk.map cleandisk.o -o cleandisk
	objcopy -O binary -R .note -R .comment -R .eh_frame -S cleandisk cleandisk.bin
	gzip -9 -f cleandisk.bin
	./gzcopy cleandisk.bin.gz -f cleandisk.kgz \
		-c="This software is distributed under the GPL license." \
		-al="Its source code is at: http://gujin.org" \
		-al="min_gujin_version=0x105 " \
		-as="realfct_size=`grep _gujin_size cleandisk.map | awk '{print $$1}'`"
	rm -f cleandisk.S cleandisk.s cleandisk.o cleandisk cleandisk.bin cleandisk.bin.gz
	rm -f cleandisk.map
	# rm -f cleandisk.kgz

cleandisk.kgz cleandisk:	cleandisk.o gzcopy
	ld -T cleandisk.ld -m elf_i386 -nostdlib -Map=cleandisk.map cleandisk.o -o cleandisk
	objcopy -R .note -R .comment -R .eh_frame -S cleandisk cleandisk.elf
	gzip -9 -f cleandisk.elf
	./gzcopy cleandisk.elf.gz -f cleandisk.kgz \
		-c="This software is distributed under the GPL license." \
		-al="Its source code is at: http://gujin.org" \
		-al="min_gujin_version=0x108"
	rm -f cleandisk.S cleandisk.s cleandisk.o cleandisk cleandisk.elf cleandisk.elf.gz
	rm -f cleandisk.map
	# rm -f cleandisk.kgz

cleandisk.lis:	cleandisk.o gzcopy
	ld -T cleandisk.ld -nostdlib -Map=cleandisk.map cleandisk.o -o cleandisk
	objcopy -R .note -R .comment -S cleandisk cleandisk.elf
	objdump -D -m i8086 cleandisk.elf > cleandisk.lis

# -Wa,-R can be replaced by -Wl,--omagic if needed:
hello_bios.kgz: hello_bios.c
	$(GCC) -m32 -Wall -Os -s -static -nostartfiles -nodefaultlibs -fno-stack-protector \
		-Wa,-R -Wl,--section-start,.init=0 \
		hello_bios.c -o hello_bios.elf
	gzip -9 hello_bios.elf -c > hello_bios.kgz

hello_32bits.kgz: hello_32bits.c
	$(GCC) -m32 -Wall -O2 -s -static -nostartfiles -nodefaultlibs -fno-stack-protector \
		-Wl,-Ttext=0x110000 hello_32bits.c -o hello_32bits.elf
	gzip -9 hello_32bits.elf -c > hello_32bits.kgz

hello_32bits_r.kgz: hello_32bits.c gzcopy
	$(GCC) -m32 -Wall -O2 -static -nostartfiles -nodefaultlibs -fno-stack-protector \
		-Wl,-Ttext=0x110000,--emit-relocs hello_32bits.c -o hello_32bits_r.elf
	gzip -9 hello_32bits_r.elf -c > hello_32bits_r.elf.gz
	./gzcopy hello_32bits_r.elf.gz -f hello_32bits_r.kgz -c="loadadr=0x210000"
# runadr is needed if version < 2.6
#	./gzcopy hello_32bits_r.elf.gz -f hello_32bits_r.kgz -c="loadadr=0x210000 runadr=0x210000"

hello_64bits.kgz: hello_64bits.c
	$(GCC64) -m64 -Wall -O2 -s -static -nostartfiles -nodefaultlibs -fno-stack-protector \
		-z max-page-size=0x1000 -Wl,-Ttext=0x110000 hello_64bits.c -o hello_64bits.elf
	gzip -9 hello_64bits.elf -c > hello_64bits.kgz

hello_64bits_r.kgz: hello_64bits.c gzcopy
	$(GCC64) -m64 -Wall -O2 -static -nostartfiles -nodefaultlibs -fno-stack-protector \
		-z max-page-size=0x1000 -Wl,-Ttext=0x110000,--emit-relocs hello_64bits.c -o hello_64bits_r.elf
	gzip -9 hello_64bits_r.elf -c > hello_64bits_r.elf.gz
	./gzcopy hello_64bits_r.elf.gz -f hello_64bits_r.kgz -c="loadadr=0x210000"
# runadr is needed if version < 2.6
#	./gzcopy hello_64bits_r.elf.gz -f hello_64bits_r.kgz -c="loadadr=0x210000 runadr=0x210000"

# maskvesa=0x10000: able to boot in text mode (only), only gcc >= 4.2
hello_gpl.kgz: hello_gpl.c gzcopy
	$(GCC) -m32 -Wall -O2 -s -static -nostartfiles -nodefaultlibs -fno-stack-protector \
		-fno-toplevel-reorder -Wl,--section-start,.init=0	\
		-Wl,-Ttext=0x110000 hello_gpl.c -o hello_gpl.elf
	gzip -9 hello_gpl.elf -c > hello_gpl.elf.gz
	./gzcopy hello_gpl.elf.gz -f hello_gpl.kgz \
		-c="This software is distributed under the GPL license." \
		-al="Its source code is at: http://gujin.org" \
		-al="maskvesa=0x10000" 

# test for bad and good:
hello_bad1.kgz: hello_gpl.c gzcopy
	$(GCC) -m32 -Wall -O2 -s -static -nostartfiles -nodefaultlibs -fno-stack-protector \
		-fno-toplevel-reorder -Wl,--section-start,.init=0	\
		-Wl,-Ttext=0x110000 hello_gpl.c -o hello_gpl.elf
	gzip -9 hello_gpl.elf -c > hello_gpl.elf.gz
	./gzcopy hello_gpl.elf.gz -f $@ \
		-c="This software is not used outside the copyright owner company." \
		-al="maskvesa=0x10000" 
	$(RM) hello_gpl.elf hello_gpl.elf.gz

hello_bad2.kgz: hello_gpl.c gzcopy
	$(GCC) -m32 -Wall -O2 -s -static -nostartfiles -nodefaultlibs -fno-stack-protector \
		-fno-toplevel-reorder -Wl,--section-start,.init=0	\
		-Wl,-Ttext=0x110000 hello_gpl.c -o hello_gpl.elf
	gzip -9 hello_gpl.elf -c > hello_gpl.elf.gz
	./gzcopy hello_gpl.elf.gz -f $@ \
		-c="This software is not distributed under the GPL license." \
		-al="Its source code is at: http://gujin.org" \
		-al="maskvesa=0x10000" 
	$(RM) hello_gpl.elf hello_gpl.elf.gz

hello_bad3.kgz: hello_gpl.c gzcopy
	$(GCC) -m32 -Wall -O2 -s -static -nostartfiles -nodefaultlibs -fno-stack-protector \
		-fno-toplevel-reorder -Wl,--section-start,.init=0	\
		-Wl,-Ttext=0x110000 hello_gpl.c -o hello_gpl.elf
	gzip -9 hello_gpl.elf -c > hello_gpl.elf.gz
	./gzcopy hello_gpl.elf.gz -f $@ \
		-c="This software is distributed under the GPL license." \
		-al="maskvesa=0x10000" 
	$(RM) hello_gpl.elf hello_gpl.elf.gz

hello_bad4.kgz: hello_gpl.c gzcopy
	$(GCC) -m32 -Wall -O2 -s -static -nostartfiles -nodefaultlibs -fno-stack-protector \
		-fno-toplevel-reorder -Wl,--section-start,.init=0	\
		-Wl,-Ttext=0x110000 hello_gpl.c -o hello_gpl.elf
	gzip -9 hello_gpl.elf -c > hello_gpl.elf.gz
	./gzcopy hello_gpl.elf.gz -f $@ \
		-c="This software has been authorised in writing by Etienne LORRAIN." \
		-al="NONGPL_productID=0x0001" \
		-al="maskvesa=0x10000" 
	$(RM) hello_gpl.elf hello_gpl.elf.gz

hello_good1.kgz: hello_gpl.c gzcopy
	$(GCC) -m32 -Wall -O2 -s -static -nostartfiles -nodefaultlibs -fno-stack-protector \
		-fno-toplevel-reorder -Wl,--section-start,.init=0	\
		-Wl,-Ttext=0x110000 hello_gpl.c -o hello_gpl.elf
	gzip -9 hello_gpl.elf -c > hello_gpl.elf.gz
	./gzcopy hello_gpl.elf.gz -f $@ \
		-c="This software is not used outside the copyright owner company." \
		-al="Copyright owner: Gujin Industries Limited" \
		-al="maskvesa=0x10000" 
	$(RM) hello_gpl.elf hello_gpl.elf.gz

hello_good2.kgz: hello_gpl.c gzcopy
	$(GCC) -m32 -Wall -O2 -s -static -nostartfiles -nodefaultlibs -fno-stack-protector \
		-fno-toplevel-reorder -Wl,--section-start,.init=0	\
		-Wl,-Ttext=0x110000 hello_gpl.c -o hello_gpl.elf
	gzip -9 hello_gpl.elf -c > hello_gpl.elf.gz
	./gzcopy hello_gpl.elf.gz -f $@ \
		-c="This software has been authorised in writing by Etienne LORRAIN." \
		-al="NONGPL_productID=0x0001" \
		-al="NONGPL_productNB=0x0001" \
		-al="maskvesa=0x10000" 
	$(RM) hello_gpl.elf hello_gpl.elf.gz

badgood:	hello_bad1.kgz hello_bad2.kgz hello_bad3.kgz hello_bad4.kgz hello_good1.kgz hello_good2.kgz
	cp hello_bad1.kgz hello_bad2.kgz hello_bad3.kgz hello_bad4.kgz hello_good1.kgz hello_good2.kgz /media/EL-BOOT-V28

##
## Standard targets:
##
clean:
	$(RM) gujin.tar.gz gujin.shar tiny_img.asm mingujin.asm floppy.144.gz USB?Gb.img
	$(RM) $(ALLSRCS:.c=.[sSio])
	$(RM) $(ALLSRCS:.c=.lis)
	$(RM) $(addsuffix ~,$(ALLFILES))
	$(RM) Makefile.tmp code16.h instboot gzcopy fntbin2h fnthex2h addhole showmap showmbr gujin-cmdline null.o gujin.8.gz
	$(RM) boot.bin boot.lnk gujin.com gujin.exe gujin.pic gujin.bcd boot.map boot.asm boot.elf
	$(RM) boot.360 boot.120 boot.720 boot.144 boot.168 boot.288 bf144.img.gz
	$(RM) cleandisk cleandisk.map cleandisk.S cleandisk.s cleandisk.o
	$(RM) cleandisk.elf cleandisk.bin cleandisk.elf.gz cleandisk.bin.gz
	$(RM) hello_bios.elf  hello_bios.kgz hello_32bits.elf hello_32bits.kgz hello_32bits_r.elf hello_32bits_r.elf.gz hello_32bits_r.kgz
	$(RM) hello_64bits.elf hello_64bits.kgz hello_64bits_r.elf hello_64bits_r.elf.gz hello_64bits_r.kgz hello_gpl.elf hello_gpl.elf.gz hello_gpl.kgz
	$(RM) hello_bad1.kgz hello_bad2.kgz hello_bad3.kgz hello_bad4.kgz hello_good1.kgz hello_good2.kgz
	$(RM) gzcp*	# results of testgzcopy when failure

# NOTE: make interprets $, to insert a true $ we should write $$
cleansource:
	@echo Cleaning $(ALLSRCS) $(ALLINCS) boot.ld
	$(foreach VAR, $(ALLSRCS) $(ALLINCS) boot.ld, $(shell	\
		sed '1,$$s/[$(tab)$(space)$(cr)]*$$//;		\
		     1,$$s/^\($(tab)*\)	        /\1$(tab)/g;	\
		     1,$$s/^        /$(tab)/g;			\
		     1,$$s/$(space)*$(tab)/$(tab)/g'		\
				< $(VAR) > $(VAR)~ ;		\
		$(MV) $(VAR)~ $(VAR) ))

cleanstandard:
	$(RM) -r ./install/ install.tar.gz standard.tar.gz cleandisk.kgz
	$(RM) full.img.gz   full.exe   full.asm
	$(RM) medium.img.gz medium.exe medium.asm
	$(RM) small.img.gz  small.exe  small.asm
	$(RM) myconf.img.gz myconf.exe myconf.asm
	$(RM) dbgvideo.exe dbgdisk.exe dbgload.exe dbgfs.exe dbgide.exe dbgmouse.exe
	$(RM) dbgdisk_screen.exe dbgdisk_screen.144
	$(RM) dbgload_screen.exe dbgload_screen.144
	$(RM) dbgfs_screen.exe dbgfs_screen.144
	$(RM) dbgvideo_com1.exe dbgvideo_com1.144
	$(RM) dbgdisk_com1.exe dbgdisk_com1.144 dbgdisk_com2.exe dbgdisk_com2.144
	$(RM) dbgload_com1.exe dbgload_com1.144 dbgload_com2.exe dbgload_com2.144
	$(RM) dbgfs_com1.exe dbgfs_com1.144 dbgfs_com2.exe dbgfs_com2.144
	$(RM) dbgmouse_com1.exe dbgmouse_com1.144
	$(RM) dbgserial.exe dbgserial.144
	$(RM) tiny_img.144 lotiny_img.144 tiny.144
	$(RM) tinyusb.bin lodtinyusb.bin tinyusb_alt.bin lodtinyusb_alt.bin tinyext4.bin
	$(RM) tinycdrom.bin tinycdrom_dbgl.bin tinycdrom_dbgd.bin tinycdrom_dbgf.bin
	$(RM) mingujin.exe fstiny.exe lotiny.exe lodtiny.exe ditiny.exe tiny_dbg.exe
	$(RM) boot2048 
	$(RM) f8x8.o f8x14.o f8x16.o null.o
	$(RM) -rf gujin_images gujin

proper: clean cleansource cleanstandard
	$(RM) log shortlog *.rpm *-stamp
	echo '##### Dependencies' >> Makefile # if it is not there...
	sed '/^[# ]*Dependencies/q' < Makefile > Makefile.tmp
	$(MV) Makefile.tmp Makefile
	chmod a-x install.txt gpg_sign.txt README.htm gujin.spec
	$(RM) -rf debian/gujin
	chmod a+x debian/rules

tar:
	- test -e ../oldgujin.tar.gz -a -e ../gujin.tar.gz && $(RM) ../oldgujin.tar.gz
	- test -e ../gujin.tar.gz && $(MV) ../gujin.tar.gz ../oldgujin.tar.gz
	rm -rf debian/gujin/*
	cd ..; tar -cvzf gujin.tar.gz gujin/debian gujin/pkg $(addprefix gujin/,$(ALLFILES))

gujin.tar.gz:
	tar -chzf gujin.tar.gz $(ALLFILES)

gujin.shar: gujin.tar.gz
	shar gujin.tar.gz > gujin.shar

#
# Terminal files (reduce time needed):
#
%.c:	   ;
%.h:	   ;
Makefile:  ;

#
# toolchain (GCC + binutils, i.e. as & ld) building:
#
toolchain:
	$(RM) -r ../toolchain
	mkdir ../toolchain
ifdef LAST_BINUTILS
	@echo :
	@echo : Rebuild the latest binutils version present in .. : $(LAST_BINUTILS)
	@echo :
	tar --directory ../toolchain -xf $(LAST_BINUTILS)
	( cd ../toolchain ;				\
	cd $(notdir $(basename $(basename $(LAST_BINUTILS)))) ;		\
	mkdir build ; cd build/ ;			\
	../configure --prefix=$(P_PWD)/toolchain ;	\
	make ; make install )
endif
ifdef LAST_GMP
	@echo
	@echo : Before rebuilding compiler, rebuild $(LAST_GMP)
	@echo
	tar --directory ../toolchain -xf $(LAST_GMP)
	( cd ../toolchain ;				\
	cd $(notdir $(basename $(basename $(subst core-,,$(LAST_GMP))))) ; \
	mkdir build ; cd build/ ;			\
	../configure --prefix=$(P_PWD)/toolchain ;	\
	make ; make install )
endif
ifdef LAST_MPFR
	@echo
	@echo : Before rebuilding compiler, rebuild $(LAST_MPFR)
	@echo
	tar --directory ../toolchain -xf $(LAST_MPFR)
	( cd ../toolchain ;				\
	cd $(notdir $(basename $(basename $(subst core-,,$(LAST_MPFR))))) ; \
	mkdir build ; cd build/ ;			\
	../configure --prefix=$(P_PWD)/toolchain --with-gmp=$(P_PWD)/toolchain ;	\
	make ; make install )
endif
ifdef LAST_MPC
	@echo
	@echo : Before rebuilding compiler, rebuild $(LAST_MPC)
	@echo
	tar --directory ../toolchain -xf $(LAST_MPC)
	( cd ../toolchain ;				\
	cd $(notdir $(basename $(basename $(subst core-,,$(LAST_MPC))))) ; \
	mkdir build ; cd build/ ;			\
	../configure --prefix=$(P_PWD)/toolchain --with-gmp=$(P_PWD)/toolchain ;	\
	make ; make install )
endif
ifdef LAST_GCC
	@echo : Rebuilding compiler $(LAST_GCC)
	@echo : If you have a fast enough PC, consider \"make bootstrap-lean\"
	@echo : else replace simply by \"make\".
	tar --directory ../toolchain -xf $(LAST_GCC)
ifdef LAST_GMP
	( cd ../toolchain ;						\
	cd $(notdir $(basename $(basename $(subst core-,,$(LAST_GCC))))) ; \
	mkdir build ; cd build/ ;					\
	echo "LD_PRELOAD for GCC-4.5.0" ; \
	LIBRARY_PATH=$(P_PWD)/toolchain/lib/ C_INCLUDE_PATH=$(P_PWD)/toolchain/include ../configure --prefix=$(P_PWD)/toolchain --enable-languages=c --with-gmp=$(P_PWD)/toolchain --disable-multilib --disable-threads --enable-tls ;	\
	LIBRARY_PATH=$(P_PWD)/toolchain/lib/ C_INCLUDE_PATH=$(P_PWD)/toolchain/include  make bootstrap ; make install )
else
	( cd ../toolchain ;						\
	cd $(notdir $(basename $(basename $(subst core-,,$(LAST_GCC))))) ; \
	mkdir build ; cd build/ ;					\
	../configure --prefix=$(P_PWD)/toolchain --enable-languages=c --disable-multilib --disable-threads --enable-tls ; \
	make bootstrap ; make install )
endif
endif

# Someone can tell me how to generate without any standard library?
newtoolchain:
	$(RM) -r ../toolchain
	mkdir ../toolchain ../toolchain/source ../toolchain/build
ifdef LAST_BINUTILS
ifneq (,$(findstring bz2,$(LAST_BINUTILS)))
	tar --directory ../toolchain -xjf $(LAST_BINUTILS)
else
	tar --directory ../toolchain -xzf $(LAST_BINUTILS)
endif
	mv ../toolchain/$(notdir $(basename $(basename $(LAST_BINUTILS)))) ../toolchain/source/binutils
endif
ifdef LAST_GMP
ifneq (,$(findstring bz2,$(LAST_GMP)))
	tar --directory ../toolchain -xjf $(LAST_GMP)
else
	tar --directory ../toolchain -xzf $(LAST_GMP)
endif
	mv ../toolchain/$(notdir $(basename $(basename $(LAST_GMP)))) ../toolchain/source/gmp
endif
ifdef LAST_MPFR
ifneq (,$(findstring bz2,$(LAST_MPFR)))
	tar --directory ../toolchain -xjf $(LAST_MPFR)
else
	tar --directory ../toolchain -xzf $(LAST_MPFR)
endif
	mv ../toolchain/$(notdir $(basename $(basename $(LAST_MPFR)))) ../toolchain/source/mpfr
endif
ifdef LAST_PPL
ifneq (,$(findstring bz2,$(LAST_PPL)))
	tar --directory ../toolchain -xjf $(LAST_PPL)
else
	tar --directory ../toolchain -xzf $(LAST_PPL)
endif
	mv ../toolchain/$(notdir $(basename $(basename $(LAST_PPL)))) ../toolchain/source/ppl
ifdef LAST_CLOOG_PPL
ifneq (,$(findstring bz2,$(LAST_CLOOG_PPL)))
	tar --directory ../toolchain -xjf $(LAST_CLOOG_PPL)
else
	tar --directory ../toolchain -xzf $(LAST_CLOOG_PPL)
endif
	#mv ../toolchain/$(notdir $(basename $(basename $(LAST_CLOOG_PPL)))) ../toolchain/source/cloog-ppl
	mv ../toolchain/cloog-ppl ../toolchain/source/cloog-ppl
endif
endif
ifdef LAST_GCC
ifneq (,$(findstring bz2,$(LAST_GCC)))
	tar --directory ../toolchain -xjf $(LAST_GCC)
else
	tar --directory ../toolchain -xzf $(LAST_GCC)
endif
	mv -f ../toolchain/$(notdir $(basename $(basename $(subst core-,,$(LAST_GCC)))))/* ../toolchain/source
	rmdir ../toolchain/$(notdir $(basename $(basename $(subst core-,,$(LAST_GCC)))))
endif
	# --with-build-libsubdir=$(P_PWD)/toolchain
	( cd ../toolchain/build ; \
	../source/configure --prefix=$(P_PWD)/toolchain --enable-languages=c --disable-multilib --disable-threads --enable-tls ; \
	make bootstrap ; make install )

#
# Various compilation tests:
#
# Those are the "supported" configurations -
# I may have forgoten a usefull config...
testdisk:
	make all DISK_SUPPORT="$(DISK_FULL)"
	make all DISK_SUPPORT="$(DISK_MIN)"
	make all DISK_SUPPORT="$(DISK_USB)"
	make all DISK_SUPPORT="$(DISK_EXT4)"
	make all DISK_SUPPORT="$(DISK_CDROM)"
	make all DISK_SUPPORT="$(DISK_FULL_SUPPORT)|E2FS_PROBE"
	make all DISK_SUPPORT="$(DISK_FULL_SUPPORT)|FAT12_PROBE"
	make all DISK_SUPPORT="$(DISK_FULL_SUPPORT)|FAT16_PROBE"
	make all DISK_SUPPORT="$(DISK_FULL_SUPPORT)|FAT12_PROBE|FAT16_PROBE"
	make all DISK_SUPPORT="$(DISK_FULL_SUPPORT)|E2FS_PROBE|FAT12_PROBE|FAT16_PROBE"
	make all DISK_SUPPORT="$(DISK_FULL_SUPPORT)|BOOTSECT_PROBE"
	make all DISK_SUPPORT="$(DISK_FULL_SUPPORT)|E2FS_PROBE|BOOTSECT_PROBE"
	make all DISK_SUPPORT="$(DISK_FULL_SUPPORT)|FAT12_PROBE|BOOTSECT_PROBE"
	make all DISK_SUPPORT="$(DISK_FULL_SUPPORT)|FAT16_PROBE|BOOTSECT_PROBE"
	make all DISK_SUPPORT="$(DISK_FULL_SUPPORT)|FAT12_PROBE|FAT16_PROBE|BOOTSECT_PROBE"
	make all DISK_SUPPORT="$(DISK_FULL_PROBE)|BIOS_SUPPORT"
	make all DISK_SUPPORT="$(DISK_FULL_PROBE)|EBIOS_SUPPORT"
	make all DISK_SUPPORT="$(DISK_FULL_PROBE)|IDE_SUPPORT"
	make all DISK_SUPPORT="$(DISK_FULL_PROBE)|DOS_SUPPORT"
	make all DISK_SUPPORT="$(DISK_FULL_PROBE)|BIOS_SUPPORT|EBIOS_SUPPORT"
	make all DISK_SUPPORT="$(DISK_FULL_PROBE)|BIOS_SUPPORT|IDE_SUPPORT"
	make all DISK_SUPPORT="$(DISK_FULL_PROBE)|BIOS_SUPPORT|IDE_SUPPORT|ATAPI_SUPPORT"
	make all DISK_SUPPORT="$(DISK_FULL_PROBE)|BIOS_SUPPORT|DOS_SUPPORT"
	make all DISK_SUPPORT="$(DISK_FULL_PROBE)|BIOS_SUPPORT|EBIOS_SUPPORT|IDE_SUPPORT"
	make all DISK_SUPPORT="$(DISK_FULL_PROBE)|BIOS_SUPPORT|EBIOS_SUPPORT|IDE_SUPPORT|ATAPI_SUPPORT"
	make all DISK_SUPPORT="$(DISK_FULL_PROBE)|BIOS_SUPPORT|EBIOS_SUPPORT|DOS_SUPPORT"
	make all DISK_SUPPORT="BIOS_SUPPORT|FAT12_PROBE|PROBE_ONLY_BIOSBOOTDEVICE"
	make all DISK_SUPPORT="BIOS_SUPPORT|FAT16_PROBE|PROBE_ONLY_BIOSBOOTDEVICE"
	make all DISK_SUPPORT="EBIOS_SUPPORT|FAT12_PROBE|PROBE_ONLY_BIOSBOOTDEVICE"
	make all DISK_SUPPORT="EBIOS_SUPPORT|FAT16_PROBE|PROBE_ONLY_BIOSBOOTDEVICE"
	make all DISK_SUPPORT="BIOS_SUPPORT|FAT16_PROBE|PROBE_ONLY_BIOSBOOTDEVICE|NO_PROBE_PARTITIONS"

testuser:
	make all USER_SUPPORT="VGA_SUPPORT"
	make all USER_SUPPORT="VGA_SUPPORT|VGA_EXTENDED"
	make all USER_SUPPORT="VGA_SUPPORT|VGA_MEMORY"
	make all USER_SUPPORT="VGA_SUPPORT|VGA_EXTENDED|VGA_MEMORY"
	make all USER_SUPPORT="VGA_SUPPORT|VGA_USEINT1013"
	make all USER_SUPPORT="VGA_SUPPORT|VGA_EXTENDED|VGA_USEINT1013"

	make all USER_SUPPORT="$(VESAMODE_ALL)|VESA_WINDOW"
	make all USER_SUPPORT="$(VESAMODE_ALL)|VESA_WINDOW|VESA_STRANGE_WINDOW"
	make all USER_SUPPORT="$(VESAMODE_ALL)|VESA_WINDOW|VESA_EDID"
	make all USER_SUPPORT="$(VESAMODE_ALL)|VESA_WINDOW|VESA_WINFUNCTION"
	make all USER_SUPPORT="$(VESAMODE_ALL)|VESA_WINDOW|VESA_WINFUNCTION|VESA_HARDWINDOW"
	make all USER_SUPPORT="$(VESAMODE_ALL)|VESA_WINDOW|VESA_2WINDOWS"
	make all USER_SUPPORT="$(VESAMODE_ALL)|VESA_WINDOW|VESA_WINFUNCTION|VESA_2WINDOWS"

	make all USER_SUPPORT="$(VESAMODE_ALL)|VESA_LINEAR"
	make all USER_SUPPORT="$(VESAMODE_ALL)|VESA_LINEAR|VESA_PMINFO"

	make all USER_SUPPORT="VESA_LINEAR|VESA_32BPP"
	make all USER_SUPPORT="VESA_LINEAR|VESA_24BPP"
	make all USER_SUPPORT="VESA_LINEAR|VESA_16BPP"
	make all USER_SUPPORT="VESA_LINEAR|VESA_8BPP"
	make all USER_SUPPORT="VESA_LINEAR|VESA_4BPP"
	make all USER_SUPPORT="VESA_LINEAR|VESA_1BPP"
	make all USER_SUPPORT="VESA_LINEAR|VESA_4BPP_EGA"
	make all USER_SUPPORT="VESA_LINEAR|VESA_4BPP_TEXT"
	make all USER_SUPPORT="VESA_LINEAR|VESA_32BPP|VESA_16BPP"
	make all USER_SUPPORT="VESA_LINEAR|VESA_32BPP|VESA_16BPP|VESA_24BPP"
	make all USER_SUPPORT="VESA_LINEAR|VESA_32BPP|VESA_16BPP|VESA_24BPP|VESA_8BPP"
	make all USER_SUPPORT="VESA_LINEAR|VESA_32BPP|VESA_16BPP|VESA_24BPP|VESA_8BPP|VESA_4BPP"
	make all USER_SUPPORT="VESA_LINEAR|VESA_32BPP|VESA_16BPP|VESA_24BPP|VESA_8BPP|VESA_4BPP|VESA_1BPP"
	make all USER_SUPPORT="VESA_LINEAR|VESA_32BPP|VESA_16BPP|VESA_24BPP|VESA_8BPP|VESA_4BPP|VESA_1BPP|VESA_4BPP_TEXT"
	make all USER_SUPPORT="VESA_LINEAR|VESA_32BPP|VESA_16BPP|VESA_24BPP|VESA_8BPP|VESA_4BPP|VESA_1BPP|VESA_4BPP_TEXT|VESA_4BPP_EGA"

	make all USER_SUPPORT="SERIAL_VT100"
	make all USER_SUPPORT="SERIAL_VT100|SERIAL_VT420"

	make all USER_SUPPORT="$(VGA_STD)|$(VESATYPE_STD)|$(VESAMODE_ALL)|$(SERIAL)"
	make all USER_SUPPORT="$(VGA_HARD)|$(VESATYPE_2)|$(VESAMODE_ALT)|$(SERIAL)"
	make all USER_SUPPORT="$(VGA_ALT)|$(VESATYPE_FEW)|$(VESAMODE_ALT)|$(SERIAL)"
	make all USER_SUPPORT="$(VGA_STD)|$(VESATYPE_STD)|$(VESAMODE_ALL)"
	make all USER_SUPPORT="$(VGA_STD)"
	make all USER_SUPPORT="$(VESATYPE_STD)|$(VESAMODE_ALL)"
	make all USER_SUPPORT="$(VESATYPE_STD)|$(VESAMODE_ALL)|$(SERIAL)"
	make all USER_SUPPORT="$(SERIAL)"

	make all USER_SUPPORT="$(VGA_STD)|BIOS_MOUSE_SUPPORT"
	make all USER_SUPPORT="$(VGA_STD)|SERIAL_MOUSE_SUPPORT"
	make all USER_SUPPORT="$(VGA_STD)|JOYSTICK_SUPPORT"
	make all USER_SUPPORT="$(VGA_STD)|BIOS_MOUSE_SUPPORT|SERIAL_MOUSE_SUPPORT"
	make all USER_SUPPORT="$(VGA_STD)|SERIAL_MOUSE_SUPPORT|JOYSTICK_SUPPORT"
	make all USER_SUPPORT="$(VGA_STD)|BIOS_MOUSE_SUPPORT|SERIAL_MOUSE_SUPPORT|JOYSTICK_SUPPORT"
	make all USER_SUPPORT="SERIAL_VT100|JOYSTICK_SUPPORT"

testappli:
	make all SETUP="ASSEMBLY_DSES|CODE_SEGMENT|XCODE_SEGMENT"
	make all SETUP="ASSEMBLY_DSES|CODE_SEGMENT|XCODE_SEGMENT|XDATA_SEGMENT"
	make all SETUP="ASSEMBLY_DSES|CODE_SEGMENT|XCODE_SEGMENT|UPDATE_BOOTPARAM"
	make all SETUP="ASSEMBLY_DSES|CODE_SEGMENT|XCODE_SEGMENT|UPDATE_BOOTPARAM|QUITE"
	make all SETUP="ASSEMBLY_DSES|CODE_SEGMENT|XCODE_SEGMENT|BIG_MALLOC|UPDATE_BOOTPARAM|BIOS_ONLY"

	make all SETUP="ASSEMBLY_RISC|CODE_SEGMENT|XCODE_SEGMENT"
	make all SETUP="ASSEMBLY_RISC|CODE_SEGMENT|XCODE_SEGMENT|XDATA_SEGMENT"
	make all SETUP="ASSEMBLY_RISC|CODE_SEGMENT|XCODE_SEGMENT|USE_INT1587"
	make all SETUP="ASSEMBLY_RISC|CODE_SEGMENT|XCODE_SEGMENT|UPDATE_BOOTPARAM"
	make all SETUP="ASSEMBLY_RISC|CODE_SEGMENT|XCODE_SEGMENT|BIG_MALLOC"

	make all SETUP="ASSEMBLY_NONE|CODE_SEGMENT|XCODE_SEGMENT"
	make all SETUP="ASSEMBLY_NONE|CODE_SEGMENT|XCODE_SEGMENT|XDATA_SEGMENT"
	make all SETUP="ASSEMBLY_NONE|CODE_SEGMENT|XCODE_SEGMENT|UPDATE_BOOTPARAM"
	make all SETUP="ASSEMBLY_NONE|CODE_SEGMENT|XCODE_SEGMENT|UPDATE_BOOTPARAM|QUITE"
	make all SETUP="ASSEMBLY_NONE|CODE_SEGMENT|XCODE_SEGMENT|BIG_MALLOC"

	make all SETUP="ASSEMBLY_CISC|CODE_SEGMENT|XCODE_SEGMENT"
	make all SETUP="ASSEMBLY_CISC|CODE_SEGMENT|XCODE_SEGMENT|XDATA_SEGMENT"
	make all SETUP="ASSEMBLY_CISC|CODE_SEGMENT|XCODE_SEGMENT|UPDATE_BOOTPARAM"
	make all SETUP="ASSEMBLY_CISC|CODE_SEGMENT|XCODE_SEGMENT|BIG_MALLOC"

testdebug:
	make all DEBUG=DEBUG_STACK
	make all DEBUG=DEBUG_ADDR
	make all DEBUG="DEBUG_STACK|DEBUG_ADDR"
	make all DEBUG="COM1|DEBUG_FS"	  USER_SUPPORT="$(USER_FEW)"
	make all DEBUG="COM4|DEBUG_DISK"  USER_SUPPORT="$(USER_FEW)" DISK_SUPPORT="$(DISK_FULL_SUPPORT)|E2FS_PROBE"
	make all DEBUG="LPT1|DEBUG_VIDEO"
	make all DEBUG="LPT2|DEBUG_SERIAL"
	make all DEBUG="LPT3|DEBUG_MOUSE"
	make all DEBUG="LPT4|DEBUG_LOADER"	USER_SUPPORT="$(USER_FEW)"
#	make all DEBUG="DOSFILE|DEBUG_DISK|DEBUG_VIDEO"
	make all DEBUG="SCREEN|DEBUG_ADDR|DEBUG_STACK"
	make all DEBUG=SCREEN

# to be used on a powerfull machine, like "make testall > log"
testall:
	echo 'make testdisk SETUP="ASSEMBLY_NONE|CODE_SEGMENT|XCODE_SEGMENT" DEBUG=0' 1>&2
	make testdisk SETUP="ASSEMBLY_NONE|CODE_SEGMENT|XCODE_SEGMENT" DEBUG=0

	echo 'make testuser SETUP="ASSEMBLY_NONE|CODE_SEGMENT|XCODE_SEGMENT"' 1>&2
	make testuser SETUP="ASSEMBLY_NONE|CODE_SEGMENT|XCODE_SEGMENT"

	echo make testappli USER_SUPPORT=0 1>&2
	make testappli USER_SUPPORT=0

	echo make testappli DEBUG=0 1>&2
	make testappli DEBUG=0

	echo 'make testdebug SETUP="ASSEMBLY_CISC|CODE_SEGMENT|XCODE_SEGMENT|XDATA_SEGMENT"' 1>&2
	make testdebug SETUP="ASSEMBLY_CISC|CODE_SEGMENT|XCODE_SEGMENT|XDATA_SEGMENT"

#### Dependencies
