1 # Makefile for GeekOS kernel, userspace, and tools
2 # Copyright (c) 2004,2005 David H. Hovemeyer <daveho@cs.umd.edu>
5 # This is free software. You are permitted to use,
6 # redistribute, and modify it as specified in the file "COPYING".
8 # Required software to build GeekOS:
9 # - GNU Make (http://www.gnu.org/software/make)
10 # - gcc 2.95.2 generating code for target (i386/ELF) and host platforms
11 # - nasm (http://nasm.sourceforge.net)
12 # - Perl5, AWK (any version), egrep
14 # Cygwin (http://cygwin.com) may be used to build GeekOS.
15 # Make sure that gcc, binutils, nasm, and perl are installed.
18 # - This makefile has been written carefully to work correctly
19 # with the -j (parallel make) option. I regularly use "make -j 2"
20 # to speed the build process on 2 processor systems.
22 # Base address of kernel
24 # Note: at top of memory minus three pages (GDT/TSS/IDT)
28 # Note that the code will initially load at 0x10000
30 # The setup code needs to copy it up to this address and jump there
32 KERNEL_BASE_ADDR := 0x100000
34 # Kernel entry point function
35 KERNEL_ENTRY = $(SYM_PFX)Main
39 VPATH := $(PROJECT_ROOT)/src
41 #when -DNDEBUG is set the kassert functions are disabled
43 ifeq ($(SERIAL_DEBUG), 1)
44 JRLDEBUG= -DDEBUG_SERIAL
49 # Figure out if we're compiling with cygwin, http://cygwin.com
50 SYSTEM_NAME := $(shell uname -s)
51 ifeq ($(findstring CYGWIN,$(SYSTEM_NAME)),CYGWIN)
53 EXTRA_C_OPTS := -DNEED_UNDERSCORE -DGNU_WIN32
54 EXTRA_NASM_OPTS := -DNEED_UNDERSCORE
56 EXTRA_CC_USER_OPTS := -Dmain=geekos_main
61 # ----------------------------------------------------------------------
63 # Various options specifying how GeekOS should be built,
64 # what source files to build, which user programs to build,
65 # etc. This is generally the only section of the makefile
66 # that will need to be modified.
67 # ----------------------------------------------------------------------
69 # List of targets to build by default.
70 # These targets encompass everything needed to boot
72 ALL_TARGETS := geekos/kernel.bin fd.img
76 KERNEL_C_SRCS := idt.c int.c trap.c irq.c io.c \
78 keyboard.c screen.c timer.c \
80 gdt.c tss.c segment.c \
89 # Kernel object files built from C source files
90 KERNEL_C_OBJS := $(KERNEL_C_SRCS:%.c=geekos/%.o)
92 # Kernel assembly files
93 KERNEL_ASM_SRCS := lowlevel.asm
97 # Kernel object files build from assembler source files
98 KERNEL_ASM_OBJS := $(KERNEL_ASM_SRCS:%.asm=geekos/%.o)
100 KERNEL_GAS_OBJS := $(KERNEL_GAS_SRCS:%.s=geekos/%.o)
103 # All kernel object files
104 KERNEL_OBJS := $(KERNEL_C_OBJS) \
105 $(KERNEL_ASM_OBJS) $(KERNEL_GAS_OBJS)
107 # Common library source files.
108 # This library is linked into both the kernel and user programs.
109 # It provides string functions and generic printf()-style
111 COMMON_C_SRCS := fmtout.c string.c memmove.c
113 # Common library object files.
114 COMMON_C_OBJS := $(COMMON_C_SRCS:%.c=common/%.o)
119 # ----------------------------------------------------------------------
121 # This section defines programs that are used to build GeekOS.
122 # ----------------------------------------------------------------------
124 # Uncomment if cross compiling
125 #TARGET_CC_PREFIX := i386-elf-
126 TARGET_CC_PREFIX := $(PROJECT_ROOT)/../../devtools/i386/bin/i386-elf-
128 # Target C compiler. gcc 2.95.2 or later should work.
129 TARGET_CC := $(TARGET_CC_PREFIX)gcc
132 # Host C compiler. This is used to compile programs to execute on
133 # the host platform, not the target (x86) platform. On x86/ELF
134 # systems, such as Linux and FreeBSD, it can generally be the same
135 # as the target C compiler.
138 # Target linker. GNU ld is probably to only one that will work.
139 TARGET_LD := $(TARGET_CC_PREFIX)ld -melf_i386
142 TARGET_AR := $(TARGET_CC_PREFIX)ar
145 TARGET_RANLIB := $(TARGET_CC_PREFIX)ranlib
148 TARGET_NM := $(TARGET_CC_PREFIX)nm
151 TARGET_OBJCOPY := $(TARGET_CC_PREFIX)objcopy
153 # Nasm (http://nasm.sourceforge.net)
154 NASM := $(PROJECT_ROOT)/../../devtools/bin/nasm
155 #NASM := /opt/vmm-tools/bin/nasm
159 # Tool to build PFAT filesystem images.
160 BUILDFAT := tools/builtFat.exe
165 # Pad a file so its size is a multiple of some unit (i.e., sector size)
166 PAD := $(PERL) $(PROJECT_ROOT)/scripts/pad
168 # Create a file filled with zeroes.
169 ZEROFILE := $(PERL) $(PROJECT_ROOT)/scripts/zerofile
171 # Calculate size of file in sectors
172 NUMSECS := $(PERL) $(PROJECT_ROOT)/scripts/numsecs
175 # ----------------------------------------------------------------------
177 # Options passed to the tools.
178 # ----------------------------------------------------------------------
180 # Flags used for all C source files
181 GENERAL_OPTS := -O -Wall $(EXTRA_C_OPTS) $(JRLDEBUG)
182 CC_GENERAL_OPTS := $(GENERAL_OPTS) -Werror
184 # Flags used for kernel C source files
185 CC_KERNEL_OPTS := -g -DGEEKOS -I$(PROJECT_ROOT)/include
187 # Flags user for kernel assembly files
188 NASM_KERNEL_OPTS := -I$(PROJECT_ROOT)/src/geekos/ -f elf $(EXTRA_NASM_OPTS)
190 # Flags used for common library and libc source files
191 CC_USER_OPTS := -I$(PROJECT_ROOT)/include -I$(PROJECT_ROOT)/include/libc \
192 $(EXTRA_CC_USER_OPTS)
194 # Flags passed to objcopy program (strip unnecessary sections from kernel.exe)
195 OBJCOPY_FLAGS := -R .dynamic -R .note -R .comment
197 # ----------------------------------------------------------------------
199 # Describes how to compile the source files.
200 # ----------------------------------------------------------------------
202 # Compilation of kernel C source files
204 geekos/%.o : geekos/%.c
205 $(TARGET_CC) -c $(CC_GENERAL_OPTS) $(CC_KERNEL_OPTS) $< -o geekos/$*.o
208 # Compilation of kernel assembly source files
209 geekos/%.o : geekos/%.asm
210 $(NASM) $(NASM_KERNEL_OPTS) $< -o geekos/$*.o
212 # Compilation of test VM
213 geekos/%.o : geekos/%.s
214 $(AS) $< -o geekos/$*.o
216 geekos/%.o : geekos/%.S
217 $(TARGET_CC) -c $(CC_GENERAL_OPTS) $(CC_KERNEL_OPTS) $< -o geekos/$*.o
219 # Compilation of common library C source files
220 common/%.o : common/%.c
221 $(TARGET_CC) -c $(CC_GENERAL_OPTS) $(CC_USER_OPTS) $< -o common/$*.o
223 # ----------------------------------------------------------------------
225 # Specifies files to be built
226 # ----------------------------------------------------------------------
228 # Default target - see definition of ALL_TARGETS in Configuration section
231 # Standard floppy image - just boots the kernel
232 fd.img : geekos/fd_boot.bin geekos/setup.bin geekos/kernel.bin
233 cat geekos/fd_boot.bin geekos/setup.bin geekos/kernel.bin > $@
239 $(PAD) guest.img 1474560
242 mkisofs -pad -b guest.img -R -o guest.iso guest.img
246 # Floppy boot sector (first stage boot loader).
247 geekos/fd_boot.bin : geekos/setup.bin geekos/kernel.bin $(PROJECT_ROOT)/src/geekos/fd_boot.asm
249 -I$(PROJECT_ROOT)/src/geekos/ \
250 -DNUM_SETUP_SECTORS=`$(NUMSECS) geekos/setup.bin` \
251 -DNUM_KERN_SECTORS=`$(NUMSECS) geekos/kernel.bin` \
252 $(PROJECT_ROOT)/src/geekos/fd_boot.asm \
255 # Setup program (second stage boot loader).
256 geekos/setup.bin : geekos/kernel.exe $(PROJECT_ROOT)/src/geekos/setup.asm
258 -I$(PROJECT_ROOT)/src/geekos/ \
259 -DENTRY_POINT=0x`egrep 'Main$$' geekos/kernel.syms |awk '{print $$1}'` \
260 $(PROJECT_ROOT)/src/geekos/setup.asm \
265 # Loadable (flat) kernel image.
266 geekos/kernel.bin : geekos/kernel.exe
267 $(TARGET_OBJCOPY) $(OBJCOPY_FLAGS) -S -O binary geekos/kernel.exe geekos/kernel.bin
270 # The kernel executable and symbol map.
271 geekos/kernel.exe : $(KERNEL_OBJS) $(COMMON_C_OBJS)
272 $(TARGET_LD) -o geekos/kernel.exe -Ttext $(KERNEL_BASE_ADDR) -e $(KERNEL_ENTRY) \
273 $(KERNEL_OBJS) $(COMMON_C_OBJS)
274 $(TARGET_NM) geekos/kernel.exe > geekos/kernel.syms
282 # Clean build directories of generated files
284 for d in geekos common libc user tools; do \
285 (cd $$d && rm -f *); \
289 # Build header file dependencies, so source files are recompiled when
290 # header files they depend on are modified.
291 depend : $(GENERATED_LIBC_SRCS)
292 $(TARGET_CC) -M $(CC_GENERAL_OPTS) $(CC_KERNEL_OPTS) \
293 $(KERNEL_C_SRCS:%.c=$(PROJECT_ROOT)/src/geekos/%.c) \
294 | $(PERL) -n -e 's,^(\S),geekos/$$1,;print' \
296 $(TARGET_CC) -M $(CC_GENERAL_OPTS) $(CC_USER_OPTS) \
297 $(COMMON_C_SRCS:%.c=$(PROJECT_ROOT)/src/common/%.c) \
298 | $(PERL) -n -e 's,^(\S),common/$$1,;print' \
301 # By default, there are no header file dependencies.