CMakeLists.txt 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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(CMAKE_ASM_FLAGS "-m32")
  7. set(C_CXX_FLAGS "-nostdinc -m32 -nostdlib -W -Wall -Wextra -Wno-builtin-declaration-mismatch -Wno-format -fverbose-asm -fno-exceptions -ffreestanding -fno-pic -mstack-protector-guard=global")
  8. set(CMAKE_C_FLAGS "${C_CXX_FLAGS} -Werror=implicit-int -Werror=implicit-function-declaration -Werror=strict-aliasing")
  9. set(CMAKE_CXX_FLAGS "${C_CXX_FLAGS} -fno-use-cxa-atexit -fno-rtti")
  10. set(CMAKE_CXX_LINK_FLAGS "")
  11. set(CMAKE_CXX_STANDARD 20)
  12. if (CMAKE_BUILD_TYPE STREQUAL "Debug")
  13. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O0 -g")
  14. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O0 -g")
  15. elseif(CMAKE_BUILD_TYPE STREQUAL "Release")
  16. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O2")
  17. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2")
  18. endif()
  19. if (NOT DEFINED FDISK_BIN)
  20. set(FDISK_BIN fdisk)
  21. endif()
  22. add_subdirectory(gblibc)
  23. add_subdirectory(gblibstdc++)
  24. add_subdirectory(user-space-program)
  25. set(BOOTLOADER_SOURCES src/boot.s
  26. src/asm/interrupt.s
  27. src/asm/port_io.s
  28. src/asm/sys.s
  29. )
  30. set(KERNEL_MAIN_SOURCES src/fs/fat.cpp
  31. src/kinit.cpp
  32. src/kernel/allocator.cc
  33. src/kernel/interrupt.cpp
  34. src/kernel/process.cpp
  35. src/kernel/tty.cpp
  36. src/kernel/syscall.cpp
  37. src/kernel/mem.cpp
  38. src/kernel/module.cc
  39. src/kernel/vfs.cpp
  40. src/kernel/vga.cpp
  41. src/kernel/hw/ahci.cc
  42. src/kernel/hw/keyboard.cpp
  43. src/kernel/hw/pci.cc
  44. src/kernel/hw/serial.cpp
  45. src/kernel/hw/timer.c
  46. src/kernel/event/event.cpp
  47. src/kernel/user/thread_local.cc
  48. src/kernel/vfs/tmpfs.cc
  49. src/kernel/signal.cpp
  50. src/types/elf.cpp
  51. src/types/libstdcpp.cpp
  52. include/asm/port_io.h
  53. include/asm/sys.h
  54. include/fs/fat.hpp
  55. include/kernel/event/event.h
  56. include/kernel/event/evtqueue.hpp
  57. include/kernel/tty.hpp
  58. include/kernel/interrupt.h
  59. include/kernel/irq.hpp
  60. include/kernel/process.hpp
  61. include/kernel/syscall.hpp
  62. include/kernel/mem.h
  63. include/kernel/mm.hpp
  64. include/kernel/module.hpp
  65. include/kernel/utsname.hpp
  66. include/kernel/vfs.hpp
  67. include/kernel/vfs/dentry.hpp
  68. include/kernel/vfs/file.hpp
  69. include/kernel/vfs/inode.hpp
  70. include/kernel/vfs/vfs.hpp
  71. include/kernel/vga.hpp
  72. include/kernel/signal.hpp
  73. include/kernel/hw/keyboard.h
  74. include/kernel/hw/pci.hpp
  75. include/kernel/hw/port.hpp
  76. include/kernel/hw/serial.h
  77. include/kernel/hw/timer.h
  78. include/kernel/input/keycodes.h
  79. include/kernel/input/input_event.h
  80. include/kernel/user/thread_local.hpp
  81. include/types/bitmap.hpp
  82. include/types/buffer.hpp
  83. include/types/elf.hpp
  84. include/types/hash_map.hpp
  85. include/types/types.h
  86. include/types/size.h
  87. include/types/status.h
  88. include/types/allocator.hpp
  89. include/types/cplusplus.hpp
  90. include/types/lock.hpp
  91. include/types/string.hpp
  92. include/kernel/log.hpp
  93. )
  94. add_executable(kernel.out ${KERNEL_MAIN_SOURCES} ${BOOTLOADER_SOURCES})
  95. target_link_libraries(kernel.out gblibc gblibstdc++)
  96. target_include_directories(kernel.out PRIVATE ${PROJECT_SOURCE_DIR}/include)
  97. target_link_options(kernel.out PRIVATE
  98. -T ${CMAKE_SOURCE_DIR}/src/kernel.ld -melf_i386 -lgblibc -L${CMAKE_BINARY_DIR}/gblibc)
  99. set_target_properties(kernel.out PROPERTIES LINK_DEPENDS ${CMAKE_SOURCE_DIR}/src/kernel.ld)
  100. add_custom_command(OUTPUT mbr.bin
  101. DEPENDS ${PROJECT_SOURCE_DIR}/src/mbr.S ${PROJECT_SOURCE_DIR}/src/mbr.ld
  102. COMMAND ${CMAKE_ASM_COMPILER} -m32 -c ${PROJECT_SOURCE_DIR}/src/mbr.S -o mbr.o
  103. COMMAND ${CMAKE_LINKER} -T ${PROJECT_SOURCE_DIR}/src/mbr.ld mbr.o -o mbr.bin
  104. )
  105. add_custom_command(OUTPUT mbr_hole.bin
  106. DEPENDS kernel.out
  107. COMMAND ${CMAKE_OBJCOPY} --strip-debug -O binary ${CMAKE_BINARY_DIR}/kernel.out mbr_hole.bin
  108. )
  109. add_custom_target(boot.img
  110. DEPENDS mbr.bin
  111. DEPENDS mbr_hole.bin
  112. DEPENDS user_space_programs
  113. COMMAND dd if=mbr.bin of=boot.img
  114. COMMAND cat mbr_hole.bin >> boot.img
  115. COMMAND dd if=/dev/zero of=boot.img bs=`expr 512 \\* 1024 \\* 1024` count=0 seek=1
  116. COMMAND sh -c \"echo n\; echo\; echo\; echo\; echo\; echo a\; echo w\" | ${FDISK_BIN} boot.img
  117. COMMAND mkfs.fat --offset=2048 -v -n SYSTEM boot.img
  118. COMMAND mcopy -i boot.img@@1M ${CMAKE_BINARY_DIR}/user-space-program/hello-world.out ::hello
  119. COMMAND mcopy -i boot.img@@1M ${CMAKE_BINARY_DIR}/user-space-program/interrupt-test.out ::int
  120. COMMAND mcopy -i boot.img@@1M ${CMAKE_BINARY_DIR}/user-space-program/stack-test.out ::stack
  121. COMMAND mcopy -i boot.img@@1M ${CMAKE_BINARY_DIR}/user-space-program/init.out ::init
  122. COMMAND mcopy -i boot.img@@1M ${CMAKE_BINARY_DIR}/user-space-program/sh.out ::sh
  123. COMMAND mcopy -i boot.img@@1M ${CMAKE_BINARY_DIR}/user-space-program/priv-test.out ::priv
  124. COMMAND mcopy -i boot.img@@1M ${CMAKE_BINARY_DIR}/user-space-program/lazybox.out ::lazybox
  125. COMMAND mcopy -i boot.img@@1M ${CMAKE_BINARY_DIR}/user-space-program/lazybox.out ::pwd
  126. COMMAND mcopy -i boot.img@@1M ${CMAKE_SOURCE_DIR}/busybox-minimal ::busybox_
  127. COMMAND mcopy -i boot.img@@1M ${CMAKE_SOURCE_DIR}/busybox ::busybox
  128. )
  129. add_custom_command(OUTPUT run
  130. POST_BUILD
  131. DEPENDS boot.img
  132. COMMAND bochs -f ${CMAKE_SOURCE_DIR}/bochs.conf
  133. )