-
Notifications
You must be signed in to change notification settings - Fork 76
Support find_package and install #256
base: master
Are you sure you want to change the base?
Conversation
We found a Contributor License Agreement for you (the sender of this pull request), but were unable to find agreements for all the commit author(s) or Co-authors. If you authored these, maybe you used a different email address in the git commits than was used to sign the CLA (login here to double check)? If these were authored by someone else, then they will need to sign a CLA as well, and confirm that they're okay with these being contributed to Google. |
Rewrites abseil and googletest to use targets and find_package Adds support for install target to opencensus
5051d2a
to
43e5789
Compare
CLAs look good, thanks! |
With the current structure, abseil and googletest targets have to be built prior to building any opencensus libs/tests. I think the "best practice" way to get around this is something like the answer in https://stackoverflow.com/questions/44990964/how-to-perform-cmakefind-package-at-build-stage-only . If I implement this - and building works properly - would this be acceptable to you? |
The approach in @coryan, what do you think? Especially as a consumer of opencensus-cpp as a library. What's the right approach here? |
@meastp: Would you like to split out "Replace OPENCENSUS_INCLUDE_DIR with CMAKE_SOURCE_DIR" into its own PR? (and please convert to Unix line endings) If that doesn't break an out-of-tree example, I'd be happy to merge that. |
TL;DR; if you want to use Longer version, these are the most common, (there are many more) choices for dependency management in CMake
I believe, and this is just like My Opinion:tm:, that you should let folks configure how the dependencies are found. Your first-time users will prefer the simplicity of |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Drive-by comments, take them with a grain of salt, feel free to ignore them too.
|
||
option(BUILD_SHARED_LIBS "Build shared libraries" OFF) | ||
|
||
IF(MSVC) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: be consistent IF
or if
everywhere (I think the trend is towards lowercase).
option(BUILD_SHARED_LIBS "Build shared libraries" OFF) | ||
|
||
IF(MSVC) | ||
add_definitions(-DNOMINMAX) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you want target_compile_definitions()
in the targets that need this definition, not need to push it on everybody.
add_definitions(-DNOMINMAX) | ||
ENDIF() | ||
|
||
if(CMAKE_BUILD_TYPE STREQUAL "Release") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is unusual... is that to avoid installing the headers twice?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think so. To be honest I took that example from somewhere else. What is the proper way to do it? :)
|
||
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake) | ||
|
||
find_package(googletest REQUIRED) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CMake has a native module to find gtest:
https://cmake.org/cmake/help/latest/module/FindGTest.html
why not use it here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, because you want to download it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, this is the rationale for changing from "inline" ExternalProject_Add to doing it via find_package
- Adding install/export failed because I then also had to export the abseil targets, which seemed wrong to me
- find_package is then a point of customization: in vcpkg I'll just delete the FindModule.cmake for abseil and googletest, and use the built-in packages for vcpkg
- when abseil supports find_package directly (with a install/targets and a config-file) this approach will still work
- for
GTest
the native module is missing gmock, unfortunately :( - else I would be happy to use that - the only thing that does not work properly is that I have to cmake build the abseil and googletest targets before I can build the opencensus. This is because (I think) ExternaProject_Add runs at build time (not configure time) and for some reason cmake is not smart enough to build the dependencies on demand.
What I want is e.g. to build an opencensus test and automatically build abseil and gtest when they are required, while also supporting install/export for opencensus. I found an approach on stackoverflow, see the other reply. If this works, might that be okay? :)
FILE opencensus-config.cmake | ||
NAMESPACE opencensus:: | ||
DESTINATION share/opencensus | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: newline at end of file.
@@ -0,0 +1,278 @@ | |||
# Copyright 2018 The Cartographer Authors |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this the right copyright?
I'm a beginner with cmake, so feel free to correct me, but: The reason I did this to support find_package for the external dependencies was to be able to support install/export opencensus targets. I think that from cmake's pow the targets from the external dependencies were part of the build, and thus those targets had to be exported as well - I didn't think this made sense, so I wrote a kind of FindModule.cmake that also builds with ExternalProject_Add to support the current workflow. However: Also, to be able to support vcpkg, using find_package is kind of required (because the dependencies already exist as packages in vcpkg). (However, it is possible to patch opencensus cmake definition before building with vcpkg). |
@meastp: Regarding the out-of-tree example, I've updated https://github.com/census-ecosystem/opencensus-cpp-example to have a CMake version of the build system. Feel free to use that for testing from the point of view of a user of the opencensus-cpp library. |
Regarding https://stackoverflow.com/questions/44990964/how-to-perform-cmakefind-package-at-build-stage-only - this seems a little odd, but IMO much less bad than enumerating all of our dependencies' targets. What do other folks think? @coryan? @fancl20? @isaachier? |
I agree with this. How about if we add a knob via
|
CMake has historically been more interested in supporting existing patterns than setting a standard for third party dependencies. CMake 3.11 actually started supporting something similar to Bazel's |
@isaachier Thanks for the pointers to |
@coryan I imagine git submodules works just as well for this sort of thing, just a bit annoying to maintain correctly, especially with transitive dependencies. Actually, reading the documentation I linked above, I see there is a great example describing multiple projects that depend on each other and how the top-level project "wins" in terms of determining the transitive dependencies. @g-easy, if you like the idea of |
Thanks for the pointer, @isaachier ! CMake 3.11 looks a bit too new today but that will change: https://packages.debian.org/search?keywords=cmake Good to know we can just import the implementation though.
I'm sorry but I don't understand how this helps. I think we still want the user to specify how opencensus-cpp should find its dependencies, e.g. find_package vs download its own. |
@meastp: In the interest of keeping things moving, would you like to split out the install (+ hdrs) part of this PR into its own PR, while we keep discussing find_package + dependencies here? |
@g-easy, I agree it is not necessarily a good solution for users to define their own versions. However, it allows them to override the version easily from the top-level CMakeLists.txt if you follow the FetchContent convention. |
Sure, I'll work on headers and msvc nominmax while this is discussed, and submit them in their own PRs :) |
@g-easy what is the status of this? |
@bogdandrutu Short term: waiting for headers and MSVC changes to be split out. Longer term I'd like to see #256 (comment) implemented. |
Hi, I am working on separate changes, and experimented with replacing the current Then (because the amount of code is reduced) it will also be easier to implement an option to switch between |
This is a work-in-progress - do not merge yet.
See #244
Unfortunately, I have to add support for find_package for third-party dependencies and install in one go, because the targets from third party dependencies messed up the install/export for opencensus.
I have tested this in Visual Studio 2017 with the open folder native cmake support. I did also successfully run the opencensus tests.
Are these changes acceptable to you? Is this the proper way to support find_package in the dependencies?