Re: [CMake] Check whether C++ headers are self-sufficient
On Sat, Mar 14, 2015 at 01:19:21PM +0100, Christoph Grüninger 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 > #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. I've taken exactly your approach. However: - I'm not testing the double include at present - I generate a single target per library, collecting all the object files into a single testcase - I compile each header *twice*, so in addition to catching errors in the include guards, I also catch errors where I might have accidentally defined a global or static member in the header and this will trigger a link error For anyone two wants to reuse it: https://github.com/openmicroscopy/bioformats/blob/develop/cpp/cmake/HeaderTest.cmake (it uses gtest but that's easily excised if not required) To use, in your source directory: https://github.com/openmicroscopy/bioformats/blob/develop/cpp/lib/ome/bioformats/CMakeLists.txt#L182 header_include_list_write(STATIC_HEADERS_VAR GENERATED_HEADERS_VAR header-test-name dest-dir) And in your test directory (if different) https://github.com/openmicroscopy/bioformats/blob/develop/cpp/test/ome-bioformats/CMakeLists.txt#L45 header_test_from_file(bin-name library-name test-name) While these were written for this particular project, if there's interest in having a similar thing in cmake, I'd be happy to clean it up and improve it for general use. Kind regards, Roger -- .''`. Roger Leigh : :' : Debian GNU/Linuxhttp://people.debian.org/~rleigh/ `. `' schroot and sbuild http://alioth.debian.org/projects/buildd-tools `-GPG Public Key F33D 281D 470A B443 6756 147C 07B3 C8BC 4083 E800 -- 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
Re: [CMake] Check whether C++ headers are self-sufficient
Based on what was posted.. I was able to get this working without much difficulty. For all of my libraries, I use the technique borrowed from my qmake days, so all headers (and source) are stored as a variable ${project_H} and if it's a qt based header ${qtproject_H} So I added to every CMakeLists.txt for every library the following call add_header_test(libname ${project_H} ${qtproject_H}) Now the function add_header, was pretty much what was posted, except I made some minor tweaks. Howevr, I wanted to be able to make these tests into a CTest based system. Not just a "did it compile" but when I do a make test, it make sure all were compiled. Im not sure how the mailing list handles attachments, so I just cut and pasted it, if anyone wants a copy just email me directly. We do use google test, and the Mock files will not work (and shouldn't be tested in all honesty) and we have some internal .h files, hence the check to see if it should be tested. FUNCTION(add_header_test name) STRING(REPLACE "${CMAKE_SOURCE_DIR}" "" LCL_DIR ${CMAKE_CURRENT_LIST_DIR}) STRING(REPLACE "CoreApp" "CLI" LCL_DIR ${LCL_DIR}) SET(FOLDER_NAME "HeaderTests/${LCL_DIR}") SET(HEADERS ${ARGN}) SET(SUFFIX ".cxx") SET(CXXFILES) FOREACH(currHeader ${HEADERS}) STRING(REPLACE "${CMAKE_CURRENT_BINARY_DIR}" "" currHeader "${currHeader}") GET_FILENAME_COMPONENT(headerbase ${currHeader} NAME_WE) SET(headername ${currHeader} ) IF( NOT ${headername} MATCHES "_int.h" AND NOT ${headername} MATCHES "Mock" ) SET(src ${CMAKE_CURRENT_BINARY_DIR}/TestHeaders_${name}_${headerbase}${SUFFIX}) CONFIGURE_FILE(${CMAKE_SOURCE_DIR}/FileTemplates/TestHeaders.cpp.in ${src}) SET(cxxfiles ${cxxfiles} ${src}) ENDIF() ENDFOREACH() SET(main ${CMAKE_CURRENT_BINARY_DIR}/TestHeaders_${name}${SUFFIX}) CONFIGURE_FILE(${CMAKE_SOURCE_DIR}/FileTemplates/TestHeadersMain.cpp.in ${main}) SET(TEST_NAME TestHeaders_${name}) SET(CMAKE_AUTOMOC OFF) include_directories(${CMAKE_CURRENT_SOURCE_DIR}) ADD_EXECUTABLE(${TEST_NAME} ${main} ${cxxfiles} ${HEADERS}) ADD_TEST( ${TEST_NAME} ${TEST_NAME} ) SET_TARGET_PROPERTIES(${TEST_NAME} PROPERTIES FOLDER ${FOLDER_NAME}) SET_SOURCE_FILES_PROPERTIES(${HEADERS} PROPERTIES HEADER_FILE_ONLY TRUE) ADD_DEPENDENCIES(BUILD_HEADER_CHECKS ${TEST_NAME}) ADD_DEPENDENCIES(ALL_TEST ${TEST_NAME}) ENDFUNCTION(add_header_test) ADD_CUSTOM_TARGET( BUILD_HEADER_CHECKS ) SET_TARGET_PROPERTIES(BUILD_HEADER_CHECKS PROPERTIES FOLDER CMakePredefinedTargets) The TestHeaders.cpp.in // first one makes sure it is self sufficient #include "@headername@" // second include makes sure its guarded properly #include "@headername@" The TestHeadersMain.cpp.in int main( int /* argc */, char ** /*argcv*/ ) { return 0; } Scott -Original Message- From: CMake [mailto:cmake-boun...@cmake.org] On Behalf Of Christoph Grüninger Sent: Monday, March 16, 2015 0:07 AM To: cmake@cmake.org Subject: Re: [CMake] Check whether C++ headers are self-sufficient Hi Andreas, > Why do you want to use one library per header? I think, it would be > sufficient to have one cc-file per header and one library per folder. Then I can test also for single headers. > Why do you worry about large build directories? Because for slow file systems like Windows it takes several minutes just to create all the build folders and files. When we have teaching sessions the computer pool uses some remote file system (NFS?) which almost collapses if several participants configure our software at the same time. Bye Christoph -- 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/co
Re: [CMake] Check whether C++ headers are self-sufficient
Hi Andreas, > Why do you want to use one library per header? I think, it would be > sufficient to have one cc-file per header and one library per folder. Then I can test also for single headers. > Why do you worry about large build directories? Because for slow file systems like Windows it takes several minutes just to create all the build folders and files. When we have teaching sessions the computer pool uses some remote file system (NFS?) which almost collapses if several participants configure our software at the same time. Bye Christoph -- 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
Re: [CMake] Check whether C++ headers are self-sufficient
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 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 > 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 >> #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 -- 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
Re: [CMake] Check whether C++ headers are self-sufficient
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 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 > #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
Re: [CMake] Check whether C++ headers are self-sufficient
Why do you want to use one library per header? I think, it would be sufficient to have one cc-file per header and one library per folder. The library would simply contain all files. Why do you worry about large build directories? Regards, Andreas Am 14.03.2015 um 13:19 schrieb Christoph Grüninger: 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 #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 -- 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
[CMake] Check whether C++ headers are self-sufficient
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 #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