Skip to content

Commit

Permalink
feat: Add rules_zig; support cross-compiling //....
Browse files Browse the repository at this point in the history
Zig doesn't work very well, yet. You can't depend on C code and can't
include C stdlib headers.
  • Loading branch information
iphydf committed Dec 25, 2023
1 parent 3537682 commit 1a83286
Show file tree
Hide file tree
Showing 65 changed files with 18,311 additions and 82 deletions.
6 changes: 3 additions & 3 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -567,17 +567,17 @@ build:zig --sandbox_add_mount_pair=/tmp
build:linux-arm64-musl --config=zig
build:linux-arm64-musl --platforms=@zig_sdk//platform:linux_arm64
build:linux-arm64-musl --extra_toolchains=@zig_sdk//toolchain:linux_arm64_musl
build:linux-arm64-musl --build_tag_filters=-windows,-macos
build:linux-arm64-musl --build_tag_filters=-no-cross,-windows,-macos

build:linux-x86_64-musl --config=zig
build:linux-x86_64-musl --platforms=@zig_sdk//platform:linux_amd64
build:linux-x86_64-musl --extra_toolchains=@zig_sdk//toolchain:linux_amd64_musl
build:linux-x86_64-musl --build_tag_filters=-windows,-macos
build:linux-x86_64-musl --build_tag_filters=-no-cross,-windows,-macos

build:windows-x86_64 --config=zig
build:windows-x86_64 --platforms=@zig_sdk//platform:windows_amd64
build:windows-x86_64 --extra_toolchains=@zig_sdk//toolchain:windows_amd64
build:windows-x86_64 --build_tag_filters=-macos
build:windows-x86_64 --build_tag_filters=-no-cross,-macos

# Pick *one* of these for the target platform. "nix" is the default Linux target.
# TODO(iphydf): Move to .bazelrc.local.
Expand Down
11 changes: 11 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@ jobs:
common:
uses: TokTok/ci-tools/.github/workflows/common-ci.yml@master

hie-bios:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
submodules: recursive
- name: "Generate HIE BIOS output and haskell_targets.bzl"
run: tools/haskell/hie-docker
- name: "Verify that haskell_targets.bzl didn't change"
run: git diff --exit-code

docker-haskell:
runs-on: ubuntu-latest
steps:
Expand Down
20 changes: 14 additions & 6 deletions .hie-bios
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
#!/usr/bin/env python3
import os
import re
import subprocess
import sys

subprocess.run(["tools/haskell/gen_haskell_targets"], check=True)
if "--regenerate" in sys.argv:
subprocess.run(["tools/haskell/gen_haskell_targets"], check=True)
result = subprocess.run(
["bazel", "run", "//tools:hie-bios@bios", "--output_groups=hie_bios"],
[
"bazel", "run", "//tools/haskell:hie-bios@bios",
"--output_groups=hie_bios"
],
check=True,
capture_output=True,
)
with open(os.environ["HIE_BIOS_OUTPUT"], "w") as fh:
prev = None
for line in result.stdout.decode("utf-8").splitlines(keepends=True):
line = line.replace(
"/home/builder/.cache/bazel/_bazel_builder/a08c2e4811c846650b733c6fc815a920/execroot/toktok/bazel-out/k8-fastbuild/bin/",
line = re.sub(
"/home/builder/.cache/bazel/_bazel_builder/[0-9a-f]+/execroot/toktok/bazel-out/k8-fastbuild/bin/",
"/src/workspace/bazel-bin/",
line,
)
line = line.replace(
"/home/builder/.cache/bazel/_bazel_builder/a08c2e4811c846650b733c6fc815a920/execroot/toktok/",
line = re.sub(
"/home/builder/.cache/bazel/_bazel_builder/[0-9a-f]+/execroot/toktok/",
"/src/workspace/",
line,
)
if line != prev and not line.startswith("-l"):
fh.write(line)
Expand Down
1 change: 1 addition & 0 deletions .restyled.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ exclude:
- "third_party/libzmq/**/*.hpp"
- "third_party/libvpx/**/*.h"
- "third_party/libvpx/**/*.mk"
- "third_party/ncurses/src/**/*"
- "third_party/opus/**/*.h"
23 changes: 23 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,29 @@ load("@hermetic_cc_toolchain//toolchain:defs.bzl", zig_toolchains = "toolchains"

zig_toolchains()

# Actual zig toolchain (the above is for C/C++)
# =========================================================

github_archive(
name = "rules_zig",
repo = "aherrmann/rules_zig",
sha256 = "3d06e19af1aa7449d3e562914ff02e7f946e67a35f7cd6daa31459a966b9b1bd",
version = "864d8e5227f2cadee0f0bcc5c3db75955668a439",
)

load(
"@rules_zig//zig:repositories.bzl",
"rules_zig_dependencies",
"zig_register_toolchains",
)

rules_zig_dependencies()

zig_register_toolchains(
name = "zig",
zig_version = "0.11.0",
)

# Fuzzing
# =========================================================

Expand Down
2 changes: 1 addition & 1 deletion c-toxcore
2 changes: 1 addition & 1 deletion c-toxcore-hs
Submodule c-toxcore-hs updated 1 files
+3 −0 BUILD.bazel
2 changes: 1 addition & 1 deletion experimental
2 changes: 1 addition & 1 deletion hs-apigen
2 changes: 1 addition & 1 deletion hs-cimple
2 changes: 1 addition & 1 deletion hs-github-tools
2 changes: 1 addition & 1 deletion hs-msgpack-arbitrary
2 changes: 1 addition & 1 deletion hs-msgpack-binary
2 changes: 1 addition & 1 deletion hs-msgpack-rpc-conduit
2 changes: 1 addition & 1 deletion hs-msgpack-testsuite
2 changes: 1 addition & 1 deletion hs-msgpack-types
Submodule hs-msgpack-types updated 1 files
+1 −0 BUILD.bazel
2 changes: 1 addition & 1 deletion hs-schema
Submodule hs-schema updated 1 files
+1 −0 BUILD.bazel
2 changes: 1 addition & 1 deletion hs-tokstyle
2 changes: 1 addition & 1 deletion hs-toxcore
2 changes: 1 addition & 1 deletion hs-toxcore-c
2 changes: 1 addition & 1 deletion py_toxcore_c
2 changes: 1 addition & 1 deletion qtox
14 changes: 12 additions & 2 deletions third_party/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,24 @@ filegroup(
"BUILD.bazel*",
# TODO
"BUILD.asound",
"BUILD.x264",
"BUILD.ffmpeg",
"BUILD.openal",
"BUILD.openssl",
"BUILD.sdl2",
"BUILD.x264",
# These depend on //c-toxcore, so can't be pre-built.
"BUILD.toxext",
"BUILD.tox_extension_messages",
],
)] + [
"@com_google_googletest//:gtest",
"@com_google_googletest//:gtest_main",
],
] + select({
"@toktok//tools/config:arm64": [],
"@toktok//tools/config:x86_64": [
"BUILD.ffmpeg",
"BUILD.openal",
"BUILD.sdl2",
],
}),
)
1 change: 1 addition & 0 deletions third_party/BUILD.libidn2
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ cc_library(
"-Iexternal/libidn2/unistring",
"-I$(GENDIR)/external/libidn2",
"-Wno-static-in-inline",
"-Wno-unused-const-variable",
] + select({
"@toktok//tools/config:linux": ["-Ithird_party/libidn2/linux"],
"@toktok//tools/config:osx": ["-Ithird_party/libidn2/osx"],
Expand Down
31 changes: 14 additions & 17 deletions third_party/BUILD.libvpx
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,27 @@ load("@toktok//third_party/yasm:build_defs.bzl", "asm_library")

asm_library(
name = "assemblies",
srcs = select({
"@toktok//tools/config:x86_64": glob([
"vp*/**/x86/*.asm",
]) + [
"vpx_ports/float_control_word.asm",
],
}) + select({
srcs = glob([
"vp*/**/x86/*.asm",
]) + [
"vpx_ports/float_control_word.asm",
] + select({
"@toktok//tools/config:linux": [],
"@toktok//tools/config:windows": ["vpx_ports/emms_mmx.asm"],
}),
hdrs = select({
"@toktok//tools/config:x86_64": [
"third_party/x86inc/x86inc.asm",
"vpx_dsp/x86/bitdepth_conversion_sse2.asm",
"vpx_ports/x86_abi_support.asm",
"@toktok//third_party/libvpx:vpx_config.asm",
],
}),
hdrs = [
"third_party/x86inc/x86inc.asm",
"vpx_dsp/x86/bitdepth_conversion_sse2.asm",
"vpx_ports/x86_abi_support.asm",
"@toktok//third_party/libvpx:linux-x86_64/vpx_config.asm",
],
archive = False,
asmopts = [
"-I$(GENDIR)/external/libvpx", # Generated headers.
"-Iexternal/libvpx",
"-I$(GENDIR)/third_party/libvpx",
],
] + select({
"@toktok//tools/config:linux-x86_64": ["-Ithird_party/libvpx/linux-x86_64"],
}),
)

CONFIGURE = """
Expand Down
44 changes: 28 additions & 16 deletions third_party/BUILD.ncurses
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library")

GENFILES = [
"include/MKterm.h.awk",
"include/curses.h",
"include/ncurses_cfg.h",
"include/ncurses_dll.h",
"include/termcap.h",
"include/unctrl.h",
"ncurses/Makefile",
"ncurses/expanded.c",
"ncurses/fallback.c",
"ncurses/init_keytry.h",
"ncurses/lib_gen.c",
"ncurses/lib_keyname.c",
]

genrule(
name = "regen_config",
srcs = glob([
Expand All @@ -20,21 +35,7 @@ genrule(
"dist.mk",
"install-sh",
],
outs = [
"configure.log",
"include/MKterm.h.awk",
"include/curses.h",
"include/ncurses_cfg.h",
"include/ncurses_dll.h",
"include/termcap.h",
"include/unctrl.h",
"ncurses/Makefile",
"ncurses/expanded.c",
"ncurses/fallback.c",
"ncurses/init_keytry.h",
"ncurses/lib_gen.c",
"ncurses/lib_keyname.c",
],
outs = ["out/" + f for f in GENFILES] + ["out/configure.log"],
cmd = """
PATH=$$PATH:`pwd`/`dirname $(location @diffutils//:cmp)`
MAKE=`pwd`/$(location @gnumake)
Expand All @@ -54,7 +55,7 @@ genrule(
DIR=`pwd`
cd -
for i in $(OUTS); do
cp $$DIR/`echo $$i | sed -e 's|$(GENDIR)/external/ncurses/||'` $$i
cp $$DIR/`echo $$i | sed -e 's|$(GENDIR)/external/ncurses/out/||'` $$i
done
""",
toolchains = ["@rules_cc//cc:current_cc_toolchain"],
Expand All @@ -64,6 +65,17 @@ genrule(
],
)

genrule(
name = "genfiles",
srcs = ["@toktok//third_party/ncurses:src/" + f for f in GENFILES],
outs = GENFILES,
cmd = """
for i in $(OUTS); do
cp `echo $$i | sed -e 's|$(GENDIR)/external/ncurses/|third_party/ncurses/src/|'` $$i
done
""",
)

filegroup(
name = "caplist",
srcs = [
Expand Down
6 changes: 4 additions & 2 deletions third_party/BUILD.openal
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ COPTS = [
"-fexceptions",
"-Wno-unused-function",
"-Wno-switch",
"-msse4.1",
]
] + select({
"@toktok//tools/config:arm64": [],
"@toktok//tools/config:x86_64": ["-msse4.1"],
})

genrule(
name = "regen_config",
Expand Down
11 changes: 8 additions & 3 deletions third_party/BUILD.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@ cc_library(
"-DHAVE_DECL_PTHREAD_MUTEX_RECURSIVE=1",
"-DHAVE_EVENTFD=1",
"-DHAVE_FREEADDRINFO=1",
"-DHAVE_FTS=0",
"-DHAVE_GETADDRINFO=1",
"-DHAVE_GETCWD=1",
"-DHAVE_GETGRGID_R=1",
Expand Down Expand Up @@ -242,7 +241,6 @@ cc_library(
"-DHAVE_LOCALTIME_R=1",
"-DHAVE_STRUCT_STAT_ST_BLKSIZE=1",
"-DHAVE_STRUCT_STAT_ST_BLOCKS=1",
"-DHAVE_SYS_EPOLL_H=1",
"-DHAVE_SYS_PARAM_H=1",
"-DHAVE_UNISTD_H=1",
"-Iexternal/tcl/unix",
Expand All @@ -255,16 +253,23 @@ cc_library(
"-DHAVE_GETHOSTBYADDR_R_8=1",
"-DHAVE_GETHOSTBYNAME_R=1",
"-DHAVE_GETHOSTBYNAME_R_6=1",
"-DNOTIFIER_EPOLL=1",
"-DTCL_SHLIB_EXT='\".so\"'",
],
"@toktok//tools/config:osx": [
"-DHAVE_FTS=1",
"-DHAVE_WEAK_IMPORT=1",
"-DHAVE_COREFOUNDATION=1",
"-DHAVE_LIBKERN_OSATOMIC_H=1",
"-DHAVE_OSSPINLOCKLOCK=1",
],
"@toktok//tools/config:windows": [],
}) + select({
"@toktok//tools/config:linux-x86_64": [
"-DHAVE_FTS=1",
"-DHAVE_SYS_EPOLL_H=1",
"-DNOTIFIER_EPOLL=1",
],
"//conditions:default": [],
}),
defines = ["STATIC_BUILD"],
# TODO(iphydf): Enable once @zlib stops being so private.
Expand Down
5 changes: 4 additions & 1 deletion third_party/haskell/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ licenses(["notice"])
# haskell_toolchain_library can access builtin GHC packages
# and assign them a bazel target name, so that they
# can be referenced as dependencies.
[haskell_toolchain_library(name = name) for name in [
[haskell_toolchain_library(
name = name,
tags = ["no-cross"],
) for name in [
"aeson",
"aeson-pretty",
"ansi-wl-pprint",
Expand Down
2 changes: 2 additions & 0 deletions third_party/haskell/hspec-discover/build_defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def hspec_library(name, src_strip_prefix, **kwargs):
name = name,
srcs = srcs + [name + "_hspec_driver"],
src_strip_prefix = src_strip_prefix,
tags = ["no-cross"],
**kwargs
)

Expand All @@ -58,6 +59,7 @@ def hspec_test(name, visibility=["//tools/haskell:__pkg__"], **kwargs):
srcs = srcs + [name + "_hspec_driver"],
main_file = "test/Spec.hs",
src_strip_prefix = "test",
tags = ["no-cross"],
visibility = visibility,
**kwargs
)
Loading

0 comments on commit 1a83286

Please sign in to comment.