Non-deterministic file(GLOB_RECURSE) ordering undermines build reproducibility - CMake
0 reputation · 06 Oct 2021, 19:32 UTC
The goal is to make file(GLOB_RECURSE) return source files in the same order on every machine and CMake version so generated build files and test discovery remain reproducible. CMake documentation states that GLOB_RECURSE ordering is unspecified and may change between releases; list(SORT) can impose order but is not applied automatically.
This assumes CMake 3.20 or later. Downstream custom scripts and test discovery can depend on list order, and no built-in cross-toolchain ordering guarantee is documented.
Does CMake offer a built-in option or policy to make file(GLOB_RECURSE) return sorted results by default? If not, is calling list(SORT) immediately after file(GLOB_RECURSE) the supported pattern for stable ordering across file systems? Are there version or platform cases where list(SORT) still cannot produce identical ordering?