cmake_minimum_required(VERSION 3.23)

project(vec VERSION 2.0.0 DESCRIPTION "a tiny C99 SIMD vector library")

add_library(vec SHARED src/vec.c)

target_sources(vec PUBLIC
	$<INSTALL_INTERFACE:vec/vec.h>
	$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include/vec/vec.h>
	$<INSTALL_INTERFACE:vec/impl/integer.h>
	$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include/vec/impl/integer.h>
)

include(CheckCCompilerFlag)

if(MSVC)
	# TODO ?
else()
	check_c_compiler_flag("-maltivec" COMPILER_HAS_ALTIVEC)
	if(COMPILER_HAS_ALTIVEC)
		target_compile_options(vec PRIVATE "-maltivec")
	endif()
	check_c_compiler_flag("-mmmx" COMPILER_HAS_MMX)
	if(COMPILER_HAS_MMX)
		target_compile_options(vec PRIVATE "-mmmx")
	endif()
	check_c_compiler_flag("-msse2" COMPILER_HAS_SSE2)
	if(COMPILER_HAS_SSE2)
		target_compile_options(vec PRIVATE "-msse2")
	endif()
	check_c_compiler_flag("-msse4.1" COMPILER_HAS_SSE41)
	if(COMPILER_HAS_SSE41)
		target_compile_options(vec PRIVATE "-msse4.1")
	endif()
	check_c_compiler_flag("-mavx2" COMPILER_HAS_AVX2)
	if(COMPILER_HAS_AVX2)
		target_compile_options(vec PRIVATE "-mavx2")
	endif()
	check_c_compiler_flag("-mavx512f" COMPILER_HAS_AVX512F)
	if(COMPILER_HAS_AVX512F)
		target_compile_options(vec PRIVATE "-mavx512f")
	endif()
endif()

#########################################################################
# integer types

include(CheckTypeSize)

check_type_size("int16_t"   INT16_T_SIZE   LANGUAGE C)
check_type_size("uint16_t"  UINT16_T_SIZE  LANGUAGE C)
check_type_size("u_int16_t" U_INT16_T_SIZE LANGUAGE C)
check_type_size("int32_t"   INT32_T_SIZE   LANGUAGE C)
check_type_size("uint32_t"  UINT32_T_SIZE  LANGUAGE C)
check_type_size("u_int32_t" U_INT32_T_SIZE LANGUAGE C)
check_type_size("int64_t"   INT64_T_SIZE   LANGUAGE C)
check_type_size("uint64_t"  UINT64_T_SIZE  LANGUAGE C)
check_type_size("u_int64_t" U_INT64_T_SIZE LANGUAGE C)
check_type_size("short"     SHORT_SIZE     LANGUAGE C)
check_type_size("int"       INT_SIZE       LANGUAGE C)
check_type_size("long"      LONG_SIZE      LANGUAGE C)
check_type_size("long long" LONG_LONG_SIZE LANGUAGE C)
check_type_size("uintptr_t" UINTPTR_T_SIZE LANGUAGE C)

if(INT16_T_SIZE EQUAL 2)
	set(SIZE16 "int16_t")
elseif(SHORT_SIZE EQUAL 2)
	set(SIZE16 "short")
elseif(INT_SIZE EQUAL 2)
	set(SIZE16 "int")
endif()

if(UINT16_T_SIZE EQUAL 2)
	set(USIZE16 "uint16_t")
elseif(U_INT16_T_SIZE EQUAL 2)
	set(USIZE16 "u_int16_t")
elseif(SHORT_SIZE EQUAL 2)
	set(USIZE16 "unsigned short")
elseif(INT_SIZE EQUAL 2)
	set(USIZE16 "unsigned int")
endif()

if(INT32_T_SIZE EQUAL 4)
	set(SIZE32 "int32_t")
elseif(SHORT_SIZE EQUAL 4)
	set(SIZE32 "short")
elseif(INT_SIZE EQUAL 4)
	set(SIZE32 "int")
elseif(LONG_SIZE EQUAL 4)
	set(SIZE32 "long")
endif()

if(UINT32_T_SIZE EQUAL 4)
	set(USIZE32 "uint32_t")
elseif(U_INT32_T_SIZE EQUAL 4)
	set(USIZE32 "u_int32_t")
elseif(SHORT_SIZE EQUAL 4)
	set(USIZE32 "unsigned short")
elseif(INT_SIZE EQUAL 4)
	set(USIZE32 "unsigned int")
elseif(LONG_SIZE EQUAL 4)
	set(USIZE32 "unsigned long")
endif()

if(INT64_T_SIZE EQUAL 8)
	set(SIZE64 "int64_t")
elseif(SHORT_SIZE EQUAL 8)
	set(SIZE64 "short")
elseif(INT_SIZE EQUAL 8)
	set(SIZE64 "int")
elseif(LONG_SIZE EQUAL 8)
	set(SIZE64 "long")
elseif(LONG_LONG_SIZE EQUAL 8)
	set(SIZE64 "long long")
endif()

if(UINT64_T_SIZE EQUAL 8)
	set(USIZE64 "uint64_t")
elseif(U_INT64_T_SIZE EQUAL 8)
	set(USIZE64 "u_int64_t")
elseif(SHORT_SIZE EQUAL 8)
	set(USIZE64 "unsigned short")
elseif(INT_SIZE EQUAL 8)
	set(USIZE64 "unsigned int")
elseif(LONG_SIZE EQUAL 8)
	set(USIZE64 "unsigned long")
elseif(LONG_LONG_SIZE EQUAL 8)
	set(USIZE64 "unsigned long long")
endif()

if(CMAKE_SIZEOF_VOID_P EQUAL UINTPTR_T_SIZE)
	set(USIZEPTR "uintptr_t")
elseif(CMAKE_SIZEOF_VOID_P EQUAL 1)
	set(USIZEPTR "unsigned char")
elseif(CMAKE_SIZEOF_VOID_P EQUAL 2)
	set(USIZEPTR "${USIZE16}")
elseif(CMAKE_SIZEOF_VOID_P EQUAL 4)
	set(USIZEPTR "${USIZE32}")
elseif(CMAKE_SIZEOF_VOID_P EQUAL 8)
	set(USIZEPTR "${USIZE64}")
endif()

configure_file(include/vec/impl/integer.h.in include/vec/impl/integer.h @ONLY)

target_compile_definitions(vec PRIVATE "VEC_HAVE_IMPL_INTEGER_H")

#########################################################################

target_compile_features(vec PRIVATE $<IF:$<COMPILE_FEATURES:c_std_11>,c_std_11,c_std_99>)
target_include_directories(vec PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/include;${CMAKE_CURRENT_BINARY_DIR}/include/vec")

# Installing

include(GNUInstallDirs)

install(TARGETS vec LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})

install(FILES "${CMAKE_CURRENT_SOURCE_DIR}/include/vec/vec.h" DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/vec")
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/include/vec/impl/integer.h" DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/vec/impl")

# pkg-config
configure_file(vec.pc.in vec.pc @ONLY)
install(FILES ${CMAKE_BINARY_DIR}/vec.pc DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/pkgconfig)
