meson: simplify and parameterize various standard function checks

This is repetitive logic. We either want to use some -lc function, or if it is not available we define it as -DNO_XXX and usually (but not always) provide some custom compatibility impl instead. Checking the intent of each block when reading through the file is slow and not very DRY. Switch to taking an array of checkable functions instead. Not all functions are straightforward to move, since different macro prefixes are used. Signed-off-by: Eli Schwartz <eschwartz@gentoo.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Eli Schwartz committed Apr 25, 2025 at 01:25 UTC 61fb2262e71a044198b8b18872a802036c332d80
1 file changed +29 -56
meson.build
+29 -56
@@ -1133,11 +1133,6 @@ else
1133 build_options_config.set('NO_UNIX_SOCKETS', '1')
1134 endif
1135
1136 -if not compiler.has_function('pread')
1137 - libgit_c_args += '-DNO_PREAD'
1138 - libgit_sources += 'compat/pread.c'
1139 -endif
1140 -
1136 if host_machine.system() == 'darwin'
1137 libgit_sources += 'compat/precompose_utf8.c'
1138 libgit_c_args += '-DPRECOMPOSE_UNICODE'
@@ -1290,23 +1285,39 @@ if not compiler.has_member('struct passwd', 'pw_gecos', prefix: '#include <pwd.h
1285 libgit_c_args += '-DNO_GECOS_IN_PWENT'
1286 endif
1287
1293 -if compiler.has_function('sync_file_range')
1294 - libgit_c_args += '-DHAVE_SYNC_FILE_RANGE'
1295 -endif
1288 +checkfuncs = {
1289 + 'strcasestr' : ['strcasestr.c'],
1290 + 'memmem' : ['memmem.c'],
1291 + 'strlcpy' : ['strlcpy.c'],
1292 + 'strtoull' : [],
1293 + 'setenv' : ['setenv.c'],
1294 + 'mkdtemp' : ['mkdtemp.c'],
1295 + 'initgroups' : [],
1296 + 'strtoumax' : ['strtoumax.c', 'strtoimax.c'],
1297 + 'pread' : ['pread.c'],
1298 +}
1299
1297 -if not compiler.has_function('strcasestr')
1298 - libgit_c_args += '-DNO_STRCASESTR'
1299 - libgit_sources += 'compat/strcasestr.c'
1300 +if host_machine.system() == 'windows'
1301 + libgit_c_args += '-DUSE_WIN32_MMAP'
1302 +else
1303 + checkfuncs += {
1304 + 'mmap' : ['mmap.c'],
1305 + # provided by compat/mingw.c.
1306 + 'unsetenv' : ['unsetenv.c'],
1307 + }
1308 endif
1309
1302 -if not compiler.has_function('memmem')
1303 - libgit_c_args += '-DNO_MEMMEM'
1304 - libgit_sources += 'compat/memmem.c'
1305 -endif
1310 +foreach func, impls : checkfuncs
1311 + if not compiler.has_function(func)
1312 + libgit_c_args += '-DNO_' + func.to_upper()
1313 + foreach impl : impls
1314 + libgit_sources += 'compat/' + impl
1315 + endforeach
1316 + endif
1317 +endforeach
1318
1307 -if not compiler.has_function('strlcpy')
1308 - libgit_c_args += '-DNO_STRLCPY'
1309 - libgit_sources += 'compat/strlcpy.c'
1319 +if compiler.has_function('sync_file_range')
1320 + libgit_c_args += '-DHAVE_SYNC_FILE_RANGE'
1321 endif
1322
1323 if not compiler.has_function('strdup')
@@ -1314,53 +1325,15 @@ if not compiler.has_function('strdup')
1325 libgit_sources += 'compat/strdup.c'
1326 endif
1327
1317 -if not compiler.has_function('strtoumax')
1318 - libgit_c_args += '-DNO_STRTOUMAX'
1319 - libgit_sources += [
1320 - 'compat/strtoumax.c',
1321 - 'compat/strtoimax.c',
1322 - ]
1323 -endif
1324 -
1325 -if not compiler.has_function('strtoull')
1326 - libgit_c_args += '-DNO_STRTOULL'
1327 -endif
1328 -
1329 -if not compiler.has_function('setenv')
1330 - libgit_c_args += '-DNO_SETENV'
1331 - libgit_sources += 'compat/setenv.c'
1332 -endif
1333 -
1328 if not compiler.has_function('qsort')
1329 libgit_c_args += '-DINTERNAL_QSORT'
1330 endif
1331 libgit_sources += 'compat/qsort_s.c'
1332
1339 -# unsetenv is provided by compat/mingw.c.
1340 -if host_machine.system() != 'windows' and not compiler.has_function('unsetenv')
1341 - libgit_c_args += '-DNO_UNSETENV'
1342 - libgit_sources += 'compat/unsetenv.c'
1343 -endif
1344 -
1345 -if not compiler.has_function('mkdtemp')
1346 - libgit_c_args += '-DNO_MKDTEMP'
1347 - libgit_sources += 'compat/mkdtemp.c'
1348 -endif
1349 -
1350 -if not compiler.has_function('initgroups')
1351 - libgit_c_args += '-DNO_INITGROUPS'
1352 -endif
1353 -
1333 if compiler.has_function('getdelim')
1334 libgit_c_args += '-DHAVE_GETDELIM'
1335 endif
1336
1358 -if host_machine.system() == 'windows'
1359 - libgit_c_args += '-DUSE_WIN32_MMAP'
1360 -elif not compiler.has_function('mmap')
1361 - libgit_c_args += '-DNO_MMAP'
1362 - libgit_sources += 'compat/mmap.c'
1363 -endif
1337
1338 if compiler.has_function('clock_gettime')
1339 libgit_c_args += '-DHAVE_CLOCK_GETTIME'