Skip to content

Instantly share code, notes, and snippets.

@Trass3r
Last active October 6, 2022 17:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Trass3r/8aefa4ae8526856fb7c65525b5e50b0c to your computer and use it in GitHub Desktop.
Save Trass3r/8aefa4ae8526856fb7c65525b5e50b0c to your computer and use it in GitHub Desktop.
get statistics on preprocessed C++ code size
set -eux
cd /build/
ninja -k0
export CCACHE_DISABLE=1
# fix CMake deps: https://gitlab.kitware.com/cmake/cmake/-/issues/15555
sed -ri 's/(build .+?\.cpp\.o: .+?) \|\| \S+depends\S+/\1/g' build.ninja
# produce preprocessed sources without #line markers
# -fdirectives-only only available in clang 15+
sed -i 's/\$out -c \$in/$out.i -E -P $in/' CMakeFiles/rules.ninja
# ninja -t commands xxx.cpp.o
# ninja -v xxx.cpp.o
# build all object files
ninja $(ninja -t targets | cut -d: -f1 | grep -E '.cpp.o$' | tr '\n' ' ')
# count per-file lines | tokens | bytes
find -name '*.i' | xargs wc -mwl
# size of preprocessed code
find -name '*.i' -exec du -ch {} +
# count sum of lines | tokens | bytes
find -name '*.i' -exec cat {} + | wc -mwl
# non-empty lines
find -name '*.i' -exec cat {} + | grep -cv ^$
find -name '*.i' -exec rm {} \;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment