Skip to content

Commit 747bcb4

Browse files
authored
Fixes for installing PDB files in debug builds (AcademySoftwareFoundation#2464)
For our debug builds on Windows, I was noticing that the PDB files that carry the debug information did not appear to be getting installed along with the binaries. The farthest back I could trace the addition of the PDB handling was from this commit about six years ago, which looks to have been included in [v1.36.3](https://github.com/AcademySoftwareFoundation/MaterialX/releases/tag/v1.36.3) as its first release: AcademySoftwareFoundation@749bb42#diff-3b1f19b41d696ced243017e7d2eaffe91809d77dbce2e91aa6d4f112dc809d53 The pattern used to assemble the path to the PDB files in the build directory back then looked like this: ``` "${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_BUILD_TYPE}/MaterialXCore.pdb" ``` And that's effectively still what they look like today. That does not seem to produce the path where they are currently being generated though. In my builds (using CMake version `3.31.6-msvc6`, currently bundled with the latest Visual Studio), that expression evaluates to this: ``` <build dir>/source/MaterialXCore//MaterialXCore.pdb ``` While a debug build of static libraries actually generates the file here: ``` <build dir>/lib/Debug/MaterialXCore_d.pdb ``` So there are a few problems with the expression currently being used: - Static builds now appear to create the PDB files under `CMAKE_LIBRARY_OUTPUT_DIRECTORY` (`<build dir>/lib`) as opposed to `CMAKE_CURRENT_BINARY_DIR` (`<build dir>/source/MaterialXCore`). - `CMAKE_BUILD_TYPE` is not necessarily set in all cases, particularly for "multi-config" generators that can support multiple configs in a single generation pass (as is the case for Visual Studio). The `CONFIG` configuration expression looks to be what we want instead. - More detail on this here: - https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html - https://www.kitware.com/cmake-and-the-default-build-type/ - https://cmake.org/cmake/help/latest/manual/cmake-generator-expressions.7.html#genex:CONFIG - Use of additional options that affect the output library name (in my case, `CMAKE_DEBUG_POSTFIX=_d`) are not accounted for. We can use the `TARGET_FILE_BASE_NAME` generator expression to get at the fully decorated output name. There's also different behavior between static and shared library builds. With PDB files on Windows, there seems to be a distinction between those that are generated by the linker for use with shared libraries and executables, and those that are generated by the compiler for use with static libraries. CMake provides the `TARGET_PDB_FILE` generator expression to access the path to the former flavor, but there is no equivalent `TARGET_COMPILE_PDB_FILE` for the latter. There is some additional detail and discussion on this here: https://cmake.org/cmake/help/latest/prop_tgt/PDB_NAME.html https://gitlab.kitware.com/cmake/cmake/-/issues/16935 So for static builds (`MATERIALX_BUILD_SHARED_LIBS=OFF`), we still need to produce a path ourselves, but the changes mentioned above should now correctly identify where that file lives in the build directory. For shared library builds (`MATERIALX_BUILD_SHARED_LIBS=ON`) and executables, we can just use `TARGET_PDB_FILE`. Finally, since compiler-generated PDB files are associated with static libraries, we direct those to install into `MATERIALX_INSTALL_LIB_PATH` (usually just `lib`), while linker-generated PDB files are instead directed into `MATERIALX_INSTALL_BIN_PATH` (usually just `bin`).
1 parent 7b91198 commit 747bcb4

3 files changed

Lines changed: 17 additions & 6 deletions

File tree

CMakeLists.txt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -418,8 +418,15 @@ function(mx_add_library MATERIALX_MODULE_NAME)
418418
FILE_SET mxHeaders DESTINATION ${MATERIALX_INSTALL_INCLUDE_PATH})
419419
endif()
420420
421-
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_BUILD_TYPE}/${MATERIALX_MODULE_NAME}.pdb"
422-
DESTINATION "${MATERIALX_INSTALL_LIB_PATH}/" OPTIONAL)
421+
if(MSVC)
422+
if(MATERIALX_BUILD_SHARED_LIBS)
423+
install(FILES $<TARGET_PDB_FILE:${MATERIALX_MODULE_NAME}>
424+
DESTINATION ${MATERIALX_INSTALL_BIN_PATH} OPTIONAL)
425+
else()
426+
install(FILES "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/$<CONFIG>/$<TARGET_FILE_BASE_NAME:${MATERIALX_MODULE_NAME}>.pdb"
427+
DESTINATION ${MATERIALX_INSTALL_LIB_PATH} OPTIONAL)
428+
endif()
429+
endif()
423430
endif()
424431
425432
# Pass TARGET_NAME back to call site, so the caller can modify the build target.

source/MaterialXGraphEditor/CMakeLists.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,5 +94,7 @@ target_link_libraries(
9494
install(TARGETS MaterialXGraphEditor
9595
EXPORT MaterialX
9696
RUNTIME DESTINATION ${MATERIALX_INSTALL_BIN_PATH})
97-
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_BUILD_TYPE}/MaterialXGraphEditor.pdb"
98-
DESTINATION ${MATERIALX_INSTALL_BIN_PATH} OPTIONAL)
97+
if(MSVC)
98+
install(FILES $<TARGET_PDB_FILE:MaterialXGraphEditor>
99+
DESTINATION ${MATERIALX_INSTALL_BIN_PATH} OPTIONAL)
100+
endif()

source/MaterialXView/CMakeLists.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,5 +142,7 @@ set_target_properties(
142142
install(TARGETS MaterialXView
143143
EXPORT MaterialX
144144
RUNTIME DESTINATION ${MATERIALX_INSTALL_BIN_PATH})
145-
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_BUILD_TYPE}/MaterialXView.pdb"
146-
DESTINATION ${MATERIALX_INSTALL_BIN_PATH} OPTIONAL)
145+
if(MSVC)
146+
install(FILES $<TARGET_PDB_FILE:MaterialXView>
147+
DESTINATION ${MATERIALX_INSTALL_BIN_PATH} OPTIONAL)
148+
endif()

0 commit comments

Comments
 (0)