1# Copyright 2023 The BoringSSL Authors 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# https://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15# Go is an optional dependency. It's a necessary dependency if running tests or 16# the FIPS build, which will check these. 17find_program(GO_EXECUTABLE go) 18 19function(require_go) 20 if(NOT GO_EXECUTABLE) 21 message(FATAL_ERROR "Could not find Go") 22 endif() 23endfunction() 24 25function(go_executable dest package) 26 require_go() 27 set(godeps "${PROJECT_SOURCE_DIR}/util/godeps.go") 28 if(NOT CMAKE_GENERATOR STREQUAL "Ninja") 29 # The DEPFILE parameter to add_custom_command only works with Ninja. Query 30 # the sources at configure time. Additionally, everything depends on go.mod. 31 # That affects what external packages to use. 32 # 33 # TODO(davidben): Starting CMake 3.20, it also works with Make. Starting 34 # 3.21, it works with Visual Studio and Xcode too. 35 execute_process(COMMAND ${GO_EXECUTABLE} run ${godeps} -format cmake 36 -pkg ${package} 37 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} 38 OUTPUT_VARIABLE sources 39 RESULT_VARIABLE godeps_result) 40 add_custom_command(OUTPUT ${dest} 41 COMMAND ${GO_EXECUTABLE} build 42 -o ${CMAKE_CURRENT_BINARY_DIR}/${dest} ${package} 43 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} 44 DEPENDS ${sources} ${PROJECT_SOURCE_DIR}/go.mod) 45 else() 46 # Ninja expects the target in the depfile to match the output. This is a 47 # relative path from the build directory. 48 binary_dir_relative_path(${dest} target) 49 50 set(depfile "${CMAKE_CURRENT_BINARY_DIR}/${dest}.d") 51 add_custom_command(OUTPUT ${dest} 52 COMMAND ${GO_EXECUTABLE} build 53 -o ${CMAKE_CURRENT_BINARY_DIR}/${dest} ${package} 54 COMMAND ${GO_EXECUTABLE} run ${godeps} -format depfile 55 -target ${target} -pkg ${package} -out ${depfile} 56 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} 57 DEPENDS ${godeps} ${PROJECT_SOURCE_DIR}/go.mod 58 DEPFILE ${depfile}) 59 endif() 60endfunction() 61 62