Skip to content

Commit

Permalink
Add option to download dependencies with CMake
Browse files Browse the repository at this point in the history
This is another step in reaching feature parity between CMake and meson.
When using the `download-deps` preset, or when setting
`CMAKE_PROJECT_TOP_LEVEL_INCLUDES` to `subprojects/provider.cmake`, the
dependencies will be downloaded and built from source.
  • Loading branch information
lunacd committed Jun 27, 2024
1 parent d6120eb commit 2212c02
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ insert_final_newline = true
[meson.build]
indent_size = 2

[CMakeLists.txt]
[{CMakeLists.txt,*.cmake}]
indent_size = 2

[*.{toml,yml,json}]
Expand Down
7 changes: 2 additions & 5 deletions .github/workflows/cmake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,8 @@ jobs:
strategy:
matrix:
cfg:
- {
id: ubuntu-gcc,
platform: ubuntu,
preset: default,
}
- { id: ubuntu-gcc, platform: ubuntu, preset: default }
- { id: ubuntu-gcc, platform: ubuntu, preset: download-deps }

steps:
- uses: actions/checkout@v2
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
subprojects/*
!subprojects/*.wrap
!subprojects/*.cmake
.pixi/
.cache/
7 changes: 7 additions & 0 deletions CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@
"CC": "gcc",
"CXX": "g++"
}
},
{
"name": "download-deps",
"inherits": "default",
"cacheVariables": {
"CMAKE_PROJECT_TOP_LEVEL_INCLUDES": "subprojects/provider.cmake"
}
}
]
}
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ find_package(fmt 8 REQUIRED)
target_link_libraries(cps PRIVATE fmt::fmt)

find_package(jsoncpp 1.9 REQUIRED)
target_link_libraries(cps PRIVATE JsonCpp::JsonCpp)
target_link_libraries(cps PRIVATE jsoncpp_lib)

find_package(CLI11 2.1 REQUIRED)
target_link_libraries(cps-config PRIVATE CLI11::CLI11)
41 changes: 41 additions & 0 deletions subprojects/provider.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
cmake_minimum_required(VERSION 3.24)

include(FetchContent)
FetchContent_Declare(
GTest
URL https://github.com/google/googletest/archive/refs/tags/v1.14.0.tar.gz
URL_HASH SHA256=8ad598c73ad796e0d8280b082cebd82a630d73e73cd3c70057938a6501bba5d7
)
FetchContent_Declare(
tl-expected
URL https://github.com/TartanLlama/expected/archive/v1.1.0.tar.gz
URL_HASH SHA256=1db357f46dd2b24447156aaf970c4c40a793ef12a8a9c2ad9e096d9801368df6
)
FetchContent_Declare(
jsoncpp
URL https://github.com/open-source-parsers/jsoncpp/archive/1.9.5.tar.gz
URL_HASH SHA256=f409856e5920c18d0c2fb85276e24ee607d2a09b5e7d5f0a371368903c275da2
)
FetchContent_Declare(
fmt
URL https://github.com/fmtlib/fmt/archive/10.1.1.tar.gz
URL_HASH SHA256=78b8c0a72b1c35e4443a7e308df52498252d1cefc2b08c9a97bc9ee6cfe61f8b
)
FetchContent_Declare(
CLI11
URL https://github.com/CLIUtils/CLI11/archive/refs/tags/v2.1.2.tar.gz
URL_HASH SHA256=26291377e892ba0e5b4972cdfd4a2ab3bf53af8dac1f4ea8fe0d1376b625c8cb
)

macro(provide_dependency method dep_name)
if ("${dep_name}" MATCHES "^(GTest|tl-expected|jsoncpp|fmt|CLI11)$")
FetchContent_MakeAvailable(${dep_name})
set(${dep_name}_FOUND TRUE)
endif()
endmacro()

cmake_language(
SET_DEPENDENCY_PROVIDER provide_dependency
SUPPORTED_METHODS
FIND_PACKAGE
)

0 comments on commit 2212c02

Please sign in to comment.