Skip to content

Commit c078c2c

Browse files
committed
Allow building metatensor and metatomic in the same cmake project
1 parent 2dbe3f2 commit c078c2c

File tree

1 file changed

+37
-4
lines changed

1 file changed

+37
-4
lines changed

metatomic-torch/CMakeLists.txt

+37-4
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,43 @@ if (${CMAKE_CURRENT_SOURCE_DIR} STREQUAL ${CMAKE_SOURCE_DIR})
5151
endif()
5252

5353

54-
set(REQUIRED_METATENSOR_TORCH_VERSION "0.7.0")
55-
find_package(metatensor_torch ${REQUIRED_METATENSOR_TORCH_VERSION} CONFIG REQUIRED)
56-
# No version specification here, we'll use whatever `metatensor_torch` uses
57-
find_package(metatensor CONFIG REQUIRED)
54+
function(check_compatible_versions _actual_ _requested_)
55+
if(${_actual_} MATCHES "^([0-9]+)\\.([0-9]+)")
56+
set(_actual_major_ "${CMAKE_MATCH_1}")
57+
set(_actual_minor_ "${CMAKE_MATCH_2}")
58+
else()
59+
message(FATAL_ERROR "Failed to parse actual version: ${_actual_}")
60+
endif()
61+
62+
if(${_requested_} MATCHES "^([0-9]+)\\.([0-9]+)")
63+
set(_requested_major_ "${CMAKE_MATCH_1}")
64+
set(_requested_minor_ "${CMAKE_MATCH_2}")
65+
else()
66+
message(FATAL_ERROR "Failed to parse requested version: ${_requested_}")
67+
endif()
68+
69+
if (${_requested_major_} EQUAL 0 AND ${_actual_minor_} EQUAL ${_requested_minor_})
70+
# major version is 0 and same minor version, everything is fine
71+
elseif (${_actual_major_} EQUAL ${_requested_major_})
72+
# same major version, everything is fine
73+
else()
74+
# not compatible
75+
message(FATAL_ERROR "Incompatible versions: we need ${_requested_}, but we got ${_actual_}")
76+
endif()
77+
endfunction()
78+
79+
80+
set(REQUIRED_METATENSOR_TORCH_VERSION "0.7.6")
81+
# Either metatensor is built as part of the same CMake project, or we try to
82+
# find the corresponding CMake package
83+
if (TARGET metatensor_torch)
84+
get_target_property(METATENSOR_TORCH_BUILD_VERSION metatensor_torch BUILD_VERSION)
85+
check_compatible_versions(${METATENSOR_TORCH_BUILD_VERSION} ${REQUIRED_METATENSOR_TORCH_VERSION})
86+
else()
87+
find_package(metatensor_torch ${REQUIRED_METATENSOR_TORCH_VERSION} CONFIG REQUIRED)
88+
# No version specification here, we'll use whatever `metatensor_torch` uses
89+
find_package(metatensor CONFIG REQUIRED)
90+
endif()
5891

5992
# FindCUDNN.cmake distributed with PyTorch is a bit broken, so we have a
6093
# fixed version in `cmake/FindCUDNN.cmake`

0 commit comments

Comments
 (0)