Skip to content

Commit

Permalink
Improve build system
Browse files Browse the repository at this point in the history
ARM target works.

Moved .gitignore from project root to build directories.
Makefile reads patterns specified in gitignore to find
all output files and remove them with the help of `find` and
`xargs`.
Do not overwrite dep info. Using `sed` to remove some dependency info.
See http://stackoverflow.com/a/9054420
and rust-lang/rust#5870.
Specified that makefiles should use bash.
  • Loading branch information
pczarn committed Mar 25, 2014
1 parent 7e4065b commit 2c32be6
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 45 deletions.
12 changes: 0 additions & 12 deletions .gitignore

This file was deleted.

5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ arch=x86
RUST_ROOT := /usr
LLVM_ROOT := /usr
GCC_PREFIX := /usr/bin/
SHELL := /bin/bash

export RUST_ROOT
export LLVM_ROOT
export GCC_PREFIX

all:
@$(MAKE) all -C arch/$(arch)/
@$(MAKE) all -C arch/$(arch)/ SHELL=$(SHELL)

%:
@$(MAKE) $* -C arch/$(arch)/
@$(MAKE) $* -C arch/$(arch)/ SHELL=$(SHELL)
33 changes: 19 additions & 14 deletions arch/arm/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ AS := $(GCC_PREFIX)as
LD := $(GCC_PREFIX)ld

LLC := $(LLVM_ROOT)/bin/llc
LLCFLAGS := -march=arm -mcpu=arm926ej-s --float-abi=hard -asm-verbose
LLCFLAGS := -mtriple=$(TARGET) -march=arm -mcpu=arm926ej-s --float-abi=hard -asm-verbose

GDB := $(GCC_PREFIX)gdb
OBJCOPY := $(GCC_PREFIX)objcopy
Expand All @@ -18,33 +18,38 @@ QEMU := qemu-system-arm
BDIR := ./boot
CORE_LIB := ../../rust-core/core/lib.rs
LCORE := $(BDIR)/$(shell $(RUSTC) --crate-file-name $(CORE_LIB))

OBJS := $(BDIR)/loader.o $(BDIR)/aeabi_runtime.o $(BDIR)/main.o $(BDIR)/core.o
LINK := $(BDIR)/linker.ld $(OBJS) $(BDIR)/initram.elf.embed

MODS := $(wildcard */*.rs) $(wildcard ../../kernel/*.rs) $(wildcard ../../kernel/*/*.rs) $(wildcard ../../kernel/*/*/*.rs)
DEP_RM := arch\/
DEP_KEEP := arch\/arm
DEP_SCRIPT := 's/\($(DEP_KEEP)\)/\n\1/g;s/ \S*$(DEP_RM)\S*//g;s/\n\($(DEP_KEEP)\)/\1/g'

-include ./config.mk
-include $(BDIR)/core.d
-include $(BDIR)/loader.d
-include $(BDIR)/main.d

.PHONY: all run debug clean

all: $(BDIR)/kernel.bin
@wc -c $^

# Library rust-core
$(LCORE):
$(RUSTC) $(RUSTCFLAGS) --dep-info $(@D)/core.d $(CORE_LIB) --emit=bc,link --out-dir $(BDIR)
$(LCORE) $(BDIR)/core.bc:
$(RUSTC) $(RUSTCFLAGS) $(CORE_LIB) --dep-info --emit=bc,link --out-dir $(BDIR)

# Compile rustboot
$(BDIR)/main.bc: ../../lib.rs $(MODS) $(LCORE)
$(RUSTC) $(RUSTCFLAGS) --dep-info $(@D)/main.d -L $(BDIR) --emit=bc ../../lib.rs --out-dir $(BDIR)
# Compile rustboot. Produce dependency info
$(BDIR)/main.bc: $(LCORE)
$(RUSTC) $(RUSTCFLAGS) -L $(BDIR) --dep-info --emit=bc ../../lib.rs --out-dir $(BDIR)
@sed -i $(DEP_SCRIPT) $(BDIR)/main.d

%.s: %.bc
$(LLC) $(LLCFLAGS) $^ -o $@

# Assemble object files. note: do not overwrite deps with -MD $*.d
%.o: %.s
$(AS) -MD $*.d -g $< -o $@
$(AS) -g $< -o $@

# initram
$(BDIR)/%.o: %.s
Expand All @@ -53,8 +58,8 @@ $(BDIR)/%.o: %.s
$(BDIR)/initram.elf: $(BDIR)/module.o
$(LD) -s -o $@ $^

%.embed: %
$(LD) -r -b binary -o $@ $^
$(BDIR)/%.embed: $(BDIR)/%
cd $(@D); $(LD) $(LDFLAGS) -r -b binary -o $(@F) $(<F)

# kernel (object)
$(BDIR)/kernel.elf: $(LINK)
Expand All @@ -65,8 +70,8 @@ $(BDIR)/kernel.bin: $(BDIR)/kernel.elf
$(OBJCOPY) -O binary $^ $@

# running
run: $(BDIR)/kernel.bin
$(QEMU) -M versatilepb -m 32M -nographic -kernel $^
run: all
$(QEMU) -M versatilepb -m 32M -nographic -kernel $(BDIR)/kernel.bin

debug: $(BDIR)/kernel.elf
ifeq ($(strip $(TMUX)),)
Expand All @@ -82,4 +87,4 @@ else
endif

clean:
rm -f $(BDIR)/*.{d,o,bc,rlib,so,ll,embed,elf,bin}
@cat $(BDIR)/.gitignore | xargs -I{} find $(BDIR) -name {} | xargs rm -f
11 changes: 10 additions & 1 deletion arch/arm/boot/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
core.s
*.d

main.s

lib*.rlib
*.bc

*.elf
*.o
*.embed
*.bin
33 changes: 18 additions & 15 deletions arch/x86/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,19 @@ BDIR := ./boot
CORE_LIB := ../../rust-core/core/lib.rs
LCORE := $(BDIR)/$(shell $(RUSTC) --crate-file-name $(CORE_LIB))
OBJS := $(BDIR)/loader.o $(BDIR)/main.o
DEPS := $(BDIR)/linker.ld $(OBJS) $(BDIR)/initram.elf.embed
LINK := "-(" $(LCORE) "-)"

# TODO: fix dep-info target
MODS := $(wildcard *.rs) $(wildcard */*.rs) $(wildcard ../../kernel/*.rs) $(wildcard ../../kernel/*/*.rs) $(wildcard ../../kernel/*/*/*.rs)
LINK := $(BDIR)/linker.ld $(OBJS) $(BDIR)/initram.elf.embed
LIBS := $(LCORE)

SECTIONS := .text .data .rodata

DEP_RM := arch\/
DEP_KEEP := arch\/i686\|arch\/common
DEP_SCRIPT := 's/\($(DEP_KEEP)\)/\n\1/g;s/ \S*$(DEP_RM)\S*//g;s/\n\($(DEP_KEEP)\)/\1/g'

-include ./config.mk
-include $(BDIR)/core.d
-include $(BDIR)/loader.d
-include $(BDIR)/main.d

.PHONY: all run debug clean

Expand All @@ -41,9 +43,10 @@ all: $(BDIR)/floppy.img
$(LCORE): $(CORE_LIB)
$(RUSTC) $(RUSTCFLAGS) --dep-info $(@D)/core.d $(CORE_LIB) --out-dir $(BDIR)

# Compile rustboot
$(BDIR)/main.bc: ../../lib.rs $(MODS) $(LCORE)
$(RUSTC) $(RUSTCFLAGS) --dep-info $(@D)/main.d -L $(BDIR) --emit bc ../../lib.rs --out-dir $(BDIR)
# Compile rustboot. Produce dependency info
$(BDIR)/main.bc: ../../lib.rs $(LCORE)
$(RUSTC) $(RUSTCFLAGS) --dep-info -L $(BDIR) --emit bc ../../lib.rs --out-dir $(BDIR)
@sed -i $(DEP_SCRIPT) $(BDIR)/main.d

%.o: %.bc
$(CC) $(CFLAGS) -c $^ -o $@
Expand All @@ -53,8 +56,8 @@ $(BDIR)/main.bc: ../../lib.rs $(MODS) $(LCORE)
$(ASM) $(ASMFLAGS) -MD $*.d -o $@ $<

# kernel (object)
$(BDIR)/kernel.elf: $(DEPS)
$(LD) $(LDFLAGS) -o $@ -T $^ $(LINK) -Map=./$(BDIR)/linker.map
$(BDIR)/kernel.elf: $(LINK)
$(LD) $(LDFLAGS) -o $@ -T $^ "-(" $(LIBS) "-)" -Map=./$(BDIR)/linker.map

# initram
$(BDIR)/%.o: %.asm
Expand All @@ -63,8 +66,8 @@ $(BDIR)/%.o: %.asm
$(BDIR)/initram.elf: $(BDIR)/module.o
$(LD) $(LDFLAGS) -s $< -o $@

%.embed: %
$(LD) $(LDFLAGS) -r -b binary -o $@ $^
$(BDIR)/%.embed: $(BDIR)/%
cd $(@D); $(LD) $(LDFLAGS) -r -b binary -o $(@F) $(<F)

# bootloader and kernel separately
$(BDIR)/kernel.bin: $(BDIR)/kernel.elf
Expand All @@ -78,8 +81,8 @@ $(BDIR)/floppy.img: $(BDIR)/boot.bin $(BDIR)/kernel.bin
cat $^ > $@

# running
run: boot/floppy.img
$(QEMU) -fda $<
run: all
$(QEMU) -fda $(BDIR)/floppy.img

debug: $(BDIR)/kernel.elf $(BDIR)/floppy.img
ifeq ($(strip $(TMUX)),)
Expand All @@ -93,4 +96,4 @@ else
endif

clean:
rm -f $(BDIR)/*.{d,o,bc,rlib,so,ll,embed,elf,bin,img,map}
@cat $(BDIR)/.gitignore | xargs -I{} find $(BDIR) -name {} | xargs rm -f
12 changes: 12 additions & 0 deletions arch/x86/boot/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
*.d
linker.map

lib*.rlib
*.bc

*.elf
*.o
*.o-*
*.embed
*.bin
*.img
3 changes: 2 additions & 1 deletion kernel/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ pub fn main() {
cpu::init();

drivers::init();
elf::exec(&_binary_boot_initram_elf_start);
elf::exec(&_binary_initram_elf_start);
extern { static _binary_initram_elf_start: u8; }
}

0 comments on commit 2c32be6

Please sign in to comment.