C++ Builder 10.2.3 – CMake and Ninja Command-Line Support

by Mar 23, 2018

Introduction

CMake is a popular C++ build tool. RAD Studio 10.2.3 Tokyo provides support for building CMake projects on the command line using RAD Studio compilers. 10.2.3 also specifically supports using Ninja with CMake, allowing for very fast parallel builds.

This will allow you to easily build third-party libraries without converting them to a C++Builder project. You can also use Ninja for improved build times.

CMake supports RAD Studio's Clang-enhanced compilers, and for Win32 uses the new Clang-enhanced driver, bcc32x. CMake command line support is provided for Windows, Android, and iOS.

Installing CMake and Ninja

CMake

Download and install CMake 3.10. Use the binary installer, since it can optionally add CMake to the system path. Make sure you select that option during the installation.

CMake files are located in C:Program Files (x86)EmbarcaderoStudio19.0cmake. However, to prevent errors in the build process, you need to move one file manually. Follow the steps below to do this:

1.       Locate your CMake installation folder and the ModulesPlatform subfolder. E.g. C:Program FilesCMakesharecmake-3.10ModulesPlatform

2.       Locate the Windows-Embarcadero.cmake file and make a backup.

3.       Copy Windows-Embarcadero.cmake from the Studio19.0cmake folder and overwrite the version in the CMake folder.

We have greatly extended the inbuilt CMake support for the Windows compilers and you need to use this file to build successfully.

Ninja

Download and install Ninja 1.8.2. You’ll need to add it to the system path manually.

Ninja is a small build system with a focus on speed. It differs from other build systems in two major respects: it is designed to have its input files generated by a higher-level build system, and it is designed to run builds as fast as possible.

CMakeLists.txt

Here is an example CMakeLists.txt file to build one .cpp file:

cmake_minimum_required (VERSION 3.10)

project (GuessANumber)

set_embt_target("DynamicRuntime")

add_executable(GuessANumber Main.cpp)

What this  CMakeLists.txt file does is:

1. This file works with CMake 3.10 (that's ten not one) or higher,

2. There is a project called GuessANumber

3. The set_embt_target is an Embarcadero-specific line.  This macro specifies whether to link with the VCL or FMX, the dynamic runtime, or as a package.  Here we will be linking with the “DynamicRuntime”.

 4. Lastly, the add_executable says to build GuessNumber.exe, build Main.cpp.

Running CMake 

1. Open a RAD Studio Command Prompt.

C:Program Files (x86)EmbarcaderoStudio19.0bin>

2. Change directory (cd) to the folder where the project source code is located.

C:Program Files (x86)EmbarcaderoStudio19.0bin>cd C:UsersamannDocumentsEmbarcaderoStudioProjectsCppGuessANumber

C:UsersamannDocumentsEmbarcaderoStudioProjectsCppGuessANumber>

For this exmaple, run CMake using this command ine: 

cmake -DCMAKE_C_COMPILER=bcc32x.exe -DCMAKE_CXX_COMPILER=bcc32x.exe -G Ninja

The –G Ninja generates build.ninja files.  The build.ninja file is generated into the build tree.

Running this cmake command produces this output:

— The C compiler identification is Embarcadero 7.30.36015

— The CXX compiler identification is Embarcadero 7.30.36015

— Check for working C compiler: C:/Program Files (x86)/Embarcadero/Studio/19.0/bin/bcc32x.exe — works

— Detecting C compiler ABI info – done

— Check for working CXX compiler: C:/Program Files (x86)/Embarcadero/Studio/19.0/bin/bcc32x.exe — works

— Detecting CXX compiler ABI info – done

— Configuring done

— Generating done

Build files have been written to: C:/Users/amann/Documents/Embarcadero/Studio/Projects/CppGuessANumber

Next, run ninja from the command line.

Ninja outputs this:

[1/2] Building CXX object CMakeFilesGuessANumber.dirMain.cpp.obj

Embarcadero C++ 7.30 for Win32 Copyright (c) 2012-2017 Embarcadero Technologies, Inc.

Embarcadero Technologies Inc. bcc32x version 3.3.1 (36350.30c6854.779bede) (based on LLVM 3.3.1)

Target: i686-pc-win32-omf

Thread model: posix

Main.cpp:

[2/2] Linking CXX executable GuessANumber.exe

 Embarcadero C++ 7.30 for Win32 Copyright (c) 2012-2017 Embarcadero Technologies, Inc.

Embarcadero Technologies Inc. bcc32x version 3.3.1 (36350.30c6854.779bede) (based on LLVM 3.3.1)

Target: i686-pc-win32-omf

Thread model: posix

bcc32x.exe: warning: argument unused during compilation: '-nobuiltininc'

bcc32x.exe: warning: argument unused during compilation: '-Xclang -cxx-abi'

bcc32x.exe: warning: argument unused during compilation: '-Xclang borland'

 "C:PROGRA~2EMBARC~1Studio19.0binilink32.exe" @"C:UsersamannAppDataLocalTempGuessANumber-817693.cfg"

Turbo Incremental Link 6.90 Copyright (c) 1997-2017 Embarcadero Technologies, Inc.

And our GuessANumber.exe gets created!

Advantages of CMake Support for C++Builder

·       Many third-party open source C++ libraries come packaged as CMake projects.  In the past, to use these you'd have to create a C++Builder project for them, which is manual work.  Now, you can just build them on the command line right away.

·       This makes it significantly easier for you to use other common libraries in your projects.

·       One secondary item is that we support using Ninja with CMake. This allows parallel building. We already support parallel compilation in C++Builder, but I'll cover Ninja in a followup blog post.