Is it possible too use Opencover with CTest? - cmake

I am new using Opencover and I would like to know if it is possible to use it with CMake tests because my project is already using it.
mkdir build
cd build
cmake ..
cmake --build . --config Debug
..\tools\OpenCover.4.6.519\tools\OpenCover.Console.exe -target:"C:\Program Files (x86)\CMake\bin\cmake.exe" -targetargs:"--build . --target RUN_TESTS --config Debug"
And the corresponding error:
Committing...
No results, this could be for a number of reasons. The most common reasons are:
1) missing PDBs for the assemblies that match the filter please review the
output file and refer to the Usage guide (Usage.rtf) about filters.
2) the profiler may not be registered correctly, please refer to the Usage
guide and the -register switch.
I tried to add the switches targetdir and/or searchdirs to bin\Debug and also register but with the same result.
Do you have any idea on solving this?
Thanks for your help!

OpenCover only works for .net project running on the windows .net runtime.

Related

Building a CMake library within a Bazel project

I've written a module on top of a private fork off of TensorFlow that uses nanomsg.
For my local development server, I used cmake install to install nanomsg (to /usr/local) and accessed the header files from their installed location. The project runs fine locally.
However, I now need to package nanomsg within my TensorFlow workspace. I've tried the following two approaches, and find neither satisfactory:
Similar to this answer for OpenCV, I precompiled nanomsg into a private repository, loaded it within my workspace (within tensorflow/workspace.bzl) using an http_archive directive then included the headers and libraries in the relevant build script. This runs fine, but is not a portable solution.
A more portable solution, I created a genrule to run a specific sequence of cmake commands that can be used to build nanomsg. This approach is neater, but the genrule cannot be reused to cmake other projects. (I referred to this discussion).
Clearly cmake is not supported as a first-class citizen in Bazel builds. Is there anyone who has faced this problem in your own projects created a generic, portable way to include libraries within Bazel projects that are built using cmake? If so, how did you approach it?
As Ulf wrote, I think your suggested option 2 should work fine.
Regarding "can I identify if the cmake fails", yes: cmake should return with an error exit code (!= 0) when it fails. This in turn will cause Bazel to automatically recognize the genrule action as failed and thus fail the build. Because Bazel sets "set -e -o pipefail" before running your command (cf. https://docs.bazel.build/versions/master/be/general.html#genrule-environment), it should also work if you chain multiple cmake commands in your genrule "cmd".
If you call out to a shell script in your "cmd" attribute that then actually runs the cmake commands, make sure to put "set -e -o pipefail" in the first line of your script yourself. Otherwise the script will not fail when cmake fails.
If I misunderstood your question "Can I identify if the cmake fails", please let me know. :)
This new project: https://github.com/bazelbuild/rules_foreign_cc seems like a solution(it build rules for cmake to build your project inside bazel).

How to do Parallel CMake ExternalProject Builds

When building a CMake project (e.g. on Windows), I can perform a parallel build by executing the following command:
cmake --build . -- /m
The /m switch is passed to msbuild and I get parallelized builds. However, if I have some external project in my CMakeLists, that switch is not passed through to its build command.
What's the best way to ensure my ExternalProject is built in parallel? I don't want to hardcode a BUILD_COMMAND if I can avoid it.
In CMake 3.12, support was added to kick off parallel jobs from within cmake.
The previous versions only supported this via passthrough arguments after the --, which understandably wouldn't pass through to external builds. Though, I'm not sure if the new support works for ExternalProject, it'd be great to try and report back if it works!
I had the same problem with make based external_projects. I ended up with the solution to pass the -j8 flag via a cmake cached variable.
set(EXTERNAL_BUILD_ARGS "/m" CACHE STRING "flags for external project builds")

CMake build & link to library without installing to /usr/local or elsewhere

I'm trying to include an external library in a build environment that uses CMake. I'm not trying to install it on the local system (in fact I'd rather not do that, I don't want /usr/local clogged up with all kinds of libraries); I'd just like to have the resulting libxml2.a available for linking with my executable. I can build it fine with the following in CMakeLists.txt:
set (LIBXML_PATH ${MY_SOURCE_DIR}/libxml2-2.9.1)
add_custom_target (build_libxml ALL
COMMENT "Building libxml"
COMMAND ./configure --prefix=/tmp
COMMAND make
WORKING_DIRECTORY ${LIBXML_PATH}
)
But I'm still having trouble with the following:
1) Is this the right approach in the first place, for the general purpose of getting libraries built with configure and make into a CMake environment?
2) How do I get the resulting library (i.e. libxml2.a) under my build output directory?
3) How can I link to that library for my executable builds?
I tried a fiddly solution with
ADD_LIBRARY( xml2 STATIC libxml2.a )
but it seems like there must be a better way than hauling a whole library's contents into… a library.
Thanks.
You need to make it clearer to CMake what is going on here. All it can see now is that you have some custom command that it will run every time. Instead of using add_custom_target with COMMAND, I've found it better to use add_custom_command.
Something like this:
set (LIBXML_PATH ${MY_SOURCE_DIR}/libxml2-2.9.1)
add_custom_command(
OUTPUT libxml2.a
COMMENT "Building libxml"
COMMAND ./configure --prefix=/tmp
COMMAND make
WORKING_DIRECTORY ${LIBXML_PATH}
)
target_link_libraries(your-program libxml2.a)
By doing it this way, CMake can understand that your custom command's essential product is libxml2.a, and when CMake sees something depending on that, it will run the command (if the library doesn't exist already).

Cmake Error: could not load cache

I'm using Cmake to try to build a project for Eclipse. When I try running Cmake, I get the following error:
Error: could not load cache
Error: Batch build stopped due to Eclipse CDT4 - Unix Makefiles error.
---- Time Elapsed: 3 secs ----
Error: could not load cache
Error: Batch build stopped due to Eclipse CDT4 - Unix Makefiles error.
I'm completely stumped on what might be causing this. I know that I'm running Cmake in the correct directory and the CMakeCache.txt file is present. Could someone point me in the right direction to solve this?
If you are using the CLion, you can use File---"Reload CMake Project".
I meet this problem after using git force pull, and Reload CMake Project solves it.
Remove the CMakeCache.txt and try again. You probably had a bad cmake setup.
I have faced the same problem and solved it using the terminal.
Delete the cached/configurations files as we will get them again.
To configure the project run cmake .
Build the project using cmake --build .
run cmake --configure . it should generate required files.
I ran into this recently using JetBrains CLion and the above instructions were helpful but not directly, I was able to reload the project using the "cog" drop down in the CMake tab:
If you are absolutely positive that you are running the build command from the binary directory, this error probably means that you have had an issue during the configuration/generation step that you should have ran before trying the build. You can try to configure again to check (cmake your-build-dir)
I would advise running the Gui and trying to load the cache to see if you get a more explicit error (although I doubt it).
Another possibility would be to try to create a new clean build directory and take it from there.
In your example Eclipse must run something like
cmake --build folder_name --target all
and I opt that the *folder_name* is bad in this case. You probably messed something up in Eclipse.
For me it helps to select CMake tab (next to Run, TODO) in CLion. Then click the Reload CMakeProject button.
I got this error on Windows WSL with ubuntu
~/tmp/cmake$ cmake --build ./build
Error: could not load cache
I was able to fix the above error by running the following cmds in order:
% cmake -S . -B ./build
% cmake --build ./build
The above solution was derived from this post.
If you are using Visual Studio 2019, close Visual Studio, delete .vs and out folders then rebuild your project.
I removed the .cxx and other ide-generated files to the recycle.bin, except app.iml. Then I restarted Android Studio, and eventually it worked fine.
The solution that worked for me using VisualStudio 2017 was choosing:
CMake --> Cache --> Generate (from the top menu)
Apart from the already given answers, it might be due to the wrong commands or in wrong order.
To be precise, for me, it was due to
cmake -B build -G Ninja
cmake --build .
The "cmake --build ." command will throw the Could Not Load Cache error.
The problem is the build directory is given as '.' which doesn't have the cache or whatever files cmake generates, so correct command was 'cmake --build build'... and fixed !
There maybe trillion other ways but my solution was this.
For eg, it happened with the repo -> https://github.com/adi-g15/worldlinesim, though may show the same for other repos too.
For Ubuntu users, provide the source code open-pose path in the CMake-GUI.
P.S I had this issue and my path was not set there.
Most probably the issue is you have not wrote the correct name of the Visual Studio version you have installed during the build file preparation:
cmake .. -G "Visual Studio 16 2019" (note if you have VS 2016 you should change in in there)
The most realistic answer and personal experienced answer is
If you are using Clion and building files with IDE
And getting the error Cmake Error: could not load cache
Because you have accidentally deleted the cache file (like me: permanently and cant get back) or there is other problems or other problems
Then do this:
Run -> Clean
Run -> Build
And your project will be working all fine
cmake -B ./build(dest dir)
cmake --build ./build(

Is there a way to get errors when a CMake command fails?

I am writing a script and started working with the install command (for copying files) and it is not working. CMake configure/generate does not show any errors (i.e. it does not stop and no warnings/errors show related to this command) and the command does not seem to be working, because I don't see any files being copied.
Since I am new, I am wondering:
How can I tell that install failed (perhaps the source directory was wrong, or the destination directory was wrong)? It appears to be failing silently.
Are there error codes I can check to see what went wrong?
When is install called? When I click configure? Or when the project is built?
I am on Windows.
To the general question, there are a number of ways to get more verbose output from CMake - I just learned a third for gnarly errors:
to debug CMake recipes, I like using the message command and you can even iterate over directories and issue messages*
e.g. message( STATUS "SQLITE3_LIB: ${SQLITE3_LIB} SQLITE3_PATH: ${SQLITE3_PATH}") # prints SQLITE3_LIB and SQLITE3_PATH variables
perform verbose builds to troubleshoot your build itself
run make VERBOSE=1 (with make, ninja -v with ninja, etc.) to help you troubleshoot the process, such as cmake -DYOUR_OPTION="insert values" ~/path/to/files/ && make VERBOSE=1
if you ever find an inscrutable error, I just learned that we can run strace on the failing command - this can be a bit overwhelming, but can help when you have exhausted #1 and #2
I just used strace /usr/bin/cmake -E copy_directory $MY_SOURCE_PATH $MY_DEST_PATH to try to understand why a copy was failing
*I have used DLRdave's answer to a different question to print out the INCLUDE_DIRS:
get_property(dirs DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY INCLUDE_DIRECTORIES)
foreach(dir ${dirs})
message(STATUS "dir='${dir}'")
endforeach()
When you add an install command to your CMakeLists.txt, you get a new target created called "install".
In order to actually install the chosen files/targets, you need to build this install target. It's not automatically built as part of the "ALL" target.
For example, if you're using Visual Studio, the "INSTALL" target should appear in the "CMakePredefinedTargets" folder of the Solution Explorer. Just selecting this target and building it will cause the solution to be built and the selected items installed.
If any part of the build or install process fails, the notifications should then be apparent.