@@ -41,7 +41,12 @@ USER_SOURCES = $(shell find $(SRC_USER) -name "*.c" \! -name "syscall.c")
4141USER_OBJECTS = $(shell printf "% s\n" $(patsubst $(SRC_USER ) /% .c, $(OUT_DIR ) /usr/% .o, $(USER_SOURCES ) ) | sort -u)
4242USER_ELFS = $(shell printf "% s\n" $(patsubst $(SRC_USER ) /% .c, $(OUT_DIR ) /usr/% .elf, $(USER_SOURCES ) ) | sort -u)
4343
44+ # # Determine apps source directory: prefer repo-root 'apps' if present, otherwise 'src/apps'
45+ ifeq ($(wildcard apps) ,apps)
4446SRC_APPS = apps
47+ else
48+ SRC_APPS = $(SRC_DIR ) /apps
49+ endif
4550APP_OUT_DIR = $(OUT_DIR ) /apps
4651APP_SOURCES = $(shell if [ -d "$(SRC_APPS ) " ]; then find $(SRC_APPS ) -name "* .c"; fi)
4752APP_OBJECTS = $(shell printf "% s\n" $(patsubst $(SRC_APPS ) /% .c, $(APP_OUT_DIR ) /% .o, $(APP_SOURCES ) ) | sort -u)
@@ -121,28 +126,29 @@ $(APP_OUT_DIR)/%.o: $(SRC_APPS)/%.c
121126 @$(CC ) $(CFLAGS ) -D_FORTIFY_SOURCE=0 -fno-builtin -I$(BIN_LIB_DIR ) /targ-include -c $< -o $@
122127
123128
129+ # Build a small syscall shim for apps so newlib's syscalls/_sbrk/_exit etc
130+ # resolve to int 0x80 stubs implemented in src/user/syscall.c
131+ $(APP_OUT_DIR ) /syscall.o : $(SRC_USER ) /syscall.c
132+ @mkdir -p $(dir $@ )
133+ @$(CC ) $(CFLAGS ) -D_FORTIFY_SOURCE=0 -fno-builtin -I$(BIN_LIB_DIR ) /targ-include -c $< -o $@
134+
135+
124136user : lib $(ALL_USER_ELFS ) $(APP_ELFS )
125137 @echo " Built user ELFs: $( ALL_USER_ELFS) "
126138
127139
128140
129- $(APP_OUT_DIR ) /% .elf : $(APP_OUT_DIR ) /% .o
141+
142+ $(APP_OUT_DIR ) /% .elf : $(APP_OUT_DIR ) /% .o $(APP_OUT_DIR ) /syscall.o
130143 @mkdir -p $(dir $@ )
131144 @echo " Linking app ELF: $@ "
132145 @if [ -f " $( BIN_LIB_DIR) /libc.a" ]; then \
133- $(CC ) -nostdlib -static $< $(USER_LDFLAGS ) -o $@ ; \
146+ $(CC ) -nostdlib -static $^ $(USER_LDFLAGS ) -o $@ ; \
134147 else \
135- $(CC ) -nostdlib -static $< -o $@ ; \
148+ $(CC ) -nostdlib -static $^ -o $@ ; \
136149 fi
137150
138- $(OUT_DIR ) /user/% .elf : $(OUT_DIR ) /user/% .o
139- @mkdir -p $(dir $@ )
140- @echo " Linking user ELF: $@ "
141- @if [ -f " $( BIN_LIB_DIR) /libc.a" ]; then \
142- $(CC ) -nostdlib -static $< $(USER_LDFLAGS ) -o $@ ; \
143- else \
144- $(CC ) -nostdlib -static $< -o $@ ; \
145- fi
151+
146152
147153
148154$(OUT_DIR ) /usr/hello.elf : $(OUT_DIR ) /usr/hello.o $(OUT_DIR ) /usr/syscall.o
@@ -196,19 +202,30 @@ $(ESP_IMG): $(BOOTX64) $(KERNEL)
196202$(EXT2_IMG ) : $(KERNEL )
197203 @rm -f $(EXT2_IMG )
198204 @echo " Creating FAT16 filesystem image..."
205+ @rm -rf bin/fs_tmp || true
199206 @mkdir -p bin/fs_tmp/kernel/fonts
200207 @mkdir -p bin/fs_tmp/usr
201208 @mkdir -p bin/fs_tmp/apps
202209 @mkdir -p bin/fs_tmp/lib
203210 @cp -f $(FONTS ) bin/fs_tmp/kernel/fonts/ 2> /dev/null || true
204- @cp -f bin/usr/* .elf bin/fs_tmp/usr/ 2> /dev/null || true
205- @cp -f bin/apps/* .elf bin/fs_tmp/apps/ 2> /dev/null || true
211+ @mkdir -p bin/fs_tmp/usr
212+ @# remove stale user/app ELFs in bin to avoid leftover artifacts
213+ @rm -f bin/usr/* .elf 2> /dev/null || true
214+ @rm -f bin/apps/* .elf 2> /dev/null || true
215+ @# copy only the ELFs we built (USER_ELFS and APP_ELFS)
216+ @for f in $(USER_ELFS ) ; do \
217+ if [ -f " $$ f" ]; then cp -f " $$ f" bin/fs_tmp/usr/; fi ; \
218+ done || true
219+ @for f in $(APP_ELFS ) ; do \
220+ if [ -f " $$ f" ]; then cp -f " $$ f" bin/fs_tmp/apps/; fi ; \
221+ done || true
206222 @cp -f bin/lib/* bin/fs_tmp/lib/ 2> /dev/null || true
207223 @find bin -type f \
208224 -not -name " *.o" \
209225 -not -name " fs.img" \
210226 -not -path " bin/fs_tmp/*" \
211227 -not -path " bin/usr/*" \
228+ -not -path " bin/user/*" \
212229 -exec bash -c ' dest="bin/fs_tmp/$${1#bin/}"; mkdir -p "$$(dirname "$$dest")"; cp "$$1" "$$dest"' _ {} \;
213230 @mkdir -p bin/fs_tmp
214231 @cp README.md bin/fs_tmp/README.md 2> /dev/null || true
0 commit comments