Skip to content

Commit

Permalink
Add support for windows (risc0#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
flaub authored Mar 6, 2022
1 parent e409855 commit cc585f1
Show file tree
Hide file tree
Showing 54 changed files with 206 additions and 194 deletions.
7 changes: 4 additions & 3 deletions .bazelrc
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
startup --windows_enable_symlinks

build -c opt
build --announce_rc
build --cxxopt=-std=c++17
build --host_cxxopt=-std=c++17
build --enable_runfiles
build --nobuild_python_zip
build --incompatible_enable_cc_toolchain_resolution
#build --toolchain_resolution_debug
build --flag_alias=riscv_root=//bazel/toolchain/risc0:root
build --flag_alias=accel=//risc0/zkp/accel:flag
build --//bazel/rules/clang_format:config=//:.clang-format

Expand Down
5 changes: 4 additions & 1 deletion BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ filegroup(

clang_format_toolchain(
name = "clang_format_toolchain_impl",
clang_format = "@py3_env//:py3_env/bin/clang-format",
clang_format = select({
"@bazel_tools//platforms:windows": "@py3_env//:py3_env/Library/bin/clang-format.exe",
"//conditions:default": "@py3_env//:py3_env/bin/clang-format",
}),
)

toolchain(
Expand Down
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ features and performance.
We recommend using [Bazelisk](https://github.com/bazelbuild/bazelisk) to make
bazel version management seamless.

In order to build Risc0 executables you'll need a RISC-V toolchain. Bazel will automatically fetch and manage the toolchain for the following platforms:
In order to build Risc0 executables you'll need a RISC-V toolchain.
Bazel will automatically fetch and manage the toolchain for the following platforms:

* Linux - Ubuntu 18+ (x86_64)
* macOS (x86_64)
* macOS (arm64)
* Windows (x86_64)

Other platforms will be supported in the future.

Expand All @@ -31,6 +33,12 @@ bazelisk test //...

Rust development on macOS **requires a full installation of Xcode**.

### Windows

Our usage of Bazel requires [symlink support to be enabled](https://bazel.build/docs/windows#symlink).
This is possible on Windows by enabling [Developer Mode](https://docs.microsoft.com/en-us/windows/apps/get-started/enable-your-device-for-development),
or by running Bazel as an administrator.

## Running the 'Battleship' Examples

'Battleship' is a 2-player hidden information game implemented in C++ & Rust.
Expand Down
5 changes: 4 additions & 1 deletion WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,15 @@ load("@rules_rust//crate_universe:defs.bzl", "crate_universe")
crate_universe(
name = "crates",
cargo_toml_files = [
"//risc0/core:Cargo.toml",
"//risc0/r0vm/rust/host:Cargo.toml",
"//examples/rust/battleship:Cargo.toml",
"//examples/rust/battleship/proof:Cargo.toml",
"//risc0/r0vm/rust/host:Cargo.toml",
],
resolver = "@rules_rust_crate_universe_bootstrap//:crate_universe_resolver",
supported_targets = [
"i686-pc-windows-msvc",
"x86_64-pc-windows-msvc",
"aarch64-apple-darwin",
"x86_64-apple-darwin",
"x86_64-unknown-linux-gnu",
Expand Down
Empty file added bazel/rules/cc/BUILD.bazel
Empty file.
37 changes: 37 additions & 0 deletions bazel/rules/cc/defs.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
def _copts(std):
return select({
"@bazel_tools//platforms:windows": [
"/std:" + std,
"/Zc:preprocessor",
],
"//conditions:default": ["-std=" + std],
})

def _linkopts():
return select({
"@bazel_tools//platforms:windows": ["bcrypt.lib"],
"//conditions:default": [],
})

def cc_binary(name, std = "c++17", copts = [], linkopts = [], **kwargs):
native.cc_binary(
name = name,
copts = copts + _copts(std),
linkopts = linkopts + _linkopts(),
**kwargs
)

def cc_library(name, std = "c++17", copts = [], **kwargs):
native.cc_library(
name = name,
copts = copts + _copts(std),
**kwargs
)

def cc_test(name, std = "c++17", copts = [], linkopts = [], **kwargs):
native.cc_test(
name = name,
copts = copts + _copts(std),
linkopts = linkopts + _linkopts(),
**kwargs
)
25 changes: 0 additions & 25 deletions bazel/rules/clang_format/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,8 @@ string_list_flag(
string_list_flag(
name = "extensions",
build_setting_default = [
"bc",
"c",
"c++",
"cc",
"cl",
"cpp",
"cu",
"cxx",
"h",
"h++",
"hh",
"hpp",
"hxx",
"i",
"ii",
"inc",
"inc",
"inl",
"ipb",
"ipp",
"m",
"mm",
"opb",
"pch",
"tcc",
"tlh",
"tli",
],
)

Expand Down
8 changes: 6 additions & 2 deletions bazel/rules/clang_format/wrapper.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#include <unistd.h>

#include <cstdlib>
#include <fstream>
#include <iostream>
Expand All @@ -12,6 +10,12 @@
#define IS_WINDOWS false
#endif

#if (IS_WINDOWS == true)
#include <direct.h>
#else
#include <unistd.h>
#endif

std::string get_working_path() {
char temp[1024];
return (getcwd(temp, sizeof(temp)) ? std::string(temp) : std::string(""));
Expand Down
8 changes: 4 additions & 4 deletions bazel/rules/doxygen/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ def _impl(ctx):

ctx.actions.run(
mnemonic = "doxygen",
executable = ctx.executable._tool,
executable = ctx.executable.tool,
arguments = [doxyfile.path],
inputs = depset(
direct = [doxyfile] + ctx.files.srcs,
),
outputs = outs,
tools = [ctx.executable._tool],
tools = [ctx.executable.tool],
)

return [DefaultInfo(files = depset(outs))]
Expand All @@ -36,8 +36,8 @@ doxygen = rule(
"doxyfile": attr.label(allow_single_file = True),
"srcs": attr.label_list(allow_files = True),
"anchor": attr.label(mandatory = True),
"_tool": attr.label(
default = Label("@py3_env//:py3_env/bin/doxygen"),
"tool": attr.label(
mandatory = True,
executable = True,
allow_files = True,
cfg = "host",
Expand Down
19 changes: 10 additions & 9 deletions bazel/toolchain/risc0/gcc_toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def pkg_path_from_label(label):
else:
return label.package

def gcc_toolchain_config(name, sysroot):
def gcc_toolchain_config(name, sysroot, version):
sysroot_path = pkg_path_from_label(Label(sysroot))

tool_paths = {tool: "bin/riscv32-unknown-elf-{}".format(tool) for tool in [
Expand All @@ -28,9 +28,9 @@ def gcc_toolchain_config(name, sysroot):
"-DRISCV=1",
"-mabi=ilp32",
"-march=rv32im",
"-nostdlib",
# A freestanding environment is one in which the standard library may not exist,
# and program startup may not necessarily be at main.
"-ffreestanding",
"-fno-common",
"-fno-exceptions",
"-fno-non-call-exceptions",
"-fno-rtti",
Expand Down Expand Up @@ -59,7 +59,8 @@ def gcc_toolchain_config(name, sysroot):
cxx_flags = []

link_flags = common_flags + [
"-Wl,--orphan-handling=warn",
# Do not use the standard system startup files or libraries when linking.
"-nostdlib",
"-pass-exit-codes",
]

Expand All @@ -77,11 +78,11 @@ def gcc_toolchain_config(name, sysroot):
]

cxx_builtin_include_directories = [
"%sysroot%/riscv32-unknown-elf/include/c++/11.1.0",
"%sysroot%/riscv32-unknown-elf/include/c++/11.1.0/riscv32-unknown-elf",
"%sysroot%/riscv32-unknown-elf/include/c++/11.1.0/backward",
"%sysroot%/lib/gcc/riscv32-unknown-elf/11.1.0/include",
"%sysroot%/lib/gcc/riscv32-unknown-elf/11.1.0/include-fixed",
"%sysroot%/riscv32-unknown-elf/include/c++/" + version,
"%sysroot%/riscv32-unknown-elf/include/c++/" + version + "/riscv32-unknown-elf",
"%sysroot%/riscv32-unknown-elf/include/c++/" + version + "/backward",
"%sysroot%/lib/gcc/riscv32-unknown-elf/" + version + "/include",
"%sysroot%/lib/gcc/riscv32-unknown-elf/" + version + "/include-fixed",
"%sysroot%/riscv32-unknown-elf/include",
]

Expand Down
13 changes: 13 additions & 0 deletions bazel/toolchain/risc0/repo.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,28 @@ TOOLCHAINS = [
host_arch = "@platforms//cpu:arm64",
host_os = "@platforms//os:osx",
sha256 = "203255aa7466a2f049169e9de4b3a155c03a0f4549e8c30593d86c7d6b93ed9b",
gcc_version = "11.1.0",
),
struct(
archive = "riscv32im-darwin-x86_64",
host_arch = "@platforms//cpu:x86_64",
host_os = "@platforms//os:osx",
sha256 = "6cad684a275c32b4cd2a75643c0417447807245a60c2130422cc14e33eb7e8a8",
gcc_version = "11.1.0",
),
struct(
archive = "riscv32im-ubuntu18-amd64",
host_arch = "@platforms//cpu:x86_64",
host_os = "@platforms//os:linux",
sha256 = "bec46711fa6f5c7bd8e09610b0b4643136a6b47318a70fea0817e6fce02233f2",
gcc_version = "11.1.0",
),
struct(
archive = "riscv32im-win32-x86_64",
host_arch = "@platforms//cpu:x86_64",
host_os = "@platforms//os:windows",
sha256 = "edba1410f44a2685caf26dea53c562f4a6e3e16d8f650146b6aea8236043c3e6",
gcc_version = "11.2.0",
),
]

Expand All @@ -25,6 +35,7 @@ def _repo_impl(rctx):
Label("//bazel/toolchain/risc0:repo.tpl.BUILD"),
{
"{workspace}": rctx.name,
"{gcc_version}": rctx.attr.gcc_version,
},
)

Expand All @@ -51,6 +62,7 @@ risc0_toolchain_repo = repository_rule(
),
"archive": attr.string(mandatory = True),
"sha256": attr.string(mandatory = True),
"gcc_version": attr.string(mandatory = True),
},
implementation = _repo_impl,
)
Expand Down Expand Up @@ -84,6 +96,7 @@ def risc0_toolchain(name, version):
version = version,
archive = toolchain.archive,
sha256 = toolchain.sha256,
gcc_version = toolchain.gcc_version,
)

proxy_name = repo_name + "_toolchain"
Expand Down
1 change: 1 addition & 0 deletions bazel/toolchain/risc0/repo.tpl.BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ filegroup(
gcc_toolchain_config(
name = "gcc-toolchain-config",
sysroot = "@{workspace}//:sysroot",
version = "{gcc_version}",
)

cc_toolchain(
Expand Down
8 changes: 8 additions & 0 deletions docs/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ doxygen(
srcs = SRCS,
anchor = "//risc0:docs_anchor",
doxyfile = "Doxyfile",
target_compatible_with = select({
"@bazel_tools//platforms:windows": ["@platforms//:incompatible"],
"//conditions:default": [],
}),
tool = select({
"@bazel_tools//platforms:windows": "@py3_env//:py3_env/Library/bin/doxygen.exe",
"//conditions:default": "@py3_env//:py3_env/bin/doxygen",
}),
)

py_binary(
Expand Down
1 change: 1 addition & 0 deletions examples/cpp/battleship/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
load("//bazel/rules/cc:defs.bzl", "cc_binary", "cc_library", "cc_test")
load("//bazel/toolchain/risc0:defs.bzl", "risc0_cc_binary", "risc0_cc_library")

risc0_cc_binary(
Expand Down
1 change: 1 addition & 0 deletions examples/cpp/deck/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
load("//bazel/rules/cc:defs.bzl", "cc_library", "cc_test")
load("//bazel/toolchain/risc0:defs.bzl", "risc0_cc_binary", "risc0_cc_library")

risc0_cc_library(
Expand Down
11 changes: 11 additions & 0 deletions risc0/core/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
load("//bazel/rules/cc:defs.bzl", "cc_library")
load("@rules_rust//rust:defs.bzl", "rust_static_library")
load("@crates//:defs.bzl", "crates_from")

cc_library(
name = "core",
srcs = [
Expand All @@ -14,4 +18,11 @@ cc_library(
"util.h",
],
visibility = ["//visibility:public"],
deps = [":lib"],
)

rust_static_library(
name = "lib",
srcs = ["src/lib.rs"],
deps = crates_from(":Cargo.toml"),
)
7 changes: 7 additions & 0 deletions risc0/core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "risc0-core"
version = "0.1.0"
edition = "2021"

[dependencies]
rand_core = {version = "0.6", features = ["getrandom"]}
Loading

0 comments on commit cc585f1

Please sign in to comment.