For large repository with many projects, dependencies and contributors, getting everything to work nicely is not an easy task. Standardization is the biggest issue. The idea behind this project is to get any project up and running with the tooling needed for a modern C or C++ codebase.
Features in a nutshell:
- Encapsulate complexities of cmake in module files
- Very small and simple makefiles for end user projects
- Handle external dependencies between projects using
find_package - Out of source compilation
- Support for installing both 'release' and 'debug' binaries and shared objects
- Support for installation target. The binaries can be installed with the command
make install. - Automatic
version.hgenerated files. - Provide toolchain support
Following is the main structure how the code is divided. Main important files are the files that end in cmake and are in templates directory. Samples directory contains two samples on how to use and can be removed later.
.
├── LICENSE.md
├── otto.cmake
├── otto_create_version.cmake
├── otto_external_dependency.cmake
├── otto_find.cmake
├── otto_install.cmake
├── otto_run.cmake
├── otto_set_toolchain.cmake
├── README.md
├── sample
│ ├── sayHello
│ │ ├── CMakeLists.txt
│ │ ├── include
│ │ │ ├── dummy.h
│ │ │ └── sayHello.h
│ │ └── src
│ │ ├── sayHello.cpp
│ │ └── sayit.cpp
│ └── saySomething
│ ├── CMakeLists.txt
│ └── src
│ └── saySomething.cpp
├── templates
│ ├── ProjectConfig.cmake
│ └── version.h.in
└── toolchains
└── linux.x86_64.gcc92.cmake
8 directories, 19 files
Note: Make sure to use cmake 3.15 or above as some of the generator expressions that are in use do not work properly with older revisions.
- Clone the repository to get the boilerplate:
git clone https://github.com/jainvishal/otto.git
cd otto
- Configure
ottoto match your release and installation environment.otto.cmakeprovides following variables that can be tuned.
CMAKE_INSTALL_PREFIX- Where to publish headers, shared library and binaryOTTO_INSTALL_RPATH_PREFIX- Expected path in production where libraries and binaries are installed. This sets therpath.
-
Create your toolchain file (with path to compiles and flags). See provided toolchain file for ideas.
-
Looking at a test project that publishes header, a shared library and a binary. It is
sayHelloproject in sample directory. ItsCMakeLists.txtfile shall provide enough guidance on the usage. To compile, follow below steps insayHelloroot directory.-
configure:
cmake -S . -B build -DCMAKE_BUILD_TYPE=Release -DOTTO_TOOLCHAIN=linux.x86_64.gcc91- Change
-DCMAKE_BUILD_TYPEtoDebugto compile in debug mode. - Change
-DOTTO_TOOLCHAINto whichever toolchain file is required.
- Change
-
To compile: (on linux
makeinbuilddirectory can also be used, also note that this could be skipped as next step will performt the build as well)cmake --build build
-
To install (in
CMAKE_INSTALL_PREFIX): (on linuxmake installinbuilddirectory will do same)cmake --build build --target install
So now you compiled a project that produces header file, a shared library and also a test binary. It also installs cmake configuration files for other projects to use
find_packageto configure flags. -
-
Next step now is to use what we just accomplished. Look at
saySomethingproject. Again checkCMakeLists.txtin that project for the idea on how to specify external dependencies.-
Setup dependencies: Main variable to look for is
OTTO_DEPENDENCY_ON. It is a list of<package>:<version>, e.g. if the project depends on:- sayHello version 1.5
- myMonitoring version 7.65
Value of
OTTO_DEPENDENCY_ONwould besayHello:1.5 myMonitoring:7.65. Note that for now this will only work if both packages and their versions are compiled usingotto(or at least published in same format asottopublishes). -
Configure and compile: Use same steps as
sayHello. -
Test: When
saySomething(from published area) is run orlddorobjdump -xis used on it, it will show that it is using rightrpathto find libraries fromOTTO_INSTALL_RPATH_PREFIXpath followed byCMAKE_INSTALL_PREFIX.
-
.
├── lib
│ └── cmake
│ └── sayHello-0.0 -> ../../sayHello/0.0.1/cmake
├── sayHello
│ └── 0.0.1
│ ├── cmake
│ │ ├── sayHelloConfig.cmake
│ │ ├── sayHelloConfigVersion.cmake
│ │ ├── sayHelloTargets.cmake
│ │ └── sayHelloTargets-debug.cmake
│ ├── include
│ │ ├── dummy.h
│ │ └── sayHello.h
│ └── linux.x86_64.gcc92
│ ├── bin_debug
│ │ └── sayHello
│ └── lib_debug
│ ├── libsayHello.so -> libsayHello.so.0
│ ├── libsayHello.so.0 -> libsayHello.so.0.0.1
│ └── libsayHello.so.0.0.1
└── saySomething
└── 1.0.0
└── linux.x86_64.gcc92
└── bin_debug
└── saySomething
14 directories, 11 files