Skip to content

Commit

Permalink
Update go.cmake to use PROJECT_SOURCE_DIR (#1484)
Browse files Browse the repository at this point in the history
Update go.cmake to use PROJECT_SOURCE_DIR to enable customers to build AWS-LC as a CMake sub_directory. Resolves P120917801.

CI check requires a copyright
  • Loading branch information
andrewhop authored and justsmth committed Mar 12, 2024
1 parent 22b1534 commit a370181
Showing 1 changed file with 27 additions and 12 deletions.
39 changes: 27 additions & 12 deletions cmake/go.cmake
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0 OR ISC
if(ANDROID)
# Android-NDK CMake files reconfigure the path and so Go won't be found.
# However, ninja will still find them in $PATH if we just name them.
Expand All @@ -15,7 +17,7 @@ if(NOT GO_EXECUTABLE AND NOT DISABLE_GO)
endif()

function(go_executable dest package)
set(godeps "${CMAKE_SOURCE_DIR}/util/godeps.go")
set(godeps "${PROJECT_SOURCE_DIR}/util/godeps.go")
if(NOT CMAKE_GENERATOR STREQUAL "Ninja")
# The DEPFILE parameter to add_custom_command only works with Ninja. Query
# the sources at configure time. Additionally, everything depends on go.mod.
Expand All @@ -32,7 +34,7 @@ function(go_executable dest package)
COMMAND ${GO_EXECUTABLE} build
-o ${CMAKE_CURRENT_BINARY_DIR}/${dest} ${package}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
DEPENDS ${sources} ${CMAKE_SOURCE_DIR}/go.mod)
DEPENDS ${sources} ${PROJECT_SOURCE_DIR}/go.mod)
else()
# Ninja expects the target in the depfile to match the output. This is a
# relative path from the build directory.
Expand All @@ -41,15 +43,28 @@ function(go_executable dest package)
string(SUBSTRING "${CMAKE_CURRENT_BINARY_DIR}" ${root_dir_length} -1 target)
set(target "${target}/${dest}")

set(depfile "${CMAKE_CURRENT_BINARY_DIR}/${dest}.d")
add_custom_command(OUTPUT ${dest}
COMMAND ${GO_EXECUTABLE} build
-o ${CMAKE_CURRENT_BINARY_DIR}/${dest} ${package}
COMMAND ${GO_EXECUTABLE} run ${godeps} -format depfile
-target ${target} -pkg ${package} -out ${depfile}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
DEPENDS ${godeps} ${CMAKE_SOURCE_DIR}/go.mod
DEPFILE ${depfile})
if(CMAKE_VERSION VERSION_GREATER "3.19")
# Silences warning about CMP0116:
# https://cmake.org/cmake/help/latest/policy/CMP0116.html
cmake_policy(SET CMP0116 OLD)
endif()

if(CMAKE_VERSION VERSION_LESS "3.7")
add_custom_command(OUTPUT ${dest}
COMMAND ${GO_EXECUTABLE} build
-o ${CMAKE_CURRENT_BINARY_DIR}/${dest} ${package}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
DEPENDS ${PROJECT_SOURCE_DIR}/go.mod)
else()
set(depfile "${CMAKE_CURRENT_BINARY_DIR}/${dest}.d")
add_custom_command(OUTPUT ${dest}
COMMAND ${GO_EXECUTABLE} build
-o ${CMAKE_CURRENT_BINARY_DIR}/${dest} ${package}
COMMAND ${GO_EXECUTABLE} run ${godeps} -format depfile
-target ${target} -pkg ${package} -out ${depfile}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
DEPENDS ${godeps} ${PROJECT_SOURCE_DIR}/go.mod
DEPFILE ${depfile})
endif()
endif()
endfunction()

0 comments on commit a370181

Please sign in to comment.