Skip to content

Commit

Permalink
[configure] Detect HAVE_GLOB_PERIOD, and use it
Browse files Browse the repository at this point in the history
Overriding GLOB_PERIOD as 0 isn't quite right.  It doesn't work in both
Python and C++.
  • Loading branch information
Andy C committed Jan 6, 2025
1 parent 00652b5 commit 950120a
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 14 deletions.
30 changes: 22 additions & 8 deletions configure
Original file line number Diff line number Diff line change
Expand Up @@ -309,9 +309,11 @@ echo_shell_vars() {
echo 'HAVE_READLINE='
echo 'READLINE_DIR='
fi
echo

echo "PREFIX=$FLAG_prefix"
echo "DATAROOTDIR=$FLAG_datarootdir"
echo

if cc_quiet build/detect-cc.c -Wl,--gc-sections; then
echo 'STRIP_FLAGS=--gc-sections'
Expand Down Expand Up @@ -368,6 +370,20 @@ check_sizeof() {
}

detect_c_language() {
# Exported by pyext/libc.c
if test "$have_fnm_extmatch" = 1; then
echo '#define HAVE_FNM_EXTMATCH 1'
else
echo '#define HAVE_FNM_EXTMATCH 0'
fi

if test "$have_glob_period" = 1; then
echo '#define HAVE_GLOB_PERIOD 1'
else
echo '#define HAVE_GLOB_PERIOD 0'
fi
echo

# This is the equivalent of AC_CHECK_SIZEOF(int, 4)
check_sizeof SIZEOF_INT 'int' 4
check_sizeof SIZEOF_LONG 'long' 4
Expand Down Expand Up @@ -465,20 +481,18 @@ echo_cpp() {
echo '/* #undef HAVE_SYSTEMTAP_SDT */'
fi

echo

if test "$have_fnm_extmatch" = 1; then
echo '/* libc defines FNM_EXTMATCH */'
echo '#define HAVE_FNM_EXTMATCH 1'
else
# INVALID value that we can detect in code
# e.g. musl libc does not support this, but glibc does
echo '#define FNM_EXTMATCH 0'
echo '#define HAVE_FNM_EXTMATCH 0'
fi

if test "$have_glob_period" = 1; then
echo '/* libc defines GLOB_PERIOD */'
echo '#define HAVE_GLOB_PERIOD 1'
else
# INVALID value that we can detect in code
# e.g. Android does not support this, but glibc and musl libc do
echo '#define GLOB_PERIOD 0'
echo '#define HAVE_GLOB_PERIOD 0'
fi

# Used by cpp/core.cc
Expand Down
4 changes: 2 additions & 2 deletions core/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
NewDict)
from pylib import os_path

from libc import GLOB_PERIOD
from libc import HAVE_GLOB_PERIOD
import posix_ as posix

from typing import Tuple, List, Dict, Optional, Any, cast, TYPE_CHECKING
Expand Down Expand Up @@ -347,7 +347,7 @@ def _SetOptionNum(opt_name):

def _MaybeWarnDotglob():
# type: () -> None
if GLOB_PERIOD == 0: # invalid value that means it wasn't detected
if HAVE_GLOB_PERIOD == 0:
# GNU libc and musl libc have GLOB_PERIOD, but Android doesn't
print_stderr(
"osh warning: GLOB_PERIOD wasn't found in libc, so 'shopt -s dotglob' won't work")
Expand Down
4 changes: 2 additions & 2 deletions osh/glob_.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from mycpp import mylib
from mycpp.mylib import log, print_stderr

from libc import GLOB_PERIOD
from libc import GLOB_PERIOD, HAVE_GLOB_PERIOD

from typing import List, Tuple, cast, TYPE_CHECKING
if TYPE_CHECKING:
Expand Down Expand Up @@ -416,7 +416,7 @@ def _Glob(self, arg, out):
# type: (str, List[str]) -> int
try:
flags = 0
if self.exec_opts.dotglob():
if self.exec_opts.dotglob() and HAVE_GLOB_PERIOD:
flags |= GLOB_PERIOD
results = libc.glob(arg, flags)
except RuntimeError as e:
Expand Down
8 changes: 7 additions & 1 deletion pyext/libc.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

#include <Python.h>

#include "_build/detected-config.h"

// Log messages to stderr.
static void debug(const char* fmt, ...) {
#ifdef LIBC_VERBOSE
Expand Down Expand Up @@ -410,8 +412,12 @@ void initlibc(void) {

module = Py_InitModule("libc", methods);
if (module != NULL) {
// ./configure values
PyModule_AddIntConstant(module, "HAVE_GLOB_PERIOD", HAVE_GLOB_PERIOD);
PyModule_AddIntConstant(module, "HAVE_FNM_EXTMATCH", HAVE_FNM_EXTMATCH);

// Actual libc values
PyModule_AddIntConstant(module, "GLOB_PERIOD", GLOB_PERIOD);
PyModule_AddIntConstant(module, "FNM_EXTMATCH", FNM_EXTMATCH);
PyModule_AddIntConstant(module, "FNM_CASEFOLD", FNM_CASEFOLD);
PyModule_AddIntConstant(module, "REG_ICASE", REG_ICASE);
PyModule_AddIntConstant(module, "REG_NEWLINE", REG_NEWLINE);
Expand Down
5 changes: 5 additions & 0 deletions pyext/libc.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
from typing import List, Optional, Tuple

# generated by ./configure
HAVE_GLOB_PERIOD: int
HAVE_FNM_EXTMATCH: int

# libc values
FNM_CASEFOLD: int
GLOB_PERIOD: int
REG_ICASE: int
Expand Down
3 changes: 2 additions & 1 deletion pyext/libc_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ class LibcTest(unittest.TestCase):

def testConstants(self):
print('GLOB_PERIOD %d' % libc.GLOB_PERIOD)
print('FNM_EXTMATCH %d' % libc.FNM_EXTMATCH)
print('HAVE_GLOB_PERIOD %d' % libc.HAVE_GLOB_PERIOD)
print('HAVE_FNM_EXTMATCH %d' % libc.HAVE_FNM_EXTMATCH)

def testFnmatch(self):

Expand Down
2 changes: 2 additions & 0 deletions pyext/setup_libc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

module = Extension('libc',
sources = ['pyext/libc.c'],
# for #include "_build/detected-config.h"
extra_compile_args = ['-I', '.'],
undef_macros = ['NDEBUG'])

setup(name = 'libc',
Expand Down

0 comments on commit 950120a

Please sign in to comment.