adding-packages-golang.adoc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // -*- mode:doc; -*-
  2. // vim: set syntax=asciidoc:
  3. === Infrastructure for Go packages
  4. This infrastructure applies to Go packages that use the standard
  5. build system and use bundled dependencies.
  6. [[golang-package-tutorial]]
  7. ==== +golang-package+ tutorial
  8. First, let's see how to write a +.mk+ file for a go package,
  9. with an example :
  10. ------------------------
  11. 01: ################################################################################
  12. 02: #
  13. 03: # foo
  14. 04: #
  15. 05: ################################################################################
  16. 06:
  17. 07: FOO_VERSION = 1.0
  18. 08: FOO_SITE = $(call github,bar,foo,$(FOO_VERSION))
  19. 09: FOO_LICENSE = BSD-3-Clause
  20. 10: FOO_LICENSE_FILES = LICENSE
  21. 11:
  22. 12: $(eval $(golang-package))
  23. ------------------------
  24. On line 7, we declare the version of the package.
  25. On line 8, we declare the upstream location of the package, here
  26. fetched from Github, since a large number of Go packages are hosted on
  27. Github.
  28. On line 9 and 10, we give licensing details about the package.
  29. Finally, on line 12, we invoke the +golang-package+ macro that
  30. generates all the Makefile rules that actually allow the package to be
  31. built.
  32. [[golang-package-reference]]
  33. ==== +golang-package+ reference
  34. In their +Config.in+ file, packages using the +golang-package+
  35. infrastructure should depend on +BR2_PACKAGE_HOST_GO_TARGET_ARCH_SUPPORTS+
  36. because Buildroot will automatically add a dependency on +host-go+
  37. to such packages.
  38. If you need CGO support in your package, you must add a dependency on
  39. +BR2_PACKAGE_HOST_GO_TARGET_CGO_LINKING_SUPPORTS+.
  40. The main macro of the Go package infrastructure is
  41. +golang-package+. It is similar to the +generic-package+ macro. The
  42. ability to build host packages is also available, with the
  43. +host-golang-package+ macro.
  44. Host packages built by +host-golang-package+ macro should depend on
  45. +BR2_PACKAGE_HOST_GO_HOST_ARCH_SUPPORTS+.
  46. Just like the generic infrastructure, the Go infrastructure works
  47. by defining a number of variables before calling the +golang-package+
  48. macro.
  49. All the package metadata information variables that exist in the
  50. xref:generic-package-reference[generic package infrastructure] also
  51. exist in the Go infrastructure.
  52. Note that it is not necessary to add +host-go+ in the
  53. +FOO_DEPENDENCIES+ variable of a package, since this basic dependency
  54. is automatically added as needed by the Go package infrastructure.
  55. A few additional variables, specific to the Go infrastructure, can
  56. optionally be defined, depending on the package's needs. Many of them
  57. are only useful in very specific cases, typical packages will
  58. therefore only use a few of them, or none.
  59. * The package must specify its Go module name in the +FOO_GOMOD+
  60. variable. If not specified, it defaults to
  61. +URL-domain/1st-part-of-URL/2nd-part-of-URL+, e.g +FOO_GOMOD+ will
  62. take the value +github.com/bar/foo+ for a package that specifies
  63. +FOO_SITE = $(call github,bar,foo,$(FOO_VERSION))+. The Go package
  64. infrastructure will automatically generate a minimal +go.mod+ file
  65. in the package source tree if it doesn't exist.
  66. * +FOO_LDFLAGS+ and +FOO_TAGS+ can be used to pass respectively the
  67. +LDFLAGS+ or the +TAGS+ to the +go+ build command.
  68. * +FOO_BUILD_TARGETS+ can be used to pass the list of targets that
  69. should be built. If +FOO_BUILD_TARGETS+ is not specified, it
  70. defaults to +.+. We then have two cases:
  71. ** +FOO_BUILD_TARGETS+ is +.+. In this case, we assume only one binary
  72. will be produced, and that by default we name it after the package
  73. name. If that is not appropriate, the name of the produced binary
  74. can be overridden using +FOO_BIN_NAME+.
  75. ** +FOO_BUILD_TARGETS+ is not +.+. In this case, we iterate over the
  76. values to build each target, and for each produced a binary that is
  77. the non-directory component of the target. For example if
  78. +FOO_BUILD_TARGETS = cmd/docker cmd/dockerd+ the binaries produced
  79. are +docker+ and +dockerd+.
  80. * +FOO_INSTALL_BINS+ can be used to pass the list of binaries that
  81. should be installed in +/usr/bin+ on the target. If
  82. +FOO_INSTALL_BINS+ is not specified, it defaults to the lower-case
  83. name of package.
  84. With the Go infrastructure, all the steps required to build and
  85. install the packages are already defined, and they generally work well
  86. for most Go-based packages. However, when required, it is still
  87. possible to customize what is done in any particular step:
  88. * By adding a post-operation hook (after extract, patch, configure,
  89. build or install). See xref:hooks[] for details.
  90. * By overriding one of the steps. For example, even if the Go
  91. infrastructure is used, if the package +.mk+ file defines its own
  92. +FOO_BUILD_CMDS+ variable, it will be used instead of the default Go
  93. one. However, using this method should be restricted to very
  94. specific cases. Do not use it in the general case.
  95. A Go package can depend on other Go modules, listed in its +go.mod+
  96. file. Buildroot automatically takes care of downloading such
  97. dependencies as part of the download step of packages that use the
  98. +golang-package+ infrastructure. Such dependencies are then kept
  99. together with the package source code in the tarball cached in
  100. Buildroot's +DL_DIR+, and therefore the hash of the package's tarball
  101. includes such dependencies.
  102. This mechanism ensures that any change in the dependencies will be
  103. detected, and allows the build to be performed completely offline.