|
| 1 | +// Copyright 2026 The Bazel Authors. All rights reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package cgo_ldflags_test |
| 16 | + |
| 17 | +import ( |
| 18 | + "testing" |
| 19 | + |
| 20 | + "github.com/bazelbuild/rules_go/go/tools/bazel_testing" |
| 21 | +) |
| 22 | + |
| 23 | +func TestMain(m *testing.M) { |
| 24 | + bazel_testing.TestMain(m, bazel_testing.Args{ |
| 25 | + Main: ` |
| 26 | +-- BUILD.bazel -- |
| 27 | +load("@io_bazel_rules_go//go:def.bzl", "go_binary") |
| 28 | +load("@rules_cc//cc:cc_library.bzl", "cc_library") |
| 29 | +
|
| 30 | +cc_library( |
| 31 | + name = "answer", |
| 32 | + srcs = ["answer.c"], |
| 33 | + hdrs = ["answer.h"], |
| 34 | +) |
| 35 | +
|
| 36 | +go_binary( |
| 37 | + name = "cgo_ldflags", |
| 38 | + srcs = ["main.go"], |
| 39 | + cdeps = [":answer"], |
| 40 | + cgo = True, |
| 41 | + # Go 1.27 expands response files before parsing cgo flags. Keep multiple |
| 42 | + # linker flags here so an unquoted response file fails the build. |
| 43 | + clinkopts = [ |
| 44 | + "-g", |
| 45 | + "-v", |
| 46 | + ], |
| 47 | + pure = "off", |
| 48 | +) |
| 49 | +
|
| 50 | +-- answer.h -- |
| 51 | +int answer(void); |
| 52 | +
|
| 53 | +-- answer.c -- |
| 54 | +#include "answer.h" |
| 55 | +
|
| 56 | +int answer(void) { |
| 57 | + return 42; |
| 58 | +} |
| 59 | +
|
| 60 | +-- main.go -- |
| 61 | +package main |
| 62 | +
|
| 63 | +/* |
| 64 | +#include "answer.h" |
| 65 | +*/ |
| 66 | +import "C" |
| 67 | +
|
| 68 | +import ( |
| 69 | + "fmt" |
| 70 | + "runtime" |
| 71 | +) |
| 72 | +
|
| 73 | +func main() { |
| 74 | + if got := runtime.Version(); got != "go1.27rc1" { |
| 75 | + panic(fmt.Sprintf("built with %s, want go1.27rc1", got)) |
| 76 | + } |
| 77 | + if got := int(C.answer()); got != 42 { |
| 78 | + panic(fmt.Sprintf("C answer = %d, want 42", got)) |
| 79 | + } |
| 80 | +} |
| 81 | +`, |
| 82 | + ModuleFileSuffix: ` |
| 83 | +bazel_dep(name = "rules_cc", version = "0.1.5") |
| 84 | +
|
| 85 | +cc_configure = use_extension("@rules_cc//cc:extensions.bzl", "cc_configure_extension") |
| 86 | +use_repo(cc_configure, "local_config_cc") |
| 87 | +
|
| 88 | +go_sdk = use_extension("@io_bazel_rules_go//go:extensions.bzl", "go_sdk") |
| 89 | +go_sdk.download( |
| 90 | + name = "go_1_27_rc1", |
| 91 | + sdks = { |
| 92 | + "darwin_amd64": ["go1.27rc1.darwin-amd64.tar.gz", "ec2abfa675a1882a13fd72035bdc50635b5b4a2b424c1f692a5737b0a333a322"], |
| 93 | + "darwin_arm64": ["go1.27rc1.darwin-arm64.tar.gz", "7b7b4d66fc0a7bc8e57b3602340dfef46d4f5f95fc5d30e5c5af6a671830de54"], |
| 94 | + "linux_amd64": ["go1.27rc1.linux-amd64.tar.gz", "102a6055d682b1f233bc1741122cc6fddae7a7dded1305fbcc30079984187144"], |
| 95 | + "linux_arm64": ["go1.27rc1.linux-arm64.tar.gz", "e9338b657430c7c32fffec696ce7d7b0286f99b65f8f90a2f5a6781a34f34928"], |
| 96 | + "windows_amd64": ["go1.27rc1.windows-amd64.zip", "10d2c755c76ca94008558bb9ec93154529ea1c0a38abe938d9b6406a9d18ffe3"], |
| 97 | + }, |
| 98 | + version = "1.27rc1", |
| 99 | +) |
| 100 | +`, |
| 101 | + }) |
| 102 | +} |
| 103 | + |
| 104 | +func TestGo127CgoWithMultipleLinkerFlags(t *testing.T) { |
| 105 | + if err := bazel_testing.RunBazel( |
| 106 | + "run", |
| 107 | + "--@io_bazel_rules_go//go/toolchain:sdk_version=1.27rc1", |
| 108 | + "//:cgo_ldflags", |
| 109 | + ); err != nil { |
| 110 | + t.Fatal(err) |
| 111 | + } |
| 112 | +} |
0 commit comments