[CMake] Implement absl_cc_library as Bazel's cc_library #136
[CMake] Implement absl_cc_library as Bazel's cc_library #136ahedberg merged 13 commits intoabseil:masterfrom
Conversation
CMake/AbseilHelpers.cmake
Outdated
| # COPTS: List of private compile options | ||
| # DEFINES: List of public defines | ||
| # LINKOPTS: List of link options | ||
| # VISIBILITY_PUBLIC: Add this so that this library will be exported under absl:: (see Note). |
There was a problem hiding this comment.
Maybe just PUBLIC? Or is that reserved by cmake?
There was a problem hiding this comment.
PUBLIC is not reserved by CMake, but I just thought that VISIBILITY_PUBLIC is easier to understand for Abseil team since Bazel uses //visibility:public to mean that "targets outside Abseil can depend on it". I can change it to PUBLIC if you think that is better.
There was a problem hiding this comment.
I think just PUBLIC is fine
CMake/AbseilHelpers.cmake
Outdated
| # | ||
| # Note: | ||
| # | ||
| # By default, absl_cc_library will always create a library named absl_${NAME}, |
There was a problem hiding this comment.
Here's an idea, while we're here. If VISIBILITY_PUBLIC is not set, the prefix should be absl_internal_blah, otherwise it should be absl::blah. What do you think? I'm okay with "internal" appearing a bunch of times in the name.
There was a problem hiding this comment.
absl_internal_ preffix will reduce the chance of name collision, but we might still face name collision within Abseil itself (some targets have name that are too general like test_util in //absl/time package). I have a longer response at https://groups.google.com/forum/#!topic/abseil-io/_Pq2NuCuYcc
I am settling at absl_ preffix for now since Abseil's CMake files use absl_ preffix too (e.g. absl_throw_delegate), so I don't have to do too much manual editing when showing some possible migrations in this PR.
There was a problem hiding this comment.
I'm not really worried about name collisions as much as I'm trying to make it as inconvenient as possible to rely on abseil internals, since we have no true hiding mechanism as we do in Bazel. Does that make more sense?
CMake/AbseilHelpers.cmake
Outdated
| ${ARGN} | ||
| ) | ||
|
|
||
| if (NOT ABSL_CC_LIB_TESTONLY OR BUILD_TESTING) |
There was a problem hiding this comment.
should this be ABSL_RUN_TESTS?
There was a problem hiding this comment.
You mean replace BUILD_TESTING with ABSL_RUN_TESTS? There is no difference actually.
When ABSL_RUN_TESTS is not set, CTest won't be included and BUILD_TESTING won't be specified. Un-specified variable has default value of "false" when accessed.
There was a problem hiding this comment.
I realized that BUILD_TESTING is global, not project-specific, so depending on BUILD_TESTING will force consumer to run Abseil tests when they just want to run their own tests. Done.
CMake/AbseilHelpers.cmake
Outdated
| if(ABSL_CC_LIB_SRCS_LEN) | ||
| add_library(${_NAME} STATIC ${ABSL_CC_LIB_SRCS} ${ABSL_CC_LIB_HDRS}) | ||
| else() | ||
| set(__dummy_header_only_lib_file "${CMAKE_CURRENT_BINARY_DIR}/${_NAME}_header_only_dummy.cc") |
There was a problem hiding this comment.
Let's take the chance to improve header-only libs while we're here. Can we not make header-only libs INTERFACE libraries? Then we just declare their deps as normal and the headers get picked up by ABSL_COMMON_INCLUDE_DIRS.
There was a problem hiding this comment.
Yes we can! Thanks for the reminder. Done.
absl/strings/CMakeLists.txt
Outdated
| ${STRINGS_PUBLIC_LIBRARIES} | ||
| EXPORT_NAME | ||
| absl_cc_library( | ||
| NAME |
There was a problem hiding this comment.
I would rather we didn't immediately migrate something so widely used as strings. I think the exception-safety testing is fine, it has a small footprint. If you want another trial case, let's try something internal instead.
|
Thanks for this! It's been gathering dust on my todo list for a while |
c142746 to
162c1a9
Compare
CMake/AbseilHelpers.cmake
Outdated
| # COPTS: List of private compile options | ||
| # DEFINES: List of public defines | ||
| # LINKOPTS: List of link options | ||
| # VISIBILITY_PUBLIC: Add this so that this library will be exported under absl:: (see Note). |
There was a problem hiding this comment.
I think just PUBLIC is fine
|
I have no more changes to make until next round of review. |
8849b05 to
59a1f35
Compare
|
@JonathanDCohen Is there anything else I need to address? |
|
Ping @JonathanDCohen |
|
Sorry for the delay, this fell off of my radar |
59a1f35 to
a76ceed
Compare
CMake/AbseilHelpers.cmake
Outdated
| # absl_internal_awesome_lib # not "awesome_lib"! | ||
| # ) | ||
| # | ||
| # If PUBLIC is set, absl_cc_library will also create an alias absl::${NAME} |
There was a problem hiding this comment.
why don't using "absl_" prefix for PUBLIC enable libraries ?
There was a problem hiding this comment.
It is already there. The following code will create absl_strings and absl::strings (alias). CMake user should depend on absl::strings only.
absl_cc_library(
NAME
strings
PUBLIC
)
There was a problem hiding this comment.
no it will create absl_internal_strings and absl::strings line 125
you should have something like this:
if (ABSL_CC_LIB_PUBLIC)
set(_NAME "absl_${ABSL_CC_LIB_NAME}")
else()
set(_NAME "absl_internal_${ABSL_CC_LIB_NAME}")
endif()
There was a problem hiding this comment.
no it will create absl_internal_strings and absl::strings line 125
Ah, I forgot that the prefix has changed to absl_internal_ (it was initially just absl_, but Jonathan requested to always use absl_internal_ for non-public targets and I didn't think of creating exception for public targets).
Is it better for user to use absl_strings instead of absl::strings? Because I want to make it clear to CMake user that only absl:: can be depended outside Abseil. (No strong objection here, just want your opinion).
There was a problem hiding this comment.
Since:
-
Abseil force user to use abseil-cpp through
add_subdirectory(abseil-cpp),
So meta project who want to add an install rules will fail at configure time if they don't addabsl_stringto one of theirinstall(TARGETS ...(that's why so much people want install rule cf Step 6: Allow Abseil to be installed and used with find_package() #111). -
Twist:
An
ALIAStarget may not be installed or exported.
src: https://cmake.org/cmake/help/latest/command/add_library.html#alias-libraries
-> 1 + 2 -> you can't integrate abseil-cpp in your project (and only use absl::) unless you'll never install your awesome target...
(i.e. unless target dependency is an imported target, it must be part of your install, aka avoid to install half target in the build tree)
note: for GTEST etc it works to use add_subdirectory() tricks since usually no one install test executables (and all their dependencies needed and build in the build_dir aka gtest)
|
@Mizux Really appreciate your comments, keep them coming! I really need to go to sleep now, so don't wait for my response. |
|
LGTM to me thanks @rongjiecomputer |
JonathanDCohen
left a comment
There was a problem hiding this comment.
looks good to me besides the small comment change. @Mizux look good for you?
|
Yes everything sound good to me. note: I will rework #182 once this one is merged |
|
@JonathanDCohen is it ready to merge? |
-- 4e043a11b4c10a24e84046827ee16f47e11e35cc by Abseil Team <absl-team@google.com>: Merge of #136 PiperOrigin-RevId: 218197648 -- e61f06e1e601061a443feaa8c5207c52437bd641 by Abseil Team <absl-team@google.com>: Don't include <iostream> into int128, it's wasteful Including iostream emits a global constructor for initializing std::cout and friends, which isn't actually used by this file. PiperOrigin-RevId: 218156386 -- 8a6c82396e4c956be7f285328aec131cb4965f16 by Xiaoyi Zhang <zhangxy@google.com>: Fix MSVC compiler warnings on discarding return values of functions with 'nodiscard' attribute. PiperOrigin-RevId: 217883401 -- abf3e3a0f22bc4070df9dbc9a4ef4d883ed686bf by Tom Manshreck <shreck@google.com>: Update public README to add new libraries PiperOrigin-RevId: 217879399 -- 43b3b420a4e861711abbfbd497b8f2b3de17ec8c by Abseil Team <absl-team@google.com>: Import of CCTZ from GitHub. PiperOrigin-RevId: 217780963 -- 1c8831947ca6a65a63842e6bd5f37a7c102a4e1b by Abseil Team <absl-team@google.com>: Fix typo in a comment (missing comma in usage example). PiperOrigin-RevId: 217776645 GitOrigin-RevId: 4e043a11b4c10a24e84046827ee16f47e11e35cc Change-Id: I8999ae928da7a0030b4ecfd8d13da8522fdd013a
-- 4e043a11b4c10a24e84046827ee16f47e11e35cc by Abseil Team <absl-team@google.com>: Merge of #136 PiperOrigin-RevId: 218197648 -- e61f06e1e601061a443feaa8c5207c52437bd641 by Abseil Team <absl-team@google.com>: Don't include <iostream> into int128, it's wasteful Including iostream emits a global constructor for initializing std::cout and friends, which isn't actually used by this file. PiperOrigin-RevId: 218156386 -- 8a6c82396e4c956be7f285328aec131cb4965f16 by Xiaoyi Zhang <zhangxy@google.com>: Fix MSVC compiler warnings on discarding return values of functions with 'nodiscard' attribute. PiperOrigin-RevId: 217883401 -- abf3e3a0f22bc4070df9dbc9a4ef4d883ed686bf by Tom Manshreck <shreck@google.com>: Update public README to add new libraries PiperOrigin-RevId: 217879399 -- 43b3b420a4e861711abbfbd497b8f2b3de17ec8c by Abseil Team <absl-team@google.com>: Import of CCTZ from GitHub. PiperOrigin-RevId: 217780963 -- 1c8831947ca6a65a63842e6bd5f37a7c102a4e1b by Abseil Team <absl-team@google.com>: Fix typo in a comment (missing comma in usage example). PiperOrigin-RevId: 217776645 GitOrigin-RevId: 4e043a11b4c10a24e84046827ee16f47e11e35cc Change-Id: I8999ae928da7a0030b4ecfd8d13da8522fdd013a
-- 4e043a11b4c10a24e84046827ee16f47e11e35cc by Abseil Team <absl-team@google.com>: Merge of abseil/abseil-cpp#136 PiperOrigin-RevId: 218197648 -- e61f06e1e601061a443feaa8c5207c52437bd641 by Abseil Team <absl-team@google.com>: Don't include <iostream> into int128, it's wasteful Including iostream emits a global constructor for initializing std::cout and friends, which isn't actually used by this file. PiperOrigin-RevId: 218156386 -- 8a6c82396e4c956be7f285328aec131cb4965f16 by Xiaoyi Zhang <zhangxy@google.com>: Fix MSVC compiler warnings on discarding return values of functions with 'nodiscard' attribute. PiperOrigin-RevId: 217883401 -- abf3e3a0f22bc4070df9dbc9a4ef4d883ed686bf by Tom Manshreck <shreck@google.com>: Update public README to add new libraries PiperOrigin-RevId: 217879399 -- 43b3b420a4e861711abbfbd497b8f2b3de17ec8c by Abseil Team <absl-team@google.com>: Import of CCTZ from GitHub. PiperOrigin-RevId: 217780963 -- 1c8831947ca6a65a63842e6bd5f37a7c102a4e1b by Abseil Team <absl-team@google.com>: Fix typo in a comment (missing comma in usage example). PiperOrigin-RevId: 217776645 GitOrigin-RevId: 4e043a11b4c10a24e84046827ee16f47e11e35cc Change-Id: I8999ae928da7a0030b4ecfd8d13da8522fdd013a
-- 4e043a11b4c10a24e84046827ee16f47e11e35cc by Abseil Team <absl-team@google.com>: Merge of abseil/abseil-cpp#136 PiperOrigin-RevId: 218197648 -- e61f06e1e601061a443feaa8c5207c52437bd641 by Abseil Team <absl-team@google.com>: Don't include <iostream> into int128, it's wasteful Including iostream emits a global constructor for initializing std::cout and friends, which isn't actually used by this file. PiperOrigin-RevId: 218156386 -- 8a6c82396e4c956be7f285328aec131cb4965f16 by Xiaoyi Zhang <zhangxy@google.com>: Fix MSVC compiler warnings on discarding return values of functions with 'nodiscard' attribute. PiperOrigin-RevId: 217883401 -- abf3e3a0f22bc4070df9dbc9a4ef4d883ed686bf by Tom Manshreck <shreck@google.com>: Update public README to add new libraries PiperOrigin-RevId: 217879399 -- 43b3b420a4e861711abbfbd497b8f2b3de17ec8c by Abseil Team <absl-team@google.com>: Import of CCTZ from GitHub. PiperOrigin-RevId: 217780963 -- 1c8831947ca6a65a63842e6bd5f37a7c102a4e1b by Abseil Team <absl-team@google.com>: Fix typo in a comment (missing comma in usage example). PiperOrigin-RevId: 217776645 GitOrigin-RevId: 4e043a11b4c10a24e84046827ee16f47e11e35cc Change-Id: I8999ae928da7a0030b4ecfd8d13da8522fdd013a
absl_cc_libraryis acc_library-like CMake function to replaceabsl_libraryandabsl_header_library.Please do not start any migration until
absl_cc_binaryandabsl_cc_testare implemented in future PRs. I migrated a few targets just to showcase howabsl_cc_librarycan be used.#114
@JonathanDCohen PTAL