[CMake] Check whether C++ headers are self-sufficient

Ryan Pavlik ryan.pavlik at gmail.com
Sat Mar 14 21:03:21 EDT 2015


I also have this implemented - in my case, the headers are fine to include
in a .cpp with just a do-nothing main function and build into an
executable, testing linking too.

https://github.com/rpavlik/util-headers/blob/master/tests/cleanbuild/CMakeLists.txt

Ryan

On Sat, Mar 14, 2015 at 8:56 AM Robert Maynard <robert.maynard at kitware.com>
wrote:

> I have worked on projects where we do something fairly similar. The
> significant difference is that we create only a single executable per
> directory. All told is it a fairly simple and looks something like:
>
> # Builds a source file and an executable that does nothing other than
> # compile the given header files.
> function(add_header_test name)
>   set(hfiles ${ARGN})
>   set(suffix ".cpp")
>   set(cxxfiles)
>   foreach (header ${ARGN})
>     string(REPLACE "${CMAKE_CURRENT_BINARY_DIR}" "" header "${header}")
>     get_filename_component(headername ${header} NAME_WE)
>     set(src
> ${CMAKE_CURRENT_BINARY_DIR}/TestBuild_${name}_${headername}${suffix})
>     configure_file(${SMTK_SOURCE_DIR}/CMake/TestBuild.cxx.in ${src} @ONLY)
>     set(cxxfiles ${cxxfiles} ${src})
>   endforeach (header)
>
>   add_library(TestBuild_${name} ${cxxfiles} ${hfiles})
>   target_include_directories(TestBuild_${name} ${CMAKE_CURRENT_BINARY_DIR})
>   set_source_files_properties(${hfiles} PROPERTIES HEADER_FILE_ONLY TRUE)
> endfunction(add_header_test)
>
> On Sat, Mar 14, 2015 at 8:19 AM, Christoph Grüninger <foss at grueninger.de>
> wrote:
>
>> Dear CMakers,
>> I want to have all my C++ headers self-sufficient (self-contained),
>> i.e., a header can be included without additional includes. This is not
>> only handy but also part of Google's C++ styleguide [2].
>>
>> It would be great to have a make target (let's call it headercheck),
>> that can check for this, by compiling a simple test.cc file for each
>> current_header.h:
>>   #include <config.h>
>>   #include "current_header.h"
>>   #include "current_header.h"
>>
>> Additionally it would be great to have such a target for every folder
>> (checking all headers recursively) and every header that is explicitly
>> passed as an argument.
>>
>> We tried this with CMake: We generate a test.cc file per header and
>> create a library for every cc file. The problem is, that we get hundreds
>> of additional targets, we generate a lot of folders and files which can
>> increase our build directory size by an order of magnitude and it does
>> not work properly on a per file or per directory basis.
>>
>> What do you think, is there a good way to have such a target headercheck
>> with CMake? Or would it be better to include it as a CTest? Or better as
>> an external (bash) script as proposed in [1]?
>>
>> If it can be done in a good way with CMake, would it be of interest to
>> include it as a feature in CMake? Or as an external project similar to
>> UseLATEX.cmake?
>>
>> Bye
>> Christoph
>>
>> [1]
>>
>> http://stackoverflow.com/questions/1892043/self-sufficient-header-files-in-c-c
>> [2]
>>
>> http://google-styleguide.googlecode.com/svn/trunk/cppguide.html#Self_contained_Headers
>>
>> --
>> When you die, that does not mean that you lose to cancer,
>> you beat cancer by how you live, why you live, and in the
>> manner in which you live.      -- Stuart Scott, 1965-2015
>> --
>>
>> Powered by www.kitware.com
>>
>> Please keep messages on-topic and check the CMake FAQ at:
>> http://www.cmake.org/Wiki/CMake_FAQ
>>
>> Kitware offers various services to support the CMake community. For more
>> information on each offering, please visit:
>>
>> CMake Support: http://cmake.org/cmake/help/support.html
>> CMake Consulting: http://cmake.org/cmake/help/consulting.html
>> CMake Training Courses: http://cmake.org/cmake/help/training.html
>>
>> Visit other Kitware open-source projects at
>> http://www.kitware.com/opensource/opensource.html
>>
>> Follow this link to subscribe/unsubscribe:
>> http://public.kitware.com/mailman/listinfo/cmake
>>
>
> --
>
> Powered by www.kitware.com
>
> Please keep messages on-topic and check the CMake FAQ at:
> http://www.cmake.org/Wiki/CMake_FAQ
>
> Kitware offers various services to support the CMake community. For more
> information on each offering, please visit:
>
> CMake Support: http://cmake.org/cmake/help/support.html
> CMake Consulting: http://cmake.org/cmake/help/consulting.html
> CMake Training Courses: http://cmake.org/cmake/help/training.html
>
> Visit other Kitware open-source projects at http://www.kitware.com/
> opensource/opensource.html
>
> Follow this link to subscribe/unsubscribe:
> http://public.kitware.com/mailman/listinfo/cmake
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/cmake/attachments/20150315/d1e08e8e/attachment.html>


More information about the CMake mailing list