bazel icon indicating copy to clipboard operation
bazel copied to clipboard

external repo custom compilation tool chain setup broken after new sandbox implementation

Open NingyuShi opened this issue 10 years ago • 15 comments

I had a separate my_build_tool repo which contains full custom tool chain (for gcc6.1) setup, including build file for compiler, scripts, CROSSTOOL and toolchain target according to the guide. The repo looks like

.
|-- BUILD
|-- compiler
|   |-- BUILD
|   |-- CROSSTOOL
|   |-- gcc61
|   |   |-- BUILD
|   |   |-- gcc61-ar
|   |   |-- gcc61-as
|   |   |-- gcc61-cpp
|   |   |-- gcc61-gcc
|   |   |-- gcc61-gcov
|   |   |-- gcc61-ld
|   |   |-- gcc61-nm
|   |   |-- gcc61-objcopy
|   |   |-- gcc61-objdump
|   |   `-- gcc61-strip
|   `-- gcc61.BUILD
|-- repository_rules
|   |-- BUILD
|   |-- compiler.bzl

In repository_rules/compiler.bzl I defined a function to setup the gcc6.1 compiler suite as an external repo in a macro.

def setup_cc_compiler(repo_name):
    native.new_local_repository(
        name = "gcc_6_1",
        path = "/path/to/gcc-v6.1.0/",
        build_file = repo_name + "//compiler:gcc61.BUILD"
    )

Then I use it in my project A's WORKSPACE like:

local_repository(name = "my_build_tool", path = "/path/to/my_build_repo")
load("@my_build_tool//repository_rules:compiler.bzl", "setup_cc_compiler")
setup_cc_compiler("@my_build_tool")

I compile use

build --ignore_unsupported_sandboxing --crosstool_top=@my_build_tool//compiler:toolchain --verbose_failures

This used to work fine on version 936c2c2c815b64525bc6d3c6ac8f049655589370

However, I recently upgraded to version d82f4a568df7bdc71e5eda53249b05046a4ac43a which contains some major sandboxing change. I hacked the bazel build file to use dummy sandboxing implementation since the new linux sandboxing is not supported on my old RH5 machine. Then I got lots of failure related to sandboxing, after fixing most of them. I found out for a dummy project A, bazel complains about

ERROR: /xxxx/xxx/BUILD:1:1: Linking of rule '//:main_test' failed: process-wrapper failed: error executing command
  (cd /tmp/ningyu/c6786be8e064c55a7e93a312a6096e8b/bazel-sandbox/cd4dcf41-e87e-4a3d-b7a2-8d5fceb79829-1/execroot && \
  exec env - \
  /tmp/ningyu/c6786be8e064c55a7e93a312a6096e8b/execroot/hehe/_bin/process-wrapper -1 5 - - external/bazel_build_tools/compiler/gcc61/gcc61-gcc -o bazel-out/local-fastbuild/bin/main_test -Wl,-no-as-needed -Bexternal/gcc_6_1/bin -B/usr/bin -pass-exit-codes '-Wl,--build-id=md5' '-Wl,--hash-style=gnu' -Wl,-rpath,/grid/common/test/gcc-v6.1.0rh55/lib64 -Lexternal/gcc_6_1/lib64 -Wl,-S -Wl,@bazel-out/local-fastbuild/bin/main_test-2.params).
process-wrapper: execvp("external/my_build_tool/compiler/gcc61/gcc61-gcc", ...): No such file or directory
Target //:main_test failed to build

With sandbox debugging turned on, I inspected the sandbox runtime dir, it looks like

tree /tmp/ningyu/c6786be8e064c55a7e93a312a6096e8b/bazel-sandbox/cd4dcf41-e87e-4a3d-b7a2-8d5fceb79829-1/execroot -L 2
/tmp/ningyu/c6786be8e064c55a7e93a312a6096e8b/bazel-sandbox/cd4dcf41-e87e-4a3d-b7a2-8d5fceb79829-1/execroot
|-- _bin
|   `-- build_interface_so -> /tmp/ningyu/c6786be8e064c55a7e93a312a6096e8b/execroot/hehe/_bin/build_interface_so
|-- bazel-out
|   `-- local-fastbuild
`-- external
    `-- gcc_6_1

Somehow, in the external dir of this jail, only gcc_6_1 external repo got instantiated which contains the compiler (btw, it's a full copy of everything instead of links, which scares me). But all the wrapper scripts and setup which should be in external/my_build_tool is gone. No wonder bazel complains cannot find my wrapper script external/my_build_tool/compiler/gcc61/gcc61-gcc

Seems like with the new sandbox change, bazel is much more strict about which file get included in the runtime jail, how can I get over this?

NingyuShi avatar Sep 01 '16 13:09 NingyuShi

Hi @NingyuShi,

indeed - the old sandbox did not support certain older Linux versions, so it deactivated itself under these circumstances. This is why it probably worked so far for you.

The new sandbox includes a compatibility mode, which creates an individual working directory for each action containing only the listed dependencies (as symlinks pointing to the files in the workspace). This requires no special support from the Linux kernel, so it is enabled by default even on RHEL 5.

I would suggest fixing the dependencies to work (unfortunately I don't know much about toolchains / external repos, maybe @damienmg could help here or point to someone who knows).

If that doesn't work for you, feel free to simply disable the sandboxing completely: bazel build --genrule_strategy=standalone --spawn_strategy=standalone //your:target

This should make it work again!

Cheers, Philipp

philwo avatar Sep 02 '16 13:09 philwo

what does the crosstool file looks like? and the build file?

damienmg avatar Sep 02 '16 13:09 damienmg

@philwo I tried the strategy option, it works very well! Thanks!

Back to the issue, with sandboxing, if every action is using a unique working dir, and need to setup its dependencies, are there lots of overhead/slow down? As I said, I noticed external dependencies are not symlinks to the original files, are they hard link or they are copy? I noticed some slow down during sandboxing mode, not sure it's related.

NingyuShi avatar Sep 02 '16 14:09 NingyuShi

@damienmg My CROSSTOOL looks like

toolchain {
  toolchain_identifier: "local"
  abi_libc_version: "local"
  abi_version: "local"
  builtin_sysroot: ""
  compiler: "compiler"
  compiler_flag: "-Wall"
  compiler_flag: "-Werror"
  compiler_flag: "-pedantic"
  compiler_flag: "-Wl,-z,-relro,-z,now"
  compiler_flag: "-Bexternal/gcc_6_1/bin"
  compiler_flag: "-B/usr/bin"
  compiler_flag: "-Wunused-but-set-parameter"
  compiler_flag: "-Wno-free-nonheap-object"
  compiler_flag: "-fno-omit-frame-pointer"
  # Use old ABI
  compiler_flag: "-D_GLIBCXX_USE_CXX11_ABI=0"
  compiler_flag: "-isystem"
  compiler_flag: "external/gcc_6_1/include/c++/6.1.0"
  compiler_flag: "-isystem"
  compiler_flag: "external/gcc_6_1/include/c++/6.1.0/x86_64-redhat-linux"
  compiler_flag: "-isystem"
  compiler_flag: "external/gcc_6_1/include/c++/6.1.0/backward"
  compiler_flag: "-isystem"
  compiler_flag: "external/gcc_6_1/lib/gcc/x86_64-redhat-linux/6.1.0/include"
  compiler_flag: "-isystem"
  compiler_flag: "external/gcc_6_1/lib/gcc/x86_64-redhat-linux/6.1.0/include-fixed"

  cxx_builtin_include_directory: "%package(gcc_6_1)%//include/c++/6.1.0"
  cxx_builtin_include_directory: "%package(gcc_6_1)%//include/c++/6.1.0/x86_64-redhat-linux"
  cxx_builtin_include_directory: "%package(gcc_6_1)%//include/c++/6.1.0/backward"
  cxx_builtin_include_directory: "%package(gcc_6_1)%//lib/gcc/x86_64-redhat-linux/6.1.0/include"
  cxx_builtin_include_directory: "%package(gcc_6_1)%//lib/gcc/x86_64-redhat-linux/6.1.0/include-fixed"
  #cxx_builtin_include_directory: "/grid/common/test/gcc-v6.1.0rh55/include/c++/6.1.0"
  #cxx_builtin_include_directory: "/grid/common/test/gcc-v6.1.0rh55/include/c++/6.1.0/x86_64-redhat-linux"
  #cxx_builtin_include_directory: "/grid/common/test/gcc-v6.1.0rh55/include/c++/6.1.0/backward"
  #cxx_builtin_include_directory: "/grid/common/test/gcc-v6.1.0rh55/lib/gcc/x86_64-redhat-linux/6.1.0/include"
  #cxx_builtin_include_directory: "/grid/common/test/gcc-v6.1.0rh55/lib/gcc/x86_64-redhat-linux/6.1.0/include-fixed"
  cxx_builtin_include_directory: "/usr/include"
  cxx_flag: "-std=c++14"
  host_system_name: "local"
  linker_flag: "-lm"
  linker_flag: "-Wl,-no-as-needed"
  linker_flag: "-Bexternal/gcc_6_1/bin"
  linker_flag: "-B/usr/bin"
  linker_flag: "-pass-exit-codes"
  linker_flag: "-Wl,--build-id=md5"
  linker_flag: "-Wl,--hash-style=gnu"
  linker_flag: "-Wl,-rpath,/grid/common/test/gcc-v6.1.0rh55/lib64"
  linker_flag: "-Lexternal/gcc_6_1/lib64"
  linker_flag: "-lstdc++"
  #linker_flag: "-static-libstdc++"
  needsPic: true
  objcopy_embed_flag: "-I"
  objcopy_embed_flag: "binary"
  supports_fission: false
  supports_gold_linker: false
  supports_incremental_linker: false
  supports_interface_shared_objects: false
  supports_normalizing_ar: false
  supports_start_end_lib: false
  supports_thin_archives: false
  target_cpu: "k8"
  target_libc: "local"
  target_system_name: "local"
  unfiltered_cxx_flag: "-fno-canonical-system-headers"
  unfiltered_cxx_flag: "-Wno-builtin-macro-redefined"
  unfiltered_cxx_flag: "-D__DATE__=\"redacted\""
  unfiltered_cxx_flag: "-D__TIMESTAMP__=\"redacted\""
  unfiltered_cxx_flag: "-D__TIME__=\"redacted\""
  tool_path {name: "ar" path: "gcc61/gcc61-ar" }
  tool_path {name: "cpp" path: "gcc61/gcc61-cpp" }
  tool_path {name: "dwp" path: "/usr/bin/dwp" }
  tool_path {name: "gcc" path: "gcc61/gcc61-gcc" }
  tool_path {name: "gcov" path: "gcc61/gcc61-gcov" }
  tool_path {name: "ld" path: "gcc61/gcc61-ld" }
  tool_path {name: "nm" path: "gcc61/gcc61-nm" }
  tool_path {name: "objcopy" path: "gcc61/gcc61-objcopy" }
  tool_path {name: "objdump" path: "gcc61/gcc61-objdump" }
  tool_path {name: "strip" path: "gcc61/gcc61-strip" }

  compilation_mode_flags {
    mode: DBG
    compiler_flag: "-ggdb3"
  }
  compilation_mode_flags {
    mode: OPT
    compiler_flag: "-g0"
    compiler_flag: "-O2"
    compiler_flag: "-DNDEBUG"
    compiler_flag: "-ffunction-sections"
    compiler_flag: "-fdata-sections"
    linker_flag: "-Wl,--gc-sections"
  }
  linking_mode_flags { mode: DYNAMIC }
}

compiler/BUILD file

package(default_visibility = ['//visibility:public'])

filegroup(
  name = 'toolchain',
  srcs = [
    ':cc-compiler-k8',
    ':gcc61_all_files',
  ],
)

filegroup(
  name = 'gcc61_all_files',
  srcs = [
    '@gcc_6_1//:compiler_pieces',
    '@gcc_6_1//:compiler_components',
    '//compiler/gcc61:tool-wrappers',
  ],
)

filegroup(
  name = 'gcc61_linker_files',
  srcs = [
    '@gcc_6_1//:compiler_pieces',
    '@gcc_6_1//:compiler_components',
  ],
)

filegroup(
  name = 'gcc61_compiler_files',
  srcs = [
    '@gcc_6_1//:gcc',
    '@gcc_6_1//:ld',
    '@gcc_6_1//:as',
  ],
)

cc_toolchain(
  name = 'cc-compiler-k8',
  all_files = ':gcc61_all_files',
  compiler_files = ':gcc61_compiler_files',
  cpu = 'k8',
  dwp_files = ':empty',
  dynamic_runtime_libs = [':empty'],
  linker_files = ':gcc61_linker_files',
  objcopy_files = '@gcc_6_1//:objcopy',
  static_runtime_libs = [':empty'],
  strip_files = '@gcc_6_1//:strip',
  supports_param_files = 1,
)

compiler/gcc61/BUILD:

package(default_visibility = ['//visibility:public'])

filegroup(
    name = 'gcc',
    srcs = [
        '@gcc_6_1//:gcc',
        'gcc61-gcc',
        ],
)

filegroup(
    name = 'cpp',
    srcs = [
        '@gcc_6_1//:cpp',
        'gcc61-cpp',
        ],
)

filegroup(
    name = 'gcov',
    srcs = [
        '@gcc_6_1//:gcov',
        'gcc61-gcov',
        ],
)

filegroup(
    name = 'nm',
    srcs = [
        '@gcc_6_1//:nm',
        'gcc61-nm',
     ],
)

filegroup(
    name = 'ld',
    srcs = [
        '@gcc_6_1//:ld',
        'gcc61-ld',
    ],
)
filegroup(
    name = 'objcopy',
    srcs = [
        '@gcc_6_1//:objcopy',
        'gcc61-objcopy',
    ],
)

filegroup(
    name = 'objdump',
    srcs = [
        '@gcc_6_1//:objdump',
        'gcc61-objdump',
        ],
)

filegroup(
    name = 'strip',
    srcs = [
        '@gcc_6_1//:strip',
        'gcc61-strip',
        ],
)

filegroup(
    name = 'as',
    srcs = [
        '@gcc_6_1//:as',
        'gcc61-as',
        ],
)

filegroup(
    name = 'ar',
    srcs = [
        '@gcc_6_1//:ar',
        'gcc61-ar',
    ],
)

filegroup(
  name = "tool-wrappers",
  srcs = [
    ':gcc',
    ':cpp',
    ':gcov',
    ':nm',
    ':ld',
    ':objcopy',
    ':objdump',
    ':strip',
    ':as',
    ':ar',
  ],
)

NingyuShi avatar Sep 02 '16 14:09 NingyuShi

@damienmg I have several problems with this setup. One thing is gcc dependency tracking somehow leeks out of external/gcc_6_1, just two entry. Most of them looks like

 external/gcc_6_1/include/c++/6.1.0/bits/stl_iterator.h \
 external/gcc_6_1/include/c++/6.1.0/bits/ptr_traits.h \
 external/gcc_6_1/include/c++/6.1.0/debug/debug.h \
 external/gcc_6_1/include/c++/6.1.0/bits/predefined_ops.h \
 external/gcc_6_1/include/c++/6.1.0/bits/stl_algo.h \
 external/gcc_6_1/include/c++/6.1.0/cstdlib \

But two looks like

 /grid/common/test/gcc-v6.1.0rh55/lib/gcc/x86_64-redhat-linux/6.1.0/../../../../include/c++/6.1.0/stdlib.h \
 /grid/common/test/gcc-v6.1.0rh55/lib/gcc/x86_64-redhat-linux/6.1.0/include/stdint.h \

That breaks the inclusion rule during compilation

this rule is missing dependency declarations for the following files included by 'main.cpp':
  '/grid/common/test/gcc-v6.1.0rh55/lib/gcc/x86_64-redhat-linux/6.1.0/include/stdint.h'
  '/grid/common/test/gcc-v6.1.0rh55/include/c++/6.1.0/stdlib.h'.
Target //:main_test failed to build

If I hardcoded the abs path to CROSSTOOL (the one I commented in previous post), I get over this, then it complains

Linking of rule '//:main_test' failed: process-wrapper failed: error executing command
  (cd /tmp/ningyu/c6786be8e064c55a7e93a312a6096e8b/bazel-sandbox/a47fdc3e-9d83-4c94-a427-86c538544c1b-1/execroot && \
  exec env - \
  /tmp/ningyu/c6786be8e064c55a7e93a312a6096e8b/execroot/hehe/_bin/process-wrapper -1 5 - - external/bazel_build_tools/compiler/gcc61/gcc61-gcc -o bazel-out/local-fastbuild/bin/main_test -Wl,-no-as-needed -Bexternal/gcc_6_1/bin -B/usr/bin -pass-exit-codes '-Wl,--build-id=md5' '-Wl,--hash-style=gnu' -Wl,-rpath,/grid/common/test/gcc-v6.1.0rh55/lib64 -Lexternal/gcc_6_1/lib64 -Wl,-S -Wl,@bazel-out/local-fastbuild/bin/main_test-2.params).
process-wrapper: execvp("external/bazel_build_tools/compiler/gcc61/gcc61-gcc", ...): No such file or directory
Target //:main_test failed to build

Also I want to rpath to link correct libstdc++.so from the compiler suite, but has to do hard coded path now, and don't know how to do it correctly. I think most of the problems are related to how to maintain strictness of sandboxing at the same time allowing flexibility. My goal is to have one external repo maintain all the compiler tool chain dependency and whoever wants to use it just call some macro like setup_compiler() to finish the job. And everything inside the external repo should be self contained.

I try to understand more on how CROSSTOOL and BUILD bring the dependency into the sandboxing jail, could you please help?

Thanks

NingyuShi avatar Sep 02 '16 15:09 NingyuShi

Wait there is no sandboxing right?

The ':gcc61_all_files' srcs in the toolchain filegroup should not be there.

Can you try putting all files in the linker_files of your cc_toolchain rule?

What are "compiler_pieces" and "compiler_components"?

damienmg avatar Sep 07 '16 08:09 damienmg

If I turned on sandboxing, compilation failed. There is sandboxing.

For ":gcc61_all_files", I'm following this tutorial. Can you explain more about why it shouldn't be here?

"compiler_pieces" and "compiler_components" are defined in compiler/gcc61.build

NingyuShi avatar Sep 07 '16 12:09 NingyuShi

Ohoh the wiki is wrong. This should definitely not be there. Though it probably not the reason for the breakage.

Can you try putting all_files in the linker_files of your cc_toolchain rule?

damienmg avatar Sep 07 '16 13:09 damienmg

I just did it, and it magically start working 👍

However, the compilation time becomes much worse, almost 10x slower. Is this caused by the overhead of sandboxing?

NingyuShi avatar Sep 07 '16 13:09 NingyuShi

Redirecting to @philwo for sandboxing performance.

damienmg avatar Sep 07 '16 13:09 damienmg

@damienmg Any plan to fix the wiki? That's the only source I got now, and if that is misleading, lots of people get confused.

NingyuShi avatar Sep 07 '16 13:09 NingyuShi

(the wiki is world-writeable :))

damienmg avatar Sep 07 '16 13:09 damienmg

Actually there is another change that is coming, so letting @lberki fix the wiki.

damienmg avatar Sep 07 '16 13:09 damienmg

@damienmg Once sandboxing is on, it becomes much harder to get a CROSSTOOL conforming to all rules yet working with a non-default compiler. Especially if you want to remove all the hard-coded dirs.

NingyuShi avatar Sep 07 '16 13:09 NingyuShi

@NingyuShi yes it is a known issue and we working on making crosstool writing easier but it will take a long time to get fixed.

The sandboxing performance issue has been followed-up on another thread. The wiki is fixed now so closing that bug.

damienmg avatar Oct 06 '16 12:10 damienmg