-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFindefi.cmake
158 lines (109 loc) · 4.93 KB
/
Findefi.cmake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#[=======================================================================[.rst:
Findefi
-------
This module finds the gnuefi and efi libraries on the system.
Additionally finds linker script and crt objects that corresponds to current
platform (CMAKE_SYSTEM_PROCESSOR), provides create_efi_image() function
to geneate resulting efi image.
Add path where Findefi.cmake is located to CMAKE_MODULE_PATH variable.
For example, if Findefi.cmake is located next to CMakeLists.txt file, use::
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_SOURCE_DIR})
CMAKE_MODULE_PATH should be set before calling find_package().
See example project.
For more information on find package modules see:
https://cmake.org/cmake/help/latest/command/find_package.html
The following will produce hello<arch>.efi image::
find_package(efi REQUIRED)
add_library(hello_efi SHARED main.c)
target_link_libraries(hello_efi efi)
create_efi_image(hello_efi hello)
The following variables are provided to indicate gnuefi support:
.. variable:: EFI_FOUND
Variable indicating if the gnuefi and efi support was found.
.. variable:: EFI_INCLUDE_DIRS
The directories containing the efi headers.
.. variable:: EFI_C_FLAGS
The gnuefi and efi compilation flags
.. variable:: EFI_LIBRARIES
The gnuefi and efi libraries to be linked.
.. variable:: EFI_LINKER_FLAGS
The gnuefi and efi linker flags to be used when linking with these libraries.
Additionally, the following :prop_tgt:`IMPORTED` target is being provided:
.. variable:: efi
Interface target for using gnuefi and efi libraries. This target has all
the required libraries, flags and defintions set.
.. variable:: efi::gnuefi
Imported target for gnuefi library (not to be used directly).
.. variable:: efi::efi
Imported target for efi library (not to be used directly).
The following cache variables may also be set:
.. variable:: EFI_INCLUDE_DIR
The directory containing the efi headers.
.. variable:: EFI_LIBRARY
The efi library.
.. variable:: GNUEFI_LIBRARY
The gnuefi library.
#]=======================================================================]
find_library(EFI_LIBRARY NAMES efi DOC "efi library")
find_path(EFI_INCLUDE_DIR efi/efi.h DOC "efi include directory")
find_library(GNUEFI_LIBRARY NAMES gnuefi DOC "gnuefi library")
# handle the QUIETLY and REQUIRED arguments and set xxx_FOUND to TRUE if
# all listed variables are TRUE
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(efi
DEFAULT_MSG
EFI_LIBRARY
EFI_INCLUDE_DIR)
find_package_handle_standard_args(gnuefi
DEFAULT_MSG
GNUEFI_LIBRARY)
IF (EFI_FOUND AND GNUEFI_FOUND)
set(EFI_ARCH "${CMAKE_SYSTEM_PROCESSOR}")
if (EFI_ARCH MATCHES "amd64.*|x86_64.*|AMD64.*")
set(EFI_ARCH "x86_64")
set(EFI_OUT_ARCH "x64")
elseif (EFI_ARCH MATCHES "i686.*|i386.*")
set(EFI_ARCH "ia32")
set(EFI_OUT_ARCH "x86")
else()
set(EFI_OUT_ARCH "")
endif()
get_filename_component(EFI_LIBRARY_DIR ${EFI_LIBRARY} DIRECTORY)
set(EFI_LDS "${EFI_LIBRARY_DIR}/elf_${EFI_ARCH}_efi.lds")
set(EFI_CRT_OBJS "${EFI_LIBRARY_DIR}/crt0-efi-${EFI_ARCH}.o")
set(EFI_C_FLAGS -std=gnu89 -nostdinc -fno-stack-protector -fshort-wchar -mno-red-zone -mno-mmx -mno-sse)
set(EFI_LINKER_FLAGS ${EFI_CRT_OBJS} -Wl,-nostdlib -Wl,-znocombreloc -Wl,-T${EFI_LDS} -Wl,-Bsymbolic -Wl,--no-undefined)
set(EFI_INCLUDE_DIRS "${EFI_INCLUDE_DIR}/efi"
"${EFI_INCLUDE_DIR}/efi/protocol"
"${EFI_INCLUDE_DIR}/efi/${EFI_ARCH}")
set(EFI_LIBRARIES ${EFI_LIBRARY} ${GNUEFI_LIBRARY})
if(NOT TARGET efi::efi)
add_library(efi::efi IMPORTED STATIC)
endif()
set_property(TARGET efi::efi PROPERTY IMPORTED_LOCATION ${EFI_LIBRARY})
if(NOT TARGET efi::gnuefi)
add_library(efi::gnuefi IMPORTED STATIC)
endif()
set_property(TARGET efi::gnuefi PROPERTY IMPORTED_LOCATION ${GNUEFI_LIBRARY})
if(NOT TARGET efi)
add_library(efi INTERFACE)
endif()
set_property(TARGET efi PROPERTY INTERFACE_LINK_LIBRARIES ${EFI_LINKER_FLAGS} efi::efi efi::gnuefi)
set_property(TARGET efi PROPERTY INTERFACE_POSITION_INDEPENDENT_CODE ON)
set_property(TARGET efi PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${EFI_INCLUDE_DIRS})
set_property(TARGET efi PROPERTY INTERFACE_COMPILE_OPTIONS ${EFI_C_FLAGS})
mark_as_advanced(
EFI_LIBRARY
EFI_INCLUDE_DIR
GNUEFI_LIBRARY)
endif()
function (create_efi_image target out)
set(EFI_IMAGE_FILE ${CMAKE_CURRENT_BINARY_DIR}/${out}${EFI_OUT_ARCH}.efi)
add_custom_command(TARGET ${target} POST_BUILD
COMMENT "Creating EFI image: ${out}${EFI_OUT_ARCH}.efi"
COMMAND ${CMAKE_OBJCOPY} -j .text -j .sdata -j .data -j .dynamic
-j .dynsym -j .rel -j .rela -j .reloc
--target=efi-app-${EFI_ARCH}
$<TARGET_FILE:${target}> ${EFI_IMAGE_FILE})
set_property(TARGET ${target} PROPERTY EFI_IMAGE_FILE ${EFI_IMAGE_FILE})
endfunction()