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

WikiCMake Build LogicCMake ScriptsHerc0x.cmake

Four utility scripts provide standardized functionality for the remaining CMake build scripts.

This script is a library of functions that standardize activities of the CMake scripts for Hercules. The functions can be grouped into the following categories:

Error Message Management
  • herc_Save_Error - Save error message for presentation at build end.

This function collects error messages that describe issues detected during configuration of the Hercules build tool script. See Error Message Display for additional information.

Tests for Headers, Functions, Structures, and Symbols
  • herc_Check_Include_Files - Test for a header and set the HAVE_ variable
  • herc_Check_Function - Test for a function and set the HAVE_ variable
  • herc_Check_Struct_Member - Test a structure for a member and set HAVE_STRUCT
  • herc_Check_Symbol_Exists - Test whether a string exists as a macro or identifier

These functions use CMake commands to test for the existence of items needed to build Hercules. Items may be required or optional; if required, an error message is logged. A CMake variable is set to indicated presence or absence of an item.

The variable name is standardized to match the name generated by a GNU configure script. For example, a test for the existence of the header stdlib.h would create the CMake variable HAVE_STDLIB_H.

C Compiler tests
  • herc_Check_C11_Atomics - Collect lock free status of C11 atomic intrinics
  • herc_Check_Packed_Struct - Determine if packed structs are supported
  • herc_Check_Compile_Capability - Test sundry compiler capabilities
  • herc_Check_Strict_Aliasing - Test for problems created by strict aliasing

These tests compile a short c program to test how the compiler treats certain c language constructs. Function herc_Check_Compile_Capability compiles a program provided by the caller, usually just a return statement, while the rest use c programs provided with the CMake build scripts.

Target Creation
  • herc_Define_Executable - Define a target for an executable program
  • herc_Define_Shared_Lib - Define a target for a shared library
  • herc_Install_Imported_Target - Install files provided by imported target
  • herc_Create_System_Import_Target - Create import target for system shared library.

The Hercules build creates a number of executables, shared libraries, and loadable modules. The two 'Define' functions standardize creation of the CMake targets for these items.

The function herc_Install_Imported_Target ensures any external package that is not installed in a system directory gets copied to the correct installation directory. Such packages include those built at the same time as Hercules and those previously built (-D\<pkg>_DIR=\<path> option).

The function herc_Create_System_Import_Target creates a synthetic import target for external packages that are used from system library locations, for example zlib. Creation of a synthetic target allows the CMake scripts to use the same logic for packages built by the scripts and those installed on the system.

Build Option Edits
  • herc_Check_User_Option_YesNo - Validate a user option as YES/NO/TARGET
  • herc_Check_User_Option_YesNoSysHerc - Validate a user option as YES/NO/SYSTEM/HERCULES

These straightforward functions standardize testing for common build option values. The option values are canonicallized to a non-abbreviated upper case string.


This CMake script checks if the Hercules source directory is a repository clone. If it is, it uses git commands to extract the status of the repository. The script then runs configure_file to create a commitinfo.h header using the extracted status.

If the source directory is not a cloned repository or if the git commands fail, dummy values are used to populate commitinfo.h

This CMake script is the target of an Add_Custom_Command call in Herc60_CreateTargets.cmake and is executed directly by the root CMakeLists.txt script.


This script contains the herc_ExtPackageBuild function. The function is defined in a separate script only because it is a long function. Logically it would make sense to include it in Herc00_Includes.cmake if it were shorter.

This function clones an external package, runs a CMake configure on it, and imports the target created by the CMake configure. It also does the externalproject_add command to cause the cloned external package to be configured and built when Hercules is configured and built.


This CMake script is the target of Add_Custom_Command calls in Herc65_ExtPackageWinCopy.cmake. The added commands ensure that shared libraries created by executable packages are copied to the Hercules binary directory when Hercules is built on Windows.

The added commands, one Add_Custom_Command for each external package, are run each time Hercules is built.

This script and the commands are not needed on UNIX-like and macOS builds because the shared libraries are located using the RPATH set in the calling executable or shared library. CMake sets RPATH to include the build directories of all external packages not retrieved from system libraries.


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.