Skip to content

Instantly share code, notes, and snippets.

@jarlostensen
Created May 7, 2021 12:54
Show Gist options
  • Save jarlostensen/db3d8b12de2bc82da29595001c73ae34 to your computer and use it in GitHub Desktop.
Save jarlostensen/db3d8b12de2bc82da29595001c73ae34 to your computer and use it in GitHub Desktop.
a modern (at the time of writing) minimal CMake file to build a basic EFI hello world application.
# a *very* simple CMake file which will build a valid PE+ 64 bit EFI executable using Clang + LLD-LINK on Windows
# with LLVM clang+linker present in an "external" folder as specified below.
#
# To generate Ninja build scripts for this example:
# cmake.exe -H. -G Ninja -Bbuild^
# -DCMAKE_SYSTEM_NAME="Generic"^
# -DCMAKE_C_COMPILER:PATH="%CD%\external\LLVM\bin\clang.exe" -DCMAKE_C_COMPILER_ID="Clang"^
# -DCMAKE_CXX_COMPILER:PATH="%CD%\external\LLVM\bin\clang.exe" -DCMAKE_CXX_COMPILER_ID="Clang"^
# -DCMAKE_LINKER:PATH="%CD%\external\LLVM\bin\lld-link.exe"
#
# efi_main.c is any basic EFI "Hello World" application that does not rely on any standard libraries
#NOTE: probably too harsh, this should work with some earlier versions too
cmake_minimum_required(VERSION 3.18 FATAL_ERROR)
project(uefi-application)
##################################################
# a bit of system information
include (CheckCCompilerFlag)
message(STATUS "system name: ${CMAKE_SYSTEM_NAME}")
message(STATUS "host system name: ${CMAKE_HOST_SYSTEM_NAME}")
message(STATUS "host system processor: ${CMAKE_HOST_SYSTEM_PROCESSOR}")
##################################################
# target defines
set(TARGET_NAME efi)
set(BUILD_TARGET "x86_64-unknown-windows")
##################################################
# build it!
add_executable("${TARGET_NAME}" efi_main.c)
# minimal required options for clang
target_compile_options("${TARGET_NAME}" PRIVATE
-target "${BUILD_TARGET}"
-nostdinc
-ffreestanding
-Og
-g
-D_DEBUG
)
# minimal options for LLD-LINK
target_link_options("${TARGET_NAME}" PRIVATE
LINKER:-nodefaultlib,
LINKER:-subsystem:efi_application,
LINKER:-entry:efi_main,
LINKER:-debug,
LINKER:-pdb:BOOTX64.PDB
)
target_include_directories("${TARGET_NAME}" PRIVATE
"${CMAKE_SOURCE_DIR}/c-efi"
)
set_target_properties("${TARGET_NAME}" PROPERTIES OUTPUT_NAME "BOOTX64.EFI")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment