Coder Social home page Coder Social logo

j-swift / rules_dotnet Goto Github PK

View Code? Open in Web Editor NEW

This project forked from bazelbuild/rules_dotnet

0.0 1.0 0.0 1.13 MB

.NET rules for Bazel

License: Apache License 2.0

Python 0.14% C 1.59% C# 7.00% PowerShell 0.02% XSLT 0.08% Batchfile 0.08% Starlark 91.09%

rules_dotnet's Introduction

C# Rules for Bazel

  • Build status:

    Azure pipelines

    Travis CI

    Appveyor

    badgeAzure

    badgeTravis

    badgeAppveyor

Documentation

Overview

This is a minimal viable set of C# bindings for building C# code with Core, Net and Mono. It's still pretty rough but it works.

Warning: on Windows requires minimum 1.1.0 version of bazel (due to improper symbolic link targets handling in previous versions).

Caveats

These rules are not compatible with sandboxing. Particularly, running dotnet rules on Linux or OSX requires passing --spawn_strategy=standalone.

Bazel creates long paths. Therefore it is recommended to increase the length limit using newer version of Windows. Please see here.

However, some Windows programs do not handle long path names. Most notably - Microsoft C compiler (cl.exe). Therefore TMP env variable should be set to something short (like X:\ or c:\TEMP).

Bazel and dotnet_rules rely on symbolic linking. On Windows it, typically, requires elevated permissions. However, newer versions of Windows have a workaround.

NUnit v2 runner used in some tests requires .NET Framework 3.5 installation.

If you intend to use Mono or .Net Framework then they have to be installed locally on the machine. The producers of these frameworks do not provide downloadable "run-in-place" (without installation) versions. The developer versions of these frameworks have to be used.

Setup

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

    # A newer version should be fine
    load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")
    git_repository(
        name = "io_bazel_rules_dotnet",
        remote = "https://github.com/bazelbuild/rules_dotnet",
        branch = "master",
    )
    
    load("@io_bazel_rules_dotnet//dotnet:deps.bzl", "dotnet_repositories")
    
    dotnet_repositories()
    
    load("@io_bazel_rules_dotnet//dotnet:defs.bzl", "core_register_sdk", "net_register_sdk", "mono_register_sdk",
        "dotnet_register_toolchains", "dotnet_repositories_nugets", "nuget_package")
    
    dotnet_register_toolchains()
    dotnet_repositories_nugets()
    # For .NET Core:
    core_register_sdk()
    # For .NET Framework:
    net_register_sdk()
    # For Mono:
    mono_register_sdk()

    The dotnet_repositories rule fetches external dependencies which have to be defined before loading any other file of rules_dotnet. dotnet_repositories_nugets loads nuget packages required by test rules.

    The dotnet_register_toolchains configures toolchains.

    The mono_register_sdk, core_register_sdk, net_register_sdk "glue" toolchains with appropriate SDKs.

  • 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).

Examples

  • dotnet_library

    dotnet_library(
      name = "foo_bar.dll",
      srcs = [
          "foo.cs",
          "bar.cs",
      ],
      deps = [
          "//examples/example_lib:MyClass",
          "@npgsql//:npgsqllib",
      ],
    )

    Note: The defined library must have extension .dll. Otherwise launchers used by rules_dotnet are not able to correctly locate necessary files.

  • dotnet_binary

    dotnet_binary(
      name = "foo_bar.exe",
      srcs = [
          "foo.cs",
          "bar.cs",
      ],
      deps = [
          "//examples/example_lib:MyClass",
          "@npgsql//:npgsqllib",
      ],
      visibility = ["//visibility:public"],
    )

    Note: The defined library must have extension .exe. Otherwise launchers used by rules_dotnet are not able to correctly locate necessary files.

  • dotnet_nunit_test

    dotnet_nunit_test(
        name = "MyTest.dll",
        srcs = [
            "MyTest.cs",
        ],
        deps = [
            "//examples/example_lib:MyClass",
            "//dotnet/externals/nunit2:nunit.framework",
        ],
    )

    Note: The defined library must have extension .dll. Otherwise launchers used by rules_dotnet are not able to correctly locate necessary files.

  • dotnet_resx

    dotnet_resx(
        name = "Transform",
        src = "//dotnet/externals/nunit2/nunitv2:src/ClientUtilities/util/Transform.resx",
    )
  • 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 = "newtonsoft.json",
        package = "newtonsoft.json",
        version = "11.0.2",
        sha256 = "679e438d5eb7d7e5599aa71b65fd23ee50dabf57579607873b87d34aec11ca1e",
        core_lib = "lib/netstandard2.0/Newtonsoft.Json.dll",
        net_lib = "lib/net45/Newtonsoft.Json.dll",
        mono_lib = "lib/net45/Newtonsoft.Json.dll",
        core_deps = [
        ],
        net_deps = [
        ],
        mono_deps = [
        ],
        core_files = [
            "lib/netstandard2.0/Newtonsoft.Json.dll",
            "lib/netstandard2.0/Newtonsoft.Json.xml",
        ],
        net_files = [
            "lib/net45/Newtonsoft.Json.dll",
            "lib/net45/Newtonsoft.Json.xml",
        ],
        mono_files = [
            "lib/net45/Newtonsoft.Json.dll",
            "lib/net45/Newtonsoft.Json.xml",
        ],
    )

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

    dotnet_binary(
        name = "foo_bar.exe",
        srcs = [
            "foo.cs",
            "bar.cs",
        ],
        deps = [
            "//examples/example_lib:MyClass",
            "@newtonsoft.json//:dotnet",
        ],
        visibility = ["//visibility:public"],
    )
  • dotnet_nuget_new

    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.exe",
        srcs = [
            "foo.cs",
            "bar.cs",
        ],
        deps = [
            "//examples/example_lib:MyClass",
            "@npgsql//:npgsqllib",
        ],
        visibility = ["//visibility:public"],
    )

rules_dotnet's People

Contributors

amlinux avatar damienmg avatar damirdev avatar davidzchen avatar dslomov avatar iainmerrick avatar ilya-klyuchnikov avatar j3parker avatar jart avatar jimevans avatar joey9801 avatar kchodorow avatar laurentlb avatar mtsgrd avatar nmalsang avatar pcj avatar philwo avatar plule-ansys avatar renovate-bot avatar strejczek avatar thelgevold avatar tomaszstrejczek avatar tomdegoede avatar toqueteos avatar vladmos avatar zaphar avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.