11if (POLICY CMP0144)
22 cmake_policy (SET CMP0144 NEW )
33endif ()
4+ if (POLICY CMP0167)
5+ cmake_policy (SET CMP0167 NEW )
6+ endif ()
47cmake_minimum_required (VERSION 3.17 )
58set (CMAKE_CXX_STANDARD 17)
69set (CMAKE_CXX_STANDARD_REQUIRED True )
710
811set (CMAKE_MACOSX_RPATH 1)
9- set (CMAKE_CXX_FLAGS_RELEASE "-O2" )
10- set (CMAKE_CXX_FLAGS_DEBUG "-g -Wall -O2 -D_DEBUG" )
1112
1213project (alm)
1314
@@ -18,27 +19,49 @@ set(alm_version ${CMAKE_MATCH_1})
1819MESSAGE ("Setting up for ALM code (ALAMODE version: ${CMAKE_MATCH_1} )" )
1920
2021if (SPGLIB_ROOT)
21- include_directories ("${SPGLIB_ROOT} /include" )
22- set (spglib "-L${SPGLIB_ROOT} /lib -L${SPGLIB_ROOT} /lib64 -lsymspg" )
22+ list (APPEND CMAKE_PREFIX_PATH "${SPGLIB_ROOT} " )
23+ endif ()
24+
25+ find_path (SPGLIB_INCLUDE_DIR spglib.h
26+ HINTS ${SPGLIB_ROOT}
27+ PATH_SUFFIXES include
28+ )
29+ find_library (SPGLIB_LIBRARY
30+ NAMES symspg spglib
31+ HINTS ${SPGLIB_ROOT}
32+ PATH_SUFFIXES lib lib64
33+ )
34+
35+ if (SPGLIB_INCLUDE_DIR AND SPGLIB_LIBRARY)
36+ set (spglib_include ${SPGLIB_INCLUDE_DIR} )
37+ set (spglib ${SPGLIB_LIBRARY} )
2338else ()
24- message ("SPGLIB_ROOT is not given by -DSPGLIB_ROOT option. Just add -lsymspg." )
39+ message ("SPGLIB not found via hints. Falling back to -lsymspg." )
2540 set (spglib "-lsymspg" )
2641endif ()
2742#
2843# Add openmp flag if available
2944#
3045find_package (OpenMP REQUIRED COMPONENTS CXX )
3146if (OPENMP_FOUND)
32- set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS} " )
3347 set (openmplibrary OpenMP::OpenMP_CXX)
3448endif ()
3549
50+ if (BOOST_ROOT)
51+ list (APPEND CMAKE_PREFIX_PATH "${BOOST_ROOT} " )
52+ endif ()
3653find_package (Boost )
3754if (Boost_FOUND)
38- include_directories (${Boost_INCLUDE_DIRS} )
55+ if (TARGET Boost::boost)
56+ set (boost_target Boost::boost)
57+ elseif (TARGET Boost::headers)
58+ set (boost_target Boost::headers)
59+ else ()
60+ set (boost_includes ${Boost_INCLUDE_DIRS} )
61+ endif ()
3962else ()
4063 if (BOOST_INCLUDE)
41- include_directories ( ${BOOST_INCLUDE} )
64+ set (boost_includes ${BOOST_INCLUDE} )
4265 else ()
4366 message (FATAL_ERROR "Boost was not found.
4467 Please specify the location of boost include directories
@@ -49,33 +72,39 @@ endif ()
4972option (EIGEN3_ROOT "Path to Eigen3 installation" "" )
5073
5174if (EIGEN3_ROOT)
52- set (CMAKE_PREFIX_PATH ${EIGEN3_ROOT} )
53- include_directories (${EIGEN3_ROOT} /include )
54- else ()
55- find_package (Eigen3 )
56- if (Eigen3_FOUND)
57- include_directories (${EIGEN3_INCLUDE_DIR} )
75+ list (APPEND CMAKE_PREFIX_PATH "${EIGEN3_ROOT} " )
76+ endif ()
77+ find_package (Eigen3 )
78+ if (Eigen3_FOUND)
79+ if (TARGET Eigen3::Eigen)
80+ set (eigen_target Eigen3::Eigen)
81+ else ()
82+ set (eigen_includes ${EIGEN3_INCLUDE_DIR} )
83+ endif ()
84+ else ()
85+ if (EIGEN3_INCLUDE)
86+ set (eigen_includes ${EIGEN3_INCLUDE} )
5887 else ()
59- if (EIGEN3_INCLUDE)
60- include_directories (${EIGEN3_INCLUDE} )
61- else ()
62- message (FATAL_ERROR "Eigen3 was not found.
63- Please specify the location of boost include directories
64- via -DEIGEN3_INCLUDE option." )
65- endif ()
88+ message (FATAL_ERROR "Eigen3 was not found.
89+ Please specify the location of boost include directories
90+ via -DEIGEN3_INCLUDE option." )
6691 endif ()
67- endif ()
92+ endif ()
6893
6994
7095
71- add_definitions (-D_HDF5 )
96+ if (HDF5_ROOT)
97+ list (APPEND CMAKE_PREFIX_PATH "${HDF5_ROOT} " )
98+ endif ()
7299find_package (HDF5 COMPONENTS CXX )
73100if (HDF5_FOUND)
74- include_directories (${HDF5_INCLUDE_DIRS} )
75101 set (hdf5library ${HDF5_LIBRARIES} )
102+ if (TARGET HDF5::HDF5)
103+ set (hdf5_target HDF5::HDF5)
104+ endif ()
76105else ()
77106 if (HDF5_ROOT)
78- include_directories ( "${HDF5_ROOT} /include" )
107+ set (hdf5_include "${HDF5_ROOT} /include" )
79108 set (hdf5library "-L${HDF5_ROOT} /lib -lhdf5_cpp -lhdf5" )
80109 else ()
81110 message (FATAL_ERROR "HDF5 was not found.
@@ -84,8 +113,6 @@ else ()
84113 endif ()
85114endif ()
86115
87- include_directories ("${PROJECT_SOURCE_DIR} /../external" )
88-
89116option (USE_MKL_BACKEND "Enable Intel MKL Pardiso" OFF )
90117option (USE_ACCEL_BACKEND "Enable Apple Accelerate LDLT" OFF )
91118
@@ -108,19 +135,33 @@ elseif (USE_ACCEL_BACKEND)
108135 add_compile_definitions (USE_ACCEL_BACKEND EIGEN_USE_ACCELERATE )
109136 set ( BACKEND_LIBS ${ACCELERATE_FRAMEWORK} )
110137else ()
138+ if (NOT BLA_VENDOR AND DEFINED ENV{CONDA_PREFIX})
139+ find_library (CONDA_OPENBLAS_LIB
140+ NAMES openblas openblas64
141+ HINTS "$ENV{CONDA_PREFIX} "
142+ PATH_SUFFIXES lib
143+ )
144+ if (CONDA_OPENBLAS_LIB)
145+ set (BLA_VENDOR OpenBLAS CACHE STRING "BLAS/LAPACK vendor" FORCE )
146+ endif ()
147+ endif ()
111148 find_package (BLAS REQUIRED )
112149 find_package (LAPACK REQUIRED )
113- include_directories (${BLAS_INCLUDE_DIRS} ${LAPACK_INCLUDE_DIRS} )
114- set ( BACKEND_LIBS ${BLAS_LIBRARIES} ${LAPACK_LIBRARIES} )
150+ if (TARGET BLAS::BLAS)
151+ set (blas_target BLAS::BLAS)
152+ else ()
153+ set (blas_target ${BLAS_LIBRARIES} )
154+ endif ()
155+ if (TARGET LAPACK::LAPACK)
156+ set (lapack_target LAPACK::LAPACK)
157+ else ()
158+ set (lapack_target ${LAPACK_LIBRARIES} )
159+ endif ()
160+ set ( BACKEND_LIBS ${blas_target} ${lapack_target} )
115161endif ()
116162
117- get_property (dirs DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY INCLUDE_DIRECTORIES )
118- foreach (dir ${dirs} )
119- message (STATUS "dir='${dir} '" )
120- endforeach ()
121163
122164# Source code
123- include_directories ("${PROJECT_SOURCE_DIR} /../include" )
124165set (SOURCES ${PROJECT_SOURCE_DIR} /alm.cpp
125166 ${PROJECT_SOURCE_DIR} /cluster.cpp
126167 ${PROJECT_SOURCE_DIR} /constraint.cpp
@@ -167,16 +208,31 @@ add_executable(alm ${PROJECT_SOURCE_DIR}/main.cpp
167208 ${PROJECT_SOURCE_DIR} /alm_cui.cpp
168209 ${SOURCES} ${HEADERS} )
169210
170-
171- if (EIGEN3_ROOT)
172- target_include_directories (alm PRIVATE ${EIGEN3_ROOT} /include/eigen3 )
173- endif ()
211+ target_compile_options (alm PRIVATE
212+ $<$<CONFIG :Release >:-O2 >
213+ $<$<CONFIG :Debug >:-g -Wall -O2 >
214+ )
215+ target_compile_definitions (alm PRIVATE
216+ $<$<CONFIG :Debug >:_DEBUG >
217+ _HDF5
218+ )
219+ target_include_directories (alm PRIVATE
220+ ${PROJECT_SOURCE_DIR} /../include
221+ ${PROJECT_SOURCE_DIR} /../external
222+ ${spglib_include}
223+ ${boost_includes}
224+ ${eigen_includes}
225+ ${hdf5_include}
226+ )
174227target_link_libraries (alm
175228# PRIVATE Eigen3::Eigen
229+ ${boost_target}
230+ ${eigen_target}
176231 ${Boost_LIBRARIES}
177232 ${BACKEND_LIBS}
178233 ${spglib}
179234 ${openmplibrary}
235+ ${hdf5_target}
180236 ${hdf5library}
181237)
182238
@@ -185,13 +241,32 @@ if (BUILD_LIBRARIES)
185241 set (soserial "1" )
186242 # Shared library
187243 add_library (almcxx SHARED ${SOURCES} )
188- target_link_libraries (almcxx ${BACKEND_LIBS} ${spglib} ${hdf5library} )
244+ target_compile_definitions (almcxx PRIVATE _HDF5 )
245+ target_include_directories (almcxx PRIVATE
246+ ${PROJECT_SOURCE_DIR} /../include
247+ ${PROJECT_SOURCE_DIR} /../external
248+ ${spglib_include}
249+ ${boost_includes}
250+ ${eigen_includes}
251+ ${hdf5_include}
252+ )
253+ target_link_libraries (almcxx ${boost_target} ${eigen_target} ${BACKEND_LIBS} ${spglib} ${hdf5_target} ${hdf5library} )
189254 set_property (TARGET almcxx PROPERTY VERSION ${serial} )
190255 set_property (TARGET almcxx PROPERTY SOVERSION ${soserial} )
191256 install (TARGETS almcxx DESTINATION ${PROJECT_SOURCE_DIR} /lib)
192257
193258 # Static link library
194259 add_library (almcxx_static STATIC ${SOURCES} )
260+ target_compile_definitions (almcxx_static PRIVATE _HDF5 )
261+ target_include_directories (almcxx_static PRIVATE
262+ ${PROJECT_SOURCE_DIR} /../include
263+ ${PROJECT_SOURCE_DIR} /../external
264+ ${spglib_include}
265+ ${boost_includes}
266+ ${eigen_includes}
267+ ${hdf5_include}
268+ )
269+ target_link_libraries (almcxx_static ${boost_target} ${eigen_target} )
195270 set_property (TARGET almcxx_static PROPERTY VERSION ${serial} )
196271 set_property (TARGET almcxx_static PROPERTY SOVERSION ${soserial} )
197272 set_property (TARGET almcxx_static PROPERTY OUTPUT_NAME almcxx )
0 commit comments