Skip to content

Commit 7653b5d

Browse files
committed
build: cleanup CheckTrezor
1 parent a1dc85c commit 7653b5d

File tree

12 files changed

+39
-764
lines changed

12 files changed

+39
-764
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,7 @@ jobs:
6868
- uses: msys2/setup-msys2@v2
6969
with:
7070
update: true
71-
install: mingw-w64-x86_64-toolchain make mingw-w64-x86_64-cmake mingw-w64-x86_64-ccache mingw-w64-x86_64-boost mingw-w64-x86_64-openssl mingw-w64-x86_64-zeromq mingw-w64-x86_64-libsodium mingw-w64-x86_64-hidapi mingw-w64-x86_64-libusb mingw-w64-x86_64-unbound git
72-
- shell: msys2 {0}
73-
run: |
74-
curl -O https://repo.msys2.org/mingw/mingw64/mingw-w64-x86_64-protobuf-c-1.4.1-1-any.pkg.tar.zst
75-
curl -O https://repo.msys2.org/mingw/mingw64/mingw-w64-x86_64-protobuf-21.9-1-any.pkg.tar.zst
76-
pacman --noconfirm -U mingw-w64-x86_64-protobuf-c-1.4.1-1-any.pkg.tar.zst mingw-w64-x86_64-protobuf-21.9-1-any.pkg.tar.zst
71+
install: mingw-w64-x86_64-toolchain make mingw-w64-x86_64-cmake mingw-w64-x86_64-ccache mingw-w64-x86_64-boost mingw-w64-x86_64-openssl mingw-w64-x86_64-zeromq mingw-w64-x86_64-libsodium mingw-w64-x86_64-hidapi mingw-w64-x86_64-libusb mingw-w64-x86_64-unbound mingw-w64-x86_64-protobuf mingw-w64-x86_64-protobuf-c git
7772
- name: build
7873
run: |
7974
${{env.CCACHE_SETTINGS}}

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ all: release-all
4848
4949
depends:
5050
cd contrib/depends && $(MAKE) HOST=$(target) && cd ../.. && mkdir -p build/$(target)/release
51-
cd build/$(target)/release && USE_DEVICE_TREZOR_MANDATORY=1 cmake -DCMAKE_TOOLCHAIN_FILE=$(CURDIR)/contrib/depends/$(target)/share/toolchain.cmake ../../.. && $(MAKE)
51+
cd build/$(target)/release && cmake -DUSE_DEVICE_TREZOR_MANDATORY=1 -DCMAKE_TOOLCHAIN_FILE=$(CURDIR)/contrib/depends/$(target)/share/toolchain.cmake ../../.. && $(MAKE)
5252
5353
cmake-debug:
5454
mkdir -p $(builddir)/debug

cmake/CheckTrezor.cmake

Lines changed: 36 additions & 174 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,10 @@
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-
171
# 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-
# Helper function to fix cmake < 3.6.0 FindProtobuf variables
27-
function(_trezor_protobuf_fix_vars)
28-
if(${CMAKE_VERSION} VERSION_LESS "3.6.0")
29-
foreach(UPPER
30-
PROTOBUF_SRC_ROOT_FOLDER
31-
PROTOBUF_IMPORT_DIRS
32-
PROTOBUF_DEBUG
33-
PROTOBUF_LIBRARY
34-
PROTOBUF_PROTOC_LIBRARY
35-
PROTOBUF_INCLUDE_DIR
36-
PROTOBUF_PROTOC_EXECUTABLE
37-
PROTOBUF_LIBRARY_DEBUG
38-
PROTOBUF_PROTOC_LIBRARY_DEBUG
39-
PROTOBUF_LITE_LIBRARY
40-
PROTOBUF_LITE_LIBRARY_DEBUG
41-
)
42-
if (DEFINED ${UPPER})
43-
string(REPLACE "PROTOBUF_" "Protobuf_" Camel ${UPPER})
44-
if (NOT DEFINED ${Camel})
45-
set(${Camel} ${${UPPER}} PARENT_SCOPE)
46-
endif()
47-
endif()
48-
endforeach()
49-
endif()
50-
endfunction()
2+
OPTION(USE_DEVICE_TREZOR "Trezor support compilation" ON)
3+
OPTION(USE_DEVICE_TREZOR_MANDATORY "Trezor compilation is mandatory, fail build if Trezor support cannot be compiled" OFF)
4+
OPTION(USE_DEVICE_TREZOR_LIBUSB "Trezor LibUSB compilation" ON)
5+
OPTION(USE_DEVICE_TREZOR_UDP_RELEASE "Trezor UdpTransport in release mode" OFF)
6+
OPTION(USE_DEVICE_TREZOR_DEBUG "Trezor Debugging enabled" OFF)
7+
OPTION(TREZOR_DEBUG "Main Trezor debugging switch" OFF)
518

529
macro(trezor_fatal_msg msg)
5310
if ($ENV{USE_DEVICE_TREZOR_MANDATORY})
@@ -71,134 +28,54 @@ macro(trezor_fatal_msg msg)
7128
endmacro()
7229

7330
# Use Trezor master switch
74-
if (USE_DEVICE_TREZOR)
75-
# Protobuf is required to build protobuf messages for Trezor
76-
include(FindProtobuf OPTIONAL)
77-
78-
# PkgConfig works better with new Protobuf
79-
find_package(PkgConfig QUIET)
80-
pkg_check_modules(PROTOBUF protobuf)
31+
if (NOT USE_DEVICE_TREZOR)
32+
message(STATUS "Trezor: support disabled by USE_DEVICE_TREZOR")
33+
else()
34+
# Look for ProtobufConfig.cmake, provided by Protobuf
35+
find_package(Protobuf CONFIG)
8136

37+
# Look for FindProtobuf.cmake, provided by CMake
8238
if (NOT Protobuf_FOUND)
83-
FIND_PACKAGE(Protobuf CONFIG)
39+
find_package(Protobuf)
8440
endif()
41+
8542
if (NOT Protobuf_FOUND)
86-
FIND_PACKAGE(Protobuf)
43+
trezor_fatal_msg("Trezor library not found")
8744
endif()
45+
endif()
8846

89-
_trezor_protobuf_fix_vars()
90-
91-
# Early fail for optional Trezor support
92-
if(NOT Protobuf_FOUND AND NOT Protobuf_LIBRARY AND NOT Protobuf_PROTOC_EXECUTABLE AND NOT Protobuf_INCLUDE_DIR)
93-
trezor_fatal_msg("Trezor: Could not find Protobuf")
94-
elseif(${CMAKE_CXX_STANDARD} LESS 17 AND ${Protobuf_VERSION} GREATER 21)
95-
trezor_fatal_msg("Trezor: Unsupported Protobuf version ${Protobuf_VERSION} with C++ ${CMAKE_CXX_STANDARD}. Please, use Protobuf v21.")
96-
elseif(NOT Protobuf_LIBRARY)
97-
trezor_fatal_msg("Trezor: Protobuf library not found: ${Protobuf_LIBRARY}")
98-
unset(Protobuf_FOUND)
99-
elseif(NOT Protobuf_PROTOC_EXECUTABLE OR NOT EXISTS "${Protobuf_PROTOC_EXECUTABLE}")
100-
trezor_fatal_msg("Trezor: Protobuf executable not found: ${Protobuf_PROTOC_EXECUTABLE}")
101-
unset(Protobuf_FOUND)
102-
elseif(NOT Protobuf_INCLUDE_DIR OR NOT EXISTS "${Protobuf_INCLUDE_DIR}")
103-
trezor_fatal_msg("Trezor: Protobuf include dir not found: ${Protobuf_INCLUDE_DIR}")
104-
unset(Protobuf_FOUND)
105-
else()
106-
message(STATUS "Trezor: Protobuf lib: ${Protobuf_LIBRARY}, inc: ${Protobuf_INCLUDE_DIR}, protoc: ${Protobuf_PROTOC_EXECUTABLE}")
107-
set(Protobuf_INCLUDE_DIRS ${Protobuf_INCLUDE_DIR})
108-
set(Protobuf_FOUND 1) # override found if all required info was provided by variables
109-
endif()
47+
# Try to build protobuf messages
48+
if(Protobuf_FOUND AND USE_DEVICE_TREZOR)
49+
find_program(Protobuf_PROTOC_EXECUTABLE protoc REQUIRED)
11050

11151
if(TREZOR_DEBUG)
11252
set(USE_DEVICE_TREZOR_DEBUG 1)
11353
message(STATUS "Trezor: debug build enabled")
114-
endif()
115-
116-
# Compile debugging support (for tests)
117-
if (USE_DEVICE_TREZOR_DEBUG)
54+
# Compile debugging support (for tests)
11855
add_definitions(-DWITH_TREZOR_DEBUGGING=1)
11956
endif()
120-
else()
121-
message(STATUS "Trezor: support disabled by USE_DEVICE_TREZOR")
122-
endif()
123-
124-
if(Protobuf_FOUND AND USE_DEVICE_TREZOR)
125-
if (NOT "$ENV{TREZOR_PYTHON}" STREQUAL "")
126-
set(TREZOR_PYTHON "$ENV{TREZOR_PYTHON}" CACHE INTERNAL "Copied from environment variable TREZOR_PYTHON")
127-
else()
128-
find_package(Python QUIET COMPONENTS Interpreter) # cmake 3.12+
129-
if(Python_Interpreter_FOUND)
130-
set(TREZOR_PYTHON "${Python_EXECUTABLE}")
131-
endif()
132-
endif()
13357

134-
if(NOT TREZOR_PYTHON)
135-
find_package(PythonInterp)
136-
if(PYTHONINTERP_FOUND AND PYTHON_EXECUTABLE)
137-
set(TREZOR_PYTHON "${PYTHON_EXECUTABLE}")
138-
endif()
139-
endif()
58+
# .proto files to compile
59+
set(_proto_files "messages.proto"
60+
"messages-common.proto"
61+
"messages-management.proto"
62+
"messages-monero.proto")
63+
if (TREZOR_DEBUG)
64+
list(APPEND _proto_files "messages-debug.proto")
65+
endif ()
14066

141-
if(NOT TREZOR_PYTHON)
142-
trezor_fatal_msg("Trezor: Python not found")
143-
endif()
144-
endif()
67+
set(_include_dir "${CMAKE_CURRENT_LIST_DIR}/../external/trezor-common/protob")
68+
set(_proto_files_absolute)
69+
foreach(file IN LISTS _proto_files)
70+
list(APPEND _proto_files_absolute "${_include_dir}/${file}")
71+
endforeach ()
14572

146-
# Protobuf compilation test
147-
if(Protobuf_FOUND AND USE_DEVICE_TREZOR AND TREZOR_PYTHON)
148-
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)
73+
execute_process(COMMAND ${Protobuf_PROTOC_EXECUTABLE} --cpp_out "${CMAKE_CURRENT_LIST_DIR}/../src/device_trezor/trezor/messages" "-I${_include_dir}" ${_proto_files_absolute} RESULT_VARIABLE RET OUTPUT_VARIABLE OUT ERROR_VARIABLE ERR)
14974
if(RET)
150-
trezor_fatal_msg("Trezor: Protobuf test generation failed: ${OUT} ${ERR}")
151-
endif()
152-
153-
if(ANDROID)
154-
set(CMAKE_TRY_COMPILE_LINKER_FLAGS "${CMAKE_TRY_COMPILE_LINKER_FLAGS} -llog")
155-
set(CMAKE_TRY_COMPILE_LINK_LIBRARIES "${CMAKE_TRY_COMPILE_LINK_LIBRARIES} log")
75+
trezor_fatal_msg("Trezor: protobuf messages could not be (re)generated (err=${RET}). OUT: ${OUT}, ERR: ${ERR}.")
15676
endif()
15777

158-
if(USE_DEVICE_TREZOR_PROTOBUF_TEST)
159-
if(PROTOBUF_LDFLAGS)
160-
set(PROTOBUF_TRYCOMPILE_LINKER "${PROTOBUF_LDFLAGS}")
161-
else()
162-
set(PROTOBUF_TRYCOMPILE_LINKER "${Protobuf_LIBRARY}")
163-
endif()
164-
165-
try_compile(Protobuf_COMPILE_TEST_PASSED
166-
"${CMAKE_BINARY_DIR}"
167-
SOURCES
168-
"${CMAKE_BINARY_DIR}/test-protobuf.pb.cc"
169-
"${CMAKE_CURRENT_LIST_DIR}/test-protobuf.cpp"
170-
CMAKE_FLAGS
171-
CMAKE_EXE_LINKER_FLAGS ${CMAKE_TRY_COMPILE_LINKER_FLAGS}
172-
"-DINCLUDE_DIRECTORIES=${Protobuf_INCLUDE_DIR};${CMAKE_BINARY_DIR}"
173-
"-DCMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD}"
174-
LINK_LIBRARIES "${PROTOBUF_TRYCOMPILE_LINKER}" ${CMAKE_TRY_COMPILE_LINK_LIBRARIES}
175-
OUTPUT_VARIABLE OUTPUT
176-
)
177-
if(NOT Protobuf_COMPILE_TEST_PASSED)
178-
trezor_fatal_msg("Trezor: Protobuf Compilation test failed: ${OUTPUT}.")
179-
endif()
180-
else ()
181-
message(STATUS "Trezor: Protobuf Compilation test skipped, build may fail later")
182-
endif()
183-
endif()
184-
185-
# Try to build protobuf messages
186-
if(Protobuf_FOUND AND USE_DEVICE_TREZOR AND TREZOR_PYTHON)
187-
set(ENV{PROTOBUF_INCLUDE_DIRS} "${Protobuf_INCLUDE_DIR}")
188-
set(ENV{PROTOBUF_PROTOC_EXECUTABLE} "${Protobuf_PROTOC_EXECUTABLE}")
189-
set(TREZOR_PROTOBUF_PARAMS "")
190-
if (USE_DEVICE_TREZOR_DEBUG)
191-
set(TREZOR_PROTOBUF_PARAMS "--debug")
192-
endif()
193-
194-
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)
195-
if(RET)
196-
trezor_fatal_msg("Trezor: protobuf messages could not be regenerated (err=${RET}, python ${PYTHON})."
197-
"OUT: ${OUT}, ERR: ${ERR}."
198-
"Please read src/device_trezor/trezor/tools/README.md")
199-
endif()
200-
201-
message(STATUS "Trezor: protobuf messages regenerated out: \"${OUT}.\"")
78+
message(STATUS "Trezor: protobuf messages regenerated")
20279
set(DEVICE_TREZOR_READY 1)
20380
add_definitions(-DDEVICE_TREZOR_READY=1)
20481
add_definitions(-DPROTOBUF_INLINE_NOT_IN_HEADERS=0)
@@ -236,19 +113,4 @@ if(Protobuf_FOUND AND USE_DEVICE_TREZOR AND TREZOR_PYTHON)
236113
elseif(USE_DEVICE_TREZOR_LIBUSB AND NOT ANDROID)
237114
trezor_fatal_msg("Trezor: LibUSB not found or test failed, please install libusb-1.0.26")
238115
endif()
239-
240-
if (BUILD_GUI_DEPS)
241-
set(TREZOR_DEP_LIBS "")
242-
set(TREZOR_DEP_LINKER "")
243-
244-
if (Protobuf_LIBRARY)
245-
list(APPEND TREZOR_DEP_LIBS ${Protobuf_LIBRARY})
246-
string(APPEND TREZOR_DEP_LINKER " -lprotobuf")
247-
endif()
248-
249-
if (TREZOR_LIBUSB_LIBRARIES)
250-
list(APPEND TREZOR_DEP_LIBS ${TREZOR_LIBUSB_LIBRARIES})
251-
string(APPEND TREZOR_DEP_LINKER " -lusb-1.0 ${LIBUSB_DEP_LINKER}")
252-
endif()
253-
endif()
254116
endif()

cmake/test-protobuf.cpp

Lines changed: 0 additions & 43 deletions
This file was deleted.

cmake/test-protobuf.proto

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/device_trezor/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ if(DEVICE_TREZOR_READY)
9494
common
9595
${sodium_LIBRARIES}
9696
${Boost_CHRONO_LIBRARY}
97-
${Protobuf_LIBRARY}
97+
protobuf::libprotobuf
9898
${TREZOR_LIBUSB_LIBRARIES}
9999
PRIVATE
100100
${EXTRA_LIBRARIES}

src/device_trezor/trezor/tools/README.md

Lines changed: 0 additions & 45 deletions
This file was deleted.

0 commit comments

Comments
 (0)