Question
I am reading the code of xv6, and find it hard to read the Makefile. Could you tell me how the following statements work:
1. "CFLAGS += $(shell $(CC) -fno-stack-protector -E -x c /dev/null >/dev/null 2>&1 && echo -fno-stack-protector)"
2. "LDFLAGS += -m $(shell $(LD) -V | grep elf_i386 2>/dev/null)"
3. "xv6.img:
bootblock kernel fs.img
dd if=/dev/zero of=xv6.img count=10000
dd if=bootblock of=xv6.img conv=notrunc
dd if=kernel of=xv6.img seek=1 conv=notrunc"
And how to learn Makefile in details? Could you recommand some good books?
Thank you!
Solution
CFLAGS
are the options ( like-fno-stack-protector
-E
etc ) you pass to your compilerCC
.$(CC)
will be replaced by the actual compiler. ieCC
should be initialized before this with something likeset CC=gcc
.LDFLAGS
are the options ( to your linkerLD
.+=
is just like your+=
operator in C. It updates to the already existing value ofCFLAGS
andLDFLAGS
This line means that
xv6.img
is dependent onbootblock
kernel
fs.img
. That is we are tellingmake
that, in order to buildxv6.img
, you need to buildbootblock
kernel
andfs.img
first
You can learn about dd command here
Here is the complete encyclopedic guide to make
and Makefiles
OTHER TIPS
To add to the answer, the dd
command acts like a copy command essentially taking an input file[if] and copying it's contents to the output file [of]. Count is an indicator of the number of the blocks that are to be copied.
The code provided by you relates with the building of the xv6.img file which contains the bootloader, file system and the kernel. The code is essentially copying 10000 blocks of zeros from the /dev/zero file into xv6.img. This is followed by copying from the bootblock(created by bootasm.S and bootmain.c as seen by the Makefile target) into sector 0. This is followed by seeking one sector and then copying the kernel into the image file.