Skip to content

Commit

Permalink
Framework imports, protos with deps
Browse files Browse the repository at this point in the history
Add an example where a proto imports another proto. This addtionally
exports the Protobuf runtime as a framework into rules_ios.

proto_map.txt - this file is used to map protobuf imports to framework
names in rules_ios
  • Loading branch information
jerrymarino committed Feb 7, 2022
1 parent e492188 commit 5f310d9
Show file tree
Hide file tree
Showing 8 changed files with 169 additions and 32 deletions.
6 changes: 5 additions & 1 deletion WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,14 @@ http_archive(
urls = ["https://github.com/rules-proto-grpc/rules_proto_grpc/archive/4.1.1.tar.gz"],
)

load("@rules_proto_grpc//:repositories.bzl", "rules_proto_grpc_toolchains", "rules_proto_grpc_repos")
load("@rules_proto_grpc//:repositories.bzl", "rules_proto_grpc_repos", "rules_proto_grpc_toolchains")

rules_proto_grpc_toolchains()

rules_proto_grpc_repos()

load("@rules_proto//proto:repositories.bzl", "rules_proto_dependencies", "rules_proto_toolchains")

rules_proto_dependencies()

rules_proto_toolchains()
57 changes: 28 additions & 29 deletions tests/ios/frameworks/protos/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,40 +1,39 @@
load("@rules_proto_grpc//objc:defs.bzl", "objc_proto_compile")
load("@rules_proto_grpc//internal:filter_files.bzl", "filter_files")
load("//rules:framework.bzl", "apple_framework")
load("proto_library.bzl", "apple_proto_libary")
load("//rules:framework.bzl", "apple_framework", "apple_framework_packaging")

exports_files(["*.proto"])

proto_library(
name = "sample_proto",
srcs = ["sample.proto"],
# Export protobuf into a framework module
objc_library(
name = "Protobuf_hdrs",
hdrs = ["Protobuf/module.modulemap"],
)

objc_proto_compile(
name = "sample_objc_proto",
protos = [":sample_proto"],
apple_framework_packaging(
name = "Protobuf",
framework_name = "Protobuf",
transitive_deps = [],
deps = [
"Protobuf_hdrs",
"@com_github_protocolbuffers_protobuf//:objectivec",
],
)

# Filter files to sources and headers
filter_files(
name = "sample_objc_proto.m",
extensions = ["m"],
target = "sample_objc_proto",
# Note: the proto_map.txt needs to correspend with the names here
# Specifically calling these Sample or SampleProto
apple_proto_libary(
name = "Sample",
proto_srcs = ["sample.proto"],
deps = [":Dep"],
)

filter_files(
name = "sample_objc_proto.h",
extensions = ["h"],
target = "sample_objc_proto",
apple_proto_libary(
name = "Dep",
proto_srcs = ["dep.proto"],
)

apple_framework(
name = "SampleProtoFramework",
srcs = [
":sample_objc_proto.h",
":sample_objc_proto.m",
],
includes = ["sample_objc_proto"],
platforms = {"ios": "10.0"},
visibility = ["//visibility:public"],
deps = ["@com_google_protobuf//:protobuf_objc"],
name = "Some",
srcs = ["Some.swift"],
objc_defines = ["GPB_USE_PROTOBUF_FRAMEWORK_IMPORTS=1"],
swift_defines = ["GPB_USE_PROTOBUF_FRAMEWORK_IMPORTS=1"],
deps = ["Sample"],
)
51 changes: 51 additions & 0 deletions tests/ios/frameworks/protos/Protobuf/module.modulemap
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
framework module Protobuf {
header "GPBAny.pbobjc.h"
header "GPBApi.pbobjc.h"
header "GPBDuration.pbobjc.h"
header "GPBEmpty.pbobjc.h"
header "GPBFieldMask.pbobjc.h"
header "GPBSourceContext.pbobjc.h"
header "GPBStruct.pbobjc.h"
header "GPBTimestamp.pbobjc.h"
header "GPBType.pbobjc.h"
header "GPBWrappers.pbobjc.h"
header "GPBArray.h"
header "GPBBootstrap.h"
header "GPBCodedInputStream.h"
header "GPBCodedOutputStream.h"
header "GPBDescriptor.h"
header "GPBDictionary.h"
header "GPBExtensionInternals.h"
header "GPBExtensionRegistry.h"
header "GPBMessage.h"
header "GPBProtocolBuffers.h"
header "GPBProtocolBuffers_RuntimeSupport.h"
header "GPBRootObject.h"
header "GPBRuntimeTypes.h"
header "GPBUnknownField.h"
header "GPBUnknownFieldSet.h"
header "GPBUtilities.h"
header "GPBWellKnownTypes.h"
header "GPBWireFormat.h"
header "Any.pbobjc.h"
header "Api.pbobjc.h"
header "Duration.pbobjc.h"
header "Empty.pbobjc.h"
header "FieldMask.pbobjc.h"
header "SourceContext.pbobjc.h"
header "Struct.pbobjc.h"
header "Timestamp.pbobjc.h"
header "Type.pbobjc.h"
header "Wrappers.pbobjc.h"
header "GPBArray_PackagePrivate.h"
header "GPBCodedInputStream_PackagePrivate.h"
header "GPBCodedOutputStream_PackagePrivate.h"
header "GPBDescriptor_PackagePrivate.h"
header "GPBDictionary_PackagePrivate.h"
header "GPBMessage_PackagePrivate.h"
header "GPBRootObject_PackagePrivate.h"
header "GPBUnknownFieldSet_PackagePrivate.h"
header "GPBUnknownField_PackagePrivate.h"
header "GPBUtilities_PackagePrivate.h"
export *
}
5 changes: 5 additions & 0 deletions tests/ios/frameworks/protos/Some.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import Sample

func main() {
print(Sample())
}
7 changes: 7 additions & 0 deletions tests/ios/frameworks/protos/dep.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
syntax = "proto3";

package example;

message Dep {
string name = 1;
}
62 changes: 62 additions & 0 deletions tests/ios/frameworks/protos/proto_library.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
load("//rules:framework.bzl", "apple_framework")
load("@rules_proto_grpc//objc:defs.bzl", "objc_proto_compile")
load("@rules_proto_grpc//internal:filter_files.bzl", "filter_files")

## This macro generates a apple_framework _per_ proto_srcs

def apple_proto_libary(name, proto_srcs, use_proto_map = True, deps = []):
native.proto_library(
name = name + "_proto",
srcs = proto_srcs,
# Internal implementation detail: assume all deps are protos
deps = [d + "_proto" for d in deps],
)

# proto_map.txt needs to correspend with the names used in rules_ios, this
# allows the devs to import them as as such
if use_proto_map:
extra_protoc_args = [
"--objc_opt",
# In-practice this might be generated by a repository_rule or a
# build rule - in that case just put the file here:
# e.g. external/my_protos/proto_map.txt
"named_framework_to_proto_path_mappings_path=tests/ios/frameworks/protos/proto_map.txt",
]
extra_protoc_files = [
"proto_map.txt",
]
else:
extra_protoc_args = []
extra_protoc_files = []

objc_proto_compile(
name = name + "_objc_proto",
protos = [name + "_proto"],
extra_protoc_args = extra_protoc_args,
extra_protoc_files = extra_protoc_files,
verbose = 2,
)

# Filter files to sources and headers
filter_files(
name = name + ".pbobjc.m",
target = name + "_objc_proto",
extensions = ["m"],
)

# This has to correspond with rules_ios
filter_files(
name = name + ".pbobjc.h",
target = name + "_objc_proto",
extensions = ["h"],
)

apple_framework(
name = name,
srcs = [name + ".pbobjc.m", name + ".pbobjc.h"],
objc_defines = ["GPB_USE_PROTOBUF_FRAMEWORK_IMPORTS=1"],
swift_defines = ["GPB_USE_PROTOBUF_FRAMEWORK_IMPORTS=1"],
deps = [":Protobuf"] + deps,
platforms = {"ios": "10.0"},
visibility = ["//visibility:public"],
)
2 changes: 2 additions & 0 deletions tests/ios/frameworks/protos/proto_map.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Sample:tests/ios/frameworks/protos/sample.proto
Dep:tests/ios/frameworks/protos/dep.proto
11 changes: 9 additions & 2 deletions tests/ios/frameworks/protos/sample.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ syntax = "proto3";

package example;

message Thing {
// Note: Need to import these in a way protoc can deal with
// e.g. this is root relative
// import "dep.proto";

import "tests/ios/frameworks/protos/dep.proto";

message Sample {
string name = 1;
}
Dep dep = 2;
}

0 comments on commit 5f310d9

Please sign in to comment.