Home

Awesome

CMake Determine Header Language

CMake macro to determine the language of a header file.

Status

Travis CI (Ubuntu)AppVeyor (Windows)CoverageBiicodeLicence
TravisAppVeyorCoverallsBiicodeLicense

Description

cmake-determine-header-language can be used to detect the language of header files based on what other files include them. This can be used for various bits of build tooling, where the build tooling itself quite require a language to be explicitly set for the header file to be scanned.

Usage

cmake-determine-header-language works by scanning source files for #include statements and then checks the passed include directories for files matching the name of files specified in the #include statements.

Scanning for header files

You can scan a source file for headers recursively using psq_scan_source_for_headers. The macro will apply the following rules to any detected header file.

See, for example:

psq_scan_source_for_headers (SOURCE /my_source.c
                             INCLUDES /path/to/include/dir
                             CPP_IDENTIFIERS
                             MY_SOURCE_IS_COMPILING_CXX)

Getting the language of any source

Once sources have been scanned for header files, their language can be obtained with psq_determine_language_for_source. If this function is used on a header which was not part of an already scanned source, then a fatal error will result.

See, for example:

psq_determine_language_for_source (/my_header.h LANGUAGE WAS_HEADER)

LANGUAGE will be set to C, CXX or both should the header be able to be compiled in both modes. WAS_HEADER will be set to TRUE if the scanned source was a header, FALSE otherwise.

Reference

psq_source_type_from_source_file_extension

Returns the initial type of a source file from its extension. It doesn't properly analyze headers and source inclusions to determine the language of any headers.

The type of the source will be set in the variable specified in RETURN_TYPE. Valid values are C_SOURCE, CXX_SOURCE, HEADER and UNKNOWN

psq_scan_source_for_headers

Opens the source file SOURCE at its absolute path and scans it for #include statements if we have not done so already. The content of the include statement is pasted together with each provided INCLUDE and checked to see if it forms the path to an existing or generated source. If it does, then the following rules apply to determine the language of the header file:

If the source including the header is a CXX source (including a CXX header, and no other language has been set for this header, then the language of the header is set to CXX

If any source including the header is a C source (including a C header) then the language of the header is forced to "C", with one caveat:

The header file will be opened and scanned for any tokens which match any provided tokens in CPP_IDENTIFIERS or __cplusplus. If it does, then the header language will be set to C;CXX

psq_determine_language_for_source

Takes any source, including a header file and writes the determined language into LANGUAGE_RETURN. If the source is a header file SOURCE_WAS_HEADER_RETURN will be set to true as well.

This function only works for header files if those header files were included by sources previously scanned by psq_scan_source_for_headers. They must be scanned before this function is called, otherwise this function will be unable to determine the language of the source file and report an error.