cgrindel/rules_spm

Target of same name as package without having a product fails

pswaminathan opened this issue · 6 comments

Example: Firebase

  • Package name: Firebase
  • Target name: Firebase

Firebase is not a product within the Firebase package. However, _gather_deps_for_target looks for the target name (Firebase) in pkg_descs_dict, finds it, and treats it as a product target (link)

@pswaminathan were you able to find a solution to resolve this issue and use Firebase in SPM rules?

Would it be trivial to fork firebase - hack the package.swift file - and get this to work? I've wasted days on this.

Or could we hijack the script for firebase?

            # Look for the product in a package with the same name.
            ref_pkg_desc = pkg_descs_dict.get(by_name)
            if ref_pkg_desc != None:
                product_refs.append(refs.create(ref_types.product, ref_pkg_desc["name"], by_name))
                continue

if ref_pkg_desc == "firebase"
then ....

I hacked this - https://github.com/wweevv-johndpope/firebase-ios-sdk.git

spm_repositories(
    name = "swift_pkgs",
    dependencies = [
        spm_pkg(
            "https://github.com/wweevv-johndpope/firebase-ios-sdk.git",
            from_version = "8.10.0",
            products = ["FirebaseAnalytics"],
        ),
    ],
)


swift_library(
    name = "main",
    module_name = "main",
    srcs = glob([
        "Sources/**/*.swift",
    ]),
    copts = [
        "-warnings-as-errors",
    ],
    deps = [
    "@swift_pkgs//FirebaseAnalytics:FirebaseAnalytics"
   ] 

changing Firebase -> FirebaseAnalytics

let package = Package(
  name: "FirebaseAnalytics",
  platforms: [.iOS(.v11), .macOS(.v10_12), .tvOS(.v12), .watchOS(.v7)],
  products: [
    .library(
      name: "FirebaseAnalytics",

this matches the product name FirebaseAnalytics - but doesn't help.


INFO: Repository swift_pkgs instantiated at:
  /Users/johndpope/Documents/gitWorkspace/Telegram-iOS/WORKSPACE:104:17: in <toplevel>
Repository rule spm_repositories defined at:
  /private/var/tmp/_bazel_johndpope/d00484eadd9b40dcabc19699d7af995d/external/cgrindel_rules_spm/spm/private/spm_repositories.bzl:595:35: in <toplevel>
ERROR: An error occurred during the fetch of repository 'swift_pkgs':
   Traceback (most recent call last):
	File "/private/var/tmp/_bazel_johndpope/d00484eadd9b40dcabc19699d7af995d/external/cgrindel_rules_spm/spm/private/spm_repositories.bzl", line 593, column 30, in _spm_repositories_impl
		_configure_spm_repository(repository_ctx, pkgs, env)
	File "/private/var/tmp/_bazel_johndpope/d00484eadd9b40dcabc19699d7af995d/external/cgrindel_rules_spm/spm/private/spm_repositories.bzl", line 503, column 57, in _configure_spm_repository
		clang_hdr_paths = _get_clang_hdrs_for_target(
	File "/private/var/tmp/_bazel_johndpope/d00484eadd9b40dcabc19699d7af995d/external/cgrindel_rules_spm/spm/private/spm_repositories.bzl", line 273, column 48, in _get_clang_hdrs_for_target
		collected_files = clang_files.collect_files(repository_ctx, [src_path])
	File "/private/var/tmp/_bazel_johndpope/d00484eadd9b40dcabc19699d7af995d/external/cgrindel_rules_spm/spm/private/clang_files.bzl", line 172, column 45, in _collect_files
		hdrs = _get_hdr_paths_from_modulemap(
	File "/private/var/tmp/_bazel_johndpope/d00484eadd9b40dcabc19699d7af995d/external/cgrindel_rules_spm/spm/private/clang_files.bzl", line 68, column 13, in _get_hdr_paths_from_modulemap
		fail("Errors parsing the %s. %s" % (modulemap_path, err))
Error in fail: Errors parsing the /private/var/tmp/_bazel_johndpope/d00484eadd9b40dcabc19699d7af995d/external/swift_pkgs/spm_build/checkouts/grpc-ios/native_src/include/grpc/module.modulemap. struct(child_errors = [], msg = "Unexpected prefix tokens found at end of module member block. tokens: [struct(type = \"reserved\", value = \"module\"), struct(type = \"operator\", value = \"*\"), struct(type = \"curly_bracket_open\", value = None)]")
ERROR: /Users/johndpope/Documents/gitWorkspace/Telegram-iOS/WORKSPACE:104:17: fetching spm_repositories rule //external:swift_pkgs: Traceback (most recent call last):
	File "/private/var/tmp/_bazel_johndpope/d00484eadd9b40dcabc19699d7af995d/external/cgrindel_rules_spm/spm/private/spm_repositories.bzl", line 593, column 30, in _spm_repositories_impl
		_configure_spm_repository(repository_ctx, pkgs, env)
	File "/private/var/tmp/_bazel_johndpope/d00484eadd9b40dcabc19699d7af995d/external/cgrindel_rules_spm/spm/private/spm_repositories.bzl", line 503, column 57, in _configure_spm_repository
		clang_hdr_paths = _get_clang_hdrs_for_target(
	File "/private/var/tmp/_bazel_johndpope/d00484eadd9b40dcabc19699d7af995d/external/cgrindel_rules_spm/spm/private/spm_repositories.bzl", line 273, column 48, in _get_clang_hdrs_for_target
		collected_files = clang_files.collect_files(repository_ctx, [src_path])
	File "/private/var/tmp/_bazel_johndpope/d00484eadd9b40dcabc19699d7af995d/external/cgrindel_rules_spm/spm/private/clang_files.bzl", line 172, column 45, in _collect_files
		hdrs = _get_hdr_paths_from_modulemap(
	File "/private/var/tmp/_bazel_johndpope/d00484eadd9b40dcabc19699d7af995d/external/cgrindel_rules_spm/spm/private/clang_files.bzl", line 68, column 13, in _get_hdr_paths_from_modulemap
		fail("Errors parsing the %s. %s" % (modulemap_path, err))
Error in fail: Errors parsing the /private/var/tmp/_bazel_johndpope/d00484eadd9b40dcabc19699d7af995d/external/swift_pkgs/spm_build/checkouts/grpc-ios/native_src/include/grpc/module.modulemap. struct(child_errors = [], msg = "Unexpected prefix tokens found at end of module member block. tokens: [struct(type = \"reserved\", value = \"module\"), struct(type = \"operator\", value = \"*\"), struct(type = \"curly_bracket_open\", value = None)]")
ERROR: /Users/johndpope/Documents/gitWorkspace/Telegram-iOS/submodules/TelegramUI/BUILD:42:14: //submodules/TelegramUI:TelegramUI depends on @swift_pkgs//FirebaseAnalytics:FirebaseAnalytics in repository @swift_pkgs which failed to fetch. no such package '@swift_pkgs//FirebaseAnalytics': Errors parsing the /private/var/tmp/_bazel_johndpope/d00484eadd9b40dcabc19699d7af995d/external/swift_pkgs/spm_build/checkouts/grpc-ios/native_src/include/grpc/module.modulemap. struct(child_errors = [], msg = "Unexpected prefix tokens found at end of module member block. tokens: [struct(type = \"reserved\", value = \"module\"), struct(type = \"operator\", value = \"*\"), struct(type = \"curly_bracket_open\", value = None)]")
ERROR: Analysis of target '//Telegram:Telegram' failed; build aborted:
INFO: Elapsed time: 44.613s
INFO: 0 processes.
INFO: Build Event Protocol files produced successfully.

[Error] Generate[FAIL]: 44.6306s General failure: Bazel aspects could not be built.

It looks like a modulemap in one of the transitive deps has some unexpected syntax. rules_spm uses a modulemap parser implemented in Starlark. Let me see if I can sort this out.

finally found a working approach to using bazel and firebase

@vikage got it working successfully - here
https://thanhvu.dev/2022/04/16/migrating-ios-project-to-bazel-part-6-tich-hop-firebase-crashlytics/

demo app
https://github.com/vikage/DemoBazel/blob/firebase/Thirdparty/Firebase/BUILD

going to abandon the spm / xcframework approach for now.