Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/additional link outputs #320

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ cc_configure = use_extension("//cc:extensions.bzl", "cc_configure_extension")
use_repo(cc_configure, "local_config_cc", "local_config_cc_toolchains")

register_toolchains("@local_config_cc_toolchains//:all")
register_toolchains("//examples/custom_toolchain:platform_based_toolchain")

bazel_dep(name = "rules_shell", version = "0.2.0", dev_dependency = True)
bazel_dep(name = "googletest", version = "1.15.2", dev_dependency = True)
Expand Down
8 changes: 8 additions & 0 deletions examples/custom_toolchain/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#
# This example demonstrates both approaches.

load("@rules_cc//cc:cc_binary.bzl", "cc_binary")
load("@rules_cc//cc:cc_library.bzl", "cc_library")
load("@rules_cc//cc/toolchains:cc_toolchain.bzl", "cc_toolchain")
load("@rules_cc//cc/toolchains:cc_toolchain_suite.bzl", "cc_toolchain_suite")
Expand All @@ -45,6 +46,13 @@ cc_library(
srcs = ["buildme.cc"],
)

# The binary we want to build. Building this calls two C++ actions: compile (.cc ->
# .o) and link (.o -> executable).
cc_binary(
name = "buildme-bin",
srcs = ["buildme-main.cc"],
)

# This example intentionally makes the cc_toolchain_config definition
# simple. You could alternative add attributes to support multiple
# cc_toolchain_config targets with finer customization.
Expand Down
4 changes: 4 additions & 0 deletions examples/custom_toolchain/buildme-main.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

int main() {
return 0;
}
32 changes: 25 additions & 7 deletions examples/custom_toolchain/sample_compiler
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
#
# Sample script demonstrating custom C++ toolchain selection: handles
# the command that translates a cc_library's .cc (source file) into .o (object
# file).
# file) or the command used to link .o files into the final executable
# (just like gcc is used as a front to the linker).

echo "$0: running sample cc_library compiler (produces .o output)."

Expand All @@ -12,10 +13,27 @@ echo "$0: running sample cc_library compiler (produces .o output)."
#
# examples/custom_toolchain/sample_compiler <various compiler flags> -o bazel-out/x86-fastbuild/bin/examples/custom_toolchain/_objs/buildme/buildme.o.

# The .o is the last parameter.
OBJECT_FILE=${@: -1}
# Swap out .o for .d to get expected .d (source dependency output).
DOTD_FILE=${OBJECT_FILE%?}d
# Get last command-line argument
OUTFILE=${!#}

echo "$0: sample .o output" > $OBJECT_FILE
echo "sample .d output ($0)" > $DOTD_FILE
if [[ "$OUTFILE" == @* ]]; then
# This means we are creating an executable.

# Remove leading @ and param file suffix.
OUTFILE=${OUTFILE#@}
OUTFILE=${OUTFILE%-*}

# Do not create a .d file
DOTD_FILE=
else
# This means we're creating a .o file
OUTFILE=${OUTFILE}
DOTD_FILE=${OUTFILE%.o}.d
fi

echo "$0: sample output" > $OUTFILE
echo "$0: sample output changed" > $OUTFILE.change

if [[ -n $DOTD_FILE ]]; then
echo "sample .d output ($0)" > $DOTD_FILE
fi
19 changes: 12 additions & 7 deletions examples/custom_toolchain/sample_linker
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,17 @@ echo "$0: running sample cc_library linker (produces .a output)."
#
# examples/custom_toolchain/sample_linker @bazel-out/x86-fastbuild/bin/examples/custom_toolchain/libbuildme.a-2.params.

# Get "@bazel-out/.../libbuildme.a-2.params".
PARAMS_FILE=${@: -1}
# Remove the "@" prefix.
OUTFILE=${PARAMS_FILE#?}
# Replace "libbuildme.a-2.params" with "libbuildme.a".
OUTFILE=${OUTFILE%-*}
# Find output file by looking for the argument with a '.a' suffix.
for arg in "$@"; do
if [[ "$arg" == *.a ]]; then
OUTFILE="$arg"
break
fi
done

echo "$0: sample output" > $OUTFILE
if [[ -z "$OUTFILE" ]]; then
echo "No '.a' file found in the arguments."
exit 255
fi

echo "$0: sample output" > $OUTFILE
3 changes: 3 additions & 0 deletions examples/custom_toolchain/toolchain_config.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ def _impl(ctx):
abi_version = "unknown",
abi_libc_version = "unknown",
tool_paths = tool_paths,
additional_link_outputs = [
".change",
],
)

cc_toolchain_config = rule(
Expand Down