Bazel: pass a build time variable to C++ program - c++

I've this C++ program that prints the value of VERSION string:
#include <iostream>
int main() {
std::cout << "Version: " << VERSION << std::endl;
return 0;
}
I want to change the value of VERSION (a string) at build time. This is my Bazel config that contains a default value for VERSION as local_defines:
cc_binary(
name = "main",
srcs = ["main.cpp"],
local_defines = ["VERSION=\\\"alpha\\\""],
)
When I call it like this, it still prints alpha and NOT the new value I'm passing for VERSION.
bazel run --define VERSION=beta main
INFO: Analyzed target //:main (1 packages loaded, 2 targets configured).
INFO: Found 1 target...
Target //:main up-to-date:
bazel-bin/main
INFO: Elapsed time: 0.965s, Critical Path: 0.80s
INFO: 4 processes: 2 internal, 2 darwin-sandbox.
INFO: Build completed successfully, 4 total actions
INFO: Build completed successfully, 4 total actions
Version: alpha
Why is this not working?

cc_binary.local_defines and the --define flag are separate things, even though they share the same name. local_defines supports "Make" variable substitution, which means this will work: local_defines = ["VERSION=\\\"$(VERSION)\\\""].

Maybe the --workspace_status_command is an option for you. An example can be found here.

Related

Why is libFuzzer on Windows yielding error: "no interesting inputs were found"?

About half a year ago I had setup a CMake project with VSCode with a libFuzzer target that ran on Windows and macOS. I use the C++ extension along with the CMakeTools extension from Microsoft.
When I resumed the project again now I'm getting an error at the end of the run of the fuzzer:
ERROR: no interesting inputs were found. Is the code instrumented for coverage? Exiting.
Full output:
INFO: Seed: 2201882200
INFO: -max_len is not provided; libFuzzer will not generate inputs larger than 4096 bytes
INFO: A corpus is not provided, starting from an empty corpus
#2 INITED exec/s: 0 rss: 61Mb
ERROR: no interesting inputs were found. Is the code instrumented for coverage? Exiting.
Compared to the same fuzzer when it's run on macOS:
INFO: Seed: 1824512455
INFO: Loaded 1 modules (7 inline 8-bit counters): 7 [0x10c2baa88, 0x10c2baa8f),
INFO: Loaded 1 PC tables (7 PCs): 7 [0x10c2baa90,0x10c2bab00),
INFO: 6 files found in /Users/thomas/SourceTree/vscode-cmake-libfuzzer/fuzzers/corpus/fuzz_test/
INFO: seed corpus: files: 6 min: 1b max: 10b total: 37b rss: 30Mb
#7 INITED cov: 1 ft: 1 corp: 1/1b exec/s: 0 rss: 30Mb
#1048576 pulse cov: 1 ft: 1 corp: 1/1b lim: 8192 exec/s: 524288 rss: 1099Mb
#2097152 pulse cov: 1 ft: 1 corp: 1/1b lim: 8192 exec/s: 699050 rss: 1100Mb
#4194304 pulse cov: 1 ft: 1 corp: 1/1b lim: 8192 exec/s: 599186 rss: 1101Mb
#5740032 DONE cov: 1 ft: 1 corp: 1/1b lim: 8192 exec/s: 521821 rss: 1101Mb
Done 5740032 runs in 11 second(s)
stat::number_of_executed_units: 5740032
stat::average_exec_per_sec: 521821
stat::new_units_added: 0
stat::slowest_unit_time_sec: 0
stat::peak_rss_mb: 1101
Snippets of how I linked libFuzzer: (I added the libs to Windows manually as one must to make it link.)
# https://llvm.org/docs/LibFuzzer.html#fuzzer-usage
target_link_libraries(clang_fuzzer INTERFACE
-fsanitize=fuzzer,address
)
target_compile_options(clang_fuzzer INTERFACE
-fsanitize=fuzzer,address
)
The test fuzzer:
#include <iostream>
#include <stddef.h>
#include <stdint.h>
bool FuzzMe(const uint8_t *Data, size_t DataSize) {
return DataSize >= 3 &&
Data[0] == 'F' &&
Data[1] == 'U' &&
Data[2] == 'Z' &&
Data[3] == 'Z'; // :‑<
}
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
{
// std::cout << "Hello Fuzzy...\n";
FuzzMe(data, size);
return 0;
}
Minimal complete example:
https://github.com/thomthom/vscode-cmake-libfuzzer
On Windows I had installed clang via the snapshots made available: https://llvm.org/builds/
On macOS I installed it via brew install llvm (Since AppleClang doesn't include libFuzzer)
I've tested this on two Windows machines now that both used to run the fuzzer fine, now they don't. But I've not able to figure out what caused the regression. I know I didn't update Clang on either systems. On one I also tried installing Clang via Visual Studio, and I tried selecting that installation as well from VSCode to see if it made any difference, but no avail.
Only other thing I can think of is that my VSCode CMake projects needed adjustments after the VSCode-Tools extension updated the default generator for Windows. I don't recall what it used to use. I still find it strange if that should affect the libFuzzer.
It turned out to be caused by my CMake config omitting these lines for Windows builds:
target_link_libraries(clang_fuzzer INTERFACE
-fsanitize=fuzzer,address
)
I think some cache came into play here that caused me not to detect this when I did the initial work on this.
Commit for fix in my above example:
https://github.com/thomthom/vscode-cmake-libfuzzer/commit/d7553fe01f88c71c19ff8f833b4235b8a3d17f99
Hoping that it can be useful for some people, I am posting my experience with this error.
In my case, I was getting this error merely because I changed the permission of the corpus folder (see the command below). The reason I was doing it because libfuzzer was not adding new inputs to the corpus folder.
chmod 644 /corpus (this command caused the error)
To avoid this error, I tried another method to be able to add new inputs to the folder; changed the owner of corpus folder to the user of the fuzzer process (i.e., daemon in my case).
mkdir /corpus;chown daemon:daemon /corpus (this command helped)

Build and test V8 from AOSP for the older version on Ubuntu 18.04.5 LTS

I am trying to run the unit tests for the V8 present in the AOSP's lollipop release: external/chromium_org/v8
by following the documentation from https://v8.dev/docs/build. But the build itself is constantly failing.
Steps followed:
Export the depot_tools path
gclient sync
install dependencies using ./build/install-build-deps.sh (This script was not present by default in the source code, so had to copy manually from the higher version)
gm x64.release
I have installed all the dependencies and followed all the steps from the documentation mentioned above but when I do:
gm x64.release
the build fails with the following output:
# echo > out/x64.release/args.gn << EOF
is_component_build = false
is_debug = false
target_cpu = "x64"
use_goma = false
v8_enable_backtrace = true
v8_enable_disassembler = true
v8_enable_object_print = true
v8_enable_verify_heap = true
EOF
# gn gen out/x64.release
ERROR at //build/config/BUILDCONFIG.gn:71:7: Undefined identifier
if (os == "chromeos") {
^-
I have tried building the it with gn as well by following the manual workflow but I am ending up with the same errors. I also tried setting the os variable to linux in the gn args list but there as well I get the unknown identifier error.
I see that the v8 used in the AOSP project differs a lot in terms of files from the main source code with the same version. The helper script tools/dev/gm.py is also not present by default so I am using one from the higher version. It would be great if anyone could suggest if there's any different set of steps I should be following or any other resources I can refer to in order to build the V8 present in the AOSP project
Version: V8 3.29.88.17
OS: Ubuntu 18.04.5 LTS
Architecture: x86_64
3.29 is seriously old; I'm not surprised that it won't build with current tools. Rule of thumb: when building old software, use the tools that were used to build it back then.
In the case at hand: try make x64.release.check -jN with N being the number of CPU cores you have.
I see that the v8 used in the AOSP project differs a lot in terms of files from the main source code with the same version.
The "lollipop-release" branch contains V8 3.27.34.15, whereas "lollipop-mr1-release" contains V8 3.29.88.17 which you quoted. Does that explain the differences?

Googletest never fails (where it should) with bazel test but works with cmake & clion

I am trying to use googletest with bazel and cmake.
The cmake and clion works perfect as for the test part, it fails where it should fail and passes where it should pass.
However, bazel test will pass all the tests even they should not.
Bazel test not working
For example, I have the stupid_test.cc file:
#include "gtest/gtest.h"
TEST(StupidTests, Stupid1) {
EXPECT_EQ(100, 20);
EXPECT_TRUE(false);
}
which should fail always.
The bazel BUILD file:
cc_test(
name = "stupid_test",
srcs = ["stupid_test.cc"],
deps = [
"//bazel_build/googletest:gtest",
],
)
where bazel_build/googletest is exactly the same files downloaded from the googletest github.
Then I run bazel test :stupid_test, the output is:
⇒ blaze test :stupid_test
DEBUG: /private/var/tmp/_bazel_myusername/741c62b201e51840aa320b156e05fd70/external/bazel_tools/tools/osx/xcode_configure.bzl:87:9: Invoking xcodebuild failed, developer dir: /Users/honghaoli/Downloads/Xcode.app/Contents/Developer ,return code 1, stderr: xcrun: error: invalid DEVELOPER_DIR path (/Users/honghaoli/Downloads/Xcode.app/Contents/Developer), missing xcrun at: /Users/honghaoli/Downloads/Xcode.app/Contents/Developer/usr/bin/xcrun
, stdout:
INFO: Analysed target //:stupid_test (0 packages loaded, 0 targets configured).
INFO: Found 1 test target...
Target //:stupid_test up-to-date:
bazel-bin/stupid_test
INFO: Elapsed time: 1.840s, Critical Path: 1.55s
INFO: 4 processes: 4 darwin-sandbox.
INFO: Build completed successfully, 4 total actions
//:stupid_test PASSED in 0.1s
Executed 1 out of 1 test: 1 test passes.
INFO: Build completed successfully, 4 total actions
I have no idea why it passed.
CMake Works
The same test file would fail with very clear message using cmake and clion. For example, part of the CMakeLists
add_executable(stupid_test stupid_test.cc)
target_link_libraries(stupid_test gtest gtest_main)
add_test(NAME stupid_test COMMAND stupid_test)
and the output fail message:
Testing started at 19:16 ...
/Users/myusername...blabla/cmake-build-debug/stupid_test --gtest_filter=* --gtest_color=no
Running main() from /Users/myusername...blabla/cmake-build-debug/googletest-src/googletest/src/gtest_main.cc
[==========] Running 1 test from 1 test suite./Users/myusername...blabla/stupid_test.cc:11: Failure
Expected equality of these values:
100
20
/Users/myusername...blabla/stupid_test.cc:12: Failure
Value of: false
Actual: false
Expected: true
/Users/myusername...blabla/stupid_test.cc:13: Failure
Value of: true
Actual: true
Expected: false
Process finished with exit code 1
I am using the Mac OS 10.14 if that helps.
I figured out the reason.
Just change
"//bazel_build/googletest:gtest",
in the BUILD file into
"//bazel_build/googletest:gtest_main",
I'm still confused!
If the gtest does not work, it should at least fail the build or throw error/warnings. It should never say:
Executed 1 out of 1 test: 1 test passes.
as the output.
I consider it as a bug.

SonarQube C++ build-wrapper produce empty build-wrapper-dump.json

I'm using sonarQube C/C++ on WIN10 machine on a vxworks (arm) cross compile project.
After running:
build-wrapper-win-x86-64 --out-dir bw_output make MyProject.make
Build succeeded, but I get empty build-wrapper-dump.json file.
looking at the build-wrapper.log file I see:
process created with pid: 25256
image path name: <C:\WindRiver_6.9\gnu\4.3.3-vxworks-6.9\x86-win32\bin\ccarm.exe>
command line: <ccarm ...long line... MwInterfaceService.cpp >
working directory: <CommonLib\>
isWow64: 1
skipping process C:\WindRiver_6.9\gnu\4.3.3-vxworks-6.9\x86-win32\bin\ccarm.exe with pid: 25256
Is there a way to make build-wrapper to fill build-wrapper-dump.json file correctly?
thanks,
Mr. G

Basic procedure to use boost with biicode and MSVC 10.0

I'd like to test how biicode allows me to quickly create a project that uses boost. I (think I) have followed the procedure outlined in the bii docs.
After modifying the CMakeLists.txt, the configuration step does not succeed. The boost library does not seem to build to be built. I know that sometimes the configure step needs to be done twice (according to http://forum.biicode.com/t/error-could-not-find-the-following-static-boost-libraries-boost-thread/374), but this does not help.
I have pasted the exact steps I took below. I'd be very grateful if someone could show me where I went wrong.
1. Environnement : Windows 7 x64, Visual Studio 10 SP 1
PS G:\> bii --version
3.2
PS G:\> cmake --version
cmake version 3.2.2
CMake suite maintained and supported by Kitware (kitware.com/cmake).
PS G:\>
2. Start in a clean folder (G:\biitests)
3. Init the bii project
PS G:\biitests> bii init boostUsageTest -L
Successfully initialized biicode project boostUsageTest
4. Create the G:\biitests\boostUsageTest\main.cpp file with the following contents:
#include <boost/lexical_cast.hpp>
#include <boost/thread/mutex.hpp>
#include <iostream>
#include <exception>
int main (int argc, char *argv[])
{
boost::mutex uselessMutex;
auto value = boost::lexical_cast<int>(argv[0]);
std::cout << "value = " << value << std::endl;
return 0;
}
5. bii find
PS G:\biitests\boostUsageTest> bii find
INFO: Processing changes...
WARN: There are local unresolved dependencies
They will not be searched in the server
Unresolved: boost/lexical_cast.hpp
INFO: Finding missing dependencies in server
INFO: Looking for boost/thread...
WARN: Can't find block candidate for: boost/thread
INFO: No block candidates found
6. bii configure
PS G:\biitests\boostUsageTest> bii cpp:configure -G "Visual Studio 10"
INFO: Processing changes...
Running: "cmake" -G "Visual Studio 10" -Wno-dev ..\cmake
-- The C compiler identification is MSVC 16.0.40219.1
-- The CXX compiler identification is MSVC 16.0.40219.1
-- Check for working C compiler using: Visual Studio 10 2010
-- Check for working C compiler using: Visual Studio 10 2010 -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler using: Visual Studio 10 2010
-- Check for working CXX compiler using: Visual Studio 10 2010 -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
BLOCK: user/boostUsageTest
-----------------------------------------------------------
+ LIB: user_boostUsageTest
+ EXE: user_boostUsageTest_main
-- Configuring done
-- Generating done
-- Build files have been written to: G:/biitests/boostUsageTest/bii/build
PS G:\biitests\boostUsageTest>
7. edit CMakeLists.txt in the following way
(before my changes, the only non-commented line was ADD_BII_TARGETS() )
include(biicode/boost/setup)
INIT_BIICODE_BLOCK()
# Create Targets
ADD_BII_TARGETS()
bii_find_boost(COMPONENTS thread REQUIRED)
8. run bii find again
PS G:\biitests\boostUsageTest> bii find --update
INFO: Processing changes...
WARN: There are local unresolved dependencies
They will not be searched in the server
Unresolved: boost/lexical_cast.hpp
INFO: Finding missing dependencies in server
INFO: Looking for biicode/boost...
INFO: Block candidate: biicode/biicode/boost/master
INFO: Version biicode/boost: 9 (DEV) discarded
INFO: Version biicode/boost: 8 (STABLE) valid
INFO: Version biicode/boost: 7 (STABLE) valid
INFO: Version biicode/boost: 6 (STABLE) valid
INFO: Version biicode/boost: 5 (STABLE) valid
INFO: Version biicode/boost: 4 (STABLE) valid
INFO: Version biicode/boost: 3 (STABLE) valid
INFO: Version biicode/boost: 2 (STABLE) valid
INFO: Version biicode/boost: 1 (STABLE) valid
INFO: Version biicode/boost: 0 (STABLE) valid
INFO: Looking for boost/thread...
WARN: Can't find block candidate for: boost/thread
INFO: Analyzing compatibility for found dependencies...
Find resolved new dependencies:
biicode/boost: 8
ERROR: Find could not resolve:
boost/thread/mutex.hpp
INFO: Saving files from: toeb/cmakepp
INFO: Saving files from: biicode/boost
INFO: Saving files from: boost/install
9. Run bii configure again
PS G:\biitests\boostUsageTest> bii cpp:configure -G "Visual Studio 10"
INFO: Processing changes...
Running: "cmake" -G "Visual Studio 10" -Wno-dev ..\cmake
BLOCK: user/boostUsageTest
-----------------------------------------------------------
CALLING INIT_BIICODE_BLOCK IS NO LONGER NECESSARY
+ LIB: user_boostUsageTest
+ EXE: user_boostUsageTest_main
-- No linking type specified. Assuming static linking
-- Setting up biicode Boost configuration...
-- Building Boost 1.57.0 components with toolset msvc-10.0...
Starting thread library build job...
Building Boost components, please wait [ ]
Finished building thread library
-- Boost version: 1.57.0
CMake Error at ../deps/toeb/cmakepp/cmake/core/message.cmake:94 (_message):
Unable to find the requested Boost libraries.
Boost version: 1.57.0
Boost include path: G:/Users/bgo/.biicode/boost/1.57.0
Could not find the following static Boost libraries:
boost_thread
No Boost libraries were found. You may need to set BOOST_LIBRARYDIR to the
directory containing Boost libraries or BOOST_ROOT to the location of
Boost.
Call Stack (most recent call first):
C:/Program Files (x86)/CMake/share/cmake-3.2/Modules/FindBoost.cmake:1182 (message)
../deps/toeb/cmakepp/cmake/targets/find_package.cmake:17 (_find_package)
../deps/boost/install/install.cmake:351 (find_package)
../../CMakeLists.txt:9 (bii_find_boost)
-- Configuring incomplete, errors occurred!
See also "G:/biitests/boostUsageTest/bii/build/CMakeFiles/CMakeOutput.log".
ERROR: CMake failed
And running bii cpp:configure after that always gives the exact same output.
Thanks a lot in advance !
Benjamin