From af2120aa55ab2b68f2be95ba7085aa329ea90fb2 Mon Sep 17 00:00:00 2001 From: Paolo Borelli Date: Tue, 9 Feb 2016 15:08:54 +0100 Subject: [PATCH] Turn the compiler into a protoc plugin Using the old "protoc-c" command directly is still supported through a symlink. --- LICENSE | 9 ++++---- Makefile.am | 30 ++++++++++++++++--------- README.md | 4 ++-- build-cmake/CMakeLists.txt | 46 ++++++++++++++++++++++++-------------- protoc-c/main.cc | 25 ++++++++++++--------- 5 files changed, 70 insertions(+), 44 deletions(-) diff --git a/LICENSE b/LICENSE index a756c3a..c1c6ac9 100644 --- a/LICENSE +++ b/LICENSE @@ -25,7 +25,8 @@ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -The code generated by the protoc-c compiler is owned by the owner of the -input files used when generating it. This code is not standalone and -requires a support library to be linked with it. This support library is -covered by the above license. +The code generated by the protoc-gen-c code generator and by the +protoc-c compiler is owned by the owner of the input files used when +generating it. This code is not standalone and requires a support +library to be linked with it. This support library is covered by the +above license. diff --git a/Makefile.am b/Makefile.am index e0a5641..991aa23 100644 --- a/Makefile.am +++ b/Makefile.am @@ -66,13 +66,13 @@ CLEANFILES += protobuf-c/libprotobuf-c.pc EXTRA_DIST += protobuf-c/libprotobuf-c.pc.in # -# protoc-c +# protoc-gen-c # if BUILD_COMPILER -bin_PROGRAMS += protoc-c/protoc-c -protoc_c_protoc_c_SOURCES = \ +bin_PROGRAMS += protoc-c/protoc-gen-c +protoc_c_protoc_gen_c_SOURCES = \ protoc-c/c_bytes_field.cc \ protoc-c/c_bytes_field.h \ protoc-c/c_enum.cc \ @@ -100,13 +100,21 @@ protoc_c_protoc_c_SOURCES = \ protoc-c/c_string_field.cc \ protoc-c/c_string_field.h \ protoc-c/main.cc -protoc_c_protoc_c_CXXFLAGS = \ +protoc_c_protoc_gen_c_CXXFLAGS = \ $(AM_CXXFLAGS) \ $(protobuf_CFLAGS) -protoc_c_protoc_c_LDADD = \ +protoc_c_protoc_gen_c_LDADD = \ $(protobuf_LIBS) \ -lprotoc +# +# protoc-c compat link +# + +install-exec-hook: + rm -f $(DESTDIR)$(bindir)/protoc-c + ln -s protoc-gen-c $(DESTDIR)$(bindir)/protoc-c + # # protobuf-c tests # @@ -155,14 +163,14 @@ t_generated_code2_cxx_generate_packed_data_CXXFLAGS = \ t_generated_code2_cxx_generate_packed_data_LDADD = \ $(protobuf_LIBS) -t/test.pb-c.c t/test.pb-c.h: $(top_builddir)/protoc-c/protoc-c$(EXEEXT) $(top_srcdir)/t/test.proto - $(AM_V_GEN)$(top_builddir)/protoc-c/protoc-c$(EXEEXT) -I$(top_srcdir) --c_out=$(top_builddir) $(top_srcdir)/t/test.proto +t/test.pb-c.c t/test.pb-c.h: $(top_builddir)/protoc-c/protoc-gen-c$(EXEEXT) $(top_srcdir)/t/test.proto + $(AM_V_GEN)@PROTOC@ --plugin=$(top_builddir)/protoc-c/protoc-gen-c$(EXEEXT) -I$(top_srcdir) --c_out=$(top_builddir) $(top_srcdir)/t/test.proto -t/test-optimized.pb-c.c t/test-optimized.pb-c.h: $(top_builddir)/protoc-c/protoc-c$(EXEEXT) $(top_srcdir)/t/test-optimized.proto - $(AM_V_GEN)$(top_builddir)/protoc-c/protoc-c$(EXEEXT) -I$(top_srcdir) --c_out=$(top_builddir) $(top_srcdir)/t/test-optimized.proto +t/test-optimized.pb-c.c t/test-optimized.pb-c.h: $(top_builddir)/protoc-c/protoc-gen-c$(EXEEXT) $(top_srcdir)/t/test-optimized.proto + $(AM_V_GEN)@PROTOC@ --plugin=$(top_builddir)/protoc-c/protoc-gen-c$(EXEEXT) -I$(top_srcdir) --c_out=$(top_builddir) $(top_srcdir)/t/test-optimized.proto -t/test-full.pb-c.c t/test-full.pb-c.h: $(top_builddir)/protoc-c/protoc-c$(EXEEXT) $(top_srcdir)/t/test-full.proto - $(AM_V_GEN)$(top_builddir)/protoc-c/protoc-c$(EXEEXT) -I$(top_srcdir) --c_out=$(top_builddir) $(top_srcdir)/t/test-full.proto +t/test-full.pb-c.c t/test-full.pb-c.h: $(top_builddir)/protoc-c/protoc-gen-c$(EXEEXT) $(top_srcdir)/t/test-full.proto + $(AM_V_GEN)@PROTOC@ --plugin=$(top_builddir)/protoc-c/protoc-gen-c$(EXEEXT) -I$(top_srcdir) --c_out=$(top_builddir) $(top_srcdir)/t/test-full.proto t/test-full.pb.cc t/test-full.pb.h: @PROTOC@ $(top_srcdir)/t/test-full.proto $(AM_V_GEN)@PROTOC@ -I$(top_srcdir) --cpp_out=$(top_builddir) $(top_srcdir)/t/test-full.proto diff --git a/README.md b/README.md index fc9f5f2..f076a7d 100644 --- a/README.md +++ b/README.md @@ -28,9 +28,9 @@ See the [online Doxygen documentation here](http://lib.protobuf-c.io) or [the Wi ## Synopsis -Use the `protoc-c` command to generate `.pb-c.c` and `.pb-c.h` output files from your `.proto` input file. +Use the `protoc` command to generate `.pb-c.c` and `.pb-c.h` output files from your `.proto` input file. The `--c_out` options instructs `protoc` to use the protobuf-c plugin. - protoc-c --c_out=. example.proto + protoc --c_out=. example.proto Include the `.pb-c.h` file from your C source code. diff --git a/build-cmake/CMakeLists.txt b/build-cmake/CMakeLists.txt index 452265c..29cc21f 100644 --- a/build-cmake/CMakeLists.txt +++ b/build-cmake/CMakeLists.txt @@ -39,31 +39,35 @@ INCLUDE_DIRECTORIES(${CMAKE_BINARY_DIR}) # for generated files FIND_PACKAGE(Protobuf REQUIRED) INCLUDE_DIRECTORIES(${PROTOBUF_INCLUDE_DIR}) -file(GLOB PROTOC_SRC ${MAIN_DIR}/protoc-c/*.h ${MAIN_DIR}/protoc-c/*.cc ) -ADD_EXECUTABLE(protoc-c ${PROTOC_SRC}) +FILE(GLOB PROTOC_GEN_C_SRC ${MAIN_DIR}/protoc-c/*.h ${MAIN_DIR}/protoc-c/*.cc ) +ADD_EXECUTABLE(protoc-gen-c ${PROTOC_GEN_C_SRC}) -TARGET_LINK_LIBRARIES(protoc-c ${PROTOBUF_PROTOC_LIBRARY} ${PROTOBUF_LIBRARY}) +TARGET_LINK_LIBRARIES(protoc-gen-c ${PROTOBUF_PROTOC_LIBRARY} ${PROTOBUF_LIBRARY}) + +IF(CMAKE_HOST_UNIX) +ADD_CUSTOM_COMMAND(TARGET ${PROJECT_NAME} POST_BUILD + COMMAND ln -sf protoc-gen-c protoc-c + DEPENDS protoc-gen-c) +ENDIF() IF(CMAKE_BUILD_TYPE MATCHES Debug) ENABLE_TESTING() ADD_CUSTOM_COMMAND(OUTPUT t/test.pb-c.c t/test.pb-c.h - COMMAND protoc-c -I${MAIN_DIR} ${TEST_DIR}/test.proto --c_out=${CMAKE_BINARY_DIR} - DEPENDS protoc-c - ) - + COMMAND ${PROTOBUF_PROTOC_EXECUTABLE} + ARGS --plugin=${CMAKE_CURRENT_BINARY_DIR}/protoc-gen-c -I${MAIN_DIR} ${TEST_DIR}/test.proto --c_out=${CMAKE_BINARY_DIR} + DEPENDS protoc-gen-c) ADD_EXECUTABLE(test-generated-code ${TEST_DIR}/generated-code/test-generated-code.c t/test.pb-c.c t/test.pb-c.h ) TARGET_LINK_LIBRARIES(test-generated-code protobuf-c) - ADD_CUSTOM_COMMAND(OUTPUT t/test-full.pb.cc t/test-full.pb.h COMMAND ${PROTOBUF_PROTOC_EXECUTABLE} - ARGS --cpp_out ${CMAKE_BINARY_DIR} -I${MAIN_DIR} ${TEST_DIR}/test-full.proto) + ARGS --cpp_out ${CMAKE_BINARY_DIR} -I${MAIN_DIR} ${TEST_DIR}/test-full.proto) ADD_CUSTOM_COMMAND(OUTPUT t/test-full.pb-c.c t/test-full.pb-c.h - COMMAND protoc-c -I${MAIN_DIR} ${TEST_DIR}/test-full.proto --c_out=${CMAKE_BINARY_DIR} - DEPENDS protoc-c - ) + COMMAND ${PROTOBUF_PROTOC_EXECUTABLE} + ARGS --plugin=${CMAKE_CURRENT_BINARY_DIR}/protoc-gen-c -I${MAIN_DIR} ${TEST_DIR}/test-full.proto --c_out=${CMAKE_BINARY_DIR} + DEPENDS protoc-gen-c) ADD_EXECUTABLE(cxx-generate-packed-data ${TEST_DIR}/generated-code2/cxx-generate-packed-data.cc t/test-full.pb.h t/test-full.pb.cc) TARGET_LINK_LIBRARIES(cxx-generate-packed-data ${PROTOBUF_LIBRARY}) @@ -74,19 +78,27 @@ ADD_CUSTOM_COMMAND(OUTPUT t/generated-code2/test-full-cxx-output.inc DEPENDS cxx-generate-packed-data ) -ADD_EXECUTABLE(test-generated-code2 ${TEST_DIR}/generated-code2/test-generated-code2.c t/generated-code2/test-full-cxx-output.inc t/test-full.pb-c.h t/test-full.pb-c.c) -TARGET_LINK_LIBRARIES(test-generated-code2 protobuf-c) +ADD_CUSTOM_COMMAND(OUTPUT t/test-optimized.pb-c.c t/test-optimized.pb-c.h + COMMAND ${PROTOBUF_PROTOC_EXECUTABLE} + ARGS --plugin=${CMAKE_CURRENT_BINARY_DIR}/protoc-gen-c -I${MAIN_DIR} ${TEST_DIR}/test-optimized.proto --c_out=${CMAKE_BINARY_DIR} + DEPENDS protoc-gen-c) + +ADD_EXECUTABLE(test-generated-code2 ${TEST_DIR}/generated-code2/test-generated-code2.c t/generated-code2/test-full-cxx-output.inc t/test-full.pb-c.h t/test-full.pb-c.c t/test-optimized.pb-c.h t/test-optimized.pb-c.c) +TARGET_LINK_LIBRARIES(test-generated-code2 protobuf-c) ADD_EXECUTABLE(test-version ${TEST_DIR}/version/version.c) TARGET_LINK_LIBRARIES(test-version protobuf-c) - ENDIF() - -INSTALL(TARGETS protoc-c protobuf-c RUNTIME DESTINATION bin LIBRARY DESTINATION lib ARCHIVE DESTINATION lib) +INSTALL(TARGETS protoc-gen-c protobuf-c RUNTIME DESTINATION bin LIBRARY DESTINATION lib ARCHIVE DESTINATION lib) INSTALL(FILES ${MAIN_DIR}/protobuf-c/protobuf-c.h DESTINATION include/protobuf-c) INSTALL(FILES ${MAIN_DIR}/protobuf-c/protobuf-c.h DESTINATION include) + +IF(CMAKE_HOST_UNIX) +INSTALL(CODE "EXECUTE_PROCESS (COMMAND ln -sf protoc-gen-c protoc-c WORKING_DIRECTORY ${CMAKE_INSTALL_PREFIX}/bin)") +ENDIF() + INCLUDE(Dart) SET(DART_TESTING_TIMEOUT 5) diff --git a/protoc-c/main.cc b/protoc-c/main.cc index 688aa2d..93c42d0 100644 --- a/protoc-c/main.cc +++ b/protoc-c/main.cc @@ -1,17 +1,22 @@ +#include + +#include #include #include - int main(int argc, char* argv[]) { - google::protobuf::compiler::CommandLineInterface cli; - - // Support generation of Foo code. google::protobuf::compiler::c::CGenerator c_generator; - cli.RegisterGenerator("--c_out", &c_generator, - "Generate C/H files."); - // Add version info generated by automake - cli.SetVersionInfo(PACKAGE_STRING); - - return cli.Run(argc, argv); + std::string invocation_name = argv[0]; + std::string invocation_basename = invocation_name.substr(invocation_name.find_last_of("/") + 1); + const std::string legacy_name = "protoc-c"; + + if (invocation_basename == legacy_name) { + google::protobuf::compiler::CommandLineInterface cli; + cli.RegisterGenerator("--c_out", &c_generator, "Generate C/H files."); + cli.SetVersionInfo(PACKAGE_STRING); + return cli.Run(argc, argv); + } + + return google::protobuf::compiler::PluginMain(argc, argv, &c_generator); }