How to auto generate pkgconfig files from cmake targets : <p>I would like to generate pkgconfig files in cmake from the targets. I stared by writing something like this:</p>

<pre><code>function(auto_pkgconfig TARGET)

    get_target_property(INCLUDE_DIRS ${TARGET} INTERFACE_INCLUDE_DIRECTORIES)
    string(REPLACE "$&lt;BUILD_INTERFACE:" "$&lt;0:" INCLUDE_DIRS "${INCLUDE_DIRS}")
    string(REPLACE "$&lt;INSTALL_INTERFACE:" "$&lt;1:" INCLUDE_DIRS "${INCLUDE_DIRS}")
    string(REPLACE "$&lt;INSTALL_PREFIX&gt;" "${CMAKE_INSTALL_PREFIX}" INCLUDE_DIRS "${INCLUDE_DIRS}")


    file(GENERATE OUTPUT ${TARGET}.pc CONTENT "
Name: ${TARGET}
Cflags: -I$&lt;JOIN:${INCLUDE_DIRS}, -I&gt;
Libs: -L${CMAKE_INSTALL_PREFIX}/lib -l${TARGET}
")
    install(FILES ${TARGET}.pc DESTINATION lib/pkgconfig)
endfunction()
</code></pre>

<p>This is a simplified version but it basically reads the <code>INTERFACE_INCLUDE_DIRECTORIES</code> properties and processes the <code>INSTALL_INTERFACE</code> of the generator expressions. 
This works well as long as the include directories are set before calling <code>auto_pkgconfig</code>, like this:</p>

<pre><code>add_library(foo foo.cpp)

target_include_directories(foo PUBLIC 
    $&lt;BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/include&gt;
    $&lt;INSTALL_INTERFACE:$&lt;INSTALL_PREFIX&gt;/include&gt;
    ${OTHER_INCLUDE_DIRS}
)

auto_pkgconfig(foo)
</code></pre>

<p>However, sometimes properties are set after the call to <code>auto_pkgconfig</code>, like this:</p>

<pre><code>add_library(foo foo.cpp)
auto_pkgconfig(foo)

target_include_directories(foo PUBLIC 
    $&lt;BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/include&gt;
    $&lt;INSTALL_INTERFACE:$&lt;INSTALL_PREFIX&gt;/include&gt;
    ${OTHER_INCLUDE_DIRS}
)
</code></pre>

<p>However, this won't properly read the include directories anymore. I would like <code>auto_pkgconfig</code> to run after all the target properties are set. I could use generator expressions for this, by changing <code>auto_pkgconfig</code> to this:</p>

<pre><code>function(auto_pkgconfig TARGET)

    file(GENERATE OUTPUT ${TARGET}.pc CONTENT "
Name: ${TARGET}
Cflags: -I$&lt;JOIN:$&lt;TARGET_PROPERTY:${TARGET},INTERFACE_INCLUDE_DIRECTORIES&gt;, -I&gt;
Libs: -L$&lt;TARGET_FILE_DIR:${TARGET}&gt; -l${TARGET}
")
    install(FILES ${TARGET}.pc DESTINATION lib/pkgconfig)
endfunction() 
</code></pre>

<p>However, this will read the <code>BUILD_INTERFACE</code> instead of the <code>INSTALL_INTERFACE</code>. So is there another way to read target properties after they have been set?</p>
