zhangkun83 | da3c3f8 | 2015-03-11 18:03:31 -0700 | [diff] [blame^] | 1 | gRPC Java Codegen Plugin for Protobuf Compiler |
zhangkun83 | d54a463 | 2015-01-26 10:53:17 -0800 | [diff] [blame] | 2 | ============================================== |
| 3 | |
zhangkun83 | da3c3f8 | 2015-03-11 18:03:31 -0700 | [diff] [blame^] | 4 | This generates the Java interfaces out of the service definition from a |
| 5 | `.proto` file. It works with the Protobuf Compiler (``protoc``). |
zhangkun83 | d54a463 | 2015-01-26 10:53:17 -0800 | [diff] [blame] | 6 | |
zhangkun83 | da3c3f8 | 2015-03-11 18:03:31 -0700 | [diff] [blame^] | 7 | Normally you don't need to compile the codegen by yourself, since pre-compiled |
| 8 | binaries for common platforms are available on Maven Central. However, if the |
| 9 | pre-compiled binaries are not compatible with your system, you may want to |
| 10 | build your own codegen. |
zhangkun83 | d54a463 | 2015-01-26 10:53:17 -0800 | [diff] [blame] | 11 | |
zhangkun83 | da3c3f8 | 2015-03-11 18:03:31 -0700 | [diff] [blame^] | 12 | ## System requirement |
zhangkun83 | d54a463 | 2015-01-26 10:53:17 -0800 | [diff] [blame] | 13 | |
zhangkun83 | da3c3f8 | 2015-03-11 18:03:31 -0700 | [diff] [blame^] | 14 | * Linux, Mac OS X with Clang, or Windows with MSYS2 |
| 15 | * Java 7 or up |
| 16 | * [Protobuf](https://github.com/google/protobuf) 3.0.0-alpha-2 or up |
| 17 | |
| 18 | ## Compiling and testing the codegen |
zhangkun83 | d54a463 | 2015-01-26 10:53:17 -0800 | [diff] [blame] | 19 | Change to the `compiler` directory: |
| 20 | ``` |
| 21 | $ cd $GRPC_JAVA_ROOT/compiler |
| 22 | ``` |
| 23 | |
| 24 | To compile the plugin: |
| 25 | ``` |
zhangkun83 | da3c3f8 | 2015-03-11 18:03:31 -0700 | [diff] [blame^] | 26 | $ ../gradlew local_archJava_pluginExecutable |
zhangkun83 | d54a463 | 2015-01-26 10:53:17 -0800 | [diff] [blame] | 27 | ``` |
| 28 | |
| 29 | To test the plugin with the compiler: |
| 30 | ``` |
zhangkun83 | da3c3f8 | 2015-03-11 18:03:31 -0700 | [diff] [blame^] | 31 | $ ../gradlew test |
zhangkun83 | d54a463 | 2015-01-26 10:53:17 -0800 | [diff] [blame] | 32 | ``` |
| 33 | You will see a `PASS` if the test succeeds. |
| 34 | |
| 35 | To compile a proto file and generate Java interfaces out of the service definitions: |
| 36 | ``` |
zhangkun83 | da3c3f8 | 2015-03-11 18:03:31 -0700 | [diff] [blame^] | 37 | $ protoc --plugin=protoc-gen-java_rpc=build/binaries/java_pluginExecutable/local_arch/protoc-gen-grpc-java \ |
zhangkun83 | d54a463 | 2015-01-26 10:53:17 -0800 | [diff] [blame] | 38 | --java_rpc_out="$OUTPUT_FILE" --proto_path="$DIR_OF_PROTO_FILE" "$PROTO_FILE" |
| 39 | ``` |
Xiao Hang | 6f0b21e | 2015-02-26 11:19:59 -0800 | [diff] [blame] | 40 | To generate Java interfaces with protobuf nano: |
| 41 | ``` |
zhangkun83 | da3c3f8 | 2015-03-11 18:03:31 -0700 | [diff] [blame^] | 42 | $ protoc --plugin=protoc-gen-java_rpc=build/binaries/java_pluginExecutable/local_arch/protoc-gen-grpc-java \ |
Xiao Hang | 6f0b21e | 2015-02-26 11:19:59 -0800 | [diff] [blame] | 43 | --java_rpc_out=nano=true:"$OUTPUT_FILE" --proto_path="$DIR_OF_PROTO_FILE" "$PROTO_FILE" |
| 44 | ``` |
zhangkun83 | da3c3f8 | 2015-03-11 18:03:31 -0700 | [diff] [blame^] | 45 | |
| 46 | ## Installing the codegen to Maven local repository |
| 47 | This will compile a codegen and put it under your ``~/.m2/repository``. This |
| 48 | will make it available to any build tool that pulls codegens from Maven |
| 49 | repostiories. |
| 50 | ``` |
| 51 | $ ../gradlew install |
| 52 | ``` |
| 53 | |
| 54 | ## Pushing the codegen to Maven Central |
| 55 | This will compile both the 32-bit and 64-bit versions of the codegen and upload |
| 56 | them to Maven Central. |
| 57 | |
| 58 | You need to have both the 32-bit and 64-bit versions of Protobuf installed. |
| 59 | It's recommended that you install Protobuf to custom locations (e.g., |
| 60 | ``~/protobuf-3.0.0-32`` and ``~/protobuf-3.0.0-64``) rather than the system |
| 61 | default location, so that different version of Protobuf won't mess up. It |
| 62 | should also be compiled statically linked. |
| 63 | |
| 64 | Generally speaking, to compile and install both 32-bit and 64-bit artifacts |
| 65 | locally. You may want to use ``--info`` so that you know what executables are |
| 66 | produced. |
| 67 | ``` |
| 68 | $ TARGET_ARCHS="<list-of-archs>" ../gradlew install --info |
| 69 | ``` |
| 70 | |
| 71 | To compile and upload the artifacts to Maven Central: |
| 72 | ``` |
| 73 | $ TARGET_ARCHS="<list-of-archs>" ../gradlew uploadArchives |
| 74 | ``` |
| 75 | |
| 76 | It's recommended that you run ``install --info`` prior to ``uploadArchives`` |
| 77 | and examine the produced artifacts before running ``uploadArchives``. |
| 78 | |
| 79 | Additional environments need to be set so that the compiler and linker can find |
| 80 | the protobuf headers and libraries. Follow the platform-specific instructions |
| 81 | below. |
| 82 | |
| 83 | ### Note for using Sonatype OSSRH services |
| 84 | |
| 85 | A full fledged release is done by running ``uploadArchives`` on multiple |
| 86 | platforms. After you have successfully run ``uploadArchives`` on the first |
| 87 | platfrom, you should go to OSSRH UI and find the staging repository that has |
| 88 | been automatically created. At the subsequent runs of ``uploadArchives``, you |
| 89 | need to append ``-DrepositoryId=<repository-id-you-just-found>`` to your |
| 90 | command line, so that the artifacts can be merged in a single release. |
| 91 | |
| 92 | ### Linux |
| 93 | You must have multilib GCC installed on your system. |
| 94 | |
| 95 | Compile and install 32-bit protobuf: |
| 96 | ``` |
| 97 | protobuf$ CXXFLAGS="-m32" ./configure --disable-shared --prefix=$HOME/protobuf-32 |
| 98 | protobuf$ make clean && make && make install |
| 99 | ``` |
| 100 | |
| 101 | Compile and install 64-bit protobuf: |
| 102 | ``` |
| 103 | protobuf$ CXXFLAGS="-m64" ./configure --disable-shared --prefix=$HOME/protobuf-64 |
| 104 | protobuf$ make clean && make && make install |
| 105 | ``` |
| 106 | |
| 107 | Compile and deploy GRPC codegen: |
| 108 | ``` |
| 109 | $ CXXFLAGS="-I$HOME/protobuf-32/include" \ |
| 110 | LDFLAGS="-L$HOME/protobuf-32/lib -L$HOME/protobuf-64/lib" \ |
| 111 | TARGET_ARCHS="x86_32 x86_64" ../gradlew uploadArchives |
| 112 | ``` |
| 113 | |
| 114 | ### Windows 64-bit with MSYS2 (Recommended for Windows) |
| 115 | Because the gcc shipped with MSYS2 doesn't support multilib, you have to |
| 116 | compile and deploy 32-bit and 64-bit binaries in separate steps. |
| 117 | |
| 118 | #### Under MinGW-w64 Win32 Shell |
| 119 | Compile and install 32-bit protobuf: |
| 120 | ``` |
| 121 | protobuf$ ./configure --disable-shared --prefix=$HOME/protobuf-32 |
| 122 | protobuf$ make clean && make && make install |
| 123 | ``` |
| 124 | |
| 125 | Compile and deploy GRPC 32-bit codegen |
| 126 | ``` |
| 127 | $ CXXFLAGS="-I$HOME/protobuf-32/include" \ |
| 128 | LDFLAGS="-L$HOME/protobuf-32/lib" \ |
| 129 | TARGET_ARCHS="x86_32" ../gradlew uploadArchives |
| 130 | ``` |
| 131 | |
| 132 | #### Under MinGW-w64 Win64 Shell |
| 133 | Compile and install 64-bit protobuf: |
| 134 | ``` |
| 135 | protobuf$ ./configure --disable-shared --prefix=$HOME/protobuf-64 |
| 136 | protobuf$ make clean && make && make install |
| 137 | ``` |
| 138 | |
| 139 | Compile and deploy GRPC 64-bit codegen: |
| 140 | ``` |
| 141 | $ CXXFLAGS="-I$HOME/protobuf-64/include" \ |
| 142 | LDFLAGS="-L$HOME/protobuf-64/lib" \ |
| 143 | TARGET_ARCHS="x86_64" ../gradlew uploadArchives |
| 144 | ``` |
| 145 | |
| 146 | ### Windows 64-bit with Cygwin64 (TODO: incomplete) |
| 147 | Because the MinGW gcc shipped with Cygwin64 doesn't support multilib, you have |
| 148 | to compile and deploy 32-bit and 64-bit binaries in separate steps. |
| 149 | |
| 150 | Compile and install 32-bit protobuf. ``-static-libgcc -static-libstdc++`` are |
| 151 | needed for ``protoc`` to be successfully run in the unit test. |
| 152 | ``` |
| 153 | protobuf$ LDFLAGS="-static-libgcc -static-libstdc++" ./configure --host=i686-w64-mingw32 --disable-shared --prefix=$HOME/protobuf-32 |
| 154 | protobuf$ make clean && make && make install |
| 155 | ``` |
| 156 | |
| 157 | Compile and install 64-bit protobuf: |
| 158 | ``` |
| 159 | protobuf$ ./configure --host=x86_64-w64-mingw32 --disable-shared --prefix=$HOME/protobuf-64 |
| 160 | protobuf$ make clean && make && make install |
| 161 | ``` |
| 162 | |
| 163 | ### Mac |
| 164 | Please refer to [Protobuf |
| 165 | README](https://github.com/google/protobuf/blob/master/README.md) for how to |
| 166 | set up GCC and Unix tools on Mac. |
| 167 | |
| 168 | Mac OS X has been 64-bit-only since 10.7 and we are compiling for 10.7 and up. |
| 169 | We only build 64-bit artifact for Mac. |
| 170 | |
| 171 | Compile and install protobuf: |
| 172 | ``` |
| 173 | protobuf$ CXXFLAGS="-m64" ./configure --disable-shared --prefix=$HOME/protobuf |
| 174 | protobuf$ make clean && make && make install |
| 175 | ``` |
| 176 | |
| 177 | Compile and deploy GRPC codegen: |
| 178 | ``` |
| 179 | $ CXXFLAGS="-I$HOME/protobuf/include" \ |
| 180 | LDFLAGS="$HOME/protobuf/lib/libprotobuf.a $HOME/protobuf/lib/libprotoc.a" \ |
| 181 | TARGET_ARCHS="x86_64" ../gradlew uploadArchives |
| 182 | ``` |