| 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. |
| 17 | find_program(GO_EXECUTABLE go) |
| 18 |
| 19 | function(require_go) |
| 20 | if(NOT GO_EXECUTABLE) |
| 21 | message(FATAL_ERROR "Could not find Go") |
| 22 | endif() |
| 23 | endfunction() |
| 24 |
| 25 | function(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() |
| 60 | endfunction() |
| 61 |
| 62 |