# Top-level CMakeLists.txt file to implement
# a complete CMake-based build system for uncrustify

project(uncrustify CXX)

message(STATUS "CMake version = ${CMAKE_VERSION}")
message(STATUS "CMAKE_SYSTEM = ${CMAKE_SYSTEM}")
message(STATUS "CMAKE_GENERATOR = ${CMAKE_GENERATOR}")
message(STATUS "CMAKE_INSTALL_PREFIX = ${CMAKE_INSTALL_PREFIX}")

cmake_minimum_required(VERSION 3.0.2 FATAL_ERROR)

# Set paths for installation
set(
  CMAKE_INSTALL_EXEC_PREFIX
  ${CMAKE_INSTALL_PREFIX}
  CACHE PATH "install location for architecture-dependent files"
  )

set(
  CMAKE_INSTALL_BINDIR
  ${CMAKE_INSTALL_EXEC_PREFIX}/bin
  CACHE PATH "install location for user executables"
  )

set(
  CMAKE_INSTALL_DATADIR
  ${CMAKE_INSTALL_PREFIX}/share
  CACHE PATH "install location for read-only architecture-independent data"
  )

set(
  CMAKE_INSTALL_MANDIR
  ${CMAKE_INSTALL_DATADIR}/man
  CACHE PATH "install location for man documentation"
  )

# Build and install the uncrustify executable:
include_directories(
  ${CMAKE_CURRENT_SOURCE_DIR}/src
  ${CMAKE_CURRENT_BINARY_DIR}/src
  )

# Generate two of the headers with a custom command/target

find_program(BASH_EXECUTABLE bash)
if(NOT BASH_EXECUTABLE)
  message(FATAL_ERROR "bash missing so cannot build uncrustify")
endif(NOT BASH_EXECUTABLE)

add_custom_command(
  OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/src/token_names.h
  COMMAND ${CMAKE_COMMAND} -E echo "Rebuilding token_names.h"
  COMMAND ${BASH_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/make_token_names.sh
  # The above command creates ${CMAKE_CURRENT_SOURCE_DIR}/src/token_names.h,
  # but you want the source tree to be unmolested so must move that result to
  # the build tree.
  COMMAND ${CMAKE_COMMAND} -E copy_if_different
  ${CMAKE_CURRENT_SOURCE_DIR}/src/token_names.h
  ${CMAKE_CURRENT_BINARY_DIR}/src/token_names.h
  COMMAND ${CMAKE_COMMAND} -E remove
  ${CMAKE_CURRENT_SOURCE_DIR}/src/token_names.h
  # This command must be run from the src subdirectory in the source tree.
  WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/src
  DEPENDS
  ${CMAKE_CURRENT_SOURCE_DIR}/make_token_names.sh
  ${CMAKE_CURRENT_SOURCE_DIR}/src/token_enum.h
  )

add_custom_target(
  build_headers
  DEPENDS
  ${CMAKE_CURRENT_BINARY_DIR}/src/token_names.h
  )

# Generate configured header needed for build at cmake time.
find_file(HAVE_INTTYPES_H inttypes.h)
find_file(HAVE_STRINGS_H strings.h)
find_file(HAVE_SYS_STAT_H sys/stat.h)
find_file(HAVE_UNISTD_H unistd.h)
find_file(HAVE_UTIME_H utime.h)

configure_file(
  ${CMAKE_CURRENT_SOURCE_DIR}/config.h.cmake
  ${CMAKE_CURRENT_BINARY_DIR}/src/config.h
  @ONLY
  )

set(uncrustify_SRC
  src/ChunkStack.cpp
  src/align.cpp
  src/align_stack.cpp
  src/args.cpp
  src/backup.cpp
  src/brace_cleanup.cpp
  src/braces.cpp
  src/chunk_list.cpp
  src/combine.cpp
  src/compat_posix.cpp
  src/compat_win32.cpp
  src/defines.cpp
  src/detect.cpp
  src/indent.cpp
  src/keywords.cpp
  src/lang_pawn.cpp
  src/logger.cpp
  src/logmask.cpp
  src/md5.cpp
  src/newlines.cpp
  src/options.cpp
  src/output.cpp
  src/parens.cpp
  src/parse_frame.cpp
  src/punctuators.cpp
  src/semicolons.cpp
  src/sorting.cpp
  src/space.cpp
  src/tokenize.cpp
  src/tokenize_cleanup.cpp
  src/unc_text.cpp
  src/uncrustify.cpp
  src/unicode.cpp
  src/universalindentgui.cpp
  src/width.cpp
  )

add_executable(uncrustify ${uncrustify_SRC})
# Make sure necessary header gets built before this
# target
add_dependencies(uncrustify build_headers)

install(TARGETS uncrustify DESTINATION ${CMAKE_INSTALL_BINDIR})

# Configure and install man page.
set(PACKAGE_VERSION 0.60)
configure_file(
  ${CMAKE_CURRENT_SOURCE_DIR}/man/uncrustify.1.in
  ${CMAKE_CURRENT_BINARY_DIR}/man/uncrustify.1
  @ONLY
  )

install(FILES
  ${CMAKE_CURRENT_BINARY_DIR}/man/uncrustify.1
  DESTINATION ${CMAKE_INSTALL_MANDIR}/man1
  )

# Install *.cfg files:

set(uncrustify_configs
  ben.cfg
  defaults.cfg
  kr-indent.cfg
  linux.cfg
  xsupplicant.cfg
  d.cfg
  gnu-indent.cfg
  linux-indent.cfg
  mono.cfg
  )

set(uncrustify_config_list)
foreach(uncrustify_config ${uncrustify_configs})
    list(APPEND uncrustify_config_list ${CMAKE_CURRENT_SOURCE_DIR}/etc/${uncrustify_config})
endforeach(uncrustify_config ${uncrustify_configs})
install(FILES
  ${uncrustify_config_list}
  DESTINATION ${CMAKE_INSTALL_DATADIR}/doc/uncrustify
  )
