# CMakeLists.txt for libgnucash/core-utils

add_subdirectory(test)

# Command to generate the swig-core-utils-guile.c wrapper file
gnc_add_swig_guile_command (swig-core-utils-guile-c
    SWIG_CORE_UTILS_GUILE_C swig-core-utils-guile.c
    ${CMAKE_CURRENT_SOURCE_DIR}/core-utils.i
)

# Command to generate the swig-core-utils-python.c wrapper file
gnc_add_swig_python_command (swig-core-utils-python
    SWIG_CORE_UTILS_PYTHON_C SWIG_CORE_UTILS_PYTHON_PY
    swig-core-utils-python.c sw_core_utils.py
    ${CMAKE_CURRENT_SOURCE_DIR}/core-utils.i
)

set (core_utils_SOURCES
  binreloc.c
  gnc-prefs.c
  gnc-environment.c
  gnc-filepath-utils.cpp
  gnc-gkeyfile-utils.c
  gnc-glib-utils.c
  gnc-guile-utils.c
  gnc-jalali.c
  gnc-locale-utils.c
  gnc-locale-utils.cpp
  gnc-path.c
  gnc-version.c
)

# Add dependency on config.h
set_source_files_properties (${core_utils_SOURCES} PROPERTIES OBJECT_DEPENDS ${CONFIG_H})

set(prefix ${CMAKE_INSTALL_PREFIX})
set(datadir ${CMAKE_INSTALL_FULL_DATADIR})
set(bindir ${CMAKE_INSTALL_FULL_BINDIR})
set(libdir ${CMAKE_INSTALL_FULL_LIBDIR})
set(sysconfdir ${CMAKE_INSTALL_FULL_SYSCONFDIR})
set(localedir "${CMAKE_INSTALL_FULL_DATAROOTDIR}/locale")
configure_file(gncla-dir.h.in gncla-dir.h)



# ---
if (BUILDING_FROM_VCS)
  set(VCS_INFO_BASE_DIR ${CMAKE_BINARY_DIR})
else()
  set(VCS_INFO_BASE_DIR ${CMAKE_SOURCE_DIR})
endif()
set(VCS_INFO_FILE ${VCS_INFO_BASE_DIR}/libgnucash/core-utils/gnc-vcs-info.h CACHE STRING "path to gnc-vcs-dir")

### Create gnc-vcs-info.h
# This can only be done when building from a vcs (git/svn/bzr/svk) working directory.
# This file is shipped in the distribution tarball, so no need to generate it in that case anyway.
# The meta-cmake gymnastics here come from https://cmake.org/pipermail/cmake/2010-July/038015.html
if (BUILDING_FROM_VCS)
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/gnc-vcs-info.h.in
"/* Autogenerated. Do not change. */
#define GNC_VCS_REV \"@GNC_VCS_REV@\"
#define GNC_VCS_REV_DATE \"@GNC_VCS_REV_DATE@\"
#define GNC_VCS_REV_YEAR \"@GNC_VCS_REV_YEAR@\"
#define GNC_VCS_REV_Y_M \"@GNC_VCS_REV_Y_M@\"
")

add_custom_target(gnc-vcs-info ALL
    BYPRODUCTS ${CMAKE_CURRENT_BINARY_DIR}/gnc-vcs-info.h
    COMMAND ${CMAKE_COMMAND} -D SRC=${CMAKE_CURRENT_BINARY_DIR}/gnc-vcs-info.h.in
                             -D DST=${CMAKE_CURRENT_BINARY_DIR}/gnc-vcs-info.h
                             -D SRC_DIR=${CMAKE_SOURCE_DIR}
                             -D SHELL="${SHELL}"
                             -P ${CMAKE_SOURCE_DIR}/cmake/git2version-info.cmake
)
else(BUILDING_FROM_VCS)
  add_custom_target(gnc-vcs-info DEPENDS ${VCS_INFO_FILE})
endif(BUILDING_FROM_VCS)
dist_add_generated (${BUILDING_FROM_VCS} gnc-vcs-info.h)
### Compile library

set(core_utils_noinst_HEADERS
  binreloc.h
  gnc-prefs.h
  gnc-prefs-p.h
  gnc-environment.h
  gnc-filepath-utils.h
  gnc-gkeyfile-utils.h
  gnc-glib-utils.h
  gnc-guile-utils.h
  gnc-jalali.h
  gnc-locale-utils.h
  gnc-locale-utils.hpp
  gnc-path.h
  gnc-version.h
)

set(core_utils_ALL_SOURCES ${core_utils_SOURCES} ${core_utils_noinst_HEADERS})
set(core_utils_ALL_LIBRARIES ${Boost_LIBRARIES} ${GUILE_LDFLAGS} ${GLIB2_LDFLAGS} ${GOBJECT_LDFLAGS} ${GTK_MAC_LDFLAGS})
set(core_utils_ALL_INCLUDES
  ${CMAKE_SOURCE_DIR}/common
  ${CMAKE_BINARY_DIR}/common
  ${CMAKE_CURRENT_BINARY_DIR}
  ${CMAKE_CURRENT_SOURCE_DIR}
  ${GLIB2_INCLUDE_DIRS}
  ${GUILE_INCLUDE_DIRS}
  ${GTK_MAC_INCLUDE_DIRS})

set_local_dist(core_utils_DIST_local ${core_utils_ALL_SOURCES} CMakeLists.txt core-utils.i core-utils.scm gncla-dir.h.in)
set(core_utils_DIST ${core_utils_DIST_local} ${test_core_utils_DIST} PARENT_SCOPE)

if (MAC_INTEGRATION)
  list(APPEND core_utils_ALL_LIBRARIES ${OSX_EXTRA_LIBRARIES})
endif()

add_library(gnc-core-utils ${core_utils_ALL_SOURCES} ${SWIG_CORE_UTILS_GUILE_C})
add_dependencies(gnc-core-utils gnc-vcs-info swig-runtime-h)

target_link_libraries(gnc-core-utils ${core_utils_ALL_LIBRARIES})

target_compile_definitions(gnc-core-utils
    PRIVATE -DG_LOG_DOMAIN=\"gnc.core-utils\" ${GTK_MAC_CFLAGS_OTHER})

target_include_directories(gnc-core-utils PUBLIC ${core_utils_ALL_INCLUDES})

if (MAC_INTEGRATION)
  target_compile_options(gnc-core-utils PRIVATE ${OSX_EXTRA_COMPILE_FLAGS})
endif(MAC_INTEGRATION)
if (BUILDING_FROM_VCS)
  target_compile_definitions(gnc-core-utils PRIVATE -DGNC_VCS=\"git\")
endif (BUILDING_FROM_VCS)

install(TARGETS gnc-core-utils
  LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
  ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
  RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)

if (WITH_PYTHON)
  add_library (sw_core_utils MODULE ${SWIG_CORE_UTILS_PYTHON_C})

  target_link_libraries(sw_core_utils gnc-core-utils ${core_utils_ALL_LIBRARIES} ${PYTHON_LIBRARIES})
  set_target_properties(sw_core_utils PROPERTIES PREFIX "_")

  if (HAVE_STRINGOP_TRUNCATION)
    target_compile_options(sw_core_utils PRIVATE -Wno-error=stringop-truncation)
  endif()

  target_include_directories (sw_core_utils
    PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}
    PRIVATE ${core_utils_ALL_INCLUDES} ${PYTHON_INCLUDE_DIRS}
  )
  target_compile_definitions (sw_core_utils PRIVATE -DG_LOG_DOMAIN=\"gnc.core-utils\")

  if (CORELE)
    set_target_properties (sw_core_utils PROPERTIES INSTALL_NAME_DIR "${CMAKE_INSTALL_FULL_LIBDIR}/gnucash")
  endif()

  install(TARGETS sw_core_utils
    LIBRARY DESTINATION ${PYTHON_SYSCONFIG_OUTPUT}/gnucash
    ARCHIVE DESTINATION ${PYTHON_SYSCONFIG_OUTPUT}/gnucash
  )

  add_custom_target(sw-core-utils-py ALL
    COMMAND ${CMAKE_COMMAND} -E copy ${SWIG_CORE_UTILS_PYTHON_PY} ${PYTHON_SYSCONFIG_BUILD}/gnucash
    DEPENDS ${SWIG_CORE_UTILS_PYTHON_C})

  add_custom_target(sw-core-utils-build ALL
    COMMAND ${CMAKE_COMMAND} -E copy ${LIBDIR_BUILD}/gnucash/_sw_core_utils${CMAKE_SHARED_MODULE_SUFFIX} ${PYTHON_SYSCONFIG_BUILD}/gnucash
    DEPENDS sw_core_utils)

endif()

# No headers to install

# Scheme

set (core_utils_SCHEME core-utils.scm)

set(GUILE_OUTPUT_DIR   gnucash)
set(GUILE_DEPENDS      gnc-core-utils)

gnc_add_scheme_targets(scm-core-utils
    "${core_utils_SCHEME}"
    ${GUILE_OUTPUT_DIR}
    "${GUILE_DEPENDS}"
    FALSE
)

