Debian Codegraph @

monero/cmake/CheckTrezor.cmake

raw
1 # Function for setting default options default values via env vars
2 function(_trezor_default_val val_name val_default)
3 if(NOT DEFINED ENV{${val_name}})
4 set(ENV{${val_name}} ${val_default})
5 endif()
6 endfunction()
7
8 # Define default options via env vars
9 _trezor_default_val(USE_DEVICE_TREZOR ON)
10 _trezor_default_val(USE_DEVICE_TREZOR_MANDATORY OFF)
11 _trezor_default_val(USE_DEVICE_TREZOR_PROTOBUF_TEST ON)
12 _trezor_default_val(USE_DEVICE_TREZOR_LIBUSB ON)
13 _trezor_default_val(USE_DEVICE_TREZOR_UDP_RELEASE OFF)
14 _trezor_default_val(USE_DEVICE_TREZOR_DEBUG OFF)
15 _trezor_default_val(TREZOR_DEBUG OFF)
16
17 # Main options
18 OPTION(USE_DEVICE_TREZOR "Trezor support compilation" $ENV{USE_DEVICE_TREZOR})
19 OPTION(USE_DEVICE_TREZOR_MANDATORY "Trezor compilation is mandatory, fail build if Trezor support cannot be compiled" $ENV{USE_DEVICE_TREZOR_MANDATORY})
20 OPTION(USE_DEVICE_TREZOR_PROTOBUF_TEST "Trezor Protobuf test" $ENV{USE_DEVICE_TREZOR_PROTOBUF_TEST})
21 OPTION(USE_DEVICE_TREZOR_LIBUSB "Trezor LibUSB compilation" $ENV{USE_DEVICE_TREZOR_LIBUSB})
22 OPTION(USE_DEVICE_TREZOR_UDP_RELEASE "Trezor UdpTransport in release mode" $ENV{USE_DEVICE_TREZOR_UDP_RELEASE})
23 OPTION(USE_DEVICE_TREZOR_DEBUG "Trezor Debugging enabled" $ENV{USE_DEVICE_TREZOR_DEBUG})
24 OPTION(TREZOR_DEBUG "Main Trezor debugging switch" $ENV{TREZOR_DEBUG})
25
26 macro(trezor_fatal_msg msg)
27 if ($ENV{USE_DEVICE_TREZOR_MANDATORY})
28 message(FATAL_ERROR
29 "${msg}\n"
30 "==========================================================================\n"
31 "[ERROR] To compile without Trezor support, set USE_DEVICE_TREZOR=OFF. "
32 "It is possible both via cmake variable and environment variable, e.g., "
33 "`USE_DEVICE_TREZOR=OFF make release`\n"
34 "For more information, please check src/device_trezor/README.md\n"
35 )
36 else()
37 message(WARNING
38 "${msg}\n"
39 "==========================================================================\n"
40 "[WARNING] Trezor support cannot be compiled! Skipping Trezor compilation. \n"
41 "For more information, please check src/device_trezor/README.md\n")
42 set(USE_DEVICE_TREZOR OFF)
43 return() # finish this cmake file processing (as this is macro).
44 endif()
45 endmacro()
46
47 # Use Trezor master switch
48 if (USE_DEVICE_TREZOR)
49 # Look for protobuf-config.cmake, provided by Protobuf
50 find_package(Protobuf CONFIG)
51
52 if (Protobuf_FOUND)
53 # https://github.com/protocolbuffers/protobuf/issues/14576
54 find_program(Protobuf_PROTOC_EXECUTABLE protoc REQUIRED)
55 set(Protobuf_LIBRARY protobuf::libprotobuf) # Compatibility with FindProtobuf.cmake
56 else()
57 # Look for FindProtobuf.cmake, provided by CMake
58 find_package(Protobuf)
59 endif()
60
61 # Early fail for optional Trezor support
62 if (NOT Protobuf_FOUND)
63 trezor_fatal_msg("Trezor: protobuf library not found")
64 endif()
65
66 if (Protobuf_VERSION VERSION_GREATER_EQUAL 22.0)
67 add_definitions(-DPROTOBUF_HAS_ABSEIL)
68 endif()
69
70 if(TREZOR_DEBUG)
71 set(USE_DEVICE_TREZOR_DEBUG 1)
72 message(STATUS "Trezor: debug build enabled")
73 endif()
74
75 # Compile debugging support (for tests)
76 if (USE_DEVICE_TREZOR_DEBUG)
77 add_definitions(-DWITH_TREZOR_DEBUGGING=1)
78 endif()
79 else()
80 message(STATUS "Trezor: support disabled by USE_DEVICE_TREZOR")
81 endif()
82
83 # Protobuf compilation test
84 if(Protobuf_FOUND AND USE_DEVICE_TREZOR)
85 execute_process(COMMAND ${Protobuf_PROTOC_EXECUTABLE} -I "${CMAKE_CURRENT_LIST_DIR}" -I "${Protobuf_INCLUDE_DIR}" "${CMAKE_CURRENT_LIST_DIR}/test-protobuf.proto" --cpp_out ${CMAKE_BINARY_DIR} RESULT_VARIABLE RET OUTPUT_VARIABLE OUT ERROR_VARIABLE ERR)
86 if(RET)
87 trezor_fatal_msg("Trezor: Protobuf test generation failed: ${OUT} ${ERR}")
88 endif()
89
90 if(ANDROID)
91 set(CMAKE_TRY_COMPILE_LINKER_FLAGS "${CMAKE_TRY_COMPILE_LINKER_FLAGS} -llog")
92 set(CMAKE_TRY_COMPILE_LINK_LIBRARIES "${CMAKE_TRY_COMPILE_LINK_LIBRARIES} log")
93 endif()
94
95 if(USE_DEVICE_TREZOR_PROTOBUF_TEST)
96 if(PROTOBUF_LDFLAGS)
97 set(PROTOBUF_TRYCOMPILE_LINKER "${PROTOBUF_LDFLAGS}")
98 else()
99 set(PROTOBUF_TRYCOMPILE_LINKER "${Protobuf_LIBRARY}")
100 endif()
101
102 try_compile(Protobuf_COMPILE_TEST_PASSED
103 "${CMAKE_BINARY_DIR}"
104 SOURCES
105 "${CMAKE_BINARY_DIR}/test-protobuf.pb.cc"
106 "${CMAKE_CURRENT_LIST_DIR}/test-protobuf.cpp"
107 CMAKE_FLAGS
108 CMAKE_EXE_LINKER_FLAGS ${CMAKE_TRY_COMPILE_LINKER_FLAGS}
109 "-DINCLUDE_DIRECTORIES=${Protobuf_INCLUDE_DIR};${CMAKE_BINARY_DIR}"
110 "-DCMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD}"
111 LINK_LIBRARIES "${PROTOBUF_TRYCOMPILE_LINKER}" ${CMAKE_TRY_COMPILE_LINK_LIBRARIES}
112 OUTPUT_VARIABLE OUTPUT
113 )
114 if(NOT Protobuf_COMPILE_TEST_PASSED)
115 trezor_fatal_msg("Trezor: Protobuf Compilation test failed: ${OUTPUT}.")
116 endif()
117 else ()
118 message(STATUS "Trezor: Protobuf Compilation test skipped, build may fail later")
119 endif()
120 endif()
121
122 # Try to build protobuf messages
123 if(Protobuf_FOUND AND USE_DEVICE_TREZOR)
124 # .proto files to compile
125 set(_proto_files "messages.proto"
126 "messages-common.proto"
127 "messages-management.proto"
128 "messages-monero.proto")
129 if (TREZOR_DEBUG)
130 list(APPEND _proto_files "messages-debug.proto")
131 endif ()
132
133 set(_proto_include_dir "${CMAKE_CURRENT_LIST_DIR}/../src/device_trezor/trezor/protob")
134 set(_proto_files_absolute)
135 foreach(file IN LISTS _proto_files)
136 list(APPEND _proto_files_absolute "${_proto_include_dir}/${file}")
137 endforeach ()
138
139 execute_process(COMMAND ${Protobuf_PROTOC_EXECUTABLE} --cpp_out "${CMAKE_CURRENT_LIST_DIR}/../src/device_trezor/trezor/messages" "-I${_proto_include_dir}" ${_proto_files_absolute} RESULT_VARIABLE RET OUTPUT_VARIABLE OUT ERROR_VARIABLE ERR)
140 if(RET)
141 trezor_fatal_msg("Trezor: protobuf messages could not be (re)generated (err=${RET}). OUT: ${OUT}, ERR: ${ERR}.")
142 endif()
143
144 message(STATUS "Trezor: protobuf messages regenerated out.")
145 set(DEVICE_TREZOR_READY 1)
146 add_definitions(-DDEVICE_TREZOR_READY=1)
147 add_definitions(-DPROTOBUF_INLINE_NOT_IN_HEADERS=0)
148
149 if(CMAKE_BUILD_TYPE STREQUAL "Debug")
150 add_definitions(-DTREZOR_DEBUG=1)
151 endif()
152
153 if(USE_DEVICE_TREZOR_UDP_RELEASE)
154 message(STATUS "Trezor: UDP transport enabled (emulator)")
155 add_definitions(-DUSE_DEVICE_TREZOR_UDP_RELEASE=1)
156 endif()
157
158 if (Protobuf_INCLUDE_DIR)
159 include_directories(${Protobuf_INCLUDE_DIR})
160 endif()
161
162 # LibUSB support, check for particular version
163 # Include support only if compilation test passes
164 if (USE_DEVICE_TREZOR_LIBUSB)
165 find_package(LibUSB)
166 endif()
167
168 if (LibUSB_COMPILE_TEST_PASSED)
169 add_definitions(-DHAVE_TREZOR_LIBUSB=1)
170 if(LibUSB_INCLUDE_DIRS)
171 include_directories(${LibUSB_INCLUDE_DIRS})
172 endif()
173 endif()
174
175 set(TREZOR_LIBUSB_LIBRARIES "")
176 if(LibUSB_COMPILE_TEST_PASSED)
177 list(APPEND TREZOR_LIBUSB_LIBRARIES ${LibUSB_LIBRARIES} ${LIBUSB_DEP_LINKER})
178 message(STATUS "Trezor: compatible LibUSB found at: ${LibUSB_INCLUDE_DIRS}")
179 elseif(USE_DEVICE_TREZOR_LIBUSB AND NOT ANDROID)
180 trezor_fatal_msg("Trezor: LibUSB not found or test failed, please install libusb-1.0.26")
181 endif()
182
183 if (BUILD_GUI_DEPS)
184 set(TREZOR_DEP_LIBS "")
185 set(TREZOR_DEP_LINKER "")
186
187 if (Protobuf_LIBRARY)
188 list(APPEND TREZOR_DEP_LIBS ${Protobuf_LIBRARY})
189 string(APPEND TREZOR_DEP_LINKER " -lprotobuf")
190 endif()
191
192 if (TREZOR_LIBUSB_LIBRARIES)
193 list(APPEND TREZOR_DEP_LIBS ${TREZOR_LIBUSB_LIBRARIES})
194 string(APPEND TREZOR_DEP_LINKER " -lusb-1.0 ${LIBUSB_DEP_LINKER}")
195 endif()
196 endif()
197 endif()