cmake_minimum_required(VERSION 3.16)

add_library(libraw STATIC)

target_compile_definitions(
    libraw

    # private compile definitions
    PRIVATE

    # public compile definitions
    PUBLIC
#    -DLIBRAW_LIBRARY_BUILD

    # LIBRAW_NODLL (Session BD-3): LibRaw is always linked statically here,
    # but its Windows header chain defaults to __declspec(dllimport) for
    # consumers and dllexport for the lib's own TUs (libraw_types.h checks
    # LIBRAW_NODLL FIRST, then LIBRAW_BUILDLIB, else dllimport). The mixed
    # objects made every consumer link thunk the references (LNK4049/4217/
    # 4286) and grew a spurious import lib next to the exes. PUBLIC so both
    # the lib's own TUs and every consumer TU (MYRawDecoder includes libraw
    # headers; PRIVATE links still inherit INTERFACE defines for their own
    # compiles) agree on plain static linkage. Off Windows the whole DllDef
    # chain is preprocessed away (LIBRAW_WIN32_DLLDEFS requires _WIN32 +
    # MSVC/Intel), so this is object-neutral there.
    -DLIBRAW_NODLL
)

target_include_directories(
    libraw

    # private include directories
    PRIVATE

    # public include directories
    PUBLIC
    ${CMAKE_CURRENT_SOURCE_DIR}
)

IF (WIN32)

    target_compile_definitions(
        libraw

        # private compile definitions
        PRIVATE
        -D_CRT_SECURE_NO_WARNINGS
        -D_ATL_SECURE_NO_WARNINGS
        -D_AFX_SECURE_NO_WARNINGS
        # LIBRAW_BUILDLIB (dllexport) dropped in Session BD-3 — dead under
        # the PUBLIC LIBRAW_NODLL above (libraw_types.h checks NODLL first).

        # LIBRAW_NO_WINSOCK2: LibRaw's three winsock2.h sites exist only to
        # carry internal/defines.h's `#pragma comment(lib, "ws2_32.lib")` —
        # the vendored tree calls no socket API at all. But winsock2.h was
        # also LibRaw's ONLY windows.h supply, and its Windows arm needs it
        # (TCHAR/HANDLE in libraw_datastream.h, parsed by all five TUs, and
        # the MSVC _BitScanReverse/_byteswap_ulong intrinsics in crx.cpp).
        # So the define is paired with the forced <windows.h> below, with
        # _WINSOCKAPI_ keeping legacy winsock.h out of it (WindowsHeaders.h
        # uses the same guard). PRIVATE, not PUBLIC like NODLL above: the
        # pragma never reached consumer TUs (internal/defines.h has exactly
        # one includer), and stripping winsock2.h — hence windows.h — from
        # SDK consumer TUs that include libraw/libraw.h would break ones
        # that today compile only because of it.
        -DLIBRAW_NO_WINSOCK2
        -D_WINSOCKAPI_

        # public compile definitions
        PUBLIC

    )

    target_compile_options(libraw PRIVATE /FIwindows.h)

    # Disable Windows warnings for libraw.
    target_compile_options(libraw PUBLIC /wd4146)
    target_compile_options(libraw PUBLIC /wd4251)
    target_compile_options(libraw PUBLIC /wd4267)
    target_compile_options(libraw PUBLIC /wd26451)
    target_compile_options(libraw PUBLIC /wd26812)

ENDIF()


target_sources(libraw PRIVATE
    
    internal/defines.h
    internal/libraw_cxx_defs.h
    internal/libraw_internal_funcs.h
    
    libraw/libraw_alloc.h
    libraw/libraw_const.h
    libraw/libraw_datastream.h
    libraw/libraw_internal.h
    libraw/libraw_types.h
    libraw/libraw_version.h
    libraw/libraw.h

    src/decoders/crx.cpp
    src/decoders/fuji_compressed.cpp
    src/decoders/pana8.cpp

    src/libraw_cxx_stub.cpp
    src/libraw_datastream.cpp
)
