C# Rules for Bazel
Travis | Appveyor |
---|---|
Contents
This is a minimal viable set of C# bindings for building C# code with Mono. It's still pretty rough but it works as a proof of concept that could grow into something more.
These rules are not compatible with sandboxing.
These rules do not currently support the .NET Framework.
These rules do not currently support .NET Core/Standard built with the official compiler on any platform.
Downloading Mono currently does not work.
Due to Windows short path limit (260) of cl compiler TMP env variable should be set to something short (like X:or c:TEMP).
Add the following to your WORKSPACE file to add the external repositories:
# A newer version should be fine git_repository( name = "io_bazel_rules_dotnet", remote = "https://github.com/tomek1909/rules_dotnet", tag = "0.0.1", ) load("@io_bazel_rules_dotnet//dotnet:defs.bzl", "dotnet_register_toolchains", "dotnet_repositories") dotnet_register_toolchains("host") dotnet_repositories()
The dotnet_repositories rule fetches external dependencies, namely the nuget binary. The dotnet_register_toolchains tries to locate mono binaries and configure toolchains to use it. Support for downloading mono is coming soon.
Add a file named
BUILD.bazel
in the root directory of your project. In general, you need one of these files in every directory with dotnet code. At the top of the file used rules should be imported:load("@io_bazel_rules_dotnet//dotnet:defs.bzl", "dotnet_library", "dotnet_binary")
If you intend to use CoreCLR make sure to install libunwind-devel if it is not present on your system (applies to Linux).
-
dotnet_library( name = "foo_bar", srcs = [ "foo.cs", "bar.cs", ], deps = [ "//examples/example_lib:MyClass", "@npgsql//:npgsqllib", ], visibility = ["//visibility:public"], )
-
dotnet_binary( name = "foo_bar", srcs = [ "foo.cs", "bar.cs", ], deps = [ "//examples/example_lib:MyClass", "@npgsql//:npgsqllib", ], visibility = ["//visibility:public"], )
-
dotnet_nunit_test( name = "MyTest", srcs = [ "MyTest.cs", ], deps = [ "//examples/example_lib:MyClass", "//dotnet/externals/nunit2:nunit.framework", ], )
-
dotnet_resx( name = "Transform", src = "//dotnet/externals/nunit2/nunitv2:src/ClientUtilities/util/Transform.resx", )
-
In the WORKSPACE file for your project record a nuget dependency like so. This is a repository rule so it will not work unless it is in a workspace file.
dotnet_nuget_new( name = "npgsql", package="Npgsql", version="3.2.7", sha256="fa3e0cfbb2caa9946d2ce3d8174031a06320aad2c9e69a60f7739b9ddf19f172", build_file_content = """ package(default_visibility = [ "//visibility:public" ]) load("@io_bazel_rules_dotnet//dotnet:defs.bzl", "dotnet_import_library") dotnet_import_library( name = "npgsqllib", src = "lib/net451/Npgsql.dll" ) """ )
Now, in a BUILD file, you can add the package to your deps:
dotnet_binary( name = "foo_bar", srcs = [ "foo.cs", "bar.cs", ], deps = [ "//examples/example_lib:MyClass", "@npgsql//:npgsqllib", ], visibility = ["//visibility:public"], )