├── .gitignore ├── .travis.yml ├── BUILD ├── CHANGES.md ├── DEPENDENCIES.md ├── LICENSE ├── Makefile ├── README.md ├── WORKSPACE ├── closure ├── BUILD ├── README.md └── rules.bzl ├── cpp ├── BUILD ├── README.md ├── deps.bzl ├── generate_cc.bzl ├── grpc_archive.bzl └── rules.bzl ├── csharp ├── BUILD ├── README.md ├── deps.bzl └── rules.bzl ├── examples ├── extra_args │ ├── BUILD │ └── person.proto ├── helloworld │ ├── Makefile │ ├── closure │ │ ├── BUILD │ │ ├── greeter.js │ │ └── greeter_test.js │ ├── cpp │ │ ├── BUILD │ │ ├── greeter_client.cc │ │ ├── greeter_client.h │ │ ├── greeter_client_main.cc │ │ ├── greeter_client_test.cc │ │ └── greeter_server.cc │ ├── csharp │ │ ├── GreeterClient │ │ │ ├── BUILD │ │ │ ├── GreeterClientTest.cs │ │ │ └── Program.cs │ │ └── GreeterServer │ │ │ ├── BUILD │ │ │ └── Program.cs │ ├── go │ │ ├── client │ │ │ ├── BUILD │ │ │ └── main.go │ │ ├── greeter_test │ │ │ ├── BUILD │ │ │ └── greeter_test.go │ │ └── server │ │ │ ├── BUILD │ │ │ └── main.go │ ├── grpc_gateway │ │ ├── BUILD │ │ ├── greeter.go │ │ ├── greeter_test.go │ │ └── helloworld.proto │ ├── java │ │ └── org │ │ │ └── pubref │ │ │ └── rules_protobuf │ │ │ └── examples │ │ │ └── helloworld │ │ │ ├── client │ │ │ ├── BUILD │ │ │ ├── HelloWorldClient.java │ │ │ └── HelloWorldClientTest.java │ │ │ └── server │ │ │ ├── BUILD │ │ │ ├── HelloWorldServer.java │ │ │ └── HelloWorldServerTest.java │ ├── node │ │ ├── BUILD │ │ ├── greeter_client.js │ │ └── greeter_server.js │ ├── proto │ │ ├── BUILD │ │ └── helloworld.proto │ └── python │ │ ├── BUILD │ │ ├── greeter_client.py │ │ ├── greeter_server.py │ │ └── test_greeter_server.py ├── proto │ ├── BUILD │ ├── README.md │ └── common.proto └── wkt │ └── go │ ├── BUILD │ ├── wkt.proto │ └── wkt_test.go ├── go ├── BUILD ├── README.md ├── deps.bzl └── rules.bzl ├── gogo ├── BUILD ├── README.md ├── deps.bzl └── rules.bzl ├── grpc_gateway ├── BUILD ├── README.md ├── deps.bzl └── rules.bzl ├── images └── wtfcat.png ├── java ├── BUILD ├── README.md ├── deps.bzl ├── grpc-java-maven-deps │ ├── BUILD │ ├── Hello.java │ └── WORKSPACE └── rules.bzl ├── node ├── BUILD ├── README.md ├── deps.bzl ├── package.json └── rules.bzl ├── objc ├── BUILD ├── README.md ├── deps.bzl └── rules.bzl ├── protobuf ├── BUILD ├── README.md ├── build_file │ ├── cares.BUILD │ └── com_github_madler_zlib.BUILD ├── deps.bzl ├── internal │ ├── github_archive.bzl │ ├── proto_compile.bzl │ ├── proto_dependencies.bzl │ ├── proto_http_archive.bzl │ ├── proto_language.bzl │ ├── proto_repositories.bzl │ └── require.bzl └── rules.bzl ├── python ├── BUILD ├── README.md ├── deps.bzl ├── requirements.txt └── rules.bzl ├── ruby ├── BUILD ├── README.md ├── deps.bzl └── rules.bzl └── tests ├── build_in_workspace_root ├── BUILD ├── WORKSPACE └── foo │ └── bar.proto ├── custom_go_importpath ├── BUILD ├── api.proto └── main.go ├── external_proto_library ├── .gitignore ├── BUILD ├── README.md ├── WORKSPACE └── message.proto ├── generated_proto_file ├── BUILD ├── complicated │ ├── complicated.proto │ └── complicated_copy.proto ├── simple.proto └── subdir │ └── simple.proto ├── gogo ├── BUILD ├── WORKSPACE ├── api.proto ├── client.go ├── gogo_test.sh └── server.go ├── proto_file_in_subdirectory ├── BUILD ├── README.md ├── foo │ └── bar │ │ └── baz.proto └── qux_test.cc └── with_grpc_false ├── BUILD └── message.proto /.gitignore: -------------------------------------------------------------------------------- 1 | # Ignore all bazel-* symlinks. There is no full list since this can change 2 | # based on the name of the directory bazel is cloned into. 3 | /bazel-* 4 | /tests/build_in_workspace_root/bazel-* 5 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | dist: trusty 2 | sudo: required 3 | osx_image: xcode8 4 | language: java 5 | 6 | os: 7 | - linux 8 | - osx 9 | 10 | env: 11 | - V=0.14.0 12 | - V=0.13.0 13 | - V=0.12.0 14 | - V=0.8.0 15 | 16 | before_install: 17 | - OS=linux 18 | - ARCH=x86_64 19 | - if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then OS=darwin; fi 20 | - GH_BASE="https://github.com/bazelbuild/bazel/releases/download/$V" 21 | - GH_ARTIFACT="bazel-$V-installer-$OS-$ARCH.sh" 22 | - CI_BASE="http://ci.bazel.io/job/Bazel/JAVA_VERSION=1.8,PLATFORM_NAME=$OS-$ARCH/lastSuccessfulBuild/artifact/output/ci" 23 | - CI_ARTIFACT="bazel--installer.sh" 24 | - URL="$GH_BASE/$GH_ARTIFACT" 25 | - if [[ "$V" == "HEAD" ]]; then CI_ARTIFACT="`wget -qO- $CI_BASE | grep -o 'bazel-[-_a-zA-Z0-9\.]*-installer.sh' | uniq`"; fi 26 | - if [[ "$V" == "HEAD" ]]; then URL="$CI_BASE/$CI_ARTIFACT"; fi 27 | - echo $URL 28 | - wget -O install.sh $URL 29 | - chmod +x install.sh 30 | - ./install.sh --user 31 | - rm -f install.sh 32 | 33 | script: 34 | - make all 35 | 36 | notifications: 37 | email: false 38 | -------------------------------------------------------------------------------- /BUILD: -------------------------------------------------------------------------------- 1 | load("@io_bazel_rules_go//go:def.bzl", "go_prefix") 2 | 3 | go_prefix("github.com/pubref/rules_protobuf") 4 | 5 | 6 | load("//protobuf:rules.bzl", "proto_dependencies") 7 | 8 | proto_dependencies( 9 | name = "deps", 10 | format = "markdown", 11 | ) 12 | -------------------------------------------------------------------------------- /CHANGES.md: -------------------------------------------------------------------------------- 1 | ## v0.8.0 (Wed Sep 6 2017) 2 | 3 | This is a significant (possibly overreaching) update that brings grpc 4 | support up to latest. rules_protobuf should also load much faster 5 | than previously. Some of the names of external workspaces have 6 | changed that may break your build (for example, string references such 7 | as 'external/com_github_google_protobuf/src' should be migrated to 8 | 'external/com_google_protobuf/src' in an 'imports' attribute). 9 | 10 | * Introduce cpp/grpc_repository.bzl to setup a custom @com_google_grpc 11 | external workspace (no longer using git_repository#init_submodules). 12 | This pulls down grpc/grpc from github as @com_google_grpc_base, sets 13 | up a mirrored external workspace in @com_google_grpc that symlinks 14 | to @com_google_grpc_base, then does a "git submodule the hard way" for 15 | c-ares, and installs a patched version of generate_cc.bzl. 16 | * Rename repository @com_github_google_protobuf to @com_google_protobuf. 17 | * Rename repository @com_github_grpc_grpc to @com_google_grpc. 18 | * Rename repository @gtest to @com_google_googletest. 19 | * Updated grpc/grpc to 1.6.1. 20 | * Updated madler/zlib to 1.2.11. 21 | * Updated boringssl to master-with-bazel Sep 2 2017. 22 | * Updated nuget deps to 1.6.0. 23 | * Adding binding to //external:protocol_compiler (that's what with grpc repo wants). 24 | * Remove binding //external:protoc (now //external:protocol_compiler). 25 | * Migrated all git_repository repository_rules to http_archive 26 | (faster). 27 | 28 | ## v0.7.2 (Fri Jul 14 2017) 29 | 30 | * Updated boringssl to latest chromium-stable (@pcj) 31 | * Updated grpc-java to 1.4.0 (@zhexuany) 32 | * Fixed broken links in README (@mikesamuel) 33 | * Improve gogo{fast,faster,slick}_proto_compile (@timpalpant) 34 | * Support grpc_gateway 1.2 (@pcj) 35 | * Improved grpc_gateway documentation (@prestonvanloon) 36 | * Update grpc go t0 1.2.1 (@clownpriest) 37 | * Overall documentation improvments (@cheister) 38 | * Fixed require bug (@raraujosc) 39 | * Upgrade gogo deps (@geeknoid) 40 | * Java documentation improvements (@bzz) 41 | * Add support for gogofast, gogofaster, and gogoslick (@douglas-reid) 42 | * Add -request_context option to grpc_gateway_library (@pcj) 43 | * Automatically export //java:grpc_compiletime_deps (@perezd) 44 | * Add importmap to gogo_proto_library (@gsf) 45 | * Documentation improvments (@Maverick-Crank-GRey) 46 | * Improved python support (Nikos Michalakis) 47 | * README update (@wiktortomczak) 48 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2016 PubRef.org 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); you 4 | may not use this file except in compliance with the License. You may 5 | obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 12 | implied. See the License for the specific language governing 13 | permissions and limitations under the License. 14 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | BAZEL := "bazel" 2 | 3 | STARTUP_FLAGS += --output_base="$HOME/.cache/bazel" 4 | STARTUP_FLAGS += --host_jvm_args=-Xmx500m 5 | STARTUP_FLAGS += --host_jvm_args=-Xms500m 6 | STARTUP_FLAGS += --batch 7 | 8 | BUILD_FLAGS += --verbose_failures 9 | BUILD_FLAGS += --spawn_strategy=standalone 10 | BUILD_FLAGS += --genrule_strategy=standalone 11 | BUILD_FLAGS += --local_resources=400,2,1.0 12 | BUILD_FLAGS += --worker_verbose 13 | BUILD_FLAGS += --strategy=Javac=worker 14 | BUILD_FLAGS += --strategy=Closure=worker 15 | #BUILD_FLAGS += --experimental_repository_cache="$HOME/.bazel_repository_cache" 16 | 17 | TESTFLAGS += $(BUILDFLAGS) 18 | TEST_FLAGS += --test_output=errors 19 | TEST_FLAGS += --test_strategy=standalone 20 | 21 | BAZEL_BUILD := $(BAZEL) $(STARTFLAGS) $(BAZELFLAGS) build $(BUILDFLAGS) 22 | BAZEL_TEST := $(BAZEL) $(STARTFLAGS) $(BAZELFLAGS) test $(TEST_FLAGS) 23 | #BAZEL_BUILD := $(BAZEL) build 24 | #BAZEL_TEST := $(BAZEL) test 25 | 26 | test_not_working_targets: 27 | $(BAZEL) test \ 28 | //examples/helloworld/node:client \ 29 | //examples/helloworld/node:server \ 30 | //examples/helloworld/csharp/GreeterClient:GreeterClientTest \ 31 | 32 | test_not_working_in_travis_targets: 33 | $(BAZEL) test \ 34 | //examples/helloworld/csharp/GreeterClient:GreeterClientTest \ 35 | //examples/helloworld/grpc_gateway:greeter_test \ 36 | 37 | # Python targets are not working (pip grpcio only compatible with 3.1.x) 38 | test_pip_dependent_targets: 39 | $(BAZEL_TEST) \ 40 | //examples/helloworld/python:test_greeter_server \ 41 | 42 | test_gogo: 43 | cd tests/gogo && $(BAZEL_TEST) :gogo_test 44 | 45 | all: build test 46 | 47 | build: external_proto_library_build workspace_root_build 48 | $(BAZEL_BUILD) \ 49 | //examples/extra_args:person_tar \ 50 | //examples/helloworld/node:client \ 51 | //examples/helloworld/node:server \ 52 | //examples/helloworld/go/client \ 53 | //examples/helloworld/go/server \ 54 | //examples/helloworld/grpc_gateway:swagger \ 55 | //tests/proto_file_in_subdirectory:protolib \ 56 | //tests/with_grpc_false:protos \ 57 | //tests/with_grpc_false:cpp \ 58 | //tests/with_grpc_false:java \ 59 | //tests/generated_proto_file:* \ 60 | //tests/custom_go_importpath:* \ 61 | 62 | test: test_pip_dependent_targets test_gogo 63 | $(BAZEL_TEST) \ 64 | //examples/helloworld/cpp:test \ 65 | //examples/helloworld/java/org/pubref/rules_protobuf/examples/helloworld/client:netty_test \ 66 | //examples/helloworld/java/org/pubref/rules_protobuf/examples/helloworld/server:netty_test \ 67 | //examples/wkt/go:wkt_test \ 68 | //tests/proto_file_in_subdirectory:test \ 69 | //examples/helloworld/closure:greeter_test \ 70 | 71 | 72 | external_proto_library_build: 73 | cd tests/external_proto_library && $(BAZEL_BUILD) :cc_gapi :go_gapi :java_gapi 74 | 75 | workspace_root_build: 76 | cd tests/build_in_workspace_root && $(BAZEL_BUILD) :bar_proto 77 | fmt: 78 | buildifier WORKSPACE 79 | buildifier BUILD 80 | find closure/ -name BUILD | xargs buildifier 81 | find cpp/ -name BUILD | xargs buildifier 82 | find csharp/ -name BUILD | xargs buildifier 83 | find examples/ -name BUILD | xargs buildifier 84 | find go/ -name BUILD | xargs buildifier 85 | find gogo/ -name BUILD | xargs buildifier 86 | find grpc_gateway/ -name BUILD | xargs buildifier 87 | find node/ -name BUILD | xargs buildifier 88 | find objc/ -name BUILD | xargs buildifier 89 | find protobuf/ -name BUILD | xargs buildifier 90 | find python/ -name BUILD | xargs buildifier 91 | find ruby/ -name BUILD | xargs buildifier 92 | find tests/ -name BUILD | xargs buildifier 93 | -------------------------------------------------------------------------------- /WORKSPACE: -------------------------------------------------------------------------------- 1 | workspace(name = "org_pubref_rules_protobuf") 2 | 3 | load("//protobuf:rules.bzl", "github_archive") 4 | 5 | # ================================================================ 6 | # Go support requires rules_go 7 | # ================================================================ 8 | 9 | github_archive( 10 | name = "io_bazel_rules_go", 11 | commit = "f676870c5caf8df559a51e7aa005d2ece148a03b", # 0.10.3 12 | org = "bazelbuild", 13 | repo = "rules_go", 14 | sha256 = "d1740b1a75d3c51f1c37e5a42ed032d113bdf1de35c393c609940af491ab6035", 15 | ) 16 | 17 | load("@io_bazel_rules_go//go:def.bzl", "go_rules_dependencies", "go_register_toolchains") 18 | 19 | go_rules_dependencies() 20 | 21 | go_register_toolchains() 22 | 23 | # ================================================================ 24 | # closure js_proto_library support requires rules_closure 25 | # ================================================================ 26 | 27 | 28 | github_archive( 29 | name = "io_bazel_rules_closure", 30 | commit = "21b757480a1e3a67f1a25a8f27a404fc751e1477", # 0.6.0 31 | org = "bazelbuild", 32 | repo = "rules_closure", 33 | sha256 = "84687d2bc01fe2a0b45ec906bee87c5d336767e00d8cc8d40236c8804d5d5ced", 34 | ) 35 | 36 | load("@io_bazel_rules_closure//closure:defs.bzl", "closure_repositories") 37 | 38 | closure_repositories(omit_com_google_protobuf=True) 39 | 40 | # ================================================================ 41 | # csharp_proto_library support requires rules_dotnet (forked) 42 | # ================================================================ 43 | 44 | github_archive( 45 | name = "io_bazel_rules_dotnet", 46 | commit = "1a6ca96fe05bca83782464453ac4657fb8ed8379", 47 | org = "bazelbuild", 48 | repo = "rules_dotnet", 49 | sha256 = "0f7d7f79bf543fdcce9ffebf422df2f858eae63367869b441d4d1005f279fa1f", 50 | ) 51 | 52 | load("@io_bazel_rules_dotnet//dotnet:csharp.bzl", "csharp_repositories") 53 | 54 | csharp_repositories() 55 | 56 | # ================================================================ 57 | # node_proto_library support requires rules_node 58 | # ================================================================ 59 | 60 | 61 | github_archive( 62 | name = "org_pubref_rules_node", 63 | # This commit is *not* on master but rather https://github.com/pubref/rules_node/pull/41. 64 | commit = "f990afc34168f81b034e642aa0dcb56320ed3988", 65 | org = "pubref", 66 | repo = "rules_node", 67 | sha256 = "a367add895f201595b618611dcf7bdd7723ffeed88c4dc327e30668d19c9d1e2", 68 | ) 69 | 70 | load("@org_pubref_rules_node//node:rules.bzl", "node_repositories", "yarn_modules") 71 | 72 | node_repositories() 73 | 74 | yarn_modules( 75 | name = "yarn_modules", 76 | package_json = "//node:package.json", 77 | ) 78 | 79 | # ================================================================ 80 | # Python GRPC support requires rules_python 81 | # ================================================================ 82 | 83 | github_archive( 84 | name = "io_bazel_rules_python", 85 | commit = "8b5d0683a7d878b28fffe464779c8a53659fc645", 86 | org = "bazelbuild", 87 | repo = "rules_python", 88 | sha256 = "40499c0a9d55f0c5deb245ed24733da805f05aaf6085cb39027ba486faf1d2e1", 89 | ) 90 | 91 | load("@io_bazel_rules_python//python:pip.bzl", "pip_repositories", "pip_import") 92 | 93 | pip_repositories() 94 | 95 | pip_import( 96 | name = "pip_grpcio", 97 | requirements = "//python:requirements.txt", 98 | ) 99 | 100 | load("@pip_grpcio//:requirements.bzl", pip_grpcio_install = "pip_install") 101 | 102 | pip_grpcio_install() 103 | 104 | 105 | # ================================================================ 106 | # Specific Languages Support 107 | # ================================================================ 108 | 109 | load("//protobuf:rules.bzl", "proto_repositories") 110 | 111 | proto_repositories() 112 | 113 | load("//cpp:rules.bzl", "cpp_proto_repositories") 114 | 115 | cpp_proto_repositories() 116 | 117 | load("//csharp:rules.bzl", "csharp_proto_repositories") 118 | 119 | csharp_proto_repositories() 120 | 121 | load("//java:rules.bzl", "java_proto_repositories", "nano_proto_repositories") 122 | 123 | java_proto_repositories( 124 | # Already picking these up from rules_closure 125 | excludes = [ 126 | "com_google_code_findbugs_jsr305", 127 | "com_google_errorprone_error_prone_annotations", 128 | ], 129 | ) 130 | #nano_proto_repositories() 131 | 132 | load("//go:rules.bzl", "go_proto_repositories") 133 | 134 | go_proto_repositories() 135 | 136 | load("//gogo:rules.bzl", "gogo_proto_repositories") 137 | 138 | gogo_proto_repositories() 139 | 140 | load("//grpc_gateway:rules.bzl", "grpc_gateway_proto_repositories") 141 | 142 | grpc_gateway_proto_repositories() 143 | 144 | load("//node:rules.bzl", "node_proto_repositories") 145 | 146 | node_proto_repositories() 147 | 148 | load("//objc:rules.bzl", "objc_proto_repositories") 149 | 150 | objc_proto_repositories() 151 | 152 | load("//python:rules.bzl", "py_proto_repositories") 153 | 154 | py_proto_repositories() 155 | 156 | load("//ruby:rules.bzl", "ruby_proto_repositories") 157 | 158 | ruby_proto_repositories() 159 | -------------------------------------------------------------------------------- /closure/BUILD: -------------------------------------------------------------------------------- 1 | package(default_visibility = ["//visibility:public"]) 2 | 3 | load("//protobuf:rules.bzl", "proto_language") 4 | 5 | proto_language( 6 | name = "closure", 7 | output_to_library = True, 8 | pb_file_extensions = [".js"], 9 | pb_options = [ 10 | "import_style=closure", 11 | "error_on_name_conflict", 12 | "binary", 13 | ], 14 | pb_plugin_name = "js", 15 | ) 16 | 17 | proto_language( 18 | name = "browser", 19 | pb_file_extensions = ["_pb.js"], 20 | pb_options = [ 21 | "import_style=browser", 22 | "error_on_name_conflict", 23 | "binary", 24 | ], 25 | pb_plugin_name = "js", 26 | ) 27 | -------------------------------------------------------------------------------- /closure/README.md: -------------------------------------------------------------------------------- 1 | # Closure Javascript Rules 2 | 3 | | Rule | Description | 4 | | ---: | :--- | 5 | | [closure_proto_repositories](#closure_proto_repositories) | Load WORKSPACE dependencies. | 6 | | [closure_proto_compile](#closure_proto_compile) | Generate closure js protobuf source files. | 7 | | [closure_proto_library](#closure_proto_library) | Generate and compiles closure js source files. | 8 | 9 | ## closure\_proto\_repositories 10 | 11 | Enable closure support by loading the dependencies in your workspace. 12 | 13 | > IMPORTANT: Closure rules require 14 | > [rules_closure](https://github.com/bazelbuild/rules_closure). 15 | 16 | ```python 17 | load("@org_pubref_rules_protobuf//closure:rules.bzl", "closure_proto_repositories") 18 | closure_proto_repositories() 19 | ``` 20 | 21 | ## closure\_proto\_compile 22 | 23 | This is a thin wrapper over the 24 | [proto_compile](../protobuf#proto_compile) rule having language 25 | `@org_pubref_rules_protobuf//closure`. 26 | 27 | ```python 28 | load("@org_pubref_rules_protobuf//closure:rules.bzl", "closure_proto_compile") 29 | 30 | closure_proto_compile( 31 | name = "protos", 32 | protos = ["message.proto"], 33 | ) 34 | ``` 35 | 36 | ```sh 37 | $ bazel build :protos 38 | Target //:protos up-to-date: 39 | bazel-genfiles/message.js 40 | ``` 41 | 42 | ## closure\_proto\_library 43 | 44 | Pass the set of protobuf source files to the `protos` attribute. 45 | 46 | ```python 47 | load("@org_pubref_rules_protobuf//closure:rules.bzl", "closure_proto_library") 48 | 49 | closure_proto_library( 50 | name = "protolib", 51 | protos = ["message.proto"], 52 | ) 53 | ``` 54 | 55 | ```sh 56 | $ bazel build :Helloworld 57 | Target //:Helloworld up-to-date: 58 | bazel-bin/protolib.js 59 | ``` 60 | 61 | --- 62 | 63 | Note that `closure_js_proto_library` is implemented in 64 | [rules_closure](https://github.com/bazelbuild/rules_closure#closure_js_proto_library). 65 | This rule is definitely a viable option but does not at the time of 66 | this writing support imports or proto::proto dependencies. 67 | -------------------------------------------------------------------------------- /closure/rules.bzl: -------------------------------------------------------------------------------- 1 | load("@io_bazel_rules_closure//closure:defs.bzl", "closure_js_library") 2 | load("//protobuf:rules.bzl", 3 | "proto_compile", 4 | "proto_repositories") 5 | 6 | def closure_proto_repositories( 7 | lang_requires = [ 8 | ], 9 | **kwargs): 10 | proto_repositories(lang_requires = lang_requires, **kwargs) 11 | 12 | def closure_proto_compile(langs = [str(Label("//closure"))], **kwargs): 13 | proto_compile(langs = langs, **kwargs) 14 | 15 | def closure_proto_library( 16 | name, 17 | langs = [str(Label("//closure"))], 18 | protos = [], 19 | imports = [], 20 | includes = [], 21 | excludes = [], 22 | inputs = [], 23 | output_to_workspace = False, 24 | proto_deps = [], 25 | protoc = None, 26 | pb_plugin = None, 27 | pb_options = [], 28 | proto_compile_args = {}, 29 | srcs = [], 30 | deps = [], 31 | verbose = 0, 32 | **kwargs): 33 | 34 | proto_compile_args += { 35 | "name": name + ".pb", 36 | "protos": protos, 37 | "deps": [dep + ".pb" for dep in proto_deps], 38 | "langs": langs, 39 | "imports": imports, 40 | "inputs": inputs, 41 | "includes": includes, 42 | "excludes": excludes, 43 | "pb_options": pb_options, 44 | "output_to_workspace": output_to_workspace, 45 | "verbose": verbose, 46 | } 47 | 48 | if protoc: 49 | proto_compile_args["protoc"] = protoc 50 | if pb_plugin: 51 | proto_compile_args["pb_plugin"] = pb_plugin 52 | 53 | proto_compile(**proto_compile_args) 54 | 55 | closure_js_library( 56 | name = name, 57 | internal_descriptors = [name + ".pb.descriptor_set"], 58 | srcs = srcs + [name + ".pb"], 59 | deps = depset(deps + proto_deps + [ 60 | "@io_bazel_rules_closure//closure/protobuf:jspb", 61 | ]).to_list(), 62 | suppress = [ 63 | "JSC_IMPLICITLY_NULLABLE_JSDOC", 64 | ], 65 | **kwargs) 66 | -------------------------------------------------------------------------------- /cpp/BUILD: -------------------------------------------------------------------------------- 1 | package(default_visibility = ["//visibility:public"]) 2 | 3 | load("//protobuf:rules.bzl", "proto_language") 4 | 5 | proto_language( 6 | name = "cpp", 7 | grpc_compile_deps = [ 8 | "@com_google_grpc//:grpc++", 9 | "@com_google_grpc//:grpc++_reflection", 10 | ], 11 | grpc_file_extensions = [ 12 | ".grpc.pb.h", 13 | ".grpc.pb.cc", 14 | ], 15 | grpc_plugin = "//external:protoc_gen_grpc_cpp", 16 | grpc_plugin_name = "grpc_cpp", 17 | pb_compile_deps = [ 18 | "//external:protobuf_clib", 19 | ], 20 | pb_file_extensions = [ 21 | ".pb.h", 22 | ".pb.cc", 23 | ], 24 | supports_grpc = True, 25 | ) 26 | 27 | -------------------------------------------------------------------------------- /cpp/README.md: -------------------------------------------------------------------------------- 1 | # C++ Rules 2 | 3 | | Rule | Description | 4 | | ---: | :--- | 5 | | [cpp_proto_repositories](#cpp_proto_repositories) | Load WORKSPACE dependencies. | 6 | | [cc_proto_compile](#cc_proto_compile) | Generate protobuf source files. | 7 | | [cc_proto_library](#cc_proto_library) | Generate and compiles protobuf source files. | 8 | 9 | ## cpp\_proto\_repositories 10 | 11 | Enable C++ support by loading the dependencies in your workspace. 12 | 13 | ```python 14 | load("@org_pubref_rules_protobuf//cpp:rules.bzl", "cpp_proto_repositories") 15 | cpp_proto_repositories() 16 | ``` 17 | 18 | ## cc\_proto\_compile 19 | 20 | This is a thin wrapper over the 21 | [proto_compile](../protobuf#proto_compile) rule having language 22 | `@org_pubref_rules_protobuf//cpp`. 23 | 24 | ```python 25 | load("@org_pubref_rules_protobuf//cpp:rules.bzl", "cc_proto_compile") 26 | 27 | cc_proto_compile( 28 | name = "protos", 29 | protos = ["message.proto"], 30 | with_grpc = True, 31 | ) 32 | ``` 33 | 34 | ```sh 35 | $ bazel build :protos 36 | Target //:protos up-to-date: 37 | bazel-genfiles/message.pb.h 38 | bazel-genfiles/message.pb.cc 39 | bazel-genfiles/message.grpc.pb.h 40 | bazel-genfiles/message.grpc.pb.cc 41 | ``` 42 | 43 | ## cc\_proto\_library 44 | 45 | Pass the set of protobuf source files to the `protos` attribute. 46 | 47 | ```python 48 | load("@org_pubref_rules_protobuf//cpp:rules.bzl", "cc_proto_library") 49 | 50 | cc_proto_library( 51 | name = "protolib", 52 | protos = ["message.proto"], 53 | with_grpc = True, 54 | ) 55 | ``` 56 | 57 | ```sh 58 | $ bazel build :protolib 59 | $ bazel build --spawn_strategy=standalone :protolib 60 | Target //:protolib up-to-date: 61 | bazel-bin/libprotolib.a 62 | bazel-bin/libprotolib.so 63 | ``` 64 | 65 | > Note: there are some remaining issues with grpc++ compiling on linux 66 | > that may require disabling the sandbox via the 67 | > `--spawn_strategy=standalone` build option. See 68 | > https://github.com/pubref/rules_protobuf/issues/7 69 | 70 | When using the compiled library in other rules, `#include` the 71 | generated files relative to the `WORKSPACE` root. For example, the 72 | `//examples/helloworld/proto/helloworld.proto` functions can be loaded 73 | via: 74 | 75 | 76 | ```cpp 77 | #include 78 | 79 | #include "examples/helloworld/proto/helloworld.pb.h" 80 | #include "examples/helloworld/proto/helloworld.grpc.pb.h" 81 | ``` 82 | 83 | To get the list of required compile-time dependencies in other 84 | contexts for grpc-related code, load the list from the rules.bzl file: 85 | 86 | ```python 87 | load("@org_pubref_rules_protobuf//cpp:rules.bzl", "GRPC_COMPILE_DEPS") 88 | 89 | cc_library( 90 | name = "mylib", 91 | srcs = ['MyApp.cpp'], 92 | deps = [ 93 | ":protolib" 94 | ] + GRPC_COMPILE_DEPS, 95 | ) 96 | ``` 97 | 98 | Consult source files in the 99 | [examples/helloworld/cpp](../examples/helloworld/cpp) directory for 100 | additional information. 101 | -------------------------------------------------------------------------------- /cpp/deps.bzl: -------------------------------------------------------------------------------- 1 | CARES_VERSION = "3be1924221e1326df520f8498d704a5c4c8d0cce" # Jun 16, 2017 (1.13.0) 2 | 3 | DEPS = { 4 | 5 | "com_google_grpc": { 6 | "rule": "grpc_archive", 7 | "url": "https://github.com/grpc/grpc/archive/66b9770a8ad326c1ee0dbedc5a8f32a52a604567.tar.gz", # 1.10.1 8 | "sha256": "14c1d63217f829f3c23bf039a76c186d0886c5b5c64e7eced44764f0fc564e6a", 9 | "strip_prefix": "grpc-66b9770a8ad326c1ee0dbedc5a8f32a52a604567", 10 | }, 11 | 12 | "boringssl": { 13 | "rule": "http_archive", 14 | # master-with-bazel Fri Sep 01 15:09:13 2017 +0000 15 | "url": "https://boringssl.googlesource.com/boringssl/+archive/886e7d75368e3f4fab3f4d0d3584e4abfc557755.tar.gz", 16 | }, 17 | 18 | # libssl is required for c++ grpc where it is expected in 19 | # //external:libssl. This can be either boringssl or openssl. 20 | "libssl": { 21 | "rule": "bind", 22 | "actual": "@boringssl//:ssl", 23 | }, 24 | 25 | # C-library for zlib 26 | "com_github_madler_zlib": { 27 | "rule": "new_http_archive", 28 | "url": "https://github.com/madler/zlib/archive/cacf7f1d4e3d44d871b605da3b647f07d718623f.zip", #v1.2.11 29 | "sha256": "1cce3828ec2ba80ff8a4cac0ab5aa03756026517154c4b450e617ede751d41bd", 30 | "strip_prefix": "zlib-cacf7f1d4e3d44d871b605da3b647f07d718623f", 31 | "build_file": str(Label("//protobuf:build_file/com_github_madler_zlib.BUILD")), 32 | }, 33 | 34 | "com_github_cares_cares": { 35 | "rule": "new_http_archive", 36 | "url": "https://github.com/c-ares/c-ares/archive/%s.zip" % CARES_VERSION, 37 | "sha256": "932bf7e593d4683fce44fd26920f27d4f0c229113338e4f6d351e35d4d7c7a39", 38 | "strip_prefix": "c-ares-" + CARES_VERSION, 39 | "build_file": str(Label("//protobuf:build_file/cares.BUILD")), 40 | }, 41 | 42 | # grpc++ expects //external:cares 43 | "cares": { 44 | "rule": "bind", 45 | "actual": "@com_github_cares_cares//:ares", 46 | }, 47 | 48 | 49 | # grpc++ expects //external:zlib 50 | "zlib": { 51 | "rule": "bind", 52 | "actual": "@com_github_madler_zlib//:zlib", 53 | }, 54 | 55 | # grpc++ expects //external:nanopb 56 | "nanopb": { 57 | "rule": "bind", 58 | "actual": "@com_google_grpc//third_party/nanopb", 59 | }, 60 | 61 | # Bind the executable cc_binary grpc plugin into 62 | # //external:protoc_gen_grpc_cpp. Expects 63 | # //external:protobuf_compiler. TODO: is it really necessary to 64 | # bind it in external? 65 | "protoc_gen_grpc_cpp": { 66 | "rule": "bind", 67 | "actual": "@com_google_grpc//:grpc_cpp_plugin", 68 | }, 69 | 70 | # GTest is for our own internal cc tests. 71 | "com_google_googletest": { 72 | "rule": "http_archive", 73 | "url": "https://github.com/google/googletest/archive/7c6353d29a147cad1c904bf2957fd4ca2befe135.zip", # master Sept 1 2017 74 | "sha256": "f87029f647276734ef076785f76652347993b6d13ac1cbb2d2e976e16d2f8137", 75 | "strip_prefix": "googletest-7c6353d29a147cad1c904bf2957fd4ca2befe135", 76 | }, 77 | 78 | } 79 | -------------------------------------------------------------------------------- /cpp/generate_cc.bzl: -------------------------------------------------------------------------------- 1 | """Generates C++ grpc stubs from proto_library rules. 2 | 3 | This is an internal rule used by cc_grpc_library, and shouldn't be used 4 | directly. 5 | 6 | This file becomes symlinked to replace the original 7 | @com_google_grpc//bazel/generate_cc.bzl, which does not seem to work 8 | out of the box. IF YOU UPDATE TO A NEWER VERSION OF GRPC/GRPC, THERE 9 | MAY BE CHANGES TO THAT FILE WHICH MIGHT EITHER BREAK THIS OR MAKE IT 10 | OBSOLETE. 11 | 12 | Modifications made for rules_protobuf: out of the box, 13 | generate_cc_impl does not seem to work if run within the context of an 14 | external workspace. 15 | 16 | The first change is that we need to adjust the output paths to account 17 | for the external workspace_root. Previously, outs is computed by 18 | slicing off the value of ctx.label.package prefix from proto.path. We 19 | ALSO need to slice off the workspace_root, if it exists. Therefore, 20 | we've created a new int field 'prefix_len' that is the sum of these 21 | two lengths. 22 | 23 | The second change is that we need protoc to evaluate within the 24 | context of the workspace_root rather than the execroot. To do this, 25 | we shift into the external workspace with a 'cd 26 | external/com_google_grpc', and adjust dir_out, plugin_out, 27 | cpp_out etc to be relative to the forward shifted position. 28 | 29 | Third change: Windows compatibility by not including : when unnecessary. 30 | 31 | """ 32 | 33 | def generate_cc_impl(ctx): 34 | """Implementation of the generate_cc rule.""" 35 | protos = [f for src in ctx.attr.srcs for f in src.proto.direct_sources] 36 | includes = [f for src in ctx.attr.srcs for f in src.proto.transitive_imports] 37 | outs = [] 38 | label_len = len(ctx.label.package) + 1 39 | workspace_len = len(ctx.label.workspace_root) + 1 40 | prefix_len = label_len + workspace_len 41 | 42 | if ctx.executable.plugin: 43 | outs += [proto.path[prefix_len:-len(".proto")] + ".grpc.pb.h" for proto in protos] 44 | outs += [proto.path[prefix_len:-len(".proto")] + ".grpc.pb.cc" for proto in protos] 45 | if ctx.attr.generate_mock: 46 | outs += [proto.path[prefix_len:-len(".proto")] + "_mock.grpc.pb.h" for proto in protos] 47 | else: 48 | outs += [proto.path[prefix_len:-len(".proto")] + ".pb.h" for proto in protos] 49 | outs += [proto.path[prefix_len:-len(".proto")] + ".pb.cc" for proto in protos] 50 | 51 | out_files = [ctx.new_file(out) for out in outs] 52 | dir_out = "../../%s/%s" % (str(ctx.genfiles_dir.path), ctx.label.workspace_root) 53 | 54 | arguments = [] 55 | if ctx.executable.plugin: 56 | arguments += ["--plugin=protoc-gen-PLUGIN=../../" + ctx.executable.plugin.path] 57 | flags = list(ctx.attr.flags) 58 | if ctx.attr.generate_mock: 59 | flags.append("generate_mock_code=true") 60 | if flags: 61 | arguments += ["--PLUGIN_out=" + ",".join(flags) + ":" + dir_out] 62 | else: 63 | arguments += ["--PLUGIN_out=" + dir_out] 64 | additional_input = [ctx.executable.plugin] 65 | else: 66 | if ctx.attr.flags: 67 | arguments += ["--cpp_out=" + ",".join(ctx.attr.flags) + ":" + dir_out] 68 | else: 69 | arguments += ["--cpp_out=" + dir_out] 70 | additional_input = [] 71 | 72 | #arguments += ["-I{0}={0}".format(include.path) for include in includes] 73 | arguments += [proto.path[workspace_len:] for proto in protos] 74 | 75 | # create a list of well known proto files if the argument is non-None 76 | well_known_proto_files = [] 77 | if ctx.attr.well_known_protos: 78 | f = ctx.attr.well_known_protos.files.to_list()[0].dirname 79 | if f != "external/com_google_protobuf/src/google/protobuf": 80 | print("Error: Only @com_google_protobuf//:well_known_protos is supported") 81 | else: 82 | # f points to "external/com_google_protobuf/src/google/protobuf" 83 | # add -I argument to protoc so it knows where to look for the proto files. 84 | arguments += ["-I{0}".format(f + "/../..")] 85 | well_known_proto_files = [f for f in ctx.attr.well_known_protos.files] 86 | 87 | proto_cmd = "../../%s " % ctx.executable._protoc.path 88 | proto_cmd += " ".join(arguments) 89 | 90 | cmds = [ 91 | "cd %s" % ctx.label.workspace_root, 92 | proto_cmd 93 | ] 94 | 95 | ### print("CMDS> %r" % cmds) 96 | 97 | ctx.action( 98 | inputs = protos + includes + additional_input + well_known_proto_files + [ctx.executable._protoc], 99 | outputs = out_files, 100 | command = " && ".join(cmds), 101 | ) 102 | 103 | return struct(files=depset(out_files)) 104 | 105 | _generate_cc = rule( 106 | attrs = { 107 | "srcs": attr.label_list( 108 | mandatory = True, 109 | non_empty = True, 110 | providers = ["proto"], 111 | ), 112 | "plugin": attr.label( 113 | executable = True, 114 | providers = ["files_to_run"], 115 | cfg = "host", 116 | ), 117 | "flags": attr.string_list( 118 | mandatory = False, 119 | allow_empty = True, 120 | ), 121 | "well_known_protos" : attr.label( 122 | mandatory = False, 123 | ), 124 | "generate_mock" : attr.bool( 125 | default = False, 126 | mandatory = False, 127 | ), 128 | "_protoc": attr.label( 129 | default = Label("//external:protocol_compiler"), 130 | executable = True, 131 | cfg = "host", 132 | ), 133 | }, 134 | # We generate .h files, so we need to output to genfiles. 135 | output_to_genfiles = True, 136 | implementation = generate_cc_impl, 137 | ) 138 | 139 | def generate_cc(well_known_protos, **kwargs): 140 | if well_known_protos: 141 | _generate_cc(well_known_protos="@com_google_protobuf//:well_known_protos", **kwargs) 142 | else: 143 | _generate_cc(**kwargs) 144 | -------------------------------------------------------------------------------- /cpp/grpc_archive.bzl: -------------------------------------------------------------------------------- 1 | def _execute(rtx, cmds, print_result = False, keep_going = True): 2 | result = rtx.execute(cmds) 3 | if result.return_code: 4 | if not keep_going: 5 | fail("$ %s failed (%s)" % (" ".join(cmds), result.stderr)) 6 | if print_result: 7 | print("$ %r\n%s" % (cmds, result.stdout)) 8 | return result 9 | 10 | 11 | # The grpc repository needs enough work that we need a custom repository 12 | # rule to set it up. 13 | def _grpc_archive_impl(rtx): 14 | 15 | rtx.download_and_extract( 16 | rtx.attr.url, 17 | output = "", 18 | sha256 = rtx.attr.sha256, 19 | type = rtx.attr.type, 20 | stripPrefix = rtx.attr.strip_prefix, 21 | ) 22 | 23 | _execute(rtx, ["rm", "bazel/generate_cc.bzl"]) 24 | 25 | rtx.symlink(rtx.path(rtx.attr.generate_cc_bzl), "bazel/generate_cc.bzl") 26 | rtx.file("WORKSPACE", "workspace(name = 'com_google_grpc')") 27 | 28 | # Http archive that patches the grpc repository so that the reflection++ targets compile 29 | # as an external workspace. 30 | # 31 | grpc_archive = repository_rule( 32 | implementation = _grpc_archive_impl, 33 | attrs = { 34 | "url": attr.string(), 35 | "sha256": attr.string(), 36 | "strip_prefix": attr.string(), 37 | "type": attr.string(), 38 | "generate_cc_bzl": attr.label( 39 | default = Label("@org_pubref_rules_protobuf//cpp:generate_cc.bzl", relative_to_caller_repository=True) 40 | ), 41 | }, 42 | ) 43 | -------------------------------------------------------------------------------- /cpp/rules.bzl: -------------------------------------------------------------------------------- 1 | load("//protobuf:rules.bzl", "proto_compile", "proto_repositories") 2 | load("//cpp:deps.bzl", "DEPS") 3 | load("//cpp:grpc_archive.bzl", "grpc_archive") 4 | 5 | def cpp_proto_repositories( 6 | lang_deps = DEPS, 7 | lang_requires = [ 8 | "com_google_grpc", 9 | "com_github_cares_cares", 10 | "com_google_googletest", 11 | "com_github_madler_zlib", 12 | "cares", 13 | "zlib", 14 | "nanopb", 15 | "boringssl", 16 | "libssl", 17 | "protoc_gen_grpc_cpp", 18 | ], **kwargs): 19 | 20 | rem = proto_repositories(lang_deps = lang_deps, 21 | lang_requires = lang_requires, 22 | **kwargs) 23 | 24 | for dep in rem: 25 | rule = dep.pop("rule") 26 | if "grpc_archive" == rule: 27 | grpc_archive(**dep) 28 | else: 29 | fail("Unknown loading rule %s for %s" % (rule, dep)) 30 | 31 | PB_COMPILE_DEPS = [ 32 | "//external:protobuf_clib", 33 | ] 34 | 35 | GRPC_COMPILE_DEPS = PB_COMPILE_DEPS + [ 36 | "@com_google_grpc//:grpc++", 37 | "@com_google_grpc//:grpc++_reflection", 38 | ] 39 | 40 | def cpp_proto_compile(langs = [str(Label("//cpp"))], **kwargs): 41 | proto_compile(langs = langs, **kwargs) 42 | 43 | cc_proto_compile = cpp_proto_compile 44 | 45 | def cpp_proto_library( 46 | name, 47 | langs = [str(Label("//cpp"))], 48 | protos = [], 49 | imports = [], 50 | inputs = [], 51 | proto_deps = [], 52 | output_to_workspace = False, 53 | protoc = None, 54 | 55 | pb_plugin = None, 56 | pb_options = [], 57 | 58 | grpc_plugin = None, 59 | grpc_options = [], 60 | 61 | proto_compile_args = {}, 62 | with_grpc = True, 63 | srcs = [], 64 | deps = [], 65 | verbose = 0, 66 | **kwargs): 67 | 68 | if with_grpc: 69 | compile_deps = GRPC_COMPILE_DEPS 70 | else: 71 | compile_deps = PB_COMPILE_DEPS 72 | 73 | proto_compile_args += { 74 | "name": name + ".pb", 75 | "protos": protos, 76 | "deps": [dep + ".pb" for dep in proto_deps], 77 | "langs": langs, 78 | "imports": imports, 79 | "inputs": inputs, 80 | "pb_options": pb_options, 81 | "grpc_options": grpc_options, 82 | "output_to_workspace": output_to_workspace, 83 | "verbose": verbose, 84 | "with_grpc": with_grpc, 85 | } 86 | 87 | if protoc: 88 | proto_compile_args["protoc"] = protoc 89 | if pb_plugin: 90 | proto_compile_args["pb_plugin"] = pb_plugin 91 | if grpc_plugin: 92 | proto_compile_args["grpc_plugin"] = grpc_plugin 93 | 94 | proto_compile(**proto_compile_args) 95 | 96 | native.cc_library( 97 | name = name, 98 | srcs = srcs + [name + ".pb"], 99 | deps = depset(deps + proto_deps + compile_deps).to_list(), 100 | **kwargs) 101 | 102 | # Alias for cpp_proto_library 103 | cc_proto_library = cpp_proto_library 104 | -------------------------------------------------------------------------------- /csharp/BUILD: -------------------------------------------------------------------------------- 1 | package(default_visibility = ["//visibility:public"]) 2 | 3 | load("//protobuf:rules.bzl", "proto_language") 4 | 5 | proto_language( 6 | name = "csharp", 7 | grpc_compile_deps = [ 8 | "@nuget_grpc//:core", 9 | ], 10 | grpc_file_extensions = ["Grpc.cs"], 11 | grpc_plugin = "@com_google_grpc//:grpc_csharp_plugin", 12 | grpc_plugin_name = "grpc_csharp", 13 | output_file_style = "pascal", 14 | output_to_libdir = True, 15 | pb_compile_deps = [ 16 | "@nuget_google_protobuf//:libnet45", 17 | ], 18 | pb_file_extensions = [".cs"], 19 | supports_grpc = True, 20 | ) 21 | -------------------------------------------------------------------------------- /csharp/README.md: -------------------------------------------------------------------------------- 1 | # C# Rules 2 | 3 | | Rule | Description | 4 | | ---: | :--- | 5 | | [csharp_proto_repositories](#csharp_proto_repositories) | Load WORKSPACE dependencies. | 6 | | [csharp_proto_compile](#csharp_proto_compile) | Generate protobuf source files. | 7 | | [csharp_proto_library](#csharp_proto_library) | Generate and compiles protobuf source files. | 8 | 9 | ## csharp\_proto\_repositories 10 | 11 | Enable C# support by loading the dependencies in your workspace. 12 | 13 | > IMPORTANT: C# currently requires a forked version of 14 | > [rules_dotnet](https://github.com/bazelbuild/rules_dotnet) having 15 | > support for the `new_nuget_package` rule found in 16 | > https://github.com/pcj/rules_dotnet. This will change once these 17 | > changes are integrated upstream. 18 | 19 | ```python 20 | git_repository( 21 | name = "io_bazel_rules_dotnet", 22 | remote = "https://github.com/pcj/rules_dotnet.git", 23 | commit = "ed06be64a1df2b446516bf0890bd0b4d41af381a", 24 | ) 25 | csharp_repositories(use_local_mono = False) # or true, if you prefer 26 | 27 | load("@org_pubref_rules_protobuf//csharp:rules.bzl", "csharp_proto_repositories") 28 | csharp_proto_repositories() 29 | ``` 30 | 31 | ## csharp\_proto\_compile 32 | 33 | This is a thin wrapper over the 34 | [proto_compile](../protobuf#proto_compile) rule having language 35 | `@org_pubref_rules_protobuf//csharp`. 36 | 37 | ```python 38 | load("@org_pubref_rules_protobuf//csharp:rules.bzl", "csharp_proto_compile") 39 | 40 | csharp_proto_compile( 41 | name = "protos", 42 | protos = ["message.proto"], 43 | with_grpc = True, 44 | ) 45 | ``` 46 | 47 | ```sh 48 | $ bazel build :protos 49 | Target //:protos up-to-date: 50 | bazel-genfiles/Message.cs 51 | ``` 52 | 53 | ## csharp\_proto\_library 54 | 55 | Pass the set of protobuf source files to the `protos` attribute. 56 | 57 | ```python 58 | load("@org_pubref_rules_protobuf//csharp:rules.bzl", "csharp_proto_library") 59 | 60 | csharp_proto_library( 61 | name = "Helloworld", 62 | protos = ["message.proto"], 63 | with_grpc = True, 64 | ) 65 | ``` 66 | 67 | ```sh 68 | $ bazel build :Helloworld 69 | Target //:Helloworld up-to-date: 70 | bazel-bin/Helloworld.dll 71 | bazel-bin/Helloworld.xml 72 | ``` 73 | 74 | To get the list of required compile-time dependencies in other 75 | contexts for grpc-related code, load the list from the rules.bzl file: 76 | 77 | ```python 78 | load("@org_pubref_rules_protobuf//csharp:rules.bzl", "GRPC_COMPILE_DEPS") 79 | 80 | csharp_binary( 81 | name = "mylib", 82 | srcs = ['MyApp.cs'], 83 | deps = [ 84 | ":protolib" 85 | ] + GRPC_COMPILE_DEPS, 86 | ) 87 | ``` 88 | 89 | Consult source files in the 90 | [examples/helloworld/csharp](../examples/helloworld/csharp) directory 91 | for additional information. 92 | -------------------------------------------------------------------------------- /csharp/deps.bzl: -------------------------------------------------------------------------------- 1 | PROTOBUF_BUILD_FILE = """ 2 | load("@io_bazel_rules_dotnet//dotnet:csharp.bzl", "dll_import") 3 | dll_import( 4 | name = "libnet45", 5 | srcs = [ 6 | "Google.Protobuf.3.4.0/lib/net451/Google.Protobuf.dll", 7 | ], 8 | visibility = ["//visibility:public"], 9 | ) 10 | """ 11 | 12 | GRPC_BUILD_FILE = """ 13 | load("@io_bazel_rules_dotnet//dotnet:csharp.bzl", "dll_import") 14 | dll_import( 15 | name = "runtime_osx", 16 | srcs = glob(["Grpc.Core.1.6.0/runtimes/osx/**/*.dll"]), 17 | visibility = ["//visibility:public"], 18 | ) 19 | dll_import( 20 | name = "system_interactive_async", 21 | srcs = glob(["System.Interactive.Async.3.4.0/lib/net45/**/*.dll"]), 22 | visibility = ["//visibility:public"], 23 | ) 24 | dll_import( 25 | name = "core", 26 | srcs = glob(["Grpc.Core.1.6.0/lib/net45/**/*.dll"]), 27 | visibility = ["//visibility:public"], 28 | ) 29 | """ 30 | 31 | DEPS = { 32 | 33 | "nuget_google_protobuf": { 34 | "rule": "new_nuget_package", 35 | "package": "Google.Protobuf", 36 | "version": "3.4.0", 37 | "build_file_content": PROTOBUF_BUILD_FILE, 38 | }, 39 | 40 | "nuget_grpc": { 41 | "rule": "new_nuget_package", 42 | "package": "Grpc", 43 | "version": "1.6.0", 44 | "build_file_content": GRPC_BUILD_FILE, 45 | }, 46 | 47 | "protoc_gen_grpc_csharp": { 48 | "rule": "bind", 49 | "actual": "@com_google_grpc//:grpc_csharp_plugin", 50 | }, 51 | 52 | } 53 | -------------------------------------------------------------------------------- /csharp/rules.bzl: -------------------------------------------------------------------------------- 1 | load("@io_bazel_rules_dotnet//dotnet:csharp.bzl", "nuget_package", "new_nuget_package", "csharp_library", "dll_import") 2 | load("//protobuf:rules.bzl", "proto_compile", "proto_repositories", "proto_language_deps") 3 | load("//cpp:rules.bzl", "cpp_proto_repositories") 4 | load("//csharp:deps.bzl", "DEPS") 5 | 6 | def csharp_proto_repositories( 7 | omit_cpp_repositories = False, 8 | lang_deps = DEPS, 9 | lang_requires = [ 10 | "protoc_gen_grpc_csharp", 11 | "nuget_google_protobuf", 12 | "nuget_grpc", 13 | ], **kwargs): 14 | 15 | if not omit_cpp_repositories: 16 | cpp_proto_repositories(**kwargs) 17 | 18 | rem = proto_repositories(lang_deps = lang_deps, 19 | lang_requires = lang_requires, 20 | **kwargs) 21 | 22 | # Load remaining (nuget) deps 23 | for dep in rem: 24 | rule = dep.pop("rule") 25 | if "new_nuget_package" == rule: 26 | new_nuget_package(**dep) 27 | else: 28 | fail("Unknown loading rule %s for %s" % (rule, dep)) 29 | 30 | PB_COMPILE_DEPS = [ 31 | "@nuget_google_protobuf//:libnet45", 32 | ] 33 | 34 | GRPC_COMPILE_DEPS = PB_COMPILE_DEPS + [ 35 | "@nuget_grpc//:core", 36 | ] 37 | 38 | def csharp_proto_compile(langs = [str(Label("//csharp"))], **kwargs): 39 | proto_compile(langs = langs, **kwargs) 40 | 41 | 42 | def csharp_proto_library( 43 | name, 44 | langs = [str(Label("//csharp"))], 45 | protos = [], 46 | imports = [], 47 | inputs = [], 48 | proto_deps = [], 49 | output_to_workspace = False, 50 | protoc = None, 51 | 52 | pb_plugin = None, 53 | pb_options = [], 54 | 55 | grpc_plugin = None, 56 | grpc_options = [], 57 | 58 | proto_compile_args = {}, 59 | with_grpc = False, 60 | srcs = [], 61 | deps = [], 62 | verbose = 0, 63 | **kwargs): 64 | 65 | proto_compile_args += { 66 | "name": name + ".pb", 67 | "protos": protos, 68 | "deps": [dep + ".pb" for dep in proto_deps], 69 | "langs": langs, 70 | "imports": imports, 71 | "inputs": inputs, 72 | "pb_options": pb_options, 73 | "grpc_options": grpc_options, 74 | "output_to_workspace": output_to_workspace, 75 | "verbose": verbose, 76 | "with_grpc": with_grpc, 77 | } 78 | 79 | if protoc: 80 | proto_compile_args["protoc"] = protoc 81 | if pb_plugin: 82 | proto_compile_args["pb_plugin"] = pb_plugin 83 | if grpc_plugin: 84 | proto_compile_args["grpc_plugin"] = grpc_plugin 85 | 86 | proto_compile(**proto_compile_args) 87 | 88 | # proto_language_deps( 89 | # name = name + "_deps", 90 | # langs = langs, 91 | # file_extensions = [".dll"], 92 | # with_grpc = with_grpc, 93 | # ) 94 | 95 | # dll_import( 96 | # name = name + "_imports", 97 | # srcs = [name + "_deps"], 98 | # ) 99 | 100 | if with_grpc: 101 | compile_deps = GRPC_COMPILE_DEPS 102 | else: 103 | compile_deps = PB_COMPILE_DEPS 104 | 105 | csharp_library( 106 | name = name, 107 | srcs = srcs + [name + ".pb"], 108 | #deps = depset(deps + proto_deps + [name + "_imports"]).to_list(), 109 | deps = depset(deps + proto_deps + compile_deps).to_list(), 110 | **kwargs) 111 | -------------------------------------------------------------------------------- /examples/extra_args/BUILD: -------------------------------------------------------------------------------- 1 | package(default_visibility = ["//visibility:public"]) 2 | 3 | # https://github.com/pubref/rules_protobuf/issues/57 4 | 5 | load("@org_pubref_rules_protobuf//protobuf:rules.bzl", "proto_compile") 6 | load("@bazel_tools//tools/build_defs/pkg:pkg.bzl", "pkg_tar") 7 | 8 | filegroup( 9 | name = "protos", 10 | srcs = ["person.proto"], 11 | ) 12 | 13 | proto_compile( 14 | name = "person_proto", 15 | args = [ 16 | "--include_imports", 17 | "--include_source_info", 18 | ], 19 | 20 | # For well-known protos, if needed 21 | imports = ["external/com_google_protobuf/src"], 22 | inputs = ["@com_google_protobuf//:well_known_protos"], 23 | protos = ["person.proto"], 24 | verbose = 0, 25 | ) 26 | 27 | # pkg_tar rule is used in this example to provide a sink for the 28 | # descriptor file, forcing bazel to actually execute upstream 29 | # dependent rules. 30 | pkg_tar( 31 | name = "person_tar", 32 | files = [ 33 | ":person_proto.descriptor_set", 34 | ], 35 | ) 36 | -------------------------------------------------------------------------------- /examples/extra_args/person.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | 3 | import "google/protobuf/any.proto"; 4 | import "google/protobuf/duration.proto"; 5 | import "google/protobuf/timestamp.proto"; 6 | import "google/protobuf/empty.proto"; 7 | import "google/protobuf/struct.proto"; 8 | import "google/protobuf/wrappers.proto"; 9 | import "google/protobuf/descriptor.proto"; 10 | import "google/protobuf/compiler/plugin.proto"; 11 | 12 | message Person { 13 | string name = 1; 14 | google.protobuf.FileDescriptorProto file = 2; 15 | google.protobuf.Any any = 3; 16 | google.protobuf.Duration duration = 4; 17 | google.protobuf.Timestamp ts = 5; 18 | google.protobuf.Empty empty = 6; 19 | google.protobuf.Struct struct = 7; 20 | google.protobuf.BytesValue bytes_wrapper = 8; 21 | google.protobuf.compiler.CodeGeneratorRequest code_generator_request = 9; 22 | } 23 | 24 | extend google.protobuf.MessageOptions { 25 | string my_option = 10001; 26 | } 27 | -------------------------------------------------------------------------------- /examples/helloworld/Makefile: -------------------------------------------------------------------------------- 1 | BAZEL_BIN := $(shell bazel info bazel-bin) 2 | JAVA_SOURCE_PATH := "java/org/pubref/rules_protobuf/examples/helloworld" 3 | GO_SOURCE_PATH := "go" 4 | 5 | # The java examples could also be run with 'bazel run ...', but the 6 | # point is to demonstrate how to run it as an executable jar. 7 | java_server: 8 | bazel build $(JAVA_SOURCE_PATH)/server:netty_deploy.jar && \ 9 | java -jar $(BAZEL_BIN)/examples/helloworld/$(JAVA_SOURCE_PATH)/server/netty_deploy.jar 10 | 11 | java_client: 12 | bazel build $(JAVA_SOURCE_PATH)/client:netty_deploy.jar && \ 13 | java -jar $(BAZEL_BIN)/examples/helloworld/$(JAVA_SOURCE_PATH)/client/netty_deploy.jar 14 | 15 | go_client: 16 | bazel run go/client 17 | 18 | go_server: 19 | bazel build go/server && $(BAZEL_BIN)/examples/helloworld/go/server/server 20 | 21 | cpp_server: 22 | bazel build cpp/server && $(BAZEL_BIN)/examples/helloworld/cpp/server 23 | 24 | cpp_client: 25 | bazel run cpp/client 26 | 27 | csharp_server: 28 | bazel build csharp/GreeterServer && $(BAZEL_BIN)/examples/helloworld/csharp/GreeterServer/GreeterServer 29 | 30 | csharp_client: 31 | bazel run csharp/GreeterClient 32 | 33 | gateway: 34 | bazel build grpc_gateway:greeter 35 | $(BAZEL_BIN)/examples/helloworld/grpc_gateway/greeter 36 | 37 | curl_post: 38 | curl -X POST -k http://localhost:8080/v1/helloworld/sayhello -d '{"name": "gRPC-gateway!"}' 39 | -------------------------------------------------------------------------------- /examples/helloworld/closure/BUILD: -------------------------------------------------------------------------------- 1 | package(default_visibility = ["//visibility:public"]) 2 | 3 | load("@io_bazel_rules_closure//closure:defs.bzl", "closure_js_library") 4 | load("@io_bazel_rules_closure//closure:defs.bzl", "closure_js_test") 5 | load("//closure:rules.bzl", "closure_proto_library") 6 | 7 | closure_proto_library( 8 | name = "protolib", 9 | protos = [ 10 | "//examples/helloworld/proto:protos", 11 | "//examples/proto:protos", 12 | ], 13 | verbose = 0, 14 | ) 15 | 16 | closure_js_library( 17 | name = "greeter", 18 | srcs = [ 19 | "greeter.js", 20 | #"protolib.pb", 21 | ], 22 | deps = [ 23 | ":protolib", 24 | "@io_bazel_rules_closure//closure/library", 25 | "@io_bazel_rules_closure//closure/protobuf:jspb", 26 | ], 27 | ) 28 | 29 | closure_js_test( 30 | name = "greeter_test", 31 | srcs = ["greeter_test.js"], 32 | compilation_level = "SIMPLE", 33 | size = "small", 34 | deps = [ 35 | ":greeter", 36 | ":protolib", 37 | "@io_bazel_rules_closure//closure/library:testing", 38 | "@io_bazel_rules_closure//closure/protobuf:jspb", 39 | ], 40 | ) 41 | -------------------------------------------------------------------------------- /examples/helloworld/closure/greeter.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @fileoverview Create a helloworld message. 3 | */ 4 | goog.provide('examples.helloworld.closure.Greeter'); 5 | 6 | goog.require('goog.events.EventTarget'); 7 | goog.require('proto.helloworld.HelloReply'); 8 | goog.require('proto.helloworld.HelloRequest'); 9 | 10 | 11 | /** 12 | * Mock greeter implementation. As there is no current browser support 13 | * for gRPC, this is just a fake implementation whose primary use is 14 | * to demo the use of the message protos. 15 | * 16 | * @constructor 17 | * @extends {goog.events.EventTarget} 18 | */ 19 | examples.helloworld.closure.Greeter = function() { 20 | goog.events.EventTarget.call(this); 21 | }; 22 | goog.inherits(examples.helloworld.closure.Greeter, goog.events.EventTarget); 23 | 24 | 25 | /** 26 | * Greet! 27 | * 28 | * @param {!proto.helloworld.HelloRequest} request 29 | * @return {!proto.helloworld.HelloReply} 30 | */ 31 | examples.helloworld.closure.Greeter.prototype.sayHello = function(request) { 32 | var reply = new proto.helloworld.HelloReply(); 33 | reply.setMessage("Hello, " + request.getName()); 34 | return reply; 35 | }; 36 | -------------------------------------------------------------------------------- /examples/helloworld/closure/greeter_test.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @fileoverview Test the greeter implementation. 3 | */ 4 | goog.setTestOnly(); 5 | 6 | goog.require('examples.helloworld.closure.Greeter'); 7 | goog.require('goog.testing.asserts'); 8 | goog.require('goog.testing.jsunit'); 9 | goog.require('proto.helloworld.HelloRequest'); 10 | 11 | 12 | function testSayHello() { 13 | //var active = goog.testing.TestCase.getActiveTest(); 14 | 15 | var greeter = new examples.helloworld.closure.Greeter(); 16 | var request = new proto.helloworld.HelloRequest(); 17 | var name = "foo"; 18 | request.setName(name); 19 | 20 | var reply = greeter.sayHello(request); 21 | greeter.dispose(); 22 | 23 | assertEquals("Hello, " + name, reply.getMessage()); 24 | } 25 | -------------------------------------------------------------------------------- /examples/helloworld/cpp/BUILD: -------------------------------------------------------------------------------- 1 | package(default_visibility = ["//visibility:public"]) 2 | 3 | cc_test( 4 | name = "test", 5 | size = "small", 6 | srcs = ["greeter_client_test.cc"], 7 | copts = ["-Iexternal/gtest/include"], 8 | tags = ["exclusive"], 9 | deps = [ 10 | ":clientlib", 11 | "@com_google_googletest//:gtest", 12 | ], 13 | ) 14 | 15 | cc_binary( 16 | name = "client", 17 | srcs = ["greeter_client_main.cc"], 18 | deps = [":clientlib"], 19 | ) 20 | 21 | cc_binary( 22 | name = "server", 23 | srcs = ["greeter_server.cc"], 24 | deps = ["//examples/helloworld/proto:cpp"], 25 | ) 26 | 27 | cc_library( 28 | name = "clientlib", 29 | srcs = ["greeter_client.cc"], 30 | hdrs = ["greeter_client.h"], 31 | deps = ["//examples/helloworld/proto:cpp"], 32 | ) 33 | -------------------------------------------------------------------------------- /examples/helloworld/cpp/greeter_client.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2015, Google Inc. 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * * Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * * Redistributions in binary form must reproduce the above 13 | * copyright notice, this list of conditions and the following disclaimer 14 | * in the documentation and/or other materials provided with the 15 | * distribution. 16 | * * Neither the name of Google Inc. nor the names of its 17 | * contributors may be used to endorse or promote products derived from 18 | * this software without specific prior written permission. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | * 32 | */ 33 | 34 | #include 35 | 36 | #include "greeter_client.h" 37 | 38 | // Constructor with "initialization list" 39 | GreeterClient::GreeterClient(std::shared_ptr channel) 40 | : stub_(Greeter::NewStub(channel)) {} 41 | 42 | std::string GreeterClient::SayHello(const std::string& user) { 43 | // Data we are sending to the server. 44 | HelloRequest request; 45 | request.set_name(user); 46 | 47 | // Container for the data we expect from the server. 48 | HelloReply reply; 49 | 50 | // Context for the client. It could be used to convey extra information to 51 | // the server and/or tweak certain RPC behaviors. 52 | ClientContext context; 53 | 54 | // The actual RPC. 55 | Status status = stub_->SayHello(&context, request, &reply); 56 | 57 | // Act upon its status. 58 | if (status.ok()) { 59 | return reply.message(); 60 | } else { 61 | return "RPC failed"; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /examples/helloworld/cpp/greeter_client.h: -------------------------------------------------------------------------------- 1 | #ifndef GREETER_CLIENT_H 2 | #define GREETER_CLIENT_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | #include "examples/helloworld/proto/helloworld.pb.h" 9 | #include "examples/helloworld/proto/helloworld.grpc.pb.h" 10 | 11 | using grpc::Channel; 12 | using grpc::ClientContext; 13 | using grpc::Status; 14 | using helloworld::HelloRequest; 15 | using helloworld::HelloReply; 16 | using helloworld::Greeter; 17 | 18 | class GreeterClient { 19 | public: 20 | GreeterClient(std::shared_ptr channel); 21 | // Assambles the client's payload, sends it and presents the 22 | // response back from the server. 23 | std::string SayHello(const std::string& user); 24 | 25 | private: 26 | std::unique_ptr stub_; 27 | }; 28 | 29 | #endif 30 | -------------------------------------------------------------------------------- /examples/helloworld/cpp/greeter_client_main.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2015, Google Inc. 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * * Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * * Redistributions in binary form must reproduce the above 13 | * copyright notice, this list of conditions and the following disclaimer 14 | * in the documentation and/or other materials provided with the 15 | * distribution. 16 | * * Neither the name of Google Inc. nor the names of its 17 | * contributors may be used to endorse or promote products derived from 18 | * this software without specific prior written permission. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | * 32 | */ 33 | 34 | #include 35 | 36 | #include "greeter_client.h" 37 | 38 | int main(int argc, char** argv) { 39 | 40 | // Instantiate the client. It requires a channel, out of which the 41 | // actual RPCs are created. This channel models a connection to an 42 | // endpoint (in this case, localhost at port 50051). We indicate 43 | // that the channel isn't authenticated (use of 44 | // InsecureCredentials()). 45 | GreeterClient greeter( 46 | grpc::CreateChannel("localhost:50051", grpc::InsecureChannelCredentials())); 47 | std::string user("world"); 48 | std::string reply = greeter.SayHello(user); 49 | std::cout << "Greeter received: " << reply << std::endl; 50 | 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /examples/helloworld/cpp/greeter_client_test.cc: -------------------------------------------------------------------------------- 1 | #include "gtest/gtest.h" 2 | #include "greeter_client.h" 3 | 4 | using grpc::Server; 5 | using grpc::ServerBuilder; 6 | using grpc::ServerContext; 7 | using grpc::Status; 8 | using helloworld::HelloRequest; 9 | using helloworld::HelloReply; 10 | using helloworld::Greeter; 11 | 12 | 13 | // Logic and data behind the server's behavior. 14 | class GreeterServiceImpl final : public Greeter::Service { 15 | Status SayHello(ServerContext* context, const HelloRequest* request, 16 | HelloReply* reply) override { 17 | std::string prefix("Hello "); 18 | reply->set_message(prefix + request->name()); 19 | return Status::OK; 20 | } 21 | }; 22 | 23 | class GreeterClientTest : public ::testing::Test { 24 | protected: 25 | virtual void SetUp() { 26 | // TODO(user): figure out how ot integrate threads into google 27 | // test. Perhaps the correct answer is to mock gRPC rather than 28 | // using threads. 29 | 30 | //std::thread([=] { StartServer(); }); 31 | } 32 | 33 | virtual void StartServer() { 34 | std::string server_address("0.0.0.0:50051"); 35 | GreeterServiceImpl service; 36 | 37 | ServerBuilder builder; 38 | // Listen on the given address without any authentication mechanism. 39 | builder.AddListeningPort(server_address, grpc::InsecureServerCredentials()); 40 | // Register "service" as the instance through which we'll communicate with 41 | // clients. In this case it corresponds to an *synchronous* service. 42 | builder.RegisterService(&service); 43 | // Finally assemble the server. 44 | std::unique_ptr server(builder.BuildAndStart()); 45 | server->Wait(); 46 | } 47 | 48 | virtual void TearDown() { 49 | } 50 | 51 | }; 52 | 53 | // At the moment this test can pass only if there is a helloworld 54 | // server running. Given this is not guaranteed at the moment, just 55 | // be happy everything compiles and runs up to this point. The main 56 | // point of the test is to ensure that bazel can compile and build it, 57 | // so at some level, even if we can run a test that does nothing is a 58 | // success. 59 | 60 | TEST_F(GreeterClientTest, testHello) { 61 | GreeterClient greeter(grpc::CreateChannel("localhost:50051", grpc::InsecureChannelCredentials())); 62 | std::string user("world"); 63 | std::string reply = ("Hello world"); 64 | //std::string reply = greeter.SayHello(user); 65 | EXPECT_EQ("Hello world", reply); 66 | } 67 | 68 | 69 | int main(int ac, char* av[]) { 70 | testing::InitGoogleTest(&ac, av); 71 | return RUN_ALL_TESTS(); 72 | } 73 | -------------------------------------------------------------------------------- /examples/helloworld/cpp/greeter_server.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2015, Google Inc. 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * * Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * * Redistributions in binary form must reproduce the above 13 | * copyright notice, this list of conditions and the following disclaimer 14 | * in the documentation and/or other materials provided with the 15 | * distribution. 16 | * * Neither the name of Google Inc. nor the names of its 17 | * contributors may be used to endorse or promote products derived from 18 | * this software without specific prior written permission. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | * 32 | */ 33 | 34 | #include 35 | #include 36 | #include 37 | 38 | #include 39 | 40 | #include "examples/helloworld/proto/helloworld.pb.h" 41 | #include "examples/helloworld/proto/helloworld.grpc.pb.h" 42 | 43 | using grpc::Server; 44 | using grpc::ServerBuilder; 45 | using grpc::ServerContext; 46 | using grpc::Status; 47 | using helloworld::HelloRequest; 48 | using helloworld::HelloReply; 49 | using helloworld::Greeter; 50 | 51 | // Logic and data behind the server's behavior. 52 | class GreeterServiceImpl final : public Greeter::Service { 53 | Status SayHello(ServerContext* context, const HelloRequest* request, 54 | HelloReply* reply) override { 55 | std::string prefix("Hello "); 56 | reply->set_message(prefix + request->name()); 57 | return Status::OK; 58 | } 59 | }; 60 | 61 | void RunServer() { 62 | std::string server_address("0.0.0.0:50051"); 63 | GreeterServiceImpl service; 64 | 65 | ServerBuilder builder; 66 | // Listen on the given address without any authentication mechanism. 67 | builder.AddListeningPort(server_address, grpc::InsecureServerCredentials()); 68 | // Register "service" as the instance through which we'll communicate with 69 | // clients. In this case it corresponds to an *synchronous* service. 70 | builder.RegisterService(&service); 71 | // Finally assemble the server. 72 | std::unique_ptr server(builder.BuildAndStart()); 73 | std::cout << "Server listening on " << server_address << std::endl; 74 | 75 | // Wait for the server to shutdown. Note that some other thread must be 76 | // responsible for shutting down the server for this call to ever return. 77 | server->Wait(); 78 | } 79 | 80 | int main(int argc, char** argv) { 81 | RunServer(); 82 | 83 | return 0; 84 | } 85 | -------------------------------------------------------------------------------- /examples/helloworld/csharp/GreeterClient/BUILD: -------------------------------------------------------------------------------- 1 | load("@io_bazel_rules_dotnet//dotnet:csharp.bzl", "csharp_binary", "csharp_nunit_test") 2 | load("//csharp:rules.bzl", "csharp_proto_library") 3 | 4 | csharp_binary( 5 | name = "GreeterClient", 6 | srcs = [ 7 | "Program.cs", 8 | ], 9 | deps = [ 10 | ":Helloworld", 11 | "@nuget_google_protobuf//:libnet45", 12 | "@nuget_grpc//:core", 13 | "@nuget_grpc//:runtime_osx", 14 | ], 15 | ) 16 | 17 | csharp_proto_library( 18 | name = "Helloworld", 19 | proto_deps = [ 20 | "//examples/proto:csharp", 21 | ], 22 | protos = ["//examples/helloworld/proto:protos"], 23 | verbose = 0, 24 | with_grpc = True, 25 | ) 26 | 27 | csharp_nunit_test( 28 | name = "GreeterClientTest", 29 | size = "small", 30 | srcs = [ 31 | "GreeterClientTest.cs", 32 | ], 33 | deps = [ 34 | # This isn't quite working yet, so this test is essentially a no-op... 35 | #":Helloworld", 36 | #"@nuget_google_protobuf//:libnet45", 37 | #"@nuget_grpc//:core", 38 | ], 39 | ) 40 | -------------------------------------------------------------------------------- /examples/helloworld/csharp/GreeterClient/GreeterClientTest.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | // Having issue pulling in this Grpc dependency. 3 | //using Grpc.Core; 4 | //using Helloworld; 5 | using NUnit.Framework; 6 | 7 | namespace examples_helloworld_csharp 8 | { 9 | [TestFixture] 10 | public class MyTest 11 | { 12 | public void TestSayHello() 13 | { 14 | /* Channel channel = new Channel("127.0.0.1:50051", ChannelCredentials.Insecure); */ 15 | /* var client = new Greeter.GreeterClient(channel); */ 16 | /* String user = "foo"; */ 17 | /* var reply = client.SayHello(new HelloRequest { Name = user }); */ 18 | /* Assert.That(reply.Message, Is.EqualTo(user)); */ 19 | } 20 | 21 | [Test] 22 | public void TestAssert() 23 | { 24 | String user = "foo"; 25 | Assert.That("foo", Is.EqualTo(user)); 26 | } 27 | 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /examples/helloworld/csharp/GreeterClient/Program.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2015, Google Inc. 2 | // All rights reserved. 3 | // 4 | // Redistribution and use in source and binary forms, with or without 5 | // modification, are permitted provided that the following conditions are 6 | // met: 7 | // 8 | // * Redistributions of source code must retain the above copyright 9 | // notice, this list of conditions and the following disclaimer. 10 | // * Redistributions in binary form must reproduce the above 11 | // copyright notice, this list of conditions and the following disclaimer 12 | // in the documentation and/or other materials provided with the 13 | // distribution. 14 | // * Neither the name of Google Inc. nor the names of its 15 | // contributors may be used to endorse or promote products derived from 16 | // this software without specific prior written permission. 17 | // 18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | 30 | using System; 31 | using Grpc.Core; 32 | using Helloworld; 33 | 34 | namespace GreeterClient 35 | { 36 | class Program 37 | { 38 | public static void Main(string[] args) 39 | { 40 | Channel channel = new Channel("127.0.0.1:50051", ChannelCredentials.Insecure); 41 | 42 | var client = new Greeter.GreeterClient(channel); 43 | String user = "you"; 44 | 45 | var reply = client.SayHello(new HelloRequest { Name = user }); 46 | Console.WriteLine("Greeting: " + reply.Message); 47 | 48 | channel.ShutdownAsync().Wait(); 49 | Console.WriteLine("Press any key to exit..."); 50 | Console.ReadKey(); 51 | } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /examples/helloworld/csharp/GreeterServer/BUILD: -------------------------------------------------------------------------------- 1 | load("@io_bazel_rules_dotnet//dotnet:csharp.bzl", "csharp_binary") 2 | load("//csharp:rules.bzl", "csharp_proto_library") 3 | 4 | csharp_binary( 5 | name = "GreeterServer", 6 | srcs = [ 7 | "Program.cs", 8 | ], 9 | deps = [ 10 | ":Helloworld", 11 | "@nuget_google_protobuf//:libnet45", 12 | "@nuget_grpc//:core", 13 | "@nuget_grpc//:runtime_osx", 14 | "@nuget_grpc//:system_interactive_async", 15 | ], 16 | ) 17 | 18 | csharp_proto_library( 19 | name = "Helloworld", 20 | proto_deps = [ 21 | "//examples/proto:csharp", 22 | ], 23 | protos = ["//examples/helloworld/proto:protos"], 24 | verbose = 0, 25 | with_grpc = True, 26 | ) 27 | -------------------------------------------------------------------------------- /examples/helloworld/csharp/GreeterServer/Program.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2015, Google Inc. 2 | // All rights reserved. 3 | // 4 | // Redistribution and use in source and binary forms, with or without 5 | // modification, are permitted provided that the following conditions are 6 | // met: 7 | // 8 | // * Redistributions of source code must retain the above copyright 9 | // notice, this list of conditions and the following disclaimer. 10 | // * Redistributions in binary form must reproduce the above 11 | // copyright notice, this list of conditions and the following disclaimer 12 | // in the documentation and/or other materials provided with the 13 | // distribution. 14 | // * Neither the name of Google Inc. nor the names of its 15 | // contributors may be used to endorse or promote products derived from 16 | // this software without specific prior written permission. 17 | // 18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | 30 | using System; 31 | using System.Threading.Tasks; 32 | using Grpc.Core; 33 | using Helloworld; 34 | 35 | namespace GreeterServer 36 | { 37 | class GreeterImpl : Greeter.GreeterBase 38 | { 39 | // Server side handler of the SayHello RPC 40 | public override Task SayHello(HelloRequest request, ServerCallContext context) 41 | { 42 | return Task.FromResult(new HelloReply { Message = "Hello " + request.Name }); 43 | } 44 | } 45 | 46 | class Program 47 | { 48 | const int Port = 50051; 49 | 50 | public static void Main(string[] args) 51 | { 52 | Server server = new Server 53 | { 54 | Services = { Greeter.BindService(new GreeterImpl()) }, 55 | Ports = { new ServerPort("localhost", Port, ServerCredentials.Insecure) } 56 | }; 57 | server.Start(); 58 | 59 | Console.WriteLine("Greeter server listening on port " + Port); 60 | Console.WriteLine("Press any key to stop the server..."); 61 | Console.ReadKey(); 62 | 63 | server.ShutdownAsync().Wait(); 64 | } 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /examples/helloworld/go/client/BUILD: -------------------------------------------------------------------------------- 1 | package(default_visibility = ["//visibility:public"]) 2 | 3 | load("@io_bazel_rules_go//go:def.bzl", "go_binary") 4 | load( 5 | "//go:rules.bzl", 6 | GO_GRPC_COMPILE_DEPS = "GRPC_COMPILE_DEPS", 7 | ) 8 | 9 | go_binary( 10 | name = "client", 11 | srcs = ["main.go"], 12 | deps = ["//examples/helloworld/proto:go"] + GO_GRPC_COMPILE_DEPS, 13 | ) 14 | 15 | #load("//gogo:rules.bzl", GOGO_GRPC_COMPILE_DEPS = "GRPC_COMPILE_DEPS") 16 | # 17 | # To enable this, uncomment and change the importpath to 'gogo' in 18 | # main.go. 19 | # 20 | # go_binary( 21 | # name = "gogo_client", 22 | # srcs = ["main.go"], 23 | # deps = ["//examples/helloworld/proto:gogo"] + GOGO_GRPC_COMPILE_DEPS, 24 | # ) 25 | -------------------------------------------------------------------------------- /examples/helloworld/go/client/main.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2016 Google Inc, PubRef Inc. 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * * Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * * Redistributions in binary form must reproduce the above 13 | * copyright notice, this list of conditions and the following disclaimer 14 | * in the documentation and/or other materials provided with the 15 | * distribution. 16 | * * Neither the name of Google Inc. nor the names of its 17 | * contributors may be used to endorse or promote products derived from 18 | * this software without specific prior written permission. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | * 32 | */ 33 | 34 | package main 35 | 36 | import ( 37 | "log" 38 | "os" 39 | 40 | "golang.org/x/net/context" 41 | "google.golang.org/grpc" 42 | 43 | pb "github.com/pubref/rules_protobuf/examples/helloworld/proto/go" 44 | // ^A ^B ^C 45 | // 46 | // A: go_prefix("github.com/pubref/rules_protobuf") 47 | // B: path to proto file directory "//examples/helloworld/proto" 48 | // C: name of BUILD target that generated the protobufs: ":go" (elide if target name is the magic "go_default_library") 49 | 50 | ) 51 | 52 | const ( 53 | address = "localhost:50051" 54 | defaultName = "world" 55 | ) 56 | 57 | func main() { 58 | // Set up a connection to the server. 59 | conn, err := grpc.Dial(address, grpc.WithInsecure()) 60 | if err != nil { 61 | log.Fatalf("did not connect: %v", err) 62 | } 63 | defer conn.Close() 64 | c := pb.NewGreeterClient(conn) 65 | 66 | // Contact the server and print out its response. 67 | name := defaultName 68 | if len(os.Args) > 1 { 69 | name = os.Args[1] 70 | } 71 | r, err := c.SayHello(context.Background(), &pb.HelloRequest{Name: name}) 72 | if err != nil { 73 | log.Fatalf("could not greet: %v", err) 74 | } 75 | log.Printf("Greeting: %s", r.Message) 76 | } 77 | -------------------------------------------------------------------------------- /examples/helloworld/go/greeter_test/BUILD: -------------------------------------------------------------------------------- 1 | package(default_visibility = ["//visibility:public"]) 2 | 3 | load("@io_bazel_rules_go//go:def.bzl", "go_test") 4 | load("//go:rules.bzl", "GRPC_COMPILE_DEPS") 5 | 6 | go_test( 7 | name = "greeter_test", 8 | size = "small", 9 | srcs = ["greeter_test.go"], 10 | tags = ["exclusive"], 11 | deps = [ 12 | "//examples/helloworld/proto:go", 13 | "//examples/helloworld/go/server:greeter", 14 | ] + GRPC_COMPILE_DEPS, 15 | ) 16 | -------------------------------------------------------------------------------- /examples/helloworld/go/greeter_test/greeter_test.go: -------------------------------------------------------------------------------- 1 | package greeter_test 2 | 3 | import ( 4 | "testing" 5 | "log" 6 | "os" 7 | "net" 8 | 9 | "golang.org/x/net/context" 10 | "google.golang.org/grpc" 11 | 12 | greeter "github.com/pubref/rules_protobuf/examples/helloworld/go/server/greeter" 13 | 14 | pb "github.com/pubref/rules_protobuf/examples/helloworld/proto/go" 15 | ) 16 | 17 | const ( 18 | port = ":50051" 19 | address = "localhost:50051" 20 | defaultName = "world" 21 | ) 22 | 23 | func startServer() { 24 | lis, err := net.Listen("tcp", port) 25 | if err != nil { 26 | log.Fatalf("failed to listen: %v", err) 27 | } 28 | s := grpc.NewServer() 29 | pb.RegisterGreeterServer(s, &greeter.Server{}) 30 | s.Serve(lis) 31 | } 32 | 33 | func TestClient(t *testing.T) { 34 | go startServer() 35 | 36 | // Set up a connection to the server. 37 | conn, err := grpc.Dial(address, grpc.WithInsecure()) 38 | if err != nil { 39 | log.Fatalf("did not connect: %v", err) 40 | } 41 | defer conn.Close() 42 | c := pb.NewGreeterClient(conn) 43 | // Contact the server and print out its response. 44 | name := defaultName 45 | if len(os.Args) > 1 { 46 | name = os.Args[1] 47 | } 48 | r, err := c.SayHello(context.Background(), &pb.HelloRequest{Name: name}) 49 | if err != nil { 50 | log.Fatalf("could not greet: %v", err) 51 | } 52 | log.Printf("Greeting: %s", r.Message) 53 | } 54 | -------------------------------------------------------------------------------- /examples/helloworld/go/server/BUILD: -------------------------------------------------------------------------------- 1 | package(default_visibility = ["//visibility:public"]) 2 | 3 | load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library") 4 | load("//go:rules.bzl", "GRPC_COMPILE_DEPS") 5 | 6 | go_binary( 7 | name = "server", 8 | srcs = ["main.go"], 9 | deps = ["//examples/helloworld/proto:go"] + GRPC_COMPILE_DEPS, 10 | ) 11 | 12 | # library used by greeter_test. 13 | go_library( 14 | name = "greeter", 15 | srcs = ["main.go"], 16 | deps = ["//examples/helloworld/proto:go"] + GRPC_COMPILE_DEPS, 17 | ) 18 | -------------------------------------------------------------------------------- /examples/helloworld/go/server/main.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2016, Google Inc., PubRef Inc. 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * * Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * * Redistributions in binary form must reproduce the above 13 | * copyright notice, this list of conditions and the following disclaimer 14 | * in the documentation and/or other materials provided with the 15 | * distribution. 16 | * * Neither the name of Google Inc. nor the names of its 17 | * contributors may be used to endorse or promote products derived from 18 | * this software without specific prior written permission. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | * 32 | */ 33 | 34 | package main 35 | 36 | import ( 37 | "log" 38 | "net" 39 | 40 | pb "github.com/pubref/rules_protobuf/examples/helloworld/proto/go" 41 | // ^A ^B ^C 42 | // 43 | // A: go_prefix("github.com/pubref/rules_protobuf") 44 | // B: path to proto file directory "//examples/helloworld/proto" 45 | // C: name of BUILD target that generated the protobufs: ":go" 46 | 47 | "golang.org/x/net/context" 48 | "google.golang.org/grpc" 49 | ) 50 | 51 | const ( 52 | port = ":50051" 53 | ) 54 | 55 | // server is used to implement helloworld.GreeterServer. 56 | type Server struct{} 57 | 58 | // SayHello implements helloworld.GreeterServer 59 | func (s *Server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) { 60 | return &pb.HelloReply{Message: "Hello " + in.Name}, nil 61 | } 62 | 63 | func main() { 64 | lis, err := net.Listen("tcp", port) 65 | if err != nil { 66 | log.Fatalf("failed to listen: %v", err) 67 | } 68 | s := grpc.NewServer() 69 | pb.RegisterGreeterServer(s, &Server{}) 70 | s.Serve(lis) 71 | } 72 | -------------------------------------------------------------------------------- /examples/helloworld/grpc_gateway/BUILD: -------------------------------------------------------------------------------- 1 | package(default_visibility = ["//visibility:public"]) 2 | 3 | load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_binary", "go_test") 4 | load( 5 | "//grpc_gateway:rules.bzl", 6 | "grpc_gateway_proto_compile", 7 | "grpc_gateway_proto_library", 8 | "grpc_gateway_swagger_compile", 9 | "grpc_gateway_binary", 10 | "GRPC_GATEWAY_DEPS", 11 | ) 12 | 13 | grpc_gateway_proto_library( 14 | name = "gateway", 15 | protos = ["helloworld.proto"], 16 | ) 17 | 18 | grpc_gateway_swagger_compile( 19 | name = "swagger", 20 | protos = ["helloworld.proto"], 21 | ) 22 | 23 | grpc_gateway_binary( 24 | name = "greeter", 25 | srcs = ["greeter.go"], 26 | protos = ["helloworld.proto"], 27 | ) 28 | 29 | # grpc_gateway_binary rule above is a shorthand equivalent for the 30 | # following go_binary. 31 | go_binary( 32 | name = "greeter_bin", 33 | srcs = ["greeter.go"], 34 | deps = [ 35 | ":gateway", 36 | ] + GRPC_GATEWAY_DEPS, 37 | ) 38 | 39 | go_test( 40 | name = "greeter_test", 41 | size = "small", 42 | srcs = [ 43 | "greeter_test.go", 44 | ], 45 | tags = ["exclusive"], 46 | deps = [ 47 | ":gateway", 48 | "//examples/helloworld/go/server:greeter", 49 | "//examples/helloworld/proto:go", 50 | "@com_github_golang_protobuf//jsonpb:go_default_library", 51 | ] + GRPC_GATEWAY_DEPS, 52 | ) 53 | -------------------------------------------------------------------------------- /examples/helloworld/grpc_gateway/greeter.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "flag" 5 | "net/http" 6 | 7 | "github.com/golang/glog" 8 | "github.com/grpc-ecosystem/grpc-gateway/runtime" 9 | "golang.org/x/net/context" 10 | "google.golang.org/grpc" 11 | gateway "github.com/pubref/rules_protobuf/examples/helloworld/grpc_gateway" 12 | ) 13 | 14 | var ( 15 | endpoint = flag.String( 16 | "endpoint", 17 | "localhost:50051", 18 | "gRPC service endpoint (default: localhost:50051)", 19 | ) 20 | ) 21 | 22 | func run() error { 23 | ctx := context.Background() 24 | ctx, cancel := context.WithCancel(ctx) 25 | defer cancel() 26 | 27 | mux := runtime.NewServeMux() 28 | opts := []grpc.DialOption{grpc.WithInsecure()} 29 | err := gateway.RegisterGreeterHandlerFromEndpoint(ctx, mux, *endpoint, opts) 30 | if err != nil { 31 | return err 32 | } 33 | 34 | http.ListenAndServe(":8080", mux) 35 | return nil 36 | } 37 | 38 | func main() { 39 | flag.Parse() 40 | defer glog.Flush() 41 | 42 | if err := run(); err != nil { 43 | glog.Fatal(err) 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /examples/helloworld/grpc_gateway/greeter_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "github.com/golang/protobuf/jsonpb" 6 | "golang.org/x/net/context" 7 | "google.golang.org/grpc" 8 | "io/ioutil" 9 | "log" 10 | "net" 11 | "net/http" 12 | "strings" 13 | "testing" 14 | 15 | "github.com/grpc-ecosystem/grpc-gateway/runtime" 16 | greeter "github.com/pubref/rules_protobuf/examples/helloworld/go/server/greeter" 17 | pb "github.com/pubref/rules_protobuf/examples/helloworld/proto/go" 18 | gateway "github.com/pubref/rules_protobuf/examples/helloworld/grpc_gateway" 19 | ) 20 | 21 | const ( 22 | grpc_port = ":50051" 23 | http_port = ":8080" 24 | ) 25 | 26 | func TestGreet(t *testing.T) { 27 | if testing.Short() { 28 | t.Skip() 29 | return 30 | } 31 | 32 | go startServer() 33 | go startGateway() 34 | 35 | testGreet(t) 36 | } 37 | 38 | func testGreet(t *testing.T) { 39 | 40 | url := fmt.Sprintf("http://localhost%s/v1/helloworld/sayhello", http_port) 41 | resp, err := http.Post(url, "application/json", strings.NewReader("{\"name\": \"foo\"}")) 42 | if err != nil { 43 | t.Errorf("http.Post(%q) failed with %v; want success", url, err) 44 | return 45 | } 46 | defer resp.Body.Close() 47 | buf, err := ioutil.ReadAll(resp.Body) 48 | if err != nil { 49 | t.Errorf("iotuil.ReadAll(resp.Body) failed with %v; want success", err) 50 | return 51 | } 52 | 53 | if got, want := resp.StatusCode, http.StatusOK; got != want { 54 | t.Errorf("resp.StatusCode = %d; want %d", got, want) 55 | t.Logf("%s", buf) 56 | } 57 | 58 | var reply gateway.HelloReply 59 | if err := jsonpb.UnmarshalString(string(buf), &reply); err != nil { 60 | t.Errorf("jsonpb.UnmarshalString(%s, &reply) failed with %v; want success", buf, err) 61 | return 62 | } 63 | if got, want := reply.Message, "Hello foo"; got != want { 64 | t.Errorf("msg.message = %q; want %q", got, want) 65 | } 66 | } 67 | 68 | func startServer() { 69 | lis, err := net.Listen("tcp", grpc_port) 70 | if err != nil { 71 | log.Fatalf("failed to listen: %v", err) 72 | } 73 | s := grpc.NewServer() 74 | pb.RegisterGreeterServer(s, &greeter.Server{}) //pb.Registergreeterserver 75 | s.Serve(lis) 76 | } 77 | 78 | func startGateway() error { 79 | ctx := context.Background() 80 | ctx, cancel := context.WithCancel(ctx) 81 | defer cancel() 82 | 83 | mux := runtime.NewServeMux() 84 | opts := []grpc.DialOption{grpc.WithInsecure()} 85 | err := gateway.RegisterGreeterHandlerFromEndpoint(ctx, mux, "localhost:50051", opts) 86 | if err != nil { 87 | return err 88 | } 89 | 90 | http.ListenAndServe(http_port, mux) 91 | return nil 92 | } 93 | -------------------------------------------------------------------------------- /examples/helloworld/grpc_gateway/helloworld.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | 3 | import "google/api/annotations.proto"; 4 | 5 | package helloworld; 6 | 7 | // The greeting service definition. The definition is repeated here 8 | // rather than re-using examples/helloworld/proto/helloworld.proto so 9 | // the other implementations / tests don't have to account for the 10 | // annotations import. 11 | 12 | service Greeter { 13 | // Sends a greeting 14 | rpc SayHello (HelloRequest) returns (HelloReply) { 15 | option (google.api.http) = { 16 | post: "/v1/helloworld/sayhello" 17 | body: "*" 18 | }; 19 | } 20 | } 21 | 22 | // The request message containing the user's name. 23 | message HelloRequest { 24 | string name = 1; 25 | } 26 | 27 | // The response message containing the greetings 28 | message HelloReply { 29 | string message = 1; 30 | } 31 | -------------------------------------------------------------------------------- /examples/helloworld/java/org/pubref/rules_protobuf/examples/helloworld/client/BUILD: -------------------------------------------------------------------------------- 1 | java_test( 2 | name = "netty_test", 3 | size = "small", 4 | srcs = ["HelloWorldClientTest.java"], 5 | tags = ["exclusive"], 6 | test_class = "org.pubref.rules_protobuf.examples.helloworld.client.HelloWorldClientTest", 7 | runtime_deps = [ 8 | "//java:netty_runtime_deps", 9 | ], 10 | deps = [ 11 | ":client", 12 | "//examples/helloworld/java/org/pubref/rules_protobuf/examples/helloworld/server", 13 | "@junit_junit_4//jar", 14 | ], 15 | ) 16 | 17 | java_binary( 18 | name = "netty", 19 | main_class = "org.pubref.rules_protobuf.examples.helloworld.client.HelloWorldClient", 20 | runtime_deps = [ 21 | ":client", 22 | "//java:netty_runtime_deps", 23 | ], 24 | ) 25 | 26 | java_library( 27 | name = "client", 28 | srcs = ["HelloWorldClient.java"], 29 | deps = [ 30 | # This rule automatically exports //java:grpc_compiletime_deps. 31 | "//examples/helloworld/proto:java", 32 | ], 33 | ) 34 | -------------------------------------------------------------------------------- /examples/helloworld/java/org/pubref/rules_protobuf/examples/helloworld/client/HelloWorldClient.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015, Google Inc, Pubref Inc. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions are 6 | * met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above 11 | * copyright notice, this list of conditions and the following disclaimer 12 | * in the documentation and/or other materials provided with the 13 | * distribution. 14 | * 15 | * * Neither the name of Google Inc. nor the names of its 16 | * contributors may be used to endorse or promote products derived from 17 | * this software without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | package org.pubref.rules_protobuf.examples.helloworld.client; 33 | 34 | import io.grpc.ManagedChannel; 35 | import io.grpc.ManagedChannelBuilder; 36 | import io.grpc.StatusRuntimeException; 37 | 38 | import java.util.concurrent.TimeUnit; 39 | import java.util.logging.Level; 40 | import java.util.logging.Logger; 41 | 42 | import org.pubref.rules_protobuf.examples.helloworld.GreeterGrpc; 43 | import org.pubref.rules_protobuf.examples.helloworld.HelloRequest; 44 | import org.pubref.rules_protobuf.examples.helloworld.HelloReply; 45 | 46 | /** 47 | * A simple client that requests a greeting from the {@link 48 | * HelloWorldServer}. 49 | */ 50 | public class HelloWorldClient { 51 | private static final Logger logger = Logger.getLogger(HelloWorldClient.class.getName()); 52 | 53 | private final ManagedChannel channel; 54 | private final GreeterGrpc.GreeterBlockingStub blockingStub; 55 | 56 | /** Construct client connecting to HelloWorld server at {@code host:port}. */ 57 | public HelloWorldClient(String host, int port) { 58 | channel = ManagedChannelBuilder.forAddress(host, port) 59 | .usePlaintext(true) 60 | .build(); 61 | blockingStub = GreeterGrpc.newBlockingStub(channel); 62 | } 63 | 64 | public void shutdown() throws InterruptedException { 65 | channel.shutdown().awaitTermination(5, TimeUnit.SECONDS); 66 | } 67 | 68 | /** Say hello to server. */ 69 | public String greet(String name) { 70 | logger.info("Will try to greet " + name + " ..."); 71 | HelloRequest request = HelloRequest.newBuilder().setName(name).build(); 72 | HelloReply response; 73 | try { 74 | response = blockingStub.sayHello(request); 75 | logger.info("Greeting: " + response.getMessage()); 76 | return response.getMessage(); 77 | } catch (StatusRuntimeException e) { 78 | String msg = "RPC failed: " + e.getStatus(); 79 | logger.log(Level.WARNING, msg); 80 | return msg; 81 | } 82 | } 83 | 84 | /** 85 | * Greet server. If provided, the first element of {@code args} is 86 | * the name to use in the greeting. 87 | */ 88 | public static void main(String[] args) throws Exception { 89 | HelloWorldClient client = new HelloWorldClient("localhost", 50051); 90 | try { 91 | /* Access a service running on the local machine on port 50051 */ 92 | String user = "world"; 93 | if (args.length > 0) { 94 | user = args[0]; /* Use the arg as the name to greet if provided */ 95 | } 96 | client.greet(user); 97 | } finally { 98 | client.shutdown(); 99 | } 100 | } 101 | 102 | } 103 | -------------------------------------------------------------------------------- /examples/helloworld/java/org/pubref/rules_protobuf/examples/helloworld/client/HelloWorldClientTest.java: -------------------------------------------------------------------------------- 1 | package org.pubref.rules_protobuf.examples.helloworld.client; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | import static org.junit.Assert.assertFalse; 5 | import static org.junit.Assert.assertTrue; 6 | import static org.junit.Assert.assertNotNull; 7 | 8 | import org.junit.After; 9 | import org.junit.Before; 10 | import org.junit.BeforeClass; 11 | import org.junit.AfterClass; 12 | import org.junit.Ignore; 13 | import org.junit.Test; 14 | 15 | import org.pubref.rules_protobuf.examples.helloworld.server.HelloWorldServer; 16 | 17 | /** 18 | * A simple integration test that runs the server at startup. 19 | */ 20 | public class HelloWorldClientTest { 21 | 22 | static HelloWorldServer server; 23 | 24 | HelloWorldClient client; 25 | 26 | @BeforeClass 27 | public static void init() throws Exception { 28 | server = new HelloWorldServer(); 29 | server.start(); 30 | } 31 | 32 | @AfterClass 33 | public static void destroy() throws InterruptedException { 34 | server.stop(); 35 | } 36 | 37 | @Before 38 | public void setUp() throws Exception { 39 | client = new HelloWorldClient("localhost", 50051); 40 | } 41 | 42 | @After 43 | public void tearDown() throws InterruptedException { 44 | client.shutdown(); 45 | } 46 | 47 | @Test 48 | public void testGreet() throws InterruptedException { 49 | String msg = client.greet("world"); 50 | assertEquals("Blocking message should return correct string", "Hello world", msg); 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /examples/helloworld/java/org/pubref/rules_protobuf/examples/helloworld/server/BUILD: -------------------------------------------------------------------------------- 1 | package(default_visibility = ["//visibility:public"]) 2 | 3 | java_test( 4 | name = "netty_test", 5 | size = "small", 6 | srcs = ["HelloWorldServerTest.java"], 7 | tags = ["exclusive"], # Address already in use if another server being tested has same port 8 | test_class = "org.pubref.rules_protobuf.examples.helloworld.server.HelloWorldServerTest", 9 | runtime_deps = [ 10 | "//java:netty_runtime_deps", 11 | ], 12 | deps = [ 13 | ":server", 14 | "@junit_junit_4//jar", 15 | ], 16 | ) 17 | 18 | java_binary( 19 | name = "netty", 20 | main_class = "org.pubref.rules_protobuf.examples.helloworld.server.HelloWorldServer", 21 | runtime_deps = [ 22 | ":server", 23 | "//java:netty_runtime_deps", 24 | ], 25 | ) 26 | 27 | java_library( 28 | name = "server", 29 | srcs = ["HelloWorldServer.java"], 30 | deps = [ 31 | # This rule automatically exports //java:grpc_compiletime_deps. 32 | "//examples/helloworld/proto:java", 33 | ], 34 | ) 35 | -------------------------------------------------------------------------------- /examples/helloworld/java/org/pubref/rules_protobuf/examples/helloworld/server/HelloWorldServer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016, Google Inc., Pubref Inc. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions are 6 | * met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above 11 | * copyright notice, this list of conditions and the following disclaimer 12 | * in the documentation and/or other materials provided with the 13 | * distribution. 14 | * 15 | * * Neither the name of Google Inc. nor the names of its 16 | * contributors may be used to endorse or promote products derived from 17 | * this software without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | package org.pubref.rules_protobuf.examples.helloworld.server; 32 | 33 | import io.grpc.Server; 34 | import io.grpc.ServerBuilder; 35 | import io.grpc.stub.StreamObserver; 36 | 37 | import java.io.IOException; 38 | import java.util.logging.Logger; 39 | 40 | import org.pubref.rules_protobuf.examples.helloworld.GreeterGrpc; 41 | import org.pubref.rules_protobuf.examples.helloworld.HelloRequest; 42 | import org.pubref.rules_protobuf.examples.helloworld.HelloReply; 43 | 44 | /** 45 | * Server that manages startup/shutdown of a {@code Greeter} server. 46 | */ 47 | public class HelloWorldServer { 48 | private static final Logger logger = Logger.getLogger(HelloWorldServer.class.getName()); 49 | 50 | /* The port on which the server should run */ 51 | private final int port; 52 | private Server server; 53 | 54 | public HelloWorldServer() { 55 | this(50051); 56 | } 57 | 58 | public HelloWorldServer(int port) { 59 | this.port = port; 60 | } 61 | 62 | public void start() throws IOException { 63 | server = ServerBuilder.forPort(port) 64 | .addService(new GreeterImpl()) 65 | .build() 66 | .start(); 67 | logger.info("Server started, listening on " + port); 68 | Runtime.getRuntime().addShutdownHook(new Thread() { 69 | @Override 70 | public void run() { 71 | // Use stderr here since the logger may have been reset by its JVM shutdown hook. 72 | System.err.println("*** shutting down gRPC server since JVM is shutting down"); 73 | HelloWorldServer.this.stop(); 74 | System.err.println("*** server shut down"); 75 | } 76 | }); 77 | } 78 | 79 | public void stop() { 80 | if (server != null) { 81 | server.shutdown(); 82 | } 83 | } 84 | 85 | /** 86 | * Await termination on the main thread since the grpc library uses daemon threads. 87 | */ 88 | private void blockUntilShutdown() throws InterruptedException { 89 | if (server != null) { 90 | server.awaitTermination(); 91 | } 92 | } 93 | 94 | /** 95 | * Main launches the server from the command line. 96 | */ 97 | public static void main(String[] args) throws IOException, InterruptedException { 98 | final HelloWorldServer server = new HelloWorldServer(); 99 | server.start(); 100 | server.blockUntilShutdown(); 101 | } 102 | 103 | private class GreeterImpl extends GreeterGrpc.GreeterImplBase { 104 | 105 | @Override 106 | public void sayHello(HelloRequest req, StreamObserver responseObserver) { 107 | HelloReply reply = HelloReply.newBuilder().setMessage("Hello " + req.getName()).build(); 108 | responseObserver.onNext(reply); 109 | responseObserver.onCompleted(); 110 | } 111 | 112 | } 113 | 114 | } 115 | -------------------------------------------------------------------------------- /examples/helloworld/java/org/pubref/rules_protobuf/examples/helloworld/server/HelloWorldServerTest.java: -------------------------------------------------------------------------------- 1 | package org.pubref.rules_protobuf.examples.helloworld.server; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | import static org.junit.Assert.assertFalse; 5 | import static org.junit.Assert.assertTrue; 6 | import static org.junit.Assert.assertNotNull; 7 | 8 | import org.junit.After; 9 | import org.junit.Before; 10 | import org.junit.BeforeClass; 11 | import org.junit.Ignore; 12 | import org.junit.Test; 13 | 14 | 15 | /** 16 | * A simple test that primarily checks that the server can compile. 17 | */ 18 | public class HelloWorldServerTest { 19 | 20 | HelloWorldServer server; 21 | 22 | @Before 23 | public void setUp() throws Exception { 24 | server = new HelloWorldServer(); 25 | server.start(); 26 | } 27 | 28 | @After 29 | public void tearDown() throws InterruptedException { 30 | } 31 | 32 | @Test 33 | public void testNothing() throws InterruptedException { 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /examples/helloworld/node/BUILD: -------------------------------------------------------------------------------- 1 | load("@org_pubref_rules_node//node:rules.bzl", "node_binary") 2 | 3 | node_binary( 4 | name = "client", 5 | main = "greeter_client.js", 6 | deps = [ 7 | "//examples/helloworld/proto:node", 8 | "@yarn_modules//:_all_", 9 | ], 10 | ) 11 | 12 | node_binary( 13 | name = "server", 14 | main = "greeter_server.js", 15 | deps = [ 16 | "//examples/helloworld/proto:node", 17 | "@yarn_modules//:_all_", 18 | ], 19 | ) 20 | -------------------------------------------------------------------------------- /examples/helloworld/node/greeter_client.js: -------------------------------------------------------------------------------- 1 | const grpc = require('grpc'); 2 | const messages = require('examples/helloworld/node/api_proto_js').api_pb; 3 | const services = require('examples/helloworld/node/api_proto_js').api_grpc_pb; 4 | 5 | function main() { 6 | var client = new services.GreeterClient( 7 | 'localhost:50051', 8 | grpc.credentials.createInsecure()); 9 | var request = new messages.HelloRequest(); 10 | var user; 11 | if (process.argv.length >= 3) { 12 | user = process.argv[2]; 13 | } else { 14 | user = 'world'; 15 | } 16 | request.setName(user); 17 | client.sayHello(request, function(err, response) { 18 | if (err) { 19 | console.error('Error:', err); 20 | } else { 21 | console.log('Greeting:', response.getMessage()); 22 | } 23 | }); 24 | } 25 | 26 | main(); 27 | -------------------------------------------------------------------------------- /examples/helloworld/node/greeter_server.js: -------------------------------------------------------------------------------- 1 | const grpc = require('grpc'); 2 | const messages = require('examples/helloworld/node/api_proto_js').api_pb; 3 | const services = require('examples/helloworld/node/api_proto_js').api_grpc_pb; 4 | 5 | /** 6 | * Implements the SayHello RPC method. 7 | */ 8 | function sayHello(call, callback) { 9 | var reply = new messages.HelloReply(); 10 | reply.setMessage('Hello ' + call.request.getName()); 11 | callback(null, reply); 12 | } 13 | 14 | /** 15 | * Starts an RPC server that receives requests for the Greeter service at the 16 | * sample server port 17 | */ 18 | function main() { 19 | var server = new grpc.Server(); 20 | server.addService(services.GreeterService, { sayHello: sayHello }); 21 | server.bind('0.0.0.0:50051', grpc.ServerCredentials.createInsecure()); 22 | server.start(); 23 | } 24 | 25 | main(); 26 | -------------------------------------------------------------------------------- /examples/helloworld/proto/BUILD: -------------------------------------------------------------------------------- 1 | package(default_visibility = ["//visibility:public"]) 2 | 3 | load("//cpp:rules.bzl", "cc_proto_library") 4 | load("//csharp:rules.bzl", "csharp_proto_library") 5 | load("//go:rules.bzl", "go_proto_library") 6 | load("//gogo:rules.bzl", "gogo_proto_library") 7 | load("//java:rules.bzl", "java_proto_library") 8 | load("//node:rules.bzl", "node_proto_library") 9 | load("//objc:rules.bzl", "objc_proto_compile") 10 | load("//protobuf:rules.bzl", "proto_compile") 11 | load("//python:rules.bzl", "py_proto_library") 12 | load("//ruby:rules.bzl", "ruby_proto_compile") 13 | 14 | filegroup( 15 | name = "protos", 16 | srcs = ["helloworld.proto"], 17 | ) 18 | 19 | cc_proto_library( 20 | name = "cpp", 21 | proto_deps = [ 22 | "//examples/proto:cpp", 23 | ], 24 | protos = [":protos"], 25 | verbose = 0, 26 | with_grpc = True, 27 | ) 28 | 29 | csharp_proto_library( 30 | name = "Helloworld", 31 | proto_deps = [ 32 | "//examples/proto:csharp", 33 | ], 34 | protos = [":protos"], 35 | verbose = 0, 36 | with_grpc = True, 37 | ) 38 | 39 | go_proto_library( 40 | name = "go", 41 | proto_deps = [ 42 | "//examples/proto:go_default_library", 43 | ], 44 | protos = [":protos"], 45 | verbose = 0, 46 | with_grpc = True, 47 | ) 48 | 49 | java_proto_library( 50 | name = "java", 51 | proto_deps = [ 52 | "//examples/proto:java", 53 | ], 54 | protos = [":protos"], 55 | verbose = 0, 56 | with_grpc = True, 57 | ) 58 | 59 | # Commented out as this conflicts with go (same output file). 60 | # 61 | # gogo_proto_library( 62 | # name = "gogo", 63 | # proto_deps = [ 64 | # "//examples/proto:go_default_library", 65 | # ], 66 | # protos = [":protos"], 67 | # verbose = 1, 68 | # with_grpc = True, 69 | # ) 70 | 71 | node_proto_library( 72 | name = "node", 73 | proto_deps = [ 74 | "//examples/proto:node", 75 | ], 76 | protos = [":protos"], 77 | verbose = 0, 78 | with_grpc = True, 79 | ) 80 | 81 | objc_proto_compile( 82 | name = "objc", 83 | protos = [":protos"], 84 | with_grpc = True, 85 | deps = [ 86 | "//examples/proto:objc", 87 | ], 88 | ) 89 | 90 | py_proto_library( 91 | name = "py", 92 | proto_deps = [ 93 | "//examples/proto:py", 94 | ], 95 | protos = [ 96 | ":protos", 97 | ], 98 | with_grpc = True, 99 | ) 100 | 101 | ruby_proto_compile( 102 | name = "ruby", 103 | protos = [":protos"], 104 | with_grpc = True, 105 | deps = [ 106 | "//examples/proto:ruby", 107 | ], 108 | ) 109 | -------------------------------------------------------------------------------- /examples/helloworld/proto/helloworld.proto: -------------------------------------------------------------------------------- 1 | 2 | syntax = "proto3"; 3 | 4 | option java_multiple_files = true; 5 | option java_package = "org.pubref.rules_protobuf.examples.helloworld"; 6 | option java_outer_classname = "HelloWorldProto"; 7 | 8 | import "examples/proto/common.proto"; 9 | 10 | package helloworld; 11 | 12 | // The greeting service definition. 13 | service Greeter { 14 | // Sends a greeting 15 | rpc SayHello (HelloRequest) returns (HelloReply) {} 16 | } 17 | 18 | // The request message containing the user's name. 19 | message HelloRequest { 20 | string name = 1; 21 | common.Config config = 2; 22 | } 23 | 24 | // The response message containing the greetings 25 | message HelloReply { 26 | string message = 1; 27 | } 28 | -------------------------------------------------------------------------------- /examples/helloworld/python/BUILD: -------------------------------------------------------------------------------- 1 | package(default_visibility = ["//visibility:public"]) 2 | 3 | load("@pip_grpcio//:requirements.bzl", "requirement") 4 | load("@io_bazel_rules_python//python:python.bzl", "py_binary", "py_library") 5 | 6 | py_binary( 7 | name = "greeter_client", 8 | srcs = [ 9 | "greeter_client.py", 10 | ], 11 | deps = [ 12 | "//examples/helloworld/proto:py", 13 | requirement("grpcio"), 14 | requirement("protobuf"), 15 | ], 16 | ) 17 | 18 | py_binary( 19 | name = "greeter_server", 20 | srcs = [":grpc_server"], 21 | ) 22 | 23 | py_test( 24 | name = "test_greeter_server", 25 | srcs = ["test_greeter_server.py"], 26 | deps = [":grpc_server"], 27 | size = "small", 28 | ) 29 | 30 | # This library is necessary for creating py_test cases o/w if we explicitly add 31 | # "//examples/helloworld/proto:py" in the 'deps' list then bazel complains with: 32 | # "'//examples/helloworld/proto:py' does not have mandatory provider 'py'". 33 | # It's also useful for packaging all the server code in a lib to reuse for the :greeter_server. 34 | py_library( 35 | name = "grpc_server", 36 | srcs = [ 37 | "greeter_server.py", 38 | ], 39 | deps = [ 40 | "//examples/helloworld/proto:py", 41 | requirement("grpcio"), 42 | requirement("protobuf"), 43 | ], 44 | ) 45 | -------------------------------------------------------------------------------- /examples/helloworld/python/greeter_client.py: -------------------------------------------------------------------------------- 1 | # Copyright 2015, Google Inc. 2 | # All rights reserved. 3 | # 4 | # Redistribution and use in source and binary forms, with or without 5 | # modification, are permitted provided that the following conditions are 6 | # met: 7 | # 8 | # * Redistributions of source code must retain the above copyright 9 | # notice, this list of conditions and the following disclaimer. 10 | # * Redistributions in binary form must reproduce the above 11 | # copyright notice, this list of conditions and the following disclaimer 12 | # in the documentation and/or other materials provided with the 13 | # distribution. 14 | # * Neither the name of Google Inc. nor the names of its 15 | # contributors may be used to endorse or promote products derived from 16 | # this software without specific prior written permission. 17 | # 18 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | 30 | """The Python implementation of the GRPC helloworld.Greeter client.""" 31 | 32 | import grpc 33 | 34 | from examples.helloworld.proto import helloworld_pb2 35 | from examples.helloworld.proto import helloworld_pb2_grpc 36 | 37 | 38 | def run(): 39 | channel = grpc.insecure_channel('localhost:50051') 40 | stub = helloworld_pb2_grpc.GreeterStub(channel) 41 | response = stub.SayHello(helloworld_pb2.HelloRequest(name='you')) 42 | print("Greeter client received: " + response.message) 43 | 44 | if __name__ == '__main__': 45 | run() 46 | -------------------------------------------------------------------------------- /examples/helloworld/python/greeter_server.py: -------------------------------------------------------------------------------- 1 | # Copyright 2015, Google Inc. 2 | # All rights reserved. 3 | # 4 | # Redistribution and use in source and binary forms, with or without 5 | # modification, are permitted provided that the following conditions are 6 | # met: 7 | # 8 | # * Redistributions of source code must retain the above copyright 9 | # notice, this list of conditions and the following disclaimer. 10 | # * Redistributions in binary form must reproduce the above 11 | # copyright notice, this list of conditions and the following disclaimer 12 | # in the documentation and/or other materials provided with the 13 | # distribution. 14 | # * Neither the name of Google Inc. nor the names of its 15 | # contributors may be used to endorse or promote products derived from 16 | # this software without specific prior written permission. 17 | # 18 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | 30 | """The Python implementation of the GRPC helloworld.Greeter server.""" 31 | 32 | from __future__ import print_function 33 | 34 | import grpc 35 | import time 36 | 37 | from concurrent import futures 38 | from examples.helloworld.proto import helloworld_pb2 39 | from examples.helloworld.proto import helloworld_pb2_grpc 40 | 41 | _ONE_DAY_IN_SECONDS = 60 * 60 * 24 42 | 43 | 44 | class _GreeterServer(object): 45 | 46 | def __init__(self, greeter_service, server_port): 47 | self.server = grpc.server(futures.ThreadPoolExecutor(max_workers=10)) 48 | helloworld_pb2_grpc.add_GreeterServicer_to_server(greeter_service, self.server) 49 | self.server.add_insecure_port('[::]:{server_port}'.format(server_port=server_port)) 50 | 51 | def start(self): 52 | self.server.start() 53 | 54 | def stop(self): 55 | self.server.stop(0) 56 | 57 | def await_termination(self): 58 | """ 59 | server.start() doesn't block so we explicitly block here unless someone keyboard-exits us. 60 | :return: 61 | """ 62 | try: 63 | while True: 64 | time.sleep(_ONE_DAY_IN_SECONDS) 65 | except KeyboardInterrupt: 66 | self.server.stop(0) 67 | pass 68 | 69 | 70 | class _GreeterService(helloworld_pb2_grpc.GreeterServicer): 71 | 72 | def SayHello(self, hello_request, context): 73 | print("Greeter server received: " + hello_request.name) 74 | hello_reply = helloworld_pb2.HelloReply() 75 | hello_reply.message = 'Hello {name}'.format(name=hello_request.name) 76 | return hello_reply 77 | 78 | 79 | def main(): 80 | greeter_server = _GreeterServer(_GreeterService(), 50051) 81 | greeter_server.start() 82 | greeter_server.await_termination() 83 | 84 | if __name__ == '__main__': 85 | main() 86 | -------------------------------------------------------------------------------- /examples/helloworld/python/test_greeter_server.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | import grpc 4 | import greeter_server 5 | 6 | from examples.helloworld.proto import helloworld_pb2 7 | from examples.helloworld.proto import helloworld_pb2_grpc 8 | 9 | 10 | def _get_random_port(): 11 | import socket 12 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 13 | s.bind(("", 0)) 14 | s.listen(1) 15 | port = s.getsockname()[1] 16 | s.close() 17 | return port 18 | 19 | TEST_PORT = _get_random_port() 20 | 21 | 22 | class GreeterServerTest(unittest.TestCase): 23 | _server = None 24 | _client = None 25 | 26 | def setUp(self): 27 | self._server = greeter_server._GreeterServer(greeter_server._GreeterService(), TEST_PORT) 28 | self._server.start() 29 | channel = grpc.insecure_channel('localhost:{port}'.format(port=TEST_PORT)) 30 | self._client = helloworld_pb2_grpc.GreeterStub(channel) 31 | 32 | def tearDown(self): 33 | self._server.stop() 34 | 35 | def test_sayhello(self): 36 | hello_reply = self._client.SayHello(helloworld_pb2.HelloRequest(name='you')) 37 | self.assertEqual(hello_reply.message, 'Hello you') 38 | 39 | if __name__ == '__main__': 40 | unittest.main() 41 | -------------------------------------------------------------------------------- /examples/proto/BUILD: -------------------------------------------------------------------------------- 1 | package(default_visibility = ["//visibility:public"]) 2 | 3 | load("//closure:rules.bzl", "closure_proto_library") 4 | load("//cpp:rules.bzl", "cc_proto_library") 5 | load("//csharp:rules.bzl", "csharp_proto_compile", "csharp_proto_library") 6 | load("//go:rules.bzl", "go_proto_library") 7 | load("//java:rules.bzl", "java_proto_library") 8 | load("//node:rules.bzl", "node_proto_compile", "node_proto_library") 9 | load("//objc:rules.bzl", "objc_proto_compile") 10 | load("//python:rules.bzl", "py_proto_library") 11 | load("//protobuf:rules.bzl", "proto_compile") 12 | load("//ruby:rules.bzl", "ruby_proto_compile") 13 | 14 | filegroup( 15 | name = "protos", 16 | srcs = [ 17 | "common.proto", 18 | ], 19 | ) 20 | 21 | cc_proto_library( 22 | name = "cpp", 23 | protos = [":protos"], 24 | ) 25 | 26 | csharp_proto_library( 27 | name = "csharp", 28 | protos = [":protos"], 29 | verbose = 0, 30 | with_grpc = False, 31 | ) 32 | 33 | go_proto_library( 34 | name = "go_default_library", 35 | protos = [":protos"], 36 | verbose = 0, 37 | ) 38 | 39 | java_proto_library( 40 | name = "java", 41 | protos = [":protos"], 42 | ) 43 | 44 | node_proto_library( 45 | name = "node", 46 | protos = [":protos"], 47 | verbose = 0, 48 | with_grpc = False, 49 | ) 50 | 51 | py_proto_library( 52 | name = "py", 53 | protos = [":protos"], 54 | verbose = 0, 55 | with_grpc = False, 56 | ) 57 | 58 | objc_proto_compile( 59 | name = "objc", 60 | protos = [":protos"], 61 | verbose = 0, 62 | with_grpc = False, 63 | ) 64 | 65 | ruby_proto_compile( 66 | name = "ruby", 67 | protos = [":protos"], 68 | verbose = 0, 69 | with_grpc = False, 70 | ) 71 | 72 | # This conflicts with outputs from other rules here but demonstrates 73 | # how to generate multiple language outputs simultaneously. 74 | # 75 | # proto_compile( 76 | # name = "pluriproto", 77 | # protos = [":protos"], 78 | # langs = [ 79 | # "//ruby", 80 | # "//java", 81 | # "//java:nano", 82 | # "//python", 83 | # "//cpp", 84 | # "//objc", 85 | # "//closure", 86 | # "//csharp", 87 | # "//go", 88 | # "//node", 89 | # ], 90 | # verbose = 2, 91 | # with_grpc = True, 92 | # ) 93 | -------------------------------------------------------------------------------- /examples/proto/README.md: -------------------------------------------------------------------------------- 1 | This directory contains proto files that are common to different 2 | examples. The main motivation is testing of proto imports and 3 | proto::proto dependencies. 4 | -------------------------------------------------------------------------------- /examples/proto/common.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | 3 | option java_package = "org.pubref.rules_protobuf.examples"; 4 | option java_outer_classname = "CommonProto"; 5 | 6 | package common; 7 | 8 | // A configuration object. This is used to test the viability of 9 | // protobuf imports. 10 | message Config { 11 | bool verbose = 1; 12 | } 13 | 14 | -------------------------------------------------------------------------------- /examples/wkt/go/BUILD: -------------------------------------------------------------------------------- 1 | package(default_visibility = ["//visibility:public"]) 2 | 3 | load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_test") 4 | load("//go:rules.bzl", "go_proto_library") 5 | 6 | # 7 | # Demonstration of importing the protobuf well-known types. 8 | # 9 | 10 | go_proto_library( 11 | name = "protolib", 12 | importmap = { 13 | "google/protobuf/descriptor.proto": "github.com/golang/protobuf/protoc-gen-go/descriptor", 14 | "google/protobuf/compiler/plugin.proto": "github.com/golang/protobuf/protoc-gen-go/plugin", 15 | }, 16 | imports = [ 17 | "external/com_google_protobuf/src", 18 | ], 19 | inputs = [ 20 | "@com_google_protobuf//:well_known_protos", 21 | ], 22 | protos = ["wkt.proto"], 23 | #verbose = 3, 24 | deps = [ 25 | "@com_github_golang_protobuf//protoc-gen-go/descriptor:go_default_library", 26 | "@com_github_golang_protobuf//protoc-gen-go/plugin:go_default_library", 27 | "@com_github_golang_protobuf//ptypes/any:go_default_library", 28 | "@com_github_golang_protobuf//ptypes/duration:go_default_library", 29 | "@com_github_golang_protobuf//ptypes/empty:go_default_library", 30 | "@com_github_golang_protobuf//ptypes/struct:go_default_library", 31 | "@com_github_golang_protobuf//ptypes/timestamp:go_default_library", 32 | "@com_github_golang_protobuf//ptypes/wrappers:go_default_library", 33 | ], 34 | ) 35 | 36 | go_test( 37 | name = "wkt_test", 38 | size = "small", 39 | srcs = ["wkt_test.go"], 40 | deps = [ 41 | "protolib", 42 | "@com_github_golang_protobuf//protoc-gen-go/descriptor:go_default_library", 43 | ], 44 | ) 45 | -------------------------------------------------------------------------------- /examples/wkt/go/wkt.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | 3 | import "google/protobuf/any.proto"; 4 | import "google/protobuf/duration.proto"; 5 | import "google/protobuf/timestamp.proto"; 6 | import "google/protobuf/empty.proto"; 7 | import "google/protobuf/struct.proto"; 8 | import "google/protobuf/wrappers.proto"; 9 | import "google/protobuf/descriptor.proto"; 10 | import "google/protobuf/compiler/plugin.proto"; 11 | 12 | message Message { 13 | string id = 1; 14 | google.protobuf.FileDescriptorProto file = 2; 15 | google.protobuf.Any any = 3; 16 | google.protobuf.Duration duration = 4; 17 | google.protobuf.Timestamp ts = 5; 18 | google.protobuf.Empty empty = 6; 19 | google.protobuf.Struct struct = 7; 20 | google.protobuf.BytesValue bytes_wrapper = 8; 21 | google.protobuf.compiler.CodeGeneratorRequest code_generator_request = 9; 22 | } 23 | 24 | extend google.protobuf.MessageOptions { 25 | string my_option = 10001; 26 | } 27 | -------------------------------------------------------------------------------- /examples/wkt/go/wkt_test.go: -------------------------------------------------------------------------------- 1 | package wkt_test 2 | 3 | import ( 4 | "testing" 5 | wkt "github.com/pubref/rules_protobuf/examples/wkt/go/protolib" 6 | fd "github.com/golang/protobuf/protoc-gen-go/descriptor" 7 | ) 8 | 9 | 10 | func TestWkt(t *testing.T) { 11 | filename := "wkt.proto" 12 | 13 | message := &wkt.Message{ 14 | Id: "foo", 15 | File: &fd.FileDescriptorProto{ 16 | Name: &filename, 17 | }, 18 | } 19 | 20 | if message.Id != "foo" { 21 | t.Error("Expected foo, got ", message.Id) 22 | } 23 | 24 | if *message.File.Name != filename { 25 | t.Error("Expected %s, got %s", filename, message.File.Name) 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /go/BUILD: -------------------------------------------------------------------------------- 1 | package(default_visibility = ["//visibility:public"]) 2 | 3 | load("//protobuf:rules.bzl", "proto_language") 4 | 5 | proto_language( 6 | name = "go", 7 | go_prefix = "//:go_prefix", 8 | pb_file_extensions = [".pb.go"], 9 | pb_plugin = "@com_github_golang_protobuf//protoc-gen-go", 10 | pb_plugin_implements_grpc = True, 11 | ) 12 | -------------------------------------------------------------------------------- /go/deps.bzl: -------------------------------------------------------------------------------- 1 | # **************************************************************** 2 | # List of external dependencies 3 | # **************************************************************** 4 | 5 | # These deps are derived empirically, starting from the grpc tag and 6 | # then updating other dependents to the master commit. 7 | 8 | DEPS = { 9 | 10 | "org_golang_google_grpc": { 11 | "rule": "go_repository", 12 | "importpath": "google.golang.org/grpc", 13 | "tag": "v1.6.0", 14 | }, 15 | 16 | "org_golang_google_genproto": { 17 | "rule": "go_repository", 18 | "importpath": "google.golang.org/genproto", 19 | "commit": "595979c8a7bf586b2d293fb42246bf91a0b893d9", 20 | }, 21 | 22 | "org_golang_x_net": { 23 | "rule": "go_repository", 24 | "importpath": "golang.org/x/net", 25 | "commit": "66aacef3dd8a676686c7ae3716979581e8b03c47", # Aug 28, 2017 26 | }, 27 | 28 | "org_golang_x_text": { 29 | "rule": "go_repository", 30 | "importpath": "golang.org/x/text", 31 | "commit": "bd91bbf73e9a4a801adbfb97133c992678533126", # Aug 31, 2017 32 | }, 33 | 34 | "com_github_golang_glog": { 35 | "rule": "go_repository", 36 | "importpath": "github.com/golang/glog", 37 | "commit": "23def4e6c14b4da8ac2ed8007337bc5eb5007998", # Jan 25, 2016 38 | }, 39 | 40 | "com_github_golang_protobuf": { 41 | "rule": "go_repository", 42 | "importpath": "github.com/golang/protobuf", 43 | "commit": "17ce1425424ab154092bbb43af630bd647f3bb0d", # Sept 1, 2017 44 | }, 45 | 46 | } 47 | -------------------------------------------------------------------------------- /go/rules.bzl: -------------------------------------------------------------------------------- 1 | load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_repository") 2 | load("//protobuf:rules.bzl", "proto_compile", "proto_repositories") 3 | load("//go:deps.bzl", "DEPS") 4 | 5 | def go_proto_repositories( 6 | lang_deps = DEPS, 7 | lang_requires = [ 8 | "com_github_golang_protobuf", 9 | "com_github_golang_glog", 10 | "org_golang_google_grpc", 11 | "org_golang_google_genproto", 12 | "org_golang_x_net", 13 | "org_golang_x_text", 14 | ], **kwargs): 15 | 16 | rem = proto_repositories(lang_deps = lang_deps, 17 | lang_requires = lang_requires, 18 | **kwargs) 19 | 20 | # Load remaining (special) deps 21 | for dep in rem: 22 | rule = dep.pop("rule") 23 | if "go_repository" == rule: 24 | go_repository(**dep) 25 | else: 26 | fail("Unknown loading rule %s for %s" % (rule, dep)) 27 | 28 | 29 | PB_COMPILE_DEPS = [ 30 | "@com_github_golang_protobuf//proto:go_default_library", 31 | ] 32 | 33 | GRPC_COMPILE_DEPS = PB_COMPILE_DEPS + [ 34 | "@com_github_golang_glog//:go_default_library", 35 | "@org_golang_google_grpc//:go_default_library", 36 | "@org_golang_x_net//context:go_default_library", 37 | ] 38 | 39 | 40 | def go_proto_compile(langs = [str(Label("//go"))], **kwargs): 41 | proto_compile(langs = langs, **kwargs) 42 | 43 | def go_proto_library( 44 | name, 45 | langs = [str(Label("//go"))], 46 | go_prefix = Label("//:go_prefix", relative_to_caller_repository=True), 47 | importpath = None, 48 | go_package = None, 49 | protos = [], 50 | importmap = {}, 51 | imports = [], 52 | inputs = [], 53 | proto_deps = [], 54 | output_to_workspace = False, 55 | protoc = None, 56 | 57 | pb_plugin = None, 58 | pb_options = [], 59 | 60 | grpc_plugin = None, 61 | grpc_options = [], 62 | 63 | proto_compile_args = {}, 64 | with_grpc = True, 65 | srcs = [], 66 | deps = [], 67 | go_proto_deps = [], 68 | verbose = 0, 69 | **kwargs): 70 | 71 | resolved_go_proto_deps = [] + go_proto_deps 72 | if not go_proto_deps: 73 | if with_grpc: 74 | resolved_go_proto_deps = GRPC_COMPILE_DEPS 75 | else: 76 | resolved_go_proto_deps = PB_COMPILE_DEPS 77 | 78 | if importpath: 79 | go_prefix = None 80 | 81 | proto_compile_args += { 82 | "name": name + ".pb", 83 | "protos": protos, 84 | "go_prefix": go_prefix, 85 | "go_importpath": importpath, 86 | "go_package": go_package, 87 | "deps": [dep + ".pb" for dep in proto_deps], 88 | "langs": langs, 89 | "importmap": importmap, 90 | "imports": imports, 91 | "inputs": inputs, 92 | "pb_options": pb_options, 93 | "grpc_options": grpc_options, 94 | "output_to_workspace": output_to_workspace, 95 | "verbose": verbose, 96 | "with_grpc": with_grpc, 97 | } 98 | 99 | if protoc: 100 | proto_compile_args["protoc"] = protoc 101 | if pb_plugin: 102 | proto_compile_args["pb_plugin"] = pb_plugin 103 | if grpc_plugin: 104 | proto_compile_args["grpc_plugin"] = grpc_plugin 105 | 106 | proto_compile(**proto_compile_args) 107 | 108 | go_library( 109 | name = name, 110 | srcs = srcs + [name + ".pb"], 111 | deps = depset(deps + proto_deps + resolved_go_proto_deps).to_list(), 112 | importpath = importpath, 113 | **kwargs) 114 | -------------------------------------------------------------------------------- /gogo/BUILD: -------------------------------------------------------------------------------- 1 | package(default_visibility = ["//visibility:public"]) 2 | 3 | load("//protobuf:rules.bzl", "proto_language") 4 | 5 | proto_language( 6 | name = "gogo", 7 | go_prefix = "//:go_prefix", 8 | pb_file_extensions = [".pb.go"], 9 | pb_plugin = "@com_github_gogo_protobuf//protoc-gen-gogo", 10 | pb_plugin_implements_grpc = True, 11 | ) 12 | 13 | proto_language( 14 | name = "gogofast", 15 | go_prefix = "//:go_prefix", 16 | pb_file_extensions = [".pb.go"], 17 | pb_plugin = "@com_github_gogo_protobuf//protoc-gen-gogofast", 18 | pb_plugin_implements_grpc = True, 19 | ) 20 | 21 | proto_language( 22 | name = "gogofaster", 23 | go_prefix = "//:go_prefix", 24 | pb_file_extensions = [".pb.go"], 25 | pb_plugin = "@com_github_gogo_protobuf//protoc-gen-gogofaster", 26 | pb_plugin_implements_grpc = True, 27 | ) 28 | 29 | proto_language( 30 | name = "gogoslick", 31 | go_prefix = "//:go_prefix", 32 | pb_file_extensions = [".pb.go"], 33 | pb_plugin = "@com_github_gogo_protobuf//protoc-gen-gogoslick", 34 | pb_plugin_implements_grpc = True, 35 | ) 36 | -------------------------------------------------------------------------------- /gogo/README.md: -------------------------------------------------------------------------------- 1 | # Gogo Rules 2 | 3 | | Rule | Description | 4 | | ---: | :--- | 5 | | [gogo_proto_repositories](#gogo_proto_repositories) | Load WORKSPACE dependencies. | 6 | | [gogo_proto_compile](#gogo_proto_compile) | Generate protobuf source files. | 7 | | [gogofast_proto_compile](#gogo_proto_compile) | Generate protobuf source files. | 8 | | [gogofaster_proto_compile](#gogo_proto_compile) | Generate protobuf source files. | 9 | | [gogoslick_proto_compile](#gogo_proto_compile) | Generate protobuf source files. | 10 | | [gogo_proto_library](#gogo_proto_library) | Generates and compiles protobuf source files. | 11 | | [gogofast_proto_library](#gogofast_proto_library) | Generates and compiles protobuf source files. | 12 | | [gogofaster_proto_library](#gogofaster_proto_library) | Generates and compiles protobuf source files. | 13 | | [gogoslick_proto_library](#gogoslick_proto_library) | Generates and compiles protobuf source files. | 14 | 15 | ## gogo\_proto\_repositories 16 | 17 | Enable C# support by loading the dependencies in your workspace. 18 | 19 | > IMPORTANT: This should occur after loading 20 | > [rules_go](https://github.com/bazelbuild/rules_go). In particular, 21 | > your version of rules_go must be a recent one that includes 22 | > **[gazelle](https://github.com/bazelbuild/rules_go/tree/master/go/tools/gazelle)** 23 | > support and the `new_go_repository` rule. 24 | 25 | ```python 26 | load("@org_pubref_rules_protobuf//gogo:rules.bzl", "gogo_proto_repositories") 27 | gogo_proto_repositories() 28 | ``` 29 | 30 | ## Usage 31 | 32 | Usage is identical to [../go], just substitute `gogo` for `go`. 33 | [Gogo](https://github.com/gogo/protobuf) is just an alternative 34 | protobuf implementation. 35 | 36 | For more information on the differences between the options, please see the 37 | discussion of the options [starting here](https://github.com/gogo/protobuf#speed). 38 | -------------------------------------------------------------------------------- /gogo/deps.bzl: -------------------------------------------------------------------------------- 1 | DEPS = { 2 | 3 | "com_github_gogo_protobuf": { 4 | "rule": "go_repository", 5 | "importpath": "github.com/gogo/protobuf", 6 | "commit": "616a82ed12d78d24d4839363e8f3c5d3f20627cf", # Nov 9, 2017 7 | "build_file_proto_mode": "disable", 8 | }, 9 | 10 | } 11 | -------------------------------------------------------------------------------- /grpc_gateway/BUILD: -------------------------------------------------------------------------------- 1 | package(default_visibility = ["//visibility:public"]) 2 | 3 | load("//protobuf:rules.bzl", "proto_language") 4 | 5 | proto_language( 6 | name = "grpc_gateway", 7 | go_prefix = "//:go_prefix", 8 | grpc_file_extensions = [".pb.gw.go"], 9 | grpc_imports = [ 10 | "external/com_google_protobuf/src/", 11 | "external/com_github_grpc_ecosystem_grpc_gateway_googleapis/", 12 | ], 13 | grpc_inputs = [ 14 | "@com_google_protobuf//:well_known_protos", 15 | "@com_github_grpc_ecosystem_grpc_gateway_googleapis//:annotations_protos", 16 | "@org_golang_google_genproto//googleapis/api/annotations:go_default_library", 17 | ], 18 | grpc_plugin = "@com_github_grpc_ecosystem_grpc_gateway//protoc-gen-grpc-gateway", 19 | grpc_plugin_name = "grpc-gateway", 20 | importmap = { 21 | "google/api/annotations.proto": "google.golang.org/genproto/googleapis/api/annotations", 22 | }, 23 | supports_grpc = True, 24 | supports_pb = False, 25 | ) 26 | 27 | proto_language( 28 | name = "pb_gateway", 29 | go_prefix = "//:go_prefix", 30 | importmap = { 31 | "google/api/annotations.proto": "google.golang.org/genproto/googleapis/api/annotations", 32 | }, 33 | pb_file_extensions = [".pb.go"], 34 | pb_imports = [ 35 | "external/com_google_protobuf/src/", 36 | "external/com_github_grpc_ecosystem_grpc_gateway_googleapis/", 37 | ], 38 | pb_inputs = [ 39 | "@com_google_protobuf//:well_known_protos", 40 | "@com_github_grpc_ecosystem_grpc_gateway_googleapis//:annotations_protos", 41 | "@org_golang_google_genproto//googleapis/api/annotations:go_default_library", 42 | ], 43 | pb_plugin = "@com_github_golang_protobuf//protoc-gen-go", 44 | pb_plugin_implements_grpc = True, 45 | pb_plugin_name = "go", 46 | ) 47 | 48 | proto_language( 49 | name = "swagger", 50 | go_prefix = "//:go_prefix", 51 | importmap = { 52 | "google/api/annotations.proto": "google.golang.org/genproto/googleapis/api/annotations", 53 | }, 54 | pb_file_extensions = [".swagger.json"], 55 | pb_imports = [ 56 | "external/com_google_protobuf/src/", 57 | "external/com_github_grpc_ecosystem_grpc_gateway_googleapis/", 58 | ], 59 | pb_inputs = [ 60 | "@com_google_protobuf//:well_known_protos", 61 | "@com_github_grpc_ecosystem_grpc_gateway_googleapis//:annotations_protos", 62 | "@org_golang_google_genproto//googleapis/api/annotations:go_default_library", 63 | ], 64 | pb_plugin = "@com_github_grpc_ecosystem_grpc_gateway//protoc-gen-swagger", 65 | supports_grpc = True, 66 | ) 67 | -------------------------------------------------------------------------------- /grpc_gateway/README.md: -------------------------------------------------------------------------------- 1 | # Grpc-Gateway Rules 2 | 3 | | Rule | Description | 4 | | ---: | :--- | 5 | | [grpc_gateway_proto_repositories](#grpc_gateway_proto_repositories) | Load WORKSPACE dependencies. | 6 | | [grpc_gateway_proto_compile](#grpc_gateway_proto_compile) | Generate protobuf source files. | 7 | | [grpc_gateway_proto_library](#grpc_gateway_proto_library) | Generate and compiles protobuf source files. | 8 | | [grpc_gateway_swagger_compile](#grpc_gateway_swagger_compile) | Generate swwagger.json source files. | 9 | 10 | ## grpc_gateway\_proto\_repositories 11 | 12 | Enable [grpc-gateway](https://github.com/grpc-ecosystem/grpc-gateway) 13 | support by loading the dependencies in your workspace. 14 | 15 | > IMPORTANT: This should occur after loading 16 | > [rules_go](https://github.com/bazelbuild/rules_go). 17 | 18 | ```python 19 | load("@org_pubref_rules_protobuf//grpc_gateway:rules.bzl", "grpc_gateway_proto_repositories") 20 | grpc_gateway_proto_repositories() 21 | ``` 22 | 23 | ## grpc_gateway\_proto\_compile 24 | 25 | This is a thin wrapper over the 26 | [proto_compile](../protobuf#proto_compile) rule having language 27 | `@org_pubref_rules_protobuf//grpc_gateway`. 28 | 29 | ```python 30 | load("@org_pubref_rules_protobuf//grpc_gateway:rules.bzl", "grpc_gateway_proto_compile") 31 | 32 | grpc_gateway_proto_compile( 33 | name = "protos", 34 | protos = ["message.proto"], 35 | ) 36 | ``` 37 | 38 | ```sh 39 | $ bazel build :protos 40 | Target //:protos up-to-date: 41 | bazel-genfiles/message.pb.gw.go 42 | ``` 43 | 44 | ## grpc_gateway\_proto\_library 45 | 46 | Pass the set of protobuf source files to the `protos` attribute. 47 | 48 | ```python 49 | load("@org_pubref_rules_protobuf//grpc_gateway:rules.bzl", "grpc_gateway_proto_library") 50 | 51 | grpc_gateway_proto_library( 52 | name = "protolib", 53 | protos = ["message.proto"], 54 | ) 55 | ``` 56 | 57 | ```sh 58 | $ bazel build :protolib 59 | Target //:protolib up-to-date: 60 | bazel-bin/protolib.a 61 | ``` 62 | 63 | To get the list of required compile-time dependencies in other 64 | contexts for grpc-related code, load the list from the rules.bzl file: 65 | 66 | ```python 67 | load("@org_pubref_rules_protobuf//grpc_gateway:rules.bzl", "GRPC_COMPILE_DEPS") 68 | 69 | go_binary( 70 | name = "gateway", 71 | srcs = ["main.go"], 72 | deps = [ 73 | ":protolib" 74 | ] + GRPC_COMPILE_DEPS, 75 | ) 76 | ``` 77 | 78 | In this case `main.go` should implement the http entrypoint for the 79 | grpc-gateway 80 | [as described](https://github.com/grpc-ecosystem/grpc-gateway#usage). 81 | 82 | This rule includes all common `_library` attributes in addition to: 83 | 84 | | Name | Type | Description | Default | 85 | | ---- | ---- | ----------- | ------- | 86 | | `log_level` | `int` | Logging threshold level. | `0` | 87 | | `log_dir` | `string` | Log directory pathname. | `""` | 88 | | `log_backtrace_at` | `int` | See grpc_gateway docs. | `0` | 89 | | `logtostderr` | `boolean` | Emit logging to stderr instead of a log file. | `True` | 90 | | `alsologtostderr` | `boolean` | Emit logging to stderr in addition to the log file. | `False` | 91 | | `stderrthreshold` | `boolean` | Emit logging to stderr rather than a file. | `True` | 92 | | `stderrthreshold` | `int` | See grpc_gateway docs. | `0` | 93 | | `log_backtrace_at` | `int` | See grpc_gateway docs. | `0` | 94 | | `request_context` | `boolean` | See grpc_gateway docs. | `False` | 95 | 96 | ## grpc_gateway_binary 97 | 98 | The shortcut rule for the above is the `grpc_gateway_binary` rule: 99 | 100 | ```python 101 | grpc_gateway_binary( 102 | name = "gateway", 103 | srcs = ["main.go"], 104 | protos = [message.proto], 105 | ) 106 | ``` 107 | 108 | ## grpc_gateway_swagger_compile 109 | 110 | The `grpc_gateway_swagger_compile` rule can be used to generate 111 | `*.swagger.json` outputs: 112 | 113 | ```python 114 | load("@org_pubref_rules_protobuf//grpc_gateway:rules.bzl", "grpc_gateway_swagger_compile") 115 | 116 | grpc_gateway_swagger_compile( 117 | name = "swagger", 118 | protos = ["message.proto"], 119 | ) 120 | ``` 121 | 122 | ```sh 123 | $ bazel build :swagger 124 | Target //:protos up-to-date: 125 | bazel-genfiles/message.swagger.json 126 | ``` 127 | 128 | Consult source files in the 129 | [examples/helloworld/grpc_gateway](../examples/helloworld/grpc_gateway) directory for 130 | additional information. 131 | 132 | 133 | [grpc-gateway-home]:https://github.com/grpc-ecosystem/grpc-gateway 134 | -------------------------------------------------------------------------------- /grpc_gateway/deps.bzl: -------------------------------------------------------------------------------- 1 | GOOGLEAPIS_BUILD_FILE = """ 2 | filegroup( 3 | name = "annotations_protos", 4 | srcs = [ 5 | "google/api/annotations.proto", 6 | "google/api/http.proto", 7 | ], 8 | visibility = ["//visibility:public"] 9 | ) 10 | """ 11 | 12 | DEPS = { 13 | 14 | "com_github_grpc_ecosystem_grpc_gateway": { 15 | "rule": "go_repository", 16 | "importpath": "github.com/grpc-ecosystem/grpc-gateway", 17 | "commit": "f2862b476edcef83412c7af8687c9cd8e4097c0f", # Jul 23, 2017 18 | "build_file_proto_mode": "legacy", 19 | }, 20 | 21 | "com_github_grpc_ecosystem_grpc_gateway_googleapis": { 22 | "rule": "new_http_archive", 23 | "url": "https://github.com/grpc-ecosystem/grpc-gateway/archive/f2862b476edcef83412c7af8687c9cd8e4097c0f.zip", 24 | "strip_prefix": "grpc-gateway-f2862b476edcef83412c7af8687c9cd8e4097c0f/third_party/googleapis", 25 | "sha256": "b8426c25492e76dec187099fb8e9f582173df3d445cb8913ca5ce78f423779a9", 26 | "build_file_content": GOOGLEAPIS_BUILD_FILE, 27 | }, 28 | 29 | "org_golang_google_genproto": { 30 | "rule": "go_repository", 31 | "importpath": "google.golang.org/genproto", 32 | "commit": "411e09b969b1170a9f0c467558eb4c4c110d9c77", # Apr 4, 2017 33 | }, 34 | 35 | 36 | } 37 | -------------------------------------------------------------------------------- /grpc_gateway/rules.bzl: -------------------------------------------------------------------------------- 1 | load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_binary") 2 | load("//protobuf:rules.bzl", "proto_compile", "proto_repositories") 3 | load("//go:rules.bzl", "PB_COMPILE_DEPS", "GRPC_COMPILE_DEPS", "go_proto_repositories") 4 | load("//grpc_gateway:deps.bzl", "DEPS") 5 | 6 | def grpc_gateway_proto_repositories( 7 | lang_deps = DEPS, 8 | lang_requires = [ 9 | "com_github_grpc_ecosystem_grpc_gateway", 10 | "com_github_grpc_ecosystem_grpc_gateway_googleapis", 11 | "org_golang_google_genproto", 12 | ], **kwargs): 13 | 14 | go_proto_repositories(lang_deps = lang_deps, 15 | lang_requires = lang_requires, 16 | **kwargs) 17 | 18 | GRPC_GATEWAY_DEPS = [ 19 | "@com_github_grpc_ecosystem_grpc_gateway//runtime:go_default_library", 20 | "@com_github_grpc_ecosystem_grpc_gateway//utilities:go_default_library", 21 | "@org_golang_google_genproto//googleapis/api/annotations:go_default_library", 22 | "@org_golang_google_grpc//codes:go_default_library", 23 | "@org_golang_google_grpc//grpclog:go_default_library", 24 | "@org_golang_google_grpc//:go_default_library", 25 | "@org_golang_google_grpc//status:go_default_library", 26 | "@org_golang_x_net//context:go_default_library", 27 | "@com_github_golang_glog//:go_default_library", 28 | ] 29 | 30 | def grpc_gateway_proto_library( 31 | name, 32 | pb_gateway = str(Label("//grpc_gateway:pb_gateway")), 33 | langs = [str(Label("//grpc_gateway"))], 34 | go_prefix = Label("//:go_prefix", relative_to_caller_repository=True), 35 | protos = [], 36 | imports = [], 37 | importmap = {}, 38 | inputs = [], 39 | proto_deps = [], 40 | output_to_workspace = False, 41 | protoc = None, 42 | pb_plugin = None, 43 | pb_options = [], 44 | grpc_plugin = None, 45 | grpc_options = [], 46 | pb_args = {}, 47 | pbgw_args = {}, 48 | srcs = [], 49 | deps = [], 50 | go_proto_deps = [], 51 | grpc_gateway_deps = GRPC_GATEWAY_DEPS, 52 | verbose = 0, 53 | 54 | logtostderr = False, 55 | alsologtostderr = False, 56 | log_backtrace_at = None, 57 | stderrthreshold = None, 58 | log_dir = None, 59 | log_level = None, 60 | import_prefix = None, 61 | request_context = False, 62 | 63 | **kwargs): 64 | 65 | _go_proto_deps = [] + go_proto_deps 66 | 67 | if not go_proto_deps: 68 | _go_proto_deps += GRPC_COMPILE_DEPS 69 | 70 | pb_args += { 71 | "name": name + ".pb", 72 | "protos": protos, 73 | "deps": [dep + ".pb" for dep in proto_deps], 74 | "go_prefix": go_prefix, 75 | "langs": [pb_gateway], 76 | "imports": imports, 77 | "importmap": importmap, 78 | "inputs": inputs, 79 | "pb_options": pb_options, 80 | "grpc_options": grpc_options, 81 | "output_to_workspace": output_to_workspace, 82 | "verbose": verbose, 83 | } 84 | 85 | if protoc: 86 | pb_args["protoc"] = protoc 87 | if pb_plugin: 88 | pb_args["pb_plugin"] = pb_plugin 89 | if grpc_plugin: 90 | pb_args["grpc_plugin"] = grpc_plugin 91 | 92 | proto_compile(**pb_args) 93 | 94 | pbgw_opts = [] 95 | if logtostderr: 96 | pbgw_opts += ["logtostderr=true"] 97 | if alsologtostderr: 98 | pbgw_opts += ["alsologtostderr=true"] 99 | if log_backtrace_at: 100 | pbgw_opts += ["log_backtrace_at=%s" % log_backtrace_at] 101 | if stderrthreshold: 102 | pbgw_opts += ["stderrthreshold=%s" % stderrthreshold] 103 | if log_dir: 104 | pbgw_opts += ["log_dir=%s" % log_dir] 105 | if log_level: 106 | pbgw_opts += ["v=%s" % log_level] 107 | if import_prefix: 108 | pbgw_opts += ["import_prefix=%s" % import_prefix] 109 | if request_context: 110 | pbgw_opts += ["request_context=true"] 111 | 112 | pbgw_args += { 113 | "name": name + ".gw", 114 | "protos": protos, 115 | "deps": [dep + ".pb" for dep in proto_deps], 116 | "go_prefix": go_prefix, 117 | "langs": langs, 118 | "imports": imports, 119 | "importmap": importmap, 120 | "inputs": inputs, 121 | "pb_options": pb_options, 122 | "grpc_options": grpc_options + pbgw_opts, 123 | "output_to_workspace": output_to_workspace, 124 | "verbose": verbose, 125 | } 126 | 127 | proto_compile(**pbgw_args) 128 | 129 | go_library( 130 | name = name, 131 | srcs = srcs + [name + ".pb"] + [name + ".gw"], 132 | deps = depset(deps + proto_deps + _go_proto_deps + grpc_gateway_deps).to_list(), 133 | **kwargs) 134 | 135 | 136 | def grpc_gateway_proto_compile(langs = [str(Label("//grpc_gateway"))], **kwargs): 137 | proto_compile(langs = langs, **kwargs) 138 | 139 | def grpc_gateway_swagger_compile(langs = [str(Label("//grpc_gateway:swagger"))], **kwargs): 140 | proto_compile(langs = langs, with_grpc = False, **kwargs) 141 | 142 | def grpc_gateway_binary(name, 143 | srcs = [], 144 | deps = [], 145 | protos = [], 146 | proto_label = "go_default_library", 147 | proto_deps = [], 148 | grpc_gateway_deps = GRPC_GATEWAY_DEPS, 149 | **kwargs): 150 | 151 | grpc_gateway_proto_library( 152 | name = proto_label, 153 | protos = protos, 154 | proto_deps = proto_deps, 155 | **kwargs 156 | ) 157 | 158 | go_binary( 159 | name = name, 160 | srcs = srcs, 161 | deps = deps + [proto_label] + grpc_gateway_deps, 162 | ) 163 | -------------------------------------------------------------------------------- /images/wtfcat.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pubref/rules_protobuf/99043441e3c473809f633f5ad049945606124b60/images/wtfcat.png -------------------------------------------------------------------------------- /java/README.md: -------------------------------------------------------------------------------- 1 | # Java Rules 2 | 3 | | Rule | Description | 4 | | ---: | :--- | 5 | | [java_proto_repositories](#java_proto_repositories) | Load WORKSPACE dependencies. | 6 | | [java_proto_compile](#java_proto_compile) | Generate protobuf source files. | 7 | | [java_proto_library](#java_proto_library) | Generate and compiles protobuf source files. | 8 | | [java_proto_language_import](#java_proto_language_import) | Collect deps from a proto_language and expose them to dependent rules via java_import. | 9 | 10 | ## java\_proto\_repositores 11 | 12 | Enable Java support by loading the dependencies in your workspace. 13 | 14 | ```python 15 | load("@org_pubref_rules_protobuf//java:rules.bzl", "java_proto_repositories") 16 | java_proto_repositories() 17 | ``` 18 | 19 | ## java\_proto\_compile 20 | 21 | This is a thin wrapper over the 22 | [proto_compile](../protobuf#proto_compile) rule having language 23 | `@org_pubref_rules_protobuf//:java`. 24 | 25 | ```python 26 | load("@org_pubref_rules_protobuf//java:rules.bzl", "java_proto_compile") 27 | 28 | java_proto_compile( 29 | name = "protos", 30 | protos = ["message.proto"], 31 | with_grpc = True, 32 | ) 33 | ``` 34 | 35 | ```sh 36 | $ bazel build :protos 37 | Target //:protos up-to-date: 38 | bazel-genfiles/protos.srcjar 39 | ``` 40 | 41 | ## java\_proto\_library 42 | 43 | Pass the set of protobuf source files to the `protos` attribute. 44 | When depending on a java_proto_library target, it will automatically export 45 | `@org_pubref_rules_protobuf//java:grpc_compiletime_deps` for you. 46 | 47 | ```python 48 | load("@org_pubref_rules_protobuf//java:rules.bzl", "java_proto_library") 49 | 50 | java_proto_library( 51 | name = "protolib", 52 | protos = ["message.proto"], 53 | with_grpc = True, 54 | ) 55 | ``` 56 | 57 | ```sh 58 | $ bazel build :protolib 59 | Target //:protolib up-to-date: 60 | bazel-bin/protolib.jar 61 | ``` 62 | 63 | ## java\_proto\_language\_import 64 | 65 | When using the compiled library in other rules, you may need the 66 | compile-time or runtime dependencies. You can access these using the 67 | `java_proto_language_import` rule. There are several pre-defined ones 68 | in the BUILD file in this directory. 69 | 70 | ```python 71 | java_library( 72 | name = "mylib", 73 | srcs = ['MyApp.java'], 74 | deps = [ 75 | ":protolib", 76 | "@org_pubref_rules_protobuf//java:grpc_compiletime_deps", 77 | ] 78 | ) 79 | ``` 80 | 81 | ```python 82 | java_binary( 83 | name = "myapp", 84 | main_class = "example.MyApp", 85 | runtime_deps = [ 86 | ":mylib", 87 | "@org_pubref_rules_protobuf//java:netty_runtime_deps", 88 | ] 89 | ) 90 | ``` 91 | 92 | ```sh 93 | # Run your app 94 | $ bazel run :myapp 95 | 96 | # Build a self-contained executable jar 97 | $ bazel build :myapp_deploy.jar 98 | ``` 99 | 100 | Consult source files in the 101 | [examples/helloworld/java](../examples/helloworld/java) directory for 102 | additional information. 103 | -------------------------------------------------------------------------------- /java/grpc-java-maven-deps/BUILD: -------------------------------------------------------------------------------- 1 | # Trivial rule exists here simply as a way to trigger WORKSPACE loading. 2 | java_library( 3 | name = "hello", 4 | srcs = ["Hello.java"], 5 | deps = [ 6 | "@maven_grpc//:compile", 7 | "@maven_protobuf//:compile", 8 | ], 9 | ) 10 | -------------------------------------------------------------------------------- /java/grpc-java-maven-deps/Hello.java: -------------------------------------------------------------------------------- 1 | class Hello { 2 | public static void main(String[] args) { 3 | System.out.println("Hi!"); 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /java/grpc-java-maven-deps/WORKSPACE: -------------------------------------------------------------------------------- 1 | # Note: This is a utility workspace (probably does not belong in 2 | # tests) to help compute the transitive deps of grpc-java. 3 | 4 | http_archive( 5 | name = "org_pubref_rules_maven", 6 | url = "https://github.com/pubref/rules_maven/archive/43d56ae584ffdf1cd79307728bfd2436c475d35e.zip", 7 | sha256 = "33053be697be119582ae294504d8857b76e6ffa6ebadffc76d644854a504af43", 8 | strip_prefix = "rules_maven-43d56ae584ffdf1cd79307728bfd2436c475d35e", 9 | ) 10 | 11 | load("@org_pubref_rules_maven//maven:rules.bzl", "maven_repositories", "maven_repository") 12 | 13 | maven_repositories() 14 | 15 | maven_repository( 16 | name = 'maven_protobuf', 17 | deps = [ 18 | 'com.google.protobuf:protobuf-java:3.4.0', 19 | ], 20 | transitive_deps = [ 21 | 'b32aba0cbe737a4ca953f71688725972e3ee927c:com.google.protobuf:protobuf-java:3.4.0', 22 | ], 23 | ) 24 | 25 | maven_repository( 26 | name = 'maven_grpc', 27 | deps = [ 28 | 'io.grpc:grpc-netty:1.6.1', 29 | 'io.grpc:grpc-protobuf:1.6.1', 30 | 'io.grpc:grpc-stub:1.6.1', 31 | ], 32 | force = [ 33 | 'com.google.protobuf:protobuf-java:3.4.0', 34 | 'com.google.errorprone:error_prone_annotations:2.0.11', 35 | 'io.grpc:grpc-context:1.6.1', 36 | ], 37 | transitive_deps = [ 38 | '3760f6a6e13c8ab070aa629876cdd183614ee877:com.google.api.grpc:proto-google-common-protos:0.1.9', 39 | '5871fb60dc68d67da54a663c3fd636a10a532948:com.google.code.findbugs:jsr305:3.0.0', 40 | '751f548c85fa49f330cecbb1875893f971b33c4e:com.google.code.gson:gson:2.7', 41 | '3624d81fca4e93c67f43bafc222b06e1b1e3b260:com.google.errorprone:error_prone_annotations:2.0.11', 42 | '6ce200f6b23222af3d8abb6b6459e6c44f4bb0e9:com.google.guava:guava:19.0', 43 | '41614af3429573dc02645d541638929d877945a2:com.google.instrumentation:instrumentation-api:0.4.3', 44 | 'b32aba0cbe737a4ca953f71688725972e3ee927c:com.google.protobuf:protobuf-java:3.4.0', 45 | '35d048270e0b2f29e7e4a45daf21d46d1b121a24:com.google.protobuf:protobuf-java-util:3.3.1', 46 | '9c52ae577c2dd4b8c6ac6e49c1154e1dc37d98ee:io.grpc:grpc-context:1.6.1', 47 | '871c934f3c7fbb477ccf2dd4c2a47a0bcc1b82a9:io.grpc:grpc-core:1.6.1', 48 | '6941e41b5f422da1670d5d01bf476644330b536e:io.grpc:grpc-netty:1.6.1', 49 | 'a309df5d2627422ceb9cb08fb6a240656d75760a:io.grpc:grpc-protobuf:1.6.1', 50 | '124bca81a50bc76b6a6babcc4bc529e5a93db70f:io.grpc:grpc-protobuf-lite:1.6.1', 51 | 'f32b7ad69749ab6c7be5dd21f1e520a315418790:io.grpc:grpc-stub:1.6.1', 52 | '71f0a707209b1356d924d6f8b2f415f8b8e1cf82:io.netty:netty-buffer:4.1.14.Final', 53 | 'b8573ae401f17e6927f158e4c446311bf0646173:io.netty:netty-codec:4.1.14.Final', 54 | 'f287b593a37e516f98c9dae7337303e7254e8ea1:io.netty:netty-codec-http:4.1.14.Final', 55 | '00d2af27befab8e1abfbf37d1ac2a5185dce1dbe:io.netty:netty-codec-http2:4.1.14.Final', 56 | 'b8d856c686ac960b9e9b9f8f9b4083978c161327:io.netty:netty-codec-socks:4.1.14.Final', 57 | '230ff063651295d2695c0b4e9411e22bbbb9c09d:io.netty:netty-common:4.1.14.Final', 58 | '626a48b846736c944eb35dd9b0fe0435b76ebf93:io.netty:netty-handler:4.1.14.Final', 59 | '9dbedd6cc6ab9299c927d0c73791d3d8fd76ac20:io.netty:netty-handler-proxy:4.1.14.Final', 60 | 'f91e0197522e7d33fce84b3dfd86ade15edb0006:io.netty:netty-resolver:4.1.14.Final', 61 | '3ed6474f1289635fc0696ec37380e20f258950a2:io.netty:netty-transport:4.1.14.Final', 62 | 'cbd0a716a7d85ac34b83d86b13f0a6655e45c2ba:io.opencensus:opencensus-api:0.5.1', 63 | ], 64 | ) 65 | 66 | load("@maven_protobuf//:rules.bzl", "maven_protobuf_compile") 67 | maven_protobuf_compile() 68 | 69 | load("@maven_grpc//:rules.bzl", "maven_grpc_compile") 70 | maven_grpc_compile() 71 | -------------------------------------------------------------------------------- /java/rules.bzl: -------------------------------------------------------------------------------- 1 | load("//java:deps.bzl", "DEPS") 2 | 3 | load("//protobuf:rules.bzl", 4 | "proto_compile", 5 | "proto_language", 6 | "proto_language_deps", 7 | "proto_repositories") 8 | 9 | def java_proto_repositories( 10 | lang_deps = DEPS, 11 | lang_requires = [ 12 | #"com_google_code_gson_gson", 13 | #"com_google_guava_guava", 14 | 15 | "protoc_gen_grpc_java_linux_x86_64", 16 | "protoc_gen_grpc_java_macosx", 17 | "protoc_gen_grpc_java_windows_x86_64", 18 | 19 | 'com_google_api_grpc_proto_google_common_protos', 20 | 'com_google_code_findbugs_jsr305', 21 | 'com_google_code_gson_gson', 22 | 'com_google_errorprone_error_prone_annotations', 23 | 'com_google_guava_guava', 24 | 'com_google_instrumentation_instrumentation_api', 25 | 'com_google_protobuf_nano_protobuf_javanano', 26 | 'com_google_protobuf_protobuf_java', 27 | 'com_google_protobuf_protobuf_java_util', 28 | 'com_squareup_okhttp_okhttp', 29 | 'com_squareup_okio_okio', 30 | 'io_grpc_grpc_all', 31 | 'io_grpc_grpc_auth', 32 | 'io_grpc_grpc_context', 33 | 'io_grpc_grpc_core', 34 | 'io_grpc_grpc_netty', 35 | 'io_grpc_grpc_okhttp', 36 | 'io_grpc_grpc_protobuf', 37 | 'io_grpc_grpc_protobuf_lite', 38 | 'io_grpc_grpc_protobuf_nano', 39 | 'io_grpc_grpc_stub', 40 | 'io_netty_netty_buffer', 41 | 'io_netty_netty_codec', 42 | 'io_netty_netty_codec_http', 43 | 'io_netty_netty_codec_http2', 44 | 'io_netty_netty_codec_socks', 45 | 'io_netty_netty_common', 46 | 'io_netty_netty_handler', 47 | 'io_netty_netty_handler_proxy', 48 | 'io_netty_netty_resolver', 49 | 'io_netty_netty_transport', 50 | 'io_opencensus_opencensus_api', 51 | 'io_opencensus_opencensus_contrib_grpc_metrics', 52 | 53 | "junit_junit_4", # TODO: separate test requirements 54 | 55 | ], **kwargs): 56 | 57 | proto_repositories(lang_deps = lang_deps, 58 | lang_requires = lang_requires, 59 | **kwargs) 60 | 61 | 62 | def nano_proto_repositories( 63 | lang_requires = [ 64 | "com_google_protobuf_nano_protobuf_javanano", 65 | "io_grpc_grpc_protobuf_nano", 66 | ], **kwargs): 67 | proto_repositories(lang_requires = lang_requires, 68 | lang_deps = DEPS, 69 | **kwargs) 70 | 71 | 72 | def java_proto_compile(langs = [str(Label("//java"))], **kwargs): 73 | proto_compile(langs = langs, **kwargs) 74 | 75 | def java_proto_library( 76 | name, 77 | langs = [str(Label("//java"))], 78 | protos = [], 79 | imports = [], 80 | inputs = [], 81 | output_to_workspace = False, 82 | proto_deps = [], 83 | protoc = None, 84 | 85 | pb_plugin = None, 86 | pb_options = [], 87 | 88 | grpc_plugin = None, 89 | grpc_options = [], 90 | 91 | proto_compile_args = {}, 92 | with_grpc = True, 93 | srcs = [], 94 | deps = [], 95 | verbose = 0, 96 | **kwargs): 97 | 98 | proto_compile_args += { 99 | "name": name + ".pb", 100 | "protos": protos, 101 | "deps": [dep + ".pb" for dep in proto_deps], 102 | "langs": langs, 103 | "imports": imports, 104 | "inputs": inputs, 105 | "pb_options": pb_options, 106 | "grpc_options": grpc_options, 107 | "output_to_workspace": output_to_workspace, 108 | "verbose": verbose, 109 | "with_grpc": with_grpc, 110 | } 111 | 112 | if protoc: 113 | proto_compile_args["protoc"] = protoc 114 | if pb_plugin: 115 | proto_compile_args["pb_plugin"] = pb_plugin 116 | if grpc_plugin: 117 | proto_compile_args["grpc_plugin"] = grpc_plugin 118 | 119 | proto_compile(**proto_compile_args) 120 | 121 | proto_language_deps( 122 | name = name + "_compile_deps", 123 | langs = langs, 124 | file_extensions = [".jar"], 125 | with_grpc = with_grpc, 126 | ) 127 | 128 | native.java_import( 129 | name = name + "_compile_imports", 130 | jars = [name + "_compile_deps"], 131 | ) 132 | 133 | java_exports = [] 134 | if with_grpc: 135 | java_exports.append(str(Label("//java:grpc_compiletime_deps"))) 136 | 137 | native.java_library( 138 | name = name, 139 | srcs = srcs + [name + ".pb"], 140 | exports = java_exports, 141 | deps = depset(deps + proto_deps + [name + "_compile_imports"]).to_list(), 142 | **kwargs) 143 | 144 | 145 | def java_proto_language_import(name, 146 | langs = [str(Label("//java"))], 147 | with_grpc = True, 148 | **kwargs): 149 | proto_language_deps( 150 | name = name + ".deps", 151 | langs = langs, 152 | file_extensions = [".jar"], 153 | with_grpc = with_grpc, 154 | **kwargs 155 | ) 156 | 157 | native.java_import( 158 | name = name, 159 | jars = [name + ".deps"], 160 | ) 161 | -------------------------------------------------------------------------------- /node/BUILD: -------------------------------------------------------------------------------- 1 | package(default_visibility = ["//visibility:public"]) 2 | 3 | load("//protobuf:rules.bzl", "proto_language") 4 | 5 | proto_language( 6 | name = "node", 7 | grpc_file_extensions = ["_grpc_pb.js"], 8 | grpc_plugin = "@com_google_grpc//:grpc_node_plugin", 9 | grpc_plugin_name = "grpc_node", 10 | pb_file_extensions = ["_pb.js"], 11 | pb_options = [ 12 | "import_style=commonjs", 13 | "binary", 14 | ], 15 | pb_plugin_name = "js", 16 | supports_grpc = True, 17 | ) 18 | -------------------------------------------------------------------------------- /node/README.md: -------------------------------------------------------------------------------- 1 | # NodeJs Rules 2 | 3 | | Rule | Description | 4 | | ---: | :--- | 5 | | [node_proto_repositories](#node_proto_repositories) | Load workspace dependencies. | 6 | | [node_proto_compile](#node_proto_compile) | Generate node js protobuf source files. | 7 | | [node_proto_library](#node_proto_library) | Generate a node module packaged with protobuf sources. | 8 | 9 | ## node\_proto\_repositories 10 | 11 | Enable node support by loading the dependencies in your workspace. 12 | 13 | > This should occur after loading 14 | > [rules_node](https://github.com/pubref/rules_node). See the 15 | > `WORKSPACE` file for the appropriate commit to use from that repo. 16 | 17 | ```python 18 | load("@org_pubref_rules_protobuf//node:rules.bzl", "node_proto_repositories") 19 | node_proto_repositories() 20 | ``` 21 | 22 | ## node\_proto\_compile 23 | 24 | This is a thin wrapper over the 25 | [proto_compile](../protobuf#proto_compile) rule having language 26 | `@org_pubref_rules_protobuf//node` (common js output of the `--js_out` 27 | protoc option. 28 | 29 | ```python 30 | load("@org_pubref_rules_protobuf//node:rules.bzl", "node_proto_compile") 31 | 32 | node_proto_compile( 33 | name = "protos", 34 | protos = ["message.proto"], 35 | with_grpc = True, 36 | ) 37 | ``` 38 | 39 | ```sh 40 | $ bazel build :protos 41 | Target //:protos up-to-date: 42 | bazel-genfiles/message_pb.js 43 | bazel-genfiles/message_grpc_pb.js 44 | ``` 45 | 46 | 47 | ## node\_proto\_library 48 | 49 | Load the requisite external node modules via the `yarn_modules` repository rule in your `WORKSPACE`: 50 | 51 | ```python 52 | # WORKSPACE 53 | load("@org_pubref_rules_node//node:rules.bzl", "yarn_modules") 54 | 55 | yarn_modules( 56 | name = "yarn_modules", 57 | deps = { 58 | "google-protobuf": "3.4.0", 59 | "grpc": "1.6.0" 60 | }, 61 | ) 62 | ``` 63 | 64 | Then, in your `BUILD` file: 65 | 66 | ```python 67 | load("@org_pubref_rules_node//node:rules.bzl", "node_binary") 68 | load("//node:rules.bzl", "node_proto_library") 69 | 70 | node_proto_library( 71 | name = "api", 72 | protos = ["api.proto"], 73 | verbose = 0, 74 | with_grpc = True, 75 | ) 76 | 77 | node_binary( 78 | name = "server", 79 | main = "server.js", 80 | deps = [ 81 | ":api", 82 | "@yarn_modules//:_all_", 83 | ], 84 | ) 85 | ``` 86 | -------------------------------------------------------------------------------- /node/deps.bzl: -------------------------------------------------------------------------------- 1 | DEPS = { 2 | 3 | "protoc_gen_grpc_node": { 4 | "rule": "bind", 5 | "actual": "@com_google_grpc//:grpc_node_plugin", 6 | }, 7 | 8 | "npm_protobuf_stack": { 9 | "rule": "npm_repository", 10 | "deps": { 11 | "async": "2.6.0", 12 | "google-protobuf": "3.5.0", 13 | "lodash": "4.17.5", 14 | "minimist": "1.2.0", 15 | }, 16 | "sha256": "96242be14d18d9f1e81603502893545c7ca0fbcabcb9e656656cbcdda2b52bbb", 17 | }, 18 | 19 | "npm_grpc": { 20 | "rule": "npm_repository", 21 | "deps": { 22 | "grpc": "1.0.0", 23 | }, 24 | # Disabiling sha for this one as it will likely fail since the sha256 is 25 | # calculated after node-gyp runs and therefore platform dependent. 26 | #sha256 = "dcbc0f43a7eb5c52529ba35a4d455d61fcef49d2ced3229e821d4b07c5aaef9d", 27 | }, 28 | 29 | } 30 | -------------------------------------------------------------------------------- /node/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "rules_protobuf", 3 | "version": "1.0.0", 4 | "dependencies": { 5 | "google-protobuf": "3.4.0", 6 | "grpc": "1.6.0" 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /node/rules.bzl: -------------------------------------------------------------------------------- 1 | load("@org_pubref_rules_node//node:rules.bzl", "node_module") 2 | load("//cpp:rules.bzl", "cpp_proto_repositories") 3 | load("//node:deps.bzl", "DEPS") 4 | 5 | load("//protobuf:rules.bzl", 6 | "proto_compile", 7 | "proto_language_deps", 8 | "proto_repositories") 9 | 10 | def node_proto_repositories( 11 | omit_cpp_repositories = False, 12 | lang_deps = DEPS, 13 | lang_requires = [ 14 | # "npm_protobuf_stack", 15 | # "npm_grpc", 16 | ], 17 | **kwargs): 18 | 19 | if not omit_cpp_repositories: 20 | cpp_proto_repositories(**kwargs) 21 | 22 | rem = proto_repositories(lang_deps = lang_deps, 23 | lang_requires = lang_requires, 24 | **kwargs) 25 | 26 | # Load remaining (special) deps 27 | for dep in rem: 28 | rule = dep.pop("rule") 29 | if "npm_repository" == rule: 30 | fail("Unknown loading rule %s for %s" % (rule, dep)) 31 | #npm_repository(**dep) 32 | else: 33 | fail("Unknown loading rule %s for %s" % (rule, dep)) 34 | 35 | 36 | def _get_js_variable_name(file): 37 | name = file.basename.rstrip(".js") 38 | # Deal with special characters here? 39 | return name 40 | 41 | 42 | def _node_proto_module_impl(ctx): 43 | compilation = ctx.attr.compilation.proto_compile_result 44 | index_js = ctx.new_file("%s/index.js" % ctx.label.name) 45 | 46 | exports = {} 47 | 48 | for unit in compilation.transitive_units: 49 | for file in unit.outputs: 50 | if file.path.endswith("_pb.js"): 51 | name = _get_js_variable_name(file) 52 | exports[name] = file.short_path 53 | elif file.path.endswith("_grpc_pb.js"): 54 | name = _get_js_variable_name(file) 55 | exports[name] = file.short_path 56 | 57 | content = [] 58 | content.append("module.exports = {") 59 | for name, path in exports.items(): 60 | content.append(" '%s': require('./%s')," % (name, path)) 61 | content.append("}") 62 | 63 | ctx.file_action( 64 | output = index_js, 65 | content = "\n".join(content) 66 | ) 67 | 68 | return struct( 69 | files = depset([index_js]), 70 | ) 71 | 72 | 73 | _node_proto_module = rule( 74 | implementation = _node_proto_module_impl, 75 | attrs = { 76 | "compilation": attr.label( 77 | providers = ["proto_compile_result"], 78 | mandatory = True, 79 | ) 80 | } 81 | ) 82 | 83 | 84 | def node_proto_compile(langs = [str(Label("//node"))], **kwargs): 85 | proto_compile(langs = langs, **kwargs) 86 | 87 | 88 | def node_proto_library( 89 | name, 90 | langs = [str(Label("//node"))], 91 | protos = [], 92 | imports = [], 93 | inputs = [], 94 | output_to_workspace = False, 95 | proto_deps = [ 96 | ], 97 | protoc = None, 98 | pb_plugin = None, 99 | pb_options = [], 100 | proto_compile_args = {}, 101 | srcs = [], 102 | deps = [ 103 | "@yarn_modules//:google-protobuf", 104 | ], 105 | data = [], 106 | verbose = 0, 107 | with_grpc = False, 108 | 109 | **kwargs): 110 | 111 | proto_compile_args += { 112 | "name": name + ".pb", 113 | "protos": protos, 114 | "deps": [dep + ".pb" for dep in proto_deps], 115 | "langs": langs, 116 | "imports": imports, 117 | "inputs": inputs, 118 | "pb_options": pb_options, 119 | "output_to_workspace": output_to_workspace, 120 | "verbose": verbose, 121 | "with_grpc": with_grpc, 122 | } 123 | 124 | if protoc: 125 | proto_compile_args["protoc"] = protoc 126 | if pb_plugin: 127 | proto_compile_args["pb_plugin"] = pb_plugin 128 | 129 | proto_compile(**proto_compile_args) 130 | 131 | _node_proto_module( 132 | name = name + "_index", 133 | compilation = name + ".pb", 134 | ) 135 | 136 | node_module( 137 | name = name, 138 | index = name + "_index", 139 | layout = "workspace", 140 | srcs = srcs + [name + ".pb"], 141 | data = data + [dep + ".pb" for dep in proto_deps], 142 | deps = depset(deps + proto_deps).to_list(), 143 | **kwargs) 144 | -------------------------------------------------------------------------------- /objc/BUILD: -------------------------------------------------------------------------------- 1 | package(default_visibility = ["//visibility:public"]) 2 | 3 | load("//protobuf:rules.bzl", "proto_language") 4 | 5 | proto_language( 6 | name = "objc", 7 | grpc_file_extensions = [ 8 | ".pbrpc.h", 9 | ".pbrpc.m", 10 | ], 11 | grpc_plugin = "//external:protoc_gen_grpc_objc", 12 | grpc_plugin_name = "grpc_objc", 13 | output_file_style = "pascal", 14 | pb_file_extensions = [ 15 | ".pbobjc.h", 16 | ".pbobjc.m", 17 | ], 18 | supports_grpc = True, 19 | ) 20 | -------------------------------------------------------------------------------- /objc/README.md: -------------------------------------------------------------------------------- 1 | # Objective-C Rules 2 | 3 | *Objective-C rules have not been adequately tested. They may or may 4 | not work for you.* 5 | 6 | | Rule | Description | 7 | | ---: | :--- | 8 | | [objc_proto_repositories](#objc_proto_repositories) | Load WORKSPACE dependencies. | 9 | | [objc_proto_compile](#objc_proto_compile) | Generate protobuf source files. | 10 | | [objc_proto_library](#objc_proto_library) | Generate and compiles protobuf source files. | 11 | 12 | ## objc\_proto\_repositories 13 | 14 | Enable support by loading the dependencies in your workspace. 15 | 16 | ```python 17 | load("@org_pubref_rules_protobuf//objc:rules.bzl", "objc_proto_repositories") 18 | objc_proto_repositories() 19 | ``` 20 | 21 | ## objc\_proto\_compile 22 | 23 | This is a thin wrapper over the 24 | [proto_compile](../protobuf#proto_compile) rule having language 25 | `@org_pubref_rules_protobuf//objc`. 26 | 27 | ```python 28 | load("@org_pubref_rules_protobuf//objc:rules.bzl", "objc_proto_compile") 29 | 30 | objc_proto_compile( 31 | name = "protos", 32 | protos = ["message.proto"], 33 | with_grpc = True, 34 | ) 35 | ``` 36 | 37 | ```sh 38 | $ bazel build :protos 39 | Target //:protos up-to-date: 40 | bazel-genfiles/message.pbobjc.h 41 | bazel-genfiles/message.pbobjc.m 42 | ``` 43 | 44 | ## objc\_proto\_library 45 | 46 | Pass the set of protobuf source files to the `protos` attribute. 47 | 48 | ```python 49 | load("@org_pubref_rules_protobuf//objc:rules.bzl", "objc_proto_library") 50 | 51 | objc_proto_library( 52 | name = "protolib", 53 | protos = ["message.proto"], 54 | with_grpc = False, # not yet implemented 55 | ) 56 | ``` 57 | 58 | ```sh 59 | $ bazel build :protolib 60 | Target //:protolib up-to-date: 61 | bazel-bin/protolib.m 62 | ``` 63 | -------------------------------------------------------------------------------- /objc/deps.bzl: -------------------------------------------------------------------------------- 1 | DEPS = { 2 | 3 | "protoc_gen_grpc_objc": { 4 | "rule": "bind", 5 | "actual": "@com_google_grpc//:grpc_objective_c_plugin", 6 | }, 7 | 8 | } 9 | -------------------------------------------------------------------------------- /objc/rules.bzl: -------------------------------------------------------------------------------- 1 | load("//protobuf:rules.bzl", "proto_compile", "proto_repositories") 2 | load("//objc:deps.bzl", "DEPS") 3 | load("//cpp:rules.bzl", "cpp_proto_repositories") 4 | 5 | def objc_proto_repositories( 6 | omit_cpp_repositories = False, 7 | lang_deps = DEPS, 8 | lang_requires = [ 9 | "protoc_gen_grpc_objc", 10 | ], **kwargs): 11 | 12 | if not omit_cpp_repositories: 13 | cpp_proto_repositories(**kwargs) 14 | 15 | proto_repositories(lang_deps = lang_deps, 16 | lang_requires = lang_requires, 17 | **kwargs) 18 | 19 | PB_COMPILE_DEPS = [ 20 | "@com_google_protobuf//:protobuf_objc", 21 | ] 22 | 23 | GRPC_COMPILE_DEPS = PB_COMPILE_DEPS + [ 24 | "@com_google_grpc//:grpc_objc", 25 | ] 26 | 27 | def objc_proto_compile(langs = [str(Label("//objc"))], **kwargs): 28 | proto_compile(langs = langs, **kwargs) 29 | 30 | 31 | def objc_proto_library( 32 | name, 33 | langs = [str(Label("//objc"))], 34 | protos = [], 35 | imports = [], 36 | inputs = [], 37 | proto_deps = [], 38 | output_to_workspace = False, 39 | protoc = None, 40 | 41 | pb_plugin = None, 42 | pb_options = [], 43 | 44 | grpc_plugin = None, 45 | grpc_options = [], 46 | 47 | proto_compile_args = {}, 48 | with_grpc = True, 49 | srcs = [], 50 | deps = [], 51 | verbose = 0, 52 | **kwargs): 53 | 54 | if with_grpc: 55 | compile_deps = GRPC_COMPILE_DEPS 56 | else: 57 | compile_deps = PB_COMPILE_DEPS 58 | 59 | proto_compile_args += { 60 | "name": name + ".pb", 61 | "protos": protos, 62 | "deps": [dep + ".pb" for dep in proto_deps], 63 | "langs": langs, 64 | "imports": imports, 65 | "inputs": inputs, 66 | "pb_options": pb_options, 67 | "grpc_options": grpc_options, 68 | "output_to_workspace": output_to_workspace, 69 | "verbose": verbose, 70 | "with_grpc": with_grpc, 71 | } 72 | 73 | if protoc: 74 | proto_compile_args["protoc"] = protoc 75 | if pb_plugin: 76 | proto_compile_args["pb_plugin"] = pb_plugin 77 | if grpc_plugin: 78 | proto_compile_args["grpc_plugin"] = grpc_plugin 79 | 80 | proto_compile(**proto_compile_args) 81 | 82 | native.objc_library( 83 | name = name, 84 | srcs = srcs + [name + ".pb"], 85 | deps = depset(deps + proto_deps + compile_deps).to_list(), 86 | **kwargs) 87 | -------------------------------------------------------------------------------- /protobuf/BUILD: -------------------------------------------------------------------------------- 1 | package(default_visibility = ["//visibility:public"]) 2 | -------------------------------------------------------------------------------- /protobuf/build_file/cares.BUILD: -------------------------------------------------------------------------------- 1 | # This is an exact copy of grpc/grpc/third_party/cares.BUILD 2 | # s/com_google_grpc/com_google_grpc/g. 3 | # Why? Here the github_grpc_grpc repo is built last 4 | # and would becomes a circular dependency. 5 | 6 | config_setting( 7 | name = "darwin", 8 | values = {"cpu": "darwin"}, 9 | ) 10 | 11 | # Android is not officially supported through C++. 12 | # This just helps with the build for now. 13 | config_setting( 14 | name = "android", 15 | values = { 16 | "crosstool_top": "//external:android/crosstool", 17 | }, 18 | ) 19 | 20 | # iOS is not officially supported through C++. 21 | # This just helps with the build for now. 22 | config_setting( 23 | name = "ios_x86_64", 24 | values = {"cpu": "ios_x86_64"}, 25 | ) 26 | 27 | config_setting( 28 | name = "ios_armv7", 29 | values = {"cpu": "ios_armv7"}, 30 | ) 31 | 32 | config_setting( 33 | name = "ios_armv7s", 34 | values = {"cpu": "ios_armv7s"}, 35 | ) 36 | 37 | config_setting( 38 | name = "ios_arm64", 39 | values = {"cpu": "ios_arm64"}, 40 | ) 41 | 42 | genrule( 43 | name = "ares_build_h", 44 | srcs = ["@com_google_grpc//third_party/cares:ares_build.h"], 45 | outs = ["ares_build.h"], 46 | cmd = "cat $< > $@", 47 | ) 48 | 49 | genrule( 50 | name = "ares_config_h", 51 | srcs = select({ 52 | ":ios_x86_64": ["@com_google_grpc//third_party/cares:config_darwin/ares_config.h"], 53 | ":ios_armv7": ["@com_google_grpc//third_party/cares:config_darwin/ares_config.h"], 54 | ":ios_armv7s": ["@com_google_grpc//third_party/cares:config_darwin/ares_config.h"], 55 | ":ios_arm64": ["@com_google_grpc//third_party/cares:config_darwin/ares_config.h"], 56 | ":darwin": ["@com_google_grpc//third_party/cares:config_darwin/ares_config.h"], 57 | ":android": ["@com_google_grpc//third_party/cares:config_android/ares_config.h"], 58 | "//conditions:default": ["@com_google_grpc//third_party/cares:config_linux/ares_config.h"], 59 | }), 60 | outs = ["ares_config.h"], 61 | cmd = "cat $< > $@", 62 | ) 63 | 64 | cc_library( 65 | name = "ares", 66 | srcs = [ 67 | "ares__close_sockets.c", 68 | "ares__get_hostent.c", 69 | "ares__read_line.c", 70 | "ares__timeval.c", 71 | "ares_cancel.c", 72 | "ares_create_query.c", 73 | "ares_data.c", 74 | "ares_destroy.c", 75 | "ares_expand_name.c", 76 | "ares_expand_string.c", 77 | "ares_fds.c", 78 | "ares_free_hostent.c", 79 | "ares_free_string.c", 80 | "ares_getenv.c", 81 | "ares_gethostbyaddr.c", 82 | "ares_gethostbyname.c", 83 | "ares_getnameinfo.c", 84 | "ares_getopt.c", 85 | "ares_getsock.c", 86 | "ares_init.c", 87 | "ares_library_init.c", 88 | "ares_llist.c", 89 | "ares_mkquery.c", 90 | "ares_nowarn.c", 91 | "ares_options.c", 92 | "ares_parse_a_reply.c", 93 | "ares_parse_aaaa_reply.c", 94 | "ares_parse_mx_reply.c", 95 | "ares_parse_naptr_reply.c", 96 | "ares_parse_ns_reply.c", 97 | "ares_parse_ptr_reply.c", 98 | "ares_parse_soa_reply.c", 99 | "ares_parse_srv_reply.c", 100 | "ares_parse_txt_reply.c", 101 | "ares_platform.c", 102 | "ares_process.c", 103 | "ares_query.c", 104 | "ares_search.c", 105 | "ares_send.c", 106 | "ares_strcasecmp.c", 107 | "ares_strdup.c", 108 | "ares_strerror.c", 109 | "ares_timeout.c", 110 | "ares_version.c", 111 | "ares_writev.c", 112 | "bitncmp.c", 113 | "inet_net_pton.c", 114 | "inet_ntop.c", 115 | "windows_port.c", 116 | ], 117 | hdrs = [ 118 | "ares.h", 119 | "ares_build.h", 120 | "ares_config.h", 121 | "ares_data.h", 122 | "ares_dns.h", 123 | "ares_getenv.h", 124 | "ares_getopt.h", 125 | "ares_inet_net_pton.h", 126 | "ares_iphlpapi.h", 127 | "ares_ipv6.h", 128 | "ares_library_init.h", 129 | "ares_llist.h", 130 | "ares_nowarn.h", 131 | "ares_platform.h", 132 | "ares_private.h", 133 | "ares_rules.h", 134 | "ares_setup.h", 135 | "ares_strcasecmp.h", 136 | "ares_strdup.h", 137 | "ares_version.h", 138 | "bitncmp.h", 139 | "config-win32.h", 140 | "nameser.h", 141 | "setup_once.h", 142 | ], 143 | copts = [ 144 | "-D_GNU_SOURCE", 145 | "-D_HAS_EXCEPTIONS=0", 146 | "-DNOMINMAX", 147 | "-DHAVE_CONFIG_H", 148 | ], 149 | includes = ["."], 150 | linkstatic = 1, 151 | visibility = [ 152 | "//visibility:public", 153 | ], 154 | ) 155 | -------------------------------------------------------------------------------- /protobuf/build_file/com_github_madler_zlib.BUILD: -------------------------------------------------------------------------------- 1 | # **************************************************************** 2 | # BUILD file for https://github.com/madler/zlib 3 | # **************************************************************** 4 | # 5 | package(default_visibility = ["//visibility:public"]) 6 | 7 | licenses(["notice"]) # BSD/MIT-like license (for zlib) 8 | 9 | cc_library( 10 | name = "zlib", 11 | srcs = [ 12 | "adler32.c", 13 | "compress.c", 14 | "crc32.c", 15 | "deflate.c", 16 | "gzclose.c", 17 | "gzlib.c", 18 | "gzread.c", 19 | "gzwrite.c", 20 | "infback.c", 21 | "inffast.c", 22 | "inflate.c", 23 | "inftrees.c", 24 | "trees.c", 25 | "uncompr.c", 26 | "zutil.c", 27 | ], 28 | hdrs = [ 29 | "crc32.h", 30 | "deflate.h", 31 | "gzguts.h", 32 | "inffast.h", 33 | "inffixed.h", 34 | "inflate.h", 35 | "inftrees.h", 36 | "trees.h", 37 | "zconf.h", 38 | "zlib.h", 39 | "zutil.h", 40 | ], 41 | includes = [ 42 | ".", 43 | ], 44 | copts = [ 45 | "-Wno-unused-variable", 46 | "-Wno-implicit-function-declaration", 47 | ], 48 | ) 49 | -------------------------------------------------------------------------------- /protobuf/deps.bzl: -------------------------------------------------------------------------------- 1 | 2 | DEPS = { 3 | 4 | "com_google_protobuf": { 5 | "rule": "http_archive", 6 | "url": "https://github.com/google/protobuf/archive/v3.5.1.zip", 7 | "strip_prefix": "protobuf-3.5.1", 8 | "sha256": "1f8b9b202e9a4e467ff0b0f25facb1642727cdf5e69092038f15b37c75b99e45", 9 | }, 10 | 11 | 12 | # This binds the cc_binary "protoc" into 13 | # //external:protoc. Required by grpc++. 14 | "protoc": { 15 | "rule": "bind", 16 | "actual": "@com_google_protobuf//:protoc", 17 | }, 18 | 19 | # Bind the protobuf proto_lib into //external. Required for 20 | # compiling the protoc_gen_grpc plugin 21 | "protocol_compiler": { 22 | "rule": "bind", 23 | "actual": "@com_google_protobuf//:protoc", 24 | }, 25 | 26 | # grpc++ expects "//external:protobuf" 27 | "protobuf": { 28 | "rule": "bind", 29 | "actual": "@com_google_protobuf//:protobuf", 30 | }, 31 | 32 | # grpc++ expects "//external:protobuf_clib" 33 | "protobuf_clib": { 34 | "rule": "bind", 35 | "actual": "@com_google_protobuf//:protoc_lib", 36 | }, 37 | 38 | "protobuf_headers": { 39 | "rule": "bind", 40 | "actual": "@com_google_protobuf//:protobuf_headers", 41 | }, 42 | 43 | } 44 | -------------------------------------------------------------------------------- /protobuf/internal/github_archive.bzl: -------------------------------------------------------------------------------- 1 | def github_archive(name, org, repo, commit, **kwargs): 2 | url = "https://github.com/{org}/{repo}/archive/{commit}.zip".format( 3 | org = org, 4 | repo = repo, 5 | commit = commit, 6 | ) 7 | strip_prefix = "{repo}-{commit}".format( 8 | repo = repo, 9 | commit = commit, 10 | ) 11 | if "build_file" in kwargs or "build_file_content" in kwargs: 12 | native.new_http_archive(name = name, 13 | url = url, 14 | strip_prefix = strip_prefix, 15 | **kwargs) 16 | else: 17 | native.http_archive(name = name, 18 | url = url, 19 | strip_prefix = strip_prefix, 20 | **kwargs) 21 | -------------------------------------------------------------------------------- /protobuf/internal/proto_http_archive.bzl: -------------------------------------------------------------------------------- 1 | load("@bazel_tools//tools/build_defs/repo:utils.bzl", "patch") 2 | 3 | def _proto_http_archive_impl(ctx): 4 | ctx.download_and_extract(ctx.attr.url, ctx.attr.output_dir, ctx.attr.sha256, ctx.attr.type, ctx.attr.strip_prefix) 5 | patch(ctx) 6 | if ctx.attr.build_file_content: 7 | ctx.file("BUILD.bazel", ctx.attr.build_file_content) 8 | 9 | # Alternative http_archive implementation that allows one to 10 | # relocate the content of the repo into a subdirectory via the 11 | # 'output_dir' attribute. This is sort of the opposite of 12 | # 'strip_prefix'. 13 | 14 | # For example, coreos/etcd imports their protos as 15 | # 'etcd/etcdserver/.../foo.proto'. However, there is no 'etcd' 16 | # subdir of the github repo, it's the assumed name of the repository 17 | # itself. We can overcome this by downloading it into a subdir as 18 | # 'output_dir = "etcd"'. 19 | 20 | proto_http_archive = repository_rule( 21 | implementation = _proto_http_archive_impl, 22 | attrs = { 23 | "url": attr.string( 24 | mandatory = True, 25 | ), 26 | "type": attr.string(), 27 | "strip_prefix": attr.string(), 28 | "output_dir": attr.string(), 29 | "sha256": attr.string(), 30 | "build_file_content": attr.string(), 31 | "patch_cmds": attr.string_list(), 32 | } 33 | ) 34 | -------------------------------------------------------------------------------- /protobuf/internal/proto_language.bzl: -------------------------------------------------------------------------------- 1 | def _proto_language_impl(ctx): 2 | go_prefix = None 3 | if hasattr(ctx.attr.go_prefix, "go_prefix"): 4 | go_prefix = ctx.attr.go_prefix.go_prefix 5 | #print("pb_compile_deps %s" % ctx.attr.pb_compile_deps) 6 | #print("pb_compile_deps files %s" % ctx.files.pb_compile_deps) 7 | return struct( 8 | proto_language = struct( 9 | name = ctx.label.name, 10 | output_to_workspace = ctx.attr.output_to_workspace, 11 | output_to_jar = ctx.attr.output_to_jar, 12 | output_to_library = ctx.attr.output_to_library, 13 | output_to_libdir = ctx.attr.output_to_libdir, 14 | output_file_style = ctx.attr.output_file_style, 15 | supports_pb = ctx.attr.supports_pb, 16 | pb_plugin_implements_grpc = ctx.attr.pb_plugin_implements_grpc, 17 | pb_file_extensions = ctx.attr.pb_file_extensions, 18 | pb_options = ctx.attr.pb_options, 19 | pb_imports = ctx.attr.pb_imports, 20 | pb_inputs = ctx.files.pb_inputs, 21 | pb_outputs = ctx.attr.pb_outputs, 22 | pb_plugin_name = ctx.attr.pb_plugin_name, 23 | pb_plugin = ctx.executable.pb_plugin, 24 | pb_compile_deps = ctx.files.pb_compile_deps, 25 | pb_runtime_deps = ctx.files.pb_runtime_deps, 26 | supports_grpc = ctx.attr.supports_grpc or ctx.attr.pb_plugin_implements_grpc, 27 | grpc_file_extensions = ctx.attr.grpc_file_extensions, 28 | grpc_options = ctx.attr.grpc_options, 29 | grpc_imports = ctx.attr.grpc_imports, 30 | grpc_inputs = ctx.files.grpc_inputs, 31 | grpc_plugin_name = ctx.attr.grpc_plugin_name, 32 | grpc_plugin = ctx.executable.grpc_plugin, 33 | grpc_compile_deps = ctx.files.grpc_compile_deps, 34 | grpc_runtime_deps = ctx.files.grpc_runtime_deps, 35 | go_prefix = go_prefix, 36 | go_package = ctx.attr.go_package, 37 | go_importpath = ctx.attr.go_importpath, 38 | importmap = ctx.attr.importmap, 39 | ), 40 | ) 41 | 42 | 43 | proto_language_attrs = { 44 | "output_to_workspace": attr.bool(), 45 | "output_to_jar": attr.bool(), 46 | "output_to_library": attr.bool(), 47 | "output_to_libdir": attr.bool(), 48 | "output_file_style": attr.string(), 49 | 50 | "supports_pb": attr.bool(default = True), 51 | "pb_file_extensions": attr.string_list(), 52 | "pb_options": attr.string_list(), 53 | "pb_inputs": attr.label_list(), 54 | "pb_imports": attr.string_list(), 55 | "pb_outputs": attr.string_list(), 56 | "pb_plugin_name": attr.string(), 57 | "pb_plugin": attr.label( 58 | executable = True, 59 | cfg = "host", 60 | ), 61 | "pb_compile_deps": attr.label_list(), 62 | "pb_runtime_deps": attr.label_list(), 63 | "pb_plugin_implements_grpc": attr.bool(), 64 | "supports_grpc": attr.bool(default = False), 65 | "grpc_file_extensions": attr.string_list(), 66 | "grpc_options": attr.string_list(), 67 | "grpc_imports": attr.string_list(), 68 | "grpc_inputs": attr.label_list(), 69 | "grpc_plugin_name": attr.string(), 70 | "grpc_plugin": attr.label( 71 | executable = True, 72 | cfg = "host", 73 | ), 74 | "grpc_compile_deps": attr.label_list(), 75 | "grpc_runtime_deps": attr.label_list(), 76 | "go_prefix": attr.label( 77 | providers = ["go_prefix"], 78 | ), 79 | "go_importpath": attr.string( 80 | ), 81 | "go_package": attr.string(), 82 | "importmap": attr.string_dict(), 83 | } 84 | 85 | 86 | proto_language = rule( 87 | implementation = _proto_language_impl, 88 | attrs = proto_language_attrs, 89 | ) 90 | 91 | def _proto_language_deps_impl(ctx): 92 | files = [] 93 | exts = ctx.attr.file_extensions 94 | 95 | for dep in ctx.attr.langs: 96 | lang = dep.proto_language 97 | if ctx.attr.compile_deps: 98 | files += lang.pb_compile_deps 99 | if lang.supports_grpc and ctx.attr.with_grpc: 100 | files += lang.grpc_compile_deps 101 | if ctx.attr.runtime_deps: 102 | files += lang.pb_runtime_deps 103 | if lang.supports_grpc and ctx.attr.with_grpc: 104 | files += lang.grpc_runtime_deps 105 | 106 | deps = [] 107 | for file in files: 108 | for ext in exts: 109 | if file.path.endswith(ext): 110 | deps.append(file) 111 | 112 | return struct( 113 | files = depset(deps), 114 | ) 115 | 116 | proto_language_deps = rule( 117 | implementation = _proto_language_deps_impl, 118 | attrs = { 119 | "langs": attr.label_list( 120 | providers = ["proto_language"], 121 | mandatory = True, 122 | ), 123 | "with_grpc": attr.bool(default = True), 124 | "file_extensions": attr.string_list(mandatory = True), 125 | "compile_deps": attr.bool(default = True), 126 | "runtime_deps": attr.bool(default = False), 127 | } 128 | ) 129 | """Aggregates the deps named in pb_ and grpc_ proto_languages. 130 | """ 131 | -------------------------------------------------------------------------------- /protobuf/internal/proto_repositories.bzl: -------------------------------------------------------------------------------- 1 | load("//protobuf:internal/require.bzl", "require") 2 | load("//protobuf:deps.bzl", "DEPS") 3 | 4 | def proto_repositories(excludes = [], 5 | lang_deps = {}, 6 | lang_requires = [], 7 | protobuf_deps = DEPS, 8 | protobuf_requires = [ 9 | "com_google_protobuf", 10 | "protobuf", 11 | "protobuf_clib", 12 | "protocol_compiler", 13 | "protobuf_headers", 14 | ], 15 | overrides = {}, 16 | strict = False, 17 | verbose = 0): 18 | return require( 19 | keys = protobuf_requires + lang_requires, 20 | deps = protobuf_deps + lang_deps, 21 | excludes = excludes, 22 | overrides = overrides, 23 | verbose = verbose, 24 | strict = strict, 25 | ) 26 | -------------------------------------------------------------------------------- /protobuf/internal/require.bzl: -------------------------------------------------------------------------------- 1 | def _needs_install(name, dep, hkeys=["sha256", "sha1", "tag"], verbose=0, strict=False): 2 | 3 | # Does it already exist? 4 | existing_rule = native.existing_rule(name) 5 | if not existing_rule: 6 | return True 7 | 8 | # If it has already been defined and our dependency lists a 9 | # hash, do these match? If a hash mismatch is encountered, has 10 | # the user specifically granted permission to continue? 11 | for hkey in hkeys: 12 | expected = dep.get(hkey) 13 | actual = existing_rule.get(hkey) 14 | if expected: 15 | # By guarding the check of expected vs actual with a 16 | # pre-check for actual=None (or empty string), we're 17 | # basically saying "If the user did not bother to set a 18 | # sha256 or sha1 hash for the rule, they probably don't 19 | # care about overriding a dependency either, so don't 20 | # complain about it." In particular, rules_go does not a 21 | # set a sha256 for their com_google_protobuf http_archive 22 | # rule, so this gives us a convenient loophole to prevent 23 | # collisions on this dependency. The "strict" flag can be 24 | # used as as master switch to disable blowing up the 25 | # loading phase due to dependency collisions. 26 | if actual and expected != actual and strict: 27 | msg = """ 28 | An existing {0} rule '{1}' was already loaded with a {2} value of '{3}'. Refusing to overwrite this with the requested value ('{4}'). 29 | Either remove the pre-existing rule from your WORKSPACE or exclude it from loading by rules_protobuf (strict={5}. 30 | """.format(existing_rule["kind"], name, hkey, actual, expected, strict) 31 | 32 | fail(msg) 33 | else: 34 | if verbose > 1: print("Skip reload %s: %s = %s" % (name, hkey, actual)) 35 | return False 36 | 37 | # No kheys for this rule - in this case no reload; first one loaded wins. 38 | if verbose > 1: print("Skipping reload of existing target %s" % name) 39 | return False 40 | 41 | 42 | def _install(deps, verbose, strict): 43 | """Install a list if dependencies for matching native rules. 44 | Return: 45 | list of deps that have no matching native rule. 46 | """ 47 | todo = [] 48 | 49 | for d in deps: 50 | name = d.get("name") 51 | rule = d.pop("rule", None) 52 | if not rule: 53 | fail("Missing attribute 'rule': %s" % name) 54 | if hasattr(native, rule): 55 | rule = getattr(native, rule) 56 | if verbose: print("Loading %s)" % name) 57 | rule(**d) 58 | else: 59 | d["rule"] = rule 60 | todo.append(d) 61 | 62 | return todo 63 | 64 | 65 | def require(keys, 66 | deps = {}, 67 | overrides = {}, 68 | excludes = [], 69 | verbose = 0, 70 | strict = False): 71 | 72 | # 73 | # Make a list of non-excluded required deps with merged data. 74 | # 75 | required = [] 76 | 77 | for key in keys: 78 | dep = deps.get(key) 79 | if not dep: 80 | fail("Unknown workspace dependency: %s" % key) 81 | d = dict(**dep) # copy the 'frozen' object. 82 | if not key in excludes: 83 | over = overrides.get(key) 84 | data = d + over if over else d 85 | if _needs_install(key, data, verbose=verbose, strict=strict): 86 | data["name"] = key 87 | required.append(data) 88 | 89 | return _install(required, verbose, strict) 90 | -------------------------------------------------------------------------------- /protobuf/rules.bzl: -------------------------------------------------------------------------------- 1 | load("//protobuf:internal/proto_compile.bzl", "proto_compile") 2 | load("//protobuf:internal/proto_language.bzl", "proto_language", "proto_language_deps") 3 | load("//protobuf:internal/proto_repositories.bzl", "proto_repositories") 4 | load("//protobuf:internal/github_archive.bzl", "github_archive") 5 | load("//protobuf:internal/proto_http_archive.bzl", "proto_http_archive") 6 | load("//protobuf:internal/proto_dependencies.bzl", "proto_dependencies") 7 | -------------------------------------------------------------------------------- /python/BUILD: -------------------------------------------------------------------------------- 1 | package(default_visibility = ["//visibility:public"]) 2 | 3 | load("//protobuf:rules.bzl", "proto_language") 4 | 5 | proto_language( 6 | name = "python", 7 | grpc_file_extensions = ["_pb2_grpc.py"], 8 | grpc_plugin = "//external:protoc_gen_grpc_python", 9 | grpc_plugin_name = "grpc_python", 10 | pb_file_extensions = ["_pb2.py"], 11 | supports_grpc = True, 12 | ) 13 | -------------------------------------------------------------------------------- /python/README.md: -------------------------------------------------------------------------------- 1 | # Python Rules 2 | 3 | | Rule | Description | 4 | | ---: | :--- | 5 | | [py_proto_repositories](#py_proto_repositories) | Load workspace dependencies. | 6 | | [py_proto_compile](#py_proto_compile) | Generate python protobuf source files. | 7 | | [py_proto_library](#py_proto_library) | Generate and compiles protobuf source files. | 8 | 9 | ## py\_proto\_repositories 10 | 11 | 1. Enable python support by loading the dependencies in your workspace: 12 | 13 | ```python 14 | load("@org_pubref_rules_protobuf//python:rules.bzl", "py_proto_repositories") 15 | py_proto_repositories() 16 | ``` 17 | 18 | ## py\_proto\_compile 19 | 20 | This is a thin wrapper over the 21 | [proto_compile](../protobuf#proto_compile) rule having language 22 | `@org_pubref_rules_protobuf//python`. 23 | 24 | ```python 25 | load("@org_pubref_rules_protobuf//python:rules.bzl", "py_proto_compile") 26 | 27 | py_proto_compile( 28 | name = "protos", 29 | protos = ["message.proto"], 30 | with_grpc = True, # only one file is generated with or without grpc 31 | ) 32 | ``` 33 | 34 | ```sh 35 | $ bazel build :protos 36 | Target //:protos up-to-date: 37 | bazel-genfiles/message_pb2.py 38 | ``` 39 | 40 | ## py\_proto\_library 41 | 42 | Pass the set of protobuf source files to the `protos` attribute. 43 | 44 | ```python 45 | load("@org_pubref_rules_protobuf//python:rules.bzl", "py_proto_library") 46 | 47 | py_proto_library( 48 | name = "protolib", 49 | protos = ["message.proto"], 50 | with_grpc = True, 51 | ) 52 | ``` 53 | 54 | # Runtime gRPC support 55 | 56 | The instructions above demonstrate how to compile python sources for 57 | protocol buffer +/- gRPC support. For runtime grpc support in a 58 | `py_binary` rule, the 59 | [grpcio](https://pypi.python.org/pypi/grpcio/1.6.0) pypi package is 60 | required. You can have bazel include this as follows using 61 | [rules_python](https://github.com/bazelbuild/rules_python). 62 | 63 | ## Step 1: Install rules_python and pip requirements in your WORKSPACE 64 | 65 | ```python 66 | # 1-A. Pull down rules_python 67 | http_archive( 68 | name = "io_bazel_rules_python", 69 | rule: "http_archive", 70 | url: "https://github.com/bazelbuild/rules_python/archive/07fba0f91bb5898d19daeaabf635d08059f7eacd.zip", 71 | sha256: "53fecb9ddc5d3780006511c9904ed09c15a8aed0644914960db89f56b1e875bd", 72 | strip_prefix: "rules_python-07fba0f91bb5898d19daeaabf635d08059f7eacd", 73 | ) 74 | 75 | # 1-B. Load the skylark file containing the needed functions 76 | load("@io_bazel_rules_python//python:pip.bzl", "pip_repositories", "pip_import") 77 | 78 | # 1-C. Invoke pip_repositories to load rules_python internal dependencies 79 | pip_repositories() 80 | 81 | # 1-D. Invoke pip_import with the necessary python requirements. You can refer to the 82 | # one in rules_protobuf, or roll your own (make sure it includes 'grpcio==1.6.0' (or later)). 83 | pip_import( 84 | name = "pip_grpcio", 85 | requirements = "@org_pubref_rules_protobuf//python:requirements.txt", 86 | ) 87 | 88 | # 1-E. Load the requirements.bzl file that will be written in this new workspace 89 | # defined in 1-D. 90 | load("@pip_grpcio//:requirements.bzl", pip_grpcio_install = "pip_install") 91 | 92 | # 1-F. Invoke this new function to trigger pypi install of 'grpcio' when needed. 93 | pip_grpcio_install() 94 | ``` 95 | 96 | 97 | ## Step 2: Include the grpcio package the py_binary rule for your gRPC client/server 98 | 99 | ```python 100 | # Load the 'packages' function from the 'pip_grpcio' workspace 101 | load("@pip_grpcio//:requirements.bzl", "packages") 102 | 103 | # Define a py_binary rule and include the grpcio package as a dependency. 104 | py_binary( 105 | name = "my_grpc_server", 106 | srcs = [ 107 | "server.py", 108 | ], 109 | deps = [ 110 | ":protolib", # a py_proto_library rule 111 | packages("grpcio"), # grpcio pypi package 112 | ], 113 | ) 114 | ``` 115 | -------------------------------------------------------------------------------- /python/deps.bzl: -------------------------------------------------------------------------------- 1 | DEPS = { 2 | 3 | "protoc_gen_grpc_python": { 4 | "rule": "bind", 5 | "actual": "@com_google_grpc//:grpc_python_plugin", 6 | }, 7 | 8 | "grpcio": { 9 | "rule": "bind", 10 | "actual": "@com_google_grpc//:grpc_python_plugin", 11 | }, 12 | 13 | } 14 | -------------------------------------------------------------------------------- /python/requirements.txt: -------------------------------------------------------------------------------- 1 | grpcio==1.12.0 2 | protobuf==3.5.2.post1 -------------------------------------------------------------------------------- /python/rules.bzl: -------------------------------------------------------------------------------- 1 | load("//protobuf:rules.bzl", "proto_compile", "proto_repositories") 2 | load("//python:deps.bzl", "DEPS") 3 | load("//cpp:rules.bzl", "cpp_proto_repositories") 4 | 5 | def py_proto_repositories( 6 | omit_cpp_repositories = False, 7 | lang_deps = DEPS, 8 | lang_requires = [ 9 | "protoc_gen_grpc_python", 10 | ], **kwargs): 11 | 12 | if not omit_cpp_repositories: 13 | cpp_proto_repositories(**kwargs) 14 | 15 | proto_repositories(lang_deps = lang_deps, 16 | lang_requires = lang_requires, 17 | **kwargs) 18 | 19 | def py_proto_compile(langs = [str(Label("//python"))], **kwargs): 20 | proto_compile(langs = langs, **kwargs) 21 | 22 | def py_proto_library( 23 | name, 24 | langs = [str(Label("//python"))], 25 | protos = [], 26 | imports = [], 27 | inputs = [], 28 | proto_deps = [], 29 | output_to_workspace = False, 30 | protoc = None, 31 | 32 | pb_plugin = None, 33 | pb_options = [], 34 | 35 | grpc_plugin = None, 36 | grpc_options = [], 37 | 38 | proto_compile_args = {}, 39 | with_grpc = True, 40 | srcs = [], 41 | deps = [], 42 | py_proto_deps = [], 43 | verbose = 0, 44 | **kwargs): 45 | 46 | proto_compile_args += { 47 | "name": name + ".pb", 48 | "protos": protos, 49 | "deps": [dep + ".pb" for dep in proto_deps], 50 | "langs": langs, 51 | "imports": imports, 52 | "inputs": inputs, 53 | "pb_options": pb_options, 54 | "grpc_options": grpc_options, 55 | "output_to_workspace": output_to_workspace, 56 | "verbose": verbose, 57 | "with_grpc": with_grpc, 58 | } 59 | 60 | if protoc: 61 | proto_compile_args["protoc"] = protoc 62 | if pb_plugin: 63 | proto_compile_args["pb_plugin"] = pb_plugin 64 | if grpc_plugin: 65 | proto_compile_args["grpc_plugin"] = grpc_plugin 66 | 67 | proto_compile(**proto_compile_args) 68 | 69 | native.py_library( 70 | name = name, 71 | srcs = srcs + [name + ".pb"], 72 | deps = depset(deps + proto_deps).to_list(), 73 | **kwargs) 74 | -------------------------------------------------------------------------------- /ruby/BUILD: -------------------------------------------------------------------------------- 1 | package(default_visibility = ["//visibility:public"]) 2 | 3 | load("//protobuf:rules.bzl", "proto_language") 4 | 5 | proto_language( 6 | name = "ruby", 7 | grpc_file_extensions = ["_services_pb.rb"], 8 | grpc_plugin = "//external:protoc_gen_grpc_ruby", 9 | grpc_plugin_name = "grpc_ruby", 10 | pb_file_extensions = ["_pb.rb"], 11 | supports_grpc = True, 12 | ) 13 | -------------------------------------------------------------------------------- /ruby/README.md: -------------------------------------------------------------------------------- 1 | # Ruby Rules 2 | 3 | | Rule | Description | 4 | | ---: | :--- | 5 | | [ruby_proto_repositories](#ruby_proto_repositories) | Load workspace dependencies. | 6 | | [ruby_proto_compile](#ruby_proto_compile) | Generate ruby protobuf source files. | 7 | 8 | ## ruby\_proto\_repositories 9 | 10 | Enable ruby support by loading the dependencies in your workspace. 11 | 12 | ```python 13 | load("@org_pubref_rules_protobuf//ruby:rules.bzl", "ruby_proto_repositories") 14 | ruby_proto_repositories() 15 | ``` 16 | 17 | ## ruby\_proto\_compile 18 | 19 | This is a thin wrapper over the 20 | [proto_compile](../protobuf#proto_compile) rule having language 21 | `@org_pubref_rules_protobuf//ruby`. 22 | 23 | ```python 24 | load("@org_pubref_rules_protobuf//ruby:rules.bzl", "ruby_proto_compile") 25 | 26 | ruby_proto_compile( 27 | name = "protos", 28 | protos = ["message.proto"], 29 | ) 30 | ``` 31 | 32 | ```sh 33 | $ bazel build :protos 34 | Target //:protos up-to-date: 35 | bazel-genfiles/message_pb.rb 36 | ``` 37 | 38 | ## ruby\_proto\_library (not implemented) 39 | 40 | Support for a library rule would be dependent on loading the ruby 41 | runtime and ruby gems dependencies (this does not exist in bazel 42 | ecosystem at the moment). 43 | -------------------------------------------------------------------------------- /ruby/deps.bzl: -------------------------------------------------------------------------------- 1 | DEPS = { 2 | 3 | "protoc_gen_grpc_ruby": { 4 | "rule": "bind", 5 | "actual": "@com_google_grpc//:grpc_ruby_plugin", 6 | }, 7 | 8 | } 9 | -------------------------------------------------------------------------------- /ruby/rules.bzl: -------------------------------------------------------------------------------- 1 | load("//protobuf:rules.bzl", "proto_compile", "proto_repositories") 2 | load("//cpp:rules.bzl", "cpp_proto_repositories") 3 | load("//ruby:deps.bzl", "DEPS") 4 | 5 | def ruby_proto_repositories( 6 | omit_cpp_repositories = False, 7 | lang_deps = DEPS, 8 | lang_requires = [ 9 | "protoc_gen_grpc_ruby", 10 | ], **kwargs): 11 | 12 | if not omit_cpp_repositories: 13 | cpp_proto_repositories(**kwargs) 14 | 15 | proto_repositories(lang_deps = lang_deps, 16 | lang_requires = lang_requires, 17 | **kwargs) 18 | 19 | def ruby_proto_compile(langs = [str(Label("//ruby"))], **kwargs): 20 | proto_compile(langs = langs, **kwargs) 21 | -------------------------------------------------------------------------------- /tests/build_in_workspace_root/BUILD: -------------------------------------------------------------------------------- 1 | load("@org_pubref_rules_protobuf//cpp:rules.bzl", "cc_proto_compile") 2 | 3 | cc_proto_compile( 4 | name = "bar_proto", 5 | protos = ["foo/bar.proto"], 6 | with_grpc = True, 7 | ) 8 | -------------------------------------------------------------------------------- /tests/build_in_workspace_root/WORKSPACE: -------------------------------------------------------------------------------- 1 | local_repository( 2 | name = "org_pubref_rules_protobuf", 3 | path = "../..", 4 | ) 5 | 6 | load("@org_pubref_rules_protobuf//cpp:rules.bzl", "cpp_proto_repositories") 7 | cpp_proto_repositories() 8 | 9 | -------------------------------------------------------------------------------- /tests/build_in_workspace_root/foo/bar.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | 3 | package foo; 4 | 5 | message Bar { 6 | string data = 1; 7 | } 8 | -------------------------------------------------------------------------------- /tests/custom_go_importpath/BUILD: -------------------------------------------------------------------------------- 1 | package(default_visibility = ["//visibility:public"]) 2 | 3 | load("@io_bazel_rules_go//go:def.bzl", "go_binary") 4 | load( 5 | "//go:rules.bzl", 6 | "go_proto_library", 7 | GO_GRPC_COMPILE_DEPS = "GRPC_COMPILE_DEPS", 8 | ) 9 | 10 | go_proto_library( 11 | name = "api_proto", 12 | importpath = "github.com/my/custom/import/path", 13 | protos = ["api.proto"], 14 | with_grpc = True, 15 | ) 16 | 17 | go_binary( 18 | name = "client", 19 | srcs = ["main.go"], 20 | deps = [ 21 | ":api_proto", 22 | ] + GO_GRPC_COMPILE_DEPS, 23 | ) 24 | -------------------------------------------------------------------------------- /tests/custom_go_importpath/api.proto: -------------------------------------------------------------------------------- 1 | 2 | syntax = "proto3"; 3 | 4 | // The greeting service definition. 5 | service Greeter { 6 | // Sends a greeting 7 | rpc SayHello (HelloRequest) returns (HelloReply) {} 8 | } 9 | 10 | // The request message containing the user's name. 11 | message HelloRequest { 12 | string name = 1; 13 | } 14 | 15 | // The response message containing the greetings 16 | message HelloReply { 17 | string message = 1; 18 | } 19 | -------------------------------------------------------------------------------- /tests/custom_go_importpath/main.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2016 Google Inc, PubRef Inc. 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * * Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * * Redistributions in binary form must reproduce the above 13 | * copyright notice, this list of conditions and the following disclaimer 14 | * in the documentation and/or other materials provided with the 15 | * distribution. 16 | * * Neither the name of Google Inc. nor the names of its 17 | * contributors may be used to endorse or promote products derived from 18 | * this software without specific prior written permission. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | * 32 | */ 33 | 34 | package main 35 | 36 | import ( 37 | "log" 38 | "os" 39 | 40 | "golang.org/x/net/context" 41 | "google.golang.org/grpc" 42 | 43 | pb "github.com/my/custom/import/path" 44 | // ^A 45 | // 46 | // A: go_importpath = "github.com/my/custom/domain") 47 | 48 | ) 49 | 50 | const ( 51 | address = "localhost:50051" 52 | defaultName = "world" 53 | ) 54 | 55 | func main() { 56 | // Set up a connection to the server. 57 | conn, err := grpc.Dial(address, grpc.WithInsecure()) 58 | if err != nil { 59 | log.Fatalf("did not connect: %v", err) 60 | } 61 | defer conn.Close() 62 | c := pb.NewGreeterClient(conn) 63 | 64 | // Contact the server and print out its response. 65 | name := defaultName 66 | if len(os.Args) > 1 { 67 | name = os.Args[1] 68 | } 69 | r, err := c.SayHello(context.Background(), &pb.HelloRequest{Name: name}) 70 | if err != nil { 71 | log.Fatalf("could not greet: %v", err) 72 | } 73 | log.Printf("Greeting: %s", r.Message) 74 | } 75 | -------------------------------------------------------------------------------- /tests/external_proto_library/.gitignore: -------------------------------------------------------------------------------- 1 | # Ignore all bazel-* symlinks. There is no full list since this can change 2 | # based on the name of the directory bazel is cloned into. 3 | /bazel-* 4 | -------------------------------------------------------------------------------- /tests/external_proto_library/BUILD: -------------------------------------------------------------------------------- 1 | package(default_visibility = ["//visibility:public"]) 2 | 3 | load("@io_bazel_rules_go//go:def.bzl", "go_prefix") 4 | load("@org_pubref_rules_protobuf//cpp:rules.bzl", "cc_proto_library") 5 | load("@org_pubref_rules_protobuf//java:rules.bzl", "java_proto_library") 6 | load("@org_pubref_rules_protobuf//go:rules.bzl", "go_proto_library") 7 | 8 | go_prefix("github.com/pubref/rules_protobuf/tests/external_proto_library") 9 | 10 | cc_proto_library( 11 | name = "cc_gapi", 12 | imports = [ 13 | "external/com_github_googleapis_googleapis", 14 | ], 15 | proto_deps = [ 16 | "@com_github_googleapis_googleapis//:cc_label_proto", 17 | ], 18 | protos = ["message.proto"], 19 | verbose = 0, 20 | ) 21 | 22 | java_proto_library( 23 | name = "java_gapi", 24 | imports = [ 25 | "external/com_github_googleapis_googleapis", 26 | ], 27 | proto_deps = [ 28 | "@com_github_googleapis_googleapis//:java_label_proto", 29 | ], 30 | protos = ["message.proto"], 31 | verbose = 0, 32 | ) 33 | 34 | go_proto_library( 35 | name = "go_gapi", 36 | imports = [ 37 | "external/com_github_googleapis_googleapis", 38 | ], 39 | proto_deps = [ 40 | "@com_github_googleapis_googleapis//:go_label_proto", 41 | ], 42 | protos = ["message.proto"], 43 | verbose = 0, 44 | ) 45 | -------------------------------------------------------------------------------- /tests/external_proto_library/README.md: -------------------------------------------------------------------------------- 1 | # tests/external_proto_library 2 | 3 | This demonstrates one approach to building protos in the googleapis repository. 4 | -------------------------------------------------------------------------------- /tests/external_proto_library/WORKSPACE: -------------------------------------------------------------------------------- 1 | git_repository( 2 | name = "io_bazel_rules_go", 3 | remote = "https://github.com/bazelbuild/rules_go.git", 4 | tag = "0.5.4", 5 | ) 6 | 7 | load("@io_bazel_rules_go//go:def.bzl", "go_repositories") 8 | 9 | go_repositories() 10 | 11 | local_repository( 12 | name = "org_pubref_rules_protobuf", 13 | path = "../..", 14 | ) 15 | 16 | load("@org_pubref_rules_protobuf//cpp:rules.bzl", "cpp_proto_repositories") 17 | cpp_proto_repositories() 18 | 19 | load("@org_pubref_rules_protobuf//go:rules.bzl", "go_proto_repositories") 20 | go_proto_repositories() 21 | 22 | load("@org_pubref_rules_protobuf//java:rules.bzl", "java_proto_repositories", "nano_proto_repositories") 23 | java_proto_repositories() 24 | 25 | GOOGLEAPIS_BUILD_FILE = """ 26 | package(default_visibility = ["//visibility:public"]) 27 | exports_files(["google/api/label.proto"]) 28 | 29 | load("@io_bazel_rules_go//go:def.bzl", "go_prefix") 30 | go_prefix("google.golang.org/genproto/googleapis") 31 | 32 | load("@org_pubref_rules_protobuf//cpp:rules.bzl", "cc_proto_library") 33 | load("@org_pubref_rules_protobuf//java:rules.bzl", "java_proto_library") 34 | load("@org_pubref_rules_protobuf//go:rules.bzl", "go_proto_library") 35 | 36 | cc_proto_library( 37 | name = "cc_label_proto", 38 | protos = [ 39 | "google/api/label.proto", 40 | ], 41 | verbose = 0, 42 | ) 43 | 44 | java_proto_library( 45 | name = "java_label_proto", 46 | protos = [ 47 | "google/api/label.proto", 48 | ], 49 | verbose = 0, 50 | ) 51 | 52 | go_proto_library( 53 | name = "go_label_proto", 54 | protos = [ 55 | "google/api/label.proto", 56 | ], 57 | go_package = "google.golang.org/genproto/googleapis/api/label", 58 | verbose = 0, 59 | ) 60 | """ 61 | 62 | new_http_archive( 63 | name = "com_github_googleapis_googleapis", 64 | sha256 = "63555639d21dd0041a2e7e02d0af57b60da0b8dfccbf638fba8a930ceb555176", 65 | url = "https://github.com/googleapis/googleapis/archive/6277786c1352a6d7be1e5a5d697394e1802f6608.zip", 66 | strip_prefix = "googleapis-6277786c1352a6d7be1e5a5d697394e1802f6608", 67 | build_file_content = GOOGLEAPIS_BUILD_FILE, 68 | ) 69 | -------------------------------------------------------------------------------- /tests/external_proto_library/message.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | 3 | import "google/api/label.proto"; 4 | 5 | message Config { 6 | google.api.LabelDescriptor label = 1; 7 | } 8 | 9 | -------------------------------------------------------------------------------- /tests/generated_proto_file/BUILD: -------------------------------------------------------------------------------- 1 | package(default_visibility = ["//visibility:public"]) 2 | 3 | load("//cpp:rules.bzl", "cpp_proto_library") 4 | load("//go:rules.bzl", "go_proto_library") 5 | 6 | genrule( 7 | name = "copy_file", 8 | srcs = ["simple.proto"], 9 | outs = ["copy.proto"], 10 | cmd = "cat $(location simple.proto) > $@", 11 | message = "copy", 12 | ) 13 | 14 | cpp_proto_library( 15 | name = "default_library", 16 | protos = ["simple.proto"], 17 | verbose = 0, 18 | ) 19 | 20 | cpp_proto_library( 21 | name = "copy_library", 22 | protos = ["copy.proto"], 23 | with_grpc = True, 24 | verbose = 0, 25 | ) 26 | 27 | go_proto_library( 28 | name = "go_default_library", 29 | protos = ["simple.proto"], 30 | ) 31 | 32 | go_proto_library( 33 | name = "go_copy_library", 34 | protos = ["copy.proto"], 35 | with_grpc = True, 36 | ) 37 | 38 | cpp_proto_library( 39 | name = "subdir_default_library", 40 | protos = ["subdir/simple.proto"], 41 | verbose = 0, 42 | ) 43 | 44 | genrule( 45 | name = "subdir_copy_proto", 46 | srcs = ["subdir/simple.proto"], 47 | outs = ["subdir/copy.proto"], 48 | cmd = "cat $< > $@", 49 | message = "copy", 50 | ) 51 | 52 | cpp_proto_library( 53 | name = "subdir_copy_library", 54 | protos = [":subdir_copy_proto"], 55 | with_grpc = True, 56 | verbose = 0, 57 | ) 58 | 59 | genrule( 60 | name = "complicated_copy_proto", 61 | srcs = ["complicated/complicated.proto"], 62 | outs = ["complicated/comp_copy.proto"], 63 | cmd = "cat $< > $@", 64 | message = "copy", 65 | ) 66 | 67 | genrule( 68 | name = "complicated_copy_proto_copy", 69 | srcs = ["complicated/complicated_copy.proto"], 70 | outs = ["complicated/comp_copy2.proto"], 71 | cmd = "cat $< > $@", 72 | message = "copy", 73 | ) 74 | 75 | cpp_proto_library( 76 | name = "complicated_default_library", 77 | protos = ["complicated/complicated.proto"], 78 | verbose = 0, 79 | proto_deps = [ 80 | ":subdir_default_library", 81 | ], 82 | inputs = [ 83 | "@com_google_protobuf//:well_known_protos", 84 | ], 85 | ) 86 | 87 | cpp_proto_library( 88 | name = "complicated_default_library_on_gen", 89 | protos = ["complicated/complicated_copy.proto"], 90 | verbose = 0, 91 | proto_deps = [ 92 | ":subdir_copy_library", 93 | ], 94 | ) 95 | 96 | cpp_proto_library( 97 | name = "complicated_copy_library", 98 | protos = [":complicated_copy_proto"], 99 | verbose = 0, 100 | proto_deps = [ 101 | ":subdir_default_library", 102 | ], 103 | ) 104 | 105 | cpp_proto_library( 106 | name = "complicated_copy_library_on_gen", 107 | protos = [":complicated_copy_proto_copy"], 108 | verbose = 0, 109 | proto_deps = [ 110 | ":subdir_copy_library", 111 | ], 112 | ) -------------------------------------------------------------------------------- /tests/generated_proto_file/complicated/complicated.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | 3 | import "tests/generated_proto_file/subdir/simple.proto"; 4 | 5 | package complicated; 6 | 7 | message Test { 8 | subdir.Test test = 1; 9 | } 10 | 11 | -------------------------------------------------------------------------------- /tests/generated_proto_file/complicated/complicated_copy.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | 3 | import "tests/generated_proto_file/subdir/copy.proto"; 4 | 5 | package complicated; 6 | 7 | message Test { 8 | subdir.Test test = 1; 9 | } 10 | -------------------------------------------------------------------------------- /tests/generated_proto_file/simple.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | 3 | package simple; 4 | 5 | message Test { 6 | string value = 1; 7 | } 8 | -------------------------------------------------------------------------------- /tests/generated_proto_file/subdir/simple.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | 3 | package subdir; 4 | 5 | message Test { 6 | string value = 1; 7 | } 8 | -------------------------------------------------------------------------------- /tests/gogo/BUILD: -------------------------------------------------------------------------------- 1 | load("@io_bazel_rules_go//go:def.bzl", "go_prefix", "go_binary") 2 | 3 | go_prefix("github.com/pubref/rules_protobuf/tests/gogo") 4 | 5 | load("@org_pubref_rules_protobuf//gogo:rules.bzl", "gogo_proto_library") 6 | 7 | gogo_proto_library( 8 | name = "api_proto_go", 9 | protos = [ 10 | "api.proto", 11 | ], 12 | importpath = "github.com/pubref/rules_protobuf/tests/gogo/api", 13 | with_grpc = True, 14 | ) 15 | 16 | go_binary( 17 | name = "client", 18 | srcs = [ 19 | "client.go", 20 | ], 21 | deps = [ 22 | ":api_proto_go", 23 | "@org_golang_google_grpc//:go_default_library", 24 | "@org_golang_x_net//context:go_default_library", 25 | ], 26 | importpath = "github.com/pubref/rules_protobuf/tests/gogo/client", 27 | ) 28 | 29 | go_binary( 30 | name = "server", 31 | srcs = [ 32 | "server.go", 33 | ], 34 | deps = [ 35 | ":api_proto_go", 36 | "@org_golang_google_grpc//:go_default_library", 37 | "@org_golang_x_net//context:go_default_library", 38 | ], 39 | importpath = "github.com/pubref/rules_protobuf/tests/gogo/server", 40 | ) 41 | 42 | sh_test( 43 | name = "gogo_test", 44 | srcs = [ 45 | "gogo_test.sh", 46 | ], 47 | data = [ 48 | ":client", 49 | ":server", 50 | ], 51 | size = "small", 52 | ) 53 | -------------------------------------------------------------------------------- /tests/gogo/WORKSPACE: -------------------------------------------------------------------------------- 1 | local_repository( 2 | name = "org_pubref_rules_protobuf", 3 | path = "../..", 4 | ) 5 | 6 | load("@org_pubref_rules_protobuf//protobuf:rules.bzl", "github_archive") 7 | 8 | # ================================================================ 9 | # Go support requires rules_go 10 | # ================================================================ 11 | 12 | github_archive( 13 | name = "io_bazel_rules_go", 14 | commit = "6d900bc95ae678bec5c91031b8e987957d2a7f93", # post-0.7.0 (includes important cross-compile fixes) 15 | org = "bazelbuild", 16 | repo = "rules_go", 17 | sha256 = "d36baba631b29151434726eb204fa93ce8793b5f8ef96da452f382d77bd95c93", 18 | ) 19 | 20 | load("@io_bazel_rules_go//go:def.bzl", "go_rules_dependencies", "go_register_toolchains") 21 | 22 | go_rules_dependencies() 23 | 24 | go_register_toolchains() 25 | 26 | 27 | load("@io_bazel_rules_go//proto:def.bzl", "proto_register_toolchains") 28 | 29 | proto_register_toolchains() 30 | 31 | 32 | # ================================================================ 33 | # Gogo Support 34 | # ================================================================ 35 | 36 | load("@org_pubref_rules_protobuf//gogo:rules.bzl", "gogo_proto_repositories") 37 | 38 | gogo_proto_repositories() 39 | -------------------------------------------------------------------------------- /tests/gogo/api.proto: -------------------------------------------------------------------------------- 1 | 2 | syntax = "proto3"; 3 | 4 | // The api service definition. 5 | service Api { 6 | rpc Get(Request) returns (Response) {} 7 | } 8 | 9 | // The request message containing the user's name. 10 | message Request { 11 | string name = 1; 12 | } 13 | 14 | // The response message containing the greetings 15 | message Response { 16 | string message = 1; 17 | } 18 | -------------------------------------------------------------------------------- /tests/gogo/client.go: -------------------------------------------------------------------------------- 1 | 2 | package main 3 | 4 | import ( 5 | "github.com/pubref/rules_protobuf/tests/gogo/api" 6 | "golang.org/x/net/context" 7 | "google.golang.org/grpc" 8 | "log" 9 | "os" 10 | ) 11 | 12 | const ( 13 | address = "localhost:50051" 14 | ) 15 | 16 | func main() { 17 | log.Printf("Client attempting gRPC request to %s\n", address) 18 | 19 | conn, err := grpc.Dial(address, grpc.WithInsecure()) 20 | if err != nil { 21 | log.Fatalf("did not connect: %v", err) 22 | } 23 | defer conn.Close() 24 | 25 | client := api.NewApiClient(conn) 26 | 27 | 28 | name := "foo" 29 | if len(os.Args) > 1 { 30 | name = os.Args[1] 31 | } 32 | 33 | r, err := client.Get(context.Background(), &api.Request{Name: name}) 34 | if err != nil { 35 | log.Fatalf("could not make request: %v", err) 36 | } 37 | 38 | log.Printf("API Response: %s", r.Message) 39 | } 40 | -------------------------------------------------------------------------------- /tests/gogo/gogo_test.sh: -------------------------------------------------------------------------------- 1 | set -euo pipefail 2 | 3 | server& 4 | 5 | if client 'gogo' 2>&1 | grep 'API Response: Hello gogo'; then 6 | echo "PASS" 7 | else 8 | exit 1 9 | fi 10 | -------------------------------------------------------------------------------- /tests/gogo/server.go: -------------------------------------------------------------------------------- 1 | 2 | package main 3 | 4 | import ( 5 | "github.com/pubref/rules_protobuf/tests/gogo/api" 6 | "golang.org/x/net/context" 7 | "google.golang.org/grpc" 8 | "log" 9 | "net" 10 | ) 11 | 12 | const ( 13 | port = ":50051" 14 | ) 15 | 16 | type Server struct{} 17 | 18 | func (s *Server) Get(ctx context.Context, req *api.Request) (*api.Response, error) { 19 | return &api.Response{Message: "Hello " + req.Name}, nil 20 | } 21 | 22 | func main() { 23 | lis, err := net.Listen("tcp", port) 24 | if err != nil { 25 | log.Fatalf("failed to listen: %v", err) 26 | } 27 | server := grpc.NewServer() 28 | api.RegisterApiServer(server, &Server{}) 29 | log.Printf("Server listening on port %s\n", port) 30 | server.Serve(lis) 31 | } 32 | -------------------------------------------------------------------------------- /tests/proto_file_in_subdirectory/BUILD: -------------------------------------------------------------------------------- 1 | load("//cpp:rules.bzl", "cc_proto_library") 2 | 3 | cc_test( 4 | name = "test", 5 | size = "small", 6 | srcs = ["qux_test.cc"], 7 | copts = ["-Iexternal/gtest/include"], 8 | deps = [ 9 | ":protolib", 10 | "@com_google_googletest//:gtest", 11 | ], 12 | ) 13 | 14 | cc_proto_library( 15 | name = "protolib", 16 | protos = [ 17 | "foo/bar/baz.proto", 18 | ], 19 | verbose = 0, 20 | ) 21 | -------------------------------------------------------------------------------- /tests/proto_file_in_subdirectory/README.md: -------------------------------------------------------------------------------- 1 | See https://github.com/pubref/rules_protobuf/issues/24. 2 | -------------------------------------------------------------------------------- /tests/proto_file_in_subdirectory/foo/bar/baz.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | 3 | package foo.bar.baz; 4 | 5 | message Qux { 6 | bool verbose = 1; 7 | } 8 | 9 | -------------------------------------------------------------------------------- /tests/proto_file_in_subdirectory/qux_test.cc: -------------------------------------------------------------------------------- 1 | #include "gtest/gtest.h" 2 | 3 | #include "tests/proto_file_in_subdirectory/foo/bar/baz.pb.h" 4 | 5 | TEST(QuxTest, CanCreateMessage) { 6 | GOOGLE_PROTOBUF_VERIFY_VERSION; 7 | foo::bar::baz::Qux msg = foo::bar::baz::Qux(); 8 | msg.set_verbose(1); 9 | EXPECT_EQ(1, msg.verbose()); 10 | } 11 | 12 | int main(int ac, char* av[]) { 13 | testing::InitGoogleTest(&ac, av); 14 | return RUN_ALL_TESTS(); 15 | } 16 | -------------------------------------------------------------------------------- /tests/with_grpc_false/BUILD: -------------------------------------------------------------------------------- 1 | package(default_visibility = ["//visibility:public"]) 2 | 3 | # See https://github.com/pubref/rules_protobuf/issues/48 4 | 5 | load("@org_pubref_rules_protobuf//cpp:rules.bzl", "cc_proto_library") 6 | load("@org_pubref_rules_protobuf//java:rules.bzl", "java_proto_library") 7 | 8 | filegroup( 9 | name = "protos", 10 | srcs = ["message.proto"], 11 | ) 12 | 13 | cc_proto_library( 14 | name = "cpp", 15 | imports = [ 16 | "external/com_google_protobuf/src/", 17 | ], 18 | inputs = [ 19 | "@com_google_protobuf//:well_known_protos", 20 | ], 21 | protos = [":protos"], 22 | verbose = 0, 23 | with_grpc = False, 24 | ) 25 | 26 | java_proto_library( 27 | name = "java", 28 | imports = [ 29 | "external/com_google_protobuf/src/", 30 | ], 31 | inputs = [ 32 | "@com_google_protobuf//:well_known_protos", 33 | ], 34 | protos = [":protos"], 35 | verbose = 0, 36 | with_grpc = False, 37 | ) 38 | -------------------------------------------------------------------------------- /tests/with_grpc_false/message.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | 3 | import "google/protobuf/any.proto"; 4 | import "google/protobuf/duration.proto"; 5 | import "google/protobuf/timestamp.proto"; 6 | import "google/protobuf/empty.proto"; 7 | import "google/protobuf/struct.proto"; 8 | import "google/protobuf/wrappers.proto"; 9 | import "google/protobuf/descriptor.proto"; 10 | import "google/protobuf/compiler/plugin.proto"; 11 | 12 | message Message { 13 | string id = 1; 14 | google.protobuf.FileDescriptorProto file = 2; 15 | google.protobuf.Any any = 3; 16 | google.protobuf.Duration duration = 4; 17 | google.protobuf.Timestamp ts = 5; 18 | google.protobuf.Empty empty = 6; 19 | google.protobuf.Struct struct = 7; 20 | google.protobuf.BytesValue bytes_wrapper = 8; 21 | google.protobuf.compiler.CodeGeneratorRequest code_generator_request = 9; 22 | } 23 | 24 | extend google.protobuf.MessageOptions { 25 | string my_option = 10001; 26 | } 27 | 28 | --------------------------------------------------------------------------------