Please explain this makefile

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
jason7007

Please explain this makefile

Post by jason7007 »

Hi Gurus,
I am very new to OS dev, and very curious about building my own os.
Recently I was studying the code Christopher Giese on his Cosmos 10 OS but I got stucked on his makefile, I could not understand it. I already learned about DJGPP and Nasm.
Can somebody teach me or explain this Makefile line by line to me.
I would appreciate it so much.
Thank u very much in advance.


# defines
.SUFFIXES: .asm .x .exe
MAKEFILE=makefile
# uncomment the next line to rebuild everything if this makefile changes
#MAKEDEP=$(MAKEFILE)
# chose ELF, COFF, or PE in the next two lines
LSCRIPT   =ldscript/coffkrnl.ld
INCDIR   =inc
AFLAGS   =-f coff -dUNDERBARS=1 -i$(INCDIR)/
CFLAGS   =-g -Wall -W -O2 -I$(INCDIR) -fno-builtin -nostdinc
LFLAGS   =-g -T$(LSCRIPT)
LIBC   =tinylib/libc.a
OBJS   =krnl/kstart.o krnl/main.o krnl/video.o krnl/kbd.o krnl/loader.o \
   krnl/time.o krnl/paging.o krnl/syscalls.o krnl/debug.o krnl/mm.o \
   krnl/tasks.o
DELETE   =deltree /y

# targets
all:   krnl.dsk

clean:
   $(DELETE) *.dsk *.lst *.sym *.x *.com krnl\*.o util\*.exe

realclean:
   make -f $(MAKEFILE) clean
   make -C tinylib -f $(MAKEFILE) clean
   make -C apps -f $(MAKEFILE) clean

# implicit rules
.c.exe:
   gcc -s -O2 -o$@ $<

.c.o:
   gcc $(CFLAGS) -c -o$@ $<

.asm.o:
   nasm $(AFLAGS) -o$@ $<

# dependencies
util/rdsk.exe: util/rdsk.c $(MAKEDEP)

krnl/kstart.o: krnl/kstart.asm $(MAKEDEP)

krnl/main.o: krnl/main.c $(MAKEDEP)

krnl/video.o: krnl/video.c $(MAKEDEP)

krnl/kbd.o: krnl/kbd.c $(MAKEDEP)

krnl/loader.o: krnl/loader.c $(MAKEDEP)

krnl/time.o: krnl/time.c $(MAKEDEP)

krnl/paging.o: krnl/paging.c $(MAKEDEP)

krnl/mm.o: krnl/mm.c $(MAKEDEP)

krnl/tasks.o: krnl/tasks.c $(MAKEDEP)

# explicit rules
load.com: load.asm $(MAKEDEP)
   nasm -f bin -o$@ $<

$(LIBC): tinylib/$(MAKEFILE)
   make -C tinylib -f $(MAKEFILE)

krnl.x: $(LIBC) $(OBJS) $(LSCRIPT) $(MAKEDEP)
   ld $(LFLAGS) -o$@ $(OBJS) $(LIBC)
   objdump --line-numbers --source $@ >krnl.lst
   nm --line-numbers $@ | sort >krnl.sym
   strip $@

krnl.dsk: util/rdsk.exe krnl.x $(MAKEDEP)
   make -C apps -f $(MAKEFILE)
   echo krnl.x >x
   echo apps/hello.x >>x
   echo apps/echo.x >>x
   echo apps/time.x >>x
   echo apps/tetris.x >>x
   echo apps/tetris.x >>x
   echo apps/invade.x >>x
   echo apps/protect.x >>x
   echo apps/protect.x >>x
   echo apps/protect.x >>x
   echo apps/protect.x >>x
   echo apps/protect.x >>x
#   echo apps/protect.x >>x
   util\rdsk.exe -o $@ @x
   del x
AR

Re:Please explain this makefile

Post by AR »

Which particular part do you want explained?

The "XYZ = ABC" creates an environment variable named "XYZ" and stores "ABC" in it. "$(XYZ)" returns the contents of the environment variable.

"clean:" declares a make target (eg. command line "C:\>make clean"), the things after the colon on the same line are "dependencies". Dependencies are built before the object in question (you usually have an "prog.exe: $(BUILDOBJS)" where BUILDOBJS declares all the object files required to construct the binary), you can also have recursive targets (The ".c.o" things) which basically mean "if the file has extension X then do this to make it into extension Y" (eg. "make C into O").

You can also write recursive makefiles (The further "make" commands within the makefile) but I won't go into the details of that for the moment.

[This was essentially a crash course, feel free to ask more specific questions]
jason7007

Re:Please explain this makefile

Post by jason7007 »

Where did you get this knowledge? from DJGPP manuals or DOS batching manual?

Thank you any way.
I hope more explanation on other lines.
Post Reply