#1461 Invalid version detected with CMake FetchContent from URL (#1465)

* Bold project documentation link in README

* #1461 Invalid version detected when used with CMake FetchContent from URL
* Reject a .git dir that belongs to a parent project, e.g. when fetched as a URL tarball with no .git of its own
* Update documentation on including ETL in another project with CMake's FetchContent

---------

Co-authored-by: John Wellbelove <jwellbelove@users.noreply.github.com>
Co-authored-by: tzijnge <someone@somewhere.com>
This commit is contained in:
Timon Zijnge 2026-06-18 12:29:38 +02:00 committed by GitHub
parent c6d17b3a8d
commit 6df8842a21
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 36 additions and 4 deletions

View File

@ -165,12 +165,12 @@ add_executable(foo main.cpp)
target_link_libraries(foo PRIVATE etl::etl) target_link_libraries(foo PRIVATE etl::etl)
``` ```
Alternatively you can use [FetchContent](https://cmake.org/cmake/help/latest/module/FetchContent.html).
Alternatively you can use [FetchContent](https://cmake.org/cmake/help/latest/module/FetchContent.html), By Git, replacing `<targetVersion>` with the version to install based on a git tag:
replacing `<targetVersion>` with the version to install based on a git tag:
```sh ```cmake
Include(FetchContent) include(FetchContent)
FetchContent_Declare( FetchContent_Declare(
etl etl
@ -184,6 +184,25 @@ add_executable(foo main.cpp)
target_link_libraries(foo PRIVATE etl::etl) target_link_libraries(foo PRIVATE etl::etl)
``` ```
Or by URL, fetching a release tarball directly, again replacing `<targetVersion>` with the desired version:
```cmake
include(FetchContent)
FetchContent_Declare(
etl
URL https://github.com/ETLCPP/etl/archive/refs/tags/<targetVersion>.tar.gz
)
FetchContent_MakeAvailable(etl)
add_executable(foo main.cpp)
target_link_libraries(foo PRIVATE etl::etl)
```
A tarball fetched this way has no `.git` directory of its own. ETL detects this and falls back to
reading its version from `version.txt` instead of `git describe`, so no extra configuration is needed.
## Profile definition ## Profile definition
When using ETL in a project, there is typically an `etl_profile.h` defined to When using ETL in a project, there is typically an `etl_profile.h` defined to

View File

@ -82,6 +82,19 @@ function(etl_get_git_head_revision _refspecvar _hashvar)
set(GIT_DIR "") set(GIT_DIR "")
endif() endif()
endif() endif()
if(NOT "${GIT_DIR}" STREQUAL "")
# Reject a .git dir that belongs to a parent project (e.g. when fetched
# as a URL tarball with no .git of its own and _etl_git_find_closest_git_dir
# walked up into the consuming project's repository). Gated by the same
# flag as above, since CMAKE_CURRENT_SOURCE_DIR is always at or below
# CMAKE_SOURCE_DIR, so this check is strictly tighter than the one above
# and must honor the same opt-out.
file(RELATIVE_PATH _relative_etl_to_git "${CMAKE_CURRENT_SOURCE_DIR}"
"${GIT_DIR}")
if("${_relative_etl_to_git}" MATCHES "^[.][.]" AND NOT ALLOW_LOOKING_ABOVE_CMAKE_SOURCE_DIR)
set(GIT_DIR "")
endif()
endif()
if("${GIT_DIR}" STREQUAL "") if("${GIT_DIR}" STREQUAL "")
set(${_refspecvar} set(${_refspecvar}
"GITDIR-NOTFOUND" "GITDIR-NOTFOUND"