diff --git a/cmake/SetupGo.cmake b/cmake/SetupGo.cmake new file mode 100644 index 0000000..3115621 --- /dev/null +++ b/cmake/SetupGo.cmake @@ -0,0 +1,22 @@ +# 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 Go build from the remote server and then extracts that +# and makes it available in the project. +# +# This function set `GO_EXECUTABLE` 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) + + set(GO_EXECUTABLE ${CMAKE_BUILD_DIR}/_deps/go/bin/go PARENT_SCOPE) +endfunction() diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index b506921..c6bca12 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -4,6 +4,7 @@ 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} ) @@ -15,3 +16,8 @@ add_cmake_test( "Create directory recursively" # Add more test cases here. ) + +add_cmake_test( + cmake/SetupGoTest.cmake + "Set up latest version of Go" +) diff --git a/test/cmake/SetupGoTest.cmake b/test/cmake/SetupGoTest.cmake new file mode 100644 index 0000000..215aa49 --- /dev/null +++ b/test/cmake/SetupGoTest.cmake @@ -0,0 +1,18 @@ +# Matches everything if not defined +if(NOT TEST_MATCHES) + set(TEST_MATCHES ".*") +endif() + +set(TEST_COUNT 0) + +if("Set up latest version of Go" MATCHES ${TEST_MATCHES}) + math(EXPR TEST_COUNT "${TEST_COUNT} + 1") + + include(SetupGo) + + setup_go() +endif() + +if(TEST_COUNT LESS_EQUAL 0) + message(FATAL_ERROR "Nothing to test with: ${TEST_MATCHES}") +endif()