################################################################################
#
# cmake script to generate settings/__init__.py
#
# Author: Maxime Arthaud
#
# Contact: ikos@lists.nasa.gov
#
# Notices:
#
# Copyright (c) 2017-2019 United States Government as represented by the
# Administrator of the National Aeronautics and Space Administration.
# All Rights Reserved.
#
# Disclaimers:
#
# No Warranty: THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY OF
# ANY KIND, EITHER EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED
# TO, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL CONFORM TO SPECIFICATIONS,
# ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,
# OR FREEDOM FROM INFRINGEMENT, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL BE
# ERROR FREE, OR ANY WARRANTY THAT DOCUMENTATION, IF PROVIDED, WILL CONFORM TO
# THE SUBJECT SOFTWARE. THIS AGREEMENT DOES NOT, IN ANY MANNER, CONSTITUTE AN
# ENDORSEMENT BY GOVERNMENT AGENCY OR ANY PRIOR RECIPIENT OF ANY RESULTS,
# RESULTING DESIGNS, HARDWARE, SOFTWARE PRODUCTS OR ANY OTHER APPLICATIONS
# RESULTING FROM USE OF THE SUBJECT SOFTWARE.  FURTHER, GOVERNMENT AGENCY
# DISCLAIMS ALL WARRANTIES AND LIABILITIES REGARDING THIRD-PARTY SOFTWARE,
# IF PRESENT IN THE ORIGINAL SOFTWARE, AND DISTRIBUTES IT "AS IS."
#
# Waiver and Indemnity:  RECIPIENT AGREES TO WAIVE ANY AND ALL CLAIMS AGAINST
# THE UNITED STATES GOVERNMENT, ITS CONTRACTORS AND SUBCONTRACTORS, AS WELL
# AS ANY PRIOR RECIPIENT.  IF RECIPIENT'S USE OF THE SUBJECT SOFTWARE RESULTS
# IN ANY LIABILITIES, DEMANDS, DAMAGES, EXPENSES OR LOSSES ARISING FROM SUCH
# USE, INCLUDING ANY DAMAGES FROM PRODUCTS BASED ON, OR RESULTING FROM,
# RECIPIENT'S USE OF THE SUBJECT SOFTWARE, RECIPIENT SHALL INDEMNIFY AND HOLD
# HARMLESS THE UNITED STATES GOVERNMENT, ITS CONTRACTORS AND SUBCONTRACTORS,
# AS WELL AS ANY PRIOR RECIPIENT, TO THE EXTENT PERMITTED BY LAW.
# RECIPIENT'S SOLE REMEDY FOR ANY SUCH MATTER SHALL BE THE IMMEDIATE,
# UNILATERAL TERMINATION OF THIS AGREEMENT.
#
################################################################################

cmake_minimum_required(VERSION 3.4.3 FATAL_ERROR)

set(SRC_DIR "@CMAKE_CURRENT_SOURCE_DIR@")
set(BIN_DIR "@CMAKE_CURRENT_BINARY_DIR@")
set(PACKAGE_VERSION "@PACKAGE_VERSION@")
set(CMAKE_INSTALL_PREFIX "@CMAKE_INSTALL_PREFIX@")
set(CMAKE_BUILD_TYPE "@CMAKE_BUILD_TYPE@")
set(CMAKE_EXECUTABLE_SUFFIX "@CMAKE_EXECUTABLE_SUFFIX@")
set(LLVM_CONFIG_EXECUTABLE "@LLVM_CONFIG_EXECUTABLE@")
set(LLVM_VERSION "@LLVM_VERSION@")
set(LLVM_ROOT "@LLVM_ROOT@")
set(LLVM_INCLUDE_DIR "@LLVM_INCLUDE_DIR@")
set(LLVM_TOOLS_BINARY_DIR "@LLVM_TOOLS_BINARY_DIR@")
set(LLVM_LIBRARY_DIR "@LLVM_LIBRARY_DIR@")
set(CLANG_EXECUTABLE "@CLANG_EXECUTABLE@")
set(CLANGXX_EXECUTABLE "@CLANGXX_EXECUTABLE@")
set(CLANG_VERSION "@CLANG_VERSION@")
if (@APRON_FOUND@)
  set(HAS_APRON "True")
else()
  set(HAS_APRON "False")
endif()

if (@APPEND_GIT_VERSION@)
  # number of commits in the current branch
  execute_process(
    COMMAND git rev-list --count HEAD
    WORKING_DIRECTORY "${SRC_DIR}"
    OUTPUT_VARIABLE GIT_COUNT_COMMITS
    ERROR_QUIET
    OUTPUT_STRIP_TRAILING_WHITESPACE
  )

  # hash of the latest commit
  execute_process(
    COMMAND git rev-parse --short HEAD
    WORKING_DIRECTORY "${SRC_DIR}"
    OUTPUT_VARIABLE GIT_HEAD_HASH
    ERROR_QUIET
    OUTPUT_STRIP_TRAILING_WHITESPACE
  )

  # git status
  execute_process(
    COMMAND git status --porcelain
    WORKING_DIRECTORY "${SRC_DIR}"
    OUTPUT_VARIABLE GIT_HEAD_STATUS
    ERROR_QUIET
    OUTPUT_STRIP_TRAILING_WHITESPACE
  )

  if (GIT_HEAD_STATUS STREQUAL "")
    set(GIT_HEAD_DIRTY "False")
  else()
    set(GIT_HEAD_DIRTY "True")
  endif()
else()
  set(GIT_COUNT_COMMITS "")
  set(GIT_HEAD_HASH "")
  set(GIT_HEAD_DIRTY "False")
endif()

# remove to force update
file(REMOVE "${BIN_DIR}/python/ikos/settings/__init__.py")

# run configure
configure_file("${SRC_DIR}/python/ikos/settings.py.in"
               "${BIN_DIR}/python/ikos/settings/__init__.py" @ONLY)
