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

Scott Aron Bloom scott.bloom at onshorecs.com
Mon Mar 16 04:42:18 EDT 2015


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-bounces at cmake.org] On Behalf Of Christoph Grüninger
Sent: Monday, March 16, 2015 0:07 AM
To: cmake at 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


More information about the CMake mailing list