From 906b882140a366b8b227a1aedc4dcd2ad5c4c3b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20P=2E=20Purkh=C3=BAs?= Date: Wed, 8 Nov 2023 15:52:46 +0000 Subject: [PATCH] Various fixes (#397) * Make output dirs unique per label * Expose the nupkg file via the NugetInfo provider * Add more of the .Net sdk * Take all runfiles from the data attribute as well --- dotnet/private/common.bzl | 5 +++++ dotnet/private/providers.bzl | 1 + dotnet/private/rules/common/binary.bzl | 4 ++-- .../private/rules/csharp/actions/csharp_assembly.bzl | 12 +++++++----- .../private/rules/fsharp/actions/fsharp_assembly.bzl | 12 +++++++----- dotnet/private/rules/nuget/imports.bzl | 11 ++++++++++- dotnet/private/rules/nuget/nuget_archive.bzl | 12 +++++++++++- dotnet/private/rules/nuget/template.BUILD | 1 + dotnet/private/tests/xml_docs/csharp/BUILD.bazel | 2 +- dotnet/private/tests/xml_docs/fsharp/BUILD.bazel | 2 +- dotnet/repositories.bzl | 4 +--- 11 files changed, 47 insertions(+), 19 deletions(-) diff --git a/dotnet/private/common.bzl b/dotnet/private/common.bzl index 3b6b498c..4d2c68e7 100644 --- a/dotnet/private/common.bzl +++ b/dotnet/private/common.bzl @@ -325,6 +325,11 @@ def collect_transitive_runfiles(ctx, assembly_runtime_info, deps): for dep in deps: transitive_runfiles.append(dep[DefaultInfo].default_runfiles) + for d in ctx.attr.data: + if not DefaultInfo in d: + continue + runfiles = runfiles.merge(d[DefaultInfo].default_runfiles) + return runfiles.merge_all(transitive_runfiles) def get_framework_version_info(tfm): diff --git a/dotnet/private/providers.bzl b/dotnet/private/providers.bzl index 352a7930..5e698b50 100644 --- a/dotnet/private/providers.bzl +++ b/dotnet/private/providers.bzl @@ -52,6 +52,7 @@ NuGetInfo = provider( "targeting_pack_overrides": "map[string, string]: Targeting packs like e.g. Microsoft.NETCore.App.Ref have a PackageOverride.txt that includes a list of NuGet packages that should be omitted in a compiliation because they are included in the targeting pack", "framework_list": "map[string, string]: Targeting packs like e.g. Microsoft.NETCore.App.Ref have a FrameworkList.xml that includes a list of the DLLs in the targeting pack. This is used for selecting the correct DLL versions during compilation and runtime.", "sha512": "string: the SHA512 SRI string for the package", + "nupkg": "File: the underlying `.nupkg` file which provides this package", }, ) diff --git a/dotnet/private/rules/common/binary.bzl b/dotnet/private/rules/common/binary.bzl index 506c240d..da7b8cf9 100644 --- a/dotnet/private/rules/common/binary.bzl +++ b/dotnet/private/rules/common/binary.bzl @@ -101,7 +101,7 @@ def build_binary(ctx, compile_action): if is_core_framework(tfm): # Create the runtimeconfig.json for the binary - runtimeconfig = ctx.actions.declare_file("bazelout/%s/%s.runtimeconfig.json" % (tfm, ctx.attr.out or ctx.attr.name)) + runtimeconfig = ctx.actions.declare_file("%s/%s/%s.runtimeconfig.json" % (ctx.label.name, tfm, ctx.attr.out or ctx.attr.name)) runtimeconfig_struct = generate_runtimeconfig( target_framework = tfm, project_sdk = ctx.attr.project_sdk, @@ -125,7 +125,7 @@ def build_binary(ctx, compile_action): content = json.encode_indent(runtimeconfig_struct), ) - depsjson = ctx.actions.declare_file("bazelout/%s/%s.deps.json" % (tfm, ctx.attr.out or ctx.attr.name)) + depsjson = ctx.actions.declare_file("%s/%s/%s.deps.json" % (ctx.label.name, tfm, ctx.attr.out or ctx.attr.name)) depsjson_struct = generate_depsjson( ctx, target_framework = tfm, diff --git a/dotnet/private/rules/csharp/actions/csharp_assembly.bzl b/dotnet/private/rules/csharp/actions/csharp_assembly.bzl index 231407b0..724bd684 100644 --- a/dotnet/private/rules/csharp/actions/csharp_assembly.bzl +++ b/dotnet/private/rules/csharp/actions/csharp_assembly.bzl @@ -17,7 +17,7 @@ load( "DotnetAssemblyRuntimeInfo", ) -def _write_internals_visible_to_csharp(actions, name, others): +def _write_internals_visible_to_csharp(actions, label_name, dll_name, others): """Write a .cs file containing InternalsVisibleTo attributes. Letting Bazel see which assemblies we are going to have InternalsVisibleTo @@ -25,7 +25,8 @@ def _write_internals_visible_to_csharp(actions, name, others): Args: actions: An actions module, usually from ctx.actions. - name: The assembly name. + label_name: The label name. + dll_name: The assembly name. others: The names of other assemblies. Returns: @@ -43,7 +44,7 @@ def _write_internals_visible_to_csharp(actions, name, others): format_each = "[assembly: System.Runtime.CompilerServices.InternalsVisibleTo(\"%s\")]", ) - output = actions.declare_file("bazelout/%s/internalsvisibleto.cs" % name) + output = actions.declare_file("%s/%s/internalsvisibleto.cs" % (label_name, dll_name)) actions.write(output, attrs) @@ -139,7 +140,7 @@ def AssemblyAction( defines = framework_preprocessor_symbols(target_framework) + defines - out_dir = "bazelout/" + target_framework + out_dir = target_name + "/" + target_framework out_ext = "dll" out_dll = actions.declare_file("%s/%s.%s" % (out_dir, assembly_name, out_ext)) @@ -188,7 +189,8 @@ def AssemblyAction( internals_visible_to_cs = _write_internals_visible_to_csharp( actions, - name = assembly_name, + label_name = target_name, + dll_name = assembly_name, others = internals_visible_to, ) diff --git a/dotnet/private/rules/fsharp/actions/fsharp_assembly.bzl b/dotnet/private/rules/fsharp/actions/fsharp_assembly.bzl index 97540cdc..15c97c77 100644 --- a/dotnet/private/rules/fsharp/actions/fsharp_assembly.bzl +++ b/dotnet/private/rules/fsharp/actions/fsharp_assembly.bzl @@ -29,7 +29,7 @@ def _format_targetprofile(tfm): return "--targetprofile:mscorlib" -def _write_internals_visible_to_fsharp(actions, name, others): +def _write_internals_visible_to_fsharp(actions, label_name, dll_name, others): """Write a .fs file containing InternalsVisibleTo attributes. Letting Bazel see which assemblies we are going to have InternalsVisibleTo @@ -37,7 +37,8 @@ def _write_internals_visible_to_fsharp(actions, name, others): Args: actions: An actions module, usually from ctx.actions. - name: The assembly name. + label_name: The label name + dll_name: The assembly name. others: The names of other assemblies. Returns: @@ -58,7 +59,7 @@ do() """ % other - output = actions.declare_file("bazelout/%s/internalsvisibleto.fs" % name) + output = actions.declare_file("%s/%s/internalsvisibleto.fs" % (label_name, dll_name)) actions.write(output, content) return output @@ -150,7 +151,7 @@ def AssemblyAction( ) defines = framework_preprocessor_symbols(target_framework) + defines - out_dir = "bazelout/" + target_framework + out_dir = target_name + "/" + target_framework out_ext = "dll" out_dll = actions.declare_file("%s/%s.%s" % (out_dir, assembly_name, out_ext)) out_iref = None @@ -193,7 +194,8 @@ def AssemblyAction( internals_visible_to_fs = _write_internals_visible_to_fsharp( actions, - name = assembly_name, + label_name = target_name, + dll_name = assembly_name, others = internals_visible_to, ) _compile( diff --git a/dotnet/private/rules/nuget/imports.bzl b/dotnet/private/rules/nuget/imports.bzl index a43b3d0a..48ee788e 100644 --- a/dotnet/private/rules/nuget/imports.bzl +++ b/dotnet/private/rules/nuget/imports.bzl @@ -30,6 +30,7 @@ def _import_library(ctx): targeting_pack_overrides = ctx.attr.targeting_pack_overrides, framework_list = ctx.attr.framework_list, sha512 = ctx.attr.sha512, + nupkg = ctx.file.nupkg, ) dotnet_assembly_compile_info = DotnetAssemblyCompileInfo( @@ -120,6 +121,10 @@ import_library = rule( "sha512": attr.string( doc = "The SHA512 sum of the NuGet package", ), + "nupkg": attr.label( + doc = "The `.nupkg` file providing this import", + allow_single_file = True, + ), }, toolchains = [ "@rules_dotnet//dotnet:toolchain_type", @@ -149,7 +154,7 @@ def _import_dll(ctx): pdbs = [], xml_docs = [], native = [], - data = [], + data = ctx.files.data, deps = depset([]), nuget_info = None, direct_deps_depsjson_fragment = {}, @@ -174,6 +179,10 @@ import_dll = rule( "version": attr.string( doc = "The version of the library", ), + "data": attr.label_list( + doc = "Other files that this DLL depends on at runtime", + allow_files = True, + ), }, executable = False, ) diff --git a/dotnet/private/rules/nuget/nuget_archive.bzl b/dotnet/private/rules/nuget/nuget_archive.bzl index ef6868e3..92449160 100644 --- a/dotnet/private/rules/nuget/nuget_archive.bzl +++ b/dotnet/private/rules/nuget/nuget_archive.bzl @@ -321,7 +321,16 @@ def _nuget_archive_impl(ctx): # Then get the auth dict for the package base urls auth = _get_auth_dict(ctx, ctx.attr.netrc, urls) - ctx.download_and_extract(urls, type = "zip", integrity = ctx.attr.sha512, auth = auth) + + # We download it as .zip because ctx.exract reads the file extension to determine the archive type + file_name = "%s.zip" % ctx.name + nupkg_name = "%s.%s.nupkg" % (ctx.attr.id, ctx.attr.version) + names = [nupkg_name] + + ctx.download(urls, output = file_name, integrity = ctx.attr.sha512, auth = auth) + ctx.extract(archive = file_name) + for name in names: + ctx.symlink(file_name, name) files = sorted(_read_dir(ctx, ".").replace(str(ctx.path(".")) + "/", "").splitlines()) @@ -447,6 +456,7 @@ load("@rules_dotnet//dotnet/private/rules/nuget:nuget_archive.bzl", "tfm_filegro "filegroup(name = \"data\", srcs = [])", _create_rid_native_select("native", native) or "filegroup(name = \"native\", srcs = [])", "filegroup(name = \"content_files\", srcs = [%s])" % ",".join(["\n \"%s\"" % a for a in groups.get("contentFiles")["any"]]), + "exports_files([\"%s\"])" % nupkg_name, ])) nuget_archive = repository_rule( diff --git a/dotnet/private/rules/nuget/template.BUILD b/dotnet/private/rules/nuget/template.BUILD index 47fd3629..83924488 100644 --- a/dotnet/private/rules/nuget/template.BUILD +++ b/dotnet/private/rules/nuget/template.BUILD @@ -10,6 +10,7 @@ import_library( library_name = "{NAME}", libs = ["@{PREFIX}.{NAME_LOWER}.v{VERSION}//:libs"], native = ["@{PREFIX}.{NAME_LOWER}.v{VERSION}//:native"], + nupkg = "@{PREFIX}.{NAME_LOWER}.v{VERSION}//:{NAME_LOWER}.{VERSION}.nupkg", refs = ["@{PREFIX}.{NAME_LOWER}.v{VERSION}//:refs"], sha512 = "{SHA_512}", targeting_pack_overrides = {TARGETING_PACK_OVERRIDES}, diff --git a/dotnet/private/tests/xml_docs/csharp/BUILD.bazel b/dotnet/private/tests/xml_docs/csharp/BUILD.bazel index b2ef8e75..fa35d036 100644 --- a/dotnet/private/tests/xml_docs/csharp/BUILD.bazel +++ b/dotnet/private/tests/xml_docs/csharp/BUILD.bazel @@ -20,7 +20,7 @@ csharp_library( select_file( name = "docs_xml", srcs = ":lib", - subpath = "bazelout/net6.0/lib.xml", + subpath = "lib/net6.0/lib.xml", ) diff_test( diff --git a/dotnet/private/tests/xml_docs/fsharp/BUILD.bazel b/dotnet/private/tests/xml_docs/fsharp/BUILD.bazel index af5ae7cf..c19b8f61 100644 --- a/dotnet/private/tests/xml_docs/fsharp/BUILD.bazel +++ b/dotnet/private/tests/xml_docs/fsharp/BUILD.bazel @@ -21,7 +21,7 @@ fsharp_library( select_file( name = "docs_xml", srcs = ":lib", - subpath = "bazelout/net6.0/lib.xml", + subpath = "lib/net6.0/lib.xml", ) diff_test( diff --git a/dotnet/repositories.bzl b/dotnet/repositories.bzl index 988559ed..3f4db482 100644 --- a/dotnet/repositories.bzl +++ b/dotnet/repositories.bzl @@ -83,9 +83,7 @@ filegroup( }}), data = glob([ "host/**/*", - "sdk/**/*.dll", - "sdk/**/dotnet.runtimeconfig.json", - "sdk/**/dotnet.deps.json", + "sdk/**/*", "shared/Microsoft.NETCore.App/**/*", ]), visibility = ["//visibility:public"],