Skip to content
Snippets Groups Projects
Commit 342f4762 authored by Andreas Dedner's avatar Andreas Dedner
Browse files

Merge branch 'bugfix/overhaul-dune-python-add-test' into 'master'

Bugfix/overhaul dune python add test

See merge request !1130
parents 183ca5b6 5690837d
Branches
Tags
1 merge request!1130Bugfix/overhaul dune python add test
Pipeline #52588 passed
Pipeline: Dune Nightly Test

#52589

    ......@@ -83,7 +83,7 @@ if(Python3_Interpreter_FOUND)
    dune_python_find_package(PACKAGE pip)
    # Add python related meta targets
    add_custom_target(test_python)
    add_custom_target(test_python COMMAND ctest CMD_ARGS -L python)
    add_custom_target(install_python)
    # this option enables the build of Python bindings for DUNE modules
    ......
    ......@@ -10,26 +10,17 @@
    #
    # .. note::
    #
    # The script will be executed using :code:`${Python3_EXECUTABLE} SCRIPT`. If the INTERPRETER
    # option is given, that interpreter is used instead.
    # The script will be executed using :code:`${Python3_EXECUTABLE} SCRIPT`.
    #
    # .. cmake_param:: MODULE
    # :multi:
    #
    # The Python module to be executed. It will be executed during :code:`make test_python`
    # The Python module to execute using the python interpreter. It will be executed during :code:`make test_python`
    # and during `ctest`. You are required to either pass SCRIPT or MODULE.
    #
    # .. note::
    #
    # The script will be executed using :code:`${Python3_EXECUTABLE} -m MODULE`. If the INTERPRETER
    # option is given, that interpreter is used instead.
    #
    # .. cmake_param:: WORKING_DIRECTORY
    # :single:
    # :argname: dir
    #
    # The working directory of the command. Defaults to
    # the current build directory.
    # The script will be executed using :code:`${Python3_EXECUTABLE} -m MODULE`.
    #
    # .. cmake_param:: NAME
    # :single:
    ......@@ -41,25 +32,27 @@
    # build system. Added commands are run, when the target
    # :code:`test_python` is built and during :code:`ctest`.
    #
    # This function uses `dune_add_test` and forwards all unparsed arguments.
    #
    include_guard(GLOBAL)
    function(dune_python_add_test)
    # Parse Arguments
    set(OPTION)
    set(SINGLE WORKING_DIRECTORY NAME)
    set(MULTI SCRIPT COMMAND LABELS MODULE)
    set(SINGLE NAME)
    set(MULTI SCRIPT MODULE CMAKE_GUARD LABELS TIMEOUT)
    cmake_parse_arguments(PYTEST "" "${SINGLE}" "${MULTI}" ${ARGN})
    if(PYTEST_COMMAND)
    message(FATAL_ERROR "dune_python_add_test: COMMAND argument should not be used, use SCRIPT instead providing only the Python script and not the Python interpreter")
    endif()
    if(PYTEST_UNPARSED_ARGUMENTS)
    message(WARNING "Unparsed arguments in dune_python_add_test: This often indicates typos!")
    endif()
    # Apply defaults
    if(NOT PYTEST_WORKING_DIRECTORY)
    set(PYTEST_WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
    set(PYTEST_CMAKE_GUARD ${PYTEST_CMAKE_GUARD} DUNE_ENABLE_PYTHONBINDINGS)
    set(PYTEST_LABELS ${PYTEST_LABELS} python)
    if(NOT PYTEST_TIMEOUT)
    set(PYTEST_TIMEOUT 3600)
    endif()
    if((NOT PYTEST_MODULE) AND (NOT PYTEST_SCRIPT))
    message(FATAL_ERROR "dune_python_add_test: Either SCRIPT or MODULE need to be specified!")
    endif()
    ......@@ -78,32 +71,18 @@ function(dune_python_add_test)
    string(REPLACE "/" "_" PYTEST_NAME ${commandstr})
    endif()
    if(DUNE_PYTHON_VENVSETUP)
    # Actually run the command
    add_custom_target(target_${PYTEST_NAME}
    COMMAND ${CMAKE_BINARY_DIR}/run-in-dune-env python ${PYTEST_SCRIPT}
    WORKING_DIRECTORY ${PYTEST_WORKING_DIRECTORY})
    else()
    # message(FATAL_ERROR "SHOULDN'T BE HERE")
    # add_custom_target(target_${PYTEST_NAME}
    # COMMAND ${CMAKE_COMMAND} -E echo \"Test not run: python setup failed\")
    add_custom_target(target_${PYTEST_NAME}
    COMMAND ${CMAKE_BINARY_DIR}/run-in-dune-env python ${PYTEST_SCRIPT}
    WORKING_DIRECTORY ${PYTEST_WORKING_DIRECTORY})
    endif()
    # Build this during make test_python
    add_dependencies(test_python target_${PYTEST_NAME})
    # make sure each label exists and its name is acceptable
    dune_declare_test_label(LABELS ${PYTEST_LABELS})
    set(PYTEST_COMMAND ${CMAKE_BINARY_DIR}/run-in-dune-env)
    set(PYTEST_CMD_ARGS python ${PYTEST_SCRIPT})
    # Also build this during ctest
    _add_test(NAME ${PYTEST_NAME}
    COMMAND ${CMAKE_BINARY_DIR}/run-in-dune-env python ${PYTEST_SCRIPT}
    WORKING_DIRECTORY ${PYTEST_WORKING_DIRECTORY}
    )
    set_tests_properties(${PYTEST_NAME} PROPERTIES SKIP_RETURN_CODE 77)
    # Set the labels on the test
    set_tests_properties(${PYTEST_NAME} PROPERTIES LABELS "${PYTEST_LABELS}")
    add_custom_target(target_${PYTEST_NAME})
    dune_add_test(NAME ${PYTEST_NAME}
    TARGET target_${PYTEST_NAME}
    COMMAND ${PYTEST_COMMAND}
    CMD_ARGS ${PYTEST_CMD_ARGS}
    PYTHON_TEST
    CMAKE_GUARD ${PYTEST_CMAKE_GUARD}
    LABELS ${PYTEST_LABELS}
    TIMEOUT ${PYTEST_TIMEOUT}
    ${PYTEST_UNPARSED_ARGUMENTS}
    )
    endfunction()
    ......@@ -157,6 +157,12 @@
    # any timeout setting in ctest (see `cmake --help-property TIMEOUT`). If you
    # specify the MPI_RANKS option, you need to specify a TIMEOUT.
    #
    # .. cmake_param:: WORKING_DIRECTORY
    # :single:
    #
    # Set the WORKING_DIRECTORY test property to specify the working directory in which to execute the test.
    # If not specified the test will be run with the current working directory set to the build directory corresponding to the current source directory.
    #
    # .. cmake_param:: LABELS
    # :multi:
    #
    ......@@ -206,6 +212,10 @@
    # build all tests during `make all`. Note, that this may take quite some time for some modules.
    # If not in use, you have to build tests through the target :code:`build_tests`.
    #
    # .. cmake_variable:: PYTHON_TEST
    #
    # This flag specifies a python test and is set by the dune_python_add_test command. It disables the check on the existence of the target file.
    #
    include_guard(GLOBAL)
    # enable the testing suite on the CMake side.
    ......@@ -249,8 +259,8 @@ if(NOT DUNE_MAX_TEST_CORES)
    endif()
    function(dune_add_test)
    set(OPTIONS EXPECT_COMPILE_FAIL EXPECT_FAIL SKIP_ON_77 COMPILE_ONLY)
    set(SINGLEARGS NAME TARGET TIMEOUT)
    set(OPTIONS EXPECT_COMPILE_FAIL EXPECT_FAIL SKIP_ON_77 COMPILE_ONLY PYTHON_TEST)
    set(SINGLEARGS NAME TARGET TIMEOUT WORKING_DIRECTORY)
    set(MULTIARGS SOURCES COMPILE_DEFINITIONS COMPILE_FLAGS LINK_LIBRARIES CMD_ARGS MPI_RANKS COMMAND CMAKE_GUARD LABELS)
    cmake_parse_arguments(ADDTEST "${OPTIONS}" "${SINGLEARGS}" "${MULTIARGS}" ${ARGN})
    ......@@ -298,6 +308,9 @@ function(dune_add_test)
    if(NOT ADDTEST_TIMEOUT)
    set(ADDTEST_TIMEOUT 300)
    endif()
    if(NOT ADDTEST_WORKING_DIRECTORY)
    set(ADDTEST_WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}")
    endif()
    foreach(num ${ADDTEST_MPI_RANKS})
    if(NOT "${num}" MATCHES "[1-9][0-9]*")
    message(FATAL_ERROR "${num} was given to the MPI_RANKS arugment of dune_add_test, but it does not seem like a correct processor number")
    ......@@ -397,10 +410,11 @@ function(dune_add_test)
    # Now add the actual test
    _add_test(NAME ${ACTUAL_NAME}
    COMMAND "${ACTUAL_TESTCOMMAND}" ${ACTUAL_CMD_ARGS}
    WORKING_DIRECTORY "${ADDTEST_WORKING_DIRECTORY}"
    )
    # Make the test depend on the existence of the target to trigger "Not Run" response
    if(NOT ADDTEST_EXPECT_COMPILE_FAIL)
    if(NOT ADDTEST_EXPECT_COMPILE_FAIL AND NOT ADDTEST_PYTHON_TEST)
    set_tests_properties(${ACTUAL_NAME} PROPERTIES REQUIRED_FILES $<TARGET_FILE:${ADDTEST_TARGET}>)
    endif()
    # Define the number of processors (ctest will coordinate this with the -j option)
    ......
    dune_python_add_test(NAME pythontests
    SCRIPT pythontests.py
    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
    LABELS quick)
    dune_python_add_test(NAME pythontests_parallel
    SCRIPT pythontests.py
    INTERPRETER ${CMAKE_BINARY_DIR}/run-in-dune-env mpirun -n 4 python
    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
    CMAKE_GUARD HAVE_MPI
    LABELS quick)
    MPI_RANKS 1 2 4
    LABELS quick
    )
    dune_add_test(SOURCES test_embed1.cc
    LINK_LIBRARIES ${DUNE_LIBS} ${Python3_LIBRARIES}
    ......@@ -16,9 +11,12 @@ dune_add_test(SOURCES test_embed1.cc
    COMMAND ${CMAKE_BINARY_DIR}/run-in-dune-env
    CMD_ARGS $<TARGET_FILE:test_embed1>
    )
    target_compile_definitions(test_embed1 PRIVATE PYTHON_INTERPRETER=L"${DUNE_PYTHON_VIRTUALENV_EXECUTABLE}")
    # add_executable(test_embed1 EXCLUDE_FROM_ALL test_embed1.cc)
    # target_link_libraries(test_embed1 PUBLIC ${DUNE_LIBS} ${Python3_LIBRARIES})
    add_executable(test_embed2 EXCLUDE_FROM_ALL test_embed2.cc)
    target_link_libraries(test_embed2 PUBLIC ${DUNE_LIBS} ${Python3_LIBRARIES})
    # the following should apparently work but doesn't target_link_libraries(test_embed2 PRIVATE Python3::Python)
    dune_add_test(SOURCES test_embed2.cc
    LINK_LIBRARIES ${DUNE_LIBS} ${Python3_LIBRARIES}
    LABELS quick
    COMMAND ${CMAKE_BINARY_DIR}/run-in-dune-env
    CMD_ARGS $<TARGET_FILE:test_embed2>
    )
    target_compile_definitions(test_embed2 PRIVATE PYTHON_INTERPRETER=L"${DUNE_PYTHON_VIRTUALENV_EXECUTABLE}")
    ......@@ -6,6 +6,7 @@
    int main()
    {
    Py_SetProgramName(PYTHON_INTERPRETER);
    /*
    remark: combine getting the guard and loading
    dune.common in a single 'initialization' function -
    ......
    ......@@ -7,8 +7,7 @@
    #include<Python.h>
    int main()
    {
    const wchar_t* path = L"/home/domse/buildsytem/build/common-build/dune-python-env/bin/python";
    Py_SetProgramName(path);
    Py_SetProgramName(PYTHON_INTERPRETER);
    pybind11::scoped_interpreter guard{};
    auto global = pybind11::dict(pybind11::module::import("__main__").attr("__dict__"));
    ......
    0% Loading or .
    You are about to add 0 people to the discussion. Proceed with caution.
    Please register or to comment