• # Cross Platform Test Driven Development Environment Using CMake (Part 4)

General and Gameplay Programming

• Posted By Guest
In part 3 of the series, we finished up with the last target type and then added the desired unit testing library with a simple example of usage. The initial work was all about getting simple things to build and how to cover all the targets properly. It is time to get into some further bits of CMake and possible uses within your codebase. Solving some common coding problems, integrating better with IDE's and cleaning up the CMake files themselves is what will be covered in this last part focused on CMake. Parts 1 2 3 4 5 The final completed (within reason) environment:

# Being More Specific

One of the key things which I did in prior work was to use the simple generic detection variables in places where we needed to know something about the target. For instance, I used 'APPLE' to fix some of the early problems on OSX. What if you need to setup flags for a specific OS though? For instance, perhaps you have a network layer which has variations based on the target OS and specific versions of the OS? In a network system, you might have variations such as kqueue for BSD and derivatives (such as OSX), epoll for Linux and event ports for Windows. Additionally you may need to fall back to less scalable solutions based on the OS version, for instance if you use event cancellation in Windows event ports there are difficulties. You need to choose some other solution on Windows XP and prior versions due to bugs in the implementation. The need for more fine grained detection of platform/target OS is required to give the code more to work with when making such choices. Much of the detection can be performed at compile time with the preprocessor. But is this good enough? First you have to figure out how to detect the compiler being run which can be difficult since finding preprocessor definitions is not always easy. This is even more complicated than it seems though due to compilers trying to be compatible with other compilers and impersonating them. For Windows compilers you may need to query if it defines '_MSVC' and then check that it is not an imposter such as Intel Compiler or Vector C etc. Once the compiler is detected, what about the target version of the OS? The OS information is not often passed into the compiler and checking defines in various headers won't very often help. What we need is a more reasonable method of dealing with the detections, thankfully CMake supplies this for us.

## Compiler Detection

CMake performs the specific detections we are looking for, in fact the generic ones we have used so far are derived from the detailed detections it has performed. In order to get the real compiler you can avoid the preprocessor difficulties by using the CMake defined variables: CMAKE_C_COMPILER_ID or CMAKE_CXX_COMPILER_ID. These two variables contain a string which corresponds to a specific compiler, even if that compiler were trying to impersonate another. In order to make detection more specific, you may use the following within a listfile:  IF( "${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC" ) SET( COMPILER_MSVC ON ) ELSEIF( "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel" ) SET( COMPILER_INTEL ON ) ELSEIF( "${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" ) SET( COMPILER_GNU ON ) ELSEIF( "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" ) SET( COMPILER_CLANG ON ) ELSE() SET( COMPILER_UNKNOWN ON ) ENDIF()  With this script the specific compiler is identified correctly, impersonators are differentiated and if the user attempts to use an unknown compiler, say Vector C, then 'COMPILER_UNKNOWN' would be set. Before the user even bothers to compile, the CMake step could tell them that their setup is not supported. The user could decide to add the support, complain to the maintainer or of course give up.

# Conclusion

While not as detailed as prior parts of this series, the information covered fleshes out the knowledge required to create a multi-platform and cross compilation capable CMake build environment. Further articles will transition to using this environment and extending it further but other than small things, will not be covering further detail of CMake. As a practical guide, the presented material supplemented with some Google searching (GameDev.net and StackOverflow being a very common source of answers), should give you all the tools required to move forward compiling on nearly any target.

Report Article

## User Feedback

You need to be a member in order to leave a review

## Create an account

Register a new account