Skip to content

Latest commit

 

History

History
447 lines (390 loc) · 10.1 KB

README.md

File metadata and controls

447 lines (390 loc) · 10.1 KB

C# Rules

Rules

WARNING: Theses rules are not compatible with sandboxing.

Overview

This is a minimal viable set of C# bindings for building csharp code with mono. It's still pretty rough but it works as a proof of concept that could grow into something more. If windows support ever happens for Bazel then this might become especially valuable.

Setup

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

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 = "some_name",
  package = "Some.Package",
  version = "0.1.2",
)

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", # referenced via path @ndesk_options//:dylibs
    package="NDesk.Options",
    version="0.2.1",
)

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.
  • .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.
  • Windows .NET framwork support

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.