Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CONTRIBUTORS
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Brian Silverman <bsilver16384@gmail.com>
Damien Martin-Guillerez <dmarting@google.com>
David Chen <dzc@google.com>
David Santiago <david.santiago@gmail.com>
Fabian Meumertzheim <fabian@meumertzhe.im>
Han-Wen Nienhuys <hanwen@google.com>
Ian Cottrell <iancottrell@google.com>
Jake Voytko <jake@reviewninja.com>
Expand Down
8 changes: 6 additions & 2 deletions go/private/actions/archive.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,13 @@ def emit_archive(go, source = None, _recompile_suffix = ""):
for a in direct:
x_defs.update(a.x_defs)
cgo_exports_direct = list(source.cgo_exports)

# Ensure that the _cgo_export.h of the current target comes first when cgo_exports is iterated
# by prepending it and specifying the order explicitly. This is required as the CcInfo attached
# to the archive only exposes a single header rather than combining all headers.
if out_cgo_export_h:
cgo_exports_direct.append(out_cgo_export_h)
cgo_exports = depset(direct = cgo_exports_direct, transitive = [a.cgo_exports for a in direct])
cgo_exports_direct.insert(0, out_cgo_export_h)
cgo_exports = depset(direct = cgo_exports_direct, transitive = [a.cgo_exports for a in direct], order = "preorder")
return GoArchive(
source = source,
data = data,
Expand Down
24 changes: 22 additions & 2 deletions tests/core/c_linkmodes/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,16 @@ cc_library(
name = "adder_sandwich_cc",
srcs = ["add_sandwich.c"],
hdrs = ["add_sandwich.h"],
alwayslink = True,
linkstatic = True,
alwayslink = True,
)

go_binary(
name = "adder_sandwich_archive",
srcs = ["add_sandwich.go"],
cdeps = [":adder_sandwich_cc"],
cgo = True,
linkmode = "c-archive",
cdeps = [":adder_sandwich_cc"],
tags = ["manual"],
)

Expand All @@ -117,3 +117,23 @@ cc_test(
"//conditions:default": [":adder_sandwich_archive"],
}),
)

go_binary(
name = "go_with_cgo_dep",
srcs = select({
"@io_bazel_rules_go//go/platform:windows": ["empty.go"],
"//conditions:default": ["go_with_cgo_dep.go"],
}),
cgo = True,
linkmode = "c-archive",
deps = ["@org_golang_x_sys//unix"],
)

cc_binary(
name = "go_with_cgo_dep_caller",
srcs = select({
"@io_bazel_rules_go//go/platform:windows": ["skip.c"],
"//conditions:default": ["go_with_cgo_dep_caller.cc"],
}),
deps = [":go_with_cgo_dep"],
)
29 changes: 29 additions & 0 deletions tests/core/c_linkmodes/go_with_cgo_dep.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2021 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.

package main

import "C"
import (
"fmt"

"golang.org/x/sys/unix"
)

//export UnixHello
func UnixHello() {
fmt.Printf("Hello, %d\n", unix.Getuid())
}

func main() {}
20 changes: 20 additions & 0 deletions tests/core/c_linkmodes/go_with_cgo_dep_caller.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright 2021 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.

#include "tests/core/c_linkmodes/go_with_cgo_dep.h"

int main() {
UnixHello();
return 0;
}