diff --git a/examples/plugins/CMakeLists.txt b/examples/plugins/CMakeLists.txt index 59c2802b45..22bd09775f 100644 --- a/examples/plugins/CMakeLists.txt +++ b/examples/plugins/CMakeLists.txt @@ -14,17 +14,21 @@ endif() project(plugins VERSION 1.0 LANGUAGES CXX) -# NOTE: the next line should be commented out when used outside of the LAMMPS package -get_filename_component(LAMMPS_SOURCE_DIR ${PROJECT_SOURCE_DIR}/../../src ABSOLUTE) -set(LAMMPS_HEADER_DIR ${LAMMPS_SOURCE_DIR} CACHE PATH "Location of LAMMPS headers") -if(NOT LAMMPS_HEADER_DIR) - message(FATAL_ERROR "Must set LAMMPS_HEADER_DIR") -endif() - -# 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) +# when this file is included as subdirectory in the LAMMPS build, many settings are directly imported +if(LAMMPS_DIR) + set(LAMMPS_HEADER_DIR ${LAMMPS_SOURCE_DIR}) +else() + # NOTE: the next line should be commented out when used outside of the LAMMPS package + get_filename_component(LAMMPS_SOURCE_DIR ${PROJECT_SOURCE_DIR}/../../src ABSOLUTE) + set(LAMMPS_HEADER_DIR ${LAMMPS_SOURCE_DIR} CACHE PATH "Location of LAMMPS headers") + if(NOT LAMMPS_HEADER_DIR) + message(FATAL_ERROR "Must set LAMMPS_HEADER_DIR") + endif() + # 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() # 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") endif() -set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}) +set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}) include(CheckIncludeFileCXX) -include(LAMMPSInterfaceCXX) +if(NOT LAMMPS_DIR) + include(LAMMPSInterfaceCXX) +endif() ########################## # building the plugins @@ -71,3 +77,6 @@ if(CMAKE_SYSTEM_NAME STREQUAL Darwin) set_target_properties(morse2plugin nve2plugin helloplugin zero2plugin PROPERTIES LINK_FLAGS "-Wl,-undefined,dynamic_lookup") endif() + +add_custom_target(plugins ALL ${CMAKE_COMMAND} -E echo "Building Plugins" + DEPENDS morse2plugin nve2plugin helloplugin zero2plugin morse2plugin) diff --git a/unittest/commands/CMakeLists.txt b/unittest/commands/CMakeLists.txt index 8891647911..b543e7691e 100644 --- a/unittest/commands/CMakeLists.txt +++ b/unittest/commands/CMakeLists.txt @@ -1,34 +1,31 @@ # build LAMMPS plugins, but not on Windows if((NOT (CMAKE_SYSTEM_NAME STREQUAL "Windows")) AND PKG_PLUGIN) - include(ExternalProject) - 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= - -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} - -DCMAKE_MAKE_PROGRAM=${CMAKE_MAKE_PROGRAM} - -DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE} - BUILD_BYPRODUCTS /morse2plugin${CMAKE_SHARED_MODULE_SUFFIX} - /nve2plugin${CMAKE_SHARED_MODULE_SUFFIX} - /helloplugin${CMAKE_SHARED_MODULE_SUFFIX} - INSTALL_COMMAND ${CMAKE_COMMAND} -E copy_if_different - /morse2plugin${CMAKE_SHARED_MODULE_SUFFIX} - /nve2plugin${CMAKE_SHARED_MODULE_SUFFIX} - /helloplugin${CMAKE_SHARED_MODULE_SUFFIX} - ${CMAKE_CURRENT_BINARY_DIR} - TEST_COMMAND "") + add_subdirectory(${LAMMPS_DIR}/examples/plugins ${CMAKE_BINARY_DIR}/build-plugins) endif() add_executable(test_simple_commands test_simple_commands.cpp) + if(PKG_PLUGIN) + add_custom_target(installplugins + ${CMAKE_COMMAND} -E copy_if_different + ${CMAKE_BINARY_DIR}/build-plugins/$/morse2plugin${CMAKE_SHARED_MODULE_SUFFIX} + ${CMAKE_BINARY_DIR}/build-plugins/$/nve2plugin${CMAKE_SHARED_MODULE_SUFFIX} + ${CMAKE_BINARY_DIR}/build-plugins/$/helloplugin${CMAKE_SHARED_MODULE_SUFFIX} + ${CMAKE_BINARY_DIR} + DEPENDS plugins) add_dependencies(test_simple_commands plugins) + target_compile_definitions(test_simple_commands PRIVATE LMP_PLUGIN) endif() target_link_libraries(test_simple_commands PRIVATE lammps gmock) 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/$/:${DYLD_LIBRARY_PATH}") +else() + set_tests_properties(SimpleCommands PROPERTIES + ENVIRONMENT "LD_LIBRARY_PATH=${CMAKE_BINARY_DIR}/build-plugins/$/:${LD_LIBRARY_PATH}") +endif() add_executable(test_lattice_region test_lattice_region.cpp) target_link_libraries(test_lattice_region PRIVATE lammps gmock)