adding-packages-golang.txt 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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_ARCH_SUPPORTS+
  36. and +BR2_PACKAGE_HOST_GO_CGO_LINKING_SUPPORTS+ because Buildroot will
  37. automatically add a dependency on +host-go+ to such packages.
  38. The main macro of the Go package infrastructure is
  39. +golang-package+. It is similar to the +generic-package+ macro. Only
  40. target packages are supported with +golang-package+.
  41. Just like the generic infrastructure, the Go infrastructure works
  42. by defining a number of variables before calling the +golang-package+.
  43. All the package metadata information variables that exist in the
  44. xref:generic-package-reference[generic package infrastructure] also
  45. exist in the Go infrastructure: +FOO_VERSION+, +FOO_SOURCE+,
  46. +FOO_PATCH+, +FOO_SITE+, +FOO_SUBDIR+, +FOO_DEPENDENCIES+,
  47. +FOO_LICENSE+, +FOO_LICENSE_FILES+, +FOO_INSTALL_STAGING+, etc.
  48. Note that it is not necessary to add +host-go+ in the
  49. +FOO_DEPENDENCIES+ variable of a package, since this basic dependency
  50. is automatically added as needed by the Go package infrastructure.
  51. A few additional variables, specific to the Go infrastructure, can
  52. optionally be defined, depending on the package's needs. Many of them
  53. are only useful in very specific cases, typical packages will
  54. therefore only use a few of them, or none.
  55. * If your package need a custom +GOPATH+ to be compiled in, you can
  56. use the +FOO_WORKSPACE+ variable. The +GOPATH+ being used will be
  57. +<package-srcdir>/<FOO_WORKSPACE>+. If +FOO_WORKSPACE+ is not
  58. specified, it defaults to +_gopath+.
  59. * +FOO_SRC_SUBDIR+ is the sub-directory where your source will be
  60. compiled relatively to the +GOPATH+. An example value is
  61. +github.com/bar/foo+. If +FOO_SRC_SUBDIR+ is not specified, it
  62. defaults to a value infered from the +FOO_SITE+ variable.
  63. * +FOO_LDFLAGS+ and +FOO_TAGS+ can be used to pass respectively the
  64. +LDFLAGS+ or the +TAGS+ to the +go+ build command.
  65. * +FOO_BUILD_TARGETS+ can be used to pass the list of targets that
  66. should be built. If +FOO_BUILD_TARGETS+ is not specified, it
  67. defaults to +.+. We then have two cases:
  68. ** +FOO_BUILD_TARGETS+ is +.+. In this case, we assume only one binary
  69. will be produced, and that by default we name it after the package
  70. name. If that is not appropriate, the name of the produced binary
  71. can be overridden using +FOO_BIN_NAME+.
  72. ** +FOO_BUILD_TARGETS+ is not +.+. In this case, we iterate over the
  73. values to build each target, and for each produced a binary that is
  74. the non-directory component of the target. For example if
  75. +FOO_BUILD_TARGETS = cmd/docker cmd/dockerd+ the binaries produced
  76. are +docker+ and +dockerd+.
  77. * +FOO_INSTALL_BINS+ can be used to pass the list of binaries that
  78. should be installed in +/usr/bin+ on the target. If
  79. +FOO_INSTALL_BINS+ is not specified, it defaults to the lower-case
  80. name of package.
  81. With the Go infrastructure, all the steps required to build and
  82. install the packages are already defined, and they generally work well
  83. for most Go-based packages. However, when required, it is still
  84. possible to customize what is done in any particular step:
  85. * By adding a post-operation hook (after extract, patch, configure,
  86. build or install). See xref:hooks[] for details.
  87. * By overriding one of the steps. For example, even if the Go
  88. infrastructure is used, if the package +.mk+ file defines its own
  89. +FOO_BUILD_CMDS+ variable, it will be used instead of the default Go
  90. one. However, using this method should be restricted to very
  91. specific cases. Do not use it in the general case.