← All tasks
cppyhirose/cpp-httplib #2263Not a task: already works

Defining `BUILD_SHARED_LIBS` option may break superproject build when using `add_subdirectory`

envgap__yhirose__cpp-httplib-2263

01 / FAILURE SIGNATURE

As reported upstream

No identifying execution failure has been captured.
Not a benchmark task.
  • The project already builds and runs before the fix, so there is nothing to repair.

02 / ENVIRONMENT RECIPE

Base commit
4b2b851dbb91f9d6c2299976b5d03f7c5c1a312d
Manifest
CMakeLists.txt
Reproduce
Awaiting issue-specific recipe
Run under trace
Awaiting a meaningful runtime command

03 / ORIGINAL ISSUE TEXT

yhirose/cpp-httplib #2263 · read the original issue
I was surprised to discover that adding `add_subdirectory(cpp-httplib)` to our project `CMakeLists.txt` has broken the build because some libraries that were supposed to be built as shared have suddenly started being built as static. The reason for this is that this library defines `BUILD_SHARED_LIBS` as `OFF` and, as CMake [documentation says](https://cmake.org/cmake/help/latest/variable/BUILD_SHARED_LIBS.html):

> Note that if bringing external dependencies directly into the build, such as with [FetchContent](https://cmake.org/cmake/help/latest/module/FetchContent.html#module:FetchContent) or a direct call to [add_subdirectory()](https://cmake.org/cmake/help/latest/command/add_subdirectory.html#command:add_subdirectory), and one of those dependencies has such a call to [option(BUILD_SHARED_LIBS ...)](https://cmake.org/cmake/help/latest/command/option.html#command:option), the top level project must also call [option(BUILD_SHARED_LIBS ...)](https://cmake.org/cmake/help/latest/command/option.html#command:option) before bringing in its dependencies.

and our project doesn't define this option. We probably could define it, but I think it would be better if cpp-httplib didn't define it instead, especially when `HTTPLIB_COMPILE` is `OFF`, i.e. when it's not even used by this library itself. I believe the best approach would be to use this option as default value for some new `HTTPLIB_SHARED` option and only use this option to decide which kind of library to build. This would have the following desirable consequences:

1. `BUILD_SHARED_LIBS` will still be respected if it's defined (by the superproject or directly on the command line when configuring).
2. It will become possible to specify whether static or shared cpp-httplib library should be built by setting `HTTPLIB_SHARED` without affecting anything else.
3. Adding this project to the existing build won't break anything else any more.

Please let me know if you'd like me to submit a PR implementing this proposal.
Continue on GitHub ↗

04 / LABELS

Labels from the report text only; not yet run

No supported category has been assigned.

Label rules and the text that matched
[]