Skip to content

Commit

Permalink
cgo: use -I instead of -iquote for source header directories (#2687)
Browse files Browse the repository at this point in the history
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 #2685
  • Loading branch information
Jay Conrod authored Oct 23, 2020
1 parent 2bada59 commit 765f676
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
17 changes: 11 additions & 6 deletions go/private/rules/cgo.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ load(
"as_iterable",
"has_simple_shared_lib_extension",
"has_versioned_shared_lib_extension",
"hdr_exts",
)
load(
"//go/private:mode.bzl",
Expand Down Expand Up @@ -61,9 +62,6 @@ def cgo_configure(go, srcs, cdeps, cppopts, copts, cxxopts, clinkopts):
fail("Go toolchain does not support cgo")

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

inputs_direct = []
inputs_transitive = []
Expand Down
2 changes: 1 addition & 1 deletion tests/core/cgo/add.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "add.h"
#include <add.h>

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

0 comments on commit 765f676

Please sign in to comment.