set(SOURCES
    AddressInfoVector.cpp
    ArgsParser.cpp
    ConfigFile.cpp
    CrashReportData.cpp
    DirIterator.cpp
    Directory.cpp
    DirectoryEntry.cpp
    ElapsedTimer.cpp
    Environment.cpp
    EventLoop.cpp
    EventLoopImplementation.cpp
    EventReceiver.cpp
    File.cpp
    GeolocationAuthorization.cpp
    GeolocationProvider.cpp
    ImmutableBytes.cpp
    MimeData.cpp
    Notifier.cpp
    ReportTime.cpp
    Resource.cpp
    ResourceImplementation.cpp
    ResourceImplementationFile.cpp
    SharedVersion.cpp
    SocketAddress.cpp
    StandardPaths.cpp
    StatsLog.cpp
    SystemServerTakeover.cpp
    ThreadEventQueue.cpp
    Timer.cpp
    TimeZone.cpp
    Version.cpp
)

if (APPLE)
    list(APPEND SOURCES GeolocationProviderCoreLocation.mm)
    set_source_files_properties(GeolocationProviderCoreLocation.mm PROPERTIES COMPILE_FLAGS "-fobjc-arc")
else()
    list(APPEND SOURCES GeolocationProviderUnimplemented.cpp)
endif()

if (WIN32)
    list(APPEND SOURCES
        AnonymousBufferWindows.cpp
        EventLoopImplementationWindows.cpp
        FileWindows.cpp
        LocalServerWindows.cpp
        MappedFileWindows.cpp
        ProcessWindows.cpp
        SocketWindows.cpp
        SocketpairWindows.cpp
        SystemWindows.cpp
        TCPServerWindows.cpp
        UDPServerWindows.cpp
    )
else()
    list(APPEND SOURCES
        AnonymousBuffer.cpp
        EventLoopImplementationUnix.cpp
        FilePosix.cpp
        LocalServer.cpp
        MappedFile.cpp
        Process.cpp
        Socket.cpp
        System.cpp
        TCPServer.cpp
        UDPServer.cpp
    )
endif()

include(CheckIncludeFile)

check_include_file(sys/inotify.h HAVE_INOTIFY)

if (HAVE_INOTIFY)
    list(APPEND SOURCES FileWatcherInotify.cpp)
else()
    list(APPEND SOURCES FileWatcherUnimplemented.cpp)
endif()

# FIXME: Implement ProcessStatistics for Windows and *BSD
if (LINUX)
    list(APPEND SOURCES Platform/ProcessStatisticsLinux.cpp)
elseif (APPLE AND NOT IOS)
    list(APPEND SOURCES Platform/ProcessStatisticsMach.cpp)
else ()
    list(APPEND SOURCES Platform/ProcessStatisticsUnimplemented.cpp)
endif()

if (APPLE AND NOT IOS)
    list(APPEND SOURCES Platform/TaskRoleMacOS.cpp)
    list(APPEND SOURCES Platform/ThreadQoSMacOS.cpp)
endif()

if (LINUX OR BSD)
    list(APPEND SOURCES TimeZoneWatcherUnix.cpp)
elseif (APPLE AND NOT IOS)
    list(APPEND SOURCES TimeZoneWatcherMacOS.mm)
elseif (WIN32)
    list(APPEND SOURCES TimeZoneWatcherWindows.cpp)
else()
    list(APPEND SOURCES TimeZoneWatcherUnimplemented.cpp)
endif()

if (APPLE OR CMAKE_SYSTEM_NAME STREQUAL "GNU")
    list(APPEND SOURCES MachPort.cpp)
endif()

if (APPLE)
    list(APPEND SOURCES IOSurface.cpp)
    list(APPEND SOURCES Platform/ScopedAutoreleasePoolMacOS.mm)
endif()

ladybird_lib(LibCore core EXPLICIT_SYMBOL_EXPORT)
target_link_libraries(LibCore PRIVATE Threads::Threads LibUnicode LibTextCodec mimalloc)

# Development builds are in no stage of development, and leave it out of their product name.
set(browser_is_release_build 0)
set(browser_is_final_release 0)
if (NOT LADYBIRD_VERSION STREQUAL "")
    set(browser_is_release_build 1)
    set(browser_release_stage "${LADYBIRD_RELEASE_STAGE}")
    if (browser_release_stage STREQUAL "")
        set(browser_is_final_release 1)
    endif()
endif()
string(JOIN " " browser_product_name "Ladybird" ${browser_release_stage})
configure_file(BrowserVersion.h.in BrowserVersion.h @ONLY)
if (ENABLE_INSTALL_HEADERS)
    install(FILES "${CMAKE_CURRENT_BINARY_DIR}/BrowserVersion.h" COMPONENT Lagom_Development
        DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/LibCore")
endif()

if (${CMAKE_SYSTEM_NAME} MATCHES "NetBSD")
    # NetBSD has its shm_open and shm_unlink functions in librt so we need to link that
    target_link_libraries(LibCore PRIVATE rt)
endif()
if (${CMAKE_SYSTEM_NAME} MATCHES "SunOS")
    # Solaris has socket and networking related functions in two extra libraries
    target_link_libraries(LibCore PRIVATE nsl socket)
endif()
if (HAIKU)
    # Haiku has networking related functions in the network library
    target_link_libraries(LibCore PRIVATE network)
endif()

if (APPLE)
    target_link_libraries(LibCore PUBLIC "-framework CoreFoundation")
    target_link_libraries(LibCore PUBLIC "-framework CoreGraphics")
    target_link_libraries(LibCore PUBLIC "-framework CoreLocation")
    target_link_libraries(LibCore PUBLIC "-framework CoreServices")
    target_link_libraries(LibCore PUBLIC "-framework Foundation")
    target_link_libraries(LibCore PUBLIC "-framework IOSurface")
endif()

if (WIN32)
    target_link_libraries(LibCore PRIVATE ntdll.dll)
    find_path(DIRENT_INCLUDE_DIR dirent.h REQUIRED)
    target_include_directories(LibCore PRIVATE ${DIRENT_INCLUDE_DIR})
endif()

if (ANDROID)
    target_link_libraries(LibCore PRIVATE log)
endif()

if ((APPLE AND NOT IOS) OR LINUX)
    target_sources(LibCore PRIVATE CrashHandlerPOSIX.cpp)
else()
    target_sources(LibCore PRIVATE CrashHandlerUnimplemented.cpp)
endif()
