Turn the compiler into a protoc plugin

Using the old "protoc-c" command directly is still supported
through a symlink.
This commit is contained in:
Paolo Borelli 2016-02-09 15:08:54 +01:00
parent 006d69bd84
commit af2120aa55
5 changed files with 70 additions and 44 deletions

View File

@ -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.

View File

@ -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

View File

@ -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.

View File

@ -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)

View File

@ -1,17 +1,22 @@
#include <string>
#include <google/protobuf/compiler/plugin.h>
#include <google/protobuf/compiler/command_line_interface.h>
#include <protoc-c/c_generator.h>
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);
}