0

我正在交叉编译 bazel 以用于 TensorFlow。

覆盆子 pi3 的 Bazel

Bazel-0.13 主机:Ubuntu 16.04 LTS x86-64 目标:Raspbery PI3 工具链:本地安装的 rpi3 工具链

为 HOST 构建 bazel。

$(BAZEL_HOME_TOP) $ ./compile.sh

构建成功

$(BAZEL_HOME_TOP) $ sudo cp output/bazel /usr/local/bin/

为交叉编译构建 bazel。

1-> 以下 bazel wiki 页面。

使用自定义工具链构建

后续步骤:

1.a :

   $(BAZEL_HOME_TOP) $ mkdir compilers   // As no directory available  created manually
   $(BAZEL_HOME_TOP) $ touch linaro_linux_gcc_4.8.BUILD // created and added as it mentioned in below link BUILD File.

为新存储库编写构建文件

在 $(BAZEL_HOME_TOP)/WORKSPACE 中添加了上面的片段部分

# For raspberry pi 3 toolchain
new_local_repository(
    name = 'linaroLinuxGcc48Repo',
    build_file = 'compilers/linaro_linux_gcc_4.8.BUILD',
    path = '$(RPI3_TOOLCHAIN_TOP)/toolchain/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64',
 )

Writing the wrapper scripts:

$ cd tools
$ mkdir arm_compiler
$ cd arm_compiler
$ mkdir linaro_linux_gcc
$ cd linaro_linux_gcc
$ cat arm-linux-gnueabihf-gcc
#!/bin/bash --norc

PATH="linaroLinuxGcc48Repo/libexec/gcc/arm-linux-gnueabihf/4.8.3:$PATH" \
exec \
linaroLinuxGcc48Repo/bin/arm-linux-gnueabihf-gcc \
"$@"

$ cat arm-linux-gnueabihf-nm 
#!/bin/bash --norc

exec -a arm-linux-gnueabihf-nm \
linaroLinuxGcc48Repo/bin/arm-linux-gnueabihf-nm \
"$@"

ar、as、cpp、gcov、ld、objcopy、objdump 和 strip 都有与 nm 非常相似的包装脚本。

对于上述包装脚本,我创建了 BUILD 文件。

<...code snippet>
package(default_visibility = ["//tools/arm_compiler:__pkg__"])

filegroup(
    name = "srcs",
    srcs = glob(["**"]),
    visibility = ["$(BAZEL_HOME_TOP)/tools/arm_compier/linaro_linux_gcc:__pkg__"],
)

filegroup(
     name = "gcc",
     srcs = [
         "arm-linux-gnueabihf-gcc",
         "@linaroLinuxGcc48Repo//:gcc",
     ],
)

filegroup(
    name = "ar",
    srcs = [
        "arm-linux-gnueabihf-ar",
        "@linaroLinuxGcc48Repo//:ar",
    ],
)
<...code snippet>

cd ..
$(BAZEL_HOME_TOP)/tools/arm_compiler $ cat CROSSTOOL
major_version: "local"
minor_version: ""
default_target_cpu: "rpi3-aarch64"

default_toolchain {
    cpu: "rpi3-aarch64"
    toolchain_identifier: "gcc_rpi3_linux_aarch64"
}

default_toolchain {
    cpu: "armeabi-v7a"
    toolchain_identifier: "clang_linux_armhf"
}

default_toolchain {
    cpu: "k8"
    toolchain_identifier: "local"
}

toolchain {
    abi_version: "aarch64"
    abi_libc_version: "glibc_2.19"
    builtin_sysroot: ""
    compiler: "gcc"
    host_system_name: "aarch64"
    needsPic: true
    supports_gold_linker: false
    supports_incremental_linker: false
    supports_fission: false
    supports_interface_shared_objects: false
    supports_normalizing_ar: true
    supports_start_end_lib: false
    supports_thin_archives: true
    target_libc: "glibc_2.19"
    target_cpu: "rpi3-aarch64"
    target_system_name: "arm64-v8a"
    toolchain_identifier: "gcc_rpi3_linux_aarch64"

    tool_path { name: "ar" path: "linaro_linux_gcc/arm-linux-gnueabihf-ar" }
    tool_path { name: "compat-ld" path: "linaro_linux_gcc/arm-linux-gnueabihf-ld" }
    tool_path { name: "cpp" path: "linaro_linux_gcc/arm-linux-gnueabihf-gcc" }
    tool_path { name: "dwp" path: "linaro_linux_gcc/arm-linux-gnueabihf-dwp" }
    tool_path { name: "gcc" path: "linaro_linux_gcc/arm-linux-gnueabihf-gcc" }
    tool_path { name: "gcov" path: "arm-frc-linux-gnueabi/arm-frc-linux-gnueabi-gcov-4.9" }
    # C(++) compiles invoke the compiler (as that is the one knowing where
    # to find libraries), but we provide LD so other rules can invoke the linker.
    tool_path { name: "ld" path: "linaro_linux_gcc/arm-linux-gnueabihf-ld" }
    tool_path { name: "nm" path: "linaro_linux_gcc/arm-linux-gnueabihf-nm" }
    tool_path { name: "objcopy" path: "linaro_linux_gcc/arm-linux-gnueabihf-objcopy" }
    objcopy_embed_flag: "-I"
    objcopy_embed_flag: "binary"
    tool_path { name: "objdump" path: "linaro_linux_gcc/arm-linux-gnueabihf-objdump" }
    tool_path { name: "strip" path: "linaro_linux_gcc/arm-linux-gnueabihf-strip" }

    compiler_flag: "--sysroot=linaroLinuxGcc48Repo/arm-linux-gnueabihf/libc"
    compiler_flag: "-mfloat-abi=hard"
    compiler_flag: "-march=armv8-a"
    compiler_flag: "-nostdinc"
    compiler_flag: "-isystem"
    compiler_flag: "linaroLinuxGcc48Repo/lib/gcc/arm-linux-gnueabihf/4.8.3/include"
    compiler_flag: "-isystem"
    compiler_flag: "linaroLinuxGcc48Repo/arm-linux-gnueabihf/libc/usr/include"
    compiler_flag: "-isystem"
    compiler_flag: "linaroLinuxGcc48Repo/lib/gcc/arm-linux-gnueabihf/4.8.3/include-fixed"
    cxx_flag: "-isystem"
    cxx_flag: "linaroLinuxGcc48Repo/arm-linux-gnueabihf/include/c++/4.8.3/arm-linux-gnueabihf"
    cxx_flag: "-isystem"
    cxx_flag: "linaroLinuxGcc48Repo/arm-linux-gnueabihf/include/c++/4.8.3"

    cxx_builtin_include_directory: "%package(@linaroLinuxGcc48Repo//)%/arm-linux-gnueabihf/libc/usr/include"
    cxx_builtin_include_directory: "%package(@linaroLinuxGcc48Repo//)%/lib/gcc/arm-linux-gnueabihf/4.8.3/include"
    cxx_builtin_include_directory: "%package(@linaroLinuxGcc48Repo//)%/lib/gcc/arm-linux-gnueabihf/4.8.3/include-fixed"

    linker_flag: "--sysroot=linaroLinuxGcc48Repo/arm-linux-gnueabihf/libc"
    linker_flag: "-lstdc++"
    linker_flag: "-latomic"
    linker_flag: "-lm"
    linker_flag: "-lpthread"
  # linker_flag: "-Ltools/arm_compiler/linaro_linux_gcc/clang_more_libs"
    linker_flag: "-LlinaroLinuxGcc48Repo/arm-linux-gnueabihf/lib"
    linker_flag: "-LlinaroLinuxGcc48Repo/arm-linux-gnueabihf/libc/lib"
    linker_flag: "-LlinaroLinuxGcc48Repo/arm-linux-gnueabihf/libc/usr/lib"
    linker_flag: "-BlinaroLinuxGcc48Repo/arm-linux-gnueabihf/bin"
    linker_flag: "-Wl,--dynamic-linker=/lib/ld-linux-armhf.so.3"

    # Anticipated future default.
    # This makes GCC and Clang do what we want when called through symlinks.
    unfiltered_cxx_flag: "-no-canonical-prefixes"
    linker_flag: "-no-canonical-prefixes"

    # Make C++ compilation deterministic. Use linkstamping instead of these
    # compiler symbols.
    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\""

    # Security hardening on by default.
    # Conservative choice; -D_FORTIFY_SOURCE=2 may be unsafe in some cases.
    # We need to undef it before redefining it as some distributions now have
    # it enabled by default.
    compiler_flag: "-U_FORTIFY_SOURCE"
    compiler_flag: "-fstack-protector"
    compiler_flag: "-fPIE"
    linker_flag: "-pie"
    linker_flag: "-Wl,-z,relro,-z,now"

    # Enable coloring even if there's no attached terminal. Bazel removes the
    # escape sequences if --nocolor is specified.
    compiler_flag: "-fdiagnostics-color=always"

    # All warnings are enabled. Maybe enable -Werror as well?
    compiler_flag: "-Wall"
    # Enable a few more warnings that aren't part of -Wall.
    compiler_flag: "-Wunused-but-set-parameter"
    # But disable some that are problematic.
    compiler_flag: "-Wno-free-nonheap-object" # has false positives

    # Keep stack frames for debugging, even in opt mode.
    compiler_flag: "-fno-omit-frame-pointer"

    compilation_mode_flags {
      mode: DBG
      # Enable debug symbols.
      compiler_flag: "-g"
    }
    compilation_mode_flags {
      mode: OPT
      # No debug symbols.
      # Maybe we should enable https://gcc.gnu.org/wiki/DebugFission for opt or
      # even generally? However, that can't happen here, as it requires special
      # handling in Bazel.
      compiler_flag: "-g0"

      # Conservative choice for -O
      # -O3 can increase binary size and even slow down the resulting binaries.
      # Profile first and / or use FDO if you need better performance than this.
      compiler_flag: "-O2"

      # Disable assertions
      compiler_flag: "-DNDEBUG"

      # Removal of unused code and data at link time (can this increase binary size in some cases?).
      compiler_flag: "-ffunction-sections"
      compiler_flag: "-fdata-sections"
      linker_flag: "-Wl,--gc-sections"
    }     
  }

  toolchain {
    toolchain_identifier: "local"
    abi_libc_version: "local"
    abi_version: "local"
    builtin_sysroot: ""
    compiler: "compiler"
    compiler_flag: "-U_FORTIFY_SOURCE"
    compiler_flag: "-D_FORTIFY_SOURCE=2"
    compiler_flag: "-fstack-protector"
    compiler_flag: "-Wall"
    compiler_flag: "-Wl,-z,-relro,-z,now"
    compiler_flag: "-B/usr/bin"
    compiler_flag: "-B/usr/bin"
    compiler_flag: "-Wunused-but-set-parameter"
    compiler_flag: "-Wno-free-nonheap-object"
    compiler_flag: "-fno-omit-frame-pointer"
    cxx_builtin_include_directory: "/usr/include/c++/4.8"
    cxx_builtin_include_directory: "/usr/include/x86_64-linux-gnu/c++/4.8"
    cxx_builtin_include_directory: "/usr/include/c++/4.8/backward"
    cxx_builtin_include_directory: "/usr/lib/gcc/x86_64-linux-gnu/4.8/include"
    cxx_builtin_include_directory: "/usr/local/include"
    cxx_builtin_include_directory: "/usr/lib/gcc/x86_64-linux-gnu/4.8/include-fixed"
    cxx_builtin_include_directory: "/usr/include/x86_64-linux-gnu"
    cxx_builtin_include_directory: "/usr/include"
    cxx_flag: "-std=c++0x"
    host_system_name: "local"
    linker_flag: "-lstdc++"
    linker_flag: "-lm"
    linker_flag: "-Wl,-no-as-needed"
    linker_flag: "-B/usr/bin"
    linker_flag: "-B/usr/bin"
    linker_flag: "-pass-exit-codes"
    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: "/usr/bin/ar" }
    tool_path {name: "cpp" path: "/usr/bin/cpp" }
    tool_path {name: "dwp" path: "/usr/bin/dwp" }
    tool_path {name: "gcc" path: "/usr/bin/gcc" }
    tool_path {name: "gcov" path: "/usr/bin/gcov" }
    tool_path {name: "ld" path: "/usr/bin/ld" }
    tool_path {name: "nm" path: "/usr/bin/nm" }
    tool_path {name: "objcopy" path: "/usr/bin/objcopy" }
    tool_path {name: "objdump" path: "/usr/bin/objdump" }
    tool_path {name: "strip" path: "/usr/bin/strip" }

    compilation_mode_flags {
      mode: DBG
      compiler_flag: "-g"
    }
    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 }
  }

为 CROSSTOOL 编写 BUILD 文件:

  # This is the entry point for --crosstool_top.  Toolchains are found
  # by lopping off the name of --crosstool_top and searching for
  # 'cc-compiler-${CPU}' in this BUILD file, where CPU is the target CPU
  # specified in --cpu.
  #
  # This file group should include
  #   * all cc_toolchain targets supported
  #   * all file groups that said cc_toolchain might refer to,
  # including the default_grte_top setting in the CROSSTOOL
  # protobuf.
  filegroup(
      name = "toolchain_fg",
      srcs = [
          ":cc-compiler-armeabi-v7a",
          ":cc-compiler-k8",
        ":cc-compiler-rpi3-aarch64",
          ":linaro_linux_all_files",
      #"@org_linaro_components_toolchain_gcc_5_3_1//:compiler_components",
          "@linaroLinuxGcc48Repo//:compiler_components",
      ],
  )

  filegroup(
      name = "srcs",
      srcs = glob(["**"]) + [
          "//$(BAZEL_HOME_TOP)/tools/arm_compiler/linaro_linux_gcc:srcs",
      ],
      visibility = ["//$(BAZEL_HOME_TOP):__pkg__"],
  )

  cc_toolchain_suite(
      name = "toolchain",
      # target_cpu | compiler
      toolchains = {
          "armeabi-v7a|gcc": "cc-compiler-armeabi-v7a",
          "k8|compiler": "cc-compiler-k8",
    "rpi3-aarch64|gcc": "cc-compiler-rpi3-aarch64",
      },
  )

  filegroup(
      name = "linaro_linux_all_files",
      srcs = [
          "//tools/arm_compiler/linaro_linux_gcc:tool-wrappers",
          "@linaroLinuxGcc48Repo//:compiler_pieces",
        #"@org_linaro_components_toolchain_gcc_5_3_1//:compiler_pieces",
      ],
  )

  filegroup(
      name = "linaro_linux_linker_files",
      srcs = [
          "//tools/arm_compiler/linaro_linux_gcc:ar",
          "//tools/arm_compiler/linaro_linux_gcc:gcc",
          "//tools/arm_compiler/linaro_linux_gcc:ld",
          "@linaroLinuxGcc48Repo//:compiler_pieces",
        #"@org_linaro_components_toolchain_gcc_5_3_1//:compiler_pieces",
      ],
  )

  filegroup(
      name = "linaro_linux_compiler_files",
      srcs = [
          "//tools/arm_compiler/linaro_linux_gcc:as",
          "//tools/arm_compiler/linaro_linux_gcc:gcc",
          "//tools/arm_compiler/linaro_linux_gcc:ld",
      ],
  )

  filegroup(
      name = "empty",
      srcs = [],
  )

  cc_toolchain(
      name = "cc-compiler-rpi3-aarch64",
      all_files = ":linaro_linux_all_files",
      ar_files = "//tools/arm_compiler/linaro_linux_gcc:ar",
      as_files = "//tools/arm_compiler/linaro_linux_gcc:as",
      compiler_files = ":linaro_linux_compiler_files",
      cpu = "gcc-aarch64",
      dwp_files = ":empty",
      dynamic_runtime_libs = [":empty"],
      linker_files = ":linaro_linux_linker_files",
      objcopy_files = "//tools/arm_compiler/linaro_linux_gcc:objcopy",
      static_runtime_libs = [":empty"],
      strip_files = "//tools/arm_compiler/linaro_linux_gcc:strip",
      supports_param_files = 1,
      visibility = ["//visibility:public"],
  )

  cc_toolchain(
      name = "cc-compiler-armeabi-v7a",
      all_files = ":linaro_linux_all_files",
      ar_files = "//tools/arm_compiler/linaro_linux_gcc:ar",
      as_files = "//tools/arm_compiler/linaro_linux_gcc:as",
      compiler_files = ":linaro_linux_compiler_files",
      cpu = "armeabi-v7a",
      dwp_files = ":empty",
      dynamic_runtime_libs = [":empty"],
      linker_files = ":linaro_linux_linker_files",
      objcopy_files = "//tools/arm_compiler/linaro_linux_gcc:objcopy",
      static_runtime_libs = [":empty"],
      strip_files = "//tools/arm_compiler/linaro_linux_gcc:strip",
      supports_param_files = 1,
      visibility = ["//visibility:public"],
  )

  cc_toolchain(
      name = "cc-compiler-k8",
      all_files = ":empty",
      ar_files = ":empty",
      as_files = ":empty",
      compiler_files = ":empty",
      cpu = "local",
      dwp_files = ":empty",
      dynamic_runtime_libs = [":empty"],
      linker_files = ":empty",
      objcopy_files = ":empty",
      static_runtime_libs = [":empty"],
      strip_files = ":empty",
      supports_param_files = 1,
  )

使用新工具链构建:

$ (BAZEL_HOME_TOP) $ bazel build --crosstool_top=//tools/arm_compiler:toolchain --cpu=rpi3-aarch64
  Starting local Bazel server and connecting to it...
  .......................................................................................
  INFO: Analysed 0 targets (1 packages loaded).
  INFO: Found 0 targets...
  INFO: Elapsed time: 5.532s, Critical Path: 0.06s
  INFO: 0 processes.
  INFO: Build completed successfully, 1 total action

  $ (BAZEL_HOME_TOP) $ ls -al bazel-*
  lrwxrwxrwx 1 gbiradar gbiradar  94 Jun 16 22:11 bazel-bazel -> /home/gbiradar/.cache/bazel/_bazel_gbiradar/aef9de00047baf5adee2a310516bd2a5/execroot/io_bazel
  lrwxrwxrwx 1 gbiradar gbiradar 131 Jun 16 22:11 bazel-bin -> /home/gbiradar/.cache/bazel/_bazel_gbiradar/aef9de00047baf5adee2a310516bd2a5/execroot/io_bazel/bazel-out/rpi3-aarch64-fastbuild/bin
  lrwxrwxrwx 1 gbiradar gbiradar 136 Jun 16 22:11 bazel-genfiles -> /home/gbiradar/.cache/bazel/_bazel_gbiradar/aef9de00047baf5adee2a310516bd2a5/execroot/io_bazel/bazel-out/rpi3-aarch64-fastbuild/genfiles
  lrwxrwxrwx 1 gbiradar gbiradar 104 Jun 16 22:11 bazel-out -> /home/gbiradar/.cache/bazel/_bazel_gbiradar/aef9de00047baf5adee2a310516bd2a5/execroot/io_bazel/bazel-out
  lrwxrwxrwx 1 gbiradar gbiradar 136 Jun 16 22:11 bazel-testlogs -> /home/gbiradar/.cache/bazel/_bazel_gbiradar/aef9de00047baf5adee2a310516bd2a5/execroot/io_bazel/bazel-out/rpi3-aarch64-fastbuild/testlogs

  $ (BAZEL_HOME_TOP) $ file output/bazel 
  output/bazel: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=b80f2c9a33e10d048843f8b8e29b16adfa7f054d, not stripped

  ls -al /home/gbiradar/.cache/bazel/_bazel_gbiradar/aef9de00047baf5adee2a310516bd2a5/execroot/io_bazel/_bin/
  total 40
  drwxrwxr-x 2 gbiradar gbiradar 4096 Jun 16 22:11 .
  drwxrwxr-x 4 gbiradar gbiradar 4096 Jun 16 22:11 ..
  lrwxrwxrwx 1 gbiradar gbiradar  116 Jun 16 22:11 A-server.jar -> /home/gbiradar/.cache/bazel/_bazel_gbiradar/install/0ac57e6ee21db2c16628d01a9f50d7dc/_embedded_binaries/A-server.jar
  lrwxrwxrwx 1 gbiradar gbiradar  118 Jun 16 22:11 build-runfiles -> /home/gbiradar/.cache/bazel/_bazel_gbiradar/install/0ac57e6ee21db2c16628d01a9f50d7dc/_embedded_binaries/build-runfiles
  lrwxrwxrwx 1 gbiradar gbiradar  120 Jun 16 22:11 install_base_key -> /home/gbiradar/.cache/bazel/_bazel_gbiradar/install/0ac57e6ee21db2c16628d01a9f50d7dc/_embedded_binaries/install_base_key
  lrwxrwxrwx 1 gbiradar gbiradar  113 Jun 16 22:11 jdk.BUILD -> /home/gbiradar/.cache/bazel/_bazel_gbiradar/install/0ac57e6ee21db2c16628d01a9f50d7dc/_embedded_binaries/jdk.BUILD
  lrwxrwxrwx 1 gbiradar gbiradar  114 Jun 16 22:11 libunix.so -> /home/gbiradar/.cache/bazel/_bazel_gbiradar/install/0ac57e6ee21db2c16628d01a9f50d7dc/_embedded_binaries/libunix.so
  lrwxrwxrwx 1 gbiradar gbiradar  117 Jun 16 22:11 linux-sandbox -> /home/gbiradar/.cache/bazel/_bazel_gbiradar/install/0ac57e6ee21db2c16628d01a9f50d7dc/_embedded_binaries/linux-sandbox
  lrwxrwxrwx 1 gbiradar gbiradar  119 Jun 16 22:11 process-wrapper -> /home/gbiradar/.cache/bazel/_bazel_gbiradar/install/0ac57e6ee21db2c16628d01a9f50d7dc/_embedded_binaries/process-wrapper
  lrwxrwxrwx 1 gbiradar gbiradar  117 Jun 16 22:11 xcode-locator -> /home/gbiradar/.cache/bazel/_bazel_gbiradar/install/0ac57e6ee21db2c16628d01a9f50d7dc/_embedded_binaries/xcode-locator

现在我提到的所有步骤。如果上述步骤正确,我应该得到 ARM 可执行格式的 bazel 二进制文件。谁能指出我错过了什么。

问候,GBiradar

4

1 回答 1

0

您的bazel build生产线没有要构建的目标,因此它没有执行任何操作。你想建立 .egbazel build //src:bazel --crosstool_top=... --cpu=...吗?

于 2018-06-27T08:38:38.180 回答