diff --git a/cmake/utils.cmake b/cmake/utils.cmake index 5abf27d402..51c6d6a48c 100644 --- a/cmake/utils.cmake +++ b/cmake/utils.cmake @@ -136,3 +136,47 @@ function(fetch_git_dependency) message(STATUS "Building ${GIT_DEP_NAME}: ...DONE") endfunction() + +# add_java_sample() +# CMake function to generate and build java sample. +# Parameters: +# the java filename +# e.g.: +# add_java_sample(Foo.java) +function(add_java_sample FILE_NAME) + message(STATUS "Building ${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) + #message(FATAL_ERROR "component name: ${COMPONENT_NAME}") + string(REPLACE "_" "" COMPONENT_NAME_LOWER ${COMPONENT_NAME}) + + set(SAMPLE_PATH ${PROJECT_BINARY_DIR}/java/${COMPONENT_NAME}/${SAMPLE_NAME}) + file(MAKE_DIRECTORY ${SAMPLE_PATH}/${JAVA_PACKAGE_PATH}) + + file(COPY ${FILE_NAME} DESTINATION ${SAMPLE_PATH}/${JAVA_PACKAGE_PATH}) + + string(TOLOWER ${SAMPLE_NAME} JAVA_SAMPLE_PROJECT) + configure_file( + ${PROJECT_SOURCE_DIR}/ortools/java/pom-sample.xml.in + ${SAMPLE_PATH}/pom.xml + @ONLY) + + add_custom_target(java_sample_${SAMPLE_NAME} ALL + DEPENDS ${SAMPLE_PATH}/pom.xml + COMMAND ${MAVEN_EXECUTABLE} compile + WORKING_DIRECTORY ${SAMPLE_PATH}) + add_dependencies(java_sample_${SAMPLE_NAME} java_package) + + if(BUILD_TESTING) + add_test( + NAME java_${SAMPLE_NAME} + COMMAND ${MAVEN_EXECUTABLE} exec:java + -Dexec.mainClass=com.google.ortools.${COMPONENT_NAME_LOWER}.samples.${SAMPLE_NAME} + WORKING_DIRECTORY ${SAMPLE_PATH}) + endif() + + message(STATUS "Building ${FILE_NAME}: ...DONE") +endfunction() + diff --git a/ortools/algorithms/samples/CMakeLists.txt b/ortools/algorithms/samples/CMakeLists.txt index a243af671f..0ad42df5d3 100644 --- a/ortools/algorithms/samples/CMakeLists.txt +++ b/ortools/algorithms/samples/CMakeLists.txt @@ -26,7 +26,10 @@ if(BUILD_PYTHON_SAMPLES) endif() if(BUILD_JAVA_SAMPLES) - + file(GLOB JAVA_SRCS "*.java") + foreach(SAMPLE IN LISTS JAVA_SRCS) + add_java_sample(${SAMPLE}) + endforeach() endif() if(BUILD_DOTNET_SAMPLES) diff --git a/ortools/constraint_solver/samples/CMakeLists.txt b/ortools/constraint_solver/samples/CMakeLists.txt index 20de39ff85..d8853df288 100644 --- a/ortools/constraint_solver/samples/CMakeLists.txt +++ b/ortools/constraint_solver/samples/CMakeLists.txt @@ -49,7 +49,10 @@ if(BUILD_PYTHON_SAMPLES) endif() if(BUILD_JAVA_SAMPLES) - + file(GLOB JAVA_SRCS "*.java") + foreach(SAMPLE IN LISTS JAVA_SRCS) + add_java_sample(${SAMPLE}) + endforeach() endif() if(BUILD_DOTNET_SAMPLES) diff --git a/ortools/graph/samples/CMakeLists.txt b/ortools/graph/samples/CMakeLists.txt index 31bd1d5772..1998b2f36f 100644 --- a/ortools/graph/samples/CMakeLists.txt +++ b/ortools/graph/samples/CMakeLists.txt @@ -27,7 +27,10 @@ if(BUILD_PYTHON_SAMPLES) endif() if(BUILD_JAVA_SAMPLES) - + file(GLOB JAVA_SRCS "*.java") + foreach(SAMPLE IN LISTS JAVA_SRCS) + add_java_sample(${SAMPLE}) + endforeach() endif() if(BUILD_DOTNET_SAMPLES) diff --git a/ortools/java/Test.java b/ortools/java/CMakeTest.java similarity index 100% rename from ortools/java/Test.java rename to ortools/java/CMakeTest.java diff --git a/ortools/linear_solver/samples/CMakeLists.txt b/ortools/linear_solver/samples/CMakeLists.txt index 97b8e15570..ce4c90408a 100644 --- a/ortools/linear_solver/samples/CMakeLists.txt +++ b/ortools/linear_solver/samples/CMakeLists.txt @@ -35,7 +35,10 @@ if(BUILD_PYTHON_SAMPLES) endif() if(BUILD_JAVA_SAMPLES) - + file(GLOB JAVA_SRCS "*.java") + foreach(SAMPLE IN LISTS JAVA_SRCS) + add_java_sample(${SAMPLE}) + endforeach() endif() if(BUILD_DOTNET_SAMPLES) diff --git a/ortools/sat/samples/CMakeLists.txt b/ortools/sat/samples/CMakeLists.txt index 16795ebd98..d11a2f7e82 100644 --- a/ortools/sat/samples/CMakeLists.txt +++ b/ortools/sat/samples/CMakeLists.txt @@ -48,7 +48,10 @@ if(BUILD_PYTHON_SAMPLES) endif() if(BUILD_JAVA_SAMPLES) - + file(GLOB JAVA_SRCS "*.java") + foreach(SAMPLE IN LISTS JAVA_SRCS) + add_java_sample(${SAMPLE}) + endforeach() endif() if(BUILD_DOTNET_SAMPLES)