[question] how to get build directory after running conan create so that I can run ctest?
What is your question?
After running conan create, I'd like to run ctest. I need the cmake cache directory for this. conan cache path is close, but it doesn't give me the build directory. Am I missing something?
Have you read the CONTRIBUTING guide?
- [X] I've read the CONTRIBUTING guide
Hi @wuziq
Thanks for your question.
This is not generally how it works. If you run conan create the package is created, only the public headers are available, for example.
The way to run unittests is generally within the package creation process, while building the package, like in the recipe:
def build(self):
cmake = CMake(self)
cmake.configure()
cmake.build()
cmake.ctest()
Note that there is also cmake.test() to run them from the target, not from ctest, and there is the tools.build:skip_test configuration, that you can set to True to disable/skip the building and execution of tests when you want to skip them.
Hi @wuziq
Any further question or issue here? May we close the ticket? Thanks for the feedback!
Closing as inactive, please re-open or create new tickets if necessary. Thanks!
If we want to run ctest separately (i.e., not relying on Conan to do it), does that mean we need to use conan install and conan build, rather than conan create?
(Sorry for resurrecting an old thread!)
Yes, the standard flow is just:
$ conan install ....
$ cmake ... # or your build system
# ctest ...
The conan create is for creating packages, if you want to just install dependencies and build something (including running tests), without creating a package, conan install to install the dependencies is enough.
The flow we'd like is:
conan install ...
conan build ...
ctest ...
# if tests pass
conan export-pkg ...
# if test_package passes
conan upload ...
If ctest passes, we want to avoid calling conan create, which would build the project again. We don't want this because we want to save time in CI.
This is all great, but a big downside is that conan export-pkg doesn't seem to care whether exports_sources is correct - i.e., if a file or folder is missing from exports_sources, the conan export-pkg step still passes. My guess is that it uses the files from the local checkout, whereas conan create uses the files that are exported to the cache, but I could be wrong.
Is there a better way to do what we're trying to do? Is there a way to ensure that conan export-pkg verifies exports_sources, maybe?
conan install ... conan build ...
The first conan install is unnecessary and redundant, the conan build already does a conan install internally.
doesn't seem to care whether exports_sources is correct - i.e., if a file or folder is missing from exports_sources
@wuziq this is exactly the reason why conan create is recommended for many CI cases, because it guarantees that everything is reproducible, as it does a clean build in the cache, exporting the sources, ensuring that everything works and builds correctly.
Why not just adding a cmake.ctest() call inside the def build() method and have a simple conan create . do everything?
We would like to keep the build and test steps separate. Also, there are some configurations where we don't need to create the package, e.g., when we run code coverage or sanitizers.
We would like to keep the build and test steps separate. Also, there are some configurations where we don't need to create the package, e.g., when we run code coverage or sanitizers.
You can build some configurations with conan build + ctest and other configurations with conan create.
But I am afraid, that what you are wanting to achieve, which is making sure that the recipe is fully correct and reproducible (the correct exporting of the files is one part of that), needs to do that, run a full and clean build, which is what conan create does. It is not possible to have one thing and the opposite at the same time, and having local builds, in which Conan doesn't fully control the build process and having guarantees that the build is reproducible are opposite things.
You can build some configurations with
conan build + ctestand other configurations withconan create. But I am afraid, that what you are wanting to achieve, which is making sure that the recipe is fully correct and reproducible (the correct exporting of the files is one part of that), needs to do that, run a full and clean build, which is whatconan createdoes. It is not possible to have one thing and the opposite at the same time, and having local builds, in which Conan doesn't fully control the build process and having guarantees that the build is reproducible are opposite things.
Got it, thank you!
We've been able to run unit tests from the Conan cache after conan create just by navigating to the location provided in the Conan output:
...
[100%] Built target mytest
foo/1.0.0: Package 'edd26af374efe02e4128d46295faa77ba5ccd92d' built
foo/1.0.0: Build folder /home/me/.conan2/p/b/fooa6a4edbd71292/b/build/Release
...
$ cd /home/me/.conan2/p/b/fooa6a4edbd71292/b/build/Release
$ source generators/conanrun.sh
$ ctest
Test project /home/me/.conan2/p/b/fooa6a4edbd71292/b/build/Release
Start 1: MyTest.test1
1/2 Test #1: MyTest.test1 ..................... Passed 0.00 sec
Start 2: MyTest.test2
2/2 Test #2: MyTest.test2 ..................... Passed 0.00 sec
100% tests passed, 0 tests failed out of 2
Total Test time (real) = 0.01 sec
Is there any issue we should be aware of when following this approach? I know you had mentioned previously that only the public headers are available, but maybe I'm not clear on the implications of that when it comes to running ctest, for example.
Is there any issue we should be aware of when following this approach? I know you had mentioned previously that only the public headers are available, but maybe I'm not clear on the implications of that when it comes to running ctest, for example.
As specified in the docs, the Conan text output shouldn't be used for parsing and automation. If you are obtaining the build folder based on some regex from the terminal text output, that can change anytime and break your flow.
Still it is not very clear why you would go to the cache build folder manually and launch ctest there, instead of letting Conan automatically do it with:
def build(self):
cmake = CMake(self)
cmake.configure()
cmake.build()
cmake.ctest()
Which is fully controllable with the tools.build:skip_test configuration, you can enable and disable the test running at will.
For anyone who happens to stumble on this issue, it looks like my original question has actually been resolved with conan cache path <ref> --build=folder, which I see is available in Conan 2.14 (it might've been added in an earlier version).
Note that for the ctest scenario specifically, you might need to actually dig a couple directories down into the provided build folder, e.g., down to build/Release, likely wherever the CMakeCache.txt lives.