pkg-golang.mk 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. -tags "$$($(2)_TAGS)" \
  42. -trimpath \
  43. -p $(PARALLEL_JOBS)
  44. # Target packages need the Go compiler on the host.
  45. $(2)_DEPENDENCIES += host-go
  46. $(2)_BUILD_TARGETS ?= .
  47. # If the build target is just ".", then we assume the binary to be
  48. # produced is named after the package. If however, a build target has
  49. # been specified, we assume that the binaries to be produced are named
  50. # after each build target building them (below in <pkg>_BUILD_CMDS).
  51. ifeq ($$($(2)_BUILD_TARGETS),.)
  52. $(2)_BIN_NAME ?= $(1)
  53. endif
  54. $(2)_INSTALL_BINS ?= $(1)
  55. # Source files in Go usually use an import path resolved around
  56. # domain/vendor/software. We infer domain/vendor/software from the upstream URL
  57. # of the project.
  58. $(2)_SRC_DOMAIN = $$(call domain,$$($(2)_SITE))
  59. $(2)_SRC_VENDOR = $$(word 1,$$(subst /, ,$$(call notdomain,$$($(2)_SITE))))
  60. $(2)_SRC_SOFTWARE = $$(word 2,$$(subst /, ,$$(call notdomain,$$($(2)_SITE))))
  61. # $(2)_GOMOD is the root Go module path for the project, inferred if not set.
  62. # If the go.mod file does not exist, one is written with this root path.
  63. $(2)_GOMOD ?= $$($(2)_SRC_DOMAIN)/$$($(2)_SRC_VENDOR)/$$($(2)_SRC_SOFTWARE)
  64. # Generate a go.mod file if it doesn't exist. Note: Go is configured
  65. # to use the "vendor" dir and not make network calls.
  66. define $(2)_GEN_GOMOD
  67. if [ ! -f $$(@D)/go.mod ]; then \
  68. printf "module $$($(2)_GOMOD)\n" > $$(@D)/go.mod; \
  69. fi
  70. endef
  71. $(2)_POST_PATCH_HOOKS += $(2)_GEN_GOMOD
  72. # Build step. Only define it if not already defined by the package .mk
  73. # file.
  74. ifndef $(2)_BUILD_CMDS
  75. ifeq ($(4),target)
  76. ifeq ($(BR2_STATIC_LIBS),y)
  77. $(2)_LDFLAGS += -extldflags '-static'
  78. endif
  79. # Build package for target
  80. define $(2)_BUILD_CMDS
  81. $$(foreach d,$$($(2)_BUILD_TARGETS),\
  82. cd $$(@D); \
  83. $$(HOST_GO_TARGET_ENV) \
  84. $$($(2)_GO_ENV) \
  85. $$(GO_BIN) build -v $$($(2)_BUILD_OPTS) \
  86. -o $$(@D)/bin/$$(or $$($(2)_BIN_NAME),$$(notdir $$(d))) \
  87. $$($(2)_GOMOD)/$$(d)
  88. )
  89. endef
  90. else
  91. # Build package for host
  92. define $(2)_BUILD_CMDS
  93. $$(foreach d,$$($(2)_BUILD_TARGETS),\
  94. cd $$(@D); \
  95. $$(HOST_GO_HOST_ENV) \
  96. $$($(2)_GO_ENV) \
  97. $$(GO_BIN) build -v $$($(2)_BUILD_OPTS) \
  98. -o $$(@D)/bin/$$(or $$($(2)_BIN_NAME),$$(notdir $$(d))) \
  99. $$($(2)_GOMOD)/$$(d)
  100. )
  101. endef
  102. endif
  103. endif
  104. # Target installation step. Only define it if not already defined by the
  105. # package .mk file.
  106. ifndef $(2)_INSTALL_TARGET_CMDS
  107. define $(2)_INSTALL_TARGET_CMDS
  108. $$(foreach d,$$($(2)_INSTALL_BINS),\
  109. $(INSTALL) -D -m 0755 $$(@D)/bin/$$(d) $$(TARGET_DIR)/usr/bin/$$(d)
  110. )
  111. endef
  112. endif
  113. # Host installation step
  114. ifndef $(2)_INSTALL_CMDS
  115. define $(2)_INSTALL_CMDS
  116. $$(foreach d,$$($(2)_INSTALL_BINS),\
  117. $(INSTALL) -D -m 0755 $$(@D)/bin/$$(d) $$(HOST_DIR)/bin/$$(d)
  118. )
  119. endef
  120. endif
  121. # Call the generic package infrastructure to generate the necessary make
  122. # targets
  123. $(call inner-generic-package,$(1),$(2),$(3),$(4))
  124. endef # inner-golang-package
  125. ################################################################################
  126. # golang-package -- the target generator macro for Go packages
  127. ################################################################################
  128. golang-package = $(call inner-golang-package,$(pkgname),$(call UPPERCASE,$(pkgname)),$(call UPPERCASE,$(pkgname)),target)
  129. host-golang-package = $(call inner-golang-package,host-$(pkgname),$(call UPPERCASE,host-$(pkgname)),$(call UPPERCASE,$(pkgname)),host)