From c93a2e593373ffd6e707ba34b7772a6bc8b68bed Mon Sep 17 00:00:00 2001 From: Ezekiel Warren Date: Mon, 13 May 2024 17:14:36 -0700 Subject: [PATCH 1/2] feat: ecsact_binary provides CcInfo --- ecsact/private/ecsact_binary.bzl | 46 +++++++++++++++++++++++--- ecsact/private/ecsact_build_recipe.bzl | 36 +++++++++++++++++++- 2 files changed, 77 insertions(+), 5 deletions(-) diff --git a/ecsact/private/ecsact_binary.bzl b/ecsact/private/ecsact_binary.bzl index cee9256..3920e84 100644 --- a/ecsact/private/ecsact_binary.bzl +++ b/ecsact/private/ecsact_binary.bzl @@ -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)) @@ -27,10 +33,11 @@ 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 = ctx.actions.declare_file("{}{}".format(ctx.attr.name, ctx.attr.interface_library_extension)) + outputs = [runtime_output_file, interface_output_file] tools = [] + ecsact_toolchain.tool_files args = ctx.actions.args() @@ -89,7 +96,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), ), @@ -111,9 +140,15 @@ _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"], @@ -121,12 +156,15 @@ _ecsact_binary = rule( 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", + }), **kwargs ) diff --git a/ecsact/private/ecsact_build_recipe.bzl b/ecsact/private/ecsact_build_recipe.bzl index 221ad0a..bbf54b9 100644 --- a/ecsact/private/ecsact_build_recipe.bzl +++ b/ecsact/private/ecsact_build_recipe.bzl @@ -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 @@ -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({ @@ -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], ), From 5f627eb43bb20ef2f0839a2011ca7e3fb541a9c6 Mon Sep 17 00:00:00 2001 From: Ezekiel Warren Date: Wed, 15 May 2024 08:31:22 -0700 Subject: [PATCH 2/2] fix: make sure we're handling other os --- MODULE.bazel.lock | 72 ++++++++++++++++---------------- ecsact/private/ecsact_binary.bzl | 12 +++++- 2 files changed, 46 insertions(+), 38 deletions(-) diff --git a/MODULE.bazel.lock b/MODULE.bazel.lock index 1382c83..00fcdec 100644 --- a/MODULE.bazel.lock +++ b/MODULE.bazel.lock @@ -1,6 +1,6 @@ { "lockFileVersion": 6, - "moduleFileHash": "e4c62b628b93180e84574aadf29d8a872611062322d9f7917a161b712c86828f", + "moduleFileHash": "8e1ceeb7845f8fa4102c3be59d6e74bee8ade8f82b40d986818e38dbd50b5e47", "flags": { "cmdRegistries": [ "https://raw.githubusercontent.com/ecsact-dev/bazel_registry/main", @@ -20,7 +20,7 @@ "moduleDepGraph": { "": { "name": "rules_ecsact", - "version": "0.5.0", + "version": "0.5.1", "key": "", "repoName": "rules_ecsact", "executionPlatformsToRegister": [], @@ -30,7 +30,7 @@ "rules_cc": "rules_cc@0.0.9", "bazel_skylib": "bazel_skylib@1.5.0", "platforms": "platforms@0.0.9", - "ecsact_cli": "ecsact_cli@0.3.2", + "ecsact_cli": "ecsact_cli@0.3.4", "aspect_bazel_lib": "aspect_bazel_lib@1.32.1", "bazel_tools": "bazel_tools@_", "local_config_platform": "local_config_platform@_" @@ -160,10 +160,10 @@ } } }, - "ecsact_cli@0.3.2": { + "ecsact_cli@0.3.4": { "name": "ecsact_cli", - "version": "0.3.2", - "key": "ecsact_cli@0.3.2", + "version": "0.3.4", + "key": "ecsact_cli@0.3.4", "repoName": "ecsact_cli", "executionPlatformsToRegister": [], "toolchainsToRegister": [], @@ -174,10 +174,10 @@ "platforms": "platforms@0.0.9", "rules_pkg": "rules_pkg@0.10.1", "bazel_skylib": "bazel_skylib@1.5.0", - "ecsact_parse": "ecsact_parse@0.3.3", - "ecsact_runtime": "ecsact_runtime@0.5.4", - "ecsact_interpret": "ecsact_interpret@0.5.3", - "ecsact_codegen": "ecsact_codegen@0.1.3", + "ecsact_parse": "ecsact_parse@0.4.0", + "ecsact_runtime": "ecsact_runtime@0.6.1", + "ecsact_interpret": "ecsact_interpret@0.6.0", + "ecsact_codegen": "ecsact_codegen@0.2.0", "boost.dll": "boost.dll@1.83.0.bzl.2", "magic_enum": "magic_enum@0.9.3", "curl": "curl@8.4.0", @@ -194,9 +194,9 @@ "ruleClassName": "http_archive", "attributes": { "urls": [ - "https://github.com/ecsact-dev/ecsact_cli/releases/download/0.3.2/ecsact_cli-0.3.2.tar.gz" + "https://github.com/ecsact-dev/ecsact_cli/releases/download/0.3.4/ecsact_cli-0.3.4.tar.gz" ], - "integrity": "sha256-s63LdsugqaX0lHO8g65s6aczHyKLe4TswoTTNg0OBBc=", + "integrity": "sha256-o/uv0o+zrxPoSgTo88232q3TWfM4raPxnB95qlIQWaw=", "strip_prefix": "", "remote_patches": {}, "remote_patch_strip": 0 @@ -512,10 +512,10 @@ } } }, - "ecsact_parse@0.3.3": { + "ecsact_parse@0.4.0": { "name": "ecsact_parse", - "version": "0.3.3", - "key": "ecsact_parse@0.3.3", + "version": "0.4.0", + "key": "ecsact_parse@0.4.0", "repoName": "ecsact_parse", "executionPlatformsToRegister": [], "toolchainsToRegister": [], @@ -523,7 +523,7 @@ "deps": { "rules_cc": "rules_cc@0.0.9", "bazel_skylib": "bazel_skylib@1.5.0", - "ecsact_runtime": "ecsact_runtime@0.5.4", + "ecsact_runtime": "ecsact_runtime@0.6.1", "lexy": "lexy@2022.05.1", "magic_enum": "magic_enum@0.9.3", "bazel_tools": "bazel_tools@_", @@ -534,19 +534,19 @@ "ruleClassName": "http_archive", "attributes": { "urls": [ - "https://github.com/ecsact-dev/ecsact_parse/releases/download/0.3.3/ecsact_parse-0.3.3.tar.gz" + "https://github.com/ecsact-dev/ecsact_parse/releases/download/0.4.0/ecsact_parse-0.4.0.tar.gz" ], - "integrity": "sha256-bdwpSsqxxD9qsH9+q+tKOAVYNZvpI+MgJFkNJIBpHHk=", + "integrity": "sha256-GFydZ7s0mkjp5bgL5/GdYvc8hqHk1Eg6QDPk05c7Zys=", "strip_prefix": "", "remote_patches": {}, "remote_patch_strip": 0 } } }, - "ecsact_runtime@0.5.4": { + "ecsact_runtime@0.6.1": { "name": "ecsact_runtime", - "version": "0.5.4", - "key": "ecsact_runtime@0.5.4", + "version": "0.6.1", + "key": "ecsact_runtime@0.6.1", "repoName": "ecsact_runtime", "executionPlatformsToRegister": [], "toolchainsToRegister": [], @@ -567,19 +567,19 @@ "ruleClassName": "http_archive", "attributes": { "urls": [ - "https://github.com/ecsact-dev/ecsact_runtime/releases/download/0.5.4/ecsact_runtime-0.5.4.tar.gz" + "https://github.com/ecsact-dev/ecsact_runtime/releases/download/0.6.1/ecsact_runtime-0.6.1.tar.gz" ], - "integrity": "sha256-EaWwFxIIoI10+H8WDe7XwJ3M29jaxDwqLN9HK+hccys=", + "integrity": "sha256-BhaaAYDm6w9LvqOJjYruvWivfR1mvHVoaxSGpc2H9zQ=", "strip_prefix": "", "remote_patches": {}, "remote_patch_strip": 0 } } }, - "ecsact_interpret@0.5.3": { + "ecsact_interpret@0.6.0": { "name": "ecsact_interpret", - "version": "0.5.3", - "key": "ecsact_interpret@0.5.3", + "version": "0.6.0", + "key": "ecsact_interpret@0.6.0", "repoName": "ecsact_interpret", "executionPlatformsToRegister": [], "toolchainsToRegister": [], @@ -588,8 +588,8 @@ "rules_cc": "rules_cc@0.0.9", "bazel_skylib": "bazel_skylib@1.5.0", "magic_enum": "magic_enum@0.9.3", - "ecsact_runtime": "ecsact_runtime@0.5.4", - "ecsact_parse": "ecsact_parse@0.3.3", + "ecsact_runtime": "ecsact_runtime@0.6.1", + "ecsact_parse": "ecsact_parse@0.4.0", "bazel_tools": "bazel_tools@_", "local_config_platform": "local_config_platform@_" }, @@ -598,19 +598,19 @@ "ruleClassName": "http_archive", "attributes": { "urls": [ - "https://github.com/ecsact-dev/ecsact_interpret/releases/download/0.5.3/ecsact_interpret-0.5.3.tar.gz" + "https://github.com/ecsact-dev/ecsact_interpret/releases/download/0.6.0/ecsact_interpret-0.6.0.tar.gz" ], - "integrity": "sha256-A7VwvcO+ggYGfjU6DR1Kq9aHMdCJIsPEbxA/rew4c1M=", + "integrity": "sha256-YDtn32eLL0JDIGBSelHroEiIBaS2tBjnXsVyn3Cuf18=", "strip_prefix": "", "remote_patches": {}, "remote_patch_strip": 0 } } }, - "ecsact_codegen@0.1.3": { + "ecsact_codegen@0.2.0": { "name": "ecsact_codegen", - "version": "0.1.3", - "key": "ecsact_codegen@0.1.3", + "version": "0.2.0", + "key": "ecsact_codegen@0.2.0", "repoName": "ecsact_codegen", "executionPlatformsToRegister": [], "toolchainsToRegister": [], @@ -619,7 +619,7 @@ "rules_cc": "rules_cc@0.0.9", "bazel_skylib": "bazel_skylib@1.5.0", "boost.dll": "boost.dll@1.83.0.bzl.2", - "ecsact_runtime": "ecsact_runtime@0.5.4", + "ecsact_runtime": "ecsact_runtime@0.6.1", "bazel_tools": "bazel_tools@_", "local_config_platform": "local_config_platform@_" }, @@ -628,9 +628,9 @@ "ruleClassName": "http_archive", "attributes": { "urls": [ - "https://github.com/ecsact-dev/ecsact_codegen/releases/download/0.1.3/ecsact_codegen-0.1.3.tar.gz" + "https://github.com/ecsact-dev/ecsact_codegen/releases/download/0.2.0/ecsact_codegen-0.2.0.tar.gz" ], - "integrity": "sha256-TvzdtIS1RdZtc6SVkp4m15zUjy6dWrvkdjnZLiBI9gs=", + "integrity": "sha256-Z1uNm6pVqJGwXQQ9YwD82rDyfJamngsGyqImkKZE/c4=", "strip_prefix": "", "remote_patches": {}, "remote_patch_strip": 0 diff --git a/ecsact/private/ecsact_binary.bzl b/ecsact/private/ecsact_binary.bzl index 3920e84..7be4b16 100644 --- a/ecsact/private/ecsact_binary.bzl +++ b/ecsact/private/ecsact_binary.bzl @@ -36,9 +36,13 @@ def _ecsact_binary_impl(ctx): preferred_output_extension = ctx.attr.shared_library_extension runtime_output_file = ctx.actions.declare_file("{}{}".format(ctx.attr.name, preferred_output_extension)) - interface_output_file = ctx.actions.declare_file("{}{}".format(ctx.attr.name, ctx.attr.interface_library_extension)) - outputs = [runtime_output_file, interface_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") @@ -165,6 +169,10 @@ def ecsact_binary(**kwargs): }), interface_library_extension = select({ "@platforms//os:windows": ".lib", + "@platforms//os:linux": "", + "@platforms//os:macos": "", + "@platforms//os:wasi": "", + "@platforms//os:none": "", # for non-wasi }), **kwargs )