从tensorflow_cc和tensorflow_framework生成静态库
据我了解,使用bazel只能产生libtensorflow_cc.so
和libtensorflow_framework.so
.
我需要生成独立于位置(-fPIC
)的静态库,因为稍后将它们链接到我自己的动态库中.
As far as I understand using bazel I can only produce libtensorflow_cc.so
and libtensorflow_framework.so
.
I need to produce static libs that are position independent (-fPIC
) because I'll link them to a dynamic lib of my own later.
我找到了此答案,它建议使用项目中包含的Makefile.
我成功地用它代替了libtensorflow_cc.so
,但是我该怎么做来代替libtensorflow_framework.so
?
I found this answer which suggest the use of a Makefile included in the project.
I successfully used it to replace the libtensorflow_cc.so
but what can I do to replace libtensorflow_framework.so
?
不是实际答案,但评论太长.
Not an actual answer, but too long for a comment.
我设法在Windows上使用Bazel做类似您提到的事情.特别是,我想制作一个带有一个或两个标头(在功能上受限制)的包装器DLL,这样我就可以轻松移动.我将总结自己所做的事情;它相当复杂,可以满足我们的需求,但是也许您会发现有用的东西.
I managed to do something like what you mention using Bazel on Windows. In particular, I wanted to make a single wrapper DLL with one or two headers (limited in functionality) that I could move around easily. I'll write a summary of the things that I did; it's rather convoluted an customized for our needs, but maybe you find something useful.
- 我将
--config=monolithic
传递给bazel build
命令(除了您需要的任何其他选项).这样可以避免对库进行模块化,从而消除对libtensorflow_framework.so
的依赖(请参阅 工具/bazel.rc ). - 我建立的目标不是TensorFlow信息库中的任何目标.相反,我添加了一个非常小的程序,该程序将包装器用作新的Bazel目标(一个C ++文件以及我的标头标头和
BUILD
文件).因此,必须预先编译所有TensorFlow才能编译此最终的虚拟程序. - 完成这些工作后,我利用了Bazel已经将每个子目标编译为静态库这一事实.我检查为我的虚拟程序目标生成的
bazel-bin
目录下的文件,文件名称的结尾为.params
-在这里找到用于编译该文件的所有静态库的路径. - 我将所有这些中间静态库复制到其他地方.另外,我复制了一堆标头,我将需要编译我的最终包装器(TensorFlow自己的包装器,现在也包括Eigen,Protobuf和Nsync).我把所有这些都放在我之前准备的构建区域中.
- 我使用NMake Makefile通过静态库,复制的标头和我自己的薄包装器来生成我的自定义DLL.
- I pass
--config=monolithic
to thebazel build
command (besides any other option that you need). That will avoid modularizing the library and thus remove the dependency to alibtensorflow_framework.so
(see tools/bazel.rc). - The goal that I build is not any of the ones in the TensorFlow repository. Instead, I add a very small program that uses my wrapper as a new Bazel target (a C++ file plus my headers headers and a
BUILD
file). So all of TensorFlow had to be compiled beforehand in order to compile this final dummy program. - When I get that done, I take advantage of the fact that Bazel does already compile every subgoal as a static library. I check a file under the
bazel-bin
directory generated for my dummy program goal with a name ending.params
- there I find the path of all the static libraries that were used to compile it. - I copy all of these intermediate static libraries to somewhere else. Also, I copy a bunch of headers I will need to compile my final wrapper (TensorFlow own's, but also Eigen, Protobuf and Nsync now too). I put all of this in a build area I have prepared before.
- I use NMake Makefile to produce my custom DLL, using the static libraries, the copied headers and my own thin wrapper.
就是这样,我想.我在MSYS2上运行一个丑陋的Bash脚本,可以为我做所有事情.通常,对于每个新发行版,我都需要调整一两个内容(configure
脚本中的某些选项,我需要复制一些其他头文件,等等),但最终还是可以使它起作用.但是,这很麻烦,所以我不一定要说您应该使用相同的方法(但是,如果需要,可以随时询问有关任何步骤的详细信息).
And that's about it, I think. I have an ugly Bash script I run on MSYS2 that does everything for me. Usually with every new release I need to tweak one or two things (some option in the configure
script, some additional headers I need to copy, etc.), but I do get it to work in the end. It's quite a lot of fiddling though, so I'm not necessarily saying you should use the same approach (but feel free to ask for details about any step if you want).