Skip to content

Commit 765f676

Browse files
author
Jay Conrod
authored
cgo: use -I instead of -iquote for source header directories (bazel-contrib#2687)
The go command uses -I for package directories, which makes directories available for "" and <> includes. We should do the same. We no longer use ctx.build_file_path, since that directory might not actually provide any build files, and its value changed in Bazel 3.7.0. Fixes bazel-contrib#2685
1 parent 2bada59 commit 765f676

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

go/private/rules/cgo.bzl

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ load(
1717
"as_iterable",
1818
"has_simple_shared_lib_extension",
1919
"has_versioned_shared_lib_extension",
20+
"hdr_exts",
2021
)
2122
load(
2223
"//go/private:mode.bzl",
@@ -61,9 +62,6 @@ def cgo_configure(go, srcs, cdeps, cppopts, copts, cxxopts, clinkopts):
6162
fail("Go toolchain does not support cgo")
6263

6364
cppopts = list(cppopts)
64-
base_dir, _, _ = go._ctx.build_file_path.rpartition("/")
65-
if base_dir:
66-
cppopts.extend(["-I", base_dir])
6765
copts = go.cgo_tools.c_compile_options + copts
6866
cxxopts = go.cgo_tools.cxx_compile_options + cxxopts
6967
objcopts = go.cgo_tools.objc_compile_options + copts
@@ -86,9 +84,16 @@ def cgo_configure(go, srcs, cdeps, cppopts, copts, cxxopts, clinkopts):
8684
seen_includes = {}
8785
seen_quote_includes = {}
8886
seen_system_includes = {}
89-
for f in srcs:
90-
if f.basename.endswith(".h"):
91-
_include_unique(cppopts, "-iquote", f.dirname, seen_quote_includes)
87+
have_hdrs = any([f.basename.endswith(ext) for f in srcs for ext in hdr_exts])
88+
if have_hdrs:
89+
# Add include paths for all sources so we can use include paths relative
90+
# to any source file or any header file. The go command requires all
91+
# sources to be in the same directory, but that's not necessarily the
92+
# case here.
93+
#
94+
# Use -I so either <> or "" includes may be used (same as go command).
95+
for f in srcs:
96+
_include_unique(cppopts, "-I", f.dirname, seen_includes)
9297

9398
inputs_direct = []
9499
inputs_transitive = []

tests/core/cgo/add.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "add.h"
1+
#include <add.h>
22

33
#if !defined(RULES_GO_C) || !defined(RULES_GO_CPP) || defined(RULES_GO_CXX)
44
#error This is a C file, only RULES_GO_C and RULES_GO_CPP should be defined.

0 commit comments

Comments
 (0)