Add to an Existing Project¶
We recommend adding Libtropic to an existing project as a git submodule. Libtropic uses the CMake build system, so it can be added to the compilation of existing CMake projects as follows:
- Set path to the Libtropic submodule, for example as:
set(PATH_LIBTROPIC ${CMAKE_CURRENT_SOURCE_DIR}/../vendor/libtropic/) - Add the Libtropic subdirectory:
add_subdirectory(${PATH_LIBTROPIC} "libtropic") - By default, Libtropic does not link a CFP (Cryptographic Functionality Provider) or its CAL (Crypto Abstraction Layer), so it can be built as a static library. This is the consumer's responsibility:
- For the chosen CFP (e.g. MbedTLS v4.0.0), add the correct subdirectory inside
libtropic/cal/, which provides the corresponding CAL sources and include directories:add_subdirectory("${PATH_LIBTROPIC}cal/mbedtls_v4") - Add the obtained sources and include directories to the
tropictarget:target_sources(tropic PRIVATE ${LT_CAL_SRCS}) target_include_directories(tropic PUBLIC ${LT_CAL_INC_DIRS}) - Link the CFP (provided by the consumer) to the
tropictarget:target_link_libraries(tropic PUBLIC mbedtls)
- For the chosen CFP (e.g. MbedTLS v4.0.0), add the correct subdirectory inside
- By default, libtropic does not link platform-specific code or its HAL, so it can be built as a static library. This is the consumer's responsibility:
- For the chosen platform (e.g. Linux with HW SPI), add a corresponding HAL using
add_subdirectory:add_subdirectory("${PATH_TO_LIBTROPIC}hal/linux/spi") - Add HAL sources and include directories to the
tropictarget. In the previous step,LT_HAL_SRCSandLT_HAL_INC_DIRSvariables were populated based on the selected HAL, so you can use those:target_sources(tropic PRIVATE ${LT_HAL_SRCS}) target_include_directories(tropic PUBLIC ${LT_HAL_INC_DIRS})
- For the chosen platform (e.g. Linux with HW SPI), add a corresponding HAL using
- And finally, link Libtropic with your binary:
target_link_libraries(my_binary_name PRIVATE tropic)
Inspiration for CMakeLists.txt
The exact CMake calls depend on a configuration of the project into which libtropic is being added. For more inspiration, refer to the Integration Examples section and the CMake Documentation.
Supported Host Platforms and CFPs
Refer to sections Supported Host Platforms and Supported Cryptographic Functionality Providers to see what is supported.
Do You Use a Makefile Instead of CMake?¶
If you use a Makefile instead of CMake, you need to:
- Manually list all
*.cand*.hLibtropic files in your Makefile (you can use the rootCMakeLists.txtfor inspiration). - For each required CMake option
<CMAKE_OPTION>, add the-D<CMAKE_OPTION>flag when building with Make.
Available CMake Options
See How to Configure for available CMake Options. However, some of these options are not directly used in the Libtropic code - based on them, additional internal macros are defined. To see those, either:
- Analyze Libtropic's root
CMakeLists.txt. - Configure Libtropic using CMake and then execute
grep LT_ CMakeCache.txtin yourbuild/directory to see all used options/defines.
Tip: Build Libtropic as a Static Library
You can compile libtropic as a static library (see Compile as a Static Library) using CMake separately and include only the resulting library file in your Makefile. This approach eliminates the need to compile the entire libtropic library and its dependencies in your Makefile. However, you will still need to manually add the HAL files for your platform (libtropic/hal/) and the CAL files for your CFP (libtropic/cal/).