Skip to content

Commit

Permalink
feat: add ecsact binary and recipe to defs
Browse files Browse the repository at this point in the history
  • Loading branch information
zaucy committed Oct 5, 2023
1 parent 1a09647 commit e729a01
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 12 deletions.
2 changes: 2 additions & 0 deletions ecsact/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
load("//ecsact/private:ecsact_codegen.bzl", _ecsact_codegen = "ecsact_codegen")
load("//ecsact/private:ecsact_codegen_plugin.bzl", _ecsact_codegen_plugin = "ecsact_codegen_plugin")
load("//ecsact/private:ecsact_binary.bzl", _ecsact_binary = "ecsact_binary")
load("//ecsact/private:ecsact_build_recipe.bzl", _ecsact_build_recipe = "ecsact_build_recipe")
load("//ecsact/private:ecsact_library.bzl", _ecsact_library = "ecsact_library")

ecsact_codegen = _ecsact_codegen
ecsact_codegen_plugin = _ecsact_codegen_plugin
ecsact_binary = _ecsact_binary
ecsact_build_recipe = _ecsact_build_recipe
ecsact_library = _ecsact_library
32 changes: 23 additions & 9 deletions ecsact/private/ecsact_binary.bzl
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
load("@rules_cc//cc:find_cc_toolchain.bzl", "find_cc_toolchain", "use_cc_toolchain")
load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES")
load("//ecsact/private:ecsact_build_recipe.bzl", "EcsactBuildRecipeInfo")

def _ecsact_binary(ctx):
cc_toolchain = find_cc_toolchain(ctx)

temp_dir = ctx.actions.declare_directory(ctx.attr.name)

inputs = []

feature_configuration = cc_common.configure_features(
ctx = ctx,
cc_toolchain = cc_toolchain,
Expand Down Expand Up @@ -32,8 +37,8 @@ def _ecsact_binary(ctx):
args.add_all(ctx.files.srcs)
args.add_all(ctx.files.recipes, before_each = "-r")
args.add("-o", runtime_output_file)
args.add("--temp_dir", ctx.attr.name)
args.add("-f", "none")
args.add("--temp_dir", temp_dir.path)
args.add("-f", "text")

# TODO(zaucy): detect shared library extension
preferred_output_extension = ".dll"
Expand All @@ -55,20 +60,29 @@ def _ecsact_binary(ctx):

args.add("--compiler_config", compiler_config_file)

for p in ecsact_toolchain.target_tool_path_runfiles:
tools.extend(p.files.to_list())

if len(ctx.files.recipes) > 1:
fail("Only 1 recipe is allowed at this time")

inputs.extend(ctx.files.srcs)
inputs.extend(ctx.files.recipes)
inputs.append(compiler_config_file)

for recipe in ctx.attr.recipes:
recipe_info = recipe[EcsactBuildRecipeInfo]
inputs.extend(recipe_info.data)

executable = ecsact_toolchain.target_tool if ecsact_toolchain.target_tool != None else ecsact_toolchain.target_tool_path

ctx.actions.run(
mnemonic = "EcsactBuild",
outputs = outputs,
inputs = ctx.files.srcs + ctx.files.recipes + [compiler_config_file],
executable = ecsact_toolchain.target_tool_path,
progress_message = "Building Ecsact Runtime %{output}",
outputs = outputs + [temp_dir],
inputs = inputs,
executable = executable,
tools = tools,
arguments = [args],
env = env,
toolchain = Label("//ecsact:toolchain_type"),
)

return [
Expand All @@ -86,7 +100,7 @@ ecsact_binary = rule(
"recipes": attr.label_list(
allow_empty = False,
mandatory = True,
allow_files = True,
providers = [EcsactBuildRecipeInfo],
),
"_cc_toolchain": attr.label(
default = Label(
Expand Down
68 changes: 68 additions & 0 deletions ecsact/private/ecsact_build_recipe.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
load("//ecsact/private:ecsact_codegen_plugin.bzl", "EcsactCodegenPluginInfo")

EcsactBuildRecipeInfo = provider(
doc = "",
fields = {
"recipe_path": "path to recipe yaml file",
"data": "files needed to build recipe",
},
)

def _ecsact_build_recipe(ctx):
recipe_yaml = ctx.actions.declare_file("{}.yml".format(ctx.attr.name))

sources = []
recipe_data = []

for dep in ctx.attr.deps:
cc_info = dep[CcInfo]
print(cc_info)
fail("todo check cc info")

for src in ctx.files.srcs:
print(src)
sources.append({
"path": src.path,
"outdir": "src",
"relative_to_cwd": True,
})
recipe_data.append(src)

recipe = {
"name": ctx.attr.name,
"sources": sources,
"imports": ctx.attr.imports,
"exports": ctx.attr.exports,
}

ctx.actions.write(recipe_yaml, json.encode(recipe))

return [
DefaultInfo(
files = depset([recipe_yaml]),
),
EcsactBuildRecipeInfo(
recipe_path = recipe_yaml,
data = recipe_data,
),
]

ecsact_build_recipe = rule(
implementation = _ecsact_build_recipe,
attrs = {
"srcs": attr.label_list(
allow_files = True,
),
"deps": attr.label_list(
providers = [CcInfo],
),
"codegen_plugins": attr.label_keyed_string_dict(
providers = [EcsactCodegenPluginInfo],
),
"imports": attr.string_list(
),
"exports": attr.string_list(
mandatory = True,
),
},
)
3 changes: 0 additions & 3 deletions ecsact/private/ecsact_codegen.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ def _ecsact_codegen(ctx):
out_basename = ctx.attr.name + "/" + src.basename
outputs.append(ctx.actions.declare_file(out_basename + "." + plugin_info.output_extension))

for p in info.target_tool_path_runfiles:
tools.extend(p.files.to_list())

ctx.actions.run(
mnemonic = "EcsactCodegen",
outputs = [outdir] + outputs,
Expand Down

0 comments on commit e729a01

Please sign in to comment.