From 1da566803e943f763fd7cad5ecab369264eb57a3 Mon Sep 17 00:00:00 2001 From: storypku Date: Sun, 21 Jun 2020 00:56:45 +0800 Subject: [PATCH] Build: local_config_vtk and no longer needed WORKSPACE.in --- .gitignore | 1 - WORKSPACE.in => WORKSPACE | 12 +- apollo.sh | 5 - external/vtk.BUILD | 15 -- .../lidar/lib/segmentation/ncut/BUILD | 2 +- .../tool/benchmark/lidar/util/BUILD | 2 +- third_party/vtk/BUILD | 0 third_party/vtk/BUILD.tpl | 21 +++ third_party/vtk_configure.bzl | 152 ++++++++++++++++++ what_works.sh | 2 - 10 files changed, 178 insertions(+), 34 deletions(-) rename WORKSPACE.in => WORKSPACE (97%) delete mode 100644 external/vtk.BUILD create mode 100644 third_party/vtk/BUILD create mode 100644 third_party/vtk/BUILD.tpl create mode 100644 third_party/vtk_configure.bzl diff --git a/.gitignore b/.gitignore index 04b893b161..91749de9a6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,5 @@ # Bazel bazel-* -WORKSPACE # Temporary files *.pyc diff --git a/WORKSPACE.in b/WORKSPACE similarity index 97% rename from WORKSPACE.in rename to WORKSPACE index 0b07e40e35..7fe5bc40c6 100644 --- a/WORKSPACE.in +++ b/WORKSPACE @@ -4,11 +4,14 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe") load("//tools/gpus:cuda_configure.bzl", "cuda_configure") load("//tools/tensorrt:tensorrt_configure.bzl", "tensorrt_configure") +load("//third_party:vtk_configure.bzl", "vtk_configure") cuda_configure(name = "local_config_cuda") tensorrt_configure(name = "local_config_tensorrt") +vtk_configure(name = "local_config_vtk") + maybe( http_archive, name = "bazel_skylib", @@ -192,20 +195,11 @@ new_local_repository( # build_file = "third_party/opengl.BUILD", # path = "/usr/include", #) -# #new_local_repository( # name = "glfw", # build_file = "third_party/glfw.BUILD", # path = "/usr/include", #) -# -# FIXME(all): hide vtk version from end users -new_local_repository( - name = "vtk", - build_file = "external/vtk.BUILD", - #path = "/opt/apollo/sysroot/include/vtk-VTK_VERSION", - path = "/opt/apollo/sysroot/include/vtk-8.2", -) # Caffe new_local_repository( diff --git a/apollo.sh b/apollo.sh index 60899b9bc5..328519ab9f 100755 --- a/apollo.sh +++ b/apollo.sh @@ -59,11 +59,6 @@ function check_machine_arch() { fail "Machine architecture $MACHINE_ARCH currently not supported yet." exit 1 fi - - #TODO(ALL): checks whether still in use - #setup vtk folder name for different systems. - VTK_VERSION=$(find /usr/include/ -type d -name "vtk-*" | tail -n1 | cut -d '-' -f 2) - sed "s/VTK_VERSION/${VTK_VERSION}/g" WORKSPACE.in > WORKSPACE } function check_esd_files() { diff --git a/external/vtk.BUILD b/external/vtk.BUILD deleted file mode 100644 index 57be6421b5..0000000000 --- a/external/vtk.BUILD +++ /dev/null @@ -1,15 +0,0 @@ -load("@rules_cc//cc:defs.bzl", "cc_library") - -licenses(["notice"]) - -package(default_visibility = ["//visibility:public"]) - -# FIXME(all): hide vtk version from end users -cc_library( - name = "vtk", - includes = ["."], - linkopts = [ - "-L/opt/apollo/sysroot/lib", - "-lvtkCommonCore-8.2", - ], -) diff --git a/modules/perception/lidar/lib/segmentation/ncut/BUILD b/modules/perception/lidar/lib/segmentation/ncut/BUILD index cf95023719..c2276570f1 100644 --- a/modules/perception/lidar/lib/segmentation/ncut/BUILD +++ b/modules/perception/lidar/lib/segmentation/ncut/BUILD @@ -39,8 +39,8 @@ cc_library( "//modules/perception/lidar/lib/segmentation/ncut/common:flood_fill", "//modules/perception/lidar/lib/segmentation/ncut/common:lr_classifier", "//modules/perception/lidar/lib/segmentation/ncut/proto:ncut_param_cc_proto", + "@local_config_vtk//:vtk", "@pcl", - "@vtk", ], ) diff --git a/modules/perception/tool/benchmark/lidar/util/BUILD b/modules/perception/tool/benchmark/lidar/util/BUILD index c4b0e7661d..c861301784 100644 --- a/modules/perception/tool/benchmark/lidar/util/BUILD +++ b/modules/perception/tool/benchmark/lidar/util/BUILD @@ -30,8 +30,8 @@ cc_library( "//modules/common/util", "@com_google_absl//absl/strings", "@eigen", + "@local_config_vtk//:vtk", "@pcl", - "@vtk", ], ) diff --git a/third_party/vtk/BUILD b/third_party/vtk/BUILD new file mode 100644 index 0000000000..e69de29bb2 diff --git a/third_party/vtk/BUILD.tpl b/third_party/vtk/BUILD.tpl new file mode 100644 index 0000000000..cc8a2968fa --- /dev/null +++ b/third_party/vtk/BUILD.tpl @@ -0,0 +1,21 @@ +package(default_visibility = ["//visibility:public"]) + +cc_library( + name = "vtk_headers", + hdrs = [ + ":vtk_include" + ], + # include_prefix = "third_party/vtk", + strip_include_prefix = "vtk/include", +) + +cc_library( + name = "vtk", + srcs = [":vtk_lib"], + linkstatic = 1, + deps = [ + ":vtk_headers", + ], +) + +%{copy_rules} diff --git a/third_party/vtk_configure.bzl b/third_party/vtk_configure.bzl new file mode 100644 index 0000000000..014e64229f --- /dev/null +++ b/third_party/vtk_configure.bzl @@ -0,0 +1,152 @@ +load("//tools/platform:common.bzl", "execute", "make_copy_dir_rule", "make_copy_files_rule") + +_APOLLO_SYSROOT_DIR = "APOLLO_SYSROOT_DIR" + +_APOLLO_VTK_LIBS = [ + "vtkCommonDataModel", + "vtkCommonCore", + "vtkCommonMath", + "vtkCommonSystem", + "vtkCommonMisc", + "vtkCommonTransforms", + "vtksys", +] + +def _vtk_solib_name(basename, version = None): + """Constructs Linux-specific name of vtk libraries""" + version = "" if not version else "-" + version + return "lib{}{}.so".format(basename, version) + +def find_sysroot_dir(repository_ctx): + if _APOLLO_SYSROOT_DIR in repository_ctx.os.environ: + return repository_ctx.os.environ[_APOLLO_SYSROOT_DIR].strip() + return None + # fail("Environment variable APOLLO_SYSROOT_DIR was not specified." + + # "Re-run ./apollo6.sh configure") + +# Ref: bazel-skylib@lib/paths.bzl +def _basename(p): + """Returns the basename (i.e., the file portion) of a path. + Note that if `p` ends with a slash, this function returns an empty string. + This matches the behavior of Python's `os.path.basename`, but differs from + the Unix `basename` command (which would return the path segment preceding + the final slash). + Args: + p: The path whose basename should be returned. + Returns: + The basename of the path, which includes the extension. + """ + return p.rpartition("/")[-1] + +def _dirname(p): + """Returns the dirname of a path. + The dirname is the portion of `p` up to but not including the file portion + (i.e., the basename). Any slashes immediately preceding the basename are not + included, unless omitting them would make the dirname empty. + Args: + p: The path whose dirname should be returned. + Returns: + The dirname of the path. + """ + prefix, sep, _ = p.rpartition("/") + if not prefix: + return sep + else: + # If there are multiple consecutive slashes, strip them all out as Python's + # os.path.dirname does. + return prefix.rstrip("/") + +def _vtk_version_from_incl_path(incl_path): + return _basename(incl_path).strip("vtk-") + +def _create_local_vtk_repository(repository_ctx): + sysroot_dir = find_sysroot_dir(repository_ctx) + result = _vtk_match_version(repository_ctx, sysroot_dir) + if result == None: + fail("Oops, Package vtk not found.") + (version, incl_dir, lib_path) = result + + # Copy the library and header files. + libraries = [_vtk_solib_name(lib, version) for lib in _APOLLO_VTK_LIBS] + + _lib_path = lib_path + "/" + + # headers = _get_vtk_headers(incl_dir) + _incl_dir = incl_dir + "/" + copy_rules = [ + make_copy_files_rule( + repository_ctx, + name = "vtk_lib", + srcs = [_lib_path + lib for lib in libraries], + outs = ["vtk/lib/" + lib for lib in libraries], + ), + make_copy_dir_rule( + repository_ctx, + name = "vtk_include", + src_dir = incl_dir, + out_dir = "vtk/include", + ), + ] + + # Set up BUILD file. + build_tpl = repository_ctx.path(Label("//third_party/vtk:BUILD.tpl")) + repository_ctx.template("BUILD", build_tpl, { + "%{copy_rules}": "\n".join(copy_rules), + }) + +def _vtk_match_version(repository_ctx, sysroot_dir = None): + cmd = """ldconfig -p | awk -F'=>' '/libvtkCommonCore-.*.so$/ {print $2}'""" + lib_result = execute( + repository_ctx, + ["sh", "-c", cmd], + empty_stdout_fine = False, + ).stdout.strip() + + libdict = {} + for solib in lib_result.split("\n"): + libpath = _dirname(solib) + version = _basename(solib).rstrip(".so").split("-")[-1] + prefix = solib[:solib.find("/lib/")] + libdict[solib] = (libpath, version, prefix) + + prefix_dirs = ["/usr", "/usr/local"] + if sysroot_dir: + prefix_dirs.append(sysroot_dir) + + for prefix in reversed(prefix_dirs): + cmd = """ls -d {}/include/vtk-* 2>/dev/null""".format(prefix) + incl_dir = execute( + repository_ctx, + ["sh", "-c", cmd], + empty_stdout_fine = True, + ).stdout.strip() + if not incl_dir: + continue + + version = _vtk_version_from_incl_path(incl_dir) + for k in libdict: + (lib_path, lib_version, lib_prefix) = libdict[k] + if lib_version == version and lib_prefix == prefix: + return (version, incl_dir, lib_path) + + return None + +def _vtk_configure_impl(repository_ctx): + # Room for _create_remote_vtk_repository + _create_local_vtk_repository(repository_ctx) + +vtk_configure = repository_rule( + implementation = _vtk_configure_impl, + environ = [], +) + +"""Detects and configures the local vtk library. +Add the following to your WORKSPACE FILE: + +```python +vtk_configure(name = "local_config_vtk") +``` + +Args: + name: A unique name for this workspace rule. +""" diff --git a/what_works.sh b/what_works.sh index a617b3e152..80e526b381 100755 --- a/what_works.sh +++ b/what_works.sh @@ -34,8 +34,6 @@ fi echo "/opt/apollo/pkgs/caffe/lib" | sudo tee -a /etc/ld.so.conf.d/apollo.conf sudo ldconfig -cp WORKSPACE.in WORKSPACE - # Fail on first failure. set -e -- GitLab