summaryrefslogtreecommitdiff
path: root/cmake/Modules/PkgBuildConfig.cmake
blob: 1c0079eb00c54c19656e628e16726f4c7ff2f268 (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
98
99
100
101
102
103
104
105
106
107
108
109
110
# pkg-config file generation
#
# Uses the following globals:
# - PKG_BUILD_PREFIX: the build location (aka prefix). Defaults to CMAKE_INSTALL_PREFIX
# - PKG_BUILD_LIBDIR: the libdir location. Defaults to ${prefix}/lib.
# - PKG_BUILD_INCLUDEDIR: the includedir location. Defaults to ${prefix}/include.
#

function(pkg_build_config)
    set(options)
    set(oneValueArgs NAME DESCRIPTION VERSION FILENAME LIBS_SELF)
    set(multiValueArgs LIBS PRIVATE_LIBS REQUIRES CFLAGS)

    cmake_parse_arguments(PKGCONFIG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})

    if (NOT DEFINED PKGCONFIG_FILENAME AND DEFINED PKGCONFIG_NAME)
        set(PKGCONFIG_FILENAME ${PKGCONFIG_NAME})
    endif()
    if (NOT DEFINED PKGCONFIG_FILENAME)
        message(FATAL_ERROR "Missing FILENAME argument")
    endif()
    set(PKGCONFIG_FILE "${PROJECT_BINARY_DIR}/${PKGCONFIG_FILENAME}.pc")

    if (NOT DEFINED PKGCONFIG_DESCRIPTION)
        message(FATAL_ERROR "Missing DESCRIPTION argument")
    endif()

    if (NOT DEFINED PKGCONFIG_VERSION)
        message(FATAL_ERROR "Missing VERSION argument")
    endif()

    if (DEFINED PKG_BUILD_PREFIX)
        set(PKGCONFIG_PREFIX "${PKG_BUILD_PREFIX}")
    else()
        set(PKGCONFIG_PREFIX "${CMAKE_INSTALL_PREFIX}")
    endif()

    if(DEFINED PKG_BUILD_LIBDIR)
        if (IS_ABSOLUTE ${PKG_BUILD_LIBDIR})
            set(PKGCONFIG_LIBDIR ${PKG_BUILD_LIBDIR})
        else()
            set(PKGCONFIG_LIBDIR "\${prefix}/${PKG_BUILD_LIBDIR}")
        endif()
    else()
        set(PKGCONFIG_LIBDIR "\${prefix}/lib")
    endif()

    if(DEFINED PKG_BUILD_INCLUDEDIR)
        if (IS_ABSOLUTE ${PKG_BUILD_INCLUDEDIR})
            set(PKGCONFIG_INCLUDEDIR ${PKG_BUILD_INCLUDEDIR})
        else()
            set(PKGCONFIG_INCLUDEDIR "\${prefix}/${PKG_BUILD_INCLUDEDIR}")
        endif()
    else()
        set(PKGCONFIG_INCLUDEDIR "\${prefix}/include")
    endif()

    # Write .pc "header"
    file(WRITE "${PKGCONFIG_FILE}"
        "prefix=\"${PKGCONFIG_PREFIX}\"\n"
        "libdir=\"${PKGCONFIG_LIBDIR}\"\n"
        "includedir=\"${PKGCONFIG_INCLUDEDIR}\"\n"
        "\n"
        "Name: ${PKGCONFIG_NAME}\n"
        "Description: ${PKGCONFIG_DESCRIPTION}\n"
        "Version: ${PKGCONFIG_VERSION}\n"
    )

    # Prepare Libs
    if(NOT DEFINED PKGCONFIG_LIBS_SELF)
        set(PKGCONFIG_LIBS_SELF "${PKGCONFIG_FILE}")
    endif()

    if(NOT DEFINED PKGCONFIG_LIBS)
        set(PKGCONFIG_LIBS "-l${PKGCONFIG_LIBS_SELF}")
    else()
        list(INSERT PKGCONFIG_LIBS 0 "-l${PKGCONFIG_LIBS_SELF}")
    endif()

    list(REMOVE_DUPLICATES PKGCONFIG_LIBS)
    string(REPLACE ";" " " PKGCONFIG_LIBS "${PKGCONFIG_LIBS}")
    file(APPEND "${PKGCONFIG_FILE}" "Libs: -L\${libdir} ${PKGCONFIG_LIBS}\n")

    # Prepare Libs.private
    if(DEFINED PKGCONFIG_PRIVATE_LIBS)
        list(REMOVE_DUPLICATES PKGCONFIG_PRIVATE_LIBS)
        string(REPLACE ";" " " PKGCONFIG_PRIVATE_LIBS "${PKGCONFIG_PRIVATE_LIBS}")
        file(APPEND "${PKGCONFIG_FILE}" "Libs.private: ${PKGCONFIG_PRIVATE_LIBS}\n")
    endif()

    # Prepare Requires.private
    if(DEFINED PKGCONFIG_REQUIRES)
        list(REMOVE_DUPLICATES PKGCONFIG_REQUIRES)
        string(REPLACE ";" " " PKGCONFIG_REQUIRES "${PKGCONFIG_REQUIRES}")
        file(APPEND "${PKGCONFIG_FILE}" "Requires.private: ${PKGCONFIG_REQUIRES}\n")
    endif()

    # Prepare Cflags
    if(DEFINED PKGCONFIG_CFLAGS)
        string(REPLACE ";" " " PKGCONFIG_CFLAGS "${PKGCONFIG_CFLAGS}")
    else()
        set(PKGCONFIG_CFLAGS "")
    endif()
    file(APPEND "${PKGCONFIG_FILE}" "Cflags: -I\${includedir} ${PKGCONFIG_CFLAGS}\n")

    # Install .pc file
    install(FILES "${PKGCONFIG_FILE}"
        DESTINATION "${PKGCONFIG_PREFIX}/${PKGCONFIG_LIBDIR}/pkgconfig"
    )
endfunction()