diff --git a/cmake/python.cmake b/cmake/python.cmake index a3cb2de324..b6976ac712 100644 --- a/cmake/python.cmake +++ b/cmake/python.cmake @@ -684,20 +684,44 @@ endfunction() # add_python_example() # CMake function to generate and build python example. # Parameters: -# the python filename +# FILE_NAME: the Python filename +# COMPONENT_NAME: name of the ortools/ subdir where the test is located +# note: automatically determined if located in ortools//samples/ # e.g.: -# add_python_example(foo.py) -function(add_python_example FILE_NAME) - message(STATUS "Configuring example ${FILE_NAME} ...") - get_filename_component(EXAMPLE_NAME ${FILE_NAME} NAME_WE) - get_filename_component(COMPONENT_DIR ${FILE_NAME} DIRECTORY) - get_filename_component(COMPONENT_NAME ${COMPONENT_DIR} NAME) +# add_python_example( +# FILE_NAME +# ${PROJECT_SOURCE_DIR}/examples/foo/bar.py +# COMPONENT_NAME +# foo +# ) +function(add_python_example) + set(options "") + set(oneValueArgs FILE_NAME COMPONENT_NAME) + set(multiValueArgs "") + cmake_parse_arguments(EXAMPLE + "${options}" + "${oneValueArgs}" + "${multiValueArgs}" + ${ARGN} + ) +if(NOT EXAMPLE_FILE_NAME) + message(FATAL_ERROR "no FILE_NAME provided") + endif() + get_filename_component(EXAMPLE_NAME ${EXAMPLE_FILE_NAME} NAME_WE) + + message(STATUS "Configuring example ${EXAMPLE_FILE_NAME} ...") + + if(NOT EXAMPLE_COMPONENT_NAME) + # example is located in example// + get_filename_component(EXAMPLE_DIR ${EXAMPLE_FILE_NAME} DIRECTORY) + get_filename_component(EXAMPLE_COMPONENT_NAME ${EXAMPLE_DIR} NAME) + endif() if(BUILD_TESTING) add_test( - NAME python_${COMPONENT_NAME}_${EXAMPLE_NAME} - COMMAND ${VENV_Python3_EXECUTABLE} ${FILE_NAME} + NAME python_${EXAMPLE_COMPONENT_NAME}_${EXAMPLE_NAME} + COMMAND ${VENV_Python3_EXECUTABLE} ${EXAMPLE_FILE_NAME} WORKING_DIRECTORY ${VENV_DIR}) endif() - message(STATUS "Configuring example ${FILE_NAME} done") + message(STATUS "Configuring example ${EXAMPLE_FILE_NAME} ...DONE") endfunction() diff --git a/examples/contrib/CMakeLists.txt b/examples/contrib/CMakeLists.txt index 302c3f245f..4915e2e431 100644 --- a/examples/contrib/CMakeLists.txt +++ b/examples/contrib/CMakeLists.txt @@ -38,7 +38,7 @@ if(BUILD_PYTHON_EXAMPLES) #list(FILTER PYTHON_SRCS EXCLUDE REGEX ".*/project_scheduling_sat.py") # ISSUE #list(FILTER PYTHON_SRCS EXCLUDE REGEX ".*/sports_schedule_sat.py") # ISSUE foreach(FILE_NAME IN LISTS PYTHON_SRCS) - add_python_example(${FILE_NAME}) + add_python_example(FILE_NAME ${FILE_NAME}) endforeach() endif() diff --git a/examples/python/CMakeLists.txt b/examples/python/CMakeLists.txt index 3cfa96d08f..8a38e49b65 100644 --- a/examples/python/CMakeLists.txt +++ b/examples/python/CMakeLists.txt @@ -22,5 +22,5 @@ list(FILTER PYTHON_SRCS EXCLUDE REGEX ".*/bus_driver_scheduling_sat.py") # too l list(FILTER PYTHON_SRCS EXCLUDE REGEX ".*/cvrptw_plot.py") # depend on numpy foreach(FILE_NAME IN LISTS PYTHON_SRCS) - add_python_example(${FILE_NAME}) + add_python_example(FILE_NAME ${FILE_NAME}) endforeach() diff --git a/examples/tests/CMakeLists.txt b/examples/tests/CMakeLists.txt index 4781cbda3c..5547a97b3c 100644 --- a/examples/tests/CMakeLists.txt +++ b/examples/tests/CMakeLists.txt @@ -25,7 +25,7 @@ endif() if(BUILD_PYTHON_EXAMPLES) file(GLOB PYTHON_SRCS "*.py") foreach(FILE_NAME IN LISTS PYTHON_SRCS) - add_python_example(${FILE_NAME}) + add_python_example(FILE_NAME ${FILE_NAME}) endforeach() endif()