cmake_minimum_required(VERSION 3.22.1)
project(ldpc)

message("CMAKE_SYSTEM_INFO_FILE           ${CMAKE_SYSTEM_INFO_FILE}")
message("CMAKE_SYSTEM_NAME                ${CMAKE_SYSTEM_NAME}")
message("CMAKE_SYSTEM_PROCESSOR           ${CMAKE_SYSTEM_PROCESSOR}")
message("CMAKE_SYSTEM                     ${CMAKE_SYSTEM}")
#string (REGEX MATCH "\\.el[1-9]" os_version_suffix ${CMAKE_SYSTEM})
#message("os_version_suffix                ${os_version_suffix}")

if ((DEFINED ${ANDROID_NATIVE_API_LEVEL}) OR ("Android" STREQUAL ${CMAKE_SYSTEM_NAME}))
    message("ANDROID_NATIVE_API_LEVEL         ${ANDROID_NATIVE_API_LEVEL}")
    message("CMAKE_TOOLCHAIN_FILE             ${CMAKE_TOOLCHAIN_FILE}")
    message("ANDROID_ABI                      ${ANDROID_ABI}")
    message("-> ANDROID_NATIVE_API_LEVEL is defined building for android")
else()
    message("-> ANDROID_NATIVE_API_LEVEL is not defined building for linux")
endif()

message("CMAKE_SOURCE_DIR                 ${CMAKE_SOURCE_DIR}")
message("CMAKE_SOURCE_DIR + ANDROID_ABI   ${CMAKE_SOURCE_DIR}/libs/${ANDROID_ABI}/")






if ((DEFINED ${ANDROID_NATIVE_API_LEVEL}) OR ("Android" STREQUAL ${CMAKE_SYSTEM_NAME}))

    message("----------------------")
    message(" building for android")

    find_library(log-lib log)
    message("  log                              ${log-lib}")
    find_library(EGL-lib EGL)
    message("  EGL                              ${EGL-lib}")
    find_library(GLESv2-lib GLESv2)
    message("  GLESv2                           ${GLESv2-lib}")
    find_library(GLESv3-lib GLESv3)
    message("  GLESv3                           ${GLESv3-lib}")
    find_library(c++_shared-lib c++_shared)
    message("  c++_shared                       ${c++_shared-lib}")

    message("----------------------")
    message(" building mpv")

    add_library(player SHARED player.cpp)
    target_include_directories(player PUBLIC ${CMAKE_SOURCE_DIR}/include/${ANDROID_ABI})

    add_library( mpv SHARED IMPORTED )
    set_target_properties( mpv PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/libs/${ANDROID_ABI}/libmpv.so )
    add_library( avdevice SHARED IMPORTED )
    set_target_properties( avdevice PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/libs/${ANDROID_ABI}/libavdevice.so )
    add_library( avfilter SHARED IMPORTED )
    set_target_properties( avfilter PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/libs/${ANDROID_ABI}/libavfilter.so )
    add_library( swscale SHARED IMPORTED )
    set_target_properties( swscale PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/libs/${ANDROID_ABI}/libswscale.so )
    add_library( avformat SHARED IMPORTED )
    set_target_properties( avformat PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/libs/${ANDROID_ABI}/libavformat.so )
    add_library( avcodec SHARED IMPORTED )
    set_target_properties( avcodec PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/libs/${ANDROID_ABI}/libavcodec.so )
    add_library( avutil SHARED IMPORTED )
    set_target_properties( avutil PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/libs/${ANDROID_ABI}/libavutil.so )
    add_library( swresample SHARED IMPORTED )
    set_target_properties( swresample PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/libs/${ANDROID_ABI}/libswresample.so )

    target_link_options(player PRIVATE "-Wl,-z,max-page-size=16384")
    target_link_libraries(
            player
            ${log-lib}
            ${EGL-lib}
            ${GLESv2-lib}
            ${GLESv3-lib}
            ${c++_shared-lib}
            mpv
            avdevice
            avfilter
            swscale
            avformat
            avcodec
            avutil
            swresample
    )

    message("----------------------")
    message(" building thumb")

    add_library(thumb SHARED thumb.cpp)
    target_include_directories(thumb PUBLIC ${CMAKE_SOURCE_DIR}/include/${ANDROID_ABI})

    target_link_options(thumb PRIVATE "-Wl,-z,max-page-size=16384")
    target_link_libraries(thumb
            ${log-lib}
            ${c++_shared-lib}
            android
            avdevice
            avfilter
            swscale
            avformat
            avcodec
            avutil
            swresample
    )

    message("----------------------")
    message(" building discover")

    add_library(ssl STATIC IMPORTED)
    set_target_properties( ssl PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/libs/${ANDROID_ABI}/libssl.a )
    add_library(crypto STATIC IMPORTED)
    set_target_properties( crypto PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/libs/${ANDROID_ABI}/libcrypto.a )
    add_library(lzma STATIC IMPORTED)       # compress to xz
    set_target_properties( lzma PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/libs/${ANDROID_ABI}/liblzma.a )

    add_library(event STATIC IMPORTED)
    set_target_properties( event PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/libs/${ANDROID_ABI}/libevent.a )
    add_library(zstd STATIC IMPORTED)
    set_target_properties( zstd PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/libs/${ANDROID_ABI}/libzstd.a )
    add_library(lz STATIC IMPORTED)
    set_target_properties( lz PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/libs/${ANDROID_ABI}/libz.a )
    add_library(nghttp2 STATIC IMPORTED)
    set_target_properties( nghttp2 PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/libs/${ANDROID_ABI}/libnghttp2.a )
    add_library(curl STATIC IMPORTED)
    set_target_properties( curl PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/libs/${ANDROID_ABI}/libcurl.a )

    add_library(tor STATIC IMPORTED )
    set_target_properties( tor PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/libs/${ANDROID_ABI}/libtor.a )

    add_library(discover SHARED discover.c)
    target_include_directories(discover PUBLIC ${CMAKE_SOURCE_DIR}/include/${ANDROID_ABI})

    target_link_options(discover PRIVATE "-Wl,-z,max-page-size=16384")
    target_link_libraries(
        discover
        ${log-lib}
        tor
        curl
        nghttp2
        ssl
        crypto
        zstd
        lz
        lzma
        event
    )

    ###################################### ocrypt ###########################################
    #
    #add_library(ocrypt SHARED ocrypt.c)
    #target_include_directories(ocrypt PUBLIC ${CMAKE_SOURCE_DIR}/include/${ANDROID_ABI})
    #
    #target_link_libraries(
    #        ocrypt
    #        ${log-lib}
    #        ${c++_static-lib}
    #        ssl
    #        crypto
    #        )

    message("----------------------")

else()

    message("")
    message(" building for linux : cmake --fresh . && make clean && make")

    if (EXISTS "thumb.cpp" AND EXISTS "thumb.h")

        message("")
        message(" building thumb : ./thumb \${f} \${f}.thmb 256 256 15 100")
        message("")

        find_library(avdevice-lib avdevice)
        message("  libavdevice                         ${avdevice-lib}")
        find_library(avfilter-lib avfilter)
        message("  libavfilter                         ${avfilter-lib}")
        find_library(swscale-lib swscale)
        message("  libswscale                          ${swscale-lib}")
        find_library(avformat-lib avformat)
        message("  libavformat                         ${avformat-lib}")
        find_library(avcodec-lib avcodec)
        message("  libavcodec                          ${avcodec-lib}")
        find_library(avutil-lib avutil)
        message("  libavutil                           ${avutil-lib}")
        find_library(swresample-lib swresample)
        message("  libswresample                       ${swresample-lib}")

        add_executable(thumb thumb.cpp)

        target_link_options(thumb PRIVATE "-Wl,-z,max-page-size=16384")
        target_link_libraries(
                thumb
                ${avdevice-lib}
                ${avfilter-lib}
                ${swscale-lib}
                ${avformat-lib}
                ${avcodec-lib}
                ${avutil-lib}
                ${swresample-lib}
        )

    endif()

    if (EXISTS "discover.c" AND EXISTS "discover.h")

        message("")
        message(" building discover : ./discover \$PWD/tor1/ 9053 8003 8003 8083 8083")
        message("")

        find_library(curl-lib curl)
        message("  libcurl                             ${curl-lib}")
        find_library(nghttp2-lib nghttp2)
        message("  libnghttp2                          ${nghttp2-lib}")
        find_library(zstd-lib zstd)
        message("  libzstd                             ${zstd-lib}")
        find_library(z-lib z)
        message("  libz                                ${z-lib}")
        find_library(lzma-lib lzma)
        message("  liblzma                             ${lzma-lib}")
        find_library(event-lib event)
        message("  libevent                            ${event-lib}")
        find_library(ssl-lib ssl)
        message("  libssl                              ${ssl-lib}")
        find_library(crypto-lib crypto)
        message("  libcrypto                           ${crypto-lib}")

        add_executable(discover discover.c)

        # fonctionne bien avec tor mais sans curl/nghttp2 ou l'inverse
        # brotli rtmp curl_idn_encode
        target_link_options(discover PRIVATE "-Wl,-z,max-page-size=16384")
        target_link_libraries(
                discover
                ${curl-lib}
                ${nghttp2-lib}
                #tor
                #cap
                #seccomp
                ${zstd-lib}
                ${z-lib}
                ${lzma-lib}
                ${event-lib}
                ${ssl-lib}
                ${crypto-lib}
                )
        message("")

    endif()

endif()












































