From 21fd586d327a239c4ea95bad26b342d81f213563 Mon Sep 17 00:00:00 2001 From: Corentin Le Molgat Date: Tue, 16 Jan 2024 15:14:34 +0100 Subject: [PATCH] cmake: rework add_python_sample() --- cmake/python.cmake | 46 ++++++++++++++----- ortools/algorithms/samples/CMakeLists.txt | 2 +- .../constraint_solver/samples/CMakeLists.txt | 2 +- ortools/graph/samples/CMakeLists.txt | 2 +- ortools/linear_solver/samples/CMakeLists.txt | 2 +- .../math_opt/samples/python/CMakeLists.txt | 4 +- ortools/pdlp/samples/CMakeLists.txt | 2 +- ortools/sat/samples/CMakeLists.txt | 2 +- 8 files changed, 44 insertions(+), 18 deletions(-) diff --git a/cmake/python.cmake b/cmake/python.cmake index 8ffc4dab23..a3cb2de324 100644 --- a/cmake/python.cmake +++ b/cmake/python.cmake @@ -635,23 +635,47 @@ endif() # add_python_sample() # CMake function to generate and build python sample. # 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_sample(foo.py) -function(add_python_sample FILE_NAME) - message(STATUS "Configuring sample ${FILE_NAME} ...") - get_filename_component(SAMPLE_NAME ${FILE_NAME} NAME_WE) - get_filename_component(SAMPLE_DIR ${FILE_NAME} DIRECTORY) - get_filename_component(COMPONENT_DIR ${SAMPLE_DIR} DIRECTORY) - get_filename_component(COMPONENT_NAME ${COMPONENT_DIR} NAME) +# add_python_sample( +# FILE_NAME +# ${PROJECT_SOURCE_DIR}/ortools/foo/sample/bar.py +# COMPONENT_NAME +# foo +# ) +function(add_python_sample) + set(options "") + set(oneValueArgs FILE_NAME COMPONENT_NAME) + set(multiValueArgs "") + cmake_parse_arguments(SAMPLE + "${options}" + "${oneValueArgs}" + "${multiValueArgs}" + ${ARGN} + ) + if(NOT SAMPLE_FILE_NAME) + message(FATAL_ERROR "no FILE_NAME provided") + endif() + get_filename_component(SAMPLE_NAME ${SAMPLE_FILE_NAME} NAME_WE) + + message(STATUS "Configuring sample ${SAMPLE_FILE_NAME} ...") + + if(NOT SAMPLE_COMPONENT_NAME) + # sample is located in ortools//sample/ + get_filename_component(SAMPLE_DIR ${SAMPLE_FILE_NAME} DIRECTORY) + get_filename_component(COMPONENT_DIR ${SAMPLE_DIR} DIRECTORY) + get_filename_component(SAMPLE_COMPONENT_NAME ${COMPONENT_DIR} NAME) + endif() if(BUILD_TESTING) add_test( - NAME python_${COMPONENT_NAME}_${SAMPLE_NAME} - COMMAND ${VENV_Python3_EXECUTABLE} ${FILE_NAME} + NAME python_${SAMPLE_COMPONENT_NAME}_${SAMPLE_NAME} + COMMAND ${VENV_Python3_EXECUTABLE} ${SAMPLE_FILE_NAME} WORKING_DIRECTORY ${VENV_DIR}) endif() - message(STATUS "Configuring sample ${FILE_NAME} done") + message(STATUS "Configuring sample ${SAMPLE_FILE_NAME} ...DONE") endfunction() ###################### diff --git a/ortools/algorithms/samples/CMakeLists.txt b/ortools/algorithms/samples/CMakeLists.txt index d9d3aeb26c..d7e1735ef5 100644 --- a/ortools/algorithms/samples/CMakeLists.txt +++ b/ortools/algorithms/samples/CMakeLists.txt @@ -25,7 +25,7 @@ endif() if(BUILD_PYTHON_SAMPLES) file(GLOB PYTHON_SRCS "*.py") foreach(SAMPLE IN LISTS PYTHON_SRCS) - add_python_sample(${SAMPLE}) + add_python_sample(FILE_NAME ${SAMPLE}) endforeach() endif() diff --git a/ortools/constraint_solver/samples/CMakeLists.txt b/ortools/constraint_solver/samples/CMakeLists.txt index d9d3aeb26c..d7e1735ef5 100644 --- a/ortools/constraint_solver/samples/CMakeLists.txt +++ b/ortools/constraint_solver/samples/CMakeLists.txt @@ -25,7 +25,7 @@ endif() if(BUILD_PYTHON_SAMPLES) file(GLOB PYTHON_SRCS "*.py") foreach(SAMPLE IN LISTS PYTHON_SRCS) - add_python_sample(${SAMPLE}) + add_python_sample(FILE_NAME ${SAMPLE}) endforeach() endif() diff --git a/ortools/graph/samples/CMakeLists.txt b/ortools/graph/samples/CMakeLists.txt index d9d3aeb26c..d7e1735ef5 100644 --- a/ortools/graph/samples/CMakeLists.txt +++ b/ortools/graph/samples/CMakeLists.txt @@ -25,7 +25,7 @@ endif() if(BUILD_PYTHON_SAMPLES) file(GLOB PYTHON_SRCS "*.py") foreach(SAMPLE IN LISTS PYTHON_SRCS) - add_python_sample(${SAMPLE}) + add_python_sample(FILE_NAME ${SAMPLE}) endforeach() endif() diff --git a/ortools/linear_solver/samples/CMakeLists.txt b/ortools/linear_solver/samples/CMakeLists.txt index d9d3aeb26c..d7e1735ef5 100644 --- a/ortools/linear_solver/samples/CMakeLists.txt +++ b/ortools/linear_solver/samples/CMakeLists.txt @@ -25,7 +25,7 @@ endif() if(BUILD_PYTHON_SAMPLES) file(GLOB PYTHON_SRCS "*.py") foreach(SAMPLE IN LISTS PYTHON_SRCS) - add_python_sample(${SAMPLE}) + add_python_sample(FILE_NAME ${SAMPLE}) endforeach() endif() diff --git a/ortools/math_opt/samples/python/CMakeLists.txt b/ortools/math_opt/samples/python/CMakeLists.txt index 1629a0c6d2..aa4e026575 100644 --- a/ortools/math_opt/samples/python/CMakeLists.txt +++ b/ortools/math_opt/samples/python/CMakeLists.txt @@ -18,6 +18,8 @@ endif() if(BUILD_PYTHON_SAMPLES) file(GLOB PYTHON_SRCS "*.py") foreach(SAMPLE IN LISTS PYTHON_SRCS) - add_python_sample(${SAMPLE}) + add_python_sample( + FILE_NAME ${SAMPLE} + COMPONENT_NAME math_opt) endforeach() endif() diff --git a/ortools/pdlp/samples/CMakeLists.txt b/ortools/pdlp/samples/CMakeLists.txt index ff66fea4c0..1e384ba3ec 100644 --- a/ortools/pdlp/samples/CMakeLists.txt +++ b/ortools/pdlp/samples/CMakeLists.txt @@ -25,6 +25,6 @@ endif() if(BUILD_PYTHON_SAMPLES) file(GLOB PYTHON_SRCS "*.py") foreach(SAMPLE IN LISTS PYTHON_SRCS) - add_python_sample(${SAMPLE}) + add_python_sample(FILE_NAME ${SAMPLE}) endforeach() endif() diff --git a/ortools/sat/samples/CMakeLists.txt b/ortools/sat/samples/CMakeLists.txt index d9d3aeb26c..d7e1735ef5 100644 --- a/ortools/sat/samples/CMakeLists.txt +++ b/ortools/sat/samples/CMakeLists.txt @@ -25,7 +25,7 @@ endif() if(BUILD_PYTHON_SAMPLES) file(GLOB PYTHON_SRCS "*.py") foreach(SAMPLE IN LISTS PYTHON_SRCS) - add_python_sample(${SAMPLE}) + add_python_sample(FILE_NAME ${SAMPLE}) endforeach() endif()