[CMake] CMake and exported headers?

Petr Kmoch petr.kmoch at gmail.com
Sun Jun 3 04:59:10 EDT 2012


Hi Mikael.

I don't think such functionality is built in, but it seems doable with
cmake functions and custom commands/targets.

Do you want the copying to happen at configure time (running cmake) or
build time (running make or similar)? For the former, you can use
`configure_file(... COPYONLY ...)'. For the latter (which I assume is
what you actually want), a possible way is to use custom commands and
a target, something like this (coded for ilustration, with no
guarantee of direct usability):

set(PublicHeaders the_headers_you_want_copied)
set(PublishedHeaders "")
foreach(Header IN LISTS PublicHeaders)
 get_filename_component(HeaderFilename "${Header}" NAME)
 set(Output "~/out/project/${HeaderFilename}")
 list(APPEND PublishedHeaders "${Output}")
 add_custom_command(
  OUTPUT "${Output}"
  COMMAND "${CMAKE_COMMAND}" -E copy_if_different "${Header}"
"~/out/project/${HeaderFilename}"
  MAIN_DEPENDENCY "${Header}"
  COMMENT "Publishing ${HeaderFilename}"
  VERBATIM
 )
endforeach()
add_custom_target(
 publish_headers
 ALL
 DEPENDS ${PublishedHeaders}
 SOURCES ${PublicHeaders}
)

You can then add dependencies between the project's target and
publish_headers so that `make project' also makes publish_headers. If
you need this for more than one project, you can easily encapsulate
the above code in a function.

The directory `~/out/project' can be added to the include paths by
include_directories().

Hope this helps,

Petr

On Sun, Jun 3, 2012 at 6:42 AM, Mikael Lyngvig <mikael at lyngvig.org> wrote:
> Hi,
>
> I've searched the docs, the FAQ, and Google, albeit somewhat quickly, and
> have not found an answer to my question.  My question is this:
>
> Is it possible to have CMake copy header files from the local source
> directory to the output directory and then automatically use the exported
> headers with an -I option to the compiler, with the added requirement that
> CMake must also add the global project name to the include path?
>
> My source code is arranged as follows:
>
>     ~/src/project/: All the source files of the project.
>     ~/obj/project/: All the object files of the project (i.e. obj can be
> erased safely at any time)
>     ~/out/project: All the headers and libraries that are "exported" (copied
> out) from the project.  These are ready for distribution and/or reuse in
> other projects.
>
> ~/out/project/ reflects the structure of the src tree 100 percent so that I
> use this code to include:
>
> [~/project/src/a/foo.cpp]
> #include "project/a/foo.hpp"
> #include "project/b/bar.hpp"
>
> In other words, I want to have to explicitly state the full prefix of the
> imported header file just like they are doing in LLVM.  However, I do NOT
> want to maintain my headers separately from my source files!
>
> Dunno if I make much sense, but is it possible to have CMake use a scheme
> where public header files are copied to another directory and then included
> from _there_ instead of from the local directory?  I use this setup because
> I don't want to accidentally export a private header file and because it
> provides in-source documentation of the structure of the project. Also, this
> way, my libraries are ready for prime time and can be readily reused in
> other projects without me having to copy around and maintain redundant
> copies.
>
> Thanks in advance for any help!
>
>
> Cheers,
> Mikael
> -- Love Thy Frog!
>
> --
>
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Please keep messages on-topic and check the CMake FAQ at:
> http://www.cmake.org/Wiki/CMake_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://www.cmake.org/mailman/listinfo/cmake


More information about the CMake mailing list