CMakeLists.txt 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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/kinit.cpp
  35. src/kernel/async/waitlist.cc
  36. src/kernel/async/lock.cc
  37. src/kernel/allocator.cc
  38. src/kernel/interrupt.cpp
  39. src/kernel/process.cpp
  40. src/kernel/tty.cpp
  41. src/kernel/syscall.cpp
  42. src/kernel/syscall/fileops.cc
  43. src/kernel/syscall/infoops.cc
  44. src/kernel/syscall/mount.cc
  45. src/kernel/syscall/procops.cc
  46. src/kernel/mem/mm_list.cc
  47. src/kernel/mem/paging.cc
  48. src/kernel/mem/slab.cc
  49. src/kernel/module.cc
  50. src/kernel/vfs.cpp
  51. src/kernel/vga.cpp
  52. src/kernel/hw/acpi.cc
  53. src/kernel/hw/pci.cc
  54. src/kernel/hw/serial.cc
  55. src/kernel/hw/timer.cc
  56. src/kernel/task/thread.cc
  57. src/kernel/task/readyqueue.cc
  58. src/kernel/user/thread_local.cc
  59. src/kernel/vfs/filearr.cc
  60. src/kernel/signal.cpp
  61. src/net/ethernet.cc
  62. src/types/crc.cc
  63. src/types/elf.cpp
  64. src/types/libstdcpp.cpp
  65. include/defs.hpp
  66. include/kernel/async/waitlist.hpp
  67. include/kernel/async/lock.hpp
  68. include/kernel/tty.hpp
  69. include/kernel/interrupt.hpp
  70. include/kernel/irq.hpp
  71. include/kernel/process.hpp
  72. include/kernel/syscall.hpp
  73. include/kernel/mem/mm_list.hpp
  74. include/kernel/mem/paging.hpp
  75. include/kernel/mem/slab.hpp
  76. include/kernel/mem/types.hpp
  77. include/kernel/mem/vm_area.hpp
  78. include/kernel/module.hpp
  79. include/kernel/utsname.hpp
  80. include/kernel/vfs.hpp
  81. include/kernel/vfs/dentry.hpp
  82. include/kernel/vfs/file.hpp
  83. include/kernel/vfs/filearr.hpp
  84. include/kernel/vga.hpp
  85. include/kernel/signal.hpp
  86. include/kernel/task/forward.hpp
  87. include/kernel/task/thread.hpp
  88. include/kernel/task/readyqueue.hpp
  89. include/kernel/hw/acpi.hpp
  90. include/kernel/hw/pci.hpp
  91. include/kernel/hw/port.hpp
  92. include/kernel/hw/serial.hpp
  93. include/kernel/hw/timer.hpp
  94. include/kernel/input/keycodes.h
  95. include/kernel/user/thread_local.hpp
  96. include/net/arp.hpp
  97. include/net/ethernet.hpp
  98. include/net/netdev.hpp
  99. include/types/bitmap.hpp
  100. include/types/buffer.hpp
  101. include/types/elf.hpp
  102. include/types/list.hpp
  103. include/types/types.h
  104. include/types/allocator.hpp
  105. include/types/cplusplus.hpp
  106. include/kernel/log.hpp
  107. )
  108. add_executable(kernel.out ${KERNEL_MAIN_SOURCES} ${BOOTLOADER_SOURCES})
  109. add_dependencies(kernel.out rustpart)
  110. target_link_libraries(kernel.out gblibc gblibstdc++ gbos_rust_part)
  111. target_include_directories(kernel.out PRIVATE ${PROJECT_SOURCE_DIR}/include)
  112. target_link_options(kernel.out PRIVATE
  113. -T "${CMAKE_SOURCE_DIR}/src/kernel.ld"
  114. -L "${CMAKE_BINARY_DIR}/x86_64-unknown-none/${CARGO_BUILD_TYPE}"
  115. )
  116. set_target_properties(kernel.out PROPERTIES LINK_DEPENDS "${CMAKE_SOURCE_DIR}/src/kernel.ld")
  117. set_source_files_properties(src/mbr.S PROPERTIES OBJECT_DEPENDS
  118. "${CMAKE_BINARY_DIR}/x86_64-unknown-none/${CARGO_BUILD_TYPE}/libgbos_rust_part.a"
  119. )
  120. add_custom_target(rustpart
  121. COMMAND cargo build ${CARGO_BUILD_FLAGS}
  122. )
  123. add_custom_command(OUTPUT mbr_hole.bin
  124. DEPENDS kernel.out
  125. COMMAND ${CMAKE_OBJCOPY} --strip-debug -O binary ${CMAKE_BINARY_DIR}/kernel.out mbr_hole.bin
  126. )
  127. add_custom_target(boot.img
  128. DEPENDS mbr_hole.bin
  129. DEPENDS user_space_programs
  130. COMMAND dd if=mbr_hole.bin of=boot.img
  131. COMMAND dd if=/dev/zero of=boot.img bs=`expr 512 \\* 1024 \\* 1024` count=0 seek=1
  132. COMMAND sh -c \"echo n\; echo\; echo\; echo\; echo\; echo a\; echo w\" | ${FDISK_BIN} boot.img
  133. COMMAND mkfs.fat --offset=2048 -v -n SYSTEM boot.img
  134. COMMAND mcopy -i boot.img@@1M ${CMAKE_BINARY_DIR}/user-space-program/hello-world.out ::hello
  135. COMMAND mcopy -i boot.img@@1M ${CMAKE_BINARY_DIR}/user-space-program/interrupt-test.out ::int
  136. COMMAND mcopy -i boot.img@@1M ${CMAKE_BINARY_DIR}/user-space-program/stack-test.out ::stack
  137. COMMAND mcopy -i boot.img@@1M ${CMAKE_BINARY_DIR}/user-space-program/init.out ::init
  138. COMMAND mcopy -i boot.img@@1M ${CMAKE_BINARY_DIR}/user-space-program/priv-test.out ::priv
  139. COMMAND mcopy -i boot.img@@1M ${CMAKE_SOURCE_DIR}/busybox-minimal ::busybox_
  140. COMMAND mcopy -i boot.img@@1M ${CMAKE_SOURCE_DIR}/busybox ::busybox
  141. COMMAND mcopy -i boot.img@@1M ${CMAKE_SOURCE_DIR}/init_script.sh ::initsh
  142. )
  143. add_custom_command(OUTPUT run
  144. POST_BUILD
  145. DEPENDS boot.img
  146. COMMAND bochs -f ${CMAKE_SOURCE_DIR}/bochs.conf
  147. )