From 580bd8862257eedeec7680b86590afcb524fa858 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Mon, 25 Nov 2019 15:34:59 +0000 Subject: [PATCH] feat(buildGo): Add x_defs support --- overrides/buildGo.nix | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/overrides/buildGo.nix b/overrides/buildGo.nix index 72d8c232f..8e1349bf9 100644 --- a/overrides/buildGo.nix +++ b/overrides/buildGo.nix @@ -1,3 +1,6 @@ +# Copyright 2019 Google LLC. +# SPDX-License-Identifier: Apache-2.0 +# # buildGo provides Nix functions to build Go packages in the style of Bazel's # rules_go. # @@ -5,7 +8,7 @@ # TODO(tazjin): Refactor to include /golang/protobuf/descriptor in goProto deps # TODO(tazjin): Find a way to expose documentation (esp. for generated stuff) -{ pkgs, ... }@args: +{ pkgs, ... }: let inherit (builtins) @@ -19,7 +22,7 @@ let replaceStrings toPath; - inherit (pkgs) bash lib go runCommand fetchFromGitHub protobuf; + inherit (pkgs) lib go runCommand fetchFromGitHub protobuf; # Helpers for low-level Go compiler invocations spaceOut = lib.concatStringsSep " "; @@ -44,15 +47,17 @@ let allDeps = deps: lib.unique (lib.flatten (deps ++ (map (d: d.goDeps) deps))); + xFlags = x_defs: spaceOut (map (k: "-X ${k}=${x_defs."${k}"}") (attrNames x_defs)); + # High-level build functions # Build a Go program out of the specified files and dependencies. - program = { name, srcs, deps ? [] }: + program = { name, srcs, deps ? [], x_defs ? {} }: let uniqueDeps = allDeps deps; in runCommand name {} '' ${go}/bin/go tool compile -o ${name}.a -trimpath=$PWD -trimpath=${go} ${includeSources uniqueDeps} ${spaceOut srcs} mkdir -p $out/bin - ${go}/bin/go tool link -o $out/bin/${name} -buildid nix ${includeLibs uniqueDeps} ${name}.a + ${go}/bin/go tool link -o $out/bin/${name} -buildid nix ${xFlags x_defs} ${includeLibs uniqueDeps} ${name}.a ''; # Build a Go library assembled out of the specified files.