From 775517a3f176a46d8d3562fed9fc882fa0de9fa6 Mon Sep 17 00:00:00 2001 From: yulin-liang Date: Tue, 14 Jul 2020 11:21:22 -0700 Subject: [PATCH] Provide an objc-generator option to import system files in a local way --- src/compiler/objective_c_plugin.cc | 64 +++++++++++++++---- .../tests/run_plugin_option_tests.sh | 16 ++++- 2 files changed, 66 insertions(+), 14 deletions(-) diff --git a/src/compiler/objective_c_plugin.cc b/src/compiler/objective_c_plugin.cc index 5a9c7e7d5a4..4c37b07aa4e 100644 --- a/src/compiler/objective_c_plugin.cc +++ b/src/compiler/objective_c_plugin.cc @@ -90,8 +90,10 @@ class ObjectiveCGrpcGenerator : public grpc::protobuf::compiler::CodeGenerator { return true; } + bool grpc_local_import; ::std::string framework; ::std::string pb_runtime_import_prefix; + ::std::string grpc_local_import_prefix; std::vector<::std::string> params_list = grpc_generator::tokenize(parameter, ","); for (auto param_str = params_list.begin(); param_str != params_list.end(); @@ -117,6 +119,13 @@ class ObjectiveCGrpcGenerator : public grpc::protobuf::compiler::CodeGenerator { } pb_runtime_import_prefix = param[1]; grpc_generator::StripSuffix(&pb_runtime_import_prefix, "/"); + } else if (param[0] == "grpc_local_import_prefix") { + grpc_local_import = true; + if (param.size() != 2) { + *error = grpc::string("Format: grpc_local_import_prefix=dir/"); + return false; + } + grpc_local_import_prefix = param[1]; } } @@ -161,14 +170,30 @@ class ObjectiveCGrpcGenerator : public grpc::protobuf::compiler::CodeGenerator { imports = FrameworkImport(file_name + ".pbobjc.h", framework); } - ::std::string system_imports = - SystemImport("ProtoRPC/ProtoService.h") + - (generator_params.no_v1_compatibility - ? SystemImport("ProtoRPC/ProtoRPC.h") - : SystemImport("ProtoRPC/ProtoRPCLegacy.h")); - if (!generator_params.no_v1_compatibility) { - system_imports += SystemImport("RxLibrary/GRXWriteable.h") + - SystemImport("RxLibrary/GRXWriter.h"); + ::std::string system_imports; + if (grpc_local_import) { + system_imports = + LocalImport(grpc_local_import_prefix + "ProtoRPC/ProtoService.h"); + if (generator_params.no_v1_compatibility) { + system_imports += + LocalImport(grpc_local_import_prefix + "ProtoRPC/ProtoRPC.h"); + } else { + system_imports += LocalImport(grpc_local_import_prefix + + "ProtoRPC/ProtoRPCLegacy.h"); + system_imports += LocalImport(grpc_local_import_prefix + + "RxLibrary/GRXWriteable.h"); + system_imports += + LocalImport(grpc_local_import_prefix + "RxLibrary/GRXWriter.h"); + } + } else { + system_imports = SystemImport("ProtoRPC/ProtoService.h"); + if (generator_params.no_v1_compatibility) { + system_imports += SystemImport("ProtoRPC/ProtoRPC.h"); + } else { + system_imports += SystemImport("ProtoRPC/ProtoRPCLegacy.h"); + system_imports += SystemImport("RxLibrary/GRXWriteable.h"); + system_imports += SystemImport("RxLibrary/GRXWriter.h"); + } } ::std::string forward_declarations = @@ -232,11 +257,24 @@ class ObjectiveCGrpcGenerator : public grpc::protobuf::compiler::CodeGenerator { imports = FrameworkImport(file_name + ".pbrpc.h", framework) + FrameworkImport(file_name + ".pbobjc.h", framework); } - imports += (generator_params.no_v1_compatibility - ? SystemImport("ProtoRPC/ProtoRPC.h") - : SystemImport("ProtoRPC/ProtoRPCLegacy.h")); - if (!generator_params.no_v1_compatibility) { - imports += SystemImport("RxLibrary/GRXWriter+Immediate.h"); + + if (grpc_local_import) { + if (generator_params.no_v1_compatibility) { + imports += + LocalImport(grpc_local_import_prefix + "ProtoRPC/ProtoRPC.h"); + } else { + imports += LocalImport(grpc_local_import_prefix + + "ProtoRPC/ProtoRPCLegacy.h"); + imports += LocalImport(grpc_local_import_prefix + + "RxLibrary/GRXWriter+Immediate.h"); + } + } else { + if (generator_params.no_v1_compatibility) { + imports += SystemImport("ProtoRPC/ProtoRPC.h"); + } else { + imports += SystemImport("ProtoRPC/ProtoRPCLegacy.h"); + imports += SystemImport("RxLibrary/GRXWriter+Immediate.h"); + } } ::std::string class_imports; diff --git a/src/objective-c/tests/run_plugin_option_tests.sh b/src/objective-c/tests/run_plugin_option_tests.sh index 2263f571d75..8f7a6a10be3 100755 --- a/src/objective-c/tests/run_plugin_option_tests.sh +++ b/src/objective-c/tests/run_plugin_option_tests.sh @@ -37,11 +37,12 @@ rm -rf RemoteTestClient/*pb* $PROTOC \ --plugin=protoc-gen-grpc=$PLUGIN \ --objc_out=RemoteTestClient \ - --grpc_out=runtime_import_prefix=$RUNTIME_IMPORT_PREFIX:RemoteTestClient \ + --grpc_out=grpc_local_import_prefix=$RUNTIME_IMPORT_PREFIX,runtime_import_prefix=$RUNTIME_IMPORT_PREFIX:RemoteTestClient \ -I $ROOT_DIR \ -I ../../../third_party/protobuf/src \ $ROOT_DIR/src/objective-c/examples/RemoteTestClient/*.proto +# Verify the "runtime_import_prefix" option # Verify the output proto filename [ -e ./RemoteTestClient/src/objective-c/examples/RemoteTestClient/Test.pbrpc.m ] || { echo >&2 "protoc outputs wrong filename." @@ -63,6 +64,19 @@ $PROTOC \ exit 1 } +# Verify the "grpc_local_import_directory" option +# Verify system files are imported in a "local" way in header files. +[ "`cat RemoteTestClient/src/objective-c/examples/RemoteTestClient/Test.pbrpc.h | + egrep '#import "'"${RUNTIME_IMPORT_PREFIX}"'/ProtoRPC/.*\.h'`"] || { + echo >&2 "grpc system files should be imported with full paths." +} + +# Verify system files are imported in a "local" way in source files. +[ "`cat RemoteTestClient/src/objective-c/examples/RemoteTestClient/Test.pbrpc.m | + egrep '#import "'"${RUNTIME_IMPORT_PREFIX}"'/ProtoRPC/.*\.h'`"] || { + echo >&2 "grpc system files should be imported with full paths." +} + # Run one extra command to clear $? before exiting the script to prevent # failing even when tests pass. echo "Plugin option tests passed."