Skip to content

Bug Solve : CMake - GTest does not respect user#4961

Open
satasiyakrish1 wants to merge 3 commits into
google:mainfrom
satasiyakrish1:main
Open

Bug Solve : CMake - GTest does not respect user#4961
satasiyakrish1 wants to merge 3 commits into
google:mainfrom
satasiyakrish1:main

Conversation

@satasiyakrish1
Copy link
Copy Markdown

Bug Fix: CMake — GTest does not respect user CMAKE_<ARTIFACT>_OUTPUT_DIRECTORY

Closes #4957


Problem

CMake exposes a family of variables that let users control where build artifacts are placed:

Variable Controls
CMAKE_RUNTIME_OUTPUT_DIRECTORY Executables / DLLs
CMAKE_ARCHIVE_OUTPUT_DIRECTORY Static libraries
CMAKE_LIBRARY_OUTPUT_DIRECTORY Shared libraries (non-DLL)
CMAKE_PDB_OUTPUT_DIRECTORY Linker PDB files
CMAKE_COMPILE_PDB_OUTPUT_DIRECTORY Compiler PDB files

cxx_library_with_type in googletest/cmake/internal_utils.cmake was overriding all five of these by setting the corresponding target properties to hardcoded paths under ${CMAKE_BINARY_DIR}/bin and ${CMAKE_BINARY_DIR}/lib.

Because target-level properties take precedence over CMAKE_* cache variables, any user-supplied -DCMAKE_ARCHIVE_OUTPUT_DIRECTORY=… (or equivalent) was silently ignored, forcing GTest artifacts into lib/ regardless.


Changes

googletest/cmake/internal_utils.cmake

1. cxx_library_with_type — removed hardcoded *_OUTPUT_DIRECTORY target properties

The entire set_target_properties(… RUNTIME_OUTPUT_DIRECTORY … ARCHIVE_OUTPUT_DIRECTORY …) block was removed.
CMake's documented behaviour: when these target properties are left unset, the build system automatically inherits their values from the corresponding CMAKE_<ARTIFACT>_OUTPUT_DIRECTORY variable. This is exactly the behaviour users expect.

# BEFORE — hardcoded, ignores user settings
set_target_properties(${name}
  PROPERTIES
  RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin"
  LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib"
  ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib"
  PDB_OUTPUT_DIRECTORY     "${CMAKE_BINARY_DIR}/bin"
  COMPILE_PDB_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib")

# AFTER — properties left unset; CMAKE_* variables are respected automatically

2. install_project — fixed broken PDB install path (MSVC)

The old PDB install code read the PDB_OUTPUT_DIRECTORY target property to build the install source path:

# BEFORE — reads a property that is now unset → yields "…-NOTFOUND/…"
get_target_property(t_pdb_output_directory ${t} PDB_OUTPUT_DIRECTORY)
install(FILES "${t_pdb_output_directory}/\${CMAKE_INSTALL_CONFIG_NAME}/….pdb" …)

Since that property is no longer set on the target, the stale lookup would have produced a *-NOTFOUND path, silently skipping PDB installation on MSVC.
Fixed by using a generator expression that resolves the actual binary output directory at install time:

# AFTER — resolves correctly regardless of what CMAKE_*_OUTPUT_DIRECTORY is set to
install(FILES
  "$<TARGET_FILE_DIR:${t}>/$<$<CONFIG:Debug>:${t_pdb_name_debug}>$<$<NOT:$<CONFIG:Debug>>:${t_pdb_name}>.pdb"OPTIONAL)

Why this is safe / no regression

  • The hardcoded paths (bin/, lib/) were already CMake's own defaults — so builds that do not set CMAKE_*_OUTPUT_DIRECTORY produce identical output as before.
  • Builds that do set those variables will now correctly have GTest artifacts placed in the user-specified directories.
  • The PDB name properties (PDB_NAME, COMPILE_PDB_NAME, etc.) are kept — they control the filename, not the directory, and are unrelated to this bug.

Testing

Reproduce the original issue and verify the fix:

cmake -B build \
  -DCMAKE_ARCHIVE_OUTPUT_DIRECTORY=/tmp/my_libs \
  -DCMAKE_RUNTIME_OUTPUT_DIRECTORY=/tmp/my_bin
cmake --build build

# Before fix: GTest .a files still appear in build/lib/
# After fix:  GTest .a files appear in /tmp/my_libs/ ✓

@google-cla
Copy link
Copy Markdown

google-cla Bot commented Apr 19, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: CMake - GTest does not respect user CMAKE_<ARTIFACT>_OUTPUT_DIRECTORY

1 participant