From patchwork Wed Oct 25 09:26:45 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Angelo Compagnucci X-Patchwork-Id: 830152 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=busybox.net (client-ip=140.211.166.136; helo=silver.osuosl.org; envelope-from=buildroot-bounces@busybox.net; receiver=) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="JDEWPAsJ"; dkim-atps=neutral Received: from silver.osuosl.org (smtp3.osuosl.org [140.211.166.136]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3yMPt30Jnyz9t2W for ; Wed, 25 Oct 2017 20:27:06 +1100 (AEDT) Received: from localhost (localhost [127.0.0.1]) by silver.osuosl.org (Postfix) with ESMTP id C4DDF30838; Wed, 25 Oct 2017 09:27:01 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from silver.osuosl.org ([127.0.0.1]) by localhost (.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id GTM9iZD83WpI; Wed, 25 Oct 2017 09:26:59 +0000 (UTC) Received: from ash.osuosl.org (ash.osuosl.org [140.211.166.34]) by silver.osuosl.org (Postfix) with ESMTP id 8A8D63037F; Wed, 25 Oct 2017 09:26:59 +0000 (UTC) X-Original-To: buildroot@lists.busybox.net Delivered-To: buildroot@osuosl.org Received: from hemlock.osuosl.org (smtp2.osuosl.org [140.211.166.133]) by ash.osuosl.org (Postfix) with ESMTP id E73AE1C0621 for ; Wed, 25 Oct 2017 09:26:57 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by hemlock.osuosl.org (Postfix) with ESMTP id DB8DC899DF for ; Wed, 25 Oct 2017 09:26:57 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from hemlock.osuosl.org ([127.0.0.1]) by localhost (.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id wvzbluroA2TX for ; Wed, 25 Oct 2017 09:26:57 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-wm0-f68.google.com (mail-wm0-f68.google.com [74.125.82.68]) by hemlock.osuosl.org (Postfix) with ESMTPS id B91C588A10 for ; Wed, 25 Oct 2017 09:26:56 +0000 (UTC) Received: by mail-wm0-f68.google.com with SMTP id p75so557325wmg.3 for ; Wed, 25 Oct 2017 02:26:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=vl78WQ7JAgl6yJlj8BkWxTeg0CgPtjDAje8X9QDn8pM=; b=JDEWPAsJn12l23WIW04KMvoRwfKRPnPgmVSmsY2qkIuqdbsbYhKJf/00kQWz1J83UC dXG1HacaKeBreert0NxllAoOmATAmkkJDQrQZx6R60kB0fD/hONUV55zXF7dvRqtttgo wg7sVZ+EjUnmeu2YeUuHrJrNmhC2lR0RJ8NZihmTIP8XjWxHrPB+LXrMr1QbQUbSNOTY Rc+parr6Xa40/b+xEcTJ7iYNFtNvHcbnjzKWWGNJKr9CgjXJB88gnsrBsdYr0zwLxfOY BbSu5S2QA07+0SLjaNtrLhH6aLEDJdGDZ/haM92B+r/mI9Sf7EORxbnn2dvV914DtQfP QgCg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=vl78WQ7JAgl6yJlj8BkWxTeg0CgPtjDAje8X9QDn8pM=; b=nv1qRDHjF6C8nLZzZzVfiLQNqb8Jj2OEPRixESAU7v7Mn8fllkVvRRZMQr4had+122 R3qBTqaMLrHeQHPI3H/te6qJsewAL2L1nw/znv4aDP0dnyToALehBRpdNjNjLqSOJCvN ahl2V7nUvklkUduHRBrN14xXtTSQfDUCkcYadwA+7FmWKosgED3Tlv3SejL+/mmndw0h V8/sEsECy5Rem8dKGtcdr26wmzbmhIQJ5JxzoKxwhwk8mMMOyFNKPmAJSzFwnED+HXDp 3DlhAp2nA/BGWU7FTqeUCk7i+QWe0DeoY+s7QOu+VhLtVC/TuWJftildQeS0pkk1Ctcs V3bA== X-Gm-Message-State: AMCzsaV043x0DZY0TVB83nlslw+cnUADpiFSjCaYTdD4foCfLW323o8u uE1r8g7Yr3nnLnIiQrAidnmKkXwO X-Google-Smtp-Source: ABhQp+R4ugFGdQr2ki/tkBB8Ve2PLpj0EOmF6VoDvditX3QT1ZwpxsFpdARepngCQzahOyLTJP0cZw== X-Received: by 10.28.5.148 with SMTP id 142mr1147570wmf.142.1508923614445; Wed, 25 Oct 2017 02:26:54 -0700 (PDT) Received: from localhost.localdomain ([89.202.204.147]) by smtp.gmail.com with ESMTPSA id q7sm2009311wrg.97.2017.10.25.02.26.52 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 25 Oct 2017 02:26:53 -0700 (PDT) From: Angelo Compagnucci To: buildroot@buildroot.org Date: Wed, 25 Oct 2017 11:26:45 +0200 Message-Id: <1508923609-2022-1-git-send-email-angelo.compagnucci@gmail.com> X-Mailer: git-send-email 2.7.4 Cc: christian@paral.in Subject: [Buildroot] [RFC v5 1/5] package/pkg-golang: new package infrastructure X-BeenThere: buildroot@busybox.net X-Mailman-Version: 2.1.18-1 Precedence: list List-Id: Discussion and development of buildroot List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: buildroot-bounces@busybox.net Sender: "buildroot" This patch adds a new infrastructure for golang based packages. Signed-off-by: Angelo Compagnucci --- Changelog: v2 -> v3: * Adding GO_LDFLAGS and GO_TAGS variables * Adding GOPATH variable * Adding option to build as static when BR2_STATIC_LIBS is enabled * Changed building logic to take into account multiple binaries v3 -> v4: * Changing GOPATH default value from gopath to _gopath v4 -> v5: * Removing unnecessary for now host package part (suggested by Arnout) * Fixing the naming of _MAKE_ENV (suggested by Arnout) * Reworking building command (suggested by Arnout) package/Makefile.in | 1 + package/pkg-golang.mk | 120 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 121 insertions(+) create mode 100644 package/pkg-golang.mk diff --git a/package/Makefile.in b/package/Makefile.in index a1a5316..60d98d0 100644 --- a/package/Makefile.in +++ b/package/Makefile.in @@ -429,3 +429,4 @@ include package/pkg-kconfig.mk include package/pkg-rebar.mk include package/pkg-kernel-module.mk include package/pkg-waf.mk +include package/pkg-golang.mk diff --git a/package/pkg-golang.mk b/package/pkg-golang.mk new file mode 100644 index 0000000..467d5b5 --- /dev/null +++ b/package/pkg-golang.mk @@ -0,0 +1,120 @@ +################################################################################ +# Golang package infrastructure +# +# This file implements an infrastructure that eases development of +# package .mk files for Go packages. It should be used for all +# packages that are written in go. +# +# See the Buildroot documentation for details on the usage of this +# infrastructure +# +# +# In terms of implementation, this golang infrastructure requires +# the .mk file to only specify metadata information about the +# package: name, version, download URL, etc. +# +# We still allow the package .mk file to override what the different +# steps are doing, if needed. For example, if _BUILD_CMDS is +# already defined, it is used as the list of commands to perform to +# build the package, instead of the default golang behavior. The +# package can also define some post operation hooks. +# +################################################################################ + +################################################################################ +# inner-golang-package -- defines how the configuration, compilation and +# installation of a Go package should be done, implements a few hooks to +# tune the build process for Go specificities and calls the generic package +# infrastructure to generate the necessary make targets +# +# argument 1 is the lowercase package name +# argument 2 is the uppercase package name, including a HOST_ prefix +# for host packages +# argument 3 is the uppercase package name, without the HOST_ prefix +# for host packages +# argument 4 is the type (target or host) +################################################################################ + +define inner-golang-package + +$(2)_GO ?= $(HOST_DIR)/bin/go install -v +$(2)_GOPATH ?= _gopath +$(2)_GO_ENV ?= $$(HOST_GO_TARGET_ENV) \ + GOPATH="$$(@D)/$$($(2)_GOPATH)" \ + CGO_ENABLED=$$(HOST_GO_CGO_ENABLED) + +ifeq ($(BR2_STATIC_LIBS),y) + $(2)_GO_LDFLAGS += -extldflags '-static' +endif + +ifdef $(2)_GO_LDFLAGS + $(2)_BUILD_OPTS += -ldflags "$$($(2)_GO_LDFLAGS)" +endif + +ifdef $(2)_GO_TAGS + $(2)_BUILD_OPTS += -tags "$$($(2)_GO_TAGS)" +endif + +# Target packages need the Go compiler on the host. +$(2)_DEPENDENCIES += host-go + +# +# The go build/install command installs the binaries inside +# gopath/bin/linux_GOARCH/ when cross compilation is enabled. +# We set this variable here to be used by packages if needed. +# +$(2)_BINDIR = $$($(2)_GOPATH)/bin/linux_$$(GO_GOARCH) + +# +# Source files in Go should be uncompressed in a precise folder in the +# hierarchy of GOPATH. It usually resolves around domain/vendor/software. +# +$(2)_GO_SRC_PATH ?= $$(call domain,$($(2)_SITE))/$$(firstword $$(subst /, ,$$(call notdomain,$($(2)_SITE)))) +$(2)_SRC_PATH = $$(@D)/$$($(2)_GOPATH)/src/$$($(2)_GO_SRC_PATH)/$(1) + +# +# Configure step. Only define it if not already defined by the package +# .mk file. +# +ifndef $(2)_CONFIGURE_CMDS +define $(2)_CONFIGURE_CMDS + mkdir -p $$(@D)/$$($(2)_GOPATH)/bin + mkdir -p $$(@D)/$$($(2)_GOPATH)/src/$$($(2)_GO_SRC_PATH) + ln -sf $$(@D) $$($(2)_SRC_PATH) +endef +endif + +# +# Build step. Only define it if not already defined by the package .mk file. +# We use the install command instead of build command here because the +# install command also moves the package binaries in gopath/bin/linux_GOARCH/. +# Using the install command also leverages the go build infrastructure +# for building and installing multiple binaries. +# +ifndef $(2)_BUILD_CMDS +define $(2)_BUILD_CMDS + cd $$($(2)_SRC_PATH) && $$($(2)_GO_ENV) $$($(2)_GO) $$($(2)_BUILD_OPTS) +endef +endif + +# +# Target installation step. Only define it if not already defined by the +# package .mk file. +# +ifndef $(2)_INSTALL_TARGET_CMDS +define $(2)_INSTALL_TARGET_CMDS + $(INSTALL) -D -m 0755 $$(@D)/$$($(2)_BINDIR)/$(1) $(TARGET_DIR)/usr/bin/ +endef +endif + +# Call the generic package infrastructure to generate the necessary make +# targets +$(call inner-generic-package,$(1),$(2),$(3),$(4)) + +endef # inner-golang-package + +################################################################################ +# golang-package -- the target generator macro for Go packages +################################################################################ + +golang-package = $(call inner-golang-package,$(pkgname),$(call UPPERCASE,$(pkgname)),$(call UPPERCASE,$(pkgname)),target)