add_library(dofs_core STATIC)

# Propagate include dir via dofs_config to anyone linking dofs_core
target_link_libraries(dofs_core PUBLIC dofs_config)

target_include_directories(dofs_core
  PUBLIC
    $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/src>
    $<INSTALL_INTERFACE:include>
)

if(TARGET yaml-cpp)
    target_link_libraries(dofs_core PUBLIC yaml-cpp)
endif()

if(DOFS_GLOB_SOURCES)
    file(GLOB CORE_CC CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/*.cc")
    target_sources(dofs_core PRIVATE ${CORE_CC})
else()
    # Prefer explicit lists for reproducibility
    target_sources(dofs_core
    PUBLIC
    	time.h
	timer.h
	types.h
	error.h
	rng.h
	logger.h
	simulator.h
	node.h
	host.h
    PRIVATE
        error.cc
        logger.cc
        simulator.cc
	node.cc
	host.cc
    )
endif()

add_library(dofs::core ALIAS dofs_core)

# --- Tooling-only unity TU for headers (core) ---
# Generate per-header stubs with the SAME basename (time.h -> time.cpp)
file(GLOB CORE_HEADERS CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/*.h")
set(CORE_STUBS)
foreach(h ${CORE_HEADERS})
    get_filename_component(_base "${h}" NAME_WE)
    set(_stub "${CMAKE_CURRENT_BINARY_DIR}/${_base}.cpp")
    file(WRITE "${_stub}" "// tooling stub for ${_base}.h\n#include \"core/${_base}.h\"\n")
    list(APPEND CORE_STUBS "${_stub}")
endforeach()

add_library(dofs_core_headers_tooling STATIC ${CORE_STUBS})
target_link_libraries(dofs_core_headers_tooling PRIVATE dofs_core)
# This target exists purely for compile_commands; no need to link it elsewhere.
