bazelbuild / bazel

a fast, scalable, multi-language and extensible build system
https://bazel.build
Apache License 2.0
22.98k stars 4.03k forks source link

Cross compile qnx, Bazel does not produce shared libraries. #15997

Open tongtongdi opened 2 years ago

tongtongdi commented 2 years ago

Description of the bug: When I cross compile QNX, no dynamic library is generated. At this time, after I add feature( name = "supports_dynamic_linker", enabled = True, ), it prompts me that ld cannot be found. then i create a ld soft connection ln aarch64-unknown-nto-qnx7.1.0-ld ld, this problem is solved.i'm surprised that i specified ld in the tool_path like this tool_path( name = "ld", path = QNX_HOST + "/usr/bin/aarch64-unknown-nto-qnx7.1.0-ld",),, but it doesn't seem to work,How can I solve this problem gracefully。

Which operating system are you running Bazel on? Linux.

What is the output of bazel info release? 5.2.0

Have you found anything relevant by searching the web? When I cross compile the example , Bazel does not produce shared libraries

sgowroji commented 2 years ago

Hello @tongtongdi, Could you please provide complete steps to reproduce the above issue. Thanks!

tongtongdi commented 2 years ago

Hi @sgowroji , i cross compile qnx on x86_64 platform, my computer system is 20.04.1-Ubuntu, I follow the instruction at https://bazel.build/tutorials/cc-toolchain-config, the target cpu is aarch64, and the system is qnx. my toolchain/cc_toolchain_config.bzl is :

load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES")

load(
       "@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl",
       "feature",
       "flag_group",
       "flag_set",
       "tool_path",
   )

all_link_actions = [
      ACTION_NAMES.cpp_link_executable,
      ACTION_NAMES.cpp_link_dynamic_library,
      ACTION_NAMES.cpp_link_nodeps_dynamic_library,
]

all_compile_actions = [
    ACTION_NAMES.assemble,
    ACTION_NAMES.preprocess_assemble,
    ACTION_NAMES.linkstamp_compile,
    ACTION_NAMES.c_compile,
    ACTION_NAMES.cpp_compile,
    ACTION_NAMES.cpp_header_parsing,
    ACTION_NAMES.cpp_module_codegen,
    ACTION_NAMES.cpp_module_compile,
    ACTION_NAMES.clif_match,
    ACTION_NAMES.lto_backend,
]

QNX_HOST="/qnx710/host/linux/x86_64"
QNX_TARGET="/qnx710/target/qnx7"

def _impl(ctx):
    tool_paths = [
        tool_path(
            name = "ld",
            path = QNX_HOST + "/usr/bin/aarch64-unknown-nto-qnx7.1.0-ld",
        ),
        tool_path(
            name = "ar",
            path = QNX_HOST + "/usr/bin/aarch64-unknown-nto-qnx7.1.0-ar",
        ),
        tool_path(
            name = "cpp",
            path = QNX_HOST + "/usr/bin/aarch64-unknown-nto-qnx7.1.0-g++",
        ),
        tool_path(
            name = "gcc",
            path = QNX_HOST + "/usr/bin/aarch64-unknown-nto-qnx7.1.0-g++",
        ),
        tool_path(
            name = "gcov",
            path = QNX_HOST + "/usr/bin/aarch64-unknown-nto-qnx7.1.0-gcov",
        ),
        tool_path(
            name = "nm",
            path = QNX_HOST + "/usr/bin/aarch64-unknown-nto-qnx7.1.0-nm",
        ),
        tool_path(
            name = "objdump",
            path = QNX_HOST + "/usr/bin/aarch64-unknown-nto-qnx7.1.0-objdump",
        ),
        tool_path(
            name = "objcopy",
            path = QNX_HOST + "/usr/bin/aarch64-unknown-nto-qnx7.1.0-objcopy",
        ),
        tool_path(
            name = "strip",
            path = QNX_HOST + "/usr/bin/aarch64-unknown-nto-qnx7.1.0-strip",
        ),
    ]

    linker_flags = feature(
        name = "default_linker_flags",
        enabled = True,
        flag_sets = [
            flag_set(
                actions = all_link_actions,
                flag_groups = [
                    flag_group(
                        flags = [
                            "-lstdc++",
                        ],
                    ),
                ],
            ),
        ],
    )

    compiler_flags = feature(
        name = "default_compile_flags",
        enabled = True,
        flag_sets = [
            flag_set(
                actions = all_compile_actions,
                flag_groups = [
                    flag_group(
                        flags = [
                            "-Wall",
                        ],
                    ),
                ],
            ),
        ],
    )

    suportDynamic = feature(
        name = "supports_dynamic_linker",
        enabled = True,
    )

    return cc_common.create_cc_toolchain_config_info(
        ctx = ctx,
        features = [linker_flags, compiler_flags, suportDynamic],
        cxx_builtin_include_directories = [
          QNX_TARGET,
          QNX_HOST + "/usr/lib/gcc/aarch64-unknown-nto-qnx7.1.0/8.3.0/include/",
        ],
        toolchain_identifier = "aarch64-toolchain",
        host_system_name = "local",
        target_system_name = "aarch64le",
        target_cpu = "aarch64",
        target_libc = "unknown",
        compiler = "clang",
        abi_version = "unknown",
        abi_libc_version = "unknown",
        tool_paths = tool_paths,
    )

cc_toolchain_config = rule(
    implementation = _impl,
    attrs = {},
    provides = [CcToolchainConfigInfo],
)

my toolchain/BUILD is :

load(":cc_toolchain_config.bzl", "cc_toolchain_config")

cc_toolchain_suite(
    name = "clang_suite",
    toolchains = {
        "aarch64": "aarch64_toolchain",
    },
)

filegroup(name = "empty")

cc_toolchain(
    name = "aarch64_toolchain",
    toolchain_identifier = "aarch64-toolchain",
    toolchain_config = ":aarch64_toolchain_config",
    all_files = ":empty",
    compiler_files = ":empty",
    dwp_files = ":empty",
    linker_files = ":empty",
    objcopy_files = ":empty",
    strip_files = ":empty",
    supports_param_files = 0,
)

cc_toolchain_config(name = "aarch64_toolchain_config")

my .bazelrc file is :

# Use our custom-configured c++ toolchain.

build:clang_config --crosstool_top=//toolchain:clang_suite

# Use --cpu as a differentiator.

build:clang_config --cpu=aarch64

build:clang_config --action_env=QNX_HOST=/qnx710/host/linux/x86_64

build:clang_config --action_env=QNX_TARGET=/qnx710/target/qnx7

# Use the default Bazel C++ toolchain to build the tools used during the
# build.

build:clang_config --host_crosstool_top=@bazel_tools//tools/cpp:toolchain

you should set your qnx info in file cc_toolchain_config.bzl and .bazelrc. then i run bazel build :hello-world --config=clang_config, i get error log collect2: fatal error: cannot find 'ld'.

tongtongdi commented 2 years ago

when i enter /qnx710/host/linux/x86_64/usr/bin and create a ld soft connection ln aarch64-unknown-nto-qnx7.1.0-ld ld, this problem is solved, but i have set tool_path( name = "ld", path = QNX_HOST + "/usr/bin/aarch64-unknown-nto-qnx7.1.0-ld",), it still find ld not aarch64-unknown-nto-qnx7.1.0-ld.

tongtongdi commented 2 years ago

Hi @sgowroji ,without creating a soft link, how can i fix this. Can I use a configuration to solve this problem ?

xzxzzxzxzca commented 1 year ago

@tongtongdi the missing ld error is not caused by Bazel but is coming from g++. I get the same error when trying to run g++ directly. QNX SDP comes with wrapper commands qcc and q++, for C and C++ respectively, and those make the linker work somehow (well, it seems it just passes some extra flags like -B which it takes from nearby toolchain config files). So the error will go away if you put q++ in your tool_paths as the gcc tool.

Unfortunately, then another issue arises, which only happens under Bazel:

Lockfile (/etc/qnxlicenses.lck) acquisition timed out after 10 seconds. Check that the file and containing folder have write permissions set for this user, and that no other processes are using this file license check failed

However, this can be solved by passing --sandbox_writable_path="~/.qnx/license/" to bazel. Also, don't forget build --action_env=HOME in your .bazelrc.

If you want to stick to calling gcc directly, then you still have to properly set ALL THREE env vars: QNX_HOST, QNX_TARGET, AND PATH pointing to the SDK's bin dir. See the note here on how cross-compilers search for ld: https://gcc.gnu.org/onlinedocs/gccint/Collect2.html

github-actions[bot] commented 1 month ago

Thank you for contributing to the Bazel repository! This issue has been marked as stale since it has not had any activity in the last 1+ years. It will be closed in the next 90 days unless any other activity occurs. If you think this issue is still relevant and should stay open, please post any comment here and the issue will no longer be marked as stale.