NetdataGoTools.cmake 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. # Macros and functions to assist in working with Go
  2. #
  3. # Copyright (c) 2024 Netdata Inc
  4. #
  5. # SPDX-License-Identifier: GPL
  6. if(CMAKE_BUILD_TYPE STREQUAL Debug)
  7. set(GO_LDFLAGS "-X github.com/netdata/netdata/go/plugins/pkg/buildinfo.Version=${NETDATA_VERSION_STRING}")
  8. else()
  9. set(GO_LDFLAGS "-w -s -X github.com/netdata/netdata/go/plugins/pkg/buildinfo.Version=${NETDATA_VERSION_STRING}")
  10. endif()
  11. # add_go_target: Add a new target that needs to be built using the Go toolchain.
  12. #
  13. # Takes four arguments, the target name, the output artifact name, the
  14. # source tree for the Go module, and the sub-directory of that source tree
  15. # to pass to `go build`.
  16. #
  17. # The target itself will invoke `go build` in the specified source tree,
  18. # using the `-o` option to produce the final output artifact, and passing
  19. # the requested sub-directory as the final argument.
  20. #
  21. # This will also automatically construct the dependency list for the
  22. # target by finding all Go source files under the specified source tree
  23. # and then appending the go.mod and go.sum files from the root of the
  24. # source tree.
  25. macro(add_go_target target output build_src build_dir)
  26. file(GLOB_RECURSE ${target}_DEPS CONFIGURE_DEPENDS "${build_src}/*.go")
  27. list(APPEND ${target}_DEPS
  28. "${build_src}/go.mod"
  29. "${build_src}/go.sum"
  30. )
  31. add_custom_command(
  32. OUTPUT ${output}
  33. COMMAND "${CMAKE_COMMAND}" -E env GOROOT=${GO_ROOT} CGO_ENABLED=0 GOPROXY=https://proxy.golang.org,direct "${GO_EXECUTABLE}" build -buildvcs=false -ldflags "${GO_LDFLAGS}" -o "${CMAKE_BINARY_DIR}/${output}" "./${build_dir}"
  34. DEPENDS ${${target}_DEPS}
  35. COMMENT "Building Go component ${output}"
  36. WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}/${build_src}"
  37. VERBATIM
  38. )
  39. add_custom_target(
  40. ${target} ALL
  41. DEPENDS ${output}
  42. )
  43. endmacro()
  44. # find_min_go_version: Determine the minimum Go version based on go.mod files
  45. #
  46. # Takes one argument, specifying a source tree to scan for go.mod files.
  47. #
  48. # All files found will be checked for a `go` directive, and the
  49. # MIN_GO_VERSION variable will be set to the highest version
  50. # number found among these directives.
  51. #
  52. # Only works on UNIX-like systems, because it has to process the go.mod
  53. # files in ways that CMake can't do on it's own.
  54. function(find_min_go_version src_tree)
  55. message(STATUS "Determining minimum required version of Go for this build")
  56. file(GLOB_RECURSE go_mod_files ${src_tree}/go.mod)
  57. set(result 1.0)
  58. foreach(f IN ITEMS ${go_mod_files})
  59. message(VERBOSE "Checking Go version specified in ${f}")
  60. execute_process(
  61. COMMAND grep -E "^go .*$" ${f}
  62. COMMAND cut -f 2 -d " "
  63. RESULT_VARIABLE version_check_result
  64. OUTPUT_VARIABLE go_mod_version
  65. )
  66. if(version_check_result EQUAL 0)
  67. string(REGEX MATCH "([0-9]+\\.[0-9]+(\\.[0-9]+)?)" go_mod_version "${go_mod_version}")
  68. if(go_mod_version VERSION_GREATER result)
  69. set(result "${go_mod_version}")
  70. endif()
  71. endif()
  72. endforeach()
  73. message(STATUS "Minimum required Go version determined to be ${result}")
  74. set(MIN_GO_VERSION "${result}" PARENT_SCOPE)
  75. endfunction()