apollo如何添加第三方库,以pylon为例

1.在apollo/third_party/ 里面增加pylon文件夹

apollo如何添加第三方库,以pylon为例_第1张图片

workspace.bazel

"""Loads the OpenCV library"""

# Sanitize a dependency so that it works correctly from code that includes
# Apollo as a submodule.
def clean_dep(dep):
    return str(Label(dep))

def repo():
    native.new_local_repository(
        name = "pylon",
        build_file = clean_dep("//third_party/pylon:pylon.BUILD"),
        path = "/apollo/third_party/pylon/pylon_6.2",
    )

pylon.BUILD

#TODO(storypku): split opencv into seperate components to speed up build
# e.g., opencv_imgproc/opencv_highgui/...

cc_library(
    name = "pylon",
    srcs = glob(["lib/*.so*"]),
    hdrs = glob(["include/**/*.hpp","include/**/*.h"]),
    includes = ["include"],
    visibility = ["//visibility:public"], 
    linkstatic = 1,
)

BUILD

package(
    default_visibility = ["//visibility:public"],
)

注意!

要在apollo/tools/workspace.bzl里面添加两行内容:

load("//third_party/pylon:workspace.bzl", pylon = "repo")
在def initialize_third_party()函数中增加

pylon()
# Apollo external dependencies that can be loaded in WORKSPACE files.
load("//third_party/pylon:workspace.bzl", pylon = "repo")
load("//third_party/absl:workspace.bzl", absl = "repo")
load("//third_party/adolc:workspace.bzl", adolc = "repo")
load("//third_party/adv_plat:workspace.bzl", adv_plat = "repo")
load("//third_party/ad_rss_lib:workspace.bzl", ad_rss_lib = "repo")
load("//third_party/atlas:workspace.bzl", atlas = "repo")
load("//third_party/benchmark:workspace.bzl", benchmark = "repo")
load("//third_party/boost:workspace.bzl", boost = "repo")
load("//third_party/civetweb:workspace.bzl", civetweb = "repo")
load("//third_party/cpplint:workspace.bzl", cpplint = "repo")
load("//third_party/eigen3:workspace.bzl", eigen = "repo")
load("//third_party/ffmpeg:workspace.bzl", ffmpeg = "repo")
load("//third_party/fftw3:workspace.bzl", fftw3 = "repo")
load("//third_party/fastrtps:workspace.bzl", fastrtps = "repo")
load("//third_party/glog:workspace.bzl", glog = "repo")
load("//third_party/gtest:workspace.bzl", gtest = "repo")
load("//third_party/gflags:workspace.bzl", gflags = "repo")
load("//third_party/ipopt:workspace.bzl", ipopt = "repo")
load("//third_party/local_integ:workspace.bzl", local_integ = "repo")
load("//third_party/libtorch:workspace.bzl", libtorch_cpu = "repo_cpu", libtorch_gpu = "repo_gpu")
load("//third_party/lz4:workspace.bzl", lz4 = "repo")
load("//third_party/ncurses5:workspace.bzl", ncurses5 = "repo")
load("//third_party/nlohmann_json:workspace.bzl", nlohmann_json = "repo")
load("//third_party/npp:workspace.bzl", npp = "repo")
load("//third_party/opencv:workspace.bzl", opencv = "repo")
load("//third_party/opengl:workspace.bzl", opengl = "repo")
load("//third_party/openh264:workspace.bzl", openh264 = "repo")
load("//third_party/osqp:workspace.bzl", osqp = "repo")
load("//third_party/poco:workspace.bzl", poco = "repo")
load("//third_party/portaudio:workspace.bzl", portaudio = "repo")
load("//third_party/proj:workspace.bzl", proj = "repo")
load("//third_party/protobuf:workspace.bzl", protobuf = "repo")
load("//third_party/qt5:workspace.bzl", qt5 = "repo")
load("//third_party/sqlite3:workspace.bzl", sqlite3 = "repo")
load("//third_party/tinyxml2:workspace.bzl", tinyxml2 = "repo")
load("//third_party/uuid:workspace.bzl", uuid = "repo")
load("//third_party/yaml_cpp:workspace.bzl", yaml_cpp = "repo")
# load("//third_party/glew:workspace.bzl", glew = "repo")

load("//third_party/gpus:cuda_configure.bzl", "cuda_configure")
load("//third_party/py:python_configure.bzl", "python_configure")
load("//third_party/tensorrt:tensorrt_configure.bzl", "tensorrt_configure")
load("//third_party/vtk:vtk_configure.bzl", "vtk_configure")
load("//third_party/pcl:pcl_configure.bzl", "pcl_configure")

def initialize_third_party():
    """ Load third party repositories.  See above load() statements. """
    pylon()
    absl()
    adolc()
    adv_plat()
    ad_rss_lib()
    atlas()
    benchmark()
    boost()
    cpplint()
    civetweb()
    eigen()
    fastrtps()
    ffmpeg()
    fftw3()
    gflags()
    glog()
    gtest()
    ipopt()
    local_integ()
    libtorch_cpu()
    libtorch_gpu()
    lz4()
    ncurses5()
    nlohmann_json()
    npp()
    opencv()
    opengl()
    openh264()
    osqp()
    poco()
    portaudio()
    proj()
    protobuf()
    qt5()
    sqlite3()
    tinyxml2()
    uuid()
    yaml_cpp()

# Define all external repositories required by
def apollo_repositories():
    cuda_configure(name = "local_config_cuda")
    tensorrt_configure(name = "local_config_tensorrt")
    python_configure(name = "local_config_python")
    vtk_configure(name = "local_config_vtk")
    pcl_configure(name = "local_config_pcl")

    initialize_third_party()

在终端输入此行命令检查该库是否增加成功:

ls $(bazel info output_base)/external

可以看到@pylon.marker 和 pylon,说明第三方库已经被增加到bazel里面了。

apollo如何添加第三方库,以pylon为例_第2张图片

 期间,注意pylon的路径问题,路径出错可是在构建过程中并没有报错,导致一直不成功也没发现原因,后来单独编译此包时发现路径错误,添加正确路径后即可添加成功。

./apollo.sh build_opt
./apollo.sh clean 
Usage:
  /apollo/scripts/apollo_clean.sh [options]
Options:
    --bazel     Remove bazel output
    --core      Remove coredump files
    --log       Remove log files
    -a, --all   Equivalent to "--bazel --core --log"
    --expunge   Run "bazel clean --expunge"
    -h, --help  Show this message and exit

可以通过./apollo.sh clean --expunge 去掉bazel第三方库依赖,
再 ./apollo.sh build_opt 可以重新构建bazel第三方库依赖。

或许有更简单的方法,欢迎分享。

参考自 https://github.com/ApolloAuto/apollo/blob/master/docs/howto/how_to_add_an_external_dependency.md

你可能感兴趣的:(apollo,apollo,bazel,third_party)