-
Notifications
You must be signed in to change notification settings - Fork 0
/
genericfg.cmake
130 lines (99 loc) · 3.62 KB
/
genericfg.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
## Generic cmake configuration for building kernel modules
## Author Umar Ba <[email protected]> github/Jukoo
## version 0.1
## NOTE : no support for multiple file yet !
## ------------------------------------------
set(CMAKE_C_STANDARD 99)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(HEADER_LOCATION /usr/lib/modules/)
string(ASCII 27 esc)
set(success "${esc}[1;32m")
set(warning "${esc}[1;33m")
set(error "${esc}[1;31m")
set(info "${esc}[1;36m")
set(normal "${esc}[0m")
function(mesginfo level _message)
message(${level} ${info}${_message}${normal})
endfunction()
function(mesgwarning level _message)
message(${level} ${warning}${_message}${normal})
endfunction()
function(mesgerror level _message)
message(${level} ${error}${_message}${normal})
endfunction()
function(mesgsuccess level _message)
message(${level} ${success}${_message}${normal})
endfunction()
mesginfo(STATUS "Load Static Config")
execute_process(
COMMAND uname -r
OUTPUT_VARIABLE KERNEL_RELEASE
OUTPUT_STRIP_TRAILING_WHITESPACE
)
mesginfo(STATUS "kernel version ${KERNEL_RELEASE}")
mesginfo(CHECK_START "Detecting Kernel Build Path Location")
find_path(
KERNEL_HEADER
include/linux/init.h
include/linux/kernel.h
include/linux/module.h
PATHS ${HEADER_LOCATION}${KERNEL_RELEASE}/build
)
if( NOT KERNEL_HEADER)
mesgwarning(CHECK_FAIL "NOT FOUND [aborting...]")
mesgerror(FATAL_ERROR "DIE")
endif()
mesgsuccess(CHECK_PASS "<${KERNEL_HEADER}> [OK]")
include_directories(${KERNEL_HEADER}/include)
function(kbuild_write _m_source_file)
file(WRITE ${CMAKE_CURRENT_SOURCE_DIR}/Kbuild "obj-m +=${_m_source_file}.o\n")
endfunction()
function(kbuild_append _m_source_file)
file(APPEND ${CMAKE_CURRENT_SOURCE_DIR}/Kbuild "obj-m +=${_m_source_file}.o\n")
endfunction()
option(ALWAYS_FLUSH_KBUILD "Everytime you rebuild your module(s) it flush automaticaly the Kbuild file" OFF)
if (ALWAYS_FLUSH_KBUILD)
mesginfo(STATUS "Kbuild Flush Enable!")
endif()
function(flush_kbuild)
if(ALWAYS_FLUSH_KBUILD)
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/Kbuild)
mesginfo(STATUS "Flushing Kbuild file!")
file(WRITE ${CMAKE_CURRENT_SOURCE_DIR}/Kbuild )
endif()
endif()
endfunction()
function(kmod_spaning _m_spaname file_1 file_2)
flush_kbuild()
file(APPEND ${CMAKE_CURRENT_SOURCE_DIR}/Kbuild "obj-m +=${_m_spaname}.o\n")
file(APPEND ${CMAKE_CURRENT_SOURCE_DIR}/Kbuild "${_m_spaname}-objs =${file_1}.o ${file_2}.o\n")
endfunction()
## Add more objects file in kbuild file
## FIXME : foreach loop doesn't work well
#function(kbuild_append_multi _m_source_files_list) #
#foreach(_mfile IN LISTS _m_source_files_list)
#kbuild_append(${_mfile_0})
#message(STATUS "build for -> ${_mfile}")
#endforeach()
#endfunction()
function(module_compile _m_source_file)
add_custom_command(OUTPUT ${_m_source_file}.ko
COMMAND $(MAKE) -C ${KERNEL_HEADER}/ M=${CMAKE_CURRENT_BINARY_DIR} src=${CMAKE_CURRENT_SOURCE_DIR} modules
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
COMMENT "[M] ${_m_source_file} Kernel Object >>> [${_m_source_file}.ko]"
VERBATIM
)
## make module_driver
add_custom_target(module ALL DEPENDS ${_m_source_file}.ko)
endfunction()
function(restore)
## make clear
add_custom_target(klean ALL
COMMAND $(MAKE) -C ${KERNEL_HEADER}/ M=${CMAKE_CURRENT_BINARY_DIR} src=${CMAKE_CURRENT_SOURCE_DIR} clean
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
COMMENT "Clean First! "
VERBATIM
)
endfunction()
#!NOTE: must be called so that the makefile takes it into account from the start.
restore()