Skip to content
Open
Changes from 3 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
46 changes: 45 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,51 @@ target_compile_features(GSL INTERFACE "cxx_std_14")
# Setup include directory
add_subdirectory(include)

target_sources(GSL INTERFACE $<BUILD_INTERFACE:${GSL_SOURCE_DIR}/GSL.natvis>)
file(GLOB GSL_HEADER_FILES
RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}/include"
"${CMAKE_CURRENT_SOURCE_DIR}/include/gsl/*"
)
list(SORT GSL_HEADER_FILES)
Copy link
Member

Choose a reason for hiding this comment

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

nit: According to the docs, the result of file(GLOB, ... is already sorted.

https://cmake.org/cmake/help/latest/command/file.html#filesystem


# Build/install interface lists ensure CMake is happy when exporting headers via
# INTERFACE sources. Absolute paths would be rejected by newer CMake versions
Copy link
Member

Choose a reason for hiding this comment

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

nit: "Newer" may be true now but won't be true in the future. Any chance you know which version of CMake starts throwing the error?

Copy link
Author

Choose a reason for hiding this comment

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

I believe it's 4.1.2, below that didn't throw for me.

# (discovered while testing with 4.1).
set(GSL_HEADER_INTERFACE_SOURCES)
foreach(header ${GSL_HEADER_FILES})
list(APPEND GSL_HEADER_INTERFACE_SOURCES
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include/${header}>
$<INSTALL_INTERFACE:include/${header}>
)
endforeach()

target_sources(GSL INTERFACE
${GSL_HEADER_INTERFACE_SOURCES}
Copy link
Member

Choose a reason for hiding this comment

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

suggestion: According to https://github.com/microsoft/GSL/pull/1112/files#r1190452831 this only affects VS solutions. Let's move it into your new if (CMAKE_GENERATOR MATCHES "Visual Studio") block and combine it with the other target_sources (which doesn't yet exist; see my comment about VS_SOLUTION_ITEMS).

$<BUILD_INTERFACE:${GSL_SOURCE_DIR}/GSL.natvis>
)

if (CMAKE_GENERATOR MATCHES "Visual Studio")
# Visual Studio still needs concrete file paths for solution grouping.
set(GSL_HEADER_ABSOLUTE_PATHS)
foreach(header ${GSL_HEADER_FILES})
list(APPEND GSL_HEADER_ABSOLUTE_PATHS ${GSL_SOURCE_DIR}/include/${header})
endforeach()
source_group(TREE ${GSL_SOURCE_DIR}/include PREFIX "Header Files" FILES ${GSL_HEADER_ABSOLUTE_PATHS})
Copy link
Member

Choose a reason for hiding this comment

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

issue: Based on your PR description, it sounds like this should appear in the solution adjacent to "Solution Items"? I'm not seeing anything.

> cmake --version
> PS D:\GSL\build> cmake --version
cmake version 4.1.0
Image

Copy link
Author

Choose a reason for hiding this comment

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

I added a source_group call, the docs should land in the solution items node now.


set(GSL_SOLUTION_FILES
${GSL_SOURCE_DIR}/.clang-format
${GSL_SOURCE_DIR}/.gitattributes
${GSL_SOURCE_DIR}/.gitignore
${GSL_SOURCE_DIR}/CMakeSettings.json
${GSL_SOURCE_DIR}/CONTRIBUTING.md
${GSL_SOURCE_DIR}/LICENSE
${GSL_SOURCE_DIR}/README.md
${GSL_SOURCE_DIR}/SECURITY.md
${GSL_SOURCE_DIR}/ThirdPartyNotices.txt
)

set_property(DIRECTORY PROPERTY VS_SOLUTION_ITEMS ${GSL_SOLUTION_FILES})
Copy link
Member

Choose a reason for hiding this comment

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

issue: The VS_SOLUTION_ITEMS property is new in CMake 4.0.0. At this time, we want to maintain backwards compatibility with older CMakes. Apologies for not catching this the first time around, but we will need to figure out something else.

From what I understand, before 4.0.0 there was no way of adding arbitrary files to VS solutions. What you'll need to do is create a custom target and add the files in the GSL workspace to that target. Something like:

if (CMAKE_GENERATOR MATCHES "Visual Studio")
    file(GLOB GSL_WORKSPACE_FILES "${CMAKE_CURRENT_SOURCE_DIR}/include/gsl/*")
    list(APPEND GSL_WORKSPACE_FILES
        ${GSL_SOURCE_DIR}/.clang-format
        ${GSL_SOURCE_DIR}/.gitattributes
        ${GSL_SOURCE_DIR}/.gitignore
        ${GSL_SOURCE_DIR}/CMakeSettings.json
        ${GSL_SOURCE_DIR}/CONTRIBUTING.md
        ${GSL_SOURCE_DIR}/LICENSE
        ${GSL_SOURCE_DIR}/README.md
        ${GSL_SOURCE_DIR}/SECURITY.md
        ${GSL_SOURCE_DIR}/ThirdPartyNotices.txt
    )
    target_sources(GSL PRIVATE ${GSL_WORKSPACE_FILES})
endif()

And that should probably be the bulk of the change.

https://cmake.org/cmake/help/latest/prop_dir/VS_SOLUTION_ITEMS.html

source_group("Solution Items" FILES ${GSL_SOLUTION_FILES})
endif()

if (GSL_TEST)
enable_testing()
Expand Down