convert plugin compilation to also use add_subdirectory() instead of external project

This commit is contained in:
Axel Kohlmeyer
2021-09-11 07:01:48 -04:00
parent 932b3cabda
commit 30558c0cd6
2 changed files with 39 additions and 33 deletions

View File

@ -14,17 +14,21 @@ endif()
project(plugins VERSION 1.0 LANGUAGES CXX) project(plugins VERSION 1.0 LANGUAGES CXX)
# NOTE: the next line should be commented out when used outside of the LAMMPS package # when this file is included as subdirectory in the LAMMPS build, many settings are directly imported
get_filename_component(LAMMPS_SOURCE_DIR ${PROJECT_SOURCE_DIR}/../../src ABSOLUTE) if(LAMMPS_DIR)
set(LAMMPS_HEADER_DIR ${LAMMPS_SOURCE_DIR} CACHE PATH "Location of LAMMPS headers") set(LAMMPS_HEADER_DIR ${LAMMPS_SOURCE_DIR})
if(NOT LAMMPS_HEADER_DIR) else()
message(FATAL_ERROR "Must set LAMMPS_HEADER_DIR") # NOTE: the next line should be commented out when used outside of the LAMMPS package
endif() get_filename_component(LAMMPS_SOURCE_DIR ${PROJECT_SOURCE_DIR}/../../src ABSOLUTE)
set(LAMMPS_HEADER_DIR ${LAMMPS_SOURCE_DIR} CACHE PATH "Location of LAMMPS headers")
# by default, install into $HOME/.local (not /usr/local), if(NOT LAMMPS_HEADER_DIR)
# so that no root access (and sudo) is needed message(FATAL_ERROR "Must set LAMMPS_HEADER_DIR")
if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT) endif()
set(CMAKE_INSTALL_PREFIX "$ENV{HOME}/.local" CACHE PATH "Default install path" FORCE) # by default, install into $HOME/.local (not /usr/local),
# so that no root access (and sudo) is needed
if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
set(CMAKE_INSTALL_PREFIX "$ENV{HOME}/.local" CACHE PATH "Default install path" FORCE)
endif()
endif() endif()
# C++11 is required # C++11 is required
@ -41,9 +45,11 @@ if(CMAKE_SYSTEM_NAME STREQUAL Windows)
message(FATAL_ERROR "LAMMPS plugins are currently not supported on Windows") message(FATAL_ERROR "LAMMPS plugins are currently not supported on Windows")
endif() endif()
set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}) set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR})
include(CheckIncludeFileCXX) include(CheckIncludeFileCXX)
include(LAMMPSInterfaceCXX) if(NOT LAMMPS_DIR)
include(LAMMPSInterfaceCXX)
endif()
########################## ##########################
# building the plugins # building the plugins
@ -71,3 +77,6 @@ if(CMAKE_SYSTEM_NAME STREQUAL Darwin)
set_target_properties(morse2plugin nve2plugin helloplugin zero2plugin PROPERTIES set_target_properties(morse2plugin nve2plugin helloplugin zero2plugin PROPERTIES
LINK_FLAGS "-Wl,-undefined,dynamic_lookup") LINK_FLAGS "-Wl,-undefined,dynamic_lookup")
endif() endif()
add_custom_target(plugins ALL ${CMAKE_COMMAND} -E echo "Building Plugins"
DEPENDS morse2plugin nve2plugin helloplugin zero2plugin morse2plugin)

View File

@ -1,34 +1,31 @@
# build LAMMPS plugins, but not on Windows # build LAMMPS plugins, but not on Windows
if((NOT (CMAKE_SYSTEM_NAME STREQUAL "Windows")) AND PKG_PLUGIN) if((NOT (CMAKE_SYSTEM_NAME STREQUAL "Windows")) AND PKG_PLUGIN)
include(ExternalProject) add_subdirectory(${LAMMPS_DIR}/examples/plugins ${CMAKE_BINARY_DIR}/build-plugins)
ExternalProject_Add(plugins
SOURCE_DIR "${LAMMPS_DIR}/examples/plugins"
BINARY_DIR ${CMAKE_BINARY_DIR}/build-plugins
INSTALL_DIR ${CMAKE_BINARY_DIR}
CMAKE_ARGS ${CMAKE_REQUEST_PIC}
-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
-DCMAKE_INSTALL_PREFIX=<INSTALL_DIR>
-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
-DCMAKE_MAKE_PROGRAM=${CMAKE_MAKE_PROGRAM}
-DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE}
BUILD_BYPRODUCTS <BINARY_DIR>/morse2plugin${CMAKE_SHARED_MODULE_SUFFIX}
<BINARY_DIR>/nve2plugin${CMAKE_SHARED_MODULE_SUFFIX}
<BINARY_DIR>/helloplugin${CMAKE_SHARED_MODULE_SUFFIX}
INSTALL_COMMAND ${CMAKE_COMMAND} -E copy_if_different
<BINARY_DIR>/morse2plugin${CMAKE_SHARED_MODULE_SUFFIX}
<BINARY_DIR>/nve2plugin${CMAKE_SHARED_MODULE_SUFFIX}
<BINARY_DIR>/helloplugin${CMAKE_SHARED_MODULE_SUFFIX}
${CMAKE_CURRENT_BINARY_DIR}
TEST_COMMAND "")
endif() endif()
add_executable(test_simple_commands test_simple_commands.cpp) add_executable(test_simple_commands test_simple_commands.cpp)
if(PKG_PLUGIN) if(PKG_PLUGIN)
add_custom_target(installplugins
${CMAKE_COMMAND} -E copy_if_different
${CMAKE_BINARY_DIR}/build-plugins/$<CONFIG>/morse2plugin${CMAKE_SHARED_MODULE_SUFFIX}
${CMAKE_BINARY_DIR}/build-plugins/$<CONFIG>/nve2plugin${CMAKE_SHARED_MODULE_SUFFIX}
${CMAKE_BINARY_DIR}/build-plugins/$<CONFIG>/helloplugin${CMAKE_SHARED_MODULE_SUFFIX}
${CMAKE_BINARY_DIR}
DEPENDS plugins)
add_dependencies(test_simple_commands plugins) add_dependencies(test_simple_commands plugins)
target_compile_definitions(test_simple_commands PRIVATE LMP_PLUGIN)
endif() endif()
target_link_libraries(test_simple_commands PRIVATE lammps gmock) target_link_libraries(test_simple_commands PRIVATE lammps gmock)
add_test(NAME SimpleCommands COMMAND test_simple_commands WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) add_test(NAME SimpleCommands COMMAND test_simple_commands WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
if(APPLE)
set_tests_properties(SimpleCommands PROPERTIES
ENVIRONMENT "DYLD_LIBRARY_PATH=${CMAKE_BINARY_DIR}/build-plugins/$<CONFIG>/:${DYLD_LIBRARY_PATH}")
else()
set_tests_properties(SimpleCommands PROPERTIES
ENVIRONMENT "LD_LIBRARY_PATH=${CMAKE_BINARY_DIR}/build-plugins/$<CONFIG>/:${LD_LIBRARY_PATH}")
endif()
add_executable(test_lattice_region test_lattice_region.cpp) add_executable(test_lattice_region test_lattice_region.cpp)
target_link_libraries(test_lattice_region PRIVATE lammps gmock) target_link_libraries(test_lattice_region PRIVATE lammps gmock)