Skip to content

feat: ecsact_binary provides CcInfo #53

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

Merged
merged 2 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 36 additions & 36 deletions MODULE.bazel.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 50 additions & 4 deletions ecsact/private/ecsact_binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ load("//ecsact/private:ecsact_build_recipe.bzl", "EcsactBuildRecipeInfo")

def _ecsact_binary_impl(ctx):
cc_toolchain = find_cc_toolchain(ctx)
feature_configuration = cc_common.configure_features(
ctx = ctx,
cc_toolchain = cc_toolchain,
requested_features = ctx.features,
unsupported_features = ctx.disabled_features,
)

temp_dir = ctx.actions.declare_directory("{}.ecsact_binary".format(ctx.attr.name))

Expand All @@ -27,11 +33,16 @@ def _ecsact_binary_impl(ctx):

ecsact_toolchain = ctx.toolchains["//ecsact:toolchain_type"].ecsact_info

preferred_output_extension = ctx.attr.lib_extension
preferred_output_extension = ctx.attr.shared_library_extension

runtime_output_file = ctx.actions.declare_file("{}{}".format(ctx.attr.name, preferred_output_extension))
outputs = [runtime_output_file]
interface_output_file = None
if ctx.attr.interface_library_extension:
interface_output_file = ctx.actions.declare_file("{}{}".format(ctx.attr.name, ctx.attr.interface_library_extension))
tools = [] + ecsact_toolchain.tool_files
outputs = [runtime_output_file]
if interface_output_file != None:
outputs.append(interface_output_file)

args = ctx.actions.args()
args.add("build")
Expand Down Expand Up @@ -89,7 +100,29 @@ def _ecsact_binary_impl(ctx):
toolchain = Label("//ecsact:toolchain_type"),
)

library_to_link = cc_common.create_library_to_link(
actions = ctx.actions,
feature_configuration = feature_configuration,
cc_toolchain = cc_toolchain,
static_library = None,
pic_static_library = None,
interface_library = interface_output_file,
dynamic_library = runtime_output_file,
alwayslink = False,
)

linker_input = cc_common.create_linker_input(
libraries = depset([library_to_link]),
user_link_flags = depset(ctx.attr.linkopts),
owner = ctx.label,
)

linking_context = cc_common.create_linking_context(
linker_inputs = depset([linker_input]),
)

return [
CcInfo(linking_context = linking_context),
DefaultInfo(
files = depset(outputs),
),
Expand All @@ -111,22 +144,35 @@ _ecsact_binary = rule(
"@rules_cc//cc:current_cc_toolchain",
),
),
"lib_extension": attr.string(
"shared_library_extension": attr.string(
mandatory = True,
),
"interface_library_extension": attr.string(
mandatory = True,
),
"linkopts": attr.string_list(
mandatory = False,
),
},
toolchains = ["//ecsact:toolchain_type"] + use_cc_toolchain(),
fragments = ["cpp"],
)

def ecsact_binary(**kwargs):
_ecsact_binary(
lib_extension = select({
shared_library_extension = select({
"@platforms//os:windows": ".dll",
"@platforms//os:linux": ".so",
"@platforms//os:macos": ".dylib",
"@platforms//os:wasi": ".wasm",
"@platforms//os:none": ".wasm", # for non-wasi
}),
interface_library_extension = select({
"@platforms//os:windows": ".lib",
"@platforms//os:linux": "",
"@platforms//os:macos": "",
"@platforms//os:wasi": "",
"@platforms//os:none": "", # for non-wasi
}),
**kwargs
)
36 changes: 35 additions & 1 deletion ecsact/private/ecsact_build_recipe.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,28 @@ EcsactBuildRecipeInfo = provider(
},
)

CPP_HEADER_SUFFIXES = [
".hh",
".h",
".hpp",
".inl",
]

def _strip_external(p):
# type: (string) -> string
EXTERNAL_PREFIX = "external/"
if p.startswith(EXTERNAL_PREFIX):
return p[p.index("/", len(EXTERNAL_PREFIX)) + 1:]
return p

def _source_outdir(src):
# type: (File) -> string
src_path = src.path
for cpp_header_suffix in CPP_HEADER_SUFFIXES:
if src.path.endswith(cpp_header_suffix):
return "include/" + _strip_external(src.dirname)
return _strip_external(src.dirname)

def _ecsact_build_recipe(ctx):
# type: (ctx) -> None

Expand All @@ -19,11 +41,20 @@ def _ecsact_build_recipe(ctx):
for src in ctx.files.srcs:
sources.append({
"path": src.path,
"outdir": src.dirname,
"outdir": _source_outdir(src),
"relative_to_cwd": True,
})
recipe_data.append(src)

for fetch_src_outdir in ctx.attr.fetch_srcs:
fetch_srcs = []
for fetch_url in ctx.attr.fetch_srcs[fetch_src_outdir]:
fetch_srcs.append({
"fetch": fetch_url,
"outdir": fetch_src_outdir,
})
sources.extend(fetch_srcs)

for codegen_plugin in ctx.attr.codegen_plugins:
info = codegen_plugin[EcsactCodegenPluginInfo]
sources.append({
Expand Down Expand Up @@ -57,6 +88,9 @@ ecsact_build_recipe = rule(
"srcs": attr.label_list(
allow_files = True,
),
"fetch_srcs": attr.string_list_dict(
allow_empty = True,
),
"codegen_plugins": attr.label_keyed_string_dict(
providers = [EcsactCodegenPluginInfo],
),
Expand Down