Skip to content

Commit 2c46d5d

Browse files
committed
Load CONFIG_SITE.
1 parent 3ad727d commit 2c46d5d

4 files changed

Lines changed: 121 additions & 0 deletions

File tree

Tools/configure/pyconf.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,48 @@ def __repr__(self) -> str:
190190
]
191191

192192

193+
def _load_config_site() -> None:
194+
"""Load CONFIG_SITE file, if set.
195+
196+
CONFIG_SITE is a shell script containing simple VAR=VALUE assignments
197+
used to pre-seed cache variables for cross-compilation. Only lines
198+
matching ``NAME=VALUE`` are processed; comments and blank lines are
199+
skipped. Values already present in ``os.environ`` (e.g. from
200+
command-line ``VAR=VALUE`` arguments) or already set on ``vars`` are
201+
NOT overridden.
202+
203+
``yes``/``no`` values are converted to ``True``/``False`` via
204+
``_cache_deserialize`` so that truthiness checks in conf_*.py work
205+
the same way as autoconf's ``test "x$var" = xyes``.
206+
"""
207+
config_site = os.environ.get("CONFIG_SITE", "")
208+
if not config_site or not os.path.isfile(config_site):
209+
return
210+
with open(config_site) as f:
211+
for line in f:
212+
line = line.strip()
213+
if not line or line.startswith("#"):
214+
continue
215+
m = re.match(r"^([A-Za-z_][A-Za-z0-9_]*)=(.*)", line)
216+
if not m:
217+
continue
218+
name, value = m.group(1), m.group(2)
219+
# Strip surrounding quotes (single or double)
220+
if len(value) >= 2 and value[0] == value[-1] and value[0] in ("'", '"'):
221+
value = value[1:-1]
222+
# Command-line VAR=VALUE (already in os.environ) takes precedence
223+
if name not in os.environ and not vars.is_set(name):
224+
converted = _cache_deserialize(value)
225+
setattr(vars, name, converted)
226+
# Also populate the cache dict so that check_func(),
227+
# check_header(), etc. honour the pre-seeded value and
228+
# skip the compile/link test (matching autoconf behaviour
229+
# where CONFIG_SITE sets ac_cv_* shell variables that
230+
# AC_CHECK_FUNC reads as cached results).
231+
if name.startswith("ac_cv_"):
232+
cache[name] = converted
233+
234+
193235
def init_args() -> None:
194236
"""Parse sys.argv early: handle VAR=VALUE, --help, --version, behaviour flags,
195237
and standard directory arguments. Must be called before configure parts run.
@@ -300,6 +342,12 @@ def init_args() -> None:
300342

301343
sys.argv[:] = new_argv
302344

345+
# Load CONFIG_SITE file if set (autoconf compatibility).
346+
# CONFIG_SITE is a shell script with simple VAR=VALUE assignments that
347+
# pre-seed cache variables for cross-compilation. Command-line VAR=VALUE
348+
# arguments (already placed into os.environ above) take precedence.
349+
_load_config_site()
350+
303351
# Load cache file if requested
304352
if cache_file:
305353
if os.path.exists(cache_file):

Tools/configure/transpiler/pyconf.awk

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,41 @@ function pyconf_cleanup() {
6565
system("rm -rf " _pyconf_tmpdir)
6666
}
6767

68+
function pyconf_load_config_site( config_site, line, eq, name, value) {
69+
# Load CONFIG_SITE file if set (autoconf compatibility).
70+
# CONFIG_SITE is a shell script with simple VAR=VALUE assignments that
71+
# pre-seed cache variables for cross-compilation. Command-line VAR=VALUE
72+
# arguments and existing environment variables take precedence.
73+
config_site = ENVIRON["CONFIG_SITE"]
74+
if (config_site == "") return
75+
while ((getline line < config_site) > 0) {
76+
# Skip comments and blank lines
77+
sub(/^[ \t]+/, "", line)
78+
if (line == "" || substr(line, 1, 1) == "#") continue
79+
# Match VAR=VALUE
80+
eq = index(line, "=")
81+
if (eq == 0) continue
82+
name = substr(line, 1, eq - 1)
83+
if (!match(name, /^[A-Za-z_][A-Za-z0-9_]*$/)) continue
84+
value = substr(line, eq + 1)
85+
# Strip surrounding quotes (single or double)
86+
if (length(value) >= 2) {
87+
if ((substr(value, 1, 1) == "'" && substr(value, length(value), 1) == "'") || \
88+
(substr(value, 1, 1) == "\"" && substr(value, length(value), 1) == "\""))
89+
value = substr(value, 2, length(value) - 2)
90+
}
91+
# Don't override existing env vars or already-set V[] entries
92+
if (name in ENVIRON) continue
93+
if (name in V) continue
94+
V[name] = value
95+
# Also populate CACHE for ac_cv_* keys so that check_func(),
96+
# check_header(), etc. honour the pre-seeded value
97+
if (substr(name, 1, 6) == "ac_cv_")
98+
CACHE[name] = value
99+
}
100+
close(config_site)
101+
}
102+
68103
# ---------------------------------------------------------------------------
69104
# Logging / messaging
70105
# ---------------------------------------------------------------------------

Tools/configure/transpiler/pysh_to_awk.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3300,6 +3300,8 @@ def transform_program(program: PyshProgram) -> A.Program:
33003300
)
33013301
# Parse args
33023302
begin_stmts.append(A.ExprStmt(A.FuncCall("pyconf_parse_args", [])))
3303+
# Load CONFIG_SITE (after arg parsing so VAR=VALUE overrides take precedence)
3304+
begin_stmts.append(A.ExprStmt(A.FuncCall("pyconf_load_config_site", [])))
33033305
# Check --help
33043306
begin_stmts.append(
33053307
A.If(

configure-new

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,41 @@ function pyconf_cleanup() {
7676
system("rm -rf " _pyconf_tmpdir)
7777
}
7878

79+
function pyconf_load_config_site( config_site, line, eq, name, value) {
80+
# Load CONFIG_SITE file if set (autoconf compatibility).
81+
# CONFIG_SITE is a shell script with simple VAR=VALUE assignments that
82+
# pre-seed cache variables for cross-compilation. Command-line VAR=VALUE
83+
# arguments and existing environment variables take precedence.
84+
config_site = ENVIRON["CONFIG_SITE"]
85+
if (config_site == "") return
86+
while ((getline line < config_site) > 0) {
87+
# Skip comments and blank lines
88+
sub(/^[ \t]+/, "", line)
89+
if (line == "" || substr(line, 1, 1) == "#") continue
90+
# Match VAR=VALUE
91+
eq = index(line, "=")
92+
if (eq == 0) continue
93+
name = substr(line, 1, eq - 1)
94+
if (!match(name, /^[A-Za-z_][A-Za-z0-9_]*$/)) continue
95+
value = substr(line, eq + 1)
96+
# Strip surrounding quotes (single or double)
97+
if (length(value) >= 2) {
98+
if ((substr(value, 1, 1) == "'" && substr(value, length(value), 1) == "'") || \
99+
(substr(value, 1, 1) == "\"" && substr(value, length(value), 1) == "\""))
100+
value = substr(value, 2, length(value) - 2)
101+
}
102+
# Don't override existing env vars or already-set V[] entries
103+
if (name in ENVIRON) continue
104+
if (name in V) continue
105+
V[name] = value
106+
# Also populate CACHE for ac_cv_* keys so that check_func(),
107+
# check_header(), etc. honour the pre-seeded value
108+
if (substr(name, 1, 6) == "ac_cv_")
109+
CACHE[name] = value
110+
}
111+
close(config_site)
112+
}
113+
79114
# ---------------------------------------------------------------------------
80115
# Logging / messaging
81116
# ---------------------------------------------------------------------------
@@ -8181,6 +8216,7 @@ BEGIN {
81818216
_pyconf_register_options()
81828217
_pyconf_init_constants()
81838218
pyconf_parse_args()
8219+
pyconf_load_config_site()
81848220
if (pyconf_check_help()) {
81858221
exit 0
81868222
}

0 commit comments

Comments
 (0)