# Based on a project at https://github.com/julian-r/file-windows

cmake_minimum_required(VERSION 3.10)

project(file
    VERSION $ENV{PKG_VERSION}
    LANGUAGES C
    DESCRIPTION "Fine Free File Command"
    HOMEPAGE_URL "https://darwinsys.com/file/"
)

# Package information

# Set the name of this package.
set(PACKAGE "file")
# Set the full name of this package.
set(PACKAGE_NAME "file")
# Set the one symbol short name of this package.
set(PACKAGE_TARNAME "file")
# Set the version of this package.
set(PACKAGE_VERSION "${FILE_VERSION}")
# Set the full name and version of this package.
set(PACKAGE_STRING "${PACKAGE_NAME} ${FILE_VERSION}")
# Set the home page for this package.
set(PACKAGE_URL "https://darwinsys.com/file/")

set(VERSION ${FILE_VERSION})

set(LT_OBJDIR "something")
# Define to 1 on platforms where this makes time_t a 64-bit type.
set(__MINGW_USE_VC2005_COMPAT 1)

include(CheckIncludeFile)
check_include_file(dlfcn.h HAVE_DLFCN_H)
check_include_file(err.h HAVE_ERR_H)
check_include_file(fcntl.h HAVE_FCNTL_H)
#check_include_file(getopt.h HAVE_GETOPT_H)
set(HAVE_GETOPT_H 1)
check_include_file(inttypes.h HAVE_INTTYPES_H)
check_include_file(Lrzip.h LRZIP_H)
if(HAVE_LRZIP_H)
    set(LRZIPLIBSUPPORT ${HAVE_LRZIP_H})
endif()
check_include_file(lzlib.h LZLIB_H)
if(HAVE_LZLIB_H)
    set(LZLIBSUPPORT ${HAVE_LZLIB_H})
endif()
check_include_file(lzma.h HAVE_LZMA_H)
check_include_file(minix/config.h HAVE_MINIX_CONFIG_H)
check_include_file(spawn.h HAVE_SPAWN_H)
check_include_file(stdint.h HAVE_STDINT_H)
check_include_file(stdio.h HAVE_STDIO_H)
check_include_file(stdlib.h HAVE_STDLIB_H)
check_include_file(strings.h HAVE_STRINGS_H)
check_include_file("string.h" HAVE_STRING_H)
check_include_file(sys/bswap.h HAVE_SYS_BSWAP_H)
check_include_file(sys/ioctl.h HAVE_SYS_IOCTL_H)
check_include_file(sys/mman.h HAVE_SYS_MMAN_H)
check_include_file(sys/mkdev.h HAVE_SYS_MKDEV_H)
check_include_file(sys/param.h HAVE_SYS_PARAM_H)
check_include_file(sys/stat.h HAVE_SYS_STAT_H)
check_include_file(sys/sysmacros.h HAVE_SYS_SYSMACROS_H)
check_include_file(sys/time.h HAVE_SYS_TIME_H)
check_include_file(sys/types.h HAVE_SYS_TYPES_H)
check_include_file(sys/utime.h HAVE_SYS_UTIME_H)
check_include_file(sys/wait.h HAVE_SYS_WAIT_H)
check_include_file(unistd.h HAVE_UNISTD_H)
check_include_file(utime.h HAVE_UTIME_H)
check_include_file(vfork.h HAVE_VFORK_H)
check_include_file(wchar.h HAVE_WCHAR_H)
check_include_file(wctype.h HAVE_WCTYPE_H)
check_include_file(xlocale.h HAVE_XLOCALE_H)
check_include_file(zlib.h ZLIB_H)
if(HAVE_ZLIB_H)
    set(ZLIBSUPPORT ${HAVE_ZLIB_H})
endif()
check_include_file(zstd_errors.h HAVE_WCTYPE_H)
check_include_file(zstd.h ZSTD_H)
if(HAVE_ZSTD_H)
    set(ZSTDLIBSUPPORT ${HAVE_ZSTD_H})
endif()
check_include_file(wctype.h HAVE_WCTYPE_H)
check_include_file(wctype.h HAVE_WCTYPE_H)
check_include_file(wctype.h HAVE_WCTYPE_H)

include(CheckFunctionExists)
check_function_exists(dprintf HAVE_DPRINTF)
check_function_exists(fmtcheck HAVE_FMTCHECK)
check_function_exists(fork FORK)
if(HAVE_FORK)
    set(HAVE_WORKING_FORK ${HAVE_FORK})
endif()
check_function_exists(freelocale HAVE_FREELOCALE)
check_function_exists(getopt_long HAVE_GETOPT_LONG)
check_function_exists(getpagesize HAVE_GETPAGESIZE)
check_function_exists(gmtime_r HAVE_GMTIME_R)
check_function_exists(localtime_r HAVE_LOCALTIME_R)
check_function_exists(memmem HAVE_MEMMEM)
check_function_exists(mkostemp HAVE_MKOSTEMP)
check_function_exists(mkstemp HAVE_MKSTEMP)
check_function_exists(mmap HAVE_MMAP)
check_function_exists(newlocale HAVE_NEWLOCALE)
check_function_exists(pipe2 HAVE_PIPE2)
check_function_exists(posix_spawnp HAVE_POSIX_SPAWNP)
check_function_exists(strcasestr HAVE_STRCASESTR)
check_function_exists(strlcat HAVE_STRLCAT)
check_function_exists(strndup HAVE_STRNDUP)
check_function_exists(strtof HAVE_STRTOF)
check_function_exists(uselocale HAVE_USELOCALE)
check_function_exists(utime HAVE_UTIME)
check_function_exists(utimes HAVE_UTIMES)
check_function_exists(vasprintf HAVE_VASPRINTF)
check_function_exists(vfork VFORK)
if(HAVE_VFORK)
    set(HAVE_WORKING_VFORK ${HAVE_VFORK})
elseif(HAVE_FORK)
    # Define as 'fork' if 'vfork' does not work.
    set(VFORK fork)
endif()
check_function_exists(wcwidth HAVE_WCWIDTH)

include(CheckSymbolExists)
check_symbol_exists(fseeko stdio.h HAVE_FSEEKO)
check_symbol_exists(mbrtowc wchar.h HAVE_MBRTOWC)
check_symbol_exists(mbstate_t wchar.h HAVE_MBSTATE_T)
if(HAVE_SYS_MKDEV_H)
    check_symbol_exists(major sys/mkdev.h MAJOR_IN_MKDEV)
endif()
if(HAVE_SYS_SYSMACROS_H)
    check_symbol_exists(major sys/sysmacros.h MAJOR_IN_SYSMACROS)
endif()

include(CheckTypeSize)
check_type_size("uintptr_t" UINTPTR_T)
check_type_size("intptr_t" INTPTR_T)
if(HAVE_INTPTR_T)
  set(SIZEOF_INTPTR_T ${INTPTR_T})
endif()
check_type_size("size_t" SIZE_T)
if(HAVE_SIZE_T)
  set(SIZEOF_SIZE_T ${SIZE_T})
endif()

find_library(HAVE_LIBBZ2 bz2)
find_library(HAVE_LIBGNURX gnurx)
find_library(HAVE_LIBLRZIP lrzip)
find_library(HAVE_LIBLZ lz)
find_library(HAVE_LIBLZMA lzma)
find_library(HAVE_LIBSECCOMP seccomp)
find_library(HAVE_LIBZ z)
find_library(HAVE_LIBZSTD zstd)

include(CheckStructHasMember)
set(HAVE_STRUCT_OPTION 1)
if(HAVE_SYS_STAT_H)
    check_struct_has_member("struct _stat" st_rdev sys/stat.h HAVE_STRUCT_STAT_ST_RDEV)
endif()
check_struct_has_member("struct tm" tm_gmtoff time.h HAVE_STRUCT_TM_TM_GMTOFF)
check_struct_has_member("struct tm" tm_zone time.h TM_ZONE)
if(HAVE_TM_ZONE)
    set(HAVE_STRUCT_TM_TM_ZONE ${TM_ZONE})
endif()
check_struct_has_member("struct tm" tm_isdst time.h HAVE_TM_ISDST)
check_struct_has_member("struct tm" tm_name time.h HAVE_TZNAME)

configure_file(config.h.in ${CMAKE_BINARY_DIR}/file/src/config.h @ONLY)

add_subdirectory(file)
