-
Notifications
You must be signed in to change notification settings - Fork 51
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
Make NVTX CMake relocatable. #114
base: dev
Are you sure you want to change the base?
Conversation
c958486
to
f4b5bbb
Compare
f4b5bbb
to
bc77913
Compare
c/CMakeLists.txt
Outdated
set(NVTX3_TARGETS_NOT_USING_IMPORTED ON) | ||
include(nvtxImportedTargets.cmake) | ||
|
||
|
||
set(INCLUDE_INSTALL_DIR "${CMAKE_INSTALL_INCLUDEDIR}/nvtx3") |
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.
All the install rules should be guarded behind a check that looks like:
set(NVTX3_IS_ROOT_PROJECT .....)
option(NVTX3_INSTALL ${NVTX3_IS_ROOT_PROJECT})
if(NVTX3_INSTALL)
....
endif()
That way existing projects don't start installing nvtx3 where previously they didn't.
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 guessed that the .....
was supposed to be this condition:
set(NVTX3_IS_ROOT_PROJECT "${CMAKE_SOURCE_DIR}" STREQUAL "${PROJECT_SOURCE_DIR}")
If that's correct, I think this is fixed.
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.
You can use PROJECT_IS_TOP_LEVEL
https://cmake.org/cmake/help/latest/variable/PROJECT_IS_TOP_LEVEL.html
If we can rely on CMake 3.21 otherwise you generally check CMAKE_PROJECT_NAME
matches our project name ( PROJECT_NAME
)
@bdice: Thanks for taking this on. @robertmaynard: Thanks for giving feedback. We may need to adjust |
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 was half way trying to do the same at my end. The lack of proper cmake scripts for find_package
is really annoying.
Your work is really appreciated.
You might want to add this snippet to README.md
:
## Use installed version of NVTX
If NVTX is installed in your system, you can use it with a standard `find_package` procedure.
```cmake
find_package(nvtx3 REQUIRED)
add_executable(some_c_program main.c)
target_link_libraries(some_c_program PRIVATE nvtx3::nvtx3-c)
add_executable(some_cpp_program main.cpp)
target_link_libraries(some_cpp_program PRIVATE nvtx3::nvtx3-cpp)
```
project(nvtx3 VERSION 3.1.1) | ||
|
||
# Determine if this is the root project | ||
set(NVTX3_IS_ROOT_PROJECT "${CMAKE_SOURCE_DIR}" STREQUAL "${PROJECT_SOURCE_DIR}") |
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 am afraid cmake will not accept the if
syntax with set
. At least version 3.22.1 I tried it with did not.
This fixed the issue at my end:
if("${CMAKE_SOURCE_DIR}" STREQUAL "${PROJECT_SOURCE_DIR}")
set(NVTX3_IS_ROOT_PROJECT TRUE)
else()
set(NVTX3_IS_ROOT_PROJECT FALSE)
endif()
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.
You are correct. CMake doesn't support the original syntax.
I would use:
set(NVTX3_IS_ROOT_PROJECT FALSE)
if("${CMAKE_SOURCE_DIR}" STREQUAL "${PROJECT_SOURCE_DIR}")
set(NVTX3_IS_ROOT_PROJECT TRUE)
endif()
set_target_properties(nvtx3-cpp PROPERTIES VERSION ${NVTX3_VERSION}) | ||
target_link_libraries(nvtx3-cpp INTERFACE nvtx3-c) | ||
|
||
# Install the targets | ||
install(TARGETS nvtx3-c nvtx3-cpp EXPORT nvtx3-targets) |
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 think this line should be moved to CMakeLists.txt
and protected with NVTX3_INSTALL
.
It fails when OPTIONALLY_IMPORTED
is IMPORTED
.
@bdice thank you for working on this. We currently can't properly use nvtx in downstream packages (like torch) without this PR. |
@dominik-wojt-2311446 @BwL1289 Thanks! I will finish this up shortly. |
This resolves #113.
I added a config and targets file, using the existing logic in
CMakeLists.txt
andnvtxImportedTargets.cmake
. I tried to modify these files in a way that wouldn't break existing uses of the CMake code (which apparently only works viaadd_subdirectory
).I tested these changes with:
I think the NVTX relocatable install in
/tmp/nvtx-install
looks like it should work, based on my knowledge of how rapids-cmake works (https://github.com/rapidsai/rapids-cmake/blob/branch-25.04/rapids-cmake/cpm/nvtx3.cmake). The example code builds and runs with the appropriate include directories.To move this PR forward, this may need some or all of the following:
c/examples
should probably be in a directory of CMake tests somewhere.