# -*- mode: cmake -*-
# vi: set ft=cmake :

# Copyright (c) 2017, Massachusetts Institute of Technology.
# Copyright (c) 2017, Toyota Research Institute.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
#   list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
#   this list of conditions and the following disclaimer in the documentation
#   and/or other materials provided with the distribution.
#
# * Neither the name of the copyright holder nor the names of its contributors
#   may be used to endorse or promote products derived from this software
#   without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

begin_stage(
  PROJECT_NAME "Drake"
  BUILD_NAME "${DASHBOARD_BUILD_NAME}")

ctest_update(SOURCE "${CTEST_SOURCE_DIRECTORY}" QUIET)

file(WRITE "${CTEST_BINARY_DIRECTORY}/CMakeCache.txt" "${CACHE_CONTENT}")

ctest_configure(BUILD "${CTEST_BINARY_DIRECTORY}"
  OPTIONS --warn-uninitialized
  SOURCE "${CTEST_SOURCE_DIRECTORY}"
  RETURN_VALUE DASHBOARD_CONFIGURE_RETURN_VALUE
  CAPTURE_CMAKE_ERROR DASHBOARD_CONFIGURE_CAPTURE_CMAKE_ERROR
  QUIET)

if(NOT DASHBOARD_CONFIGURE_RETURN_VALUE EQUAL 0 OR DASHBOARD_CONFIGURE_CAPTURE_CMAKE_ERROR EQUAL -1)
  append_step_status("CMAKE CONFIGURE" FAILURE)
endif()

ctest_read_custom_files("${CTEST_BINARY_DIRECTORY}")

ctest_build(BUILD "${CTEST_BINARY_DIRECTORY}"
  NUMBER_ERRORS DASHBOARD_BUILD_NUMBER_ERRORS
  NUMBER_WARNINGS DASHBOARD_BUILD_NUMBER_WARNINGS
  RETURN_VALUE DASHBOARD_BUILD_RETURN_VALUE
  CAPTURE_CMAKE_ERROR DASHBOARD_BUILD_CAPTURE_CMAKE_ERROR
  QUIET)

if(DASHBOARD_BUILD_NUMBER_ERRORS GREATER 0 OR NOT DASHBOARD_BUILD_RETURN_VALUE EQUAL 0 OR DASHBOARD_BUILD_CAPTURE_CMAKE_ERROR EQUAL -1)
  append_step_status("CMAKE BUILD" FAILURE)
endif()

if(MATLAB)
  set(CTEST_CUSTOM_MAXIMUM_FAILED_TEST_OUTPUT_SIZE 307200)
  set(CTEST_CUSTOM_MAXIMUM_PASSED_TEST_OUTPUT_SIZE 307200)

  ctest_test(BUILD "${CTEST_BINARY_DIRECTORY}/drake_matlab-build" ${CTEST_TEST_ARGS}
    RETURN_VALUE DASHBOARD_TEST_RETURN_VALUE
    CAPTURE_CMAKE_ERROR DASHBOARD_TEST_CAPTURE_CMAKE_ERROR
    QUIET)
endif()

ctest_submit(RETRY_COUNT 4 RETRY_DELAY 15 QUIET)
