# The hot-reload runtime is infrastructure the host application links
# against directly. Unlike the plugins it loads at runtime (see
# source/examples/HotReloadDemo), this library is never itself meant
# to be hot-reloaded, so TYPE STATIC is a deliberate choice here,
# independent of the project-wide BUILD_SHARED_LIBS default.
add_lib_module(hotreload_lib TYPE STATIC OUTPUT_NAME "hotreload")

# hot_reload::Context (Context.cpp) uses std::shared_mutex internally for its thread-safety
# guarantee (see Context.hpp's own doc comment), and the MSVC branch of Demangle.cpp uses
# std::mutex to serialize calls into DbgHelp. Both need pthread linked explicitly on Linux with
# glibc older than 2.34 (newer glibc merged libpthread into libc itself, making this a no-op
# there rather than a requirement) -- Threads::Threads is CMake's own portable way to ask for
# whatever that platform actually needs. PUBLIC for the same reason as CMAKE_DL_LIBS below: for a
# STATIC library this needs to reach whatever executable ultimately links hotreload_lib.
find_package(Threads REQUIRED)
target_link_libraries(hotreload_lib PUBLIC Threads::Threads)

if(NOT WIN32)
	# dlopen/dlclose/dlinfo (ModuleHandle.cpp). PUBLIC, not PRIVATE:
	# for a STATIC library this needs to reach whatever executable
	# ultimately links hotreload_lib, not just hotreload_lib's own
	# (nonexistent, for a static lib) link step.
	target_link_libraries(hotreload_lib PUBLIC ${CMAKE_DL_LIBS})
endif()

# Two different questions, easy to conflate: "does this compiler take
# MSVC-style command-line syntax" (the MSVC variable: real cl.exe and
# clang-cl only) versus "does it produce MSVC-ABI output -- MSVC name
# mangling, the Microsoft CRT, DbgHelp as its demangler" (also true of a
# GNU-style clang++ pointed at the MSVC target, which is what a plain
# Windows LLVM install gives you: no /W4-style flags, but still MSVC
# mangling). This condition is the second question, and CMake derives
# CMAKE_CXX_SIMULATE_ID from whether the compiler defines _MSC_VER --
# the very macro Demangle.cpp's own #if keys on, so the two stay in
# agreement by construction rather than by someone remembering to update
# both. (cmake/BuildHelpers.cmake's /W4 and the root CMakeLists.txt's
# /MP /EHsc are the first question, correctly keyed on MSVC alone.)
if(MSVC OR CMAKE_CXX_SIMULATE_ID STREQUAL "MSVC")
	# UnDecorateSymbolName (Demangle.cpp) lives in dbghelp.lib, which --
	# unlike kernel32/user32 -- MSVC does not link by default. Without
	# this, the final link fails with an undefined symbol for it;
	# found via an actual Windows build of this library, not just
	# documentation. PUBLIC for the same reason as CMAKE_DL_LIBS above.
	# Guarded on the MSVC ABI rather than WIN32, since MinGW's build of
	# Demangle.cpp takes the __GNUC__/__cxa_demangle branch instead and
	# never needs dbghelp at all.
	target_link_libraries(hotreload_lib PUBLIC dbghelp)
endif()
