Skip to content

Commit 3368840

Browse files
committed
factor out getting buf into helper function
1 parent 94f3cfd commit 3368840

File tree

3 files changed

+52
-62
lines changed

3 files changed

+52
-62
lines changed

src/viam/api/CMakeLists.txt

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -55,49 +55,9 @@ list(JOIN BUF_PROTO_COMPONENTS , BUF_PROTO_COMPONENTS_JOINED)
5555

5656
if (VIAMCPPSDK_USE_DYNAMIC_PROTOS)
5757

58-
# Look for the `buf` command in the usual places, and use it if
59-
# found. If we can't find it, try to download it and use that.
60-
#
61-
# TODO: File an upstream issue with `buf.build` to add
62-
# `find_package` support for it, then use it.
63-
#
64-
find_program(BUF_COMMAND buf)
65-
if (NOT BUF_COMMAND)
66-
67-
set(HOST_SYSTEM_PROCESSOR_FOR_BUF_DOWNLOAD ${CMAKE_HOST_SYSTEM_PROCESSOR})
68-
if (CMAKE_HOST_WIN32)
69-
if (HOST_SYSTEM_PROCESSOR_FOR_BUF_DOWNLOAD STREQUAL "AMD64")
70-
set(HOST_SYSTEM_PROCESSOR_FOR_BUF_DOWNLOAD x86_64)
71-
elseif (HOST_SYSTEM_PROCESSOR_FOR_BUF_DOWNLOAD STREQUAL "ARM64")
72-
set(HOST_SYSTEM_PROCESSOR_FOR_BUF_DOWNLOAD arm64)
73-
else()
74-
message(FATAL_ERROR "Unknown Windows platform to correct buf download URL: ${HOST_SYSTEM_PROCESSOR_FOR_BUF_DOWNLOAD}")
75-
endif()
76-
endif()
77-
78-
set(BUF_DOWNLOAD_URL https://github.com/bufbuild/buf/releases/latest/download/buf-${CMAKE_HOST_SYSTEM_NAME}-${HOST_SYSTEM_PROCESSOR_FOR_BUF_DOWNLOAD}${CMAKE_HOST_EXECUTABLE_SUFFIX})
79-
80-
message(STATUS "system name ${CMAKE_SYSTEM_NAME} host name ${CMAKE_HOST_SYSTEM_NAME}")
81-
message(STATUS "exe suffix ${CMAKE_EXECUTABLE_SUFFIX} host suffix ${CMAKE_HOST_EXECUTABLE_SUFFIX}")
82-
83-
file(
84-
DOWNLOAD
85-
${BUF_DOWNLOAD_URL}
86-
${CMAKE_CURRENT_BINARY_DIR}/buf_latest${CMAKE_HOST_EXECUTABLE_SUFFIX}
87-
STATUS buf_status
88-
)
89-
list(GET buf_status 0 buf_status_code)
90-
list(GET buf_status 1 buf_status_string)
91-
92-
if(NOT buf_status_code EQUAL 0)
93-
message(FATAL_ERROR "No local `buf` program found (try setting PATH?) and failed to download: ${buf_status_string} for ${BUF_DOWNLOAD_URL}")
94-
endif()
95-
96-
set(BUF_COMMAND ${CMAKE_CURRENT_BINARY_DIR}/buf_latest${CMAKE_HOST_EXECUTABLE_SUFFIX})
97-
file(CHMOD ${BUF_COMMAND} PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE)
98-
endif()
58+
viamcppsdk_get_buf()
9959

100-
# TODO: IDeally we could just generate to `CMAKE_CURRENT_BINARY_DIR`
60+
# TODO: Ideally we could just generate to `CMAKE_CURRENT_BINARY_DIR`
10161
# and everything would work just fine. However, that directory also
10262
# has intermediate cmake state in it, so we can't use it as a source
10363
# for a directory copy to update the static protos, as we do

src/viam/cmake/viamcppsdk_utils.cmake

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,50 @@ function(viamcppsdk_link_viam_api TARGET_LIBRARY)
2525
target_link_libraries(${TARGET_LIBRARY} ${ARGV1} "$<LINK_LIBRARY:WHOLE_ARCHIVE,viam-cpp-sdk::viamapi>")
2626
endif()
2727
endfunction()
28+
29+
# Look for the `buf` command in the usual places, and use it if
30+
# found. If we can't find it, try to download it and use that.
31+
#
32+
# TODO: File an upstream issue with `buf.build` to add
33+
# `find_package` support for it, then use it.
34+
#
35+
function(viamcppsdk_get_buf)
36+
if (BUF_COMMAND)
37+
return()
38+
endif()
39+
40+
find_program(BUF_COMMAND buf)
41+
if (BUF_COMMAND)
42+
return()
43+
endif()
44+
45+
set(HOST_SYSTEM_PROCESSOR_FOR_BUF_DOWNLOAD ${CMAKE_HOST_SYSTEM_PROCESSOR})
46+
if (CMAKE_HOST_WIN32)
47+
if (HOST_SYSTEM_PROCESSOR_FOR_BUF_DOWNLOAD STREQUAL "AMD64")
48+
set(HOST_SYSTEM_PROCESSOR_FOR_BUF_DOWNLOAD x86_64)
49+
elseif (HOST_SYSTEM_PROCESSOR_FOR_BUF_DOWNLOAD STREQUAL "ARM64")
50+
set(HOST_SYSTEM_PROCESSOR_FOR_BUF_DOWNLOAD arm64)
51+
else()
52+
message(FATAL_ERROR "Unknown Windows platform to correct buf download URL: ${HOST_SYSTEM_PROCESSOR_FOR_BUF_DOWNLOAD}")
53+
endif()
54+
endif()
55+
56+
set(BUF_DOWNLOAD_URL https://github.com/bufbuild/buf/releases/latest/download/buf-${CMAKE_HOST_SYSTEM_NAME}-${HOST_SYSTEM_PROCESSOR_FOR_BUF_DOWNLOAD}${CMAKE_HOST_EXECUTABLE_SUFFIX})
57+
58+
file(
59+
DOWNLOAD
60+
${BUF_DOWNLOAD_URL}
61+
${CMAKE_CURRENT_BINARY_DIR}/buf_latest${CMAKE_HOST_EXECUTABLE_SUFFIX}
62+
STATUS buf_status
63+
)
64+
65+
list(GET buf_status 0 buf_status_code)
66+
list(GET buf_status 1 buf_status_string)
67+
68+
if(NOT buf_status_code EQUAL 0)
69+
message(FATAL_ERROR "No local `buf` program found (try setting PATH?) and failed to download: ${buf_status_string} for ${BUF_DOWNLOAD_URL}")
70+
endif()
71+
72+
set(BUF_COMMAND ${CMAKE_CURRENT_BINARY_DIR}/buf_latest${CMAKE_HOST_EXECUTABLE_SUFFIX} CACHE INTERNAL "buf command")
73+
file(CHMOD ${BUF_COMMAND} PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE)
74+
endfunction()

src/viam/examples/modules/complex/CMakeLists.txt

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -36,26 +36,9 @@ set(MODULE_PROTO_OUTPUT_FILES
3636
${MODULE_PROTO_GEN_DIR}/summation.pb.h
3737
)
3838

39-
# Look for the `buf` command in the usual places, and use it if found. If we
40-
# can't find it, try to download it and use that.
41-
find_program(BUF_COMMAND buf)
42-
if (NOT BUF_COMMAND)
43-
file(
44-
DOWNLOAD
45-
https://github.com/bufbuild/buf/releases/latest/download/buf-${CMAKE_HOST_SYSTEM_NAME}-${CMAKE_HOST_SYSTEM_PROCESSOR}
46-
${CMAKE_CURRENT_BINARY_DIR}/buf_latest
47-
STATUS buf_status
48-
)
49-
list(GET buf_status 0 buf_status_code)
50-
list(GET buf_status 1 buf_status_string)
51-
52-
if(NOT buf_status_code EQUAL 0)
53-
message(FATAL_ERROR "No local `buf` program found (try setting PATH?) and failed to download: ${buf_status_string}")
54-
endif()
55-
56-
set(BUF_COMMAND ${CMAKE_CURRENT_BINARY_DIR}/buf_latest)
57-
file(CHMOD ${BUF_COMMAND} PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE)
58-
endif()
39+
# Try to find buf in the path or get it online.
40+
# See the definition of this function for logic that can be adapted to other project files.
41+
viamcppsdk_get_buf()
5942

6043
if ((NOT VIAMCPPSDK_OFFLINE_PROTO_GENERATION) AND VIAMCPPSDK_BUF_REMOTE_PLUGIN_SUPPORTED)
6144
configure_file(

0 commit comments

Comments
 (0)