Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -231,22 +231,41 @@ common:v8-target-arm64 --@v8//bazel/config:v8_target_cpu=arm64
# the matching runtime objects.
common:rusty-v8-upstream-libcxx --@v8//:v8_use_rusty_v8_custom_libcxx=True

# Native Windows archive builds inherit the runner's MSVC SDK environment.
# Client environment capture and repository discovery happen before target
# transitions, so these runner settings cannot live in runtime_package.bzl.
# The archive target selects its own MSVC host platform and native toolchains.
build:ci-windows-msvc --repo_env=BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=0
build:ci-windows-msvc --action_env=PATH
build:ci-windows-msvc --host_action_env=PATH
build:ci-windows-msvc --action_env=INCLUDE
build:ci-windows-msvc --host_action_env=INCLUDE
build:ci-windows-msvc --action_env=LIB
build:ci-windows-msvc --host_action_env=LIB
build:ci-windows-msvc --action_env=LIBPATH
build:ci-windows-msvc --host_action_env=LIBPATH
build:ci-windows-msvc --action_env=SystemRoot
build:ci-windows-msvc --host_action_env=SystemRoot
build:ci-windows-msvc --action_env=PROCESSOR_ARCHITECTURE
build:ci-windows-msvc --host_action_env=PROCESSOR_ARCHITECTURE
build:ci-windows-msvc --action_env=WindowsSdkDir
build:ci-windows-msvc --host_action_env=WindowsSdkDir
build:ci-windows-msvc --action_env=VCINSTALLDIR
build:ci-windows-msvc --host_action_env=VCINSTALLDIR

# Release profile settings are also defined in codex-rs/Cargo.toml.
# runtime_package.bzl applies this profile to archive dependencies automatically.
# Packaging extracts symbols before stripping; the Rust toolchains retain
# line tables and use split-debuginfo=off. opt mode selects Rust opt-level=3;
# Cargo release disables assertions/checks even if a smoke build overrides
# opt-level to 0, so these are explicit rather than inferred from optimization.
# line tables and use split-debuginfo=off. opt mode selects Rust opt-level=3,
# which disables debug assertions and overflow checks by default.
build:release --compilation_mode=opt
build:release --@rules_rust//rust/settings:lto=thin
build:release --@rules_rust//rust/settings:codegen_units=4
build:release --@rules_rust//rust/settings:extra_rustc_flag=-Cdebug-assertions=no
build:release --@rules_rust//rust/settings:extra_rustc_flag=-Coverflow-checks=no
# Match observed Cargo 1.95 build-tool commands: opt-level=0, assertions off,
# and rustc's non-incremental default of 16 codegen units (not target's 4).
build:release --@rules_rust//rust/settings:extra_exec_rustc_flag=-Copt-level=0
build:release --@rules_rust//rust/settings:extra_exec_rustc_flag=-Ccodegen-units=16
build:release --@rules_rust//rust/settings:extra_exec_rustc_flag=-Cdebug-assertions=no
build:release --@rules_rust//rust/settings:extra_exec_rustc_flag=-Coverflow-checks=no

# Compatibility: Cargo build scripts consume the Rust profile, including debug
# info, and choose their native optimization. Preserve explicit user C/C++ flags.
Expand Down
50 changes: 50 additions & 0 deletions .github/actions/build-bazel-release/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name: Build Bazel release staging archive
description: Build a binary/symbol archive and extract writable inputs for existing signing and packaging.
inputs:
target:
description: Rust release target triple.
required: true
bundle:
description: Binary bundle (combined, primary, app-server, or helpers).
default: combined
release-dir:
description: Directory where the archive is extracted for signing and packaging.
required: true
build-mode:
description: Release optimizations, or debug for faster package smoke builds.
default: release

runs:
using: composite
steps:
- name: Refresh release workspace metadata
shell: bash
working-directory: ${{ github.workspace }}/codex-rs
# Release tags change Cargo.toml; rules_rs needs matching workspace lock entries.
run: cargo update --workspace

- name: Build staging archive
shell: bash
working-directory: ${{ github.workspace }}
env:
TARGET: ${{ inputs.target }}
BUNDLE: ${{ inputs.bundle }}
BUILD_MODE: ${{ inputs.build-mode }}
# Keep Git Bash from rewriting Bazel labels as Windows paths.
MSYS2_ARG_CONV_EXCL: "*"
run: |
python3 .github/scripts/run_bazel_with_buildbuddy.py build \
${{ runner.os == 'Windows' && '--config=ci-windows-msvc' || '' }} \
"--platforms=@rules_rs//rs/platforms:$TARGET" \
"--//codex-rs:runtime_package_build_mode=$BUILD_MODE" \
"//codex-rs:runtime_package_$BUNDLE"

- name: Extract staging archive
shell: bash
working-directory: ${{ github.workspace }}
env:
BUNDLE: ${{ inputs.bundle }}
RELEASE_DIR: ${{ inputs.release-dir }}
run: |
mkdir -p "$RELEASE_DIR"
tar -xzf "bazel-bin/codex-rs/runtime_package_$BUNDLE.tar.gz" -C "$RELEASE_DIR"
1 change: 1 addition & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ single_version_override(
)

bazel_dep(name = "rules_platform", version = "0.1.0")
bazel_dep(name = "rules_pkg", version = "1.0.1")
bazel_dep(name = "aws-lc", version = "5.1.0.bcr.1")
bazel_dep(name = "rules_rs", version = "0.0.96")
bazel_dep(name = "rules_foreign_cc", version = "0.15.1")
Expand Down
1 change: 1 addition & 0 deletions bazel/rules/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ package(default_visibility = ["//visibility:public"])

exports_files([
"e2e_benchmark.bzl",
"runtime_package.bzl",
"schema_bundle.bzl",
])
178 changes: 178 additions & 0 deletions bazel/rules/runtime_package.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
"""Archive unstripped release binaries and debug symbols for signing and packaging."""

load("@platforms//host:constraints.bzl", "HOST_CONSTRAINTS")
load("@rules_pkg//pkg:providers.bzl", "PackageFilegroupInfo", "PackageFilesInfo")
load("@rules_pkg//pkg:tar.bzl", "pkg_tar")

# Keep the profile in sync with --config=release in .bazelrc and Cargo.toml.
# Archive targets apply it themselves, including when built without CI flags.
_RUSTC_FLAGS = "@rules_rust//rust/settings:extra_rustc_flag"
_EXEC_RUSTC_FLAGS = "@rules_rust//rust/settings:extra_exec_rustc_flag"
_PROFILE_INPUTS = [
"//command_line_option:features",
"//command_line_option:host_features",
"//command_line_option:copt",
"//command_line_option:action_env",
"//command_line_option:host_action_env",
"//command_line_option:macos_minimum_os",
"//command_line_option:host_platform",
"//command_line_option:extra_toolchains",
_RUSTC_FLAGS,
_EXEC_RUSTC_FLAGS,
]
_PROFILE_OUTPUTS = _PROFILE_INPUTS + [
"//command_line_option:compilation_mode",
"@rules_rust//rust/settings:lto",
"@rules_rust//rust/settings:codegen_units",
]

def _release_profile_impl(settings, attr):
profile = {key: settings[key] for key in _PROFILE_INPUTS}
smoke = settings["//codex-rs:runtime_package_build_mode"] == "debug"
profile.update({
"//command_line_option:compilation_mode": "opt",
"@rules_rust//rust/settings:lto": "off" if smoke else "thin",
"@rules_rust//rust/settings:codegen_units": 256 if smoke else 4,
# opt mode selects opt-level=3, which disables assertions and overflow
# checks by default. Unoptimized release smokes need the explicit opt-out.
_RUSTC_FLAGS: settings[_RUSTC_FLAGS] + ([
"-Copt-level=0",
"-Cdebug-assertions=no",
] if smoke else []),
# Cargo release build tools use opt-level=0, assertions/checks off,
# and rustc's default 16 CGUs. Overflow checks follow debug assertions.
_EXEC_RUSTC_FLAGS: settings[_EXEC_RUSTC_FLAGS] + [
"-Copt-level=0",
"-Ccodegen-units=16",
"-Cdebug-assertions=no",
],
})
for key in ["//command_line_option:features", "//command_line_option:host_features"]:
profile[key] = settings[key] + ["cargo-release-profile"]
if attr.platform_os == "macos":
profile[_RUSTC_FLAGS] += ["-Csplit-debuginfo=packed"]
profile["//command_line_option:macos_minimum_os"] = attr.macos_minimum_os
profile["//command_line_option:action_env"] += ["MACOSX_DEPLOYMENT_TARGET=" + attr.macos_minimum_os]

# Host tools must run on the build host, including x86_64 cross builds.
host_minimum_os = "11.0" if "@platforms//cpu:aarch64" in HOST_CONSTRAINTS else "10.12"
profile["//command_line_option:host_action_env"] += ["MACOSX_DEPLOYMENT_TARGET=" + host_minimum_os]
elif attr.platform_os == "linux":
# Compatibility with the Cargo musl release: aws-lc jitter can hang.
profile["//command_line_option:copt"] += ["-DDISABLE_CPU_JITTER_ENTROPY"]
elif attr.platform_os == "windows" and "@platforms//os:windows" in HOST_CONSTRAINTS:
# Native MSVC supplies the SDK and host tools for both architectures.
profile["//command_line_option:host_platform"] = "//:local_windows_msvc"
profile["//command_line_option:extra_toolchains"] += [
"@local_config_cc//:cc-toolchain-x64_windows",
"@local_config_cc//:cc-toolchain-arm64_windows",
]
return profile

_release_profile = transition(
implementation = _release_profile_impl,
inputs = _PROFILE_INPUTS + ["//codex-rs:runtime_package_build_mode"],
outputs = _PROFILE_OUTPUTS,
)

def _runtime_binary_files_impl(ctx):
binary = ctx.attr.binary[0]
executable = binary[DefaultInfo].files_to_run.executable
outputs = [executable]
validations = []
files = [PackageFilesInfo(
attributes = {"mode": "0755"},
dest_src_map = {executable.basename: executable},
)]

# Use the binary's target configuration, including cross builds. Linux keeps
# DWARF inside the executable until the existing packaging strip pass.
for group, suffix in [("pdb_file", ".pdb"), ("dsym_folder", ".dSYM")]:
symbols = getattr(binary[OutputGroupInfo], group, depset()).to_list()
if not symbols and group == ctx.attr.symbol_group:
fail("Missing %s for %s" % (group, binary.label))
if not symbols:
continue
if len(symbols) != 1:
fail("Expected one %s for %s, got %s" % (group, binary.label, symbols))
if group == "dsym_folder":
validation = ctx.actions.declare_file(ctx.label.name + ".dsym-checked")
ctx.actions.run_shell(
inputs = symbols,
outputs = [validation],
arguments = [symbols[0].path, executable.basename, validation.path],
command = '''
test -s "$1/Contents/Resources/DWARF/$2" || {
echo "Missing or empty DWARF for $2 in $1" >&2
exit 1
}
touch "$3"
''',
mnemonic = "ValidateRuntimeSymbols",
)
validations.append(validation)
outputs.extend(symbols)
files.append(PackageFilesInfo(
attributes = {"mode": "0644"},
dest_src_map = {binary.label.name + suffix: symbols[0]},
))

return [
DefaultInfo(files = depset(outputs)),
OutputGroupInfo(_validation = depset(validations)),
PackageFilegroupInfo(
pkg_files = [(info, ctx.label) for info in files],
pkg_dirs = [],
pkg_symlinks = [],
),
]

_runtime_binary_files = rule(
implementation = _runtime_binary_files_impl,
attrs = {
"binary": attr.label(mandatory = True, executable = True, cfg = _release_profile),
"platform_os": attr.string(),
"macos_minimum_os": attr.string(),
"_allowlist_function_transition": attr.label(
default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
),
"symbol_group": attr.string(),
},
)

def runtime_archive(name, srcs, **kwargs):
"""Produce a flat archive of writable files, without stripping or signing."""
pkg_tar(
name = name,
srcs = srcs,
extension = "tar.gz",
mode = "0755",
stamp = 0,
allow_duplicates_with_different_content = False,
**kwargs
)

def release_binary(binary, name = "release", **kwargs):
"""Expose release_files and release.tar.gz; override name for sibling binaries."""
kwargs.setdefault("tags", ["manual"])
kwargs.setdefault("visibility", ["//visibility:public"])
_runtime_binary_files(
name = name + "_files",
binary = binary,
platform_os = select({
"@platforms//os:macos": "macos",
"@platforms//os:linux": "linux",
"@platforms//os:windows": "windows",
}),
macos_minimum_os = select({
"@platforms//cpu:aarch64": "11.0",
"//conditions:default": "10.12",
}),
symbol_group = select({
"@platforms//os:macos": "dsym_folder",
"@platforms//os:windows": "pdb_file",
"//conditions:default": "",
}),
**kwargs
)
runtime_archive(name = name, srcs = [":" + name + "_files"], **kwargs)
79 changes: 79 additions & 0 deletions codex-rs/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
load("@bazel_skylib//rules:common_settings.bzl", "string_flag")
load("//bazel/rules:runtime_package.bzl", "runtime_archive")

exports_files([
"clippy.toml",
])
Expand All @@ -24,3 +27,79 @@ test_suite(
],
visibility = ["//visibility:public"],
)

string_flag(
name = "runtime_package_build_mode",
build_setting_default = "release",
values = [
"release",
"debug",
],
visibility = ["//visibility:public"],
)

alias(
name = "runtime_package",
actual = ":runtime_package_combined",
tags = ["manual"],
visibility = ["//visibility:public"],
)

# These targets produce flat staging archives for the existing signing and
# packaging steps. Release builds retain their binaries and debug symbols.
runtime_archive(
name = "runtime_package_combined",
srcs = [
"//codex-rs/app-server:release_files",
"//codex-rs/cli:release_files",
"//codex-rs/code-mode-host:release_files",
] + select({
"@platforms//os:linux": ["//codex-rs/bwrap:release_files"],
"@platforms//os:windows": [
"//codex-rs/windows-sandbox-rs:command-runner-release_files",
"//codex-rs/windows-sandbox-rs:sandbox-setup-release_files",
],
"//conditions:default": [],
}),
tags = ["manual"],
visibility = ["//visibility:public"],
)

runtime_archive(
name = "runtime_package_primary",
srcs = [
"//codex-rs/cli:release_files",
"//codex-rs/code-mode-host:release_files",
"//codex-rs/responses-api-proxy:release_files",
] + select({
"@platforms//os:linux": ["//codex-rs/bwrap:release_files"],
"//conditions:default": [],
}),
tags = ["manual"],
visibility = ["//visibility:public"],
)

runtime_archive(
name = "runtime_package_app-server",
srcs = [
"//codex-rs/app-server:release_files",
"//codex-rs/code-mode-host:release_files",
] + select({
"@platforms//os:linux": ["//codex-rs/bwrap:release_files"],
"//conditions:default": [],
}),
tags = ["manual"],
visibility = ["//visibility:public"],
)

runtime_archive(
name = "runtime_package_helpers",
srcs = [
"//codex-rs/windows-sandbox-rs:command-runner-release_files",
"//codex-rs/windows-sandbox-rs:sandbox-setup-release_files",
"//codex-rs/windows-sandbox-service:release_files",
],
tags = ["manual"],
target_compatible_with = ["@platforms//os:windows"],
visibility = ["//visibility:public"],
)
5 changes: 5 additions & 0 deletions codex-rs/app-server/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
load("//:defs.bzl", "codex_rust_crate")
load("//bazel/rules:runtime_package.bzl", "release_binary")

codex_rust_crate(
name = "app-server",
Expand Down Expand Up @@ -29,3 +30,7 @@ codex_rust_crate(
"//conditions:default": 0,
}),
)

release_binary(
binary = ":codex-app-server",
)
Loading
Loading