Skip to content

Commit

Permalink
Various fixes (#397)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
purkhusid authored Nov 8, 2023
1 parent a957ae8 commit 906b882
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 19 deletions.
5 changes: 5 additions & 0 deletions dotnet/private/common.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
1 change: 1 addition & 0 deletions dotnet/private/providers.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
)

Expand Down
4 changes: 2 additions & 2 deletions dotnet/private/rules/common/binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
12 changes: 7 additions & 5 deletions dotnet/private/rules/csharp/actions/csharp_assembly.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@ 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
allows for more robust caching of compiles.
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:
Expand All @@ -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)

Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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,
)

Expand Down
12 changes: 7 additions & 5 deletions dotnet/private/rules/fsharp/actions/fsharp_assembly.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,16 @@ 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
allows for more robust caching of compiles.
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:
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down
11 changes: 10 additions & 1 deletion dotnet/private/rules/nuget/imports.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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 = {},
Expand All @@ -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,
)
12 changes: 11 additions & 1 deletion dotnet/private/rules/nuget/nuget_archive.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down Expand Up @@ -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(
Expand Down
1 change: 1 addition & 0 deletions dotnet/private/rules/nuget/template.BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down
2 changes: 1 addition & 1 deletion dotnet/private/tests/xml_docs/csharp/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion dotnet/private/tests/xml_docs/fsharp/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
4 changes: 1 addition & 3 deletions dotnet/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down

0 comments on commit 906b882

Please sign in to comment.