NetdataGoTools.cmake 3.1 KB

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