By default, Bazel supports #include'ing files relative to the WORKSPACE directory. However, as soon as a relative #include path from the workspace root contains a symlink, this seems to no longer hold. For example, suppose we have some directory structure as follows:
WORKSPACE
libs/foo/src/foo.cpp
libs/foo/itf/foo.h
libs/foo/BUILD
foo -> libs/foo
Where foo in the root directory is a symlink to libs/foo. Suppose we have:
foo.h:
#pragma once
void foo();
foo.cpp:
#include "foo/itf/foo.h"
void foo() {}
BUILD:
load("#rules_cc//cc:defs.bzl", "cc_binary", "cc_library")
cc_library(
name = "foo",
srcs = ["src/foo.cpp"],
hdrs = ["itf/foo.h"],
)
In this case, bazel build //... gives the following error message:
Starting local Bazel server and connecting to it...
INFO: Analyzed 2 targets (38 packages loaded, 166 targets configured).
INFO: Found 2 targets...
ERROR: [...]/foo/libs/foo/BUILD:3:11: Compiling libs/foo/src/foo.cpp failed: (Exit 1): gcc failed: error executing command /usr/bin/gcc -U_FORTIFY_SOURCE -fstack-protector -Wall -Wunused-but-set-parameter -Wno-free-nonheap-object -fno-omit-frame-pointer '-std=c++0x' -MD -MF ... (remaining 16 arguments skipped)
Use --sandbox_debug to see verbose messages from the sandbox and retain the sandbox build root for debugging
libs/foo/src/foo.cpp:1:10: fatal error: foo/itf/foo.h: No such file or directory
1 | #include "foo/itf/foo.h"
| ^~~~~~~~~~~~~~~
Is this a known issue or known behavior?
Bazel is not aware of the symlink. Therefore this does not work - you have to change your include path to libs/foo/itf/foo.h. You can add includes = ["libs"] to the cc_library to get around this.
I need to use meson build system, for a project, I am forced to give CXXFLAGS via, meson.build itself. According to offical meson documentation To add compiler options via add_project_arguments meson I need to give clang and opt options in the meson.build itself. This is how I do it in meson.build, I am supposed to use clang-14. The problem is as per the error log file, It accepts the clang options ( Those not prefixed by -mllvm ) like -O3 or -ftlo, because I see no error log for these clang options, but the options with ( -mllvm -some_opt_option ) gives me error clang-14: error: unknown argument: 'some_opt_option' how do I get past this error, and make meson to accept these opt options and not just clang options.
if cc.get_id() == 'clang'
# Thread safety annotation
add_project_arguments('-O3', language : 'cpp')
I am an absolute beginner to bazel. I am trying to build a CMake based application using bazel through rules_foreign_cc
General Info:
OS: Ubuntu 20.04 LTS
Bazel version: Tried with both 3.4.0 and 5.0.0
CMake version: 3.19.5
My directory structure looks like this
ROOT
|-----BUILD
|-----WORKSPACE
|-----<my_cmake_based_app>-<commit_id>.tar.gz
<my_cmake_based_app> has a CMakeLists.txt at its root.
WORKSPACE file looks like this:
workspace(name = "rules_foreign_cc_usage_example")
load("#bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
# Rule repository, note that it's recommended to use a pinned commit to a released version of the rules
http_archive(
name = "rules_foreign_cc",
# TODO: Get the latest sha256 value from a bazel debug message or the latest
# release on the releases page: https://github.com/bazelbuild/rules_foreign_cc/releases
#
# sha256 = "...",
strip_prefix = "rules_foreign_cc-0.7.0",
url = "https://github.com/bazelbuild/rules_foreign_cc/archive/0.7.0.tar.gz",
)
load("#rules_foreign_cc//foreign_cc:repositories.bzl", "rules_foreign_cc_dependencies")
# This sets up some common toolchains for building targets. For more details, please see
# https://github.com/bazelbuild/rules_foreign_cc/tree/main/docs#rules_foreign_cc_dependencies
rules_foreign_cc_dependencies()
_ALL_CONTENT = """
filegroup(
name = "all_srcs",
srcs = glob(["**"]),
visibility = ["//visibility:public"],
)
"""
http_archive(
name = "<target>",
strip_prefix = "<my_cmake_based_app>-<commit_id>",
build_file_content = _ALL_CONTENT,
url = "file:///<absolute/path/to>/<my_cmake_based_app>-<commit_id>.tar.gz",
)
My BUILD file looks like this:
load("#rules_foreign_cc//foreign_cc:defs.bzl", "cmake")
cmake(
name = "<target>",
cache_entries = {
"CMAKE_C_FLAGS": "-fPIC",
},
lib_source = "#<target>//:all_srcs",
out_shared_libs = ["lib<target>.so"],
)
For starting the build. I use the following command:
bazel build //:<target>
Error log looks like this:
DEBUG: Rule 'rules_foreign_cc' indicated that a canonical reproducible form can be obtained by modifying arguments sha256 = "1df78c7d7eed2dc21b8b325a2853c31933a81e7b780f9a59a5d078be9008b13a"
DEBUG: Repository rules_foreign_cc instantiated at:
no stack (--record_rule_instantiation_callstack not enabled)
Repository rule http_archive defined at:
/home/ubuntu/.cache/bazel/_bazel_ubuntu/312d04fd6758f8908769adc1bc8469ac/external/bazel_tools/tools/build_defs/repo/http.bzl:336:31: in <toplevel>
INFO: Repository local_config_cc instantiated at:
no stack (--record_rule_instantiation_callstack not enabled)
Repository rule cc_autoconf defined at:
/home/ubuntu/.cache/bazel/_bazel_ubuntu/312d04fd6758f8908769adc1bc8469ac/external/bazel_tools/tools/cpp/cc_configure.bzl:143:30: in <toplevel>
ERROR: An error occurred during the fetch of repository 'local_config_cc':
Traceback (most recent call last):
File "/home/ubuntu/.cache/bazel/_bazel_ubuntu/312d04fd6758f8908769adc1bc8469ac/external/bazel_tools/tools/cpp/cc_configure.bzl", line 125
configure_unix_toolchain(<3 more arguments>)
File "/home/ubuntu/.cache/bazel/_bazel_ubuntu/312d04fd6758f8908769adc1bc8469ac/external/bazel_tools/tools/cpp/unix_cc_configure.bzl", line 352, in configure_unix_toolchain
_find_generic(repository_ctx, <3 more arguments>)
File "/home/ubuntu/.cache/bazel/_bazel_ubuntu/312d04fd6758f8908769adc1bc8469ac/external/bazel_tools/tools/cpp/unix_cc_configure.bzl", line 314, in _find_generic
repository_ctx.which(result)
Program argument of which() may not contains a / or a \ ('x86_64-<name_of_sdk>-linux-gcc -march=x86-64 -mtune=generic -mfpmath=sse -msse4.2 -m64 -fstack-protector-strong -O2 -D_FORTIFY_SOURCE=2 -Wformat -Wformat-security -Werror=format-security --sysroot=/home/ubuntu/sdks/sysroots/x86-64-generic-<name_of_sdk>-linux' given)
INFO: Repository cmake-3.22.0-linux-x86_64 instantiated at:
no stack (--record_rule_instantiation_callstack not enabled)
Repository rule http_archive defined at:
/home/ubuntu/.cache/bazel/_bazel_ubuntu/312d04fd6758f8908769adc1bc8469ac/external/bazel_tools/tools/build_defs/repo/http.bzl:336:31: in <toplevel>
INFO: Repository <target> instantiated at:
no stack (--record_rule_instantiation_callstack not enabled)
Repository rule http_archive defined at:
/home/ubuntu/.cache/bazel/_bazel_ubuntu/312d04fd6758f8908769adc1bc8469ac/external/bazel_tools/tools/build_defs/repo/http.bzl:336:31: in <toplevel>
ERROR: /home/ubuntu/<ROOT>/BUILD:3:6: //:<target> depends on #local_config_cc//:cc-compiler-k8 in repository #local_config_cc which failed to fetch. no such package '#local_config_cc//': Traceback (most recent call last):
File "/home/ubuntu/.cache/bazel/_bazel_ubuntu/312d04fd6758f8908769adc1bc8469ac/external/bazel_tools/tools/cpp/cc_configure.bzl", line 125
configure_unix_toolchain(<3 more arguments>)
File "/home/ubuntu/.cache/bazel/_bazel_ubuntu/312d04fd6758f8908769adc1bc8469ac/external/bazel_tools/tools/cpp/unix_cc_configure.bzl", line 352, in configure_unix_toolchain
_find_generic(repository_ctx, <3 more arguments>)
File "/home/ubuntu/.cache/bazel/_bazel_ubuntu/312d04fd6758f8908769adc1bc8469ac/external/bazel_tools/tools/cpp/unix_cc_configure.bzl", line 314, in _find_generic
repository_ctx.which(result)
Program argument of which() may not contains a / or a \ ('x86_64-<name_of_sdk>-linux-gcc -march=x86-64 -mtune=generic -mfpmath=sse -msse4.2 -m64 -fstack-protector-strong -O2 -D_FORTIFY_SOURCE=2 -Wformat -Wformat-security -Werror=format-security --sysroot=/home/ubuntu/sdks/sysroots/x86-64-generic-<name_of_sdk>-linux' given)
ERROR: Analysis of target '//:<target>' failed; build aborted: Analysis failed
INFO: Elapsed time: 0.127s
INFO: 0 processes.
FAILED: Build did NOT complete successfully (0 packages loaded, 0 targets configured)
Some of my obvious questions are:
I have no idea from where this #local_config_cc is coming from.
Am I missing out on something to add in my WORKSPACE / BUILD files? OR
Is my directory structure a complete mess?
As a side note, since this is the 1st time I am posting something over stackoverflow, I would highly appreciate any tips so that I could ask questions in a better way in the future.
Thanks!
EDIT: FYI, I was able to build the example given in rules_foreign_cc on my existing system.
EDIT: FYI, The system I am using is an AWS EC2 Instance
#local_config_cc is part of Bazel's automatic C++ toolchain set up. It looks like something is going wrong between Bazel and the C++ toolchain on your system, because this is the error:
Program argument of which() may not contains a / or a \ ('x86_64-<name_of_sdk>-linux-gcc -march=x86-64 -mtune=generic -mfpmath=sse -msse4.2 -m64 -fstack-protector-strong -O2 -D_FORTIFY_SOURCE=2 -Wformat -Wformat-security -Werror=format-security --sysroot=/home/ubuntu/sdks/sysroots/x86-64-generic-<name_of_sdk>-linux' given)
So the argument to which looks like some command, whereas it should be just the name of a c++ tool like "gcc". Bazel will look at environment variables to find these tools too. Is there something in the CC environment variable?
If not, it might be useful to try to build something simple first, e.g.:
main.c:
#include <stdio.h>
int main() {
printf("hello world\n");
return 0;
}
BUILD:
cc_binary(
name = "main",
srcs = ["main.c"],
)
then:
$ bazel run main
Starting local Bazel server and connecting to it...
INFO: Analyzed target //:main (35 packages loaded, 146 targets configured).
INFO: Found 1 target...
Target //:main up-to-date:
bazel-bin/main
INFO: Elapsed time: 3.657s, Critical Path: 0.03s
INFO: 1 process: 1 internal.
INFO: Build completed successfully, 1 total action
INFO: Build completed successfully, 1 total action
hello world
Commenting more generally on your other questions, it's somewhat unusual to have a tar.gz archive in your workspace and then reference that archive in an http_archive rule in your WORKSPACE file[1]. If you have specific reasons or other context for doing that, it might be helpful to talk about those. Otherwise, you're basically just using Bazel to untar an archive and run make on the contents, which you could do in a two-line shell script :) Typically your build files and workspace file would be a part of your code repository itself.
[1] Also, putting absolute paths in your workspace file (or really anywhere) makes your set up not portable
I have been learning CMake and am having persistent build issues with the library Catch2.
I am using Qt as my IDE and have downlaoded Qt6 and MinGW 8 via the Qt maintainance tool.
My issue seems to be that during compilation my test/main.cpp, which includes Catch2, is bloating to the point where the compiler errors.
14:57:22: Running steps for project CMakeTestbed...
14:57:22: Starting: "<dir>\CMake\bin\cmake.exe" --build . --target all
[0/1 ?/sec] Re-running CMake...
-- Could NOT find Vulkan (missing: Vulkan_INCLUDE_DIR)
-- Using the single-header code from <dir>/build-CMakeTestbed-Desktop_Qt_6_0_0_MinGW_64_bit-Debug/_deps/json-src/single_include/
-- Version: 7.1.3
-- Build type: Debug
-- CXX_STANDARD: 17
-- Required features: cxx_variadic_templates
-- Configuring done
-- Generating done
-- Build files have been written to: <dir>/build-CMakeTestbed-Desktop_Qt_6_0_0_MinGW_64_bit-Debug
[1/17 3.7/sec] Automatic MOC and UIC for target fmt
[2/15 5.6/sec] Automatic MOC and UIC for target Catch2WithMain
[3/14 6.8/sec] Automatic MOC and UIC for target CMakeTestbedLib
[4/12 3.6/sec] Automatic MOC and UIC for target CMakeTestbed
[5/10 4.5/sec] Automatic MOC and UIC for target Tests
[6/9 0.4/sec] Building CXX object _deps/catch2-build/CMakeFiles/Catch2WithMain.dir/src/catch_with_main.cpp.obj
FAILED: _deps/catch2-build/CMakeFiles/Catch2WithMain.dir/src/catch_with_main.cpp.obj
C:\Qt\Tools\mingw810_64\bin\g++.exe -I_deps/catch2-build -I_deps/catch2-src -I_deps/catch2-build/Catch2WithMain_autogen/include -I_deps/json-src/include -I_deps/fmt-src/include -I_deps/catch2-src/single_include -g -std=gnu++17 -MD -MT _deps/catch2-build/CMakeFiles/Catch2WithMain.dir/src/catch_with_main.cpp.obj -MF _deps\catch2-build\CMakeFiles\Catch2WithMain.dir\src\catch_with_main.cpp.obj.d -o _deps/catch2-build/CMakeFiles/Catch2WithMain.dir/src/catch_with_main.cpp.obj -c _deps/catch2-src/src/catch_with_main.cpp
C:/Qt/Tools/mingw810_64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/bin/as.exe: _deps/catch2-build/CMakeFiles/Catch2WithMain.dir/src/catch_with_main.cpp.obj: too many sections (32805)
<dir>\AppData\Local\Temp\ccE30B5r.s: Assembler messages:
<dir>\AppData\Local\Temp\ccE30B5r.s: Fatal error: can't write 233 bytes to section .text of _deps/catch2-build/CMakeFiles/Catch2WithMain.dir/src/catch_with_main.cpp.obj: 'File too big'
C:/Qt/Tools/mingw810_64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/bin/as.exe: _deps/catch2-build/CMakeFiles/Catch2WithMain.dir/src/catch_with_main.cpp.obj: too many sections (32805)
<dir>\AppData\Local\Temp\ccE30B5r.s: Fatal error: can't close _deps/catch2-build/CMakeFiles/Catch2WithMain.dir/src/catch_with_main.cpp.obj: File too big
[7/9 0.4/sec] Building CXX object test/CMakeFiles/Tests.dir/main.cpp.obj
FAILED: test/CMakeFiles/Tests.dir/main.cpp.obj
C:\Qt\Tools\mingw810_64\bin\g++.exe -DFMT_LOCALE -DJSON_USE_IMPLICIT_CONVERSIONS=1 -Itest -I"<dir>/CMakeTestbed/test" -Itest/Tests_autogen/include -I_deps/json-src/include -I_deps/fmt-src/include -I_deps/catch2-src/single_include -I"<dir>/CMakeTestbed/test/../libs" -I"<dir>/CMakeTestbed/test/../src" -I_deps/json-src/single_include -g -std=gnu++17 -MD -MT test/CMakeFiles/Tests.dir/main.cpp.obj -MF test\CMakeFiles\Tests.dir\main.cpp.obj.d -o test/CMakeFiles/Tests.dir/main.cpp.obj -c "<dir>/CMakeTestbed/test/main.cpp"
C:/Qt/Tools/mingw810_64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/bin/as.exe: test/CMakeFiles/Tests.dir/main.cpp.obj: too many sections (32805)
<dir>\AppData\Local\Temp\ccARyb79.s: Assembler messages:
<dir>\AppData\Local\Temp\ccARyb79.s: Fatal error: can't write 233 bytes to section .text of test/CMakeFiles/Tests.dir/main.cpp.obj: 'File too big'
C:/Qt/Tools/mingw810_64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/bin/as.exe: test/CMakeFiles/Tests.dir/main.cpp.obj: too many sections (32805)
<dir>\AppData\Local\Temp\ccARyb79.s: Fatal error: can't close test/CMakeFiles/Tests.dir/main.cpp.obj: File too big
ninja: build stopped: subcommand failed.
14:57:49: The process "C:\Program Files\CMake\bin\cmake.exe" exited with code 1.
Error while building/deploying project CMakeTestbed (kit: Desktop Qt 6.0.0 MinGW 64-bit)
When executing step "Build"
These issues only occur in debug builds, so I believe the combination of heavy templating, along with the debug symbols is hitting some MinGW compiler threshold...
I am trying something like
target_compile_options(Tests
-Wa
-mbig-obj
)
(Doesn't seem to actually be valid CMake, tried without the '-' too, regardless, this doesn't fix the problem, just hides it...)
But it seems to me the best option would be to make the library precompile in release (I don't need to debug the Catch2 internals, if I am debugging, it is because I have a broken test that I am investigating, and I shouldn't need the dirty details of the lib internals to do that right?)
I have a minimal working example here https://github.com/SebastianTroy/CMakeTestbed.
The main files of interest here would be:
catch2.cmake (from https://github.com/catchorg/Catch2/blob/v2.x/docs/cmake-integration.md#cmake-target)
test/CMakeLists.txt (includes Catch2)
test/main.cpp (the unit that is failing to compile)
I would really appreciate some help with either forcing CMake to precompile the library with higher optimisation flags than the target (to prevent the issue altogether) I would settle for help telling the compiler to ignore the overly large file (to just sidestep the issue)
Build fails without much helpful information after I've added this into my CmakeLists.txt to enable clang-tidy checks:
if(ENABLE_CLANG_TIDY)
find_program(CLANGTIDY clang-tidy)
if(CLANGTIDY)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_CXX_CLANG_TIDY "${CLANGTIDY};-extra-arg=-Wno-unknown-warning-option;-header-filter=${CMAKE_SOURCE_DIR}/src/*;")
else()
message(SEND_ERROR "clang-tidy requested but executable not found")
endif()
endif()
but then, when I call cmake -DENABLE_CLANG_TIDY=ON .. && make the build process stops on 2% with minimum explanations of what is wrong. Here is the most meaningful parts of it:
make[2]: Leaving directory '/path/to/module/build'
make -f src/CMakeFiles/module.dir/build.make src/CMakeFiles/module.dir/build
make[2]: Entering directory '/path/to/module/build'
[ 2%] Building CXX object src/CMakeFiles/module.dir/api/base_controller.cpp.o
cd /path/to/module/build/src && ccache /usr/local/bin/cmake -E __run_co_compile --tidy="/usr/bin/clang-tidy;-extra-arg=-Wno-unknown-warning-option;-header-filter=/path/to/module/src/*;-p=/path/to/module/src;;--extra-arg-before=--driver-mode=g++" --source=/path/to/module/src/api/base_controller.cpp -- /usr/lib64/ccache/c++ -I/path/to/module/src -I/home/vagrant/.conan/data/catch2/2.11.0/_/_/package/5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9/include -DFAST_LOCK -D__CPU_x86_64 -DPKG_MALLOC -DSHM_MMAP -DDNS_IP_HACK -DUSE_MCAST -DUSE_TCP -DDISABLE_NAGLE -DHAVE_RESOLV_RES -DUSE_DNS_CACHE -DUSE_DNS_FAILOVER -DUSE_DST_BLACKLIST -DUSE_NAPTR -DWITH_XAVP -DMEM_JOIN_FREE -DF_MALLOC -DQ_MALLOC -DTLSF_MALLOC -DDBG_SR_MEMORY -DUSE_TLS -DTLS_HOOKS -DUSE_CORE_STATS -DSTATISTICS -DMALLOC_STATS -DUSE_SCTP -DFAST_LOCK -DADAPTIVE_WAIT -DADAPTIVE_WAIT_LOOPS=1024 -DCC_GCC_LIKE_ASM -DHAVE_GETHOSTBYNAME2 -DHAVE_UNION_SEMUN -DHAVE_SCHED_YIELD -DHAVE_MSG_NOSIGNAL -DHAVE_MSGHDR_MSG_CONTROL -DHAVE_ALLOCA_H -DHAVE_TIMEGM -DHAVE_SCHED_SETSCHEDULER -DHAVE_IP_MREQN -DUSE_RAW_SOCKS -DHAVE_EPOLL -DHAVE_SIGIO_RT -DSIGINFO64_WORKARROUND -DUSE_FUTEX -DHAVE_SELECT -D_GNU_SOURCE -DCUSTOM_LOG_FMT -fPIC -Wall -Werror -Wextra -Wno-write-strings -Wno-format -g -Wextra -Wno-error=class-memaccess -Wno-error=implicit-fallthrough -O3 -DNDEBUG -D_GLIBCXX_USE_CXX11_ABI=1 -o CMakeFiles/module.dir/api/base_controller.cpp.o -c /path/to/module/src/api/base_controller.cpp
error: unable to handle compilation, expected exactly one compiler job in '' [clang-diagnostic-error]
Error while processing some/path/src/.
5481 warnings and 1 error generated.
Error while processing some/path/src/api/file.cpp.
Suppressed 5481 warnings (5403 in non-user code, 78 with check filters).
Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well.
Found compiler error(s).
Some notes:
Without ENABLE_CLANG_TIDY flag, the project compiles just fine
I've fixed errors that clang-tidy was reporting
I got the same problem and solution is simple - remove the last semicolon in your CMAKE_CXX_CLANG_TIDY string:
Your line:
set(CMAKE_CXX_CLANG_TIDY "${CLANGTIDY};-extra-arg=-Wno-unknown-warning-option;-header-filter=${CMAKE_SOURCE_DIR}/src/*;")
To be replaced with:
set(CMAKE_CXX_CLANG_TIDY "${CLANGTIDY};-extra-arg=-Wno-unknown-warning-option;-header-filter=${CMAKE_SOURCE_DIR}/src/*")