meson: add support for 'hdr-check'

The Makefile supports a target called 'hdr-check', which checks if individual header files can be independently compiled. Let's port this functionality to Meson, our new build system too. The implementation resembles that of the Makefile and provides the same check. Since meson builds are out-of-tree, header dependencies are not automatically met. So unlike the Makefile version, we also need to add the required dependencies. Also add the 'xdiff/' dir to the list of 'third_party_sources' as those headers must be skipped from the checks too. This also skips the folder from the 'coccinelle' checks, this is okay, since this code is an external dependency. Signed-off-by: Karthik Nayak <karthik.188@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Karthik Nayak committed Apr 23, 2025 at 10:15 UTC 02a132616ac69cd546c823c47afd60eab738792c
1 file changed +63
meson.build
+63
@@ -645,6 +645,7 @@ third_party_excludes = [
645 ':!sha1dc',
646 ':!t/unit-tests/clar',
647 ':!t/t[0-9][0-9][0-9][0-9]*',
648 + ':!xdiff',
649 ]
650
651 headers_to_check = []
@@ -1994,6 +1995,68 @@ endif
1995
1996 subdir('contrib')
1997
1998 +exclude_from_check_headers = [
1999 + 'compat/',
2000 + 'unicode-width.h',
2001 +]
2002 +
2003 +if sha1_backend != 'openssl'
2004 + exclude_from_check_headers += 'sha1/openssl.h'
2005 +endif
2006 +if sha256_backend != 'openssl'
2007 + exclude_from_check_headers += 'sha256/openssl.h'
2008 +endif
2009 +if sha256_backend != 'nettle'
2010 + exclude_from_check_headers += 'sha256/nettle.h'
2011 +endif
2012 +if sha256_backend != 'gcrypt'
2013 + exclude_from_check_headers += 'sha256/gcrypt.h'
2014 +endif
2015 +
2016 +if headers_to_check.length() != 0 and compiler.get_argument_syntax() == 'gcc'
2017 + hco_targets = []
2018 + foreach h : headers_to_check
2019 + skip_header = false
2020 + foreach exclude : exclude_from_check_headers
2021 + if h.startswith(exclude)
2022 + skip_header = true
2023 + break
2024 + endif
2025 + endforeach
2026 +
2027 + if skip_header
2028 + continue
2029 + endif
2030 +
2031 + hcc = custom_target(
2032 + input: h,
2033 + output: h.underscorify() + 'cc',
2034 + command: [
2035 + shell,
2036 + '-c',
2037 + 'echo \'#include "git-compat-util.h"\' > @OUTPUT@ && echo \'#include "' + h + '"\' >> @OUTPUT@'
2038 + ]
2039 + )
2040 +
2041 + hco = custom_target(
2042 + input: hcc,
2043 + output: fs.replace_suffix(h.underscorify(), '.hco'),
2044 + command: [
2045 + compiler.cmd_array(),
2046 + libgit_c_args,
2047 + '-I', meson.project_source_root(),
2048 + '-I', meson.project_source_root() / 't/unit-tests',
2049 + '-o', '/dev/null',
2050 + '-c', '-xc',
2051 + '@INPUT@'
2052 + ]
2053 + )
2054 + hco_targets += hco
2055 + endforeach
2056 +
2057 + alias_target('hdr-check', hco_targets)
2058 +endif
2059 +
2060 foreach key, value : {
2061 'DIFF': diff.full_path(),
2062 'GIT_SOURCE_DIR': meson.project_source_root(),