pkg-golang.mk 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. ################################################################################
  2. # Golang package infrastructure
  3. #
  4. # This file implements an infrastructure that eases development of package .mk
  5. # files for Go packages. It should be used for all packages that are written in
  6. # go.
  7. #
  8. # See the Buildroot documentation for details on the usage of this
  9. # infrastructure
  10. #
  11. #
  12. # In terms of implementation, this golang infrastructure requires the .mk file
  13. # to only specify metadata information about the package: name, version,
  14. # download URL, etc.
  15. #
  16. # We still allow the package .mk file to override what the different steps are
  17. # doing, if needed. For example, if <PKG>_BUILD_CMDS is already defined, it is
  18. # used as the list of commands to perform to build the package, instead of the
  19. # default golang behavior. The package can also define some post operation
  20. # hooks.
  21. #
  22. ################################################################################
  23. GO_BIN = $(HOST_DIR)/bin/go
  24. ################################################################################
  25. # inner-golang-package -- defines how the configuration, compilation and
  26. # installation of a Go package should be done, implements a few hooks to tune
  27. # the build process for Go specificities and calls the generic package
  28. # infrastructure to generate the necessary make targets
  29. #
  30. # argument 1 is the lowercase package name
  31. # argument 2 is the uppercase package name, including a HOST_ prefix for host
  32. # packages
  33. # argument 3 is the uppercase package name, without the HOST_ prefix for host
  34. # packages
  35. # argument 4 is the type (target or host)
  36. #
  37. ################################################################################
  38. define inner-golang-package
  39. $(2)_BUILD_OPTS += \
  40. -ldflags "$$($(2)_LDFLAGS)" \
  41. -modcacherw \
  42. -tags "$$($(2)_TAGS)" \
  43. -trimpath \
  44. -p $$(PARALLEL_JOBS) \
  45. -buildvcs=false
  46. # Target packages need the Go compiler on the host at download time (for
  47. # vendoring), and at build and install time.
  48. $(2)_DOWNLOAD_DEPENDENCIES += host-go
  49. $(2)_DEPENDENCIES += host-go
  50. $(2)_BUILD_TARGETS ?= .
  51. # If the build target is just ".", then we assume the binary to be
  52. # produced is named after the package. If however, a build target has
  53. # been specified, we assume that the binaries to be produced are named
  54. # after each build target building them (below in <pkg>_BUILD_CMDS).
  55. ifeq ($$($(2)_BUILD_TARGETS),.)
  56. $(2)_BIN_NAME ?= $$($(2)_RAWNAME)
  57. endif
  58. $(2)_INSTALL_BINS ?= $$($(2)_RAWNAME)
  59. # Source files in Go usually use an import path resolved around
  60. # domain/vendor/software. We infer domain/vendor/software from the upstream URL
  61. # of the project.
  62. $(2)_SRC_DOMAIN = $$(call domain,$$($(2)_SITE))
  63. $(2)_SRC_VENDOR = $$(word 1,$$(subst /, ,$$(call notdomain,$$($(2)_SITE))))
  64. $(2)_SRC_SOFTWARE = $$(word 2,$$(subst /, ,$$(call notdomain,$$($(2)_SITE))))
  65. # $(2)_GOMOD is the root Go module path for the project, inferred if not set.
  66. # If the go.mod file does not exist, one is written with this root path.
  67. $(2)_GOMOD ?= $$($(2)_SRC_DOMAIN)/$$($(2)_SRC_VENDOR)/$$($(2)_SRC_SOFTWARE)
  68. # Generate a go.mod file if it doesn't exist. Note: Go is configured
  69. # to use the "vendor" dir and not make network calls.
  70. define $(2)_GEN_GOMOD
  71. if [ ! -f $$(@D)/go.mod ]; then \
  72. printf "module $$($(2)_GOMOD)\n" > $$(@D)/go.mod; \
  73. fi
  74. endef
  75. $(2)_POST_PATCH_HOOKS += $(2)_GEN_GOMOD
  76. $(2)_DOWNLOAD_POST_PROCESS = go
  77. $(2)_DL_ENV += \
  78. $$(HOST_GO_COMMON_ENV) \
  79. GOPROXY=direct \
  80. $$($(2)_GO_ENV)
  81. # Because we append vendored info, we can't rely on the values being empty
  82. # once we eventually get into the generic-package infra. So, we duplicate
  83. # the heuristics here
  84. ifndef $(2)_LICENSE
  85. ifdef $(3)_LICENSE
  86. $(2)_LICENSE = $$($(3)_LICENSE)
  87. endif
  88. endif
  89. # Due to vendoring, it is pretty likely that not all licenses are
  90. # listed in <pkg>_LICENSE. If the license is unset, it is "unknown"
  91. # so adding unknowns to some unknown is still some other unknown,
  92. # so don't append the blurb in that case.
  93. ifneq ($$($(2)_LICENSE),)
  94. $(2)_LICENSE += , vendored dependencies licenses probably not listed
  95. endif
  96. # Build step. Only define it if not already defined by the package .mk
  97. # file.
  98. ifndef $(2)_BUILD_CMDS
  99. ifeq ($(4),target)
  100. ifeq ($(BR2_STATIC_LIBS),y)
  101. $(2)_LDFLAGS += -extldflags '-static'
  102. $(2)_TAGS += osusergo netgo
  103. endif
  104. # Build package for target
  105. define $(2)_BUILD_CMDS
  106. $$(foreach d,$$($(2)_BUILD_TARGETS),\
  107. cd $$(@D); \
  108. $$(HOST_GO_TARGET_ENV) \
  109. $$($(2)_GO_ENV) \
  110. $$(GO_BIN) build -v $$($(2)_BUILD_OPTS) \
  111. -o $$(@D)/bin/$$(or $$($(2)_BIN_NAME),$$(notdir $$(d))) \
  112. $$($(2)_GOMOD)/$$(d)
  113. )
  114. endef
  115. else
  116. # Build package for host
  117. define $(2)_BUILD_CMDS
  118. $$(foreach d,$$($(2)_BUILD_TARGETS),\
  119. cd $$(@D); \
  120. $$(HOST_GO_HOST_ENV) \
  121. $$($(2)_GO_ENV) \
  122. $$(GO_BIN) build -v $$($(2)_BUILD_OPTS) \
  123. -o $$(@D)/bin/$$(or $$($(2)_BIN_NAME),$$(notdir $$(d))) \
  124. $$($(2)_GOMOD)/$$(d)
  125. )
  126. endef
  127. endif
  128. endif
  129. # Target installation step. Only define it if not already defined by the
  130. # package .mk file.
  131. ifndef $(2)_INSTALL_TARGET_CMDS
  132. define $(2)_INSTALL_TARGET_CMDS
  133. $$(foreach d,$$($(2)_INSTALL_BINS),\
  134. $$(INSTALL) -D -m 0755 $$(@D)/bin/$$(d) $$(TARGET_DIR)/usr/bin/$$(d)
  135. )
  136. endef
  137. endif
  138. # Host installation step
  139. ifndef $(2)_INSTALL_CMDS
  140. define $(2)_INSTALL_CMDS
  141. $$(foreach d,$$($(2)_INSTALL_BINS),\
  142. $$(INSTALL) -D -m 0755 $$(@D)/bin/$$(d) $$(HOST_DIR)/bin/$$(d)
  143. )
  144. endef
  145. endif
  146. # Call the generic package infrastructure to generate the necessary make
  147. # targets
  148. $(call inner-generic-package,$(1),$(2),$(3),$(4))
  149. endef # inner-golang-package
  150. ################################################################################
  151. # golang-package -- the target generator macro for Go packages
  152. ################################################################################
  153. golang-package = $(call inner-golang-package,$(pkgname),$(call UPPERCASE,$(pkgname)),$(call UPPERCASE,$(pkgname)),target)
  154. host-golang-package = $(call inner-golang-package,host-$(pkgname),$(call UPPERCASE,host-$(pkgname)),$(call UPPERCASE,$(pkgname)),host)