Skip to content
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

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open

Conversation

bdice
Copy link
Collaborator

@bdice bdice commented Feb 23, 2025

This resolves #113.

I added a config and targets file, using the existing logic in CMakeLists.txt and nvtxImportedTargets.cmake. I tried to modify these files in a way that wouldn't break existing uses of the CMake code (which apparently only works via add_subdirectory).

I tested these changes with:

mkdir -p /tmp/nvtx-install
mkdir -p c/build

pushd c/build
rm -rf * && rm -rf /tmp/nvtx-install/* && cmake .. -DCMAKE_INSTALL_PREFIX=/tmp/nvtx-install && cmake --build . && cmake --install .
popd

mkdir -p c/examples/build

pushd c/examples/build
rm -rf * && cmake .. -DCMAKE_PREFIX_PATH=/tmp/nvtx-install && cmake --build .
popd

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:

  • Better CMake test code in CI, perhaps this can be done in a separate PR prior to merging this PR. The current code in c/examples should probably be in a directory of CMake tests somewhere.
  • Some kind of documentation changes? The NVTX docs appear to live in several places.
  • Review from two of the RAPIDS CMake folks: @robertmaynard, @vyasr, @KyleFromNVIDIA
  • Review from NVTX maintainers

@bdice bdice force-pushed the cmake-relocatable branch from c958486 to f4b5bbb Compare February 23, 2025 06:22
@bdice bdice changed the base branch from release-v3 to dev February 23, 2025 06:22
@bdice bdice force-pushed the cmake-relocatable branch from f4b5bbb to bc77913 Compare February 23, 2025 06:23
c/CMakeLists.txt Outdated
set(NVTX3_TARGETS_NOT_USING_IMPORTED ON)
include(nvtxImportedTargets.cmake)


set(INCLUDE_INSTALL_DIR "${CMAKE_INSTALL_INCLUDEDIR}/nvtx3")

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.

Copy link
Collaborator Author

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.

Copy link

@robertmaynard robertmaynard Feb 26, 2025

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 bdice marked this pull request as ready for review February 25, 2025 17:55
@bdice bdice requested a review from robertmaynard February 25, 2025 17:55
@bdice bdice self-assigned this Feb 25, 2025
@evanramos-nvidia
Copy link
Collaborator

@bdice: Thanks for taking this on.

@robertmaynard: Thanks for giving feedback.

We may need to adjust tests/CMakeLists.txt based on your improvements so that our CI passes. I'm not sure if there are ways that file and directory should make more idiomatic use of CMake. The one thing I want to avoid is users building the tests by mistake when they just want to use NVTX in their own project.

Copy link

@dominik-wojt-2311446 dominik-wojt-2311446 left a 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}")

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

Copy link

@robertmaynard robertmaynard Mar 10, 2025

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

@dominik-wojt-2311446 dominik-wojt-2311446 Mar 10, 2025

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.

@BwL1289
Copy link

BwL1289 commented Mar 12, 2025

@bdice thank you for working on this. We currently can't properly use nvtx in downstream packages (like torch) without this PR.

@bdice
Copy link
Collaborator Author

bdice commented Mar 12, 2025

@dominik-wojt-2311446 @BwL1289 Thanks! I will finish this up shortly.

@bdice bdice changed the title Attempt at making NVTX CMake relocatable. Make NVTX CMake relocatable. Mar 14, 2025
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.

5 participants