Skip to content

Commit 7e18eb3

Browse files
committed
Improve shipped FindHOOMD cmake module
1 parent 5e38e05 commit 7e18eb3

File tree

2 files changed

+107
-110
lines changed

2 files changed

+107
-110
lines changed

Diff for: cmake/Modules/FindHOOMD.cmake

+96-107
Original file line numberDiff line numberDiff line change
@@ -1,118 +1,107 @@
1-
# CMake script for finding HOOMD and setting up all needed compile options to create and link a plugin library
1+
# FindHOOMD
2+
# ---------
3+
#
4+
# CMake script for finding HOOMD and setting up all needed compile options
5+
# to create and link a plugin library
26
#
37
# Variables taken as input to this module:
4-
# HOOMD_ROOT : location to look for HOOMD, if it is not in the python path
8+
# HOOMD_ROOT -- Location to look for HOOMD, if it is not in the python path
59
#
610
# Variables defined by this module:
7-
# FOUND_HOOMD : set to true if HOOMD is found
8-
# HOOMD_LIBRARIES : a list of all libraries needed to link to to access hoomd (uncached)
9-
# HOOMD_INCLUDE_DIR : a list of all include directories that need to be set to include HOOMD
10-
# HOOMD_LIB : a cached var locating the hoomd library to link to
11+
# HOOMD_INCLUDE_DIR -- Include directories that need to be set to include HOOMD
12+
# HOOMD_LIB -- Cached var locating the hoomd library to link to
13+
# HOOMD_LIBRARIES -- Libraries needed to link to to access hoomd (uncached)
14+
# HOOMD_FOUND
1115
#
12-
# various ENABLE_ flags translated from hoomd_config.h so this plugin build can match the ABI of the installed hoomd
16+
# Various ENABLE_ flags are translated from hoomd_config.h
17+
# so this plugin build can match the ABI of the installed hoomd
1318
#
14-
# as a convenience (for the intended purpose of this find script), all include directories and definitions needed
15-
# to compile with all the various libs (boost, python, winsoc, etc...) are set within this script
16-
17-
set(HOOMD_ROOT "" CACHE FILEPATH "Directory containing a hoomd installation (i.e. _hoomd.so)")
19+
# As a convenience (for the intended purpose of this find script),
20+
# all include directories and definitions needed to compile with all the various libs
21+
# (boost, python, winsoc, etc...) are set within this script
1822

19-
# Let HOOMD_ROOT take precedence, but if unset, try letting Python find a hoomd package in its default paths.
20-
if(HOOMD_ROOT)
21-
set(hoomd_installation_guess ${HOOMD_ROOT})
22-
else(HOOMD_ROOT)
23-
find_package(PythonInterp)
24-
25-
set(find_hoomd_script "
23+
function(find_hoomd_with_python)
24+
find_package(Python QUIET COMPONENTS Interpreter)
25+
set(FIND_HOOMD_SCRIPT "
2626
from __future__ import print_function;
27-
import sys, os; sys.stdout = open(os.devnull, 'w')
28-
import hoomd
29-
print(os.path.dirname(hoomd.__file__), file=sys.stderr, end='')")
30-
31-
execute_process(COMMAND ${PYTHON_EXECUTABLE} -c "${find_hoomd_script}"
32-
ERROR_VARIABLE hoomd_installation_guess)
33-
message(STATUS "Python output: " ${hoomd_installation_guess})
34-
endif(HOOMD_ROOT)
27+
import os
28+
try:
29+
import hoomd
30+
print(os.path.dirname(hoomd.__file__), end='')
31+
except:
32+
print('', end='')"
33+
)
34+
execute_process(
35+
COMMAND ${Python_EXECUTABLE} -c "${FIND_HOOMD_SCRIPT}"
36+
OUTPUT_VARIABLE HOOMD_ROOT
37+
)
38+
set(HOOMD_DIR ${HOOMD_ROOT} PARENT_SCOPE)
39+
endfunction()
3540

36-
message(STATUS "Looking for a HOOMD installation at " ${hoomd_installation_guess})
37-
find_path(FOUND_HOOMD_ROOT
38-
NAMES _hoomd.so __init__.py
39-
HINTS ${hoomd_installation_guess}
40-
)
41-
42-
if(FOUND_HOOMD_ROOT)
43-
set(HOOMD_ROOT ${FOUND_HOOMD_ROOT} CACHE FILEPATH "Directory containing a hoomd installation (i.e. _hoomd.so)" FORCE)
44-
message(STATUS "Found hoomd installation at " ${HOOMD_ROOT})
45-
else(FOUND_HOOMD_ROOT)
46-
message(FATAL_ERROR "Could not find hoomd installation, either set HOOMD_ROOT or set PYTHON_EXECUTABLE to a python which can find hoomd")
47-
endif(FOUND_HOOMD_ROOT)
41+
if(HOOMD_ROOT)
42+
set(HOOMD_DIR ${HOOMD_ROOT})
43+
elseif(DEFINED ENV{HOOMD_ROOT})
44+
set(HOOMD_DIR $ENV{HOOMD_ROOT})
45+
else()
46+
find_hoomd_with_python()
47+
endif()
4848

49-
# search for the hoomd include directory
5049
find_path(HOOMD_INCLUDE_DIR
51-
NAMES HOOMDVersion.h
52-
HINTS ${HOOMD_ROOT}/include
53-
)
54-
55-
if (HOOMD_INCLUDE_DIR)
56-
message(STATUS "Found HOOMD include directory: ${HOOMD_INCLUDE_DIR}")
57-
mark_as_advanced(HOOMD_INCLUDE_DIR)
58-
endif (HOOMD_INCLUDE_DIR)
59-
60-
set(HOOMD_FOUND FALSE)
61-
if (HOOMD_INCLUDE_DIR AND HOOMD_ROOT)
62-
set(HOOMD_FOUND TRUE)
63-
mark_as_advanced(HOOMD_ROOT)
64-
endif (HOOMD_INCLUDE_DIR AND HOOMD_ROOT)
65-
66-
if (NOT HOOMD_FOUND)
67-
message(SEND_ERROR "HOOMD Not found. Please specify the location of your hoomd installation in HOOMD_ROOT")
68-
endif (NOT HOOMD_FOUND)
69-
70-
#############################################################
71-
## Now that we've found hoomd, lets do some setup
72-
if (HOOMD_FOUND)
73-
74-
include_directories(${HOOMD_INCLUDE_DIR})
75-
76-
# run all of HOOMD's generic lib setup scripts
77-
set(CMAKE_MODULE_PATH ${HOOMD_ROOT}
78-
${HOOMD_ROOT}/CMake/hoomd
79-
${HOOMD_ROOT}/CMake/thrust
80-
${CMAKE_MODULE_PATH}
81-
)
82-
83-
# grab previously-set hoomd configuration
84-
include (hoomd_cache)
85-
86-
# Handle user build options
87-
include (CMake_build_options)
88-
include (CMake_preprocessor_flags)
89-
# setup the install directories
90-
include (CMake_install_options)
91-
92-
# Find the python executable and libraries
93-
include (HOOMDPythonSetup)
94-
# Find CUDA and set it up
95-
include (HOOMDCUDASetup)
96-
# Set default CFlags
97-
include (HOOMDCFlagsSetup)
98-
# include some os specific options
99-
include (HOOMDOSSpecificSetup)
100-
# setup common libraries used by all targets in this project
101-
include (HOOMDCommonLibsSetup)
102-
# setup macros
103-
include (HOOMDMacros)
104-
# setup MPI support
105-
include (HOOMDMPISetup)
106-
107-
set(HOOMD_LIB ${HOOMD_ROOT}/_hoomd${PYTHON_MODULE_EXTENSION})
108-
set(HOOMD_MD_LIB ${HOOMD_ROOT}/md/_md${PYTHON_MODULE_EXTENSION})
109-
set(HOOMD_DEM_LIB ${HOOMD_ROOT}/dem/_dem${PYTHON_MODULE_EXTENSION})
110-
set(HOOMD_HPMC_LIB ${HOOMD_ROOT}/hpmc/_hpmc${PYTHON_MODULE_EXTENSION})
111-
set(HOOMD_CGCMM_LIB ${HOOMD_ROOT}/cgcmm/_cgcmm${PYTHON_MODULE_EXTENSION})
112-
set(HOOMD_METAL_LIB ${HOOMD_ROOT}/metal/_metal${PYTHON_MODULE_EXTENSION})
113-
set(HOOMD_DEPRECATED_LIB ${HOOMD_ROOT}/deprecated/_deprecated${PYTHON_MODULE_EXTENSION})
114-
115-
set(HOOMD_LIBRARIES ${HOOMD_LIB} ${HOOMD_COMMON_LIBS})
116-
set(HOOMD_LIBRARIES ${HOOMD_LIB} ${HOOMD_COMMON_LIBS})
117-
118-
endif (HOOMD_FOUND)
50+
NAMES HOOMDVersion.h
51+
HINTS ${HOOMD_DIR}/include
52+
)
53+
if(HOOMD_INCLUDE_DIR)
54+
file(READ "${HOOMD_INCLUDE_DIR}/HOOMDVersion.h" HOOMD_VERSION_HEADER)
55+
string(REGEX
56+
MATCH "([0-9]+)\\.([0-9]+)\\.([0-9]+)" HOOMD_VERSION ${HOOMD_VERSION_HEADER}
57+
)
58+
endif()
59+
mark_as_advanced(HOOMD_FOUND HOOMD_DIR HOOMD_INCLUDE_DIR HOOMD_VERSION)
60+
61+
include(FindPackageHandleStandardArgs)
62+
find_package_handle_standard_args(HOOMD
63+
REQUIRED_VARS HOOMD_DIR HOOMD_INCLUDE_DIR HOOMD_VERSION
64+
)
65+
66+
if(HOOMD_FOUND)
67+
include_directories(${HOOMD_INCLUDE_DIR})
68+
# Run all of HOOMD's generic lib setup scripts
69+
set(CMAKE_MODULE_PATH
70+
${HOOMD_DIR}
71+
${HOOMD_DIR}/CMake/hoomd
72+
${HOOMD_DIR}/CMake/thrust
73+
${CMAKE_MODULE_PATH}
74+
)
75+
# Grab previously-set hoomd configuration
76+
include(hoomd_cache)
77+
# Handle user build options
78+
include(CMake_build_options)
79+
include(CMake_preprocessor_flags)
80+
# setup the install directories
81+
include(CMake_install_options)
82+
# Find the python executable and libraries
83+
include(HOOMDPythonSetup)
84+
# Find CUDA and set it up
85+
include(HOOMDCUDASetup)
86+
# Set default CFlags
87+
include(HOOMDCFlagsSetup)
88+
# Include some os specific options
89+
include(HOOMDOSSpecificSetup)
90+
# Setup common libraries used by all targets in this project
91+
include(HOOMDCommonLibsSetup)
92+
# Setup macros
93+
include(HOOMDMacros)
94+
# Setup MPI support
95+
include(HOOMDMPISetup)
96+
97+
set(HOOMD_LIB ${HOOMD_DIR}/_hoomd${PYTHON_MODULE_EXTENSION})
98+
set(HOOMD_MD_LIB ${HOOMD_DIR}/md/_md${PYTHON_MODULE_EXTENSION})
99+
set(HOOMD_DEM_LIB ${HOOMD_DIR}/dem/_dem${PYTHON_MODULE_EXTENSION})
100+
set(HOOMD_HPMC_LIB ${HOOMD_DIR}/hpmc/_hpmc${PYTHON_MODULE_EXTENSION})
101+
set(HOOMD_CGCMM_LIB ${HOOMD_DIR}/cgcmm/_cgcmm${PYTHON_MODULE_EXTENSION})
102+
set(HOOMD_METAL_LIB ${HOOMD_DIR}/metal/_metal${PYTHON_MODULE_EXTENSION})
103+
set(HOOMD_DEPRECATED_LIB ${HOOMD_DIR}/deprecated/_deprecated${PYTHON_MODULE_EXTENSION})
104+
105+
set(HOOMD_LIBRARIES ${HOOMD_LIB} ${HOOMD_COMMON_LIBS})
106+
set(HOOMD_LIBRARIES ${HOOMD_LIB} ${HOOMD_COMMON_LIBS})
107+
endif(HOOMD_FOUND)

Diff for: cmake/Modules/FindHOOMDTools.cmake

+11-3
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,18 @@ set(HOOMD_GPU_PLATFORM "CUDA" CACHE STRING "GPU backend: CUDA or HIP.")
33
find_package(HOOMD QUIET)
44

55
if(HOOMD_FOUND)
6-
if(${HOOMD_VERSION} VERSION_GREATER_EQUAL "2.6.0")
6+
if(
7+
${HOOMD_VERSION} VERSION_GREATER_EQUAL "3.5.0" OR (
8+
${HOOMD_VERSION} VERSION_LESS "3" AND
9+
${HOOMD_VERSION} VERSION_GREATER_EQUAL "2.6.0"
10+
)
11+
)
712
message(STATUS "Found HOOMD: ${HOOMD_DIR} (version ${HOOMD_VERSION})")
813
else()
9-
message(FATAL_ERROR "Minimum supported HOOMD version is v2.6.0 (version ${HOOMD_VERSION})")
14+
message(FATAL_ERROR
15+
"Supported HOOMD versions are v2.6.0 to v2.9.7 or >= v3.5.0 "
16+
"(version ${HOOMD_VERSION})"
17+
)
1018
endif()
1119
endif()
1220

@@ -21,7 +29,7 @@ if(ENABLE_STATIC)
2129
message(SEND_ERROR "Plugins cannot be built against a statically compiled hoomd")
2230
endif()
2331

24-
if(${HOOMD_VERSION} VERSION_LESS "3.4.0")
32+
if(${HOOMD_VERSION} VERSION_LESS "3.5.0")
2533
add_compile_definitions(EXPORT_HALFSTEPHOOK)
2634
if(${HOOMD_VERSION} VERSION_LESS "3")
2735
add_compile_definitions(HOOMD2)

0 commit comments

Comments
 (0)