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

Add CMake Function for Setting Up Latest Go #5

Merged
merged 3 commits into from
Apr 21, 2024
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.19)
cmake_minimum_required(VERSION 3.29)

project(
SetupGo
Expand Down Expand Up @@ -31,7 +31,7 @@ if(NOT SUBPROJECT)

install(
FILES
cmake/MkdirRecursive.cmake
cmake/SetupGo.cmake
cmake/SetupGoConfig.cmake
${CMAKE_CURRENT_BINARY_DIR}/SetupGoConfigVersion.cmake
DESTINATION lib/cmake/SetupGo
Expand Down
5 changes: 0 additions & 5 deletions cmake/MkdirRecursive.cmake

This file was deleted.

28 changes: 28 additions & 0 deletions cmake/SetupGo.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# This code is licensed under the terms of the MIT License.
# Copyright (c) 2024 Alfi Maulana

include_guard(GLOBAL)

# Sets up the latest version of Go within this project.
#
# This function downloads the latest version of the Go build from the remote server, extracts it,
# and makes it available in the project.
#
# This function sets the `GO_EXECUTABLE` variable to the location of the Go executable.
function(setup_go)
file(MAKE_DIRECTORY ${CMAKE_BUILD_DIR}/_deps)
file(
DOWNLOAD https://go.dev/dl/go1.22.2.linux-amd64.tar.gz ${CMAKE_BUILD_DIR}/_deps/go.tar.gz
EXPECTED_MD5 f64eb5791a9dab9cbcdf6549b9583280
)

execute_process(
COMMAND tar -xf ${CMAKE_BUILD_DIR}/_deps/go.tar.gz -C ${CMAKE_BUILD_DIR}/_deps
RESULT_VARIABLE RES
)
if(NOT RES EQUAL 0)
message(FATAL_ERROR "Failed to extract '${CMAKE_BUILD_DIR}/_deps/go.tar.gz' to '${CMAKE_BUILD_DIR}/_deps' (${RES})")
endif()

set(GO_EXECUTABLE ${CMAKE_BUILD_DIR}/_deps/go/bin/go PARENT_SCOPE)
endfunction()
6 changes: 3 additions & 3 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ function(add_cmake_test FILE)
NAME ${NAME}
COMMAND ${CMAKE_COMMAND}
-D CMAKE_MODULE_PATH=${CMAKE_MODULE_PATH}
-D CMAKE_BUILD_DIR=${CMAKE_CURRENT_SOURCE_DIR}/build
-D TEST_MATCHES=^${NAME}$
-P ${CMAKE_CURRENT_SOURCE_DIR}/${FILE}
)
endforeach()
endfunction()

add_cmake_test(
cmake/MkdirRecursiveTest.cmake
"Create directory recursively"
# Add more test cases here.
cmake/SetupGoTest.cmake
"Set up the latest version of Go"
)
38 changes: 0 additions & 38 deletions test/cmake/MkdirRecursiveTest.cmake

This file was deleted.

38 changes: 38 additions & 0 deletions test/cmake/SetupGoTest.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Matches everything if not defined
if(NOT TEST_MATCHES)
set(TEST_MATCHES ".*")
endif()

set(TEST_COUNT 0)

include(SetupGo)

if("Set up the latest version of Go" MATCHES ${TEST_MATCHES})
math(EXPR TEST_COUNT "${TEST_COUNT} + 1")

setup_go()

if(NOT DEFINED GO_EXECUTABLE)
message(FATAL_ERROR "The GO_EXECUTABLE variable should be defined")
endif()

if(NOT EXISTS ${GO_EXECUTABLE})
message(FATAL_ERROR "The Go executable at '${GO_EXECUTABLE}' should exist")
endif()

if(NOT IS_EXECUTABLE ${GO_EXECUTABLE})
message(FATAL_ERROR "The Go executable at '${GO_EXECUTABLE}' should be executable")
endif()

execute_process(
COMMAND ${GO_EXECUTABLE} version
RESULT_VARIABLE RES
)
if(NOT RES EQUAL 0)
message(FATAL_ERROR "It should not fail to execute the Go executable")
endif()
endif()

if(TEST_COUNT LESS_EQUAL 0)
message(FATAL_ERROR "Nothing to test with: ${TEST_MATCHES}")
endif()