set(CMAKE_INCLUDE_CURRENT_DIR OFF)

include(configure.cmake)

set(SOURCES
    cpufeatures.c
    descriptorlimit.c
    memorybarrierprocesswide.c
    mutex.c
    guid.c
    random.c
    debugger.c
    strings.c
    time.c
    unicodedata.c
    utf8.c
    xoshiro128pp.c
    log.c
)

# ospagesize is provided inline in the header on Windows and WASM; the .c file
# only contains the POSIX implementation. Including it on those platforms would
# produce a redefinition error (mono builds for wasi/browser set HOST_WASM but
# not CLR_CMAKE_TARGET_ARCH_WASM, so check both). In cross-component builds
# (e.g. host=x64, target=wasm) the code runs on the host, so we still need the
# POSIX implementation; only skip when the HOST is actually wasm.
if(NOT WIN32 AND NOT HOST_WASM AND NOT (CLR_CMAKE_TARGET_ARCH_WASM AND NOT CLR_CROSS_COMPONENTS_BUILD))
    list(APPEND SOURCES ospagesize.c)
endif()

if(CLR_CMAKE_HOST_UNIX)
    list(APPEND SOURCES cpucount.c)
endif()

# Provide an object library for scenarios where we ship static libraries
include_directories(${CLR_SRC_NATIVE_DIR} ${CMAKE_CURRENT_BINARY_DIR})

add_library(minipal_objects OBJECT ${SOURCES})

if (WIN32)
    target_link_libraries(minipal_objects PUBLIC bcrypt)
endif()

# Add a copy of the minipal object library with interprocedural optimization disabled
# for NativeAOT scenarios, where we ship static libraries that need to be able to be run with
# a variety of toolchain versions, not only the exact one we built with.
add_library(aotminipal STATIC ${SOURCES})
set_target_properties(aotminipal PROPERTIES INTERPROCEDURAL_OPTIMIZATION OFF)

if (WIN32)
    target_link_libraries(aotminipal PUBLIC bcrypt)
endif()

# Provide a static library for our shared library and executable scenarios
# for easier usability.
add_library(minipal STATIC)
target_link_libraries(minipal PRIVATE minipal_objects)

if(CLR_CMAKE_HOST_ANDROID)
  target_link_libraries(minipal PRIVATE log)
endif(CLR_CMAKE_HOST_ANDROID)

set(SANITIZER_EXPORTS_FILE ${CMAKE_CURRENT_SOURCE_DIR}/sanitizer_exports.def)

add_library(minipal_sanitizer_support OBJECT
    sansupport.c)

set_source_files_properties(sansupport.c PROPERTIES OBJECT_DEPENDS ${SANITIZER_EXPORTS_FILE})

if (MSVC)
    target_link_options(minipal_sanitizer_support INTERFACE /DEF:${SANITIZER_EXPORTS_FILE})
endif()
# Exclude this target from the default build as we may not have sanitizer headers available
# in a non-sanitized build.
set_target_properties(minipal_sanitizer_support PROPERTIES EXCLUDE_FROM_ALL ON)

if(CLR_CMAKE_TARGET_ARCH_WASM)
    install(TARGETS minipal DESTINATION sharedFramework COMPONENT runtime)
endif(CLR_CMAKE_TARGET_ARCH_WASM)
