Skip to content

Commit

Permalink
Merge pull request #170 from MolSSI-MDI/cmake
Browse files Browse the repository at this point in the history
Improve CMake Options
  • Loading branch information
taylor-a-barnes authored Jan 8, 2025
2 parents 906c81c + f7a733a commit b0c4803
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 66 deletions.
80 changes: 63 additions & 17 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,44 @@
cmake_minimum_required(VERSION 3.7)
cmake_minimum_required(VERSION 3.10)

option(language "language")
option(libtype "libtype")
option(mpi "on or off" ON)
option(python_package "on or off" OFF)
option(plugins "on or off" ON)
option(python_plugins "on or off" ${plugins})
option(python_version "python version number" OFF)
option(test_codes "on or off" OFF)
option(test_drivers "on or off" ${test_codes})
option(test_engines "on or off" ${test_codes})
option(debug "on or off" OFF)
# Options
option(MDI_CXX "Build with CXX suport - ON (DEFAULT) or OFF)" ON)
option(MDI_Fortran "Build with Fortran support - ON (DEFAULT) or OFF)" ON)
option(MDI_Python "Build with Python support - ON (DEFAULT) or OFF)" ON)
option(MDI_Python_PACKAGE "Flag to install MDI as a Python package" OFF)
option(MDI_PLUGINS "Flag to compile with support for MDI plugins" ON)
option(MDI_Python_PLUGINS "Flag to compile with support for MDI Python plugins" ${MDI_PLUGINS})
option(MDI_TEST_CODES "Flag to compile MDI test codes" OFF)
option(MDI_TEST_DRIVERS "Flag to compile MDI test drivers" ${MDI_TEST_CODES})
option(MDI_TEST_ENGINES "Flag to compile MDI test engines" ${MDI_TEST_CODES})
option(MDI_DEBUG "Flag to compile MDI in debug mode" OFF)
set(MDI_USE_MPI "" CACHE STRING "Flag to use MPI - ON or OFF")
set(MDI_Python_VERSION "OFF" CACHE STRING "When linking against a build of Python, MDI will attempt to link against a build of Python corresponding to this version number.")

# Deprecated Options
option(language "Deprecated; use MDI_CXX, MDI_Fortran, and/or MDI_Python instead.")
option(libtype "Deprecated; use BUILD_SHARED_LIBS instead.")
set(mpi ${MDI_USE_MPI} CACHE STRING "Deprecated; use MDI_USE_MPI instead.")
set(python_package ${MDI_Python_PACKAGE} CACHE STRING "Deprecated; use MDI_Python_PACKAGE instead.")
set(plugins "" CACHE STRING "Deprecated; use MDI_PLUGINS instead.")
if ( plugins STREQUAL "" )
set(plugins ${MDI_PLUGINS})
else()
set(MDI_PLUGINS ${plugins})
set(MDI_Python_PLUGINS ${plugins})
endif()
set(python_plugins ${MDI_Python_PLUGINS} CACHE STRING "Deprecated; use MDI_Python_PLUGINS instead.")
set(python_version ${MDI_Python_VERSION} CACHE STRING "Deprecated; use MDI_Python_VERSION instead.")
set(test_codes "" CACHE STRING "Deprecated; use MDI_TEST_CODES instead.")
if( test_codes STREQUAL "" )
set(test_codes ${MDI_TEST_CODES})
else()
set(MDI_TEST_CODES ${test_codes})
set(MDI_TEST_DRIVERS ${test_codes})
set(MDI_TEST_ENGINES ${test_codes})
endif()
set(test_drivers ${MDI_TEST_DRIVERS} CACHE STRING "Deprecated; use MDI_TEST_DRIVERS instead.")
set(test_engines ${MDI_TEST_ENGINES} CACHE STRING "Deprecated; use MDI_TEST_ENGINES instead.")
set(debug ${MDI_DEBUG} CACHE STRING "Deprecated; use MDI_DEBUG instead.")

set(use_C "")
set(use_CXX "")
Expand All @@ -20,9 +48,15 @@ set(use_Python "")
if( NOT language )
# By default, compile for all languages
set(use_C "C")
set(use_CXX "CXX")
set(use_Fortran "Fortran")
set(use_Python "Python")
if( MDI_CXX )
set(use_CXX "CXX")
endif()
if( MDI_Fortran )
set(use_Fortran "Fortran")
endif()
if( MDI_Python )
set(use_Python "Python")
endif()
elseif( language STREQUAL "C" )
set(use_C "C")
elseif( language STREQUAL "CXX" )
Expand All @@ -39,10 +73,22 @@ else()
endif()

project(mdi
VERSION 1.4.30
VERSION 1.4.31
LANGUAGES ${use_C} ${use_CXX} ${use_Fortran})

# set a definition to enable MDI debug mode
# Check for MPI
if ( mpi STREQUAL "ON" )
find_package(MPI REQUIRED)
elseif( NOT ( mpi STREQUAL "OFF") )
find_package(MPI)
if ( MPI_FOUND )
set( mpi "ON" )
else()
set( mpi "OFF" )
endif()
endif()

# Set a definition to enable MDI debug mode
if( ${debug} )
add_definitions(-D_MDI_DEBUG=1)
else()
Expand Down
69 changes: 34 additions & 35 deletions MDI_Library/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,22 @@ macro(string_to_list _VAR _STR)
STRING(REPLACE " " ";" ${_VAR} "${_STR}")
endmacro(string_to_list _VAR _STR)


################################# MDI Project ##################################
include(GNUInstallDirs)
include(CMakePackageConfigHelpers)


#check for MPI
if ( NOT ( mpi STREQUAL "OFF") )
find_package(MPI)
endif()
if( NOT MPI_FOUND )
if( mpi STREQUAL "ON" )
message( WARNING "Could not find MPI. Compiling without MPI support." )
endif()
set(mpi "OFF")
endif()

#confirm that "language" is a valid value
# Confirm that "language" is a valid value
if( language AND (NOT language STREQUAL "C") AND (NOT language STREQUAL "CXX") AND (NOT language STREQUAL "Fortran") AND (NOT language STREQUAL "Python") )
message( FATAL_ERROR "Value of language not recognized. Accepted values are: C; CXX; Fortran; Python." )
endif()

#determine whether this is a SHARED or STATIC build
if( NOT libtype )
set(libtype "SHARED")
elseif ( (NOT libtype STREQUAL "STATIC") AND
(NOT libtype STREQUAL "SHARED") )
message( FATAL_ERROR "Value of libtype not recognized. Accepted values are: SHARED; STATIC; PACKAGE." )
endif()

# Set flag for whether to build plugins
if( ${plugins} )
add_definitions(-D_MDI_PLUGIN_SUPPORT=1)
else()
add_definitions(-D_MDI_PLUGIN_SUPPORT=0)
endif()


#construct the list of source files
# Construct the list of source files
list(APPEND sources "mdi.c")
list(APPEND sources "mdi_global.h")
list(APPEND sources "mdi_global.c")
Expand Down Expand Up @@ -79,18 +56,41 @@ if( (NOT language) OR (language STREQUAL "Fortran") )
list(APPEND sources "mdi_f90.F90")
endif()

add_library(mdi ${libtype}
${sources})
# Set whether MDI should be built as a shared or static library
if ( libtype STREQUAL "SHARED" )
set(BUILD_SHARED_LIBS ON)
endif()
if ( (NOT BUILD_SHARED_LIBS) AND MDI_Python )
message( WARNING "Python support requires that the MDI Library be compiled as a shared library. Turning on shared library build. To disable this warning, set -DBUILD_SHARED_LIBS=ON or -DMDI_Python=OFF." )
set(BUILD_SHARED_LIBS ON)
endif()
if (BUILD_SHARED_LIBS)
set(mdi_STATIC_BUILD OFF)
else()
set(mdi_STATIC_BUILD ON)
endif()

# Add the MDI target
add_library(mdi)

# Add the source files
target_sources(mdi
PRIVATE ${sources})

# If this is a static library, should compile with position independent code
if ( mdi_STATIC_BUILD )
set_target_properties(mdi PROPERTIES POSITION_INDEPENDENT_CODE ON)
endif()

# set API version of MDI Library
# Set API version of MDI Library
set_target_properties(mdi PROPERTIES SOVERSION 1) # bump whenever interface has changes or removals

#if this is a Windows build, link to ws2_32
# If this is a Windows build, link to ws2_32
if(WIN32)
target_link_libraries(mdi wsock32 ws2_32)
endif()

#link to libdl, which is used for the plugin system
# Link to libdl, which is used for the plugin system
if(NOT WIN32)
target_link_libraries(mdi dl)
endif()
Expand All @@ -102,10 +102,10 @@ else()
add_definitions(-DMDI_WINDOWS=0)
endif()

#include and link to MPI
# Include and link to MPI
if( mpi STREQUAL "ON" )

#include MPI
# Include MPI
string_to_list(MPI_C_COMPILE_OPTIONS "${MPI_C_COMPILE_FLAGS}")
string_to_list(MPI_C_LINK_OPTIONS "${MPI_C_LINK_FLAGS}")

Expand All @@ -120,7 +120,7 @@ elseif( mpi STREQUAL "OFF" )

else()

message( FATAL_ERROR "Value of mpi not recognized. Accepted values are: ON; OFF." )
message( FATAL_ERROR "Value of mpi (${mpi}) not recognized. Accepted values are: ON; OFF." )

endif()

Expand All @@ -136,7 +136,7 @@ else()

endif()

#do any Python-specific work
# Do any Python-specific work
if( (NOT language) OR (language STREQUAL "Python") )

if( libtype STREQUAL "STATIC" )
Expand Down Expand Up @@ -221,7 +221,6 @@ install(TARGETS mdi
ARCHIVE DESTINATION ${MDI_INSTALL_LIBDIR})

# Provide support for packages

set(CMAKECONFIG_INSTALL_DIR "share/cmake/${PN}")
configure_package_config_file("${CMAKE_CURRENT_SOURCE_DIR}/../cmake/${PN}Config.cmake.in"
"${CMAKE_CURRENT_BINARY_DIR}/${PN}Config.cmake"
Expand Down
2 changes: 1 addition & 1 deletion MDI_Library/mdi_global.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
// MDI version numbers
#define MDI_MAJOR_VERSION_ 1
#define MDI_MINOR_VERSION_ 4
#define MDI_PATCH_VERSION_ 30
#define MDI_PATCH_VERSION_ 31

// length of an MDI command in characters
#define MDI_COMMAND_LENGTH_ 256
Expand Down
15 changes: 2 additions & 13 deletions tests/MDI_Test_Codes/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,20 +1,9 @@
# Check for MPI

if ( NOT ( mpi STREQUAL "OFF") )
find_package(MPI)
endif()
if( NOT MPI_FOUND )
if( mpi STREQUAL "ON" )
message( WARNING "Could not find MPI. Compiling without MPI support." )
endif()
# Include MPI stubs, if needed
if( mpi STREQUAL "OFF" )
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/STUBS_MPI/mpi.h ${CMAKE_CURRENT_BINARY_DIR}/STUBS_MPI/mpi.h COPYONLY)
set(mpi "OFF")
endif()



# Macro to link target to MPI

macro(link_against_mpi _TAR)
if( mpi STREQUAL "ON" )

Expand Down

0 comments on commit b0c4803

Please sign in to comment.