# Licensed to the .NET Foundation under one or more agreements.
# The .NET Foundation licenses this file to you under the MIT license.

project(nativehost)

set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
set(MACOSX_RPATH ON)
if (CLR_CMAKE_TARGET_OSX)
    set(CMAKE_INSTALL_RPATH "@loader_path")
else()
    set(CMAKE_INSTALL_RPATH "\$ORIGIN")
endif()

set(SOURCES
    ./error_writer_redirector.cpp
    ./get_native_search_directories_test.cpp
    ./hostfxr_exports.cpp
    ./hostpolicy_exports.cpp
    ./host_context_test.cpp
    ./resolve_component_dependencies_test.cpp
    ./nativehost.cpp
)

set(HEADERS
    ./error_writer_redirector.h
    ./get_native_search_directories_test.h
    ./hostfxr_exports.h
    ./hostpolicy_exports.h
    ./host_context_test.h
    ./resolve_component_dependencies_test.h
)

if(CLR_CMAKE_TARGET_WIN32)
    list(APPEND SOURCES
        ./comhost_test.cpp)

    list(APPEND HEADERS
        ./comhost_test.h)
endif()

add_executable(nativehost ${SOURCES})

add_sanitizer_runtime_support(nativehost)

install_with_stripped_symbols(nativehost TARGETS corehost_test)

target_link_libraries(nativehost PRIVATE
    $<$<BOOL:${PTHREAD_LIB}>:${PTHREAD_LIB}>)


target_link_libraries(nativehost PRIVATE nethost hostmisc)

if (CLR_CMAKE_TARGET_WIN32)
    target_link_options(nativehost PRIVATE /DELAYLOAD:$<TARGET_FILE_NAME:nethost>)
    target_link_libraries(nativehost PRIVATE delayimp.lib ole32 oleaut32)
endif()

# Build a second executable that links against the static nethost library (libnethost)
# instead of the shared one. This validates the static library consumption scenario.
add_executable(nativehost_static ${SOURCES})

add_sanitizer_runtime_support(nativehost_static)

install_with_stripped_symbols(nativehost_static TARGETS corehost_test)

target_compile_definitions(nativehost_static PRIVATE NETHOST_USE_AS_STATIC)

target_link_libraries(nativehost_static PRIVATE
    libnethost
    hostmisc_public
    $<$<BOOL:${PTHREAD_LIB}>:${PTHREAD_LIB}>)

if (CLR_CMAKE_TARGET_WIN32)
    target_link_libraries(nativehost_static PRIVATE ole32 oleaut32)
endif()

if (MSVC)
    # Add a linker hook to validate that modules with LTCG are not found.
    # Shipped static libraries (libnethost) must have LTCG disabled to ensure
    # that non-MSVC toolchains can work with them.
    set_target_properties(nativehost_static PROPERTIES
        INTERPROCEDURAL_OPTIMIZATION OFF
        CXX_LINKER_LAUNCHER "${CMAKE_COMMAND};-P;${CMAKE_CURRENT_SOURCE_DIR}/run-linker-verify-no-ltcg.cmake")
endif()

