Skip to content
Stephen Orso edited this page May 13, 2018 · 1 revision

WikiCMake Build LogicCMake ScriptsCMake C programs

A set of c programs are used to test compiler characteristics.

Most of these programs are run via CMake try_compile commands coded in functions defined in Herc00_Includes.

Atomic Operation Capability Tests

These three programs test which, if any, atomic operation intrinsics or functions may be used to emulate the Interlocked Access Facility 2 instructions (And Immediate, Or Immediate, and Exclusive Or Immediate). The CMake scripts Herc25_Compilergcclike.cmake and Herc25_CompilerMSVC.cmake for gcc-like and MSVC compilers respectively use the results of these tests to set variables that are eventually queried by hatomic.h.

Executed by a try_run command in function herc_Check_C11_Atomics in Herc00_Includes.cmake; while this confounds cross-compilation, gcc 4.9.2 requires program execution because the test result comes from a function call, not a macro definition.

This c program creates a CMake script of set commands that create CMake variables defining whether C11 Atomics are lock-free. The script name is determined by the caller of the herc_Check_C11_Atomics function. The file is currently named CMakeHercC11LockFree.cmake and is built in the binary directory.

Compiled by a try_compile command, this program tests if selected atomic operations are supported by the c compiler. This program is run only if C11 atomic operations are not available.

Compiled by a try_compile command, this program tests if selected _sync_ built-in functions are available in the c compiler. This program is run only if C11 atomic operations and Atomic operations are not available.

Array and Structure Definition Tests

Compiled by a try_compile command, this program tests whether the C compiler in use will accept a structure definition that includes a flexible array, for example:

struct flexible
{
    int foo;
    char bar[];
}

The MSVC compiler is assumed to support C99 flexible array definitions; this test program is not compiled when the target system is Windows.

Executed by a try_run command in function herc_Check_Packed_Struct in Herc00_Includes.cmake, this program determines if the c compiler in use will respect an instruction to not insert alignment padding within a structure. The program compares the size of a structure that requires padding to align its second variable to the size of its component variables.

The same program is used for gcc-like and MSVC compilers even though the two compilers take radically different approaches to instructing the compiler to pack a structure. Preprocessor code adapts the program to the compiler in use.

It would be an interesting exercise to change this program to detect padding without requiring execution, perhaps by using a compile-time assertion, or if this test could be combined with the test for structure padding, next.

Executed by a try_run command in function herc_Check_Struct_Padding in Herc00_Includes.cmake, this program determines if the c compiler in use will respect an instruction to not insert alignment padding at the end of a structure. The program compares the size of a structure that requires padding to align successive elements in an array of that structure.

The same program is used for gcc-like and MSVC compilers even though the two compilers take radically different approaches to instructing the compiler to pad a structure. Preprocessor code adapts the program to the compiler in use.

It would be an interesting exercise to change this program to detect padding without requiring execution, perhaps by using a compile-time assertion, or if this test could be combined with the test for structure packing, above.


This Wiki page Copyright © 2018 by Stephen R. Orso. This work is licensed under the Creative Commons Attribution-ShareAlike 4.0 International License. To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/4.0/ or send a letter to Creative Commons, PO Box 1866, Mountain View, CA 94042, USA.