bazelbuild / bazel-gazelle

Gazelle is a Bazel build file generator for Bazel projects. It natively supports Go and protobuf, and it may be extended to support new languages and custom rule sets.
Apache License 2.0
1.15k stars 369 forks source link

Header files not available when source contains assembly importing header file #1421

Open tokongs opened 1 year ago

tokongs commented 1 year ago

What version of gazelle are you using?

v0.28.0

What version of rules_go are you using?

v0.37.0

What version of Bazel are you using?

5.3.2

Does this issue reproduce with the latest releases of all the above?

What operating system and processor architecture are you using?

What did you do?

Have a dependency on github.com/cloudflare/circl

    go_repository(
        name = "com_github_cloudflare_circl",
        build_file_proto_mode = "disable_global",
        importpath = "github.com/cloudflare/circl",
        sum = "h1:bZgT/A+cikZnKIwn7xL2OBj012Bmvho/o6RpRvv3GKY=",
        version = "v1.1.0",
    )

The issue occurs when you have a source with a Go assembly file which includes a header file, and the Go code is not CGO.

What did you expect to see?

Expect Gazelle to generate valid BUILD.bazel files for this.

What did you see instead?

The generated BUILD.bazel contains a go_library which fails to build because a header is ignored as the library is not marked with cgo=True

external/com_github_cloudflare_circl/dh/x25519/curve_amd64.s:7: #include: open external/com_github_cloudflare_circl/math/fp25519/fp_amd64.h: no such file or directory

compilepkg: error running subcommand external/go_sdk/pkg/tool/linux_amd64/asm: exit status 1
aman-harness commented 1 year ago

I am facing same issue in my setup. Is there a workaround to use this repo com_github_cloudflare_circl in bazel by making some manual changes?

wenpincui commented 1 year ago

encountered same issue +1

tokongs commented 1 year ago

@aman-harness @wenpincui We solved this by patching the generated BUILD.bazel files.

go_repository(
    name = "com_github_cloudflare_circl",
    build_file_proto_mode = "disable_global",
    importpath = "github.com/cloudflare/circl",
    patches = [
        "//third-party/go/com_github_cloudflare_circl:fp448.patch",
        "//third-party/go/com_github_cloudflare_circl:fp25519.patch",
        "//third-party/go/com_github_cloudflare_circl:x448.patch",
        "//third-party/go/com_github_cloudflare_circl:x25519.patch",
    ],
        sum = "h1:bZgT/A+cikZnKIwn7xL2OBj012Bmvho/o6RpRvv3GKY=",
        version = "v1.1.0",
    )

fp448.patch

--- math/fp448/BUILD.bazel
+++ math/fp448/BUILD.bazel
@@ -10,6 +10,7 @@ go_library(
         "fp_generic.go",
         "fp_noasm.go",
     ],
+    cgo = True,
     importpath = "github.com/cloudflare/circl/math/fp448",
     visibility = ["//visibility:public"],
     deps = [
@@ -28,6 +29,8 @@ alias(
     visibility = ["//visibility:public"],
 )

+exports_files(["fp_amd64.h"], ["//visibility:public"])
+
 go_test(
     name = "fp448_test",
     srcs = [

x448.patch

--- dh/x448/BUILD.bazel
+++ dh/x448/BUILD.bazel
@@ -12,7 +12,9 @@ go_library(
         "doc.go",
         "key.go",
         "table.go",
+       "//math/fp448:fp_amd64.h",
     ],
+    cgo = True,
     importpath = "github.com/cloudflare/circl/dh/x448",
     visibility = ["//visibility:public"],
     deps = [

With similar files for fp25519 and x25519

satreix commented 1 year ago

Thanks @tokongs for the workaround. I use it here.

ramilmsh commented 1 year ago

@tokongs thank you!

friendly-pineapple commented 1 year ago

Thank you @tokongs!!!

ashi009 commented 11 months ago

Duplicate of #1393

klandergren commented 3 months ago

FYI: if you are using bzlmod there is another workaround described in this comment on https://github.com/bazelbuild/rules_go/issues/3799