CMakeLists.txt 4.6 KB

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