xref: /aosp_15_r20/external/cronet/third_party/boringssl/src/cmake/go.cmake (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1# Go is an optional dependency. It's a necessary dependency if running tests or
2# the FIPS build, which will check these.
3find_program(GO_EXECUTABLE go)
4
5function(require_go)
6  if(NOT GO_EXECUTABLE)
7    message(FATAL_ERROR "Could not find Go")
8  endif()
9endfunction()
10
11function(go_executable dest package)
12  require_go()
13  set(godeps "${PROJECT_SOURCE_DIR}/util/godeps.go")
14  if(NOT CMAKE_GENERATOR STREQUAL "Ninja")
15    # The DEPFILE parameter to add_custom_command only works with Ninja. Query
16    # the sources at configure time. Additionally, everything depends on go.mod.
17    # That affects what external packages to use.
18    #
19    # TODO(davidben): Starting CMake 3.20, it also works with Make. Starting
20    # 3.21, it works with Visual Studio and Xcode too.
21    execute_process(COMMAND ${GO_EXECUTABLE} run ${godeps} -format cmake
22                            -pkg ${package}
23                    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
24                    OUTPUT_VARIABLE sources
25                    RESULT_VARIABLE godeps_result)
26    add_custom_command(OUTPUT ${dest}
27                       COMMAND ${GO_EXECUTABLE} build
28                               -o ${CMAKE_CURRENT_BINARY_DIR}/${dest} ${package}
29                       WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
30                       DEPENDS ${sources} ${PROJECT_SOURCE_DIR}/go.mod)
31  else()
32    # Ninja expects the target in the depfile to match the output. This is a
33    # relative path from the build directory.
34    binary_dir_relative_path(${dest} target)
35
36    set(depfile "${CMAKE_CURRENT_BINARY_DIR}/${dest}.d")
37    add_custom_command(OUTPUT ${dest}
38                       COMMAND ${GO_EXECUTABLE} build
39                               -o ${CMAKE_CURRENT_BINARY_DIR}/${dest} ${package}
40                       COMMAND ${GO_EXECUTABLE} run ${godeps} -format depfile
41                               -target ${target} -pkg ${package} -out ${depfile}
42                       WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
43                       DEPENDS ${godeps} ${PROJECT_SOURCE_DIR}/go.mod
44                       DEPFILE ${depfile})
45  endif()
46endfunction()
47
48