| 1 |
if(NOT DISABLE_GO) |
| 2 |
find_program(GO_EXECUTABLE go) |
| 3 |
endif() |
| 4 |
|
| 5 |
if(NOT GO_EXECUTABLE AND NOT DISABLE_GO) |
| 6 |
message(FATAL_ERROR "Could not find Go") |
| 7 |
elseif(NOT DISABLE_GO) |
| 8 |
execute_process( |
| 9 |
COMMAND ${GO_EXECUTABLE} version |
| 10 |
OUTPUT_VARIABLE go_version_output |
| 11 |
OUTPUT_STRIP_TRAILING_WHITESPACE |
| 12 |
) |
| 13 |
# Example: 'go version go1.21.3 darwin/arm64' match any number of '#.' and one '#' |
| 14 |
string(REGEX MATCH "([0-9]+\\.)*[0-9]+" go_version ${go_version_output}) |
| 15 |
|
| 16 |
# This should track /go.mod and /BUILDING.md |
| 17 |
set(minimum_go_version "1.20") |
| 18 |
if(go_version VERSION_LESS minimum_go_version) |
| 19 |
message(FATAL_ERROR "Go compiler version must be at least ${minimum_go_version}. Found version ${go_version}") |
| 20 |
else() |
| 21 |
message(STATUS "Go compiler ${go_version} found") |
| 22 |
endif() |
| 23 |
endif() |
| 24 |
|
| 25 |
function(go_executable dest package) |
| 26 |
set(godeps "${PROJECT_SOURCE_DIR}/util/godeps.go") |
| 27 |
if(NOT CMAKE_GENERATOR STREQUAL "Ninja") |
| 28 |
# The DEPFILE parameter to add_custom_command only works with Ninja. Query |
| 29 |
# the sources at configure time. Additionally, everything depends on go.mod. |
| 30 |
# That affects what external packages to use. |
| 31 |
# |
| 32 |
# TODO(davidben): Starting CMake 3.20, it also works with Make. Starting |
| 33 |
# 3.21, it works with Visual Studio and Xcode too. |
| 34 |
execute_process(COMMAND ${GO_EXECUTABLE} run ${godeps} -format cmake |
| 35 |
-pkg ${package} |
| 36 |
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} |
| 37 |
OUTPUT_VARIABLE sources |
| 38 |
RESULT_VARIABLE godeps_result) |
| 39 |
add_custom_command(OUTPUT ${dest} |
| 40 |
COMMAND ${GO_EXECUTABLE} build |
| 41 |
-o ${CMAKE_CURRENT_BINARY_DIR}/${dest} ${package} |
| 42 |
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} |
| 43 |
DEPENDS ${sources} ${PROJECT_SOURCE_DIR}/go.mod) |
| 44 |
else() |
| 45 |
# Ninja expects the target in the depfile to match the output. This is a |
| 46 |
# relative path from the build directory. |
| 47 |
string(LENGTH "${CMAKE_BINARY_DIR}" root_dir_length) |
| 48 |
math(EXPR root_dir_length "${root_dir_length} + 1") |
| 49 |
string(SUBSTRING "${CMAKE_CURRENT_BINARY_DIR}" ${root_dir_length} -1 target) |
| 50 |
set(target "${target}/${dest}") |
| 51 |
|
| 52 |
if(CMAKE_VERSION VERSION_GREATER "3.19") |
| 53 |
# Silences warning about CMP0116: |
| 54 |
# https://cmake.org/cmake/help/latest/policy/CMP0116.html |
| 55 |
cmake_policy(SET CMP0116 OLD) |
| 56 |
endif() |
| 57 |
|
| 58 |
if(CMAKE_VERSION VERSION_LESS "3.7") |
| 59 |
add_custom_command(OUTPUT ${dest} |
| 60 |
COMMAND ${GO_EXECUTABLE} build |
| 61 |
-o ${CMAKE_CURRENT_BINARY_DIR}/${dest} ${package} |
| 62 |
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} |
| 63 |
DEPENDS ${PROJECT_SOURCE_DIR}/go.mod) |
| 64 |
else() |
| 65 |
set(depfile "${CMAKE_CURRENT_BINARY_DIR}/${dest}.d") |
| 66 |
add_custom_command(OUTPUT ${dest} |
| 67 |
COMMAND ${GO_EXECUTABLE} build |
| 68 |
-o ${CMAKE_CURRENT_BINARY_DIR}/${dest} ${package} |
| 69 |
COMMAND ${GO_EXECUTABLE} run ${godeps} -format depfile |
| 70 |
-target ${target} -pkg ${package} -out ${depfile} |
| 71 |
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} |
| 72 |
DEPENDS ${godeps} ${PROJECT_SOURCE_DIR}/go.mod |
| 73 |
DEPFILE ${depfile}) |
| 74 |
endif() |
| 75 |
endif() |
| 76 |
endfunction() |
| 77 |
|