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