rules_go: Gazelle generated cgo library unable to resolve mac libraries

CGo libraries fail to link frameworks. Given // #cgo LDFLAGS: -framework IOKit the following error is generated:

ERROR: /project/vendor/github.com/shirou/gopsutil/host/BUILD.bazel:3:1: in linkopts attribute of cc_library rule //vendor/github.com/shirou/gopsutil/host:cgo_default_library.cgo_c_lib: could not resolve label 'IOKit'. Since this rule was created by the macro 'cgo_library', the error might have been caused by the macro implementation in /private/var/tmp/_bazel_edward/ad32b996cb424fa4cc44a5c86220cb2f/external/io_bazel_rules_go/go/private/cgo.bzl:402:18.
cgo_library(
    name = "cgo_default_library",
    srcs = select({
        "@io_bazel_rules_go//go/platform:darwin_amd64": [
            "host_darwin_cgo.go",
        ],
        "//conditions:default": [],
    }),
    clinkopts = select({
        "@io_bazel_rules_go//go/platform:darwin_amd64": [
            "-framework",
	    "IOKit",
        ],
        "//conditions:default": [],
    }),
    visibility = ["//visibility:private"],
)```

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 16 (9 by maintainers)

Commits related to this issue

Most upvoted comments

Hey @jayconrod bumping into this issue when trying to build https://github.com/shirou/gopsutil on OSX as a vendored dependency with Bazel.

Current error log looks like

INFO: Found 4 targets...
ERROR: /Users/ains/Projects/infra-go/go/src/improbable.io/vendor/github.com/shirou/gopsutil/host/BUILD.bazel:3:1: CGoCodeGen //go/src/improbable.io/vendor/github.com/shirou/gopsutil/host:go_default_library.cgo_codegen failed (Exit 1).
/private/var/tmp/_bazel_ains/d5881538c6f49328692a7434d44d9eb5/bazel-sandbox/2182892111453255433/execroot/__main__/go/src/improbable.io/vendor/github.com/shirou/gopsutil/host/host_darwin_cgo.go:7:11: fatal error: 'include/smc.c' file not found
 #include "include/smc.c"
          ^~~~~~~~~~~~~~~
1 error generated.
CgoCodegen: error running cgo: exit status 1
INFO: Elapsed time: 5.134s, Critical Path: 2.82s

Generated BUILD file in our vendor directory is

load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")

go_library(
    name = "go_default_library",
    srcs = [
        "host.go",
        "host_fallback.go",
    ] + select({
        "@io_bazel_rules_go//go/platform:darwin_amd64": [
            "host_darwin.go",
            "host_darwin_amd64.go",
            "host_darwin_cgo.go",
        ],
        "@io_bazel_rules_go//go/platform:linux_amd64": [
            "host_linux.go",
            "host_linux_amd64.go",
        ],
        "@io_bazel_rules_go//go/platform:windows_amd64": [
            "host_windows.go",
        ],
        "//conditions:default": [],
    }),
    cgo = True,
    clinkopts = select({
        "@io_bazel_rules_go//go/platform:darwin_amd64": [
            "-framework IOKit",
        ],
        "//conditions:default": [],
    }),
    importpath = "github.com/shirou/gopsutil/host",
    visibility = ["//visibility:public"],
    deps = [
        "//go/src/improbable.io/vendor/github.com/shirou/gopsutil/internal/common:go_default_library",
    ] + select({
        "@io_bazel_rules_go//go/platform:darwin_amd64": [
            "//go/src/improbable.io/vendor/github.com/shirou/gopsutil/process:go_default_library",
        ],
        "@io_bazel_rules_go//go/platform:windows_amd64": [
            "//go/src/improbable.io/vendor/github.com/StackExchange/wmi:go_default_library",
            "//go/src/improbable.io/vendor/github.com/shirou/gopsutil/process:go_default_library",
            "//go/src/improbable.io/vendor/golang.org/x/sys/windows:go_default_library",
        ],
        "//conditions:default": [],
    }),
)

go_test(
    name = "go_default_test",
    srcs = [
        "host_test.go",
    ] + select({
        "@io_bazel_rules_go//go/platform:linux_amd64": [
            "host_linux_test.go",
        ],
        "//conditions:default": [],
    }),
    importpath = "github.com/shirou/gopsutil/host",
    library = ":go_default_library",
)