Skip to content

Commit beea472

Browse files
committed
restructure into directories
- move src files into directories - adapt Makefile logic to the hierarchical structure
1 parent 755fa0f commit beea472

40 files changed

Lines changed: 87 additions & 63 deletions

Makefile

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,8 @@ TEST_BASE_LDFLAGS = -m32 -g -L$(BUILD)
1717
SANITIZER_FLAGS := -fsanitize=address,pointer-compare,pointer-subtract,leak,undefined -fno-sanitize=alignment
1818

1919
BUILD := build
20-
SRCS := stage1.asm stage2_entry.asm stage2.c print.c pci21.c pit.c mem.c serial.c gdbstub.c i386-stub.c idt.c uhci.c
21-
OBJS := $(foreach f,$(SRCS), $(BUILD)/$(basename $(notdir $(f))).o)
22-
DEPS := $(foreach f,$(SRCS), $(BUILD)/$(basename $(notdir $(f))).d)
2320
TARGET := $(BUILD)/usbloader.bin
21+
TARGET_IMG := $(BUILD)/usbloader.img
2422
TEST_TARGETS :=
2523

2624
ifeq ($(TEST_SAN), true)
@@ -33,9 +31,10 @@ endif
3331
# 1: target name
3432
# 2: souce file list
3533
define test_target
34+
TEST_BUILD_$(1) := $$(BUILD)/$(1).build
3635
TEST_SRCS_$(1) := $(2)
37-
TEST_OBJS_$(1) := $$(foreach f,$$(TEST_SRCS_$(1)),$(BUILD)/$$(basename $$(notdir $(1).$$(f))).o)
38-
TEST_DEPS_$(1) := $$(foreach f,$$(TEST_SRCS_$(1)),$(BUILD)/$$(basename $$(notdir $(1).$$(f))).d)
36+
TEST_OBJS_$(1) := $$(foreach f,$$(TEST_SRCS_$(1)),$$(TEST_BUILD_$(1))/$$(basename $$(f)).o)
37+
TEST_DEPS_$(1) := $$(foreach f,$$(TEST_SRCS_$(1)),$$(TEST_BUILD_$(1))/$$(basename $$(f)).d)
3938

4039
TEST_TARGETS += $(1)
4140

@@ -44,8 +43,9 @@ $(1): $$(BUILD)/$(1)
4443
$$(BUILD)/$(1): $$(TEST_OBJS_$(1))
4544
$$(TEST_LD) $$(TEST_BASE_LDFLAGS) $$(TEST_LDFLAGS) -o $$@ $$(TEST_OBJS_$(1))
4645

47-
$$(BUILD)/$(1).%.o: %.c $$(BUILD)/$(1).%.d | $$(BUILD)
48-
$$(TEST_CC) $$(TEST_BASE_CFLAGS) $$(TEST_CFLAGS) $$(CDEPFLAGS) -c $$< -o $$@
46+
$$(TEST_BUILD_$(1))/%.o: %.c $$(TEST_BUILD_$(1))/%.d
47+
@mkdir -p $$(@D)
48+
$$(TEST_CC) $$(TEST_BASE_CFLAGS) -I. $$(TEST_CFLAGS) $$(CDEPFLAGS) -c $$< -o $$@
4949

5050
$$(TEST_DEPS_$(1)):
5151

@@ -56,36 +56,43 @@ all: usbloader usbloader.img tests
5656

5757
usbloader: $(TARGET)
5858

59-
usbloader.img: $(TARGET)
60-
dd if=/dev/zero of=$(BUILD)/$@ bs=512 count=2880
61-
dd if=$(TARGET) of=$(BUILD)/$@ bs=512 conv=notrunc
59+
usbloader.img: $(TARGET_IMG)
6260

63-
$(TARGET): $(OBJS)
64-
$(CC) $(OBJS) $(BASE_LDFLAGS) $(LDFLAGS) -o $(BUILD)/usbloader.elf
65-
$(OBJCOPY) -O binary $(BUILD)/usbloader.elf $@
61+
$(TARGET_IMG): $(TARGET)
62+
dd if=/dev/zero of=$(TARGET_IMG) bs=512 count=2880
63+
dd if=$(TARGET) of=$(TARGET_IMG) bs=512 conv=notrunc
6664

67-
$(BUILD)/%.o: %.asm $(BUILD)/%.d | $(BUILD)
65+
$(BUILD)/%.o: %.asm $(BUILD)/%.d
6866
# NASM produces a dep (.d) file that is newer than the .o
6967
# and that causes unnecessary reassembly every time.
7068
# `touch`-ing the result after assembly to update the modification time
71-
$(NASM) $(BASE_NASMFLAGS) $(NASMFLAGS) $(NASMDEPFLAGS) $< -o $@
69+
@mkdir -p $(@D)
70+
$(NASM) $(BASE_NASMFLAGS) -I$(<D) $(NASMFLAGS) $(NASMDEPFLAGS) $< -o $@
7271
touch $@
7372

74-
$(BUILD)/%.o: %.c $(BUILD)/%.d | $(BUILD)
75-
$(CC) $(BASE_CFLAGS) $(CFLAGS) $(CDEPFLAGS) -c $< -o $@
76-
77-
$(BUILD):
78-
mkdir -p $@
73+
$(BUILD)/%.o: %.c $(BUILD)/%.d
74+
@mkdir -p $(@D)
75+
$(CC) $(BASE_CFLAGS) -I. $(CFLAGS) $(CDEPFLAGS) -c $< -o $@
7976

8077
clean:
8178
rm -rf $(BUILD)
8279

83-
$(DEPS):
80+
include arch/module.mk
81+
include boot/module.mk
82+
include drivers/module.mk
83+
include mem/module.mk
84+
include utils/module.mk
8485

85-
include $(wildcard $(DEPS))
86+
OBJS := $(foreach f,$(SRCS), $(BUILD)/$(basename $(f)).o)
87+
DEPS := $(foreach f,$(SRCS), $(BUILD)/$(basename $(f)).d)
8688

87-
$(eval $(call test_target,test_mem,unity.c mem_test.c mem.c))
89+
$(TARGET): $(OBJS)
90+
$(CC) $^ $(BASE_LDFLAGS) $(LDFLAGS) -o $(BUILD)/usbloader.elf
91+
$(OBJCOPY) -O binary $(BUILD)/usbloader.elf $@
92+
93+
$(DEPS):
94+
include $(wildcard $(DEPS))
8895

8996
tests: $(TEST_TARGETS)
9097

91-
.PHONY: all clean
98+
.PHONY: all clean usbloader usbloader.img tests

idt.c renamed to arch/idt.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include <stddef.h>
22

3+
#include "drivers/display/print.h"
34
#include "idt.h"
4-
#include "print.h"
55

66
#define MAX_ISR_CNT 48 // CPU exceptions + remaped PIC
77

File renamed without changes.

arch/module.mk

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
SRCS += arch/idt.c \
2+
arch/pit.c

pit.c renamed to arch/pit.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
#include <stddef.h>
33
#include <stdint.h>
44

5+
#include "drivers/io/io.h"
56
#include "idt.h"
6-
#include "io.h"
77
#include "pit.h"
88

99
// ========================================================
File renamed without changes.
File renamed without changes.

boot/module.mk

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
SRCS += boot/io.asm \
2+
boot/stage1.asm \
3+
boot/stage2_entry.asm \
4+
boot/stage2.c

stage1.asm renamed to boot/stage1.asm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ extern stage2_size
3838
push bx
3939
push dx
4040
mov al, %1
41-
PrintCharCom
41+
;PrintCharCom
4242
PrintCharBios
4343
pop dx
4444
pop bx
@@ -213,7 +213,7 @@ LogString:
213213
lodsb
214214
or al, al
215215
jz .done
216-
PrintCharCom
216+
;PrintCharCom
217217
PrintCharBios
218218
jmp .loop
219219

stage2.c renamed to boot/stage2.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
#include <stdint.h>
22

3-
#include "gdbstub.h"
4-
#include "mem.h"
5-
#include "pci21.h"
6-
#include "pit.h"
7-
#include "print.h"
8-
#include "serial.h"
9-
#include "uhci.h"
3+
#include "arch/pit.h"
4+
#include "drivers/display/print.h"
5+
#include "drivers/pci/pci21.h"
6+
#include "drivers/serial/serial.h"
7+
#include "drivers/usb/uhci.h"
8+
#include "mem/mem.h"
9+
#include "utils/gdbstub.h"
1010

1111
void stage2_main(void) {
1212
init_output();

0 commit comments

Comments
 (0)