cmake_minimum_required(VERSION 3.30)

set(CMAKE_C_COMPILER clang CACHE STRING "")
set(CMAKE_CXX_COMPILER clang++ CACHE STRING "")

find_program(LLVM_AR_PATH "llvm-ar")
if(LLVM_AR_PATH)
    set(CMAKE_AR "${LLVM_AR_PATH}" CACHE FILEPATH "")
    set(CMAKE_C_COMPILER_AR "${LLVM_AR_PATH}" CACHE FILEPATH "")
    set(CMAKE_CXX_COMPILER_AR "${LLVM_AR_PATH}" CACHE FILEPATH "")
endif()

find_program(LLVM_RANLIB_PATH "llvm-ranlib")
if(LLVM_RANLIB_PATH)
    set(CMAKE_RANLIB "${LLVM_RANLIB_PATH}" CACHE FILEPATH "")
    set(CMAKE_C_COMPILER_RANLIB "${LLVM_RANLIB_PATH}" CACHE FILEPATH "")
    set(CMAKE_CXX_COMPILER_RANLIB "${LLVM_RANLIB_PATH}" CACHE FILEPATH "")
endif()

find_program(LLVM_NM_PATH "llvm-nm")
if(LLVM_NM_PATH)
    set(CMAKE_NM "${LLVM_NM_PATH}" CACHE FILEPATH "")
endif()

find_program(LLVM_RC_PATH "llvm-rc")
if(LLVM_RC_PATH)
    set(CMAKE_RC_COMPILER "${LLVM_RC_PATH}" CACHE FILEPATH "")
endif()

if(WIN32)
    find_program(SCCACHE_PATH "sccache")
    if(SCCACHE_PATH)
        set(CMAKE_C_COMPILER_LAUNCHER "${SCCACHE_PATH}" CACHE FILEPATH "")
        set(CMAKE_CXX_COMPILER_LAUNCHER "${SCCACHE_PATH}" CACHE FILEPATH "")
    endif()
    set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreadedDLL" CACHE STRING "")
    set(CMAKE_EXE_LINKER_FLAGS_INIT "-fuse-ld=lld-link")
    set(CMAKE_SHARED_LINKER_FLAGS_INIT "-fuse-ld=lld-link")
    set(CMAKE_MODULE_LINKER_FLAGS_INIT "-fuse-ld=lld-link")
else()
    find_program(CCACHE_PATH "ccache")
    if(CCACHE_PATH)
        set(CMAKE_C_COMPILER_LAUNCHER "${CCACHE_PATH}" CACHE FILEPATH "")
        set(CMAKE_CXX_COMPILER_LAUNCHER "${CCACHE_PATH}" CACHE FILEPATH "")
    endif()
    set(CMAKE_EXE_LINKER_FLAGS_INIT "-fuse-ld=lld")
    set(CMAKE_SHARED_LINKER_FLAGS_INIT "-fuse-ld=lld")
    set(CMAKE_MODULE_LINKER_FLAGS_INIT "-fuse-ld=lld")
endif()
