Skip to content
This repository has been archived by the owner on Apr 3, 2023. It is now read-only.

Commit

Permalink
fix: resolve certain srcs before attempting to copy and use c99name
Browse files Browse the repository at this point in the history
… to determine output directory (#190)

- For srcs with absolute paths (i.e., headers that are expected to be
installed on the system), if the path is not found, check under the SDK
path.
- Use the `c99name` when determining the output directory for a clang
library.
- Add `xcmetrics` example.

Closes #189.
  • Loading branch information
cgrindel authored Nov 14, 2022
1 parent 1b8626b commit 48d9b06
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 8 deletions.
4 changes: 2 additions & 2 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
# This lets us glob() up all the files inside the examples to make them inputs to tests
# (Note, we cannot use `common --deleted_packages` because the bazel version command doesn't support it)
# To update these lines, run `bazel run @contrib_rules_bazel_integration_test//tools:update_deleted_packages`.
build --deleted_packages=examples/interesting_deps,examples/ios_sim/Sources/Foo,examples/ios_sim/Tests/FooTests,examples/local_package,examples/public_hdrs,examples/simple,examples/simple_revision,examples/simple_with_binary,examples/simple_with_dev_dir,examples/vapor,examples/vapor/Sources/App/Configuration,examples/vapor/Sources/App/Migrations,examples/vapor/Sources/App/Models,examples/vapor/Sources/Run,examples/vapor/Tests/AppTests
query --deleted_packages=examples/interesting_deps,examples/ios_sim/Sources/Foo,examples/ios_sim/Tests/FooTests,examples/local_package,examples/public_hdrs,examples/simple,examples/simple_revision,examples/simple_with_binary,examples/simple_with_dev_dir,examples/vapor,examples/vapor/Sources/App/Configuration,examples/vapor/Sources/App/Migrations,examples/vapor/Sources/App/Models,examples/vapor/Sources/Run,examples/vapor/Tests/AppTests
build --deleted_packages=examples/interesting_deps,examples/ios_sim/Sources/Foo,examples/ios_sim/Tests/FooTests,examples/local_package,examples/public_hdrs,examples/simple,examples/simple_revision,examples/simple_with_binary,examples/simple_with_dev_dir,examples/vapor,examples/vapor/Sources/App/Configuration,examples/vapor/Sources/App/Migrations,examples/vapor/Sources/App/Models,examples/vapor/Sources/Run,examples/vapor/Tests/AppTests,examples/xcmetrics
query --deleted_packages=examples/interesting_deps,examples/ios_sim/Sources/Foo,examples/ios_sim/Tests/FooTests,examples/local_package,examples/public_hdrs,examples/simple,examples/simple_revision,examples/simple_with_binary,examples/simple_with_dev_dir,examples/vapor,examples/vapor/Sources/App/Configuration,examples/vapor/Sources/App/Migrations,examples/vapor/Sources/App/Models,examples/vapor/Sources/Run,examples/vapor/Tests/AppTests,examples/xcmetrics

# Import Shared settings
import %workspace%/shared.bazelrc
Expand Down
8 changes: 8 additions & 0 deletions examples/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ LINUX_TEST_EXAMPLES = [
MACOS_TEST_EXAMPLES = [
"ios_sim",
"public_hdrs",
"xcmetrics",
]

NO_SUDO_TEST_EXAMPLES = ALL_OS_TEST_EXAMPLES + MACOS_TEST_EXAMPLES + LINUX_TEST_EXAMPLES
Expand Down Expand Up @@ -95,6 +96,10 @@ test_suite(

default_test_runner(
name = "default_test_runner",
bazel_cmds = [
"info",
"test //... --verbose_failures --sandbox_debug",
],
)

[
Expand Down Expand Up @@ -132,6 +137,9 @@ default_test_runner(
[
bazel_integration_test(
name = example + "_test",
# Switched to eternal due to the xcmetrics test taking a very long time when
# run via CI on macos.
timeout = "eternal",
bazel_version = CURRENT_BAZEL_VERSION,
# The test needs to be local due to the operations that rules_spm performs.
local = True,
Expand Down
2 changes: 1 addition & 1 deletion examples/incompatible_xcode_use_dev_dir_attr_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ cd "${workspace_dir}"
# BEGIN Custom test logic

# Find Xcode 12.4
xcode_12_4_locations=("/Applications/Xcode-12.4.app" "/Applications/Xcode_12.4.app")
xcode_12_4_locations=("/Applications/Xcode-12.4.app" "/Applications/Xcode_12.4.app" "/Applications/Xcode-12.4.0.app")
for path in "${xcode_12_4_locations[@]}" ; do
[[ -d "${path}" ]] && xcode_12_4_location="${path}" && break
done
Expand Down
2 changes: 1 addition & 1 deletion examples/incompatible_xcode_use_dev_dir_env_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ cd "${workspace_dir}"
# BEGIN Custom test logic

# Find Xcode 12.4
xcode_12_4_locations=("/Applications/Xcode-12.4.app" "/Applications/Xcode_12.4.app")
xcode_12_4_locations=("/Applications/Xcode-12.4.app" "/Applications/Xcode_12.4.app" "/Applications/Xcode-12.4.0.app")
for path in "${xcode_12_4_locations[@]}" ; do
[[ -d "${path}" ]] && xcode_12_4_location="${path}" && break
done
Expand Down
8 changes: 8 additions & 0 deletions examples/xcmetrics/.bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Import Shared settings
import %workspace%/../../shared.bazelrc

# Import CI settings.
import %workspace%/../../ci.bazelrc

# Try to import a local.rc file; typically, written by CI
try-import %workspace%/../../local.bazelrc
8 changes: 8 additions & 0 deletions examples/xcmetrics/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
load("@bazel_skylib//rules:build_test.bzl", "build_test")

build_test(
name = "xcode_metrics_package_targets",
targets = [
"@xcode_metrics_packages//XCMetrics:XCMetricsClient",
],
)
59 changes: 59 additions & 0 deletions examples/xcmetrics/WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
workspace(name = "xcmetrics_example")

local_repository(
name = "cgrindel_rules_spm",
path = "../..",
)

load(
"@cgrindel_rules_spm//spm:deps.bzl",
"spm_rules_dependencies",
)

spm_rules_dependencies()

load("@cgrindel_bazel_starlib//:deps.bzl", "bazel_starlib_dependencies")

bazel_starlib_dependencies()

load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace")

bazel_skylib_workspace()

load(
"@build_bazel_rules_swift//swift:repositories.bzl",
"swift_rules_dependencies",
)

swift_rules_dependencies()

load(
"@build_bazel_rules_swift//swift:extras.bzl",
"swift_rules_extra_dependencies",
)

swift_rules_extra_dependencies()

load("@cgrindel_rules_spm//spm:defs.bzl", "spm_pkg", "spm_repositories")

# From GH189
spm_repositories(
name = "xcode_metrics_packages",
dependencies = [
spm_pkg(
"https://github.com/apple/swift-argument-parser",
exact_version = "0.3.2",
products = ["ArgumentParser"],
),
spm_pkg(
"https://github.com/spotify/XCMetrics",
exact_version = "0.0.10",
products = [
"XCMetricsClient",
"XCMetricsPlugins",
],
),
],
platforms = [".macOS(.v10_15)"],
swift_version = "5.5",
)
32 changes: 30 additions & 2 deletions spm/private/exec_spm_build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,44 @@ if [[ -n "${sdk_name:-}" ]]; then
# NOTE: This will only succeed when Xcode is installed.
sdk_path=$(xcrun --sdk "${sdk_name}" --show-sdk-path)
spm_build_args+=(-Xswiftc "-sdk" -Xswiftc "${sdk_path}")

fi

# Execute the SPM build
"${worker_exec}" "${swift_exec}" build "${spm_build_args[@]}"

# There are cases where the utility has been asked to copy a file that does
# not exist. This has been seen with header files for system libraries.
# Example: https://github.com/cgrindel/rules_spm/issues/189 fails due to
# /usr/include/zconf.h not being available. So, we check for the existence of
# the file under the SDK path, if one was provided. Otherwise, we fail.
resolve_path() {
local src="${1}"
local resolved_src
if [[ -e "${src}" ]]; then
# We found the src
resolved_src="${src}"
elif [[ -n "${sdk_path:-}" && "${src}" =~ ^/ ]]; then
# If the src is an absolute path, then look under the SDK path
resolved_src="${sdk_path}${src}"
if [[ ! -e "${resolved_src}" ]]; then
echo >&2 "WARNING(exec_spm_build): Did not find '${src}' or '${resolved_src}'."
resolved_src=""
fi
else
echo >&2 "WARNING(exec_spm_build): Did not find '${src}'."
resolved_src=""
fi
echo "${resolved_src}"
}

# Replace the specified files with the provided ones
idx=0
while [ "$idx" -lt "${#args[@]}" ]; do
src="${args[idx]}"
src="$(resolve_path "${args[idx]}")"
dest="${args[idx+1]}"
cp -f "${src}" "${dest}"
if [[ -n "${src}" ]]; then
cp -f "${src}" "${dest}"
fi
idx=$((idx+2))
done
12 changes: 10 additions & 2 deletions spm/private/spm_package.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,13 @@ def _declare_clang_library_target_files(
all_outputs = []
o_files = []

target_name = target["name"]
# It turns out that the c99name ends up being the output directory name,
# not the 'name'. Example:
# https://github.com/cgrindel/rules_spm/issues/189 fails due to
# '.../system-zlib.build/anchor.c.o' not being found. It was build under
# '.../system_zlib.build/anchor.c.o'. Note the underscore instead of the
# hyphen. It turns out that the 'c99name' uses an underscore.
target_name = target["c99name"]
module_name = target["name"]

target_build_dirname = "%s/%s.build" % (build_config_path, target_name)
Expand All @@ -100,7 +106,9 @@ def _declare_clang_library_target_files(
# MARK: - System Library Module Info

def _declare_system_library_target_files(ctx, pkg_name, target):
target_name = target["name"]
# Use the 'c99name' as it can differ from the target name. For clang
# libraries, this is the output directory.
target_name = target["c99name"]
module_name = target["name"]

target_path = paths.join(pkg_name, target["path"])
Expand Down

0 comments on commit 48d9b06

Please sign in to comment.