|
9 | 9 | load(":csharp_providers.bzl", "DllDepTSet", "DllReference", "DotNetLibraryInfo", "generate_target_tset_children") |
10 | 10 | load(":toolchain.bzl", "CSharpToolchainInfo") |
11 | 11 |
|
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: |
13 | 13 | toolchain = ctx.attrs._csharp_toolchain[CSharpToolchainInfo] |
14 | 14 |
|
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) |
21 | 17 |
|
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. |
23 | 19 | cmd = [toolchain.csc] |
24 | 20 |
|
25 | 21 | # Add caller specified compiler flags. |
26 | 22 | cmd.append(ctx.attrs.compiler_flags) |
27 | 23 |
|
28 | 24 | # Set the output target as a .NET library. |
29 | | - cmd.append("/target:library") |
| 25 | + cmd.append("/target:" + target_type) |
30 | 26 | cmd.append(cmd_args( |
31 | | - library.as_output(), |
| 27 | + library_or_exe.as_output(), |
32 | 28 | format = "/out:{}", |
33 | 29 | )) |
34 | 30 |
|
@@ -63,13 +59,32 @@ def csharp_library_impl(ctx: AnalysisContext) -> list[Provider]: |
63 | 59 | # Run the C# compiler to produce the output artifact. |
64 | 60 | ctx.actions.run(cmd, category = "csharp_compile") |
65 | 61 |
|
| 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 | + |
66 | 75 | 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)), |
73 | 88 | ] |
74 | 89 |
|
75 | 90 | def prebuilt_dotnet_library_impl(ctx: AnalysisContext) -> list[Provider]: |
|
0 commit comments