generated from bazel-contrib/rules-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ecsact_binary and ecsact_library (#29)
- Loading branch information
Showing
7 changed files
with
210 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
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, | ||
) | ||
|
||
variables = cc_common.create_link_variables( | ||
cc_toolchain = cc_toolchain, | ||
feature_configuration = feature_configuration, | ||
) | ||
|
||
env = cc_common.get_environment_variables( | ||
feature_configuration = feature_configuration, | ||
action_name = ACTION_NAMES.cpp_link_dynamic_library, | ||
variables = variables, | ||
) | ||
|
||
ecsact_toolchain = ctx.toolchains["//ecsact:toolchain_type"].ecsact_info | ||
|
||
# TODO(zaucy): derive runtime library extension based on ctx and cc_toolchain | ||
runtime_output_file = ctx.actions.declare_file("{}.dll".format(ctx.attr.name)) | ||
outputs = [runtime_output_file] | ||
tools = [] + ecsact_toolchain.tool_files | ||
|
||
args = ctx.actions.args() | ||
args.add("build") | ||
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", temp_dir.path) | ||
args.add("-f", "text") | ||
|
||
# TODO(zaucy): detect shared library extension | ||
preferred_output_extension = ".dll" | ||
|
||
compiler_config = { | ||
"compiler_type": "auto", | ||
"compiler_path": cc_toolchain.compiler_executable, | ||
"compiler_version": "bazel c++ toolchain", | ||
"install_path": "", | ||
"std_inc_paths": cc_toolchain.built_in_include_directories, | ||
"std_lib_paths": [], | ||
"preferred_output_extension": preferred_output_extension, | ||
"allowed_output_extensions": [preferred_output_extension], | ||
} | ||
|
||
compiler_config_file = ctx.actions.declare_file("{}.compiler_config.json".format(ctx.attr.name)) | ||
|
||
ctx.actions.write(compiler_config_file, json.encode(compiler_config)) | ||
|
||
args.add("--compiler_config", compiler_config_file) | ||
|
||
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", | ||
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 [ | ||
DefaultInfo( | ||
files = depset(outputs), | ||
), | ||
] | ||
|
||
ecsact_binary = rule( | ||
implementation = _ecsact_binary, | ||
attrs = { | ||
"srcs": attr.label_list( | ||
allow_files = [".ecsact"], | ||
), | ||
"recipes": attr.label_list( | ||
allow_empty = False, | ||
mandatory = True, | ||
providers = [EcsactBuildRecipeInfo], | ||
), | ||
"_cc_toolchain": attr.label( | ||
default = Label( | ||
"@rules_cc//cc:current_cc_toolchain", | ||
), | ||
), | ||
}, | ||
toolchains = ["//ecsact:toolchain_type"] + use_cc_toolchain(), | ||
fragments = ["cpp"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
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 src in ctx.files.srcs: | ||
sources.append({ | ||
"path": src.path, | ||
"outdir": "src", | ||
"relative_to_cwd": True, | ||
}) | ||
recipe_data.append(src) | ||
|
||
for codegen_plugin in ctx.attr.codegen_plugins: | ||
info = codegen_plugin[EcsactCodegenPluginInfo] | ||
sources.append({ | ||
"codegen": [info.plugin], | ||
"outdir": ctx.attr.codegen_plugins[codegen_plugin], | ||
}) | ||
|
||
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, | ||
), | ||
"codegen_plugins": attr.label_keyed_string_dict( | ||
providers = [EcsactCodegenPluginInfo], | ||
), | ||
"imports": attr.string_list( | ||
), | ||
"exports": attr.string_list( | ||
mandatory = True, | ||
), | ||
}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
EcsactLibraryInfo = provider() | ||
|
||
def _ecsact_library(ctx): | ||
return [EcsactLibraryInfo()] | ||
|
||
ecsact_library = rule( | ||
implementation = _ecsact_library, | ||
attrs = { | ||
"srcs": attr.label_list( | ||
allow_files = [".ecsact"], | ||
), | ||
# "deps": attr.label_list( | ||
# allow_rules = [EcsactLibraryInfo], | ||
# ), | ||
}, | ||
toolchains = ["//ecsact:toolchain_type"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters