-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6f5bea1
commit b680096
Showing
9 changed files
with
186 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package(default_visibility = ["//visibility:public"]) | ||
|
||
exports_files([ | ||
"posix.bzl", | ||
]) | ||
|
||
filegroup( | ||
name = "srcs", | ||
srcs = glob(["**"]), | ||
visibility = ["//visibility:public"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module( | ||
name = "rules_nixpkgs_posix", | ||
version = "0.8.1", | ||
) | ||
|
||
bazel_dep(name = "rules_nixpkgs_core", version = "0.8.1") | ||
bazel_dep(name = "rules_sh", version = "0.2.0") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# only temporary for compatibility with `WORKSPACE` setup | ||
# TODO: remove when migration to `bzlmod` is complete |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
load( | ||
"@bazel_tools//tools/cpp:lib_cc_configure.bzl", | ||
"get_cpu_value", | ||
) | ||
load("@rules_nixpkgs_core//:nixpkgs.bzl", "nixpkgs_package") | ||
|
||
def nixpkgs_sh_posix_config(name, packages, **kwargs): | ||
nixpkgs_package( | ||
name = name, | ||
nix_file_content = """ | ||
with import <nixpkgs> {{ config = {{}}; overlays = []; }}; | ||
let | ||
# `packages` might include lists, e.g. `stdenv.initialPath` is a list itself, | ||
# so we need to flatten `packages`. | ||
flatten = builtins.concatMap (x: if builtins.isList x then x else [x]); | ||
env = buildEnv {{ | ||
name = "posix-toolchain"; | ||
paths = flatten [ {} ]; | ||
}}; | ||
cmd_glob = "${{env}}/bin/*"; | ||
os = if stdenv.isDarwin then "osx" else "linux"; | ||
in | ||
runCommand "bazel-nixpkgs-posix-toolchain" | ||
{{ executable = false; | ||
# Pointless to do this on a remote machine. | ||
preferLocalBuild = true; | ||
allowSubstitutes = false; | ||
}} | ||
'' | ||
n=$out/nixpkgs_sh_posix.bzl | ||
mkdir -p "$(dirname "$n")" | ||
cat >>$n <<EOF | ||
load("@rules_sh//sh:posix.bzl", "posix", "sh_posix_toolchain") | ||
discovered = {{ | ||
EOF | ||
for cmd in ${{cmd_glob}}; do | ||
if [[ -x $cmd ]]; then | ||
echo " '$(basename $cmd)': '$cmd'," >>$n | ||
fi | ||
done | ||
cat >>$n <<EOF | ||
}} | ||
def create_posix_toolchain(): | ||
sh_posix_toolchain( | ||
name = "nixpkgs_sh_posix", | ||
cmds = {{ | ||
cmd: discovered[cmd] | ||
for cmd in posix.commands | ||
if cmd in discovered | ||
}} | ||
) | ||
EOF | ||
'' | ||
""".format(" ".join(packages)), | ||
build_file_content = """ | ||
load("//:nixpkgs_sh_posix.bzl", "create_posix_toolchain") | ||
create_posix_toolchain() | ||
""", | ||
**kwargs | ||
) | ||
|
||
# Note [Target constraints for POSIX tools] | ||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
# | ||
# There at least three cases for POSIX tools. | ||
# | ||
# Case 1) The tools are used at build time in the execution platform. | ||
# | ||
# Case 2) The tools are used at runtime time in the target platform | ||
# when the target platform is the same as the execution | ||
# platform. | ||
# | ||
# Case 3) The tools are used at runtime time in the target platform | ||
# when cross-compiling. | ||
# | ||
# At the moment, only (1) and (2) are supported by ignoring any target | ||
# constraints when defining the toolchain. This makes available | ||
# any tools that don't depend on the target platform like grep, find | ||
# or sort. In case (2), the tools are still usable at runtime since | ||
# the platforms match. | ||
# | ||
# POSIX tools that depend on the target platform, like cc and strip, | ||
# are better taken from the Bazel cc toolchain instead, so they do | ||
# match the target platform. | ||
# | ||
# TODO: In order to support (3), where the tools would be needed at | ||
# runtime, nixpkgs_sh_posix_configure will need to be changed to take | ||
# as parameter the constraints for the platform in which the tools | ||
# should run. | ||
|
||
def _nixpkgs_sh_posix_toolchain_impl(repository_ctx): | ||
cpu = get_cpu_value(repository_ctx) | ||
repository_ctx.file("BUILD", executable = False, content = """ | ||
toolchain( | ||
name = "nixpkgs_sh_posix_toolchain", | ||
toolchain = "@{workspace}//:nixpkgs_sh_posix", | ||
toolchain_type = "@rules_sh//sh/posix:toolchain_type", | ||
exec_compatible_with = [ | ||
"@platforms//cpu:x86_64", | ||
"@platforms//os:{os}", | ||
"@rules_nixpkgs_core//constraints:support_nix", | ||
], | ||
# Leaving the target constraints empty matter for cross-compilation. | ||
# See Note [Target constraints for POSIX tools] | ||
target_compatible_with = [], | ||
) | ||
""".format( | ||
workspace = repository_ctx.attr.workspace, | ||
os = {"darwin": "osx"}.get(cpu, "linux"), | ||
)) | ||
|
||
_nixpkgs_sh_posix_toolchain = repository_rule( | ||
_nixpkgs_sh_posix_toolchain_impl, | ||
attrs = { | ||
"workspace": attr.string(), | ||
}, | ||
) | ||
|
||
def nixpkgs_sh_posix_configure( | ||
name = "nixpkgs_sh_posix_config", | ||
packages = ["stdenv.initialPath"], | ||
**kwargs): | ||
"""Create a POSIX toolchain from nixpkgs. | ||
Loads the given Nix packages, scans them for standard Unix tools, and | ||
generates a corresponding `sh_posix_toolchain`. | ||
Make sure to call `nixpkgs_sh_posix_configure` before `sh_posix_configure`, | ||
if you use both. Otherwise, the local toolchain will always be chosen in | ||
favor of the nixpkgs one. | ||
Args: | ||
name: Name prefix for the generated repositories. | ||
packages: List of Nix attribute paths to draw Unix tools from. | ||
nix_file_deps: See nixpkgs_package. | ||
repositories: See nixpkgs_package. | ||
repository: See nixpkgs_package. | ||
nixopts: See nixpkgs_package. | ||
fail_not_supported: See nixpkgs_package. | ||
""" | ||
nixpkgs_sh_posix_config( | ||
name = name, | ||
packages = packages, | ||
**kwargs | ||
) | ||
|
||
# The indirection is required to avoid errors when `nix-build` is not in `PATH`. | ||
_nixpkgs_sh_posix_toolchain( | ||
name = name + "_toolchain", | ||
workspace = name, | ||
) | ||
native.register_toolchains( | ||
"@{}//:nixpkgs_sh_posix_toolchain".format(name + "_toolchain"), | ||
) |