Skip to content

[CMake] Implement absl_cc_library as Bazel's cc_library #136

Merged
ahedberg merged 13 commits intoabseil:masterfrom
rongjiecomputer:cc_library
Oct 22, 2018
Merged

[CMake] Implement absl_cc_library as Bazel's cc_library #136
ahedberg merged 13 commits intoabseil:masterfrom
rongjiecomputer:cc_library

Conversation

@rongjiecomputer
Copy link
Contributor

absl_cc_library is a cc_library-like CMake function to replace absl_library and absl_header_library.

Please do not start any migration until absl_cc_binary and absl_cc_test are implemented in future PRs. I migrated a few targets just to showcase how absl_cc_library can be used.

#114

@JonathanDCohen PTAL

# 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).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe just PUBLIC? Or is that reserved by cmake?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think just PUBLIC is fine

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

#
# Note:
#
# By default, absl_cc_library will always create a library named absl_${NAME},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

${ARGN}
)

if (NOT ABSL_CC_LIB_TESTONLY OR BUILD_TESTING)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be ABSL_RUN_TESTS?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

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")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes we can! Thanks for the reminder. Done.

${STRINGS_PUBLIC_LIBRARIES}
EXPORT_NAME
absl_cc_library(
NAME
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@JonathanDCohen
Copy link
Contributor

Thanks for this! It's been gathering dust on my todo list for a while

# 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).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think just PUBLIC is fine

@rongjiecomputer
Copy link
Contributor Author

I have no more changes to make until next round of review.

@rongjiecomputer
Copy link
Contributor Author

@JonathanDCohen Is there anything else I need to address?

@rongjiecomputer
Copy link
Contributor Author

Ping @JonathanDCohen

@JonathanDCohen
Copy link
Contributor

Sorry for the delay, this fell off of my radar

# absl_internal_awesome_lib # not "awesome_lib"!
# )
#
# If PUBLIC is set, absl_cc_library will also create an alias absl::${NAME}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why don't using "absl_" prefix for PUBLIC enable libraries ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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()

Copy link
Contributor Author

@rongjiecomputer rongjiecomputer Oct 10, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Copy link
Contributor

@Mizux Mizux Oct 10, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since:

  1. 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 add absl_string to one of their install(TARGETS ... (that's why so much people want install rule cf Step 6: Allow Abseil to be installed and used with find_package() #111).

  2. Twist:

An ALIAS target 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)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@rongjiecomputer
Copy link
Contributor Author

@Mizux Really appreciate your comments, keep them coming! I really need to go to sleep now, so don't wait for my response.

@Mizux
Copy link
Contributor

Mizux commented Oct 11, 2018

LGTM to me thanks @rongjiecomputer
@JonathanDCohen seems odd that PR don't have any CI job associated to them (you should strongly consider having Travis/Appveyor jobs than only kokoro ones)

Copy link
Contributor

@JonathanDCohen JonathanDCohen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good to me besides the small comment change. @Mizux look good for you?

@Mizux
Copy link
Contributor

Mizux commented Oct 15, 2018

Yes everything sound good to me.

note: I will rework #182 once this one is merged

@pifon2a
Copy link

pifon2a commented Oct 17, 2018

@JonathanDCohen is it ready to merge?

@ahedberg ahedberg merged commit 5fbde92 into abseil:master Oct 22, 2018
ahedberg pushed a commit that referenced this pull request Oct 22, 2018
--
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
ahedberg added a commit that referenced this pull request Oct 22, 2018
--
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
@rongjiecomputer rongjiecomputer deleted the cc_library branch October 23, 2018 00:16
Titaniapamy added a commit to Titaniapamy/abseil-cpp that referenced this pull request Jul 4, 2024
--
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
Titaniapamy added a commit to Titaniapamy/abseil-cpp that referenced this pull request Jul 4, 2024
--
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants