From e3a761a8f4f2e49976c123fbc583a31fb634f7ff Mon Sep 17 00:00:00 2001 From: njlr Date: Fri, 4 Oct 2024 19:11:03 +0100 Subject: [PATCH 1/2] Expose compilation information --- dotnet/private/providers.bzl | 21 ++++++++++ dotnet/private/rules/common/binary.bzl | 8 +++- dotnet/private/rules/common/compile_info.bzl | 42 +++++++++++++++++++ dotnet/private/rules/common/library.bzl | 7 ++++ .../rules/fsharp/actions/fsharp_assembly.bzl | 2 +- 5 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 dotnet/private/rules/common/compile_info.bzl diff --git a/dotnet/private/providers.bzl b/dotnet/private/providers.bzl index 929eb529..497c8478 100644 --- a/dotnet/private/providers.bzl +++ b/dotnet/private/providers.bzl @@ -89,3 +89,24 @@ DotnetApphostPackInfo = provider( "apphost": "File: The apphost file", }, ) + +DotnetCompileInfo = provider( + doc = "Information about how a .Net target is to be compiled", + fields = { + "label": "Label: The label of the target", + "srcs": "list[File]: sources to be compiled", + "deps": "list[DotnetCompileDepVariantInfo]: The direct dependencies of the target", + "transitive_deps": "depset[DotnetCompileDepVariantInfo]: The transitive dependencies of the target", + }, +) + +DotnetCompileDepVariantInfo = provider( + doc = "A wrapper provider for a compilation dependency. The dependency can be a project " + + "dependency, in which case the `dotnet_compile_info` will be populated" + + "or a NuGet dependency, in which case `dotnet_assembly_compile_info` will be populated.", + fields = { + "label": "Label: The label of the dependency", + "dotnet_compile_info": "DotnetCompileInfo: The DotnetCompileInfo of a dependency", + "dotnet_assembly_compile_info": "DotnetAssemblyCompileInfo: The NuGet info of a dependency", + }, +) diff --git a/dotnet/private/rules/common/binary.bzl b/dotnet/private/rules/common/binary.bzl index 2c110e21..f2f3c976 100644 --- a/dotnet/private/rules/common/binary.bzl +++ b/dotnet/private/rules/common/binary.bzl @@ -13,6 +13,10 @@ load( "to_rlocation_path", ) load("//dotnet/private:providers.bzl", "DotnetApphostPackInfo", "DotnetBinaryInfo", "DotnetRuntimePackInfo") +load( + "//dotnet/private/rules/common:compile_info.bzl", + "gather_compile_info" +) def _create_launcher(ctx, runfiles, executable): runtime = ctx.toolchains["//dotnet:toolchain_type"].runtime @@ -136,4 +140,6 @@ def build_binary(ctx, compile_action): runtime_pack_info = ctx.attr._runtime_pack[0][DotnetRuntimePackInfo], ) - return [default_info, dotnet_binary_info, compile_provider, runtime_provider] + compile_info_provider = gather_compile_info(ctx) + + return [default_info, dotnet_binary_info, compile_provider, runtime_provider, compile_info_provider] diff --git a/dotnet/private/rules/common/compile_info.bzl b/dotnet/private/rules/common/compile_info.bzl new file mode 100644 index 00000000..5defe3ad --- /dev/null +++ b/dotnet/private/rules/common/compile_info.bzl @@ -0,0 +1,42 @@ +"Utility function for collecting compilation information from contexts" + +load("//dotnet/private:providers.bzl", "NuGetInfo", "DotnetCompileInfo", "DotnetCompileDepVariantInfo", "DotnetAssemblyCompileInfo") + + +def gather_compile_info(ctx): + """Collects compilation information from a context + + Args: + ctx: Bazel build ctx. + Returns: + A collection of the source-files, dependencies and transitive dependencies + """ + direct_deps = [] + transitive_deps = [] + + for dep in ctx.attr.deps: + if DotnetCompileInfo in dep: + variant = DotnetCompileDepVariantInfo( + label = dep.label, + dotnet_compile_info = dep[DotnetCompileInfo], + dotnet_assembly_compile_info = None, + ) + + direct_deps.append(variant) + transitive_deps.append(depset(dep[DotnetCompileInfo].deps, transitive = [ dep[DotnetCompileInfo].transitive_deps ])) + + if NuGetInfo in dep and DotnetAssemblyCompileInfo in dep: + variant = DotnetCompileDepVariantInfo( + label = dep.label, + dotnet_compile_info = None, + dotnet_assembly_compile_info = dep[DotnetAssemblyCompileInfo], + ) + + direct_deps.append(variant) + + return DotnetCompileInfo( + label = ctx.label, + srcs = ctx.files.srcs, + deps = direct_deps, + transitive_deps = depset([], transitive = transitive_deps), + ) diff --git a/dotnet/private/rules/common/library.bzl b/dotnet/private/rules/common/library.bzl index 0e577c41..3a3b73e5 100644 --- a/dotnet/private/rules/common/library.bzl +++ b/dotnet/private/rules/common/library.bzl @@ -2,6 +2,10 @@ load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo") load("//dotnet/private:common.bzl", "collect_transitive_runfiles") +load( + "//dotnet/private/rules/common:compile_info.bzl", + "gather_compile_info" +) def build_library(ctx, compile_action): """Builds a .Net library from a compilation action @@ -21,9 +25,12 @@ def build_library(ctx, compile_action): (compile_provider, runtime_provider) = compile_action(ctx, tfm) + compile_info_provider = gather_compile_info(ctx) + return [ compile_provider, runtime_provider, + compile_info_provider, DefaultInfo( files = depset(runtime_provider.libs + runtime_provider.xml_docs), default_runfiles = collect_transitive_runfiles( diff --git a/dotnet/private/rules/fsharp/actions/fsharp_assembly.bzl b/dotnet/private/rules/fsharp/actions/fsharp_assembly.bzl index 8e7c35f8..f4ceddb4 100644 --- a/dotnet/private/rules/fsharp/actions/fsharp_assembly.bzl +++ b/dotnet/private/rules/fsharp/actions/fsharp_assembly.bzl @@ -118,7 +118,7 @@ def AssemblyAction( keyfile: Specifies a strong name key file of the assembly. langversion: Specify language version: Default, ISO-1, ISO-2, 3, 4, 5, 6, 7, 7.1, 7.2, 7.3, or Latest resources: The list of resouces to be embedded in the assembly. - srcs: The list of source (.cs) files that are processed to create the assembly. + srcs: The list of source (.fs) files that are processed to create the assembly. data: List of files that are a direct runtime dependency appsetting_files: List of appsettings files to include in the output. compile_data: List of files that are a direct compile time dependency From 8807677e2ac672dc239d906e6427d37d773c66e2 Mon Sep 17 00:00:00 2001 From: njlr Date: Fri, 4 Oct 2024 19:58:11 +0100 Subject: [PATCH 2/2] Gazelle --- dotnet/private/rules/common/BUILD.bazel | 7 +++++++ dotnet/private/tests/appsettings/BUILD.bazel | 12 ++++++++++++ .../private/tests/publish/cross_publish/BUILD.bazel | 8 ++++++++ 3 files changed, 27 insertions(+) diff --git a/dotnet/private/rules/common/BUILD.bazel b/dotnet/private/rules/common/BUILD.bazel index 21149f2b..f36c2190 100644 --- a/dotnet/private/rules/common/BUILD.bazel +++ b/dotnet/private/rules/common/BUILD.bazel @@ -32,3 +32,10 @@ bzl_library( "@bazel_skylib//lib:dicts", ], ) + +bzl_library( + name = "compile_info", + srcs = ["compile_info.bzl"], + visibility = ["//dotnet:__subpackages__"], + deps = ["//dotnet/private:providers"], +) diff --git a/dotnet/private/tests/appsettings/BUILD.bazel b/dotnet/private/tests/appsettings/BUILD.bazel index e2654556..8fe466b0 100644 --- a/dotnet/private/tests/appsettings/BUILD.bazel +++ b/dotnet/private/tests/appsettings/BUILD.bazel @@ -1,3 +1,15 @@ +load("@bazel_skylib//:bzl_library.bzl", "bzl_library") load(":tests.bzl", "appsettings_test_suite") appsettings_test_suite(name = "appsettings_test_suite") + +bzl_library( + name = "tests", + srcs = ["tests.bzl"], + visibility = ["//dotnet:__subpackages__"], + deps = [ + "//dotnet:defs", + "//dotnet/private/tests:utils", + "@rules_testing//lib:analysis_test", + ], +) diff --git a/dotnet/private/tests/publish/cross_publish/BUILD.bazel b/dotnet/private/tests/publish/cross_publish/BUILD.bazel index 934e5f3c..83287169 100644 --- a/dotnet/private/tests/publish/cross_publish/BUILD.bazel +++ b/dotnet/private/tests/publish/cross_publish/BUILD.bazel @@ -1,3 +1,11 @@ +load("@bazel_skylib//:bzl_library.bzl", "bzl_library") load(":tests.bzl", "tests") tests() + +bzl_library( + name = "tests", + srcs = ["tests.bzl"], + visibility = ["//dotnet:__subpackages__"], + deps = ["//dotnet:defs"], +)