-
-
Notifications
You must be signed in to change notification settings - Fork 661
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Split C and C++ compilation #1366
Changes from 3 commits
253b2d6
b7d8a8b
eda7f26
9e92da8
76e73a1
5614b4d
53ab130
d7b39f9
137685e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -78,20 +78,23 @@ def _c_filter_options(options, blacklist): | |
return [opt for opt in options | ||
if not any([opt.startswith(prefix) for prefix in blacklist])] | ||
|
||
def _select_archive(files): | ||
def _select_archives(files): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this is safe. See the documentation below. Instead of calling this once on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i still need to check that one out There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok got it, i've pushed a fix |
||
"""Selects a single archive from a list of files produced by a | ||
static cc_library. | ||
|
||
In some configurations, cc_library can produce multiple files, and the | ||
order isn't guaranteed, so we can't simply pick the first one. | ||
""" | ||
# list of file extensions in descending order or preference. | ||
outs = [] | ||
exts = [".pic.lo", ".lo", ".a"] | ||
for ext in exts: | ||
for f in files: | ||
if f.basename.endswith(ext): | ||
return f | ||
fail("cc_library did not produce any files") | ||
outs.append(f) | ||
if not outs: | ||
fail("cc_library did not produce any files") | ||
return outs | ||
|
||
def _cgo_codegen_impl(ctx): | ||
go = go_context(ctx) | ||
|
@@ -111,6 +114,7 @@ def _cgo_codegen_impl(ctx): | |
args.add(["-cc", str(cc), "-objdir", out_dir]) | ||
|
||
c_outs = [cgo_export_h, cgo_export_c] | ||
cxx_outs = [cgo_export_h] | ||
transformed_go_outs = [] | ||
gen_go_outs = [cgo_types] | ||
|
||
|
@@ -135,6 +139,11 @@ def _cgo_codegen_impl(ctx): | |
gen_file = go.declare_file(go, path=mangled_stem + ".cgo1."+src_ext) | ||
c_outs.append(gen_file) | ||
args.add(["-src", gen_file.path + "=" + src.path]) | ||
for src in source.cxx: | ||
mangled_stem, src_ext = _mangle(src, stems) | ||
gen_file = go.declare_file(go, path=mangled_stem + ".cgo1."+src_ext) | ||
cxx_outs.append(gen_file) | ||
args.add(["-src", gen_file.path + "=" + src.path]) | ||
|
||
inputs = sets.union(ctx.files.srcs, go.crosstool, go.stdlib.files, | ||
*[d.cc.transitive_headers for d in ctx.attr.deps]) | ||
|
@@ -164,7 +173,7 @@ def _cgo_codegen_impl(ctx): | |
args.add(copts) | ||
ctx.actions.run( | ||
inputs = inputs + go.crosstool, | ||
outputs = c_outs + gen_go_outs + transformed_go_outs + [cgo_main], | ||
outputs = c_outs + cxx_outs + gen_go_outs + transformed_go_outs + [cgo_main], | ||
mnemonic = "CGoCodeGen", | ||
progress_message = "CGoCodeGen %s" % ctx.label, | ||
executable = go.builders.cgo, | ||
|
@@ -185,6 +194,7 @@ def _cgo_codegen_impl(ctx): | |
), | ||
OutputGroupInfo( | ||
c_files = sets.union(c_outs, source.headers), | ||
cxx_files = sets.union(cxx_outs, source.headers), | ||
go_files = sets.union(transformed_go_outs, gen_go_outs), | ||
main_c = as_set([cgo_main]), | ||
), | ||
|
@@ -199,6 +209,8 @@ _cgo_codegen = go_rule( | |
providers = ["cc"], | ||
), | ||
"copts": attr.string_list(), | ||
"cxxopts": attr.string_list(), | ||
"cppopts": attr.string_list(), | ||
"linkopts": attr.string_list(), | ||
}, | ||
) | ||
|
@@ -254,7 +266,7 @@ def _not_pure(ctx, mode): | |
def _cgo_resolve_source(go, attr, source, merge): | ||
library = source["library"] | ||
cgo_info = library.cgo_info | ||
|
||
source["orig_srcs"] = cgo_info.orig_srcs | ||
source["runfiles"] = cgo_info.runfiles | ||
if source["mode"].pure: | ||
|
@@ -266,7 +278,7 @@ def _cgo_resolve_source(go, attr, source, merge): | |
source["cover"] = cgo_info.transformed_go_srcs | ||
source["cgo_deps"] = cgo_info.cgo_deps | ||
source["cgo_exports"] = cgo_info.cgo_exports | ||
source["cgo_archive"] = cgo_info.cgo_archive | ||
source["cgo_archives"] = cgo_info.cgo_archives | ||
|
||
def _cgo_collect_info_impl(ctx): | ||
go = go_context(ctx) | ||
|
@@ -283,7 +295,7 @@ def _cgo_collect_info_impl(ctx): | |
gen_go_srcs = codegen.gen_go + import_files, | ||
cgo_deps = codegen.deps, | ||
cgo_exports = codegen.exports, | ||
cgo_archive = _select_archive(ctx.files.lib), | ||
cgo_archives = _select_archives(ctx.files.libs), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please update documentation for FYI, I'm okay with small API breaking changes like this in providers between major versions. I doubt many people are using this yet. In the future, we may need to be more strict about this though. |
||
runfiles = runfiles, | ||
), | ||
) | ||
|
@@ -305,7 +317,7 @@ _cgo_collect_info = go_rule( | |
mandatory = True, | ||
providers = [_CgoCodegen], | ||
), | ||
"lib": attr.label( | ||
"libs": attr.label_list( | ||
mandatory = True, | ||
providers = ["cc"], | ||
), | ||
|
@@ -315,7 +327,7 @@ _cgo_collect_info = go_rule( | |
"""No-op rule that collects information from _cgo_codegen and cc_library | ||
info into a GoSourceList provider for easy consumption.""" | ||
|
||
def setup_cgo_library(name, srcs, cdeps, copts, clinkopts): | ||
def setup_cgo_library(name, srcs, cdeps, copts, cxxopts, cppopts, clinkopts): | ||
# Apply build constraints to source files (both Go and C) but not to header | ||
# files. Separate filtered Go and C sources. | ||
|
||
|
@@ -325,13 +337,17 @@ def setup_cgo_library(name, srcs, cdeps, copts, clinkopts): | |
"external/" + REPOSITORY_NAME[1:] if len(REPOSITORY_NAME) > 1 else "", | ||
PACKAGE_NAME) | ||
copts = copts + ["-I", base_dir] | ||
cxxopts = cxxopts + ["-I", base_dir] | ||
cppopts = cppopts + ["-I", base_dir] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since we're separating Please also double-check all mentions of |
||
|
||
cgo_codegen_name = name + ".cgo_codegen" | ||
_cgo_codegen( | ||
name = cgo_codegen_name, | ||
srcs = srcs, | ||
deps = cdeps, | ||
copts = copts, | ||
cxxopts = cxxopts, | ||
cppopts = cppopts, | ||
linkopts = clinkopts, | ||
visibility = ["//visibility:private"], | ||
) | ||
|
@@ -352,6 +368,14 @@ def setup_cgo_library(name, srcs, cdeps, copts, clinkopts): | |
visibility = ["//visibility:private"], | ||
) | ||
|
||
select_cxx_files = name + ".select_cxx_files" | ||
native.filegroup( | ||
name = select_cxx_files, | ||
srcs = [cgo_codegen_name], | ||
output_group = "cxx_files", | ||
visibility = ["//visibility:private"], | ||
) | ||
|
||
select_main_c = name + ".select_main_c" | ||
native.filegroup( | ||
name = select_main_c, | ||
|
@@ -366,9 +390,9 @@ def setup_cgo_library(name, srcs, cdeps, copts, clinkopts): | |
platform_copts = _DEFAULT_PLATFORM_COPTS | ||
platform_linkopts = platform_copts | ||
|
||
cgo_lib_name = name + ".cgo_c_lib" | ||
cgo_c_lib_name = name + ".cgo_c_lib" | ||
native.cc_library( | ||
name = cgo_lib_name, | ||
name = cgo_c_lib_name, | ||
srcs = [select_c_files], | ||
deps = cdeps, | ||
copts = copts + platform_copts + [ | ||
|
@@ -382,13 +406,33 @@ def setup_cgo_library(name, srcs, cdeps, copts, clinkopts): | |
visibility = ["//visibility:private"], | ||
) | ||
|
||
cgo_cxx_lib_name = name + ".cgo_cxx_lib" | ||
native.cc_library( | ||
name = cgo_cxx_lib_name, | ||
srcs = [select_cxx_files], | ||
deps = cdeps, | ||
copts = cxxopts + platform_copts + [ | ||
# The generated thunks often contain unused variables. | ||
"-Wno-unused-variable", | ||
], | ||
linkopts = clinkopts + platform_linkopts, | ||
linkstatic = 1, | ||
# _cgo_.o needs all symbols because _cgo_import needs to see them. | ||
alwayslink = 1, | ||
visibility = ["//visibility:private"], | ||
) | ||
cgo_libs = [ | ||
cgo_c_lib_name, | ||
cgo_cxx_lib_name, | ||
] | ||
|
||
# Create a loadable object with no undefined references. cgo reads this | ||
# when it generates _cgo_import.go. | ||
cgo_o_name = name + "._cgo_.o" | ||
native.cc_binary( | ||
name = cgo_o_name, | ||
srcs = [select_main_c], | ||
deps = cdeps + [cgo_lib_name], | ||
deps = cdeps + cgo_libs, | ||
copts = copts, | ||
linkopts = clinkopts, | ||
visibility = ["//visibility:private"], | ||
|
@@ -408,7 +452,7 @@ def setup_cgo_library(name, srcs, cdeps, copts, clinkopts): | |
name = cgo_embed_name, | ||
srcs = srcs, | ||
codegen = cgo_codegen_name, | ||
lib = cgo_lib_name, | ||
libs = cgo_libs, | ||
cgo_import = cgo_import_name, | ||
visibility = ["//visibility:private"], | ||
) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,8 @@ _CGO_ATTRS = { | |
"srcs": None, | ||
"cdeps": [], | ||
"copts": [], | ||
"cxxopts": [], | ||
"cppopts": [], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please update attribute tables in go/core.rst to include the new attributes. This may break people that have C++ code that currently depends on options in |
||
"clinkopts": [], | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please update documentation in
go/toolchains.rst#pack
to reflect this change.