Debian Codegraph @

cmake/CheckTrezor.cmake

raw
1 OPTION(USE_DEVICE_TREZOR "Trezor support compilation" ON)
2 OPTION(USE_DEVICE_TREZOR_LIBUSB "Trezor LibUSB compilation" ON)
3 OPTION(USE_DEVICE_TREZOR_UDP_RELEASE "Trezor UdpTransport in release mode" OFF)
4 OPTION(USE_DEVICE_TREZOR_DEBUG "Trezor Debugging enabled" OFF)
5 OPTION(TREZOR_DEBUG "Main trezor debugging switch" OFF)
6
7 # Helper function to fix cmake < 3.6.0 FindProtobuf variables
8 function(_trezor_protobuf_fix_vars)
9 if(${CMAKE_VERSION} VERSION_LESS "3.6.0")
10 foreach(UPPER
11 PROTOBUF_SRC_ROOT_FOLDER
12 PROTOBUF_IMPORT_DIRS
13 PROTOBUF_DEBUG
14 PROTOBUF_LIBRARY
15 PROTOBUF_PROTOC_LIBRARY
16 PROTOBUF_INCLUDE_DIR
17 PROTOBUF_PROTOC_EXECUTABLE
18 PROTOBUF_LIBRARY_DEBUG
19 PROTOBUF_PROTOC_LIBRARY_DEBUG
20 PROTOBUF_LITE_LIBRARY
21 PROTOBUF_LITE_LIBRARY_DEBUG
22 )
23 if (DEFINED ${UPPER})
24 string(REPLACE "PROTOBUF_" "Protobuf_" Camel ${UPPER})
25 if (NOT DEFINED ${Camel})
26 set(${Camel} ${${UPPER}} PARENT_SCOPE)
27 endif()
28 endif()
29 endforeach()
30 endif()
31 endfunction()
32
33 # Use Trezor master switch
34 if (USE_DEVICE_TREZOR)
35 # Protobuf is required to build protobuf messages for Trezor
36 include(FindProtobuf OPTIONAL)
37 find_package(Protobuf)
38 _trezor_protobuf_fix_vars()
39
40 # Protobuf handling the cache variables set in docker.
41 if(NOT Protobuf_FOUND AND NOT Protobuf_LIBRARY AND NOT Protobuf_PROTOC_EXECUTABLE AND NOT Protobuf_INCLUDE_DIR)
42 message(STATUS "Could not find Protobuf")
43 elseif(NOT Protobuf_LIBRARY OR NOT EXISTS "${Protobuf_LIBRARY}")
44 message(STATUS "Protobuf library not found: ${Protobuf_LIBRARY}")
45 unset(Protobuf_FOUND)
46 elseif(NOT Protobuf_PROTOC_EXECUTABLE OR NOT EXISTS "${Protobuf_PROTOC_EXECUTABLE}")
47 message(STATUS "Protobuf executable not found: ${Protobuf_PROTOC_EXECUTABLE}")
48 unset(Protobuf_FOUND)
49 elseif(NOT Protobuf_INCLUDE_DIR OR NOT EXISTS "${Protobuf_INCLUDE_DIR}")
50 message(STATUS "Protobuf include dir not found: ${Protobuf_INCLUDE_DIR}")
51 unset(Protobuf_FOUND)
52 else()
53 message(STATUS "Protobuf lib: ${Protobuf_LIBRARY}, inc: ${Protobuf_INCLUDE_DIR}, protoc: ${Protobuf_PROTOC_EXECUTABLE}")
54 set(Protobuf_INCLUDE_DIRS ${Protobuf_INCLUDE_DIR})
55 set(Protobuf_FOUND 1) # override found if all rquired info was provided by variables
56 endif()
57
58 if (Protobuf_VERSION VERSION_GREATER_EQUAL 22.0)
59 add_definitions(-DPROTOBUF_HAS_ABSEIL)
60 endif()
61
62 if(TREZOR_DEBUG)
63 set(USE_DEVICE_TREZOR_DEBUG 1)
64 endif()
65
66 # Compile debugging support (for tests)
67 if (USE_DEVICE_TREZOR_DEBUG)
68 add_definitions(-DWITH_TREZOR_DEBUGGING=1)
69 endif()
70 else()
71 message(STATUS "Trezor support disabled by USE_DEVICE_TREZOR")
72 endif()
73
74 if(Protobuf_FOUND AND USE_DEVICE_TREZOR)
75 if (NOT "$ENV{TREZOR_PYTHON}" STREQUAL "")
76 set(TREZOR_PYTHON "$ENV{TREZOR_PYTHON}" CACHE INTERNAL "Copied from environment variable TREZOR_PYTHON")
77 else()
78 find_package(Python QUIET COMPONENTS Interpreter) # cmake 3.12+
79 if(Python_Interpreter_FOUND)
80 set(TREZOR_PYTHON "${Python_EXECUTABLE}")
81 endif()
82 endif()
83
84 if(NOT TREZOR_PYTHON)
85 find_package(PythonInterp)
86 if(PYTHONINTERP_FOUND AND PYTHON_EXECUTABLE)
87 set(TREZOR_PYTHON "${PYTHON_EXECUTABLE}")
88 endif()
89 endif()
90
91 if(NOT TREZOR_PYTHON)
92 message(STATUS "Trezor: Python not found")
93 endif()
94 endif()
95
96 # Protobuf compilation test
97 if(Protobuf_FOUND AND USE_DEVICE_TREZOR AND TREZOR_PYTHON)
98 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)
99 if(RET)
100 message(STATUS "Protobuf test generation failed: ${OUT} ${ERR}")
101 endif()
102
103 try_compile(Protobuf_COMPILE_TEST_PASSED
104 "${CMAKE_BINARY_DIR}"
105 SOURCES
106 "${CMAKE_BINARY_DIR}/test-protobuf.pb.cc"
107 "${CMAKE_CURRENT_LIST_DIR}/test-protobuf.cpp"
108 CMAKE_FLAGS
109 "-DINCLUDE_DIRECTORIES=${Protobuf_INCLUDE_DIR};${CMAKE_BINARY_DIR}"
110 "-DCMAKE_CXX_STANDARD=11"
111 LINK_LIBRARIES ${Protobuf_LIBRARY}
112 OUTPUT_VARIABLE OUTPUT
113 )
114 if(NOT Protobuf_COMPILE_TEST_PASSED)
115 message(STATUS "Protobuf Compilation test failed: ${OUTPUT}.")
116 endif()
117 endif()
118
119 # Try to build protobuf messages
120 if(Protobuf_FOUND AND USE_DEVICE_TREZOR AND TREZOR_PYTHON AND Protobuf_COMPILE_TEST_PASSED)
121 set(ENV{PROTOBUF_INCLUDE_DIRS} "${Protobuf_INCLUDE_DIR}")
122 set(ENV{PROTOBUF_PROTOC_EXECUTABLE} "${Protobuf_PROTOC_EXECUTABLE}")
123 set(TREZOR_PROTOBUF_PARAMS "")
124 if (USE_DEVICE_TREZOR_DEBUG)
125 set(TREZOR_PROTOBUF_PARAMS "--debug")
126 endif()
127
128 execute_process(COMMAND ${TREZOR_PYTHON} tools/build_protob.py ${TREZOR_PROTOBUF_PARAMS} WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/../src/device_trezor/trezor RESULT_VARIABLE RET OUTPUT_VARIABLE OUT ERROR_VARIABLE ERR)
129 if(RET)
130 message(WARNING "Trezor protobuf messages could not be regenerated (err=${RET}, python ${PYTHON})."
131 "OUT: ${OUT}, ERR: ${ERR}."
132 "Please read src/device_trezor/trezor/tools/README.md")
133 else()
134 message(STATUS "Trezor protobuf messages regenerated out: \"${OUT}.\"")
135 set(DEVICE_TREZOR_READY 1)
136 add_definitions(-DDEVICE_TREZOR_READY=1)
137 add_definitions(-DPROTOBUF_INLINE_NOT_IN_HEADERS=0)
138
139 if(CMAKE_BUILD_TYPE STREQUAL "Debug")
140 add_definitions(-DTREZOR_DEBUG=1)
141 endif()
142
143 if(USE_DEVICE_TREZOR_UDP_RELEASE)
144 add_definitions(-DUSE_DEVICE_TREZOR_UDP_RELEASE=1)
145 endif()
146
147 if (Protobuf_INCLUDE_DIR)
148 include_directories(${Protobuf_INCLUDE_DIR})
149 endif()
150
151 # LibUSB support, check for particular version
152 # Include support only if compilation test passes
153 if (USE_DEVICE_TREZOR_LIBUSB)
154 find_package(LibUSB)
155 endif()
156
157 if (LibUSB_COMPILE_TEST_PASSED)
158 add_definitions(-DHAVE_TREZOR_LIBUSB=1)
159 if(LibUSB_INCLUDE_DIRS)
160 include_directories(${LibUSB_INCLUDE_DIRS})
161 endif()
162 endif()
163
164 set(TREZOR_LIBUSB_LIBRARIES "")
165 if(LibUSB_COMPILE_TEST_PASSED)
166 list(APPEND TREZOR_LIBUSB_LIBRARIES ${LibUSB_LIBRARIES} ${LIBUSB_DEP_LINKER})
167 message(STATUS "Trezor compatible LibUSB found at: ${LibUSB_INCLUDE_DIRS}")
168 endif()
169
170 if (BUILD_GUI_DEPS)
171 set(TREZOR_DEP_LIBS "")
172 set(TREZOR_DEP_LINKER "")
173
174 if (Protobuf_LIBRARY)
175 list(APPEND TREZOR_DEP_LIBS ${Protobuf_LIBRARY})
176 string(APPEND TREZOR_DEP_LINKER " -lprotobuf")
177 endif()
178
179 if (TREZOR_LIBUSB_LIBRARIES)
180 list(APPEND TREZOR_DEP_LIBS ${TREZOR_LIBUSB_LIBRARIES})
181 string(APPEND TREZOR_DEP_LINKER " -lusb-1.0 ${LIBUSB_DEP_LINKER}")
182 endif()
183 endif()
184 endif()
185 endif()