Skip to content

Commit

Permalink
chore: example updates (#145)
Browse files Browse the repository at this point in the history
* Remove separate pdm and poetry examples

* Fix lock_repo example

* chore: convert external_linking example to bzlmod

* Add readme
  • Loading branch information
jvolkman authored Jan 23, 2025
1 parent 20fba49 commit 656a226
Show file tree
Hide file tree
Showing 52 changed files with 238 additions and 13,543 deletions.
1 change: 1 addition & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
More usage example can be found in the [e2e](../e2e) directory.
223 changes: 0 additions & 223 deletions examples/external_linking/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,223 +0,0 @@
load("@bazel_skylib//rules:write_file.bzl", "write_file")
load(
"@rules_pycross//pycross:defs.bzl",
"package_annotation",
"pycross_lock_file",
"pycross_pdm_lock_model",
"pycross_target_environment",
)
load(
"@rules_pycross//pycross:toolchain.bzl",
"pycross_hermetic_toolchain",
)

_darwin_x86_64 = [
"@platforms//os:macos",
"@platforms//cpu:x86_64",
]

_darwin_arm64 = [
"@platforms//os:macos",
"@platforms//cpu:arm64",
]

_linux_x86_64 = [
"@platforms//os:linux",
"@platforms//cpu:x86_64",
]

_linux_arm64 = [
"@platforms//os:linux",
"@platforms//cpu:arm64",
]

platform(
name = "darwin_x86_64",
constraint_values = _darwin_x86_64,
)

platform(
name = "darwin_arm64",
constraint_values = _darwin_arm64,
)

platform(
name = "linux_x86_64",
constraint_values = _linux_x86_64,
)

platform(
name = "linux_arm64",
constraint_values = _linux_arm64,
)

pycross_target_environment(
name = "python_darwin_x86_64",
abis = ["cp310"],
platforms = ["macosx_12_0_x86_64"],
python_compatible_with = _darwin_x86_64,
version = "3.10.8",
visibility = ["//visibility:public"],
)

pycross_target_environment(
name = "python_darwin_arm64",
abis = ["cp310"],
platforms = ["macosx_12_0_arm64"],
python_compatible_with = _darwin_arm64,
version = "3.10.8",
visibility = ["//visibility:public"],
)

pycross_target_environment(
name = "python_linux_x86_64",
abis = ["cp310"],
platforms = [] + [
"manylinux_2_%s_x86_64" % str(i)
for i in range(5, 38)
],
python_compatible_with = _linux_x86_64,
version = "3.10.8",
visibility = ["//visibility:public"],
)

pycross_target_environment(
name = "python_linux_arm64",
abis = ["cp310"],
platforms = [
"linux_aarch64",
"manylinux2014_aarch64",
] + [
"manylinux_2_%s_aarch64" % str(i)
for i in range(17, 25)
],
python_compatible_with = _linux_arm64,
version = "3.10.8",
visibility = ["//visibility:public"],
)

pycross_hermetic_toolchain(
name = "pycross_darwin_linux",
exec_interpreter = "@python3_10_aarch64-apple-darwin//:py3_runtime",
target_interpreter = "@python3_10_x86_64-unknown-linux-gnu//:py3_runtime",
)

toolchain(
name = "pycross_darwin_linux_tc",
exec_compatible_with = _darwin_arm64,
target_compatible_with = _linux_x86_64,
toolchain = ":pycross_darwin_linux",
toolchain_type = "@rules_pycross//pycross:toolchain_type",
)

pycross_hermetic_toolchain(
name = "pycross_linux_darwin",
exec_interpreter = "@python3_10_x86_64-unknown-linux-gnu//:py3_runtime",
target_interpreter = "@python3_10_aarch64-apple-darwin//:py3_runtime",
)

toolchain(
name = "pycross_linux_darwin_tc",
exec_compatible_with = _linux_x86_64,
target_compatible_with = _darwin_arm64,
toolchain = ":pycross_linux_darwin",
toolchain_type = "@rules_pycross//pycross:toolchain_type",
)

pycross_hermetic_toolchain(
name = "pycross_linux_x86_64_linux_arm64",
exec_interpreter = "@python3_10_x86_64-unknown-linux-gnu//:py3_runtime",
target_interpreter = "@python3_10_aarch64-unknown-linux-gnu//:py3_runtime",
)

toolchain(
name = "pycross_linux_x86_64_linux_arm64_tc",
exec_compatible_with = _linux_x86_64,
target_compatible_with = _linux_arm64,
toolchain = ":pycross_linux_x86_64_linux_arm64",
toolchain_type = "@rules_pycross//pycross:toolchain_type",
)

pycross_pdm_lock_model(
name = "example_lock_model",
lock_file = "pdm.lock",
project_file = "pyproject.toml",
)

pycross_lock_file(
name = "example_lock",
out = "example_lock.bzl",
annotations = {
"numpy": package_annotation(
always_build = True,
build_dependencies = [
"cython",
"setuptools",
"wheel",
],
build_target = "@//deps/numpy",
),
"pandas": package_annotation(
always_build = True,
build_dependencies = [
"cython",
"setuptools",
"wheel",
],
),
"psycopg2": package_annotation(
build_target = "@//deps/psycopg2",
),
"setproctitle": package_annotation(
always_build = True,
build_dependencies = [
"setuptools",
"wheel",
],
),
},
default_alias_single_version = True,
lock_model_file = ":example_lock_model",
target_environments = [
":python_darwin_x86_64",
":python_darwin_arm64",
":python_linux_x86_64",
":python_linux_arm64",
],
)

_GENERATED = {
"example_lock.bzl": "//:example_lock.bzl",
}

# Generate the updater script so there's only one target for devs to run,
# even if many generated files are in the source folder.
write_file(
name = "gen_update",
out = "update.sh",
content = [
# This depends on bash, would need tweaks for Windows
"#!/usr/bin/env bash",
# Bazel gives us a way to access the source folder!
"cd $BUILD_WORKSPACE_DIRECTORY",
] + [
# Paths are now relative to the workspace.
# We can copy files from bazel-bin to the sources
"cp -fv bazel-bin/{1} {0}".format(
k,
# Convert label to path
v.replace(":", "/"),
)
for [
k,
v,
] in _GENERATED.items()
],
)

# This is what you can `bazel run` and it can write to the source folder
sh_binary(
name = "update_example_lock",
srcs = ["update.sh"],
data = _GENERATED.values(),
)
107 changes: 107 additions & 0 deletions examples/external_linking/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
bazel_dep(name = "hermetic_cc_toolchain", version = "3.1.1")
bazel_dep(name = "platforms", version = "0.0.11")
bazel_dep(name = "rules_cc", version = "0.1.0")
bazel_dep(name = "rules_python", version = "1.1.0")
bazel_dep(name = "rules_foreign_cc")
single_version_override(
module_name = "rules_foreign_cc",
version = "0.13.0",
# patches = ["//patches:rules_foreign_cc.iconv.patch"],
# patch_strip = 1,
)

bazel_dep(name = "rules_pycross", version = "0.0.0")
local_path_override(
module_name = "rules_pycross",
path = "../..",
)

# Third party deps
include("//third_party/boringssl:repositories.MODULE.bazel")

include("//third_party/openblas:repositories.MODULE.bazel")

include("//third_party/openssl:repositories.MODULE.bazel")

include("//third_party/postgresql:repositories.MODULE.bazel")

include("//third_party/zlib:repositories.MODULE.bazel")

python = use_extension("@rules_python//python/extensions:python.bzl", "python")
python.toolchain(
is_default = True,
python_version = "3.10.8",
)
use_repo(python, "python_versions")

pycross = use_extension("@rules_pycross//pycross/extensions:pycross.bzl", "pycross")
pycross.configure_environments(
platforms = [
"aarch64-apple-darwin",
"aarch64-unknown-linux-gnu",
"x86_64-unknown-linux-gnu",
"x86_64-unknown-linux-musl",
],
python_versions = [
"3.10.8",
],
)

# Use the lock_import extension to import external lock files.
lock_import = use_extension("@rules_pycross//pycross/extensions:lock_import.bzl", "lock_import")

# Import from PDM
lock_import.import_pdm(
default_alias_single_version = True,
lock_file = "//:pdm.lock",
project_file = "//:pyproject.toml",
repo = "pydeps",
)
lock_import.package(
name = "numpy",
always_build = True,
build_dependencies = [
"cython",
"setuptools",
"wheel",
],
build_target = "@//deps/numpy",
repo = "pydeps",
)
lock_import.package(
name = "pandas",
always_build = True,
build_dependencies = [
"cython",
"setuptools",
"wheel",
],
repo = "pydeps",
)
lock_import.package(
name = "psycopg2",
build_target = "@//deps/psycopg2",
repo = "pydeps",
)
lock_import.package(
name = "setproctitle",
always_build = True,
build_dependencies = [
"setuptools",
"wheel",
],
repo = "pydeps",
)

lock_repos = use_extension("@rules_pycross//pycross/extensions:lock_repos.bzl", "lock_repos")
use_repo(lock_repos, "pydeps")

zig_toolchains = use_extension("@hermetic_cc_toolchain//toolchain:ext.bzl", "toolchains")
use_repo(zig_toolchains, "zig_sdk")

register_toolchains(
"@zig_sdk//toolchain:linux_amd64_gnu.2.19",
"@zig_sdk//toolchain:linux_arm64_gnu.2.28",
"@zig_sdk//toolchain:darwin_amd64",
"@zig_sdk//toolchain:darwin_arm64",
)
3 changes: 3 additions & 0 deletions examples/external_linking/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
This example uses some more advanced techniques for building wheels, including linking to external libraries. Look at [third_party](third_party) for external libraries, and [deps](deps) for wheels.

Note: examples probably don't work on macOS until https://github.com/uber/hermetic_cc_toolchain/issues/10 is solved.
Loading

0 comments on commit 656a226

Please sign in to comment.