INTRODUCTION Overview Download and Install Documentation Publications REPOSITORY Libraries DEVELOPER Dev Guide Dashboard PEOPLE Contributors Users Project Download Mailing lists
|
Tutorial for the GearBox Build SystemThe term build system refers the software and procedures responsible for compiling, installing, and distributing source code. GearBox uses CMake to handle its build system. CMake is a cross-platform tool, designed as a replacement for autotools.Contents: BasicsBuilds are controlled by a set of files called 'CMakeLists.txt'. There is approximately one in each directory, and builds descend recursively through the source tree.An individual developer only needs to be concerned with writing CMakeLists.txt files in the directory containing his or her library code and below. ExampleHere's an example of a CMakeLists.txt file to builid a library. We'll go through it line by line.
SET ( lib_name GbxAdvanced ) GBX_ADD_LICENSE( GPL ) SET ( build TRUE ) # Check for human input GBX_REQUIRE_OPTION( build LIB ${lib_name} ON ) # Check arbitrary variables GBX_REQUIRE_VAR( build LIB ${lib_name} GBX_OS_LINUX "only Linux OS is supported" ) # Check local targets (libraries or executables) SET( dep_libs basic ) GBX_REQUIRE_TARGETS( build LIB ${lib_name} ${dep_libs} ) IF ( build ) INCLUDE( ${GBX_CMAKE_DIR}/UseBasicRules.cmake ) FILE( GLOB hdrs *.h ) FILE( GLOB srcs *.cpp ) GBX_ADD_LIBRARY( ${lib_name} DEFAULT ${srcs} ) TARGET_LINK_LIBRARIES( ${lib_name} ${dep_libs} ) GBX_ADD_HEADERS( gbxadvanced ${hdrs} ) ENDIF ( build ) Line-by-LineSET ( lib_name GbxAdvanced ) string lib_name = "GbxAdvanced" ).
Notice the GearBox convention for naming CMake variables:
GBX_ADD_LICENSE( GPL ) LICENSE file at the top level of the distribution (check out what it looks like). This is a custom GearBox macro as indicated by the "GBX_" prefix. Any text can be put into this macro, e.g. "LGPL (with written permission from the original authors)".
SET ( build TRUE ) build and set it to TRUE, i.e. "this library will be built". Failure to meet any one of the requirements will assign FALSE to this "cumulative variable".
# Check for human input GBX_REQUIRE_OPTION( build LIB ${lib_name} ON ) This GearBox macro checks for user input into the build process. It is actually a shortcut which does several things:
Notice that to evaluate the variable you have to inclose it in braces and add a dollar sign, i.e. This macro is quite flexible. You can specify custom names for the option variables and provide a custom description.
It is sometimes useful to disable compilation of all targets and then selectively enable a few. To do this set GBX_DISABLE_ALL to ON. In this case, all default compilation settings for all targets are set to OFF. It is still possible to individually enable targets with e.g. ENABLE_LIB_GBXADVANCED=ON. For GBX_DISABLE_ALL to take effect, make sure you clear CMake cache first, by deleting CMakeCache.txt file.
GBX_REQUIRE_VAR( build LIB ${lib_name} GBX_OS_LINUX "only Linux OS is supported" ) GBX_OS_LINUX , evaluates to TRUE. Examples of usage are: check that the OS is supported, the compiler version is acceptable, that dependencies are found, versions of required libraries are new enough, etc.
SET( dep_libs basic ) GBX_REQUIRE_TARGETS( build LIB ${lib_name} ${dep_libs} ) dep_libs can contain a list of libraries separated by a space. In this case we only have one called "basic". In order to reduce the amount of typing required we will reuse this variable later when it comes to actually linking.
IF ( build ) build variable can tell us to build or not to build.
A note on slightly funky CMake syntax: notice that in the IF statement and in other places with a boolean context, CMake accepts both the variable name, i.e.
INCLUDE( ${GBX_CMAKE_DIR}/UseBasicRules.cmake ) [GEARBOX-SRC]/cmake. For convenience, this path is stored in the special variable GBX_CMAKE_DIR . The INCLUDE command literally pastes the contents of the referenced scripts right here in the middle of our file. So you can take a look at that script to see what it does. Importantly, it adds -Wall compiler definition.
FILE( GLOB hdrs *.h ) FILE( GLOB srcs *.cpp ) hdrs and srcs .
GBX_ADD_LIBRARY( ${lib_name} DEFAULT ${srcs} ) TARGET_LINK_LIBRARIES( ${lib_name} ${dep_libs} )
Now you can see why we bothered with defining variables. These commands are generic and can be easily used for other libraries.
GBX_ADD_HEADERS( gbxadvanced ${hdrs} ) [PREFIX]/include/gearbox/gbxadvanced. With the custom macro only the module subdirectory needs to be specified.
ENDIF ( build ) IF statements: the signature of the closing ENDIF must be identical.A comprehensive list of CMake variables defined by GearBoxBuild system configuration:GBX_DEFAULT_LIB_TYPE # Valid options {SHARED, STATIC}. Defaults to SHARED. Project description: GBX_PROJECT_VERSION_MAJOR GBX_PROJECT_VERSION_MINOR GBX_PROJECT_VERSION_PATCH GBX_PROJECT_VERSION (=MAJOR.MINOR.PATCH) GBX_PROJECT_NAME_LOWER (e.g. 'gearbox' ) GBX_PROJECT_NAME_UPPER (e.g. 'GEARBOX' ) GBX_PROJECT_NAME_CAP (e.g. 'Gearbox' ) OS variables: evaluate to TRUE when running on the corresponding OS, otherwise to FALSE. We define our own because the standard CMake ones are inconsistently named and the one for Linux is not defined. GBX_OS_LINUX GBX_OS_MAC GBX_OS_QNX GBX_OS_WIN Under Linux, an additional variable if defined GBX_PROC_64BIT Source and binary directories can be distinguished as so GBX_PROJECT_BINARY_DIR GBX_PROJECT_SOURCE_DIR Install directories can be referenced with absolute paths GBX_BIN_INSTALL_DIR GBX_CMAKE_INSTALL_DIR GBX_CMAKE_PKGCONFIG_INSTALL_DIR GBX_INCLUDE_INSTALL_DIR GBX_LIB_INSTALL_DIR GBX_PKGCONFIG_INSTALL_DIR GBX_SHARE_INSTALL_DIR GBX_BIN_INSTALL_SUFFIX GBX_CMAKE_INSTALL_SUFFIX GBX_CMAKEPKGCONFIG_INSTALL_SUFFIX GBX_INCLUDE_INSTALL_SUFFIX GBX_LIB_INSTALL_SUFFIX GBX_PKGCONFIG_INSTALL_SUFFIX GBX_SHARE_INSTALL_SUFFIX |