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

Posted by on in Blogs

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)\Embarcadero\Studio\19.0\cmake. 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 Modules\Platform subfolder. E.g. C:\Program Files\CMake\share\cmake-3.10\Modules\Platform

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

3.       Copy Windows-Embarcadero.cmake from the Studio\19.0\cmake 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)\Embarcadero\Studio\19.0\bin>

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

C:\Program Files (x86)\Embarcadero\Studio\19.0\bin>cd C:\Users\amann\Documents\Embarcadero\Studio\Projects\CppGuessANumber\

C:\Users\amann\Documents\Embarcadero\Studio\Projects\CppGuessANumber>

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 CMakeFiles\GuessANumber.dir\Main.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~2\EMBARC~1\Studio\19.0\bin\ilink32.exe" @"C:\Users\amann\AppData\Local\Temp\GuessANumber-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.

 



About
Gold User, Rank: 90, Points: 4
Al Mannarino has 25+ years of software development experience, including object-oriented analysis and design (OOAD) and developing and deploying production applications. He is currently a Principal Software Consultant and Evangelist for Embarcadero Technologies. Prior to joining Embarcadero, Al spent three years working with CodeGear, a division of Borland that was acquired by Embarcadero in 2008. He also worked for five years as a lead systems engineer for Borland supporting application lifecycle management, software delivery optimization and developer tools solutions. Prior to Borland, Al served as a systems engineer for companies including Objectivity, Versant, Red Brick Systems, Information Builders, and was an electrical engineer for Grumman Aerospace performing application implementations on complex electrical-mechanical systems. Al has a bachelor's of science degree in electrical engineering from Manhattan College.

Comments

Check out more tips and tricks in this development video: