summaryrefslogtreecommitdiffstats
path: root/packaging/cmake/Modules/FindGo.cmake
diff options
context:
space:
mode:
Diffstat (limited to 'packaging/cmake/Modules/FindGo.cmake')
-rw-r--r--packaging/cmake/Modules/FindGo.cmake39
1 files changed, 39 insertions, 0 deletions
diff --git a/packaging/cmake/Modules/FindGo.cmake b/packaging/cmake/Modules/FindGo.cmake
new file mode 100644
index 000000000..454a0051e
--- /dev/null
+++ b/packaging/cmake/Modules/FindGo.cmake
@@ -0,0 +1,39 @@
+# Custom CMake module to find the Go toolchain
+#
+# Copyright (c) 2024 Netdata Inc
+#
+# SPDX-License-Identifier: GPL-3.0-or-later
+#
+# This is a relatively orthodox CMake Find Module. It can be used by
+# simply including it and then invoking `find_package(Go)`.
+#
+# Version handling is done by CMake itself via the
+# find_package_handle_standard_args() function, so `find_package(Go 1.21)`
+# will also work correctly.
+
+if(GO_FOUND)
+ return()
+endif()
+
+# Two passes are needed here so that we prefer a copy in `/usr/local/go/bin` over a system copy.
+find_program(GO_EXECUTABLE go PATHS /usr/local/go/bin DOC "Go toolchain" NO_DEFAULT_PATH)
+find_program(GO_EXECUTABLE go DOC "Go toolchain")
+
+if (GO_EXECUTABLE)
+ execute_process(
+ COMMAND ${GO_EXECUTABLE} version
+ OUTPUT_VARIABLE GO_VERSION_STRING
+ RESULT_VARIABLE RESULT
+ )
+ if (RESULT EQUAL 0)
+ string(REGEX MATCH "go([0-9]+\\.[0-9]+(\\.[0-9]+)?)" GO_VERSION_STRING "${GO_VERSION_STRING}")
+ string(REGEX MATCH "([0-9]+\\.[0-9]+(\\.[0-9]+)?)" GO_VERSION_STRING "${GO_VERSION_STRING}")
+ endif()
+endif()
+
+include(FindPackageHandleStandardArgs)
+find_package_handle_standard_args(
+ Go
+ REQUIRED_VARS GO_EXECUTABLE
+ VERSION_VAR GO_VERSION_STRING
+)