Created
February 5, 2017 11:39
-
-
Save sgielen/ddce7238dece2b0d18d3c2646684edd2 to your computer and use it in GitHub Desktop.
Building kernel with CMake, clang and binutils ld
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
* Build and install binutils with --target i686-elf | |
* This gives you the GNU linker in $PATH as i686-elf-ld | |
* Install LLVM + Clang | |
* Symlink the compiler with your triplet, e.g. i686-elf-cc | |
* Put the CMakeLists.txt + cmake/Toolchain-i686-elf file in a directory, make your changes | |
mkdir build | |
cd build | |
cmake -DCMAKE_BUILD_TYPE=Debug \ | |
-DCMAKE_TOOLCHAIN_FILE=../Toolchain-i686-elf.cmake .. | |
make |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cmake_minimum_required(VERSION 2.8.12) | |
project(MyKernel C CXX ASM) | |
# ... | |
add_executable(mykernel boot.s kernel_main.cpp) # + other source files | |
set_target_properties(mykernel PROPERTIES LINK_DEPENDS "${CMAKE_SOURCE_DIR}/linker.ld") | |
set_target_properties(mykernel PROPERTIES LINK_FLAGS "-T ${CMAKE_SOURCE_DIR}/linker.ld") | |
set(QEMU_ARGS -kernel mykernel -m 1024 | |
-net nic,model=virtio -net dump,file=dump.pcap -net user | |
-no-reboot -no-shutdown -d int -d cpu_reset -d guest_errors) | |
add_custom_target(boot | |
COMMAND qemu-system-i386 ${QEMU_ARGS} | |
DEPENDS mykernel | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SET(CMAKE_SYSTEM_NAME Generic) | |
if(CMAKE_VERSION VERSION_LESS "3.6.0") | |
INCLUDE(CMakeForceCompiler) | |
CMAKE_FORCE_C_COMPILER(i686-elf-cc Clang) | |
CMAKE_FORCE_CXX_COMPILER(i686-elf-c++ Clang) | |
else() | |
set(CMAKE_C_COMPILER i686-elf-cc) | |
set(CMAKE_CXX_COMPILER i686-elf-c++) | |
endif() | |
set(CMAKE_GLD_LINKER_NAME i686-elf-ld CACHE STRING "Name of the Binutils linker") | |
mark_as_advanced(CMAKE_GLD_LINKER_NAME) | |
find_program(CMAKE_GLD_LINKER ${CMAKE_GLD_LINKER_NAME}) | |
mark_as_advanced(CMAKE_GLD_LINKER) | |
if(NOT CMAKE_GLD_LINKER) | |
message(FATAL_ERROR "Could not find the GNU LD linker: ${CMAKE_GLD_LINKER_NAME}") | |
endif() | |
set(CMAKE_CXX_LINK_EXECUTABLE "${CMAKE_GLD_LINKER} <CMAKE_CXX_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>") | |
set(C_AND_CXX_FLAGS "-ffreestanding -O0 -g -mno-sse -mno-mmx -fno-sanitize=safe-stack -Wno-reserved-id-macro") | |
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${C_AND_CXX_FLAGS} -fno-exceptions -fno-rtti") | |
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${C_AND_CXX_FLAGS}") | |
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -O0 -g -nostdlib") | |
set(CMAKE_C_COMPILER_FORCED TRUE) | |
set(CMAKE_CXX_COMPILER_FORCED TRUE) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment