Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Extract Downloaded Go Build to a Unique Directory #24

Merged
merged 2 commits into from
Apr 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 33 additions & 25 deletions cmake/SetupGo.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -10,46 +10,54 @@ include_guard(GLOBAL)
#
# This function sets the `GO_EXECUTABLE` variable to the location of the Go executable.
function(setup_go)
if(CMAKE_SYSTEM_NAME STREQUAL Windows)
set(GO_EXECUTABLE ${CMAKE_BINARY_DIR}/_deps/go/bin/go.exe)
set(VERSION 1.22.2)

if(CMAKE_SYSTEM_NAME STREQUAL Darwin)
set(OS darwin)
elseif(CMAKE_SYSTEM_NAME STREQUAL Linux)
set(OS linux)
elseif(CMAKE_SYSTEM_NAME STREQUAL Windows)
set(OS windows)
else()
set(GO_EXECUTABLE ${CMAKE_BINARY_DIR}/_deps/go/bin/go)
message(FATAL_ERROR "Unsupported system for setting up Go: ${CMAKE_SYSTEM_NAME}")
endif()

if(NOT EXISTS ${GO_EXECUTABLE})
if(CMAKE_SYSTEM_NAME STREQUAL Linux)
set(URL https://go.dev/dl/go1.22.2.linux-amd64.tar.gz)
elseif(CMAKE_SYSTEM_NAME STREQUAL Darwin)
if(CMAKE_SYSTEM_PROCESSOR STREQUAL x86_64)
set(URL https://go.dev/dl/go1.22.2.darwin-amd64.tar.gz)
elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL arm64)
set(URL https://go.dev/dl/go1.22.2.darwin-arm64.tar.gz)
else()
message(FATAL_ERROR "Unsupported architecture for setting up Go: ${CMAKE_SYSTEM_PROCESSOR}")
endif()
elseif(CMAKE_SYSTEM_NAME STREQUAL Windows)
set(URL https://go.dev/dl/go1.22.2.windows-amd64.zip)
else()
message(FATAL_ERROR "Unsupported system for setting up Go: ${CMAKE_SYSTEM_NAME}")
endif()
if(CMAKE_SYSTEM_PROCESSOR STREQUAL x86_64 OR CMAKE_SYSTEM_PROCESSOR STREQUAL AMD64)
set(ARCH amd64)
elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL arm64)
set(ARCH arm64)
else()
message(FATAL_ERROR "Unsupported architecture for setting up Go: ${CMAKE_SYSTEM_PROCESSOR}")
endif()

file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/_deps)
get_filename_component(FILENAME ${URL} NAME)
if(OS STREQUAL windows)
set(PACKAGE_EXT .zip)
set(EXECUTABLE_EXT .exe)
else()
set(PACKAGE_EXT .tar.gz)
endif()

set(GO_BUILD go${VERSION}.${OS}-${ARCH})
set(GO_PACKAGE ${GO_BUILD}${PACKAGE_EXT})
set(GO_EXECUTABLE ${CMAKE_BINARY_DIR}/_deps/${GO_BUILD}/go/bin/go${EXECUTABLE_EXT})

if(NOT EXISTS ${GO_EXECUTABLE})
# Download the Go build.
file(DOWNLOAD ${URL} ${CMAKE_BINARY_DIR}/_deps/${FILENAME})
file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/_deps)
file(DOWNLOAD https://go.dev/dl/${GO_PACKAGE} ${CMAKE_BINARY_DIR}/_deps/${GO_PACKAGE})

# Extract the Go build.
file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/_deps/${GO_BUILD})
execute_process(
COMMAND tar -xf ${CMAKE_BINARY_DIR}/_deps/${FILENAME} -C ${CMAKE_BINARY_DIR}/_deps
COMMAND tar -xf ${CMAKE_BINARY_DIR}/_deps/${GO_PACKAGE} -C ${CMAKE_BINARY_DIR}/_deps/${GO_BUILD}
RESULT_VARIABLE RES
)
if(NOT RES EQUAL 0)
message(FATAL_ERROR "Failed to extract '${CMAKE_BINARY_DIR}/_deps/${FILENAME}' to '${CMAKE_BINARY_DIR}/_deps' (${RES})")
message(FATAL_ERROR "Failed to extract '${CMAKE_BINARY_DIR}/_deps/${GO_PACKAGE}' to '${CMAKE_BINARY_DIR}/_deps/${GO_BUILD}' (${RES})")
endif()

# Remove the downloaded Go build to free up space.
file(REMOVE ${CMAKE_BINARY_DIR}/_deps/${FILENAME})
file(REMOVE ${CMAKE_BINARY_DIR}/_deps/${GO_PACKAGE})
endif()

set(GO_EXECUTABLE ${GO_EXECUTABLE} PARENT_SCOPE)
Expand Down