-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rewrite cmake/external/protobuf.cmake and add proto_library to cmake/generic.cmake #2569
Conversation
# protobuf_generate_cpp. This find_package invocation might fail if | ||
# the host doesn't have protobuf install. In that case, it overwrites | ||
# some variables like PROTOBUF_LIBRARIES. Because we don't really use |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the reason I did not modify this code is too hard to do that, because its also include how to build protobuf on arm, it's better if yiqun can review this. we only need to import the generate protobuf libs as name protobuf
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the reminder. I see. I am basically rewriting this protobuf.cmake from scratch, in hope that we can rebuild those functions step-by-step.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
简而言之,protobuf.cmake的功能是:
- 交叉编译:
- 编译host版protobuf,使用protoc
- 编译target版protobuf,使用include和libprotobuf.a
- 普通编译:
- 通过PROTOBUF_ROOT寻找protobuf库,找到则退出
- 没找到:通过find_package寻找3.1版本以上的protobuf库,找到则退出
- 没找到:编译target版protobuf
这个PR修改之前,protobuf.cmake的工作流程是:
- line 29-44,在用户手动配置的
PROTOBUF_ROOT
路径下面寻找protobuf,若找到则调用line 17-23定义的marcoPROMPT_PROTOBUF_LIB
,其中打印出protoc
、libprotobuf.a
的位置信息,以及protobuf
的版本信息,然后退出整个protobuf.cmake。这个改动是由@reyoung 在Add user can define PROTOBUF_ROOT. #2281 和Set protobuf version when PROTOBUF_ROOT set #2297 中引入,原因是一些场景下,并不要求3.1以上的版本。可通过cmake -DPROTOBUF_ROOT=xxx ..
来配置。 - 若用户没有配置
PROTOBUF_ROOT
,或者是在PROTOBUF_ROOT
下没有找到完整的protobuf库,cmake流程会走到line 104,后面的流程分是否交叉编译两种情况,修改在Add cross-compiling toolchain files for Android and Raspberry Pi. #1973 中引入:
if 交叉编译:
调用find_package(Prorobuf ${PROTOBUF_VERSION}),并且要求版本在3.1以上。没找到或者版本过低,都会设置PROTOBUF_FOUND为OFF。对应line 106-113
else:
编译host版本的protobuf即build_protobuf(protobuf_host TRUE),调用line 45-102定义的function build_protobuf。
设置PROTOBUF_PROTOC_EXECUTABLE为host版本的protoc
注意,这个时候没有设置PROTOBUF_FOUND的值。对应代码line 115-119
if PROTOBUF_FOUND没有设置或者为OFF:
编译target版本的protobuf即build_protobuf(protobuf FALSE),仍然调用line 45-102定义的function build_protobuf。
设置PROTOBUF_INCLUDE_DIR为target版本的include
if 不是交叉编译:
设置PROTOBUF_PROTOC_EXECUTABLE为target版本的protoc
设置相关的LIBIRARY路径为target protobuf的lib路径
调用line 17-23定义的macro PROMPT_PROTOBUF_LIB,打印protobuf库的信息
所以,我建议对这个文件的修改是:
- line 123:
build_protobuf(protobuf FALSE)
->build_protobuf(extern_protobuf FALSE)
,并将line 124-134中的protobuf
都替换成extern_protobuf
- 无论哪个分支,都是调用
macro(PROMPT_PROTOBUF_LIB)
总结并退出的,因此建议在line17-23macro(PROMPT_PROTOBUF_LIB)
中添加add_library
内容,还需要个参数控制是否存在extern_protobuf
依赖:
ADD_LIBRARY(protobuf STATIC IMPORTED GLOBAL)
SET_PROPERTY(TARGET protobuf PROPERTY IMPORTED_LOCATION ${PROTOBUF_LIBRARIES})
IF(some_flag)
ADD_DEPENDENCIES(protobuf extern_protobuf)
ADD_LIBRARY(protobuf_lite STATIC IMPORTED GLOBAL)
SET_PROPERTY(TARGET protobuf_lite PROPERTY IMPORTED_LOCATION ${PROTOBUF_LITE_LIBRARIES})
IF(some_flag)
ADD_DEPENDENCIES(protobuf_lite extern_protobuf)
ADD_LIBRARY(protoc STATIC IMPORTED GLOBAL)
SET_PROPERTY(TARGET protoc PROPERTY IMPORTED_LOCATION ${PROTOC_LIBRARIES})
IF(some_flag)
ADD_DEPENDENCIES(protoc extern_protobuf)
这个和
有啥区别呢?为什么前者的结果要叫做 protobuf_host 呢? |
@wangkuiyi 我们在进行交叉编译的时候,比如交叉编译android库的时候,会配置 |
This PR includes too many changes, and there had been a better implementation for the change of making protobuf dependable by cc_library. I (or someone else) will try add proto_libarary to generic.cmake later. |
Fixes #2570
Fixes #2568
Fixes #2567