# SPDX-FileCopyrightText: 2023 Copyright contributors to the cppTango project
#
# SPDX-License-Identifier: LGPL-3.0-or-later

set(versions_cpp ${CMAKE_CURRENT_BINARY_DIR}/versions.cpp)
configure_file(versions.cpp.in ${versions_cpp})
set(SOURCES
    corba_idl_class_helper.cpp
    net.cpp
    utils.cpp
    parse.cpp
    assert.cpp
    $<$<BOOL:${TANGO_USE_TELEMETRY}>:
        ${CMAKE_CURRENT_SOURCE_DIR}/telemetry/Configuration.cpp
        ${CMAKE_CURRENT_SOURCE_DIR}/telemetry/Span.cpp
        ${CMAKE_CURRENT_SOURCE_DIR}/telemetry/Scope.cpp
        ${CMAKE_CURRENT_SOURCE_DIR}/telemetry/Interface.cpp
        ${CMAKE_CURRENT_SOURCE_DIR}/telemetry/InterfaceFactory.cpp
        ${CMAKE_CURRENT_SOURCE_DIR}/telemetry/TelemetryAppender.cpp>
    ${versions_cpp}
    zmq_event_marshalling.cpp)

add_library(common_objects OBJECT ${SOURCES})
add_dependencies(common_objects idl_objects)
set_cflags_and_include(common_objects)

if(WIN32)
    target_compile_definitions(common_objects PRIVATE "${windows_defs}")
else()
    target_compile_options(common_objects PRIVATE -fPIC)
    if(CMAKE_BUILD_TYPE STREQUAL "Debug")
        # Do not define _TANGO_LIB when compiling Tango debug library on Linux
        # in order to keep the same behaviour as in the past:
        # A Linux Tango Device server using the debug tango lib will display
        # twice "Ready to accept requests" at startup
    else()
        target_compile_definitions(common_objects PRIVATE _TANGO_LIB)
    endif()
endif()

target_precompile_headers(common_objects PRIVATE ${PROJECT_BINARY_DIR}/src/include/tango/tango.h)

# C++ std::from_chars don't support floats on some versions of some compilers.
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/check_floats_from_chars.cpp [===[
#include <charconv>

int main()
{
    float f;
    std::from_chars(nullptr, nullptr, f);
    double d;
    std::from_chars(nullptr, nullptr, d);
    return 0;
}

]===])

try_compile(
    TANGO_HAS_FLOATS_FROM_CHARS
    ${CMAKE_CURRENT_BINARY_DIR}
    ${CMAKE_CURRENT_BINARY_DIR}/check_floats_from_chars.cpp)

if(TANGO_HAS_FLOATS_FROM_CHARS)
    set_source_files_properties(parse.cpp PROPERTIES COMPILE_DEFINITIONS TANGO_HAS_FLOATS_FROM_CHARS)
endif()
