Skip to content

Commit 28ddbf5

Browse files
committed
Added csharp_binary rule
1 parent ebb2259 commit 28ddbf5

3 files changed

Lines changed: 94 additions & 17 deletions

File tree

prelude/csharp/csharp.bzl

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,22 @@
99
load(":csharp_providers.bzl", "DllDepTSet", "DllReference", "DotNetLibraryInfo", "generate_target_tset_children")
1010
load(":toolchain.bzl", "CSharpToolchainInfo")
1111

12-
def csharp_library_impl(ctx: AnalysisContext) -> list[Provider]:
12+
def _csharp_library_or_exe(ctx: AnalysisContext, library_or_exe_name: str, target_type: str) -> DotNetLibraryInfo:
1313
toolchain = ctx.attrs._csharp_toolchain[CSharpToolchainInfo]
1414

15-
# Automatically set the output dll_name to this target's name if the caller did not specify a
16-
# custom name.
17-
dll_name = "{}.dll".format(ctx.attrs.name) if not ctx.attrs.dll_name else ctx.attrs.dll_name
18-
19-
# Declare that this rule will produce a dll.
20-
library = ctx.actions.declare_output(dll_name, has_content_based_path = False)
15+
# Declare that this rule will produce a dll or exe.
16+
library_or_exe = ctx.actions.declare_output(library_or_exe_name, has_content_based_path = False)
2117

22-
# Create a command invoking a wrapper script that calls csc.exe to compile the .dll.
18+
# Create a command invoking a wrapper script that calls csc.exe to compile the .dll or the .exe.
2319
cmd = [toolchain.csc]
2420

2521
# Add caller specified compiler flags.
2622
cmd.append(ctx.attrs.compiler_flags)
2723

2824
# Set the output target as a .NET library.
29-
cmd.append("/target:library")
25+
cmd.append("/target:" + target_type)
3026
cmd.append(cmd_args(
31-
library.as_output(),
27+
library_or_exe.as_output(),
3228
format = "/out:{}",
3329
))
3430

@@ -63,13 +59,32 @@ def csharp_library_impl(ctx: AnalysisContext) -> list[Provider]:
6359
# Run the C# compiler to produce the output artifact.
6460
ctx.actions.run(cmd, category = "csharp_compile")
6561

62+
return DotNetLibraryInfo(
63+
name = library_or_exe_name,
64+
object = library_or_exe,
65+
dll_deps = ctx.actions.tset(DllDepTSet, value = DllReference(reference = library_or_exe), children = child_deps),
66+
)
67+
68+
def csharp_library_impl(ctx: AnalysisContext) -> list[Provider]:
69+
# Automatically set the output dll_name to this target's name if the caller did not specify a
70+
# custom name.
71+
dll_name = "{}.dll".format(ctx.attrs.name) if not ctx.attrs.dll_name else ctx.attrs.dll_name
72+
73+
dotNetLibraryInfo = _csharp_library_or_exe(ctx, dll_name, "library")
74+
6675
return [
67-
DefaultInfo(default_output = library),
68-
DotNetLibraryInfo(
69-
name = ctx.attrs.dll_name,
70-
object = library,
71-
dll_deps = ctx.actions.tset(DllDepTSet, value = DllReference(reference = library), children = child_deps),
72-
),
76+
DefaultInfo(default_output = dotNetLibraryInfo.object),
77+
dotNetLibraryInfo,
78+
]
79+
80+
def csharp_binary_impl(ctx: AnalysisContext) -> list[Provider]:
81+
exe_name = "{}.exe".format(ctx.attrs.name) if not ctx.attrs.exe_name else ctx.attrs.exe_name
82+
83+
dotNetLibraryInfo = _csharp_library_or_exe(ctx, exe_name, "exe")
84+
85+
return [
86+
DefaultInfo(default_output = dotNetLibraryInfo.object),
87+
RunInfo(args = cmd_args(dotNetLibraryInfo.object)),
7388
]
7489

7590
def prebuilt_dotnet_library_impl(ctx: AnalysisContext) -> list[Provider]:

prelude/decls/dotnet_rules.bzl

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,63 @@ csharp_library = prelude_rule(
6969
),
7070
)
7171

72+
csharp_binary = prelude_rule(
73+
name = "csharp_binary",
74+
docs = """
75+
A csharp\\_binary() rule builds a .Net library from the supplied set of C# source files
76+
and dependencies by invoking csc.
77+
""",
78+
examples = """
79+
For more examples, check out our [integration tests](https://github.com/facebook/buck/tree/dev/test/com/facebook/buck/rust/testdata/).
80+
81+
82+
```
83+
84+
csharp_binary(
85+
name = 'simple',
86+
exe_name = 'Cake.exe',
87+
framework_ver = 'net46',
88+
srcs = [
89+
'Hello.cs',
90+
],
91+
resources = {
92+
'greeting.txt': '//some:target',
93+
},
94+
deps=[
95+
':other',
96+
'System.dll',
97+
],
98+
)
99+
100+
prebuilt_dotnet_library(
101+
name = 'other',
102+
assembly = 'other-1.0.dll',
103+
)
104+
105+
```
106+
""",
107+
further = None,
108+
attrs = (
109+
# @unsorted-dict-items
110+
{
111+
"exe_name": attrs.string(default = "", doc = """
112+
The output name of the dll. This allows you to specify the name of
113+
the dll exactly. When this is not set, the dll will be named after
114+
the short name of the target.
115+
"""),
116+
} |
117+
dotnet_common.srcs_arg() |
118+
dotnet_common.resources_arg() |
119+
dotnet_common.framework_ver_arg() |
120+
dotnet_common.deps_arg() |
121+
dotnet_common.compiler_flags_arg() |
122+
dotnet_common.add_hermetic_arguments_arg() |
123+
buck.licenses_arg() |
124+
buck.labels_arg() |
125+
buck.contacts_arg()
126+
),
127+
)
128+
72129
prebuilt_dotnet_library = prelude_rule(
73130
name = "prebuilt_dotnet_library",
74131
docs = """
@@ -111,5 +168,6 @@ prebuilt_dotnet_library = prelude_rule(
111168

112169
dotnet_rules = struct(
113170
csharp_library = csharp_library,
171+
csharp_binary = csharp_binary,
114172
prebuilt_dotnet_library = prebuilt_dotnet_library,
115173
)

prelude/rules_impl.bzl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ load("@prelude//apple:apple_common.bzl", "apple_common")
2222
load("@prelude//apple:apple_rules_decls.bzl", "apple_rules")
2323
load("@prelude//apple:apple_rules_impl.bzl", _apple_extra_attributes = "extra_attributes", _apple_implemented_rules = "implemented_rules")
2424
load("@prelude//configurations:rules.bzl", _config_extra_attributes = "extra_attributes", _config_implemented_rules = "implemented_rules")
25-
load("@prelude//csharp:csharp.bzl", "csharp_library_impl", "prebuilt_dotnet_library_impl")
25+
load("@prelude//csharp:csharp.bzl", "csharp_library_impl", "csharp_binary_impl", "prebuilt_dotnet_library_impl")
2626
load("@prelude//cxx:bitcode.bzl", "llvm_link_bitcode_impl")
2727
load("@prelude//cxx:cuda.bzl", "CudaCompileStyle")
2828
load("@prelude//cxx:cxx.bzl", "cxx_binary_impl", "cxx_library_impl", "cxx_precompiled_header_impl", "cxx_test_impl", "prebuilt_cxx_library_impl")
@@ -175,6 +175,7 @@ extra_implemented_rules = struct(
175175

176176
#c#
177177
csharp_library = csharp_library_impl,
178+
csharp_binary = csharp_binary_impl,
178179
prebuilt_dotnet_library = prebuilt_dotnet_library_impl,
179180

180181
#c++
@@ -274,6 +275,9 @@ _dotnet_extra_attributes = {
274275
"csharp_library": {
275276
"_csharp_toolchain": toolchains_common.csharp(),
276277
},
278+
"csharp_binary": {
279+
"_csharp_toolchain": toolchains_common.csharp(),
280+
},
277281
}
278282

279283
_cxx_extra_library_attrs = (

0 commit comments

Comments
 (0)