usr.bin: add usr.bin

Signed-off-by: Michael Shaw <m@michaelsshaw.com>
This commit is contained in:
Michael Shaw 2026-07-18 15:28:53 -04:00
commit f47a4bfa2c
8 changed files with 87 additions and 11 deletions

View file

@ -21,6 +21,8 @@ LD := $(ARCH)-elf-ld
CFLAGS := -g3 -O0 -pipe -Wall CFLAGS := -g3 -O0 -pipe -Wall
INITRAMFS := initramfs.cpio
CFLAGS += \ CFLAGS += \
-std=gnu99 \ -std=gnu99 \
-ffreestanding \ -ffreestanding \
@ -69,34 +71,38 @@ export LD
export QEMUFLAGS export QEMUFLAGS
export CFLAGS export CFLAGS
export LDFLAGS export LDFLAGS
export INITRAMFS
PHONY: default PHONY: default
default: default:
$(Q)compiledb -n make $(KERNEL) $(Q)compiledb -n $(MAKE) --no-print-directory $(KERNEL)
$(Q)make $(KERNEL) $(Q)$(MAKE) --no-print-directory $(KERNEL)
.PHONY: all .PHONY: all
all: $(KERNEL) all: $(KERNEL)
$(Q)compiledb -n make $(Q)compiledb -n $(MAKE) --no-print-directory
.PHONY: __build_all .PHONY: __build_all
__build_all: __build_all:
$(Q)$(MAKE) --no-print-directory -f scripts/Makefile.build _all obj=. $(Q)$(MAKE) --no-print-directory -f scripts/Makefile.build _all obj=.
$(Q)$(MAKE) --no-print-directory -C usr.bin
$(KOBJ): __build_all $(KOBJ): __build_all
$(KERNEL): $(KOBJ) built-in.o linker.ld $(KERNEL): $(KOBJ) linker.ld
$(Q)echo " LD $@" $(Q)echo " LD $@"
$(Q)$(LD) $(KOBJ) $(LDFLAGS) -T linker.ld -o $@ $(Q)$(LD) $(KOBJ) $(LDFLAGS) -T linker.ld -o $@
.PHONY: clean .PHONY: clean
clean: clean:
$(Q)$(MAKE) -f scripts/Makefile.build _clean obj=. $(Q)$(MAKE) --no-print-directory -f scripts/Makefile.build _clean obj=.
$(Q)$(MAKE) --no-print-directory -C usr.bin clean
$(Q)rm -rf $(IMG) $(Q)rm -rf $(IMG)
$(Q)rm -rf iso_root $(Q)rm -rf iso_root
$(Q)rm -rf os_root $(Q)rm -rf os_root
$(Q)rm -rf edk2-ovmf $(Q)rm -rf edk2-ovmf
$(Q)rm -rf os_root.img $(Q)rm -rf os_root.img
$(Q)rm -rf $(INITRAMFS)
print-%: print-%:
$(Q)echo $($(*)) $(Q)echo $($(*))

View file

@ -4,10 +4,10 @@
# #
# Important to remember that this is run at the project-root-level, # Important to remember that this is run at the project-root-level,
# all of it, as it is invoked with $(MAKE) -f # all of it, as it is invoked with $(MAKE) -f
ifneq ($(shell test -e .config && echo -n yes),yes) #ifneq ($(shell test -e .config && echo -n yes),yes)
$(error Please type `make config` to generate a config) # $(error Please type `make config` to generate a config)
endif #endif
-include .config #-include .config
export export
obj-y := obj-y :=
@ -22,7 +22,11 @@ real-objs-y := $(addprefix $(obj)/, $(filter-out %/, $(obj-y)))
subdir-objs-y := $(foreach d,$(subdir-y),$(obj)/$(d)/built-in.o) subdir-objs-y := $(foreach d,$(subdir-y),$(obj)/$(d)/built-in.o)
deps := $(real-objs-y:.o=.d) deps := $(real-objs-y:.o=.d)
ifeq ($(obj),.)
_all: $(subdir-objs-y)
else
_all: $(obj)/built-in.o _all: $(obj)/built-in.o
endif
$(obj)/%.o: $(obj)/%.c $(obj)/%.o: $(obj)/%.c
$(Q)echo " CC $@" $(Q)echo " CC $@"

View file

@ -1,7 +1,6 @@
# SPDX-License-Identifier: GPL-2.0-only # SPDX-License-Identifier: GPL-2.0-only
# Makefile stuff for all the booting and bootloader related stuff # Makefile stuff for all the booting and bootloader related stuff
# #
INITRAMFS := initramfs.cpio
$(IMG): limine/limine $(KERNEL) $(IMG): limine/limine $(KERNEL)
rm -rf $(IMG) rm -rf $(IMG)
@ -27,7 +26,7 @@ $(IMG): limine/limine $(KERNEL)
limine/limine: limine/limine:
rm -rf limine rm -rf limine
git clone https://codeberg.org/Limine/Limine.git limine --branch=$(LIMINE_VERSION)-binary --depth=1 git clone https://codeberg.org/Limine/Limine.git limine --branch=$(LIMINE_VERSION)-binary --depth=1
$(MAKE) -C limine \ $(MAKE) --no-print-directory -C limine \
CC="$(HOST_CC)" \ CC="$(HOST_CC)" \
CFLAGS="$(HOST_CFLAGS)" \ CFLAGS="$(HOST_CFLAGS)" \
CPPFLAGS="$(HOST_CPPFLAGS)" \ CPPFLAGS="$(HOST_CPPFLAGS)" \
@ -68,3 +67,4 @@ edk2-ovmf:
distclean: distclean:
make clean make clean
rm -rf limine rm -rf limine
rm -rf $(INITRAMFS)

26
usr.bin/Makefile Normal file
View file

@ -0,0 +1,26 @@
ifneq ($(V),1)
Q := @
else
Q :=
endif
usr_dirs := utest
usr_bins := $(foreach d,$(usr_dirs),$d/$d)
.PHONY: all $(usr_dirs)
all: initramfs
$(usr_dirs):
$(Q)$(MAKE) --no-print-directory -C $@ DIR=usr.bin/$@
.PHONY: initramfs
initramfs: $(usr_dirs)
$(Q)mkdir -p ../initramfs/bin/
$(Q)cp $(usr_bins) ../initramfs/bin/
.PHONY: clean
clean:
$(Q)for dir in $(usr_dirs); do \
echo " CLEAN usr.bin/$$dir"; \
$(MAKE) --no-print-directory -C $$dir clean; \
done

30
usr.bin/Makefile.common Normal file
View file

@ -0,0 +1,30 @@
ifneq ($(V),1)
Q := @
else
Q :=
endif
USR_CFLAGS ?= -g3 -O0 -pipe -Wall -ffreestanding -nostdlib \
-fno-stack-protector -m64 -march=x86-64 -mabi=sysv \
-Iinclude -MMD
USR_LDFLAGS ?= -nostdlib -static -m elf_x86_64 -z max-page-size=0x1000
$(OUT): $(UOBJ)
$(Q)echo " LD [U] $(DIR)/$@"
$(Q)$(LD) $(USR_LDFLAGS) -o $@ $^
%.o: %.c
$(Q)echo " CC [U] $(DIR)/$@"
$(Q)$(CC) $(USR_CFLAGS) -c $< -o $@
%.o: %.S
$(Q)echo " CC [U] $(DIR)/$@"
$(Q)$(CC) $(USR_CFLAGS) -c $< -o $@
clean:
$(Q)rm -rf $(UOBJ) $(OUT)
print-%:
$(Q)echo $($(*))

4
usr.bin/utest/Makefile Normal file
View file

@ -0,0 +1,4 @@
# SPDX-License-Identifier: GPL-2.0-only
OUT := utest
UOBJ := utest.o
-include ../Makefile.common

BIN
usr.bin/utest/utest Executable file

Binary file not shown.

6
usr.bin/utest/utest.c Normal file
View file

@ -0,0 +1,6 @@
/* SPDX-License-Identifier: GPL-2.0-only */
void _start()
{
while (1)
(void)0;
}