12/01/2016

Bazel Memo

  • If some weird errors happen, such as some files exist, but bazel complains No file or directory, take a look a the .bazelrc file. See if you're using --spawn_strategy=standalone --genrule_strategy=standalone
  • If you changed a third party package in the WORKSPACE file and bazel build fails, it can likely be fixed by running bazel clean --expunge
  • Don't use copts = ["-Ithirdparty/cc"] to solve "cannot find xxx.h file" errors. Instead, use includes = ["../", "include/../", etc...], to solve the include errors. Reason? See the reason bellow.
    • If you depend on a third party library with copts = [-Ithird_party/cc], all your libraries or binaries that depend on this library must have copts.
  • copts can use Make variable substitutions. For example copts = [-I$(GENFILES)] will add bazel-out/local-fastbuild/genfiles to the include path. For other Make variables: https://bazel.build/versions/master/docs/be/make-variables.html#predefined_variables.genrule.cmd
  • If bazel project A depends on project B (both have WORKSPACE files), only the root project (A)'s WORKSPACE file is used. Project B WORKSPACE won't have affect if the bazel build starts from project A.
  • Use bazel build -s to print out detailed logs
  • genrule meanings:
    •  outs = ["omap_ground_detector_config.cc"], a list of files to be generated
    • tools = [":generate_ground_detector_config"], the binary.
    • cmd = "$(location generate_ground_detector_config) ", the command to run. Flag means: $@, space separated string of outs
  • Options can be put in the ~/.bazelrc file:  build --jobs=4 --local_resources=6144,6,1.0 --experimental_local_memory_estimate=true --worker_max_instances=1 --spawn_strategy=standalone --genrule_strategy=standalone 
  • For linking errors, such as undefined reference to 'cublasCreate_v2', need to know which library it tries to link to, then use linkopts = ["-L/path/to/lib", "-Wl,-rpath,/path/to/lib"] to add the lib search paths. Also in the copts and linkopts, you can use the external folder directly: e.g. "-Wl,-rpath,external/drive_t186ref_linux_aarch64/lib/aarch64-linux-gnu". But this doesn't work with the %package%: -L%package(@cuda_full_aarch64//lib)%
  • What is RPATH and $ORIGIN: RPATH stands for run-time search path. According to Wikipedia, “rpath designates the run-time search path hard-coded in an executable file or library. $ORIGIN is a special variable that indicate actual executable filename. It is resolved to where the executable is at run-time, and can be quite useful when setting RPATH. https://nehckl0.medium.com/creating-relocatable-linux-executables-by-setting-rpath-with-origin-45de573a2e98
  • Build a self-contained python executable: bazel build --build_python_zip
  • Example of a genrule:
    genrule(
        name = "gcc_sh_files",
        srcs = ["wrapper_scripts/wrapper.sh"],
        outs = [
            "gcc_linaro_aarch64_linux_gnu/ar.sh",
            "gcc_linaro_aarch64_linux_gnu/cpp.sh",
            "gcc_linaro_aarch64_linux_gnu/gcc.sh",
            "gcc_linaro_aarch64_linux_gnu/gcov.sh",
            "gcc_linaro_aarch64_linux_gnu/ld.sh",
            "gcc_linaro_aarch64_linux_gnu/nm.sh",
            "gcc_linaro_aarch64_linux_gnu/objdump.sh",
            "gcc_linaro_aarch64_linux_gnu/strip.sh",
        ],
        cmd = ("for i in $(OUTS); do " +
               "/bin/cp $(location wrapper_scripts/wrapper.sh) $$i; done")
    )

                                  No comments: