summaryrefslogtreecommitdiffstats
path: root/cmake/FindWrapBrotli.cmake
blob: bc8e058f99621c170220a79eb7eec5d9b972e77f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
if(TARGET WrapBrotli::WrapBrotliDec)
    set(WrapBrotli_FOUND ON)
    return()
endif()

# From VCPKG
find_package(unofficial-brotli CONFIG QUIET)
if (unofficial-brotli_FOUND)
    add_library(WrapBrotli::WrapBrotliDec INTERFACE IMPORTED)
    target_link_libraries(WrapBrotli::WrapBrotliDec INTERFACE unofficial::brotli::brotlidec)

    add_library(WrapBrotli::WrapBrotliEnc INTERFACE IMPORTED)
    target_link_libraries(WrapBrotli::WrapBrotliEnc INTERFACE unofficial::brotli::brotlienc)

    add_library(WrapBrotli::WrapBrotliCommon INTERFACE IMPORTED)
    target_link_libraries(WrapBrotli::WrapBrotliCommon INTERFACE unofficial::brotli::brotlicommon)

    set(WrapBrotli_FOUND ON)
else()
    find_package(PkgConfig QUIET)
    if (PKG_CONFIG_FOUND)
        pkg_check_modules(libbrotlidec QUIET libbrotlidec IMPORTED_TARGET)
        if (libbrotlidec_FOUND)
            add_library(WrapBrotli::WrapBrotliDec INTERFACE IMPORTED)
            target_link_libraries(WrapBrotli::WrapBrotliDec INTERFACE PkgConfig::libbrotlidec)
            set(WrapBrotli_FOUND ON)
        endif()

        pkg_check_modules(libbrotlienc QUIET libbrotlienc IMPORTED_TARGET)
        if (libbrotlienc_FOUND)
            add_library(WrapBrotli::WrapBrotliEnc INTERFACE IMPORTED)
            target_link_libraries(WrapBrotli::WrapBrotliEnc INTERFACE PkgConfig::libbrotlienc)
            set(WrapBrotli_FOUND ON)
        endif()

        pkg_check_modules(libbrotlicommon QUIET libbrotlicommon IMPORTED_TARGET)
        if (libbrotlicommon_FOUND)
            add_library(WrapBrotli::WrapBrotliCommon INTERFACE IMPORTED)
            target_link_libraries(WrapBrotli::WrapBrotliCommon INTERFACE PkgConfig::libbrotlicommon)
            set(WrapBrotli_FOUND ON)
        endif()
    else()
        find_path(BROTLI_INCLUDE_DIR NAMES "brotli/decode.h")

        foreach(lib_name BrotliDec BrotliEnc BrotliCommon)
            string(TOLOWER ${lib_name} lower_lib_name)

            find_library(${lib_name}_LIBRARY_RELEASE
                         NAMES ${lower_lib_name} ${lower_lib_name}-static)

            find_library(${lib_name}_LIBRARY_DEBUG
                         NAMES ${lower_lib_name}d ${lower_lib_name}-staticd
                         ${lower_lib_name} ${lower_lib_name}-static)

            include(SelectLibraryConfigurations)
            select_library_configurations(${lib_name})

            if (BROTLI_INCLUDE_DIR AND ${lib_name}_LIBRARY)
                set(${lib_name}_FOUND TRUE)
            endif()

            if (${lib_name}_FOUND AND NOT TARGET WrapBrotli::Wrap${lib_name})
                add_library(WrapBrotli::Wrap${lib_name} UNKNOWN IMPORTED)
                set_target_properties(WrapBrotli::Wrap${lib_name} PROPERTIES
                                      INTERFACE_INCLUDE_DIRECTORIES "${BROTLI_INCLUDE_DIR}"
                                      IMPORTED_LOCATION "${${lib_name}_LIBRARY}")

                if(${lib_name}_LIBRARY_RELEASE)
                    foreach(config_name RELEASE RELWITHDEBINFO MINSIZEREL)
                        set_property(TARGET WrapBrotli::Wrap${lib_name} APPEND PROPERTY
                                     IMPORTED_CONFIGURATIONS ${config_name})
                        set_target_properties(WrapBrotli::Wrap${lib_name} PROPERTIES
                                              IMPORTED_LOCATION_${config_name} "${${lib_name}_LIBRARY_RELEASE}")
                    endforeach()
                endif()

                if(${lib_name}_LIBRARY_DEBUG)
                    set_property(TARGET WrapBrotli::Wrap${lib_name} APPEND PROPERTY
                                 IMPORTED_CONFIGURATIONS DEBUG)
                    set_target_properties(WrapBrotli::Wrap${lib_name} PROPERTIES
                                          IMPORTED_LOCATION_DEBUG "${${lib_name}_LIBRARY_DEBUG}")
                endif()
            endif()
        endforeach()

        include(FindPackageHandleStandardArgs)
        find_package_handle_standard_args(WrapBrotli REQUIRED_VARS
                                          BrotliDec_FOUND BrotliEnc_FOUND BrotliCommon_FOUND)

        if (WrapBrotli_FOUND)
            set_property(TARGET WrapBrotli::WrapBrotliDec APPEND PROPERTY
                         INTERFACE_LINK_LIBRARIES WrapBrotli::WrapBrotliCommon)
            set_property(TARGET WrapBrotli::WrapBrotliEnc APPEND PROPERTY
                         INTERFACE_LINK_LIBRARIES WrapBrotli::WrapBrotliCommon)
        endif()
    endif()
endif()