Python Smarty
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
dotnet
examples
tools
.gitignore
AUTHORS
CONTRIBUTING.md
CONTRIBUTORS
LICENSE.txt
README.md
WORKSPACE

README.md

C# Rules

Bazel CI
Build Status

Rules

Overview

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.

Caveats

These rules are not compatible with sandboxing.

These rules do not currently support Windows and the.NET Framework. Issue #54 tracks the necessary work. Bazel support for Windows is currently in beta.

These rules do not currently support .NET Core/Standard built with the official compiler on any platform.

Setup

Add the following to your WORKSPACE file to add the external repositories:

# A newer version should be fine
http_archive(
  name = "bazel_skylib",
  url = "https://github.com/bazelbuild/bazel-skylib/archive/ff23a62c57d2912c3073a69c12f42c3d6e58a957.zip",
  strip_prefix = "bazel-skylib-ff23a62c57d2912c3073a69c12f42c3d6e58a957",
  sha256 = "ccf83f162e4a265b3aa09445c84fbc470215e392b250c86f0ce00536c99d5c17",
)

git_repository(
    name = "io_bazel_rules_dotnet",
    remote = "https://github.com/bazelbuild/rules_dotnet.git",
    tag = "0.0.3",
)

load(
    "@io_bazel_rules_dotnet//dotnet:csharp.bzl",
    "csharp_repositories",
    "nuget_package",
)

csharp_repositories(use_local_mono = True)

nuget_package(
  name = "Netwonsoft.Json",
  package = "Newtonsoft.Json",
  version = "10.0.3",
)

The csharp_repositories rule fetches external dependencies, namely the mono repository, the nuget binary, and the nunit binary. Setting the environment variable RULES_DOTNET_USE_LOCAL_MONO=1 or the rule argument use_local_mono to True will use your installed mono framework instead of downloading one. If you are on OS X you can set use_local_mono to False and mono will be downloaded for you by bazel.

Support for downloading mono on Linux is coming soon.

Examples

csharp_library

csharp_library(
    name = "MyLib",
    srcs = ["MyLib.cs"],
    deps = ["//my/dependency:SomeLib"],
)

csharp_binary

csharp_binary(
    name = "MyApp",
    main = "MyApp", # optional name of the main class.
    srcs = ["MyApp.cs"],
    deps = ["//my/dependency:MyLib"],
)

csharp_nunit_test

csharp_nunit_test(
    name = "MyApp",
    srcs = ["MyApp.cs"],
    deps = ["//my/dependency:MyLib"],
)

nuget_package

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.

nuget_package(
    name = "ndesk_options",
    package = "NDesk.Options",
    version = "0.2.1",
)

Now, in a BUILD file, you can add the package to your deps:

csharp_binary(
    name = "MyApp",
    srcs = ["MyApp.cs"],
    deps = ["@ndesk_options//:dylibs"],
)

new_nuget_package

This repository rule accepts either a BUILD file label or build_file_content string. Typically the build content will include dll_import rules that expose the correct set of libraries to the project. For example:

new_nuget_package(
  name = "nuget_grpc",
  package = "Grpc",
  version = "1.0.0",
  build_file_content =
"""
load("@io_bazel_rules_dotnet//dotnet:csharp.bzl", "dll_import")
dll_import(
  name = "system_interactive_async",
  srcs = glob(["System.Interactive.Async.3.0.0/lib/net45/**/*.dll"]),
  visibility = ["//visibility:public"],
)
dll_import(
  name = "core",
  srcs = glob(["Grpc.Core.1.0.0/lib/net45/**/*.dll"]),
  visibility = ["//visibility:public"],
)
"""
)

The structure of the nuget_grpc external workspace can be examined once downloaded and extracted via cd $(bazel info output_base)/external/nuget_grpc.

dll_import

Add a collection of dotnet assembly dll's to be used as a dependency.

dll_import(
    name="some_lib",
    srcs=[
      "Some.dll"
      "Some.Other.dll",
    ]
)

Things still missing:

  • Handle .resx files correctly. See issue #51.
  • .Net Modules
  • Conditionally building documentation.
  • Pulling Mono in through a mono.WORKSPACE file.
    • Still need to implement this for linux.
  • Multiple target framework support for nuget packages.

Future nice to haves:

  • nuget_packages repository rule that will handle multiple different nuget packages in one rule.
  • Building csproj and sln files for VS and MonoDevelop.

csharp_library

csharp_library(name, srcs, deps, warn=4, csc)

Builds a C# .NET library and its corresponding documentation.

Attributes
name

Name, required

Unique name for this rule

srcs

List of Labels; required

Csharp .cs or .resx files.

deps

List of Labels; optional

Dependencies for this rule.

warn

Int; optional; default is 4

Compiler warn level for this library. (Defaults to 4.)

csc

string; optional

Override the default csharp compiler.

Note: This attribute may removed in future versions.

csharp_binary

csharp_binary(name, srcs, deps, main_class, warn=4, csc)

Builds a C# .NET binary.

Attributes
name

Name, required

Unique name for this rule

srcs

List of Labels; required

Csharp .cs or .resx files.

deps

List of Labels; optional

Dependencies for this rule.

main_class

String; optional

Name of class with main() method to use as entry point.

warn

Int; optional; default is 4

Compiler warn level for this binary. (Defaults to 4.)

csc

string; optional

Override the default csharp compiler.

Note: This attribute may removed in future versions.

csharp_nunit_test

csharp_nunit_test(name, srcs, deps, warn=4, csc)

Builds a C# .NET test binary that uses the NUnit unit testing framework.

Attributes
name

Name, required

Unique name for this rule

srcs

List of Labels; required

Csharp .cs or .resx files.

deps

List of Labels; optional

Dependencies for this rule.

warn

Int; optional; default is 4

Compiler warn level for this test. (Defaults to 4.)

csc

string; optional

Override the default csharp compiler.

Note: This attribute may removed in future versions.

nuget_package

nuget_package(name, package, version, package_sources)

A repository_rule that adds a nuget package dependency for the Workspace.

Attributes
name

Name, required

Unique name for this rule

package

String, Required

Name of the nuget package.

version

String, Required

Version of the nuget package to install.

package\_sources

List of strings, optional

Sources to pull nuget packages from. Either url or path.

dll_import(name, srcs)

A repository_rule that adds a nuget package dependency for the Workspace.

Attributes
name

Name, required

Unique name for this rule

srcs

Labels, required

List of dotnet dll assemblies to use.