FindGo.cmake 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. # Custom CMake module to find the Go toolchain
  2. #
  3. # Copyright (c) 2024 Netdata Inc
  4. #
  5. # SPDX-License-Identifier: GPL-3.0-or-later
  6. #
  7. # This is a relatively orthodox CMake Find Module. It can be used by
  8. # simply including it and then invoking `find_package(Go)`.
  9. #
  10. # Version handling is done by CMake itself via the
  11. # find_package_handle_standard_args() function, so `find_package(Go 1.21)`
  12. # will also work correctly.
  13. if(GO_FOUND)
  14. return()
  15. endif()
  16. # Two passes are needed here so that we prefer a copy in `/usr/local/go/bin` over a system copy.
  17. find_program(GO_EXECUTABLE go PATHS /usr/local/go/bin DOC "Go toolchain" NO_DEFAULT_PATH)
  18. find_program(GO_EXECUTABLE go DOC "Go toolchain")
  19. if (GO_EXECUTABLE)
  20. execute_process(
  21. COMMAND ${GO_EXECUTABLE} version
  22. OUTPUT_VARIABLE GO_VERSION_STRING
  23. RESULT_VARIABLE RESULT
  24. )
  25. if (RESULT EQUAL 0)
  26. string(REGEX MATCH "go([0-9]+\\.[0-9]+(\\.[0-9]+)?)" GO_VERSION_STRING "${GO_VERSION_STRING}")
  27. string(REGEX MATCH "([0-9]+\\.[0-9]+(\\.[0-9]+)?)" GO_VERSION_STRING "${GO_VERSION_STRING}")
  28. endif()
  29. endif()
  30. include(FindPackageHandleStandardArgs)
  31. find_package_handle_standard_args(
  32. Go
  33. REQUIRED_VARS GO_EXECUTABLE
  34. VERSION_VAR GO_VERSION_STRING
  35. )