Browse Source

chore: build user-space-program with cmake

greatbridf 2 years ago
parent
commit
fc545e1710
4 changed files with 26 additions and 51 deletions
  1. 5 10
      CMakeLists.txt
  2. 0 4
      user-space-program/.gitignore
  3. 21 0
      user-space-program/CMakeLists.txt
  4. 0 37
      user-space-program/Makefile.src

+ 5 - 10
CMakeLists.txt

@@ -24,6 +24,7 @@ if (NOT DEFINED FDISK_BIN)
 endif()
 
 add_subdirectory(gblibc)
+add_subdirectory(user-space-program)
 
 set(BOOTLOADER_SOURCES src/boot.s
                        src/asm/a20.s
@@ -93,13 +94,7 @@ set(KERNEL_MAIN_SOURCES src/fs/fat.cpp
                         include/kernel_main.hpp
                         )
 
-add_custom_target(user_space_programs
-    COMMAND cp ${CMAKE_SOURCE_DIR}/user-space-program/Makefile.src ${CMAKE_SOURCE_DIR}/user-space-program/Makefile
-    COMMAND make -C ${CMAKE_SOURCE_DIR}/user-space-program CROSS_COMPILE=${TOOLCHAIN_PATH_AND_PREFIX} all
-)
-
 add_executable(kernel.out ${KERNEL_MAIN_SOURCES} ${BOOTLOADER_SOURCES})
-add_dependencies(kernel.out user_space_programs)
 target_link_libraries(kernel.out gblibc)
 target_include_directories(kernel.out PRIVATE ${PROJECT_SOURCE_DIR}/include)
 target_link_options(kernel.out PRIVATE
@@ -126,10 +121,10 @@ add_custom_target(boot.img
     COMMAND dd if=/dev/zero of=boot.img bs=`expr 512 \\* 1024 \\* 1024` count=0 seek=1
     COMMAND sh -c \"echo n\; echo\; echo\; echo\; echo\; echo a\; echo w\" | ${FDISK_BIN} boot.img
     COMMAND mkfs.fat --offset=2048 -v -n SYSTEM boot.img
-    COMMAND mcopy -i boot.img@@1M ${CMAKE_SOURCE_DIR}/user-space-program/build/hello-world.out ::hello.out
-    COMMAND mcopy -i boot.img@@1M ${CMAKE_SOURCE_DIR}/user-space-program/build/interrupt-test.out ::int.out
-    COMMAND mcopy -i boot.img@@1M ${CMAKE_SOURCE_DIR}/user-space-program/build/stack-test.out ::stack.out
-    COMMAND mcopy -i boot.img@@1M ${CMAKE_SOURCE_DIR}/user-space-program/build/init.out ::init.elf
+    COMMAND mcopy -i boot.img@@1M ${CMAKE_BINARY_DIR}/user-space-program/hello-world.out ::hello.out
+    COMMAND mcopy -i boot.img@@1M ${CMAKE_BINARY_DIR}/user-space-program/interrupt-test.out ::int.out
+    COMMAND mcopy -i boot.img@@1M ${CMAKE_BINARY_DIR}/user-space-program/stack-test.out ::stack.out
+    COMMAND mcopy -i boot.img@@1M ${CMAKE_BINARY_DIR}/user-space-program/init.out ::init.elf
 )
 
 add_custom_command(OUTPUT run

+ 0 - 4
user-space-program/.gitignore

@@ -1,4 +0,0 @@
-*.o
-*.bin
-*.res
-user.sym

+ 21 - 0
user-space-program/CMakeLists.txt

@@ -0,0 +1,21 @@
+cmake_minimum_required(VERSION 3.15)
+project(user_space_program C ASM)
+
+set(CMAKE_C_FLAGS "-nostdinc -nostdlib -static -m32 -W -Wall -Wextra -Werror -mstack-protector-guard=global")
+set(CMAKE_ASM_FLAGS "-m32 -static -mstack-protector-guard=global -g0")
+
+link_libraries(gblibc)
+add_link_options(-nostdlib -e main -Ttext 0x40000000 -T ${CMAKE_CURRENT_SOURCE_DIR}/script.ld)
+# set(LINK_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/script.ld)
+
+set(CMAKE_C_IMPLICIT_LINK_DIRECTORIES "")
+set(CMAKE_C_IMPLICIT_LINK_LIBRARIES "")
+
+add_executable(hello-world.out hello-world.s)
+add_executable(interrupt-test.out interrupt-test.s)
+add_executable(stack-test.out stack-test.s)
+add_executable(init.out init.c)
+
+add_custom_target(user_space_programs
+    DEPENDS hello-world.out interrupt-test.out stack-test.out init.out
+)

+ 0 - 37
user-space-program/Makefile.src

@@ -1,37 +0,0 @@
-CROSS_COMPILE=
-CC=$(CROSS_COMPILE)gcc
-LD=$(CROSS_COMPILE)ld
-OBJCOPY=$(CROSS_COMPILE)objcopy
-XXD=xxd
-
-CFLAGS=-nostdinc -nostdlib -static -g -m32 -W -Wall -Wextra -Werror -mstack-protector-guard=global
-
-OBJS=hello-world.out interrupt-test.out stack-test.out init.out
-SYMS=init.sym
-
-all: $(OBJS) $(SYMS)
-	mkdir -p build
-	mv $(OBJS) build
-	mv $(SYMS) build
-
-%.o: %.c
-	$(CC) $(CFLAGS) -c -o $@ $<
-
-%.o: %.s
-	$(CC) $(CFLAGS) -c -o $@ $<
-
-%.out1: %.o script.ld
-	$(LD) -nostdlib -e main -Ttext 0x40000000 -T script.ld $< -o $@
-
-%.out: %.out1
-	$(OBJCOPY) --strip-debug $< $@
-
-%.sym: %.out1
-	$(OBJCOPY) --only-keep-debug $< $@
-
-.PHONY: clean
-clean:
-	-rm -rf build
-	-rm $(OBJS)
-	-rm $(SYMS)
-	-rm compile_commands.json