CMakeLists.txt 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. cmake_minimum_required(VERSION 3.15)
  2. project(kernel_main C CXX ASM)
  3. set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
  4. set(CMAKE_CXX_LINK_EXECUTABLE
  5. "<CMAKE_LINKER> <CMAKE_CXX_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>")
  6. set(C_CXX_FLAGS "-nostdinc -nostdlib -W -Wall -Wextra -Wno-stringop-overflow -Wno-builtin-declaration-mismatch -Wno-format -fverbose-asm -fno-exceptions -ffreestanding -fno-pic -mno-red-zone -mstack-protector-guard=global -mcmodel=kernel")
  7. set(CMAKE_C_FLAGS "${C_CXX_FLAGS} -Werror=implicit-int -Werror=implicit-function-declaration -Werror=strict-aliasing")
  8. set(CMAKE_CXX_FLAGS "${C_CXX_FLAGS} -fno-use-cxa-atexit -fno-rtti")
  9. set(CMAKE_CXX_LINK_FLAGS "-z noexecstack --gc-sections")
  10. SET(CMAKE_ASM_FLAGS "${CFLAGS} -x assembler-with-cpp")
  11. set(CMAKE_CXX_STANDARD 20)
  12. if (CMAKE_BUILD_TYPE STREQUAL "Debug")
  13. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DDEBUG -O0 -g")
  14. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DDEBUG -O0 -g")
  15. set(CARGO_BUILD_TYPE "debug")
  16. set(CARGO_BUILD_FLAGS "")
  17. elseif(CMAKE_BUILD_TYPE STREQUAL "Release")
  18. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O2 -g -DNDEBUG")
  19. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2 -g -DNDEBUG")
  20. set(CARGO_BUILD_TYPE "release")
  21. set(CARGO_BUILD_FLAGS "--release")
  22. endif()
  23. if (NOT DEFINED FDISK_BIN)
  24. set(FDISK_BIN fdisk)
  25. endif()
  26. add_subdirectory(gblibc)
  27. add_subdirectory(gblibstdc++)
  28. add_subdirectory(user-space-program)
  29. set(BOOTLOADER_SOURCES src/boot.s
  30. src/mbr.S
  31. src/asm/interrupt.s
  32. )
  33. set(KERNEL_MAIN_SOURCES src/dev/builtin-chardev.cc
  34. src/fs/fat.cpp
  35. src/fs/tmpfs.cc
  36. src/fs/procfs.cc
  37. src/kinit.cpp
  38. src/kernel/async/waitlist.cc
  39. src/kernel/async/lock.cc
  40. src/kernel/allocator.cc
  41. src/kernel/interrupt.cpp
  42. src/kernel/process.cpp
  43. src/kernel/tty.cpp
  44. src/kernel/syscall.cpp
  45. src/kernel/syscall/fileops.cc
  46. src/kernel/syscall/infoops.cc
  47. src/kernel/syscall/mount.cc
  48. src/kernel/syscall/procops.cc
  49. src/kernel/mem/mm_list.cc
  50. src/kernel/mem/paging.cc
  51. src/kernel/mem/slab.cc
  52. src/kernel/module.cc
  53. src/kernel/vfs.cpp
  54. src/kernel/vga.cpp
  55. src/kernel/hw/acpi.cc
  56. src/kernel/hw/ahci.cc
  57. src/kernel/hw/pci.cc
  58. src/kernel/hw/serial.cc
  59. src/kernel/hw/timer.cc
  60. src/kernel/task/thread.cc
  61. src/kernel/task/readyqueue.cc
  62. src/kernel/user/thread_local.cc
  63. src/kernel/vfs/dentry.cc
  64. src/kernel/vfs/filearr.cc
  65. src/kernel/vfs/inode.cc
  66. src/kernel/vfs/vfs.cc
  67. src/kernel/signal.cpp
  68. src/net/ethernet.cc
  69. src/types/crc.cc
  70. src/types/elf.cpp
  71. src/types/libstdcpp.cpp
  72. include/defs.hpp
  73. include/fs/fat.hpp
  74. include/kernel/async/waitlist.hpp
  75. include/kernel/async/lock.hpp
  76. include/kernel/tty.hpp
  77. include/kernel/interrupt.hpp
  78. include/kernel/irq.hpp
  79. include/kernel/process.hpp
  80. include/kernel/syscall.hpp
  81. include/kernel/mem/mm_list.hpp
  82. include/kernel/mem/paging.hpp
  83. include/kernel/mem/slab.hpp
  84. include/kernel/mem/types.hpp
  85. include/kernel/mem/vm_area.hpp
  86. include/kernel/module.hpp
  87. include/kernel/utsname.hpp
  88. include/kernel/vfs.hpp
  89. include/kernel/vfs/dentry.hpp
  90. include/kernel/vfs/file.hpp
  91. include/kernel/vfs/filearr.hpp
  92. include/kernel/vfs/inode.hpp
  93. include/kernel/vfs/vfs.hpp
  94. include/kernel/vga.hpp
  95. include/kernel/signal.hpp
  96. include/kernel/task/forward.hpp
  97. include/kernel/task/thread.hpp
  98. include/kernel/task/readyqueue.hpp
  99. include/kernel/hw/acpi.hpp
  100. include/kernel/hw/pci.hpp
  101. include/kernel/hw/port.hpp
  102. include/kernel/hw/serial.hpp
  103. include/kernel/hw/timer.hpp
  104. include/kernel/input/keycodes.h
  105. include/kernel/user/thread_local.hpp
  106. include/net/arp.hpp
  107. include/net/ethernet.hpp
  108. include/net/netdev.hpp
  109. include/types/bitmap.hpp
  110. include/types/buffer.hpp
  111. include/types/elf.hpp
  112. include/types/hash.hpp
  113. include/types/list.hpp
  114. include/types/types.h
  115. include/types/allocator.hpp
  116. include/types/cplusplus.hpp
  117. include/kernel/log.hpp
  118. )
  119. add_executable(kernel.out ${KERNEL_MAIN_SOURCES} ${BOOTLOADER_SOURCES})
  120. add_dependencies(kernel.out rustpart)
  121. target_link_libraries(kernel.out gblibc gblibstdc++ gbos_rust_part)
  122. target_include_directories(kernel.out PRIVATE ${PROJECT_SOURCE_DIR}/include)
  123. target_link_options(kernel.out PRIVATE
  124. -T "${CMAKE_SOURCE_DIR}/src/kernel.ld"
  125. -L "${CMAKE_BINARY_DIR}/x86_64-unknown-none/${CARGO_BUILD_TYPE}"
  126. )
  127. set_target_properties(kernel.out PROPERTIES LINK_DEPENDS "${CMAKE_SOURCE_DIR}/src/kernel.ld")
  128. set_source_files_properties(src/mbr.S PROPERTIES OBJECT_DEPENDS
  129. "${CMAKE_BINARY_DIR}/x86_64-unknown-none/${CARGO_BUILD_TYPE}/libgbos_rust_part.a"
  130. )
  131. add_custom_target(rustpart
  132. COMMAND cargo build ${CARGO_BUILD_FLAGS}
  133. )
  134. add_custom_command(OUTPUT mbr_hole.bin
  135. DEPENDS kernel.out
  136. COMMAND ${CMAKE_OBJCOPY} --strip-debug -O binary ${CMAKE_BINARY_DIR}/kernel.out mbr_hole.bin
  137. )
  138. add_custom_target(boot.img
  139. DEPENDS mbr_hole.bin
  140. DEPENDS user_space_programs
  141. COMMAND dd if=mbr_hole.bin of=boot.img
  142. COMMAND dd if=/dev/zero of=boot.img bs=`expr 512 \\* 1024 \\* 1024` count=0 seek=1
  143. COMMAND sh -c \"echo n\; echo\; echo\; echo\; echo\; echo a\; echo w\" | ${FDISK_BIN} boot.img
  144. COMMAND mkfs.fat --offset=2048 -v -n SYSTEM boot.img
  145. COMMAND mcopy -i boot.img@@1M ${CMAKE_BINARY_DIR}/user-space-program/hello-world.out ::hello
  146. COMMAND mcopy -i boot.img@@1M ${CMAKE_BINARY_DIR}/user-space-program/interrupt-test.out ::int
  147. COMMAND mcopy -i boot.img@@1M ${CMAKE_BINARY_DIR}/user-space-program/stack-test.out ::stack
  148. COMMAND mcopy -i boot.img@@1M ${CMAKE_BINARY_DIR}/user-space-program/init.out ::init
  149. COMMAND mcopy -i boot.img@@1M ${CMAKE_BINARY_DIR}/user-space-program/sh.out ::sh
  150. COMMAND mcopy -i boot.img@@1M ${CMAKE_BINARY_DIR}/user-space-program/priv-test.out ::priv
  151. COMMAND mcopy -i boot.img@@1M ${CMAKE_BINARY_DIR}/user-space-program/lazybox.out ::lazybox
  152. COMMAND mcopy -i boot.img@@1M ${CMAKE_BINARY_DIR}/user-space-program/lazybox.out ::pwd
  153. COMMAND mcopy -i boot.img@@1M ${CMAKE_SOURCE_DIR}/busybox-minimal ::busybox_
  154. COMMAND mcopy -i boot.img@@1M ${CMAKE_SOURCE_DIR}/busybox ::busybox
  155. COMMAND mcopy -i boot.img@@1M ${CMAKE_SOURCE_DIR}/init_script.sh ::initsh
  156. )
  157. add_custom_command(OUTPUT run
  158. POST_BUILD
  159. DEPENDS boot.img
  160. COMMAND bochs -f ${CMAKE_SOURCE_DIR}/bochs.conf
  161. )