View of xos/Makefile


XOS | Parent Directory | View | Download

# Copyright (C) 2008  Emmanuel Varoquaux

# This file is part of XOS.

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# 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, see <http://www.gnu.org/licenses/>.

SHELL = /bin/sh

wants = $(strip $(filter 1 y yes,$(1)))
wants_hosted := $(call wants,$(HOSTED))
wants_static := $(call wants,$(STATIC))
wants_badblocks := $(call wants,$(BADBLOCKS))
wants_tests := $(call wants,$(TESTS))
wants_grub := $(call wants,$(GRUB))

INTERP = /lib/ld.so

CC = gcc
LD = ld
AR = ar
RM = rm -f
NASM = nasm
RANLIB = ranlib

CPPFLAGS =
KERNEL_CPPFLAGS = $(CPPFLAGS) -nostdinc
XOS_CPPFLAGS = $(KERNEL_CPPFLAGS) -Iinclude -Icommon/include
STARTUP_CPPFLAGS = $(KERNEL_CPPFLAGS) -Icommon/include
USR_CPPFLAGS = $(CPPFLAGS)
ifeq ($(wants_hosted),)
USR_CPPFLAGS += -nostdinc
endif
USR_LIB_CPPFLAGS = $(USR_CPPFLAGS)
ifeq ($(wants_hosted),)
USR_LIB_CPPFLAGS += -Iusr/include -Icommon/include
endif
USR_EXEC_CPPFLAGS = $(USR_CPPFLAGS)
ifeq ($(wants_hosted),)
USR_EXEC_CPPFLAGS += -isystem usr/include
endif
TESTS_CPPFLAGS = $(USR_CPPFLAGS) -isystem usr/include
TOOLS_CPPFLAGS = $(CPPFLAGS) -Icommon/include

CFLAGS = -Wstrict-overflow=1 -Wall -Wextra -Wno-unused-parameter -Wfloat-equal -Wdeclaration-after-statement -Wundef -Wshadow -Wcast-qual -Wcast-align -Wwrite-strings -Waggregate-return -Wredundant-decls -Werror -pipe
ifeq ($(wants_hosted),)
CFLAGS += -O2 -fno-strict-aliasing -fomit-frame-pointer
else
CFLAGS += -D_GNU_SOURCE -g
endif
KERNEL_CFLAGS = $(CFLAGS) -Wmissing-noreturn -ffreestanding
XOS_CFLAGS = $(KERNEL_CFLAGS)
STARTUP_CFLAGS = $(KERNEL_CFLAGS)
USR_CFLAGS = $(CFLAGS) -Wmissing-declarations
USR_LIB_CFLAGS = $(USR_CFLAGS)
ifeq ($(wants_hosted),)
USR_LIB_CFLAGS += -ffreestanding
endif
USR_EXEC_CFLAGS = $(USR_CFLAGS)
ifeq ($(wants_hosted),)
USR_EXEC_CFLAGS += -fno-builtin
endif
TESTS_CFLAGS = $(USR_CFLAGS) -fno-builtin
TOOLS_CFLAGS = $(CFLAGS) -Wmissing-declarations

ASFLAGS =
KERNEL_ASFLAGS = $(ASFLAGS)
XOS_ASFLAGS = $(KERNEL_ASFLAGS)
STARTUP_ASFLAGS = $(KERNEL_ASFLAGS)
USR_ASFLAGS = $(ASFLAGS)
USR_LIB_ASFLAGS = $(USR_ASFLAGS)
TESTS_ASFLAGS = $(USR_ASFLAGS)

LDFLAGS =
KERNEL_LDFLAGS = $(LDFLAGS) --fatal-warnings -s -x
XOS_LDFLAGS = $(KERNEL_LDFLAGS)
STARTUP_LDFLAGS = $(KERNEL_LDFLAGS)
USR_LDFLAGS = $(LDFLAGS) -Wl,--fatal-warnings -Wl,--hash-style=sysv
ifeq ($(wants_hosted),)
USR_LDFLAGS += -nostdlib -s
endif
USR_EXEC_LDFLAGS = $(USR_LDFLAGS)
ifeq ($(wants_hosted),)
ifeq ($(wants_static),)
USR_EXEC_LDFLAGS += -Wl,--dynamic-linker=$(INTERP)
endif
endif
USR_LIB_LDFLAGS = $(USR_LDFLAGS) -Wl,--warn-shared-textrel
TESTS_LDFLAGS = $(USR_LDFLAGS)
ifeq ($(wants_hosted),)
ifeq ($(wants_static),)
TESTS_LDFLAGS += -Wl,--dynamic-linker=$(INTERP)
endif
endif
TOOLS_LDFLAGS =

LOADLIBES =
TOOLS_LOADLIBES = $(LOADLIBES)

LDLIBS =
TOOLS_LDLIBS = $(LDLIBS)

ARFLAGS = rv
USR_ARFLAGS = $(ARFLAGS)
USR_LIB_ARFLAGS = $(USR_ARFLAGS)

# Composition des programmes et bibliotheques

tests := $(basename $(filter-out tests/common.c,$(wildcard tests/*.c tests/*.S)))

xos_init_objs := init/head.o init/main.o
xos_kernel_objs := kernel/it.o kernel/exceptions.o kernel/syscall.o kernel/sys_proc.o kernel/sys_io.o kernel/about.o kernel/reboot.o kernel/fork.o kernel/init.o kernel/usr.o kernel/exec.o kernel/elf.o kernel/exit.o kernel/current.o kernel/signal.o kernel/stop.o kernel/sleep.o kernel/status.o kernel/proc.o kernel/panic.o kernel/printk.o kernel/time.o kernel/string_table.o kernel/rw_lock.o kernel/mutex.o kernel/condition.o kernel/sched.o kernel/alloc_proc.o kernel/proc_queue.o kernel/idt.o kernel/gdt.o kernel/areas.o kernel/segment.o kernel/system.o kernel/debug.o kernel/sprintf.o kernel/string.o kernel/ctype.o
xos_fs_objs := fs/fd.o fs/file.o fs/fs.o fs/resolve.o fs/path.o fs/node.o fs/pipe.o fs/file_table.o fs/file_system_instance.o fs/ro_fs.o fs/rootfs.o fs/bin.o fs/devfs.o fs/procfs.o fs/vfat.o fs/vfat_instance.o fs/vfat_traverse.o fs/vfat_lock.o fs/vfat_cluster_set.o fs/vfat_fat_info.o fs/vfat_names.o fs/ramfs.o fs/pfs.o fs/buffer.o fs/device_manager.o fs/file_systems.o
xos_drivers_objs := drivers/rtc.o drivers/floppy.o drivers/floppy_motor.o drivers/dma.o drivers/console.o drivers/keyboard.o drivers/video.o drivers/vc.o drivers/vt.o drivers/tty.o drivers/tty_queue.o drivers/beep.o drivers/timer.o drivers/pic.o
xos_mm_objs := mm/vm.o mm/memory.o mm/page_fault.o mm/paging.o mm/pages.o mm/page.o mm/maps.o mm/map.o mm/mapping.o mm/kmalloc.o mm/slab.o mm/page_frame.o mm/highmem.o mm/lowmem.o
xos_objs := $(xos_init_objs) $(xos_kernel_objs) $(xos_fs_objs) $(xos_drivers_objs) $(xos_mm_objs) usr.o
startup_objs := startup/startup.o startup/unzip.o startup/inflate.o startup/crc.o startup/image.o

usr_crt1_objs := usr/lib/libc/crt-start.o
usr_crti_objs := usr/lib/libc/crt-init.o
usr_crtn_objs := usr/lib/libc/crt-fini.o
usr_Scrt1_objs := usr/lib/libc/shared-crt-start.o
usr_crtbegin_objs := usr/lib/libc/crt-begin.o
usr_crtbeginS_objs := usr/lib/libc/shared-crt-begin.o
usr_crtbeginT_objs := usr/lib/libc/crt-begin.o
usr_crtend_objs := usr/lib/libc/crt-end.o
usr_crtendS_objs := usr/lib/libc/shared-crt-end.o

usr_libc_xos_objs := usr/lib/libc/termios.o usr/lib/libc/unistd.o usr/lib/libc/exec.o usr/lib/libc/brk.o usr/lib/libc/wait.o usr/lib/libc/kill.o usr/lib/libc/utsname.o usr/lib/libc/fcntl.o usr/lib/libc/stat.o usr/lib/libc/mman.o usr/lib/libc/ioctl.o usr/lib/libc/mkdir.o usr/lib/libc/rename.o usr/lib/libc/mount.o usr/lib/libc/reboot.o usr/lib/libc/time.o usr/lib/libc/xos.o usr/lib/libc/errno.o
usr_libc_objs := usr/lib/libc/init.o usr/lib/libc/getopt.o usr/lib/libc/error.o usr/lib/libc/assert.o usr/lib/libc/exit.o usr/lib/libc/strftime.o usr/lib/libc/ctime.o usr/lib/libc/make_time.o usr/lib/libc/tzset.o usr/lib/libc/system.o usr/lib/libc/environ.o usr/lib/libc/signal.o usr/lib/libc/getcwd.o usr/lib/libc/printf.o usr/lib/libc/scanf.o usr/lib/libc/ctype.o usr/lib/libc/dirent.o usr/lib/libc/stdio.o usr/lib/libc/strtol.o usr/lib/libc/strings.o usr/lib/libc/string.o usr/lib/libc/locale.o usr/lib/libc/malloc.o usr/lib/libc/program_invocation_name.o usr/lib/libc/sys_errlist.o usr/lib/libc/sys_siglist.o $(usr_libc_xos_objs)
usr_libc_shared_objs := usr/lib/libc/shared-init.o usr/lib/libc/shared-start.o $(usr_libc_objs)
usr_libc_nonshared_objs := usr/lib/libc/nonshared-init-self.o
usr_libc_static_objs := usr/lib/libc/static-start.o $(usr_libc_objs) usr/lib/libc/static-init-self.o
usr_libc_ld_objs := usr/lib/libc/ld-init.o usr/lib/libc/getopt.o usr/lib/libc/error.o usr/lib/libc/exit.o usr/lib/libc/environ.o usr/lib/libc/signal.o usr/lib/libc/printf.o usr/lib/libc/ctype.o usr/lib/libc/stdio.o usr/lib/libc/string.o usr/lib/libc/ld-malloc.o usr/lib/libc/program_invocation_name.o usr/lib/libc/sys_errlist.o usr/lib/libc/sys_siglist.o $(usr_libc_xos_objs)
usr_ldso_objs := usr/lib/ld-so/start.o usr/lib/ld-so/ld.so.o usr/lib/ld-so/dynamic_link.o usr/lib/ld-so/bind.o usr/lib/ld-so/reloc.o usr/lib/ld-so/resolve.o usr/lib/ld-so/load.o usr/lib/ld-so/vars.o usr/lib/ld-so/initfini.o usr/lib/ld-so/object_reference.o usr/lib/ld-so/object_info.o usr/lib/ld-so/auxv.o usr/lib/ld-so/elf.o usr/lib/ld-so/xmalloc.o usr/lib/ld-so/error.o usr/lib/ld-so/sysvars.o $(usr_libc_ld_objs)
usr_libreadline_objs := usr/lib/readline/readline.o usr/lib/readline/localhist.o usr/lib/readline/history.o usr/lib/readline/kill.o usr/lib/readline/edit.o usr/lib/readline/input.o usr/lib/readline/output.o usr/lib/readline/tty.o usr/lib/readline/terminal.o usr/lib/readline/prompt.o usr/lib/readline/line.o usr/lib/readline/rlvars.o usr/lib/readline/xmalloc.o

usr_cat_objs := usr/utilities/cat.o usr/utilities/common.o
usr_cat_libs :=
usr_date_objs := usr/utilities/date.o usr/utilities/common.o
usr_date_libs :=
usr_echo_objs := usr/utilities/echo.o usr/utilities/common.o
usr_echo_libs :=
usr_false_objs := usr/utilities/false.o usr/utilities/common.o
usr_false_libs :=
usr_hello_objs := usr/hello/hello.o
usr_hello_libs :=
usr_ls_objs := usr/utilities/ls.o usr/utilities/common.o
usr_ls_libs :=
usr_more_objs := usr/more/more.o
usr_more_libs :=
usr_mount_objs := usr/mount/mount.o
usr_mount_libs :=
usr_ps_objs := usr/ps/ps.o
usr_ps_libs :=
usr_pwd_objs := usr/utilities/pwd.o usr/utilities/common.o
usr_pwd_libs :=
usr_sleep_objs := usr/utilities/sleep.o usr/utilities/common.o
usr_sleep_libs :=
usr_reboot_objs := usr/reboot/reboot.o
usr_reboot_libs :=
usr_reset_objs := usr/reset/reset.o
usr_reset_libs :=
usr_timer_objs := usr/timer/timer.o
usr_timer_libs :=
usr_true_objs := usr/utilities/true.o usr/utilities/common.o
usr_true_libs :=
usr_umount_objs := usr/umount/umount.o
usr_umount_libs :=
usr_uname_objs := usr/utilities/uname.o usr/utilities/common.o
usr_uname_libs :=
usr_xsh_builtins_objs := usr/xsh/builtins/builtins.o usr/xsh/builtins/bg.o usr/xsh/builtins/cd.o usr/xsh/builtins/exit.o usr/xsh/builtins/fg.o usr/xsh/builtins/help.o usr/xsh/builtins/history.o usr/xsh/builtins/jobs.o usr/xsh/builtins/kill.o usr/xsh/builtins/util.o usr/xsh/builtins/wait.o
usr_xsh_objs := usr/xsh/xsh.o usr/xsh/env.o usr/xsh/parse.o usr/xsh/execute_cmd.o usr/xsh/redir.o usr/xsh/print_cmd.o usr/xsh/free_cmd.o usr/xsh/lex.o $(usr_xsh_builtins_objs) usr/xsh/jobs.o usr/xsh/getc.o usr/xsh/tty.o usr/xsh/util.o usr/xsh/safe_malloc.o usr/xsh/error.o usr/xsh/vars.o
ifeq ($(wants_hosted),)
usr_xsh_libs := usr/lib/libreadline.a
else
usr_xsh_libs := /usr/lib/i386-linux-gnu/libreadline.a -lncurses
endif
tests_objs := $(addsuffix .o,$(tests))

tools_build_objs := tools/build.o tools/format.o tools/names.o tools/floppy.o tools/fat12.o
tools_makebin_objs := tools/makebin.o
tools_makeusrhdr_objs := tools/makeusrhdr.o

# Variables diverses
usr_crt_objs := usr/lib/crt1.o usr/lib/crti.o usr/lib/crtn.o usr/lib/Scrt1.o usr/lib/crtbegin.o usr/lib/crtbeginS.o usr/lib/crtbeginT.o usr/lib/crtend.o usr/lib/crtendS.o
usr_shared_libs := usr/lib/libc.so usr/lib/ld.so
usr_static_libs := usr/lib/libc_nonshared.a usr/lib/libc.a usr/lib/libreadline.a
ifeq ($(wants_hosted),)
usr_shared_prolog := usr/lib/crt1.o usr/lib/crti.o usr/lib/crtbegin.o
usr_shared_epilog := usr/lib/libc.so usr/lib/libc_nonshared.a usr/lib/crtend.o usr/lib/crtn.o
usr_static_prolog := usr/lib/crt1.o usr/lib/crti.o usr/lib/crtbeginT.o
usr_static_epilog := usr/lib/libc.a usr/lib/crtend.o usr/lib/crtn.o
ifeq ($(wants_static),)
usr_prolog := $(usr_shared_prolog)
usr_epilog := $(usr_shared_epilog)
else
usr_prolog := $(usr_static_prolog)
usr_epilog := $(usr_static_epilog)
endif
else
usr_prolog :=
usr_epilog :=
endif
usr_execs := usr/utilities/cat usr/utilities/date usr/utilities/echo usr/utilities/false usr/hello/hello usr/utilities/ls usr/more/more usr/mount/mount usr/ps/ps usr/utilities/pwd usr/utilities/sleep usr/reboot/reboot usr/reset/reset usr/timer/timer usr/utilities/true usr/umount/umount usr/utilities/uname usr/xsh/xsh
usr_libs := usr/lib/ld.so usr/lib/libc.so
tests_nolibc := tests/minimal
tests_others := $(filter-out $(tests_nolibc),$(tests))
tools_execs := tools/build tools/makebin tools/makeusrhdr

# Cibles finales
kernel_targets := xos System.map startup.bin setup.bin
usr_crt_targets := usr/lib/crt1.o usr/lib/crti.o usr/lib/crtn.o usr/lib/Scrt1.o usr/lib/crtbegin.o usr/lib/crtbeginS.o usr/lib/crtbeginT.o usr/lib/crtend.o usr/lib/crtendS.o
usr_lib_targets := usr/lib/libc.so usr/lib/libc_nonshared.a usr/lib/libc.a usr/lib/ld.so usr/lib/libreadline.a
usr_exec_targets := usr/utilities/cat usr/utilities/date usr/utilities/echo usr/utilities/false usr/hello/hello usr/utilities/ls usr/more/more usr/mount/mount usr/ps/ps usr/utilities/pwd usr/utilities/sleep usr/reboot/reboot usr/reset/reset usr/timer/timer usr/utilities/true usr/umount/umount usr/utilities/uname usr/xsh/xsh
tests_targets := $(tests)
tools_targets := tools/build tools/makebin tools/makeusrhdr
images_targets := bin.img lib.img usr.hdr usr.img usr.o xos.gz zxos boot.bin fd.img
ifeq ($(wants_hosted),)
targets := $(kernel_targets) $(usr_crt_targets) $(usr_lib_targets) $(usr_exec_targets) $(tests_targets) $(tools_targets) $(images_targets)
else
targets := $(usr_exec_targets)
endif

# Objets construits par ce Makefile
objs := $(xos_objs) $(startup_objs) $(usr_crt1_objs) $(usr_crti_objs) $(usr_crtn_objs) $(usr_Scrt1_objs) $(usr_crtbegin_objs) $(usr_crtbeginS_objs) $(usr_crtbeginT_objs) $(usr_crtend_objs) $(usr_crtendS_objs) $(usr_libc_shared_objs) $(usr_libc_nonshared_objs) $(usr_libc_static_objs) $(usr_ldso_objs) $(usr_libreadline_objs) $(usr_cat_objs) $(usr_date_objs) $(usr_echo_objs) $(usr_false_objs) $(usr_hello_objs) $(usr_ls_objs) $(usr_more_objs) $(usr_mount_objs) $(usr_ps_objs) $(usr_pwd_objs) $(usr_sleep_objs) $(usr_reboot_objs) $(usr_reset_objs) $(usr_timer_objs) $(usr_true_objs) $(usr_umount_objs) $(usr_uname_objs) $(usr_xsh_objs) $(tests_objs) $(tools_build_objs) $(tools_makebin_objs) $(tools_makeusrhdr_objs)

# Repertoires contenant des objets
dirs := $(sort $(foreach obj,$(objs),$(dir $(obj))))

# Divers

ifneq ($(wants_badblocks),)
BUILD = badblocks -b 512 /dev/fd0 | tools/build -b -
else
BUILD = tools/build
endif

ifneq ($(wants_tests),)
BUILDROOTDIR = tests/floppy
else
BUILDROOTDIR = $(wildcard floppy)
endif

# Cibles principales

.SUFFIXES:

.PHONY: all
all: $(targets)

.PHONY: real
real: clean all

.PHONY: floppy
floppy: fd.img
	dd if=$< of=/dev/fd0
	sync

.PHONY: clean
clean:
	-$(RM) -r tests/floppy
	-$(RM) *.log
	-$(RM) $(targets) $(foreach dir,$(dirs),$(dir)*.o $(dir)*.d)

# Noyau

xos System.map: $(xos_objs)
	$(LD) $(XOS_LDFLAGS) -M -Ttext 0x100000 --defsym kernel_start=0x100000 --oformat binary $^ -o $@ > System.map

startup.bin: $(startup_objs)
	$(LD) $(STARTUP_LDFLAGS) -Ttext 0x10000 --oformat binary $^ -o $@

setup.bin: boot/setup.asm
	$(NASM) -f bin $< -o $@

# C runtime

usr/lib/crt1.o: $(usr_crt1_objs)

usr/lib/crti.o: $(usr_crti_objs)

usr/lib/crtn.o: $(usr_crtn_objs)

usr/lib/Scrt1.o: $(usr_Scrt1_objs)

usr/lib/crtbegin.o: $(usr_crtbegin_objs)

usr/lib/crtbeginS.o: $(usr_crtbeginS_objs)

usr/lib/crtbeginT.o: $(usr_crtbeginT_objs)

usr/lib/crtend.o: $(usr_crtend_objs)

usr/lib/crtendS.o: $(usr_crtendS_objs)

$(usr_crt_objs):
	$(LD) -r -o $@ $^

# Bibliotheques

usr/lib/libc.so: usr/lib/crtbeginS.o $(usr_libc_shared_objs) usr/lib/crtendS.o

usr/lib/libc_nonshared.a: $(usr_libc_nonshared_objs)

usr/lib/libc.a: $(usr_libc_static_objs)

usr/lib/ld.so: $(usr_ldso_objs)

usr/lib/libreadline.a: $(usr_libreadline_objs)

$(usr_shared_libs):
	$(CC) -shared $(USR_LIB_LDFLAGS) -Wl,-soname=$(@F),--no-undefined $^ -o $@

$(usr_static_libs):
	$(AR) $(USR_LIB_ARFLAGS) $@ $?
	$(RANLIB) $@

# Programmes

$(usr_execs): $(usr_prolog)

usr/utilities/cat: $(usr_cat_objs) $(usr_cat_libs)

usr/utilities/date: $(usr_date_objs) $(usr_date_libs)

usr/utilities/echo: $(usr_echo_objs) $(usr_echo_libs)

usr/utilities/false: $(usr_false_objs) $(usr_false_libs)

usr/hello/hello: $(usr_hello_objs) $(usr_hello_libs)

usr/utilities/ls: $(usr_ls_objs) $(usr_ls_libs)

usr/more/more: $(usr_more_objs) $(usr_more_libs)

usr/mount/mount: $(usr_mount_objs) $(usr_mount_libs)

usr/ps/ps: $(usr_ps_objs) $(usr_ps_libs)

usr/utilities/pwd: $(usr_pwd_objs) $(usr_pwd_libs)

usr/utilities/sleep: $(usr_sleep_objs) $(usr_sleep_libs)

usr/reboot/reboot: $(usr_reboot_objs) $(usr_reboot_libs)

usr/reset/reset: $(usr_reset_objs) $(usr_reset_libs)

usr/timer/timer: $(usr_timer_objs) $(usr_timer_libs)

usr/utilities/true: $(usr_true_objs) $(usr_true_libs)

usr/umount/umount: $(usr_umount_objs) $(usr_umount_libs)

usr/xsh/xsh: $(usr_xsh_objs) $(usr_xsh_libs)

usr/utilities/uname: $(usr_uname_objs) $(usr_uname_libs)

$(usr_execs): $(usr_epilog)

$(usr_execs):
	$(CC) $(USR_EXEC_LDFLAGS) $^ -o $@

# Tests

$(tests_nolibc): %: %.o

$(tests_others): %: $(usr_prolog) %.o tests/common.o $(usr_epilog)

$(tests):
	$(CC) $(TESTS_LDFLAGS) $^ -o $@

# Outils

tools/build: $(tools_build_objs)

tools/makebin: $(tools_makebin_objs)

tools/makeusrhdr: $(tools_makeusrhdr_objs)

$(tools_execs):
	$(CC) $(TOOLS_LDFLAGS) $^ $(TOOLS_LOADLIBES) $(TOOLS_LDLIBS) -o $@

# Images

bin.img: tools/makebin $(usr_execs)
	tools/makebin $(usr_execs) > $@

lib.img: tools/makebin $(usr_libs)
	tools/makebin $(usr_libs) > $@

usr.hdr: tools/makeusrhdr bin.img lib.img
	tools/makeusrhdr bin.img lib.img > $@

usr.img: usr.hdr bin.img lib.img
	cat $^ > $@

usr.o: usr.img
	$(LD) -r $(XOS_LDFLAGS) -b binary --defsym _usr_start=_binary_usr_img_start --defsym _usr_end=_binary_usr_img_end $^ -o $@

xos.gz:

zxos: startup.bin xos.gz
	cat $^ > $@

boot.bin: boot/boot.asm setup.bin zxos
	$(NASM) -D SETUP_SECS=$$((($$(stat -c %s setup.bin)+511)>>9)) -D SYS_SECS=$$((($$(stat -c %s zxos)+511)>>9)) -f bin $< -o $@

ifneq ($(wants_grub),)
fd.img rebuild: xos
else
fd.img rebuild: tools/build boot.bin setup.bin zxos
endif

ifneq ($(wants_tests),)
fd.img rebuild: $(tests)
endif

.PHONY: rebuild
fd.img rebuild:
ifneq ($(wants_tests),)
ifneq ($(wildcard floppy),)
	cp -r floppy tests
else
	mkdir -p tests/floppy
endif
	cp $(tests) tests/floppy
endif
ifneq ($(wants_grub),)
	tools/build-grub.sh fd.img xos $(BUILDROOTDIR)
else
	$(BUILD) boot.bin setup.bin zxos $(BUILDROOTDIR) > fd.img
endif


# Objets et cibles generiques

tools/%.o: tools/%.c
	$(CC) $(TOOLS_CPPFLAGS) -MMD -MP $(TOOLS_CFLAGS) -c -o $@ $<

tests/%.o: tests/%.c
	$(CC) $(TESTS_CPPFLAGS) -MD -MP $(TESTS_CFLAGS) -c -o $@ $<

tests/%.o: tests/%.S
	$(CC) $(TESTS_CPPFLAGS) -MD -MP $(TESTS_ASFLAGS) -c -o $@ $<

usr/lib/libc/crt-begin.o usr/lib/libc/crt-end.o: %.o: %.c
	$(CC) $(USR_LIB_CPPFLAGS) -MD -MP $(USR_LIB_CFLAGS) -c -o $@ $<

usr/lib/%.o: usr/lib/%.c
	$(CC) $(USR_LIB_CPPFLAGS) -MD -MP $(USR_LIB_CFLAGS) -fpic -fvisibility=hidden -c -o $@ $<

usr/lib/%.o: usr/lib/%.S
	$(CC) $(USR_LIB_CPPFLAGS) -MD -MP $(USR_LIB_ASFLAGS) -c -o $@ $<

usr/%.o: usr/%.c
	$(CC) $(USR_EXEC_CPPFLAGS) -MD -MP $(USR_EXEC_CFLAGS) -c -o $@ $<

startup/%.o: startup/%.c
	$(CC) $(STARTUP_CPPFLAGS) -MD -MP $(STARTUP_CFLAGS) -c -o $@ $<

startup/%.o: startup/%.S
	$(CC) $(STARTUP_CPPFLAGS) -MD -MP $(STARTUP_ASFLAGS) -c -o $@ $<

%.o: %.c
	$(CC) $(XOS_CPPFLAGS) -MD -MP $(XOS_CFLAGS) -c -o $@ $<

%.o: %.S
	$(CC) $(XOS_CPPFLAGS) -MD -MP $(XOS_ASFLAGS) -c -o $@ $<

%.gz: %
	gzip -c $< > $@

# Inclusion des fichiers de dependances

-include $(objs:.o=.d)