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

Layout set_property("cmake_build_modules") #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,7 @@ install(
DESTINATION visualizers
)

file(
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One of the issues is that the same file with the fixed path cannot work for 2 different layouts, the "build/editable" layout, and the "package" layout.

I moved this to the "source" tree, so I could keep the same relative position of the add_visualizers.cmake file and the lib.natvis file. Another strategy would be to have different files: one file written at "build" time, but then, do not copy that file as-is to the package folder at install() time but instead generate a new one with an updated relative position.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't quite solve my problem, because now I have to define the add_visualizers.cmake script manually. Instead I would like to automate the process. That was what the CMake script file invocation represents. In reality the scripts to generate it are somewhat more involved. I can generate the file using Conan too if that's more appropriate, but it seemed to me that I probably shouldn't generate files in package_info.

Copy link
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 process could be more complicated, this is why I suggested the above. The main issue is that the relative path between the add_visualizers.cmake and the nat.vis file should be correct in both places, and this cannot happen if you use exactly the same file copied at install() time. So the automated solution, if you want to support both the create and the editable different layouts, would be to make sure that 2 different files are generated:

  • One add_visualizers.cmake that you create at build() time, with the relative path in the "build" or editable layout
  • Another add_visualizers.cmake that you create at package() time, with the relative path of the "package" layout
  • It is not necessary to create the files in package_info() only in build() and package()
  • If it is easier, the add_visualizers.cmake could be "patched" at package() time, doing a copy to the package folder, then patching the relative paths (it could be already an easy to patch variable that is reused in the script)

Please let me know if this helps.

WRITE "${CMAKE_CURRENT_BINARY_DIR}/cmake_build_modules/add_visualizers.cmake"
# This doesn't work in editable mode either because the relative path is different:
"target_sources(my::lib INTERFACE \${CMAKE_CURRENT_LIST_DIR}/../visualizers/my/lib.natvis)\n"
)

install(
FILES "${CMAKE_CURRENT_BINARY_DIR}/cmake_build_modules/add_visualizers.cmake"
FILES "${CMAKE_CURRENT_LIST_DIR}/cmake_build_modules/add_visualizers.cmake"
DESTINATION "cmake_build_modules"
)
2 changes: 2 additions & 0 deletions lib/cmake_build_modules/add_visualizers.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
message(STATUS "Adding target_sources() in add_visualizers.cmake")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved this here, so the relative position of add_visualizers.cmake and nat.vis was the same.

target_sources(my::lib INTERFACE ${CMAKE_CURRENT_LIST_DIR}/../visualizers/my/lib.natvis)
17 changes: 3 additions & 14 deletions lib/conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@ class package(ConanFile):
"include/*",
"source/*",
"visualizers/*",
"cmake_build_modules/*"
)

def layout(self):
cmake_layout(self)

self.cpp.build.builddirs.append("cmake_build_modules")
self.cpp.package.builddirs.append("cmake_build_modules")
self.cpp.source.set_property("cmake_build_modules", ["cmake_build_modules/add_visualizers.cmake"])

def build(self):
cmake = CMake(self)
Expand All @@ -34,14 +33,4 @@ def package(self):

def package_info(self):
self.cpp_info.set_property("cmake_target_name", "my::lib")

# Find and add all scripts in package_folder/cmake_build_modules/
cmake_build_modules_dir = os.path.join(self.package_folder, "cmake_build_modules")
if os.path.isdir(cmake_build_modules_dir):
cmake_build_modules = self.cpp_info.get_property("cmake_build_modules") or []

for entry in os.scandir(cmake_build_modules_dir):
cmake_build_modules.append(os.path.join(cmake_build_modules_dir, entry.name))

if len(cmake_build_modules) > 0:
self.cpp_info.set_property("cmake_build_modules", cmake_build_modules)
self.cpp_info.set_property("cmake_build_modules", ["cmake_build_modules/add_visualizers.cmake"])