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
2 changes: 2 additions & 0 deletions examples/firebase_example/.bazelignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Swift build directory
.build
48 changes: 33 additions & 15 deletions examples/firebase_example/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,36 @@ bzl_library(
deps = ["@cgrindel_swift_bazel//swiftpkg:defs"],
)

# build_test(
# name = "xcode_metrics_package_targets",
# targets = [
# "@swiftpkg_xcmetrics//:XCMetrics",
# "@swiftpkg_xcmetrics//:XCMetricsBackend",
# ],
# )

# test_suite(
# name = "xcode_metrics_package_build_tests",
# tests = [
# "@swiftpkg_xcmetrics//:XCMetricsClientBuildTest",
# "@swiftpkg_xcmetrics//:XCMetricsUtilsBuildTest",
# ],
# )
# List the build_test targets defined in the package:
# bazel query 'kind(_empty_test, @swiftpkg_firebase_ios_sdk//...)'
test_suite(
name = "xcode_metrics_package_build_tests",
tests = [
"@swiftpkg_firebase_ios_sdk//:FirebaseAnalyticsBuildTest",
"@swiftpkg_firebase_ios_sdk//:FirebaseAnalyticsSwift-BetaBuildTest",
"@swiftpkg_firebase_ios_sdk//:FirebaseAnalyticsWithoutAdIdSupportBuildTest",
"@swiftpkg_firebase_ios_sdk//:FirebaseAppCheckBuildTest",
"@swiftpkg_firebase_ios_sdk//:FirebaseAppDistribution-BetaBuildTest",
"@swiftpkg_firebase_ios_sdk//:FirebaseAuthBuildTest",
"@swiftpkg_firebase_ios_sdk//:FirebaseAuthCombine-CommunityBuildTest",
"@swiftpkg_firebase_ios_sdk//:FirebaseCrashlyticsBuildTest",
"@swiftpkg_firebase_ios_sdk//:FirebaseDatabaseBuildTest",
"@swiftpkg_firebase_ios_sdk//:FirebaseDatabaseSwift-BetaBuildTest",
"@swiftpkg_firebase_ios_sdk//:FirebaseDynamicLinksBuildTest",
"@swiftpkg_firebase_ios_sdk//:FirebaseFirestoreBuildTest",
"@swiftpkg_firebase_ios_sdk//:FirebaseFirestoreCombine-CommunityBuildTest",
"@swiftpkg_firebase_ios_sdk//:FirebaseFirestoreSwift-BetaBuildTest",
"@swiftpkg_firebase_ios_sdk//:FirebaseFunctionsBuildTest",
"@swiftpkg_firebase_ios_sdk//:FirebaseFunctionsCombine-CommunityBuildTest",
"@swiftpkg_firebase_ios_sdk//:FirebaseInAppMessaging-BetaBuildTest",
"@swiftpkg_firebase_ios_sdk//:FirebaseInAppMessagingSwift-BetaBuildTest",
"@swiftpkg_firebase_ios_sdk//:FirebaseInstallationsBuildTest",
"@swiftpkg_firebase_ios_sdk//:FirebaseMLModelDownloaderBuildTest",
"@swiftpkg_firebase_ios_sdk//:FirebaseMessagingBuildTest",
"@swiftpkg_firebase_ios_sdk//:FirebasePerformanceBuildTest",
"@swiftpkg_firebase_ios_sdk//:FirebaseRemoteConfigBuildTest",
"@swiftpkg_firebase_ios_sdk//:FirebaseStorageBuildTest",
"@swiftpkg_firebase_ios_sdk//:FirebaseStorageCombine-CommunityBuildTest",
"@swiftpkg_firebase_ios_sdk//:FirebaseStorageSwift-BetaBuildTest",
],
)
9 changes: 5 additions & 4 deletions swiftpkg/internal/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -187,14 +187,15 @@ bzl_library(
)

bzl_library(
name = "bazel_repo_names",
srcs = ["bazel_repo_names.bzl"],
name = "repository_files",
srcs = ["repository_files.bzl"],
visibility = ["//swiftpkg:__subpackages__"],
deps = ["@cgrindel_bazel_starlib//bzllib:defs"],
)

bzl_library(
name = "repository_files",
srcs = ["repository_files.bzl"],
name = "bazel_repo_names",
srcs = ["bazel_repo_names.bzl"],
visibility = ["//swiftpkg:__subpackages__"],
)

Expand Down
21 changes: 4 additions & 17 deletions swiftpkg/internal/clang_files.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ load("@bazel_skylib//lib:sets.bzl", "sets")
load("@cgrindel_bazel_starlib//bzllib:defs.bzl", "lists")
load("//swiftpkg/internal/modulemap_parser:declarations.bzl", dts = "declaration_types")
load("//swiftpkg/internal/modulemap_parser:parser.bzl", modulemap_parser = "parser")
load(":repository_files.bzl", "repository_files")

# Directory names that may include public header files.
_PUBLIC_HDR_DIRNAMES = ["include", "public"]
Expand Down Expand Up @@ -91,29 +90,17 @@ def _remove_prefix(path, prefix):
def _remove_prefixes(paths_list, prefix):
if prefix == None:
return paths_list
prefix_len = len(prefix)
return [
path[prefix_len:] if path.startswith(prefix) else path
path.removeprefix(prefix)
for path in paths_list
]

def _collect_files(
repository_ctx,
root_paths,
public_includes = None,
all_srcs,
public_includes = [],
remove_prefix = None,
is_library = True):
paths_list = []
for root_path in root_paths:
paths_list.extend(
repository_files.list_files_under(
repository_ctx,
root_path,
),
)

public_includes = [] if public_includes == None else public_includes

# hdrs: Public headers
# srcs: Private headers and source files.
# others: Uncategorized
Expand All @@ -124,7 +111,7 @@ def _collect_files(

modulemap = None
modulemap_orig_path = None
for orig_path in paths_list:
for orig_path in all_srcs:
path = _remove_prefix(orig_path, remove_prefix)
_root, ext = paths.split_extension(path)
if ext == ".h":
Expand Down
13 changes: 10 additions & 3 deletions swiftpkg/internal/modulemap_parser/collect_module.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ load(":collection_results.bzl", "collection_results")
load(":declarations.bzl", "declarations")
load(":errors.bzl", "errors")
load(":module_declarations.bzl", "module_declarations")
load(":tokens.bzl", "tokens", rws = "reserved_words", tts = "token_types")
load(":tokens.bzl", "operators", "tokens", rws = "reserved_words", tts = "token_types")

# MARK: - Attribute Collection

Expand Down Expand Up @@ -91,10 +91,17 @@ def _process_module_tokens(parsed_tokens, prefix_tokens, is_submodule):
return None, err
consumed_count += 1

module_id_token, err = tokens.get_as(parsed_tokens, 1, tts.identifier, count = tlen)
module_id_or_asterisk_token, err = tokens.get(parsed_tokens, 1, count = tlen)
if err != None:
return None, err
consumed_count += 1
if not tokens.is_a(module_id_or_asterisk_token, tts.identifier) and \
not tokens.is_a(module_id_or_asterisk_token, tts.operator, operators.asterisk):
return None, errors.new(
"Expected module identifier or asterisk, but was {}.".format(
module_id_or_asterisk_token.type,
),
)

# Collect the attributes and module members
skip_ahead = 0
Expand Down Expand Up @@ -139,7 +146,7 @@ def _process_module_tokens(parsed_tokens, prefix_tokens, is_submodule):

# Create the declaration
decl = declarations.module(
module_id = module_id_token.value,
module_id = module_id_or_asterisk_token.value,
explicit = explicit,
framework = framework,
attributes = attributes,
Expand Down
9 changes: 9 additions & 0 deletions swiftpkg/internal/pkginfos.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ def _new_target_from_json_maps(dump_map, desc_map):
path = desc_map["path"],
# List of sources provided by SPM
sources = desc_map["sources"],
# Exclude paths specified by the Swift package manifest author.
exclude_paths = dump_map.get("exclude", default = []),
# Source paths specified by the Swift package manifest author.
source_paths = dump_map.get("sources"),
dependencies = dependencies,
Expand Down Expand Up @@ -574,6 +576,7 @@ def _new_target(
path,
sources,
dependencies,
exclude_paths = [],
source_paths = None,
clang_settings = None,
swift_settings = None,
Expand All @@ -592,6 +595,8 @@ def _new_target(
to the `path`.
dependencies: A `list` of target dependency values as returned by
`pkginfos.new_target_dependency()`.
exclude_paths: Optional. A `list` of paths that should be excluded as
specified by the Swift package manifest author.
source_paths: Optional. A `list` of paths (`string` values) specified by
the Swift package manfiest author.
clang_settings: Optional. A `struct` as returned by `pkginfos.new_clang_settings`.
Expand All @@ -614,6 +619,9 @@ def _new_target(
module_type,
"Unrecognized module type. type:",
)

# NOTE: We are explicitly not normalizing the exclude_paths. The inclusion
# of a trailing slash can be critical when the exclude logic is applied.
normalized_src_paths = None
if source_paths != None:
normalized_src_paths = [
Expand All @@ -628,6 +636,7 @@ def _new_target(
path = path,
sources = sources,
dependencies = dependencies,
exclude_paths = exclude_paths,
source_paths = normalized_src_paths,
clang_settings = clang_settings,
swift_settings = swift_settings,
Expand Down
55 changes: 49 additions & 6 deletions swiftpkg/internal/repository_files.bzl
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
"""Module for retrieving and manipulating repository file information."""

def _list_files_under(repository_ctx, path):
load("@cgrindel_bazel_starlib//bzllib:defs.bzl", "lists")

def _list_files_under(repository_ctx, path, exclude = []):
"""Retrieves the list of files under the specified path.

This function returns paths for all of the files under the specified path.

Args:
repository_ctx: A `repository_ctx` instance.
path: A path `string` value.
exclude: Optional. A `list` of path `string` values that should be
excluded from the result.

Returns:
A `list` of path `string` values.
Expand All @@ -19,8 +23,46 @@ def _list_files_under(repository_ctx, path):
)
if exec_result.return_code != 0:
fail("Failed to list files in %s. stderr:\n%s" % (path, exec_result.stderr))
paths = exec_result.stdout.splitlines()
return paths
path_list = exec_result.stdout.splitlines()
path_list = _exclude_paths(path_list, exclude)
return path_list

def _exclude_paths(path_list, exclude):
"""Filter the list of paths using the provided exclude list.

An exclude list item can be a file or a directory. An entry is considered a
directory if it has a trailing slash (`/`). If a path equals a file entry,
it is excluded. If a path starts with a directory entry, it is excluded.

Args:
path_list: A `list` of paths as `string` values.
exclude: A `list` of files and directories to exclude from the provided
paths.

Returns:
The input `list` with the files and directories excluded.
"""
exclude_files = []
exclude_dirs = []
for ex in exclude:
if ex.endswith("/"):
exclude_dirs.append(ex)
else:
exclude_files.append(ex)

results = []
for path in path_list:
if lists.contains(exclude_files, path):
continue
keep = True
for exd in exclude_dirs:
if path.startswith(exd):
keep = False
break
if keep:
results.append(path)

return results

def _list_directories_under(repository_ctx, path, max_depth = None):
"""Retrieves the list of directories under the specified path.
Expand Down Expand Up @@ -82,8 +124,9 @@ def _copy_directory(repository_ctx, src, dest):
)

repository_files = struct(
list_files_under = _list_files_under,
list_directories_under = _list_directories_under,
find_and_delete_files = _find_and_delete_files,
copy_directory = _copy_directory,
exclude_paths = _exclude_paths,
find_and_delete_files = _find_and_delete_files,
list_directories_under = _list_directories_under,
list_files_under = _list_files_under,
)
9 changes: 9 additions & 0 deletions swiftpkg/internal/swift_package.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ load(
)
load(":pkginfos.bzl", "pkginfos")
load(":repo_rules.bzl", "repo_rules")
load(":repository_files.bzl", "repository_files")

# The implementation of this repository rule is heavily influenced by the
# implementation for git_repository.
Expand Down Expand Up @@ -38,6 +39,11 @@ def _update_git_attrs(orig, keys, override):
result.pop("branch", None)
return result

def _remove_bazel_files(repository_ctx, directory):
files = ["BUILD.bazel", "BUILD", "WORKSPACE", "WORKSPACE.bazel"]
for file in files:
repository_files.find_and_delete_files(repository_ctx, directory, file)

def _swift_package_impl(repository_ctx):
directory = str(repository_ctx.path("."))
env = repo_rules.get_exec_env(repository_ctx)
Expand All @@ -46,6 +52,9 @@ def _swift_package_impl(repository_ctx):
# Download the repo
update = _clone_or_update_repo(repository_ctx, directory)

# Remove any Bazel build files.
_remove_bazel_files(repository_ctx, directory)

# Get the package info
pkg_info = pkginfos.get(repository_ctx, directory, env = env)

Expand Down
Loading