12

寻找用于构建使用协议缓冲区的项目的 autoconf 和 automake 规则的一个很好的示例,将 protoc 添加到构建过程的最佳方式?

4

2 回答 2

17

configure.ac

就 protobuf 库而言,它使用pkg-config,因此最好使用PKG_CHECK_MODULES宏来引用它:

PKG_CHECK_MODULES(PROTOBUF, protobuf >= 2.4.0)
AC_SUBST(PROTOBUF_LIBS)
AC_SUBST(PROTOBUF_CFLAGS)
AC_SUBST(PROTOBUF_VERSION)

并检查protoc路径中的命令。这是一个非常基本的检查它是否在路径中:

AC_CHECK_PROG([PROTOC], [protoc], [protoc])
AS_IF([test "x${PROTOC}" == "x"],
    [AC_MSG_ERROR([ProtoBuf compiler "protoc" not found.])])

protoc或者,允许用户使用--with-protoc=/path/to/protoc或使用环境变量指定不同的PROTOC

# ProtoBuf compiler.
# First, specify with --with-protoc=/path/of/protoc.
# Or, specify with env variable PROTOC.
# If neither of the above, find it in the path.
#AC_MSG_CHECKING([for ProtoBuf compiler protoc])
AC_ARG_WITH([protoc],
    [AS_HELP_STRING([--with-protoc=/path/of/protoc],
        [Location of the protocol buffers compiler protoc. Defaults to looking on path.])],
    [PROTOC="$withval"],
    [ AS_IF([test "x${PROTOC}" == "x"],
        [AC_PATH_PROG([PROTOC], [protoc], [no])])
    ]
)
#AC_MSG_RESULT([${PROTOC}])
AS_IF([test "${PROTOC}" == "no"], [AC_MSG_ERROR([ProtoBuf compiler "protoc" not found.])])

Makefile.am

添加规则以构建proto文件:

%.pb.cc %.pb.h: %.proto
    $(PROTOC) --proto_path=$(srcdir) --cpp_out=$(builddir) $^

用 . 指定 protobuf 源文件dist_noinst_DATA。这对于确保将它们捆绑在.tar.gz使用make dist.

dist_noinst_DATA = whatever.proto

(注意:对于较新版本的 autoconf/automake,可能需要使用@builddir@而不是$(builddir).)

nodist_使用前缀和$(builddir)路径指定生成的文件:

nodist_myprog_SOURCES = $(builddir)/whatever.pb.cc $(builddir)/whatever.pb.h

并用以下方法清洁它们make clean

MOSTLYCLEANFILES = whatever.pb.cc whatever.pb.h

用于BUILT_SOURCES处理构建的头文件的依赖关系:

BUILT_SOURCES = whatever.pb.h

您的编译器标志可能需要参考构建目录来查找头文件(在 VPATH 构建中工作):

AM_CPPFLAGS += -I$(builddir)
于 2013-08-20T07:09:25.983 回答
7

这似乎有效:

配置.ac:

AC_ARG_WITH([protobuf-libdir],
    [AS_HELP_STRING([--with-protobuf-libdir=LIB_DIR],
        [location of the protocol buffers libraries, defaults to /usr/lib])],
    [PROTOBUF_LIBDIR="$withval"],
    [PROTOBUF_LIBDIR='/usr/lib'])
AC_SUBST([PROTOBUF_LIBDIR])

LDFLAGS="$LDFLAGS -L$PROTOBUF_LIBDIR"

AC_CHECK_LIB([protobuf], [main], [], [AC_MSG_ERROR([cannot find protobuf library])])

AC_ARG_WITH([protoc],
    [AS_HELP_STRING([--with-protoc=PATH],
        [location of the protoc protocol buffer compiler binary, defaults to protoc])],
    [PROTOC="$withval"],
    [PROTOC='protoc'])
AC_SUBST([PROTOC])

生成文件.am:

%.pb.cc %.pb.h: %.proto
    $(PROTOC) --proto_path=$(dir $^) --cpp_out=$(dir $^) $^

然后将 .pb.cc 文件添加到 SOURCES。

于 2012-12-19T00:31:59.993 回答