diff --git a/go/private/actions/compilepkg.bzl b/go/private/actions/compilepkg.bzl index 6398616000..48eb6dc9b9 100644 --- a/go/private/actions/compilepkg.bzl +++ b/go/private/actions/compilepkg.bzl @@ -92,10 +92,7 @@ def emit_compilepkg( if testfilter: args.add("-testfilter", testfilter) - gc_flags = [ - go._ctx.expand_make_variables("gc_goopts", f, {}) - for f in gc_goopts - ] + gc_flags = list(gc_goopts) asm_flags = [] if go.mode.race: gc_flags.append("-race") diff --git a/go/private/context.bzl b/go/private/context.bzl index f82ab66f4f..e5cef027a7 100644 --- a/go/private/context.bzl +++ b/go/private/context.bzl @@ -226,14 +226,14 @@ def _library_to_source(go, attr, library, coverage_instrumented): "embedsrcs": embedsrcs, "x_defs": {}, "deps": getattr(attr, "deps", []), - "gc_goopts": getattr(attr, "gc_goopts", []), + "gc_goopts": _expand_opts(go, "gc_goopts", getattr(attr, "gc_goopts", [])), "runfiles": _collect_runfiles(go, getattr(attr, "data", []), getattr(attr, "deps", [])), "cgo": getattr(attr, "cgo", False), "cdeps": getattr(attr, "cdeps", []), - "cppopts": getattr(attr, "cppopts", []), - "copts": getattr(attr, "copts", []), - "cxxopts": getattr(attr, "cxxopts", []), - "clinkopts": getattr(attr, "clinkopts", []), + "cppopts": _expand_opts(go, "cppopts", getattr(attr, "cppopts", [])), + "copts": _expand_opts(go, "copts", getattr(attr, "copts", [])), + "cxxopts": _expand_opts(go, "cxxopts", getattr(attr, "cxxopts", [])), + "clinkopts": _expand_opts(go, "clinkopts", getattr(attr, "clinkopts", [])), "cgo_deps": [], "cgo_exports": [], } @@ -815,3 +815,6 @@ go_config = rule( configuration. Rules may depend on this instead of depending on all the build settings directly.""", ) + +def _expand_opts(go, attribute_name, opts): + return [go._ctx.expand_make_variables(attribute_name, opt, {}) for opt in opts] diff --git a/tests/core/cgo/BUILD.bazel b/tests/core/cgo/BUILD.bazel index 68a7668f91..fd8dfec486 100644 --- a/tests/core/cgo/BUILD.bazel +++ b/tests/core/cgo/BUILD.bazel @@ -7,6 +7,33 @@ go_test( embed = [":opts"], ) +genrule( + name = "generate_header_copts", + outs = ["generated_copts/generated_copts.h"], + cmd = "echo '#define GENERATED_COPTS 1' >$@", +) + +genrule( + name = "generate_header_cppopts", + outs = ["generated_cppopts/generated_cppopts.h"], + cmd = "echo '#define GENERATED_CPPOPTS 1' >$@", +) + +genrule( + name = "generate_header_cxxopts", + outs = ["generated_cxxopts/generated_cxxopts.h"], + cmd = "echo '#define GENERATED_CXXOPTS 1' >$@", +) + +cc_library( + name = "generated_headers", + hdrs = [ + "generated_copts/generated_copts.h", + "generated_cppopts/generated_cppopts.h", + "generated_cxxopts/generated_cxxopts.h", + ], +) + go_library( name = "opts", srcs = [ @@ -21,10 +48,23 @@ go_library( ], "//conditions:default": [], }), + cdeps = [":generated_headers"], cgo = True, - copts = ["-DRULES_GO_C"], - cppopts = ["-DRULES_GO_CPP"], - cxxopts = ["-DRULES_GO_CXX"], + copts = [ + "-DRULES_GO_C", + "-I$(GENDIR)/tests/core/cgo/generated_copts", + "-DDOLLAR_SIGN_C=$$", # the dollar sign should be escaped + ], + cppopts = [ + "-DRULES_GO_CPP", + "-I$(GENDIR)/tests/core/cgo/generated_cppopts", + "-DDOLLAR_SIGN_CPP=$$", # the dollar sign should be escaped + ], + cxxopts = [ + "-DRULES_GO_CXX", + "-I$(GENDIR)/tests/core/cgo/generated_cxxopts", + "-DDOLLAR_SIGN_CXX=$$", # the dollar sign should be escaped + ], importpath = "github.com/bazelbuild/rules_go/tests/core/cxx", ) diff --git a/tests/core/cgo/add.c b/tests/core/cgo/add.c index ecb94d2946..fba09af282 100644 --- a/tests/core/cgo/add.c +++ b/tests/core/cgo/add.c @@ -1,9 +1,19 @@ #include +#include +#include #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. #endif +#if !defined(GENERATED_COPTS) || !defined(GENERATED_CPPOPTS) || defined(GENERATED_CXXOPTS) +#error Generated headers should be correctly included +#endif + int add_c(int a, int b) { - return a + b; + int $ = 0; + int sum = a + b; + sum += DOLLAR_SIGN_C; + sum += DOLLAR_SIGN_CPP; + return sum; } diff --git a/tests/core/cgo/add.cpp b/tests/core/cgo/add.cpp index 87d78e8035..e5dbc0a93b 100644 --- a/tests/core/cgo/add.cpp +++ b/tests/core/cgo/add.cpp @@ -1,9 +1,19 @@ #include "add.h" +#include +#include #if !defined(RULES_GO_CPP) || !defined(RULES_GO_CXX) || defined(RULES_GO_C) #error This is a C++ file, only RULES_GO_CXX and RULES_GO_CPP should be defined. #endif +#if !defined(GENERATED_CPPOPTS) || !defined(GENERATED_CXXOPTS) || defined(GENERATED_COPTS) +#error Generated headers should be correctly included +#endif + int add_cpp(int a, int b) { - return a + b; + int $ = 0; + int sum = a + b; + sum += DOLLAR_SIGN_CXX; + sum += DOLLAR_SIGN_CPP; + return sum; } diff --git a/tests/core/cgo/add.m b/tests/core/cgo/add.m index d0df575f78..5a321b29fd 100644 --- a/tests/core/cgo/add.m +++ b/tests/core/cgo/add.m @@ -1,5 +1,11 @@ #include "add.h" +#include +#include #if !defined(RULES_GO_C) || !defined(RULES_GO_CPP) || defined(RULES_GO_CXX) #error This is an Objective-C file, only RULES_GO_C and RULES_GO_CPP should be defined. #endif + +#if !defined(GENERATED_COPTS) || !defined(GENERATED_CPPOPTS) || defined(GENERATED_CXXOPTS) +#error Generated headers should be correctly included +#endif diff --git a/tests/core/cgo/add.mm b/tests/core/cgo/add.mm index 2b9693d171..aed54ab527 100644 --- a/tests/core/cgo/add.mm +++ b/tests/core/cgo/add.mm @@ -1,5 +1,11 @@ #include "add.h" +#include +#include #if !defined(RULES_GO_CPP) || !defined(RULES_GO_CXX) || defined(RULES_GO_C) #error This is an Objective-C++ file, only RULES_GO_CXX and RULES_GO_CPP should be defined. #endif + +#if !defined(GENERATED_CPPOPTS) || !defined(GENERATED_CXXOPTS) || defined(GENERATED_COPTS) +#error Generated headers should be correctly included +#endif