(smollerGraph)
  • Episode 20
  • 15 min

Packaging a C++ library for CMake FetchContent

smollerGraph as a library another CMake project fetches, with its tests, tools and docs kept to its own build, its headers in include/smollergraph, a query in one call, a consumer project built in CI, and a version to pin.

20 November 2026 · Watch on YouTube

smollerGraph becomes a library that another CMake project can fetch with FetchContent, as it would fetch Abseil, and link. A small project of its own shows what that was like at first, with our tests, sg and the docs' theme coming along, 72 tests in its ctest, and a path that pointed into the other project. CMake's PROJECT_IS_TOP_LEVEL keeps the rest to smollerGraph's own build, an ALIAS target gives it a name to link, target_compile_features asks for C++20 at least, the headers move to include/smollergraph, and Execute runs a query in one call. CI builds the consumer on each change, a decision record says why, and the release gets a version tag to pin, v0.20.0. nanoMMO, the MMO from the other series, is going to use it at some point, to check its game content as a graph.

Topics: Tooling and deployment

The code

Each step on screen is a tag in the database's repository. The code at the end of the episode is ep020/end.

  1. 2:01ep020/step-1From the other sideBrowseChanges
  2. 4:47ep020/step-2The library and nothing elseBrowseChanges
  3. 8:37ep020/step-3Headers with our name on themBrowseChanges
  4. 9:34ep020/step-4A query in one callBrowseChanges
  5. 11:26ep020/step-5In CIBrowseChanges
  6. 12:20ep020/step-6A version and a recordBrowseChanges

Transcript

Intro

0:00 Another project fetches smollerGraph here, the way it would fetch Abseil, and builds our library and nothing else of ours. Then it asks our graph a question, in a single call. By the end of today, a CMake project on C++20 or newer can do that, and the first one I have in mind is a game.

0:20 Welcome to episode twenty, and today smollerGraph turns into a library other projects can fetch, starting with nanoMMO, the MMO from our other series.

0:32 The series' website tells the story of smollerGraph so far, and what it's for, if you've just found us. It links the code on GitHub, where you can check out any step of any episode, and each episode's page has its chapters, with the code that changed in each.

Last time

0:48 In episode nineteen, we went looking for attack paths through a company's Active Directory. The company came out of a Python script, as facts, and a pattern in our Cypher learnt to match any of several relationship types, like MemberOf bar AdminTo. Of the two fixes we tried, the obvious one changed nothing, and the second took the accounts with a path to Domain Admins from a hundred and five down to thirty-eight. So far, though, smollerGraph has been used through programs of its own, sg and the explorer page. Today, a program from another project links it, and calls it directly.

Why a library

1:24 I think a database starts to be useful once other code can build on it, and nanoMMO is the first project that wants to. In the other series, nanoMMO's content is full of links, a quest given by an NPC, a monster that drops an item, the Ashlands behind their gate, and so on. I'd like that content checked as a graph, with questions like whether a new player can get each item, in CI, on each change. nanoMMO fetches its libraries with CMake's FetchContent, as we fetch Abseil, so smollerGraph has to work that way too. Before changing anything, let's see what using it from another project is like today.

From the other side

2:01 So we start with a small project of its own, in tests slash consumer, which uses smollerGraph the way nanoMMO would. Its CMake file starts by saying what it is, another project, which fetches smollerGraph and links it like any other library. CI is going to build it on this checkout, with a variable that points FetchContent at the code in hand, in place of the download. It needs CMake three point two eight, like us, it's called consumer, and it's written in C++23, a newer standard than ours, as nanoMMO is. FetchContent declare names smollerGraph, with the address GitHub serves a tag's code from, as a tarball. FetchContent make available downloads it, and adds its CMake file to the build as if it were a folder of this project. After that comes the executable, consumer, which links smollergraph, the library's target, and a test that runs it. Main builds a small graph, asks it a question in Cypher, and checks the answer, with nothing but our library. It includes our facts, graph, parser and plan headers, by their names alone, as the library's include folder is source. The graph is made in temporary files, and two facts go in from a string stream, the red fox eats the rabbit, and the rabbit lives in the meadow. Load facts returns a status, which the if declares and checks in one line, and an error is printed with its message, and main returns one. Then the query, where the fox eats something that lives somewhere, and three calls to answer it. Parse gives a query or an error, plan query takes the query when there's one, or passes the error on, and run takes the plan. We expect the meadow, and anything else is an error, with one as the exit code, so ctest sees it fail. And the git ignore file gets the consumer's build folder, next to ours.

3:52 CMake configures the consumer, with dash D fetch content source dir smollergraph set to this checkout, so FetchContent takes our code from here. The folder FetchContent fills has four downloads, and the library needs one of them, Abseil. GoogleTest, cpp-httplib and the theme of the API docs came along too, for our tests, sg and our docs. The build runs a hundred and seventy-eight steps, and the last one links sg, our program, which the consumer didn't ask for. Ctest runs seventy-two tests, the consumer's one and our seventy-one. And grep finds where sg is built to look for its page files, which is in a folder of the consumer's, called UI, which isn't there. Our CMake file says CMAKE_SOURCE_DIR, and that's the top-level project's folder, the consumer's, when it fetches us.

The library and nothing else

4:47 All of that gets fixed in our CMake file, and most of it with a single variable. CMake three point two one gave us a variable for it, PROJECT_IS_TOP_LEVEL, which is true when smollerGraph is the project being built, and false when another project has added it. So the build type and the standard move inside an if on it, as a project that fetches us picks its own. Our default of a release build stays, for our own builds, with the standard, twenty, and the compile commands file our editor reads. GoogleTest's declare leaves the top, to come back further down with the tests, and cpp-httplib's leaves too, as sg alone needs it. After the library comes an alias, smollergraph colon colon smollergraph, which is the name another project links. A name with two colons in it can't be anything but a target, so a typo in it stops CMake with an error. A plain name it can't find would be taken for a library file on disk, and fail at link time instead, with a less helpful message. Target compile features asks for C++20, publicly, so the library and anything that links it get C++20 at least. A project like the consumer, on twenty-three, keeps twenty-three, and picking the standard stays with the top-level project, as this line says the oldest one our code compiles with. Next comes sg, for any build but the browser one, as a browser runs no server, and what sg needs moves in here with it. Its options for cpp-httplib, a library in a single header, still turn off HTTPS and compression, one each for OpenSSL, zlib, Brotli and Zstandard. The download is the version we had, nought point sixty point one, so nothing about sg changes for us. Its page files' folder comes from PROJECT_SOURCE_DIR, which is smollerGraph's own folder wherever the build starts, in place of CMAKE_SOURCE_DIR. And when we're not the top-level project, sg is excluded from all, so the other project's build leaves it out, and builds it when it asks for sg by name. For another project, the CMake file returns right after sg. In the browser build, each CMAKE_BINARY_DIR and CMAKE_SOURCE_DIR becomes PROJECT_BINARY_DIR and PROJECT_SOURCE_DIR. GoogleTest's declare comes back right before the tests, and the docs' folder uses the project's binary dir too. In the consumer, the target it links is the alias now.

7:11 Configured again from scratch, the consumer downloads two things now, Abseil and cpp-httplib, whose declare is with sg's. The build is a hundred and fifty-five steps, and most of those are Abseil, which a project like nanoMMO already builds for its own code. Ctest runs the consumer's own test, one, and it passes. And asking for sg by name builds it, in three steps, and grep finds the folder it's built to look in for its page files, which is smollerGraph's own folder now.

Headers with our name on them

7:42 Our header files have plain names, like graph and plan, and the source folder is on the include path. So a project like the consumer, with a graph header of its own, could get ours, or the other way round. Most libraries, like Abseil and GoogleTest, keep their headers in a folder named after them, so a project includes them with that name in front. So git mv moves each header from source to include slash smollergraph, and git log can still follow each one's history, as it sees a rename. Sed rewrites each quoted include of one of them, with smollergraph slash in front, in the sources, the headers, the tests, the tools and the consumer. Includes with a slash in them already, like Abseil's, don't match its pattern. Clang format sorts the includes again, now they've changed names. Git counts forty-five files changed, and in graph's source file, the first include has the folder in front now.

8:38 In the CMake file, the library's public include folder is include, in place of source. So a project that links it includes smollergraph slash graph, and our header names can't clash with its own. The library's own sources include their headers that way too, and further down, Doxygen reads the include folder for the API docs. In cpplint's settings, the filter that turned off its check of include paths goes, as each include has a folder in front now, which the check asks for. Root is include, so it names a header guard from the header's path under it, and they stay smollergraph graph h. Clang tidy's header filter is the headers' new folder. And in the Dockerfile, the lint stage copies the include folder, the sources and the rest as they are, and runs clang format and cpplint over them. It used to copy the sources into a folder named after the project, for the header guards.

A query in one call

9:34 Three calls to answer a question is a lot to ask of someone who wants the rows, so the library gets one that does all three. In the plan's header file, string view joins the includes, for the query's text. Execute takes a graph and a query, and parses, plans and runs it, as Kùzu's connection does with its execute. It returns the parser's or the planner's error for a query it can't run, and an error for a query with EXPLAIN in front, as that asks for the plan, which plan query and explain give. In the plan source file, Execute parses the text with Parse, as sg query does, and returns the parser's status when that fails, with its message saying what it expected and where. EXPLAIN gets an error of its own, as running the query would be wrong, and returning a plan would need a different type. Then it plans the query, returns that error too, and runs the plan, whose result goes back in the status or. In the plan's tests, the helper that answers a query as strings goes through Execute. So all the run tests check it as they go, from the food chain to the cheapest path. And a new test, called says why it cannot execute a query, checks its three errors. The parser's comes on a missing bracket, whose message says it expected the bracket at nine and found RETURN there. The planner's comes next, on a variable the pattern doesn't have, and names it, b, and EXPLAIN's says what to call for a plan. Each check compares the whole message, so a change to one of them shows up here. The consumer is what this was for, and its parser's header goes, as the plan's header declares Execute. Its three calls become one, Execute, with the graph and the query's text. When it fails, its status goes to standard error, as before. The check reads the rows through the status or's arrow, as the result is a status or now, holding a result when the query ran.

In CI

11:27 The consumer helps most when CI runs it on each change, so it gets a stage of its own in the Dockerfile, after the tests. It's built on the sources the stage is given, which FetchContent takes in place of the download, so a change that would break smollerGraph for other projects fails here. It starts from the toolchain stage, copies the sources in, and configures the consumer with FetchContent's variable pointing at them. Then it builds it, and runs its test, with the output shown when it fails. In the CI workflow, a new step after the tests builds that stage, called build a project that uses the library.

12:04 Built the way CI builds it, in Docker, the consumer stage configures, compiles Abseil, our library and the consumer, and its test passes. A change that moves a header, or renames the target, would fail this stage, and with it the pull request that made it.

A version and a record

12:21 A project that fetches us should pin a version, not a step of an episode. So the project's version goes from nought point one to nought point twenty, for episode twenty. This release gets a tag of its own, v nought point twenty point oh, which the consumer's download names now. Being fetched by other projects is the kind of choice the decision records are for, so it gets record twenty-four. The context is what the consumer showed, the hundred and seventy-eight steps, our seventy-one tests in its ctest, the downloads, and the path into another project, and that nanoMMO wants the library. The decision lists what we did, step by step, from the top-level check to the version tag. Copying our sources into each project was weighed, which saves a download, but leaves a copy to keep in step by hand. An installed package, for find package, is the usual way for a library that a machine installs once, and it wasn't weighed past that, as the projects in view fetch theirs at a version. A repository for the library alone would be more to keep in step, for a library this small. The costs are the headers' new paths and a tag for each release, which follows the episode for now. And CI gets slower, as it builds Abseil a second time, for the consumer. The index of the records gets a row for it, from episode twenty. The README gets a section called in another project, with the CMake that fetches us at a version and links the library, and a few lines that load facts and execute a query. It also says that we declare Abseil under the name abseil, in lower case, so a project that declares that name first shares its copy.

What's missing

13:59 I think our library is good enough for nanoMMO now, though a few things are still missing. A project that installs its libraries on the machine, for CMake's find package, would want an installed package, with the targets exported, and we haven't written one. Our versions follow the episodes for now, where semantic versioning would say, with its numbers, whether a release can break the code that uses it. A single header that includes the rest would make the first steps simpler. And all of our headers are on the include path, pager and buffer pool among them, where a library would keep the ones it doesn't want used out of sight, so it can change them freely.

Wrap-up

14:36 smollerGraph is a library another project can fetch now, which builds the library and leaves the rest to us, with its headers under its own name and a query in one call. If you're making a library of your own, I'd copy the consumer in CI before anything else. From now on, a change that would break a project using smollerGraph fails our own CI first.

14:58 Have you packaged a C++ library for other people's builds, and which way did you go, FetchContent, an installed package or something else? Tell me in the comments, and subscribe if you'd like to follow smollerGraph as it grows. I hope you find this useful, and thanks for watching.