Skip to content

Commit b2f21d3

Browse files
committed
chore(cmake): adopt submodule-first deps and standardize FetchContent usage
- Adopt a "submodule-first, FetchContent-fallback" strategy for third-party dependencies: - Default `*_FETCHCONTENT` to OFF in top-level `CMakeLists.txt` and prefer pinned submodules under `3rdparty/`. - Each `cmake/*.cmake` module auto-detects a local submodule and enables FetchContent only as a fallback when the submodule is absent. - Auto-detection is non-authoritative (does not FORCE cache values) so `-D` overrides from users take precedence; a sanity check fails when the user explicitly requests both modes. - Add and pin third-party submodules (commits/tags): - `3rdparty/spdlog` (pinned) - `3rdparty/fmt` (pinned to 8.1.1) - `3rdparty/httplib` (pinned to v0.22.0) - `3rdparty/skywalking-data-collect-protocol` (pinned to v10.3.0) - `3rdparty/grpc` (pinned to v1.74.1) - Standardize FetchContent declarations: - Use `GIT_REPOSITORY` + `GIT_TAG` (via `*_GIT_URL` / `*_GIT_TAG` variables) for FetchContent where appropriate so pins can be tags or commit SHAs. - For gRPC, support archive or git+tag; when using git clone, initialize nested submodules after `FetchContent_Populate`. - Move and tidy dependency logic: - Move auto-detection and conflict validation into each dependency module (`cmake/*.cmake`) to keep `CMakeLists.txt` lean. - Disable `*_FETCHCONTENT` automatically only when the submodule is present and the user has not explicitly requested FetchContent. - CI and docs: - Update CI (`.github/workflows/main.yml`) to checkout submodules recursively and build gRPC from `3rdparty/grpc`. - Update `README.md` documenting submodule-first workflow, FetchContent fallback, `GIT_REPOSITORY+GIT_TAG` usage, and the bump procedure. - Additions and refactors: - Add `cmake/skywalking.cmake` and expose `SKYWALKING_PROTOCOL_PATH` for `proto2cpp`. - Harmonize FetchContent usage and expose `*_GIT_URL` / `*_GIT_TAG` variables for maintainability. Why - Improves reproducibility and developer ergonomics by making dependency pins explicit and consistent. - Allows local `-D` overrides for advanced use-cases or debugging while keeping CI deterministic. Signed-off-by: Matthieu MOREL <[email protected]>
1 parent 9d1bcfa commit b2f21d3

File tree

16 files changed

+352
-48
lines changed

16 files changed

+352
-48
lines changed

.github/workflows/main.yml

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ jobs:
8080
- run: echo "/opt/llvm/bin" >> $GITHUB_PATH
8181

8282
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
83+
with:
84+
submodules: 'recursive'
85+
fetch-depth: 0
8386

8487
- name: Generate dependencies hash
8588
id: deps-hash
@@ -90,19 +93,12 @@ jobs:
9093
echo "hash=$DEPS_HASH" >> $GITHUB_OUTPUT
9194
echo "Dependencies hash (first 8 chars): $DEPS_HASH"
9295
93-
- name: Checkout skywalking-data-collect-protocol
94-
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
95-
with:
96-
repository: apache/skywalking-data-collect-protocol
97-
ref: ${{ env.SKYWALKING_PROTOCOL_REF }}
98-
path: 3rdparty/skywalking-data-collect-protocol
99-
10096
# Cache gRPC build
10197
- name: Cache gRPC build
10298
uses: actions/cache@v3
10399
with:
104100
path: |
105-
grpc/build
101+
3rdparty/grpc/build
106102
/usr/local/lib/libgrpc*
107103
/usr/local/lib/libprotobuf*
108104
/usr/local/lib/cmake/grpc
@@ -115,21 +111,12 @@ jobs:
115111
restore-keys: |
116112
grpc-cmake-${{ runner.os }}-${{ steps.deps-hash.outputs.hash }}-
117113
grpc-cmake-${{ runner.os }}-
118-
119-
- name: Checkout grpc
120-
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
121-
with:
122-
repository: grpc/grpc
123-
ref: ${{ env.GRPC_VERSION }}
124-
path: grpc
125-
submodules: true
126-
127114
- name: Install cmake dependencies and run cmake compile
128115
run: |
129116
sudo apt-get update
130117
sudo apt-get install -y cmake build-essential
131-
sudo cmake -S ./grpc -B ./grpc/build
132-
sudo cmake --build ./grpc/build --parallel 8 --target install
118+
sudo cmake -S ./3rdparty/grpc -B ./3rdparty/grpc/build
119+
sudo cmake --build ./3rdparty/grpc/build --parallel 8 --target install
133120
sudo cmake -S . -B ./build
134121
sudo cmake --build ./build
135122

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ coverage_report
4848

4949
# CMake build directories and dependencies
5050
build/
51-
3rdparty/
5251
grpc/
5352

5453
### Automatically added by Hedron's Bazel Compile Commands Extractor: https://github.com/hedronvision/bazel-compile-commands-extractor

.gitmodules

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[submodule "3rdparty/fmt"]
2+
path = 3rdparty/fmt
3+
url = https://github.com/fmtlib/fmt.git
4+
5+
[submodule "3rdparty/grpc"]
6+
path = 3rdparty/grpc
7+
url = https://github.com/grpc/grpc.git
8+
9+
[submodule "3rdparty/httplib"]
10+
path = 3rdparty/httplib
11+
url = https://github.com/yhirose/cpp-httplib.git
12+
13+
[submodule "3rdparty/skywalking-data-collect-protocol"]
14+
path = 3rdparty/skywalking-data-collect-protocol
15+
url = https://github.com/apache/skywalking-data-collect-protocol.git
16+
17+
[submodule "3rdparty/spdlog"]
18+
path = 3rdparty/spdlog
19+
url = https://github.com/gabime/spdlog.git

3rdparty/fmt

Submodule fmt added at b6f4cea

3rdparty/grpc

Submodule grpc added at 893bdad

3rdparty/httplib

Submodule httplib added at b6c55c6

3rdparty/spdlog

Submodule spdlog added at 76fb40d

CMakeLists.txt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@ project(cpp2sky
1010
HOMEPAGE_URL "https://github.com/SkyAPM/cpp2sky"
1111
)
1212

13-
option(OVERRIDE_CXX_STANDARD_FLAGS "Force building with -std=c++11 even if the CXXFLAGS are configured differently" ON)
14-
option(SPDLOG_FETCHCONTENT "Using spdlog FetchContent to build" ON)
15-
option(FMTLIB_FETCHCONTENT "Using fmt FetchContent to build" ON)
16-
option(HTTPLIB_FETCHCONTENT "Using httplib FetchContent to build" ON)
1713
option(CPP2SKY_INSTALL "Generate the install target." OFF)
14+
option(FMTLIB_FETCHCONTENT "Using fmt FetchContent to build" OFF)
1815
option(GENERATE_CPP2SKY_PKGCONFIG "Generate and install pkg-config files for UNIX" OFF)
19-
16+
option(GRPC_FETCHCONTENT "Using gRPC FetchContent to build" OFF)
17+
option(HTTPLIB_FETCHCONTENT "Using httplib FetchContent to build" OFF)
18+
option(OVERRIDE_CXX_STANDARD_FLAGS "Force building with -std=c++11 even if the CXXFLAGS are configured differently" ON)
19+
option(SKYWALKING_FETCHCONTENT "Using skywalking-data-collect-protocol FetchContent to build" OFF)
20+
option(SPDLOG_FETCHCONTENT "Using spdlog FetchContent to build" OFF)
2021

2122
if(OVERRIDE_CXX_STANDARD_FLAGS)
2223
set(CMAKE_CXX_STANDARD 17)
@@ -46,6 +47,7 @@ include(fmtlib)
4647
include(spdlog)
4748
include(httplib)
4849
include(grpc)
50+
include(skywalking)
4951
include(proto2cpp)
5052

5153
target_link_libraries(${PROJECT_NAME}

README.md

Lines changed: 127 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,136 @@ cc_binary(
3333

3434
#### Cmake
3535

36-
You can compile this project, according to the following steps:
36+
You can compile this project in one of three supported ways (submodule-first is recommended):
37+
38+
- Recommended — Submodule-first (most reproducible):
39+
40+
```bash
41+
git clone --recurse-submodules [email protected]:SkyAPM/cpp2sky.git
42+
git submodule update --init --recursive
3743
```
38-
step 01: git clone [email protected]:SkyAPM/cpp2sky.git
39-
step 02: git clone -b v9.1.0 https://github.com/apache/skywalking-data-collect-protocol.git ./3rdparty/skywalking-data-collect-protocol
40-
step 03: git clone -b v1.46.6 https://github.com/grpc/grpc.git --recursive
41-
step 04: cmake -S ./grpc -B ./grpc/build && cmake --build ./grpc/build --parallel 8 --target install
42-
step 05: cmake -S . -B ./build && cmake --build ./build
44+
45+
This repository pins several third-party dependencies under `3rdparty/` (example: `spdlog`, `fmt`, `httplib`, `skywalking-data-collect-protocol`). The top-level CMake will auto-detect these submodules and use them via `add_subdirectory()`.
46+
47+
- FetchContent fallback (automatic clone at configure time):
48+
49+
If a submodule is not present, CMake can automatically download the dependency at configure time using FetchContent. This is controlled by CMake options of the form `-D<LIB>_FETCHCONTENT=ON`.
50+
51+
Important: this project now defaults `*_FETCHCONTENT` to `OFF` to favour a submodule-first workflow (reproducible builds). Each dependency module will auto-detect a local `3rdparty/<lib>` submodule and, unless you explicitly set the corresponding `-D` option, enable the submodule and only enable FetchContent as a fallback when the submodule is absent.
52+
53+
FetchContent is declared uniformly using `GIT_REPOSITORY` + `GIT_TAG` where possible so the build can be pinned to a tag or an exact commit SHA. We strongly encourage this pattern because it makes bumps and temporary testing against a branch/commit straightforward.
54+
55+
Recommended FetchContent pattern (preferred)
56+
57+
```cmake
58+
# variables make bumps easy and visible in the cmake file
59+
set(FMTLIB_GIT_URL https://github.com/fmtlib/fmt.git)
60+
set(FMTLIB_GIT_TAG 8.1.1) # or a commit SHA like `d6a5b8f...`
61+
62+
FetchContent_Declare(
63+
fmtlib
64+
GIT_REPOSITORY ${FMTLIB_GIT_URL}
65+
GIT_TAG ${FMTLIB_GIT_TAG}
66+
)
67+
FetchContent_MakeAvailable(fmtlib)
4368
```
4469

70+
Why this is useful
71+
- `GIT_TAG` accepts tags, branches or a full commit SHA — use a SHA to pin an exact commit that isn't tagged.
72+
- Using named variables (`*_GIT_URL`, `*_GIT_TAG`) makes automated bump scripts and review diffs clearer.
73+
74+
Notes on projects with nested submodules
75+
- Some repositories (notably gRPC) include their own git submodules. For those projects we recommend either:
76+
- Use the release archive (`URL` + `URL_HASH`) which avoids nested submodule handling, or
77+
- Use `GIT_REPOSITORY` + `GIT_TAG` but initialize nested submodules after `FetchContent_Populate`:
78+
79+
```cmake
80+
FetchContent_GetProperties(grpc)
81+
if(NOT grpc_POPULATED)
82+
FetchContent_Populate(grpc)
83+
execute_process(COMMAND ${GIT_EXECUTABLE} submodule update --init --recursive
84+
WORKING_DIRECTORY ${grpc_SOURCE_DIR})
85+
add_subdirectory(${grpc_SOURCE_DIR} ${grpc_BINARY_DIR})
86+
endif()
87+
```
88+
89+
In this repository we prefer the archive approach for gRPC in CI for simplicity, but the git+tag approach is supported and useful for local testing or when you need to pin to a commit SHA.
90+
91+
If you need to force FetchContent for any dependency (for example, to debug or when you prefer not to initialize submodules), you can pass `-D<LIB>_FETCHCONTENT=ON` on the `cmake` command line. If you pass conflicting options (both `-D<LIB>_AS_SUBMODULE=ON` and `-D<LIB>_FETCHCONTENT=ON`) CMake will stop with a helpful error and you must choose one.
92+
93+
For SkyWalking you can enable FetchContent with:
94+
95+
```bash
96+
cmake -DSKYWALKING_FETCHCONTENT=ON -S . -B build
97+
cmake --build build
98+
```
99+
100+
- Explicit path (developer workflow):
101+
102+
If you already have a local checkout of `skywalking-data-collect-protocol`, point CMake to it:
103+
104+
```bash
105+
cmake -DSKYWALKING_PROTOCOL_PATH=/path/to/skywalking-data-collect-protocol -S . -B build
106+
cmake --build build
107+
```
108+
109+
Notes about dependencies with special handling
110+
111+
Most third-party dependencies follow the same pattern: prefer the pinned submodule under `3rdparty/` (submodule-first) and fall back to `FetchContent` at configure time when the submodule is not present. See the top-level `CMakeLists.txt` and the modules in `cmake/` for details.
112+
113+
There are two cases worth calling out because their consumption/build steps differ slightly:
114+
115+
- gRPC
116+
117+
gRPC is not header-only and typically needs configuration and a build step (or its targets must be available via `find_package`). This repository includes a pinned copy of gRPC at `3rdparty/grpc` (tag `v1.74.1`) so builds are reproducible. CI is configured to build gRPC from that submodule.
118+
119+
To build gRPC locally from the submodule and install it for the rest of the project:
120+
121+
```bash
122+
# initialize submodules (if you haven't already)
123+
git submodule update --init --recursive
124+
125+
# install build deps
126+
sudo apt-get update
127+
sudo apt-get install -y cmake build-essential
128+
129+
# configure & install gRPC from the submodule
130+
cmake -S 3rdparty/grpc -B 3rdparty/grpc/build
131+
cmake --build 3rdparty/grpc/build --parallel 8 --target install
132+
133+
# configure & build cpp2sky
134+
cmake -S . -B build
135+
cmake --build build --parallel $(nproc)
136+
```
137+
138+
If you prefer not to use the submodule you can still clone and build gRPC separately and make its CMake targets available to the project, but using the pinned submodule is recommended for reproducibility.
139+
140+
- skywalking-data-collect-protocol (protobufs)
141+
142+
The SkyWalking protocol repository contains the protobuf definitions used to generate code for the project. The `cmake/skywalking.cmake` module sets `SKYWALKING_PROTOCOL_PATH` when the `3rdparty/skywalking-data-collect-protocol` submodule is present so the proto generation step can locate the `.proto` files.
143+
144+
You can override that behavior by supplying an explicit path:
145+
146+
```bash
147+
cmake -DSKYWALKING_PROTOCOL_PATH=/path/to/skywalking-data-collect-protocol -S . -B build
148+
cmake --build build
149+
```
150+
151+
Alternatively, enable FetchContent for SkyWalking with `-DSKYWALKING_FETCHCONTENT=ON` to let CMake fetch the proto repo at configure time.
152+
153+
How to bump a submodule (example for skywalking-data-collect-protocol):
154+
```bash
155+
cd 3rdparty/skywalking-data-collect-protocol
156+
git fetch --tags
157+
git checkout tags/v10.4.0 # or a specific commit
158+
cd ../..
159+
git add 3rdparty/skywalking-data-collect-protocol
160+
git commit -m "Pin skywalking-data-collect-protocol to v10.4.0"
161+
git push
162+
```
163+
164+
If you prefer CI to always fetch the latest submodules, ensure the workflow initializes submodules (this repo's CI uses `actions/checkout` with `submodules: 'recursive'`).
165+
45166
You can also use find_package to get target libary in your project. Like this:
46167
```
47168
find_package(cpp2sky CONFIG REQUIRED)

0 commit comments

Comments
 (0)