hrydgard / minitrace

Simple C/C++ library for producing JSON traces suitable for Chrome's built-in trace viewer (about:tracing).
MIT License
370 stars 63 forks source link

Make minitrace installable via CMake #34

Closed JuliusBrueggemann closed 1 year ago

JuliusBrueggemann commented 1 year ago

Changes

The changes in this PR enable a user to install the library via cmake --install. Along with the header and the static library, it also installs CMake export files which allows users to use the library with CMake's find_package().

The necessary CMake code is pretty much taken straight from the CMake guide at https://cmake.org/cmake/help/latest/guide/tutorial/Adding%20Export%20Configuration.html.

I chose the installation prefixes (mostly just the prefix for the CMake exports, which is share/minitrace) to conform to the layout that vcpkg uses.

How to test

First, build and install minitrace into a directory of your choosing:

$ cd minitrace
$ cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug --install-prefix=<dir>
$ cmake --build build/
$ cmake --install build/

This will install minitrace to <dir>. Afterwards, you can test the exported CMake targets by creating a new CMake project which imports minitrace:

# CMakeLists.txt
cmake_minimum_required(VERSION 3.20)
project(Test)

find_package(minitrace CONFIG REQUIRED)

add_executable(Test main.cpp)
target_link_libraries(TestPUBLIC minitrace::minitrace)

When configuring the project, you need to tell CMake where to find the exported CMake files by setting the CMAKE_PREFIX_PATH like so:

$ cd test-project
$ cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug -DCMAKE_PREFIX_PATH=<path to installed minitrace>/share/minitrace

Background

I plan on also opening a PR at the vcpkg repo to update the minitrace port to the latest version. Currently, vcpkg uses an old version from 2019 and supplies its own CMakeLists.txt to build and install minitrace. However, they don't define MTR_ENABLED when building, resulting in a functionally useless binary. While I investigated that issue, I noticed that minitrace has since added its own CMakeLists.txt, so it seemed like a good idea to use that one and remove the custom one in vcpkg. However, vcpkg installs libraries with cmake --install, so without the changes in this PR, vcpkg would have to keep (and maintain) their own CMakeLists.txt.

hrydgard commented 1 year ago

Looks good to me.