# 3.9 required by: zlib targets, OpenMP target
cmake_minimum_required(VERSION 3.9)

project(
  kissplice
  VERSION 2.6.7 # Definition which is propagated through PROJECT_VERSION
  LANGUAGES CXX
)

set(CMAKE_CXX_STANDARD 14) # Default to C++14 for C++ targets
set(CMAKE_CXX_STANDARD_REQUIRED ON) # Fail if not supported (very old compiler at this point)

# Build type, should be set by user. Useful predefined values (see `cmake -LH`): Debug, Release, RelWithDebInfo.
# Default to non-persistent RelWithDebInfo (-g -O2) if no build type is specified.
if(NOT CMAKE_BUILD_TYPE)
  # Not cached : this keeps the help string (`cmake -LH`) and user setting intact.
  set(CMAKE_BUILD_TYPE RelWithDebInfo)
endif()

# Cmake already adds -g in Debug and -O3 in Release, so no need to set them manually.
# Enables common warnings for ALL targets.
add_compile_options(-Wall)

# Date for manpage and pdf doc
string(TIMESTAMP CONFIGURE_DATE "%Y-%m-%d" UTC)

###############################################################################
# structure and paths
# kissplice is composed of:
# - internal binaries which should go to <prefix>/libexec/kissplice
# - a entry point "binary" which is a python script which should go to <prefix>/bin
# The entry point script locates internal binaries using relative paths from itself.
# For tests to work, this architecture is replicated in the temporary build directory wih <prefix>=<build_dir>.
include(GNUInstallDirs) # GNU standard installation directories definitions (bin, libexec, and definitions for doc/man)
# Path to internal binaries relative to prefix. This is used in install() for internal binaries, to let the cmake prefix handling work.
set(RELATIVE_INTERNAL_BINDIR "${CMAKE_INSTALL_LIBEXECDIR}/kissplice") 
# Absolute path to build location of internal binaries. This is used to define RUNTIME_OUTPUT_DIRECTORY of internal binaries (build dir).
set(BUILD_INTERNAL_BINDIR "${PROJECT_BINARY_DIR}/${RELATIVE_INTERNAL_BINDIR}")
# Absolute path to build location of main entry point (kissplice script only for now)
set(BUILD_BINDIR "${PROJECT_BINARY_DIR}/${CMAKE_INSTALL_BINDIR}")
# Relative path from script to internal binaries directory, written to kissplice script
file(RELATIVE_PATH KISSPLICE_BINDIR_TO_INTERNAL_BINDIR "${BUILD_BINDIR}" "${BUILD_INTERNAL_BINDIR}")

###############################################################################
# bcalm dependency : bcalm is a binary program needed for tests and after install.
# It is packaged on debian, so default to using the system wide version (best practice for packaging kissplice).
# The USE_BUNDLED_BCALM option can be used to locally build and ship an install of bcalm, placed in the internal binaries directory (build and install).
# The BCALM_PACKAGING_MODE string variable propagates this choice to the kissplice script (with known values unlike USE_BUNDLED_BCALM).
option(
  USE_BUNDLED_BCALM
  "by default kissplice requires an already installed bcalm program ; enable this option to bundle a local bcalm with kissplice."
  OFF
)
if(NOT USE_BUNDLED_BCALM)
  # Check system bcalm
  find_program(SYSTEM_BCALM_PATH NAMES bcalm)
  mark_as_advanced(SYSTEM_BCALM_PATH)
  if(SYSTEM_BCALM_PATH)
    message(STATUS "Found bcalm tool: ${SYSTEM_BCALM_PATH}")
  else()
    message(WARNING "bcalm not found in PATH. install it with system packages, or use bundled bcalm version with -DUSE_BUNDLED_BCALM=TRUE")
  endif()
  set(BCALM_PACKAGING_MODE "system")
else()
  message(STATUS "using bundled version of bcalm (will be compiled at make time)")
  include(ExternalProject)
  ExternalProject_Add(
    bundled_bcalm
    PREFIX bundled_bcalm
    # Using commit with Mac fix from a fork awaiting merge. FIXME replace by upstream (GATB user) when merged.
    GIT_REPOSITORY "https://github.com/fgindraud/bcalm.git"
    GIT_TAG cf371b697c537ca492da877f2531ebd494a4c79e
    UPDATE_COMMAND "" # Prevent rebuilding bcalm everytime
    # Manually extract the bcalm binary to our internal binary build dir, instead of broken install target.
    # This is run inside bcalm build dir.
    INSTALL_COMMAND "${CMAKE_COMMAND}" -E copy bcalm "${BUILD_INTERNAL_BINDIR}"
  )
  install(PROGRAMS "${BUILD_INTERNAL_BINDIR}/bcalm" DESTINATION "${RELATIVE_INTERNAL_BINDIR}")
  set(BCALM_PACKAGING_MODE "bundled")
endif()

###############################################################################
# Complete the main script template to include the relative directory and bcalm location.
configure_file("${PROJECT_SOURCE_DIR}/kissplice.in.py" "${BUILD_BINDIR}/kissplice" @ONLY)
install(PROGRAMS "${BUILD_BINDIR}/kissplice" DESTINATION "${CMAKE_INSTALL_BINDIR}")

###############################################################################
# subdirs
add_subdirectory(thirdparty) # kissreads
add_subdirectory(modules) # various ks_* internal tools
add_subdirectory(man) # manpage
add_subdirectory(doc) # user guide

enable_testing()
add_subdirectory(tests)

