11
\$\begingroup\$

I'm now posting the second semi-complete part of my operating system makefile. The kernel itself is currently incomplete, so don't be surprised by the small size of the makefile. When the kernel expands, it will follow suit.

OFLAGS = -Ofast
DEBUGFLAGS = -g -g3 -ggdb3
TARGET = i686-elf
CC = gcc
INCDIR = -I kernel/include/
KERNEL = opal101
CFLAGS = -ffreestanding -Wall -Wextra -fno-exceptions -funsigned-char
LDFLAGS = -T linker.ld -Ofast -nostdlib -lgcc
AS = as
VPATH = kernel/arch/$(TARGET) $(shell find . -type d ! -name "arch"*)
include kernel/arch/$(TARGET)/Makefile
include kernel/drivers/vga/Makefile
include kernel/lib/Makefile
OBJECTS = $(C_OBJECTS) $(ASM_OBJECTS)
.PHONY: help all clean iso run debug
help:
 @echo "make: Usage: make [target]";\
 echo "Targets [run | iso | all | cleani | debug]"
debug: CFLAGS:= $(CFLAGS) $(DEBUGFLAGS)
debug: all
 qemu-system-i386 --kernel $(KERNEL).bin -S -s& \
 gdb-multiarch -s $(KERNEL).bin
run:iso
 qemu-system-i386 -cdrom $(KERNEL).iso
iso: CFLAGS := $(CFLAGS) $(OFLAGS)
iso: all
 mkdir -p $(KERNEL)/boot/grub;\
 cp kernel/arch/i686-elf/stage2_eltorito $(KERNEL)/boot/grub;\
 cp $(KERNEL).bin $(KERNEL)/boot;\
 printf "default 0\n" >>menu.lst;\
 printf "title %s\n" $(KERNEL) >>menu.lst;\
 printf "kernel /boot/%s.bin" $(KERNEL) >>menu.lst;\
 mv menu.lst $(KERNEL)/boot/grub;\
 genisoimage -R -b boot/grub/stage2_eltorito -no-emul-boot \
 -boot-load-size 4 -boot-info-table -o $(KERNEL).iso $(KERNEL)
all: $(KERNEL).bin
$(KERNEL).bin:$(OBJECTS)
 $(TARGET)-$(CC) $(CFLAGS) $(LDFLAGS) $(OBJECTS) -o $(KERNEL).bin
$(C_OBJECTS):%.o:%.c
 $(TARGET)-$(CC) $(CFLAGS) -c $< -o $@ $(INCDIR)
$(ASM_OBJECTS): %.o:%.s
 $(TARGET)-$(AS) $< -o $@
clean:
 rm -rf *.o *.bin *.iso opal101/

I will show the kernel/lib/Makefile. All the rest are almost the same.

C_OBJECTS := $(C_OBJECTS) string.o
string.o:string.h

NOTE: I am not willing to use auto dependency generators because I found them very complex to learn and use, so please don't ask me to use them.

T145
3,1492 gold badges23 silver badges44 bronze badges
asked Sep 25, 2015 at 20:28
\$\endgroup\$
0

1 Answer 1

3
+50
\$\begingroup\$

Disclaimer: I'm not experienced with this field, but this question definitely needs some activity, so this is my attempt at an answer. If there's anything that you want me to cover that I don't mention here, just tell me about it and I'll try my best to see what I can do.


Style/nitpicks

I find that some of your rule indentation is a bit excessive, and makes your code a little harder to read. Right now you're indenting your rules by tabs with a width of eight spaces. I'd recommend that you indent by tabs with a width of four, like this:

some_rule : file.c
 do_something file.c -foo -bar

You also seem to have a lack of blank lines between your rules as well. I'd highly recommend including at least one blank line between each rule, and a comment, if necessary:

# ...
some_rule1 : file.c
 ...
some_rule2 : file.c
 ...

Finally, you're inconsistent in your colon (:) placement in some places. For example, sometimes you place your colons like this:

all: $(KERNEL).bin

Other times, you place them like this:

$(C_OBJECTS):%.o:%.c

I prefer the following style, but as long as you're, again, consistent, any of the above or below are fine:

some_rule : ...
answered Oct 4, 2015 at 22:12
\$\endgroup\$
3
  • \$\begingroup\$ As I recall, Makefiles expect your rules to be indented with a tab - I don't recall with how many, but I know the markdown editor will replace the tabs with spaces. That might be what you're seeing. \$\endgroup\$ Commented Oct 4, 2015 at 22:21
  • \$\begingroup\$ @Dannnno Ah. Edited. \$\endgroup\$ Commented Oct 4, 2015 at 22:22
  • \$\begingroup\$ well I think nothing is wrong but with style thank you \$\endgroup\$ Commented Oct 5, 2015 at 5:33

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.