CMakeLists.txt 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 -mno-sse -mno-mmx -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/kinit.cpp
  34. src/kernel/async/lock.cc
  35. src/kernel/allocator.cc
  36. src/kernel/process.cpp
  37. src/kernel/mem/slab.cc
  38. src/kernel/vga.cpp
  39. src/kernel/hw/acpi.cc
  40. src/kernel/hw/pci.cc
  41. src/net/ethernet.cc
  42. src/types/crc.cc
  43. src/types/libstdcpp.cpp
  44. include/defs.hpp
  45. include/kernel/async/lock.hpp
  46. include/kernel/interrupt.hpp
  47. include/kernel/process.hpp
  48. include/kernel/mem/paging.hpp
  49. include/kernel/mem/slab.hpp
  50. include/kernel/mem/types.hpp
  51. include/kernel/utsname.hpp
  52. include/kernel/vga.hpp
  53. include/kernel/task/forward.hpp
  54. include/kernel/hw/acpi.hpp
  55. include/kernel/hw/pci.hpp
  56. include/kernel/hw/port.hpp
  57. include/kernel/input/keycodes.h
  58. include/net/arp.hpp
  59. include/net/ethernet.hpp
  60. include/net/netdev.hpp
  61. include/types/bitmap.hpp
  62. include/types/buffer.hpp
  63. include/types/list.hpp
  64. include/types/types.h
  65. include/types/allocator.hpp
  66. include/types/cplusplus.hpp
  67. include/kernel/log.hpp
  68. )
  69. add_executable(kernel.out ${KERNEL_MAIN_SOURCES} ${BOOTLOADER_SOURCES})
  70. add_dependencies(kernel.out rustpart)
  71. target_link_libraries(kernel.out gblibc gblibstdc++ gbos_rust_part)
  72. target_include_directories(kernel.out PRIVATE ${PROJECT_SOURCE_DIR}/include)
  73. target_link_options(kernel.out PRIVATE
  74. -T "${CMAKE_SOURCE_DIR}/src/kernel.ld"
  75. -L "${CMAKE_BINARY_DIR}/x86_64-unknown-none/${CARGO_BUILD_TYPE}"
  76. --no-check-sections
  77. )
  78. set_target_properties(kernel.out PROPERTIES LINK_DEPENDS "${CMAKE_SOURCE_DIR}/src/kernel.ld")
  79. set_source_files_properties(src/mbr.S PROPERTIES OBJECT_DEPENDS
  80. "${CMAKE_BINARY_DIR}/x86_64-unknown-none/${CARGO_BUILD_TYPE}/libgbos_rust_part.a"
  81. )
  82. add_custom_target(rustpart
  83. COMMAND cargo build ${CARGO_BUILD_FLAGS}
  84. )
  85. add_custom_command(OUTPUT mbr_hole.bin
  86. DEPENDS kernel.out
  87. COMMAND ${CMAKE_OBJCOPY} --strip-debug -O binary ${CMAKE_BINARY_DIR}/kernel.out mbr_hole.bin
  88. )
  89. add_custom_target(boot.img
  90. DEPENDS mbr_hole.bin
  91. DEPENDS user_space_programs
  92. COMMAND dd if=mbr_hole.bin of=boot.img
  93. COMMAND dd if=/dev/zero of=boot.img bs=`expr 512 \\* 1024 \\* 1024` count=0 seek=1
  94. COMMAND sh -c \"echo n\; echo\; echo \; echo 8192\; echo\; echo a\; echo w\" | ${FDISK_BIN} boot.img
  95. COMMAND mkfs.fat --offset=8192 -v -n SYSTEM boot.img
  96. COMMAND mcopy -i boot.img@@4M ${CMAKE_BINARY_DIR}/user-space-program/hello-world.out ::hello
  97. COMMAND mcopy -i boot.img@@4M ${CMAKE_BINARY_DIR}/user-space-program/interrupt-test.out ::int
  98. COMMAND mcopy -i boot.img@@4M ${CMAKE_BINARY_DIR}/user-space-program/stack-test.out ::stack
  99. COMMAND mcopy -i boot.img@@4M ${CMAKE_BINARY_DIR}/user-space-program/init.out ::init
  100. COMMAND mcopy -i boot.img@@4M ${CMAKE_BINARY_DIR}/user-space-program/priv-test.out ::priv
  101. COMMAND mcopy -i boot.img@@4M ${CMAKE_SOURCE_DIR}/busybox-minimal ::busybox_
  102. COMMAND mcopy -i boot.img@@4M ${CMAKE_SOURCE_DIR}/busybox ::busybox
  103. COMMAND mcopy -i boot.img@@4M ${CMAKE_SOURCE_DIR}/init_script.sh ::initsh
  104. )
  105. add_custom_command(OUTPUT run
  106. POST_BUILD
  107. DEPENDS boot.img
  108. COMMAND bochs -f ${CMAKE_SOURCE_DIR}/bochs.conf
  109. )