Skip to content

Commit

Permalink
Add test generates long compile/link command lines
Browse files Browse the repository at this point in the history
Related #1637
  • Loading branch information
Jay Conrod committed Aug 13, 2018
1 parent 2179a6e commit 7734f03
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 0 deletions.
3 changes: 3 additions & 0 deletions tests/core/go_binary/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_test")
load(":many_deps.bzl", "many_deps")

test_suite(name = "go_binary")

Expand All @@ -18,3 +19,5 @@ go_binary(
srcs = ["custom_bin.go"],
out = "alt_bin",
)

many_deps(name = "many_deps")
7 changes: 7 additions & 0 deletions tests/core/go_binary/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,10 @@ out_test

Test that a `go_binary`_ rule can write its executable file with a custom name
in the package directory (not the mode directory).

many_deps
---------

Test that a `go_binary`_ with many imports with long names can be linked. This
makes sure we don't exceed command-line length limits with -I and -L flags.
Verifies #1637.
89 changes: 89 additions & 0 deletions tests/core/go_binary/many_deps.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# Copyright 2018 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

load(
"@io_bazel_rules_go//go:def.bzl",
"go_binary",
"go_context",
"go_rule",
)

_PREFIX = "/".join(["abcdefgh"[i] * 100 for i in range(7)]) + "/"

def _gen_library_impl(ctx):
go = go_context(ctx)
src = go.actions.declare_file(ctx.label.name + ".go")
go.actions.write(src, "package " + ctx.label.name + "\n")
library = go.new_library(go, srcs = [src])
source = go.library_to_source(go, ctx.attr, library, ctx.coverage_instrumented())
archive = go.archive(go, source)
return [
library,
source,
archive,
DefaultInfo(files = depset([archive.data.file])),
]

_gen_library = go_rule(
_gen_library_impl,
attrs = {
"importpath": attr.string(mandatory = True),
},
)

def _gen_main_src_impl(ctx):
src = ctx.actions.declare_file(ctx.label.name + ".go")
lines = [
"package main",
"",
"import (",
]
for i in range(ctx.attr.n):
lines.append('\t_ "{}many_deps{}"'.format(_PREFIX, i))
lines.extend([
")",
"",
"func main() {}",
])
ctx.actions.write(src, "\n".join(lines))
return [DefaultInfo(files = depset([src]))]

_gen_main_src = rule(
_gen_main_src_impl,
attrs = {
"n": attr.int(mandatory = True),
},
)

def many_deps(name, **kwargs):
deps = []
n = 200
for i in range(n):
lib_name = "many_deps" + str(i)
_gen_library(
name = lib_name,
importpath = _PREFIX + lib_name,
visibility = ["//visibility:private"],
)
deps.append(lib_name)
_gen_main_src(
name = "many_deps_src",
n = n,
)
go_binary(
name = name,
srcs = [":many_deps_src"],
deps = deps,
**kwargs
)

0 comments on commit 7734f03

Please sign in to comment.