xtensor icon indicating copy to clipboard operation
xtensor copied to clipboard

[Doc suggestion] CMake Fetch Content

Open TobiasWallner opened this issue 1 year ago • 2 comments

Hi, and thanks for the library.

I just tried out xtensor and did so using CMakes FetchContent.

Maybe you would like to add the following way of importing your library into a project to the documentation. I think that FetchContent is very convenient, because one only has to paste the following into the CMakeLists.txt, the library gets downloaded and built if necessary, and one does not need to do anything else making it almost plug and play.

# ======================== External Libraries =========================

include(FetchContent)

# -------- xtl --------
find_package(xtl QUIET)
if(NOT xtensor_FOUND)
    message(STATUS "xtl not found, attempting to access it via FetchContent")
    FetchContent_Declare(xtl GIT_REPOSITORY https://github.com/xtensor-stack/xtl.git GIT_TAG master)
    FetchContent_MakeAvailable(xtl)
else()
    message(STATUS "Found xtl library.")
endif()

# -------- xtensor --------
find_package(xtensor QUIET)
if(NOT xtensor_FOUND)
    message(STATUS "xtensor not found, attempting to access it via FetchContent")
    FetchContent_Declare(xtensor GIT_REPOSITORY https://github.com/xtensor-stack/xtensor.git GIT_TAG master)
    FetchContent_MakeAvailable(xtensor)
else()
    message(STATUS "Found xtensor library.")
endif()

# ================================== END ===============================

# project building process: 
set(PROJECT_NAME xtensor_tryout)
project(${PROJECT_NAME})

add_executable(${PROJECT_NAME}
	main.cpp
)

target_include_directories(${PROJECT_NAME} PUBLIC
	${xtensor_INCLUDE_DIRS}
)

target_link_libraries(${PROJECT_NAME} PUBLIC
	xtensor
)

TobiasWallner avatar Aug 18 '24 15:08 TobiasWallner

Generally I think it is bad practice to use cmake like a package manager. Conda/Mamba/Vcpkg all work for xtensor. The environment-dev.yml file makes it pretty seamless as well.

spectre-ns avatar Dec 22 '24 21:12 spectre-ns

When using this library as part of another library, which is my use case, a pure CMake solution using fetch_content is definitely the way to go. This is a cross-platform library that does not rely on Python or platform specific package managers.

@TobiasWallner, Thank you for this post!

Radagan avatar May 30 '25 17:05 Radagan