-
Notifications
You must be signed in to change notification settings - Fork 385
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: cmake option to skip building mock libraries #13673
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -253,37 +253,6 @@ function (google_cloud_cpp_add_gapic_library library display_name) | |
|
||
add_library(${library_alias} ALIAS ${library_target}) | ||
|
||
# Create a header-only library for the mocks. We use a CMake `INTERFACE` | ||
# library for these, a regular library would not work on macOS (where the | ||
# library needs at least one .o file). Unfortunately INTERFACE libraries are | ||
# a bit weird in that they need absolute paths for their sources. | ||
file( | ||
GLOB relative_mock_files | ||
RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" | ||
${mocks_globs}) | ||
list(SORT relative_mock_files) | ||
set(mock_files) | ||
foreach (file IN LISTS relative_mock_files) | ||
# We use a generator expression per the recommendation in: | ||
# https://stackoverflow.com/a/62465051 | ||
list(APPEND mock_files | ||
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/${file}>") | ||
endforeach () | ||
add_library(${mocks_target} INTERFACE) | ||
target_sources(${mocks_target} INTERFACE ${mock_files}) | ||
target_link_libraries( | ||
${mocks_target} INTERFACE ${library_alias} GTest::gmock_main | ||
GTest::gmock GTest::gtest) | ||
set_target_properties(${mocks_target} PROPERTIES EXPORT_NAME | ||
${library_alias}_mocks) | ||
target_include_directories( | ||
${mocks_target} | ||
INTERFACE $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}> | ||
$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}> | ||
$<INSTALL_INTERFACE:include>) | ||
target_compile_options(${mocks_target} | ||
INTERFACE ${GOOGLE_CLOUD_CPP_EXCEPTIONS_FLAG}) | ||
|
||
# Get the destination directories based on the GNU recommendations. | ||
include(GNUInstallDirs) | ||
|
||
|
@@ -349,7 +318,40 @@ function (google_cloud_cpp_add_gapic_library library display_name) | |
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${library_target}" | ||
COMPONENT google_cloud_cpp_development) | ||
|
||
google_cloud_cpp_install_mocks("${library}" "${display_name}") | ||
if (GOOGLE_CLOUD_CPP_WITH_MOCKS) | ||
# Create a header-only library for the mocks. We use a CMake `INTERFACE` | ||
# library for these, a regular library would not work on macOS (where | ||
# the library needs at least one .o file). Unfortunately INTERFACE | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No action needed: I wonder if this is still true now that we use CMake >= 3.13 .. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ack, I don't know the answer and I don't have easy access to a mac. |
||
# libraries are a bit weird in that they need absolute paths for their | ||
# sources. | ||
file( | ||
GLOB relative_mock_files | ||
RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" | ||
${mocks_globs}) | ||
list(SORT relative_mock_files) | ||
set(mock_files) | ||
foreach (file IN LISTS relative_mock_files) | ||
# We use a generator expression per the recommendation in: | ||
# https://stackoverflow.com/a/62465051 | ||
list(APPEND mock_files | ||
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/${file}>") | ||
endforeach () | ||
add_library(${mocks_target} INTERFACE) | ||
target_sources(${mocks_target} INTERFACE ${mock_files}) | ||
target_link_libraries( | ||
${mocks_target} INTERFACE ${library_alias} GTest::gmock_main | ||
GTest::gmock GTest::gtest) | ||
set_target_properties(${mocks_target} PROPERTIES EXPORT_NAME | ||
${library_alias}_mocks) | ||
target_include_directories( | ||
${mocks_target} | ||
INTERFACE $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}> | ||
$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}> | ||
$<INSTALL_INTERFACE:include>) | ||
target_compile_options(${mocks_target} | ||
INTERFACE ${GOOGLE_CLOUD_CPP_EXCEPTIONS_FLAG}) | ||
google_cloud_cpp_install_mocks("${library}" "${display_name}") | ||
endif () | ||
|
||
# ${library_alias} must be defined before we can add the samples. | ||
if (BUILD_TESTING AND GOOGLE_CLOUD_CPP_ENABLE_CXX_EXCEPTIONS) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
TIL, you can use
NOT condition
incmake_dependent_option
. UsingON ... ON
looks weird. I think you want something like this:That is, the option always exists, but its default value is based on a generator expression.
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 don't think I want that.
The default value should always be
ON
, regardless ofBUILD_TESTING
. But ifBUILD_TESTING
isON
, thenGOOGLE_CLOUD_CPP_WITH_MOCKS
must beON
(because our unit tests depend on the mock libraries). That is what I am enforcing with theON
...ON
.I think this is doing what I want. Adding a debug print statement after the option is defined: