Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d4fcdb1

Browse files
committed
Issue #9189: Allow users to set $CFLAGS, $CPPFLAGS, and $LDFLAGS when running
configure to append to Python's default values for those variables, and similarly allow users to set $XXFLAGS on the make command line to append to the values set by configure. In the makefile, this renames the variables that used to be $XXFLAGS to $PY_XXFLAGS, and renames the old $PY_CFLAGS to $PY_CORE_CFLAGS. To compensate, sysconfig now aliases $XXFLAGS=$PY_XXFLAGS so that scripts using it keep working. I see that as the right interface, not a backward-compatibility hack, since these are logically the $XXFLAGS variables; we just use a different name in the makefile to deal with make's semantics.
1 parent 74e4561 commit d4fcdb1

6 files changed

Lines changed: 53 additions & 38 deletions

File tree

Lib/sysconfig.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,11 @@ def _parse_makefile(filename, vars=None):
259259
# bogus variable reference; just drop it since we can't deal
260260
variables.remove(name)
261261

262+
# Add in CFLAGS, LDFLAGS, and CPPFLAGS, which are named with a
263+
# prefix in the Makefile.
264+
for var in ('CFLAGS', 'LDFLAGS', 'CPPFLAGS'):
265+
done[var] = done['PY_' + var]
266+
262267
# save the results in the global dictionary
263268
vars.update(done)
264269
return vars

Makefile.pre.in

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,18 @@ MAKESETUP= $(srcdir)/Modules/makesetup
5959
# Compiler options
6060
OPT= @OPT@
6161
BASECFLAGS= @BASECFLAGS@
62-
CFLAGS= $(BASECFLAGS) @CFLAGS@ $(OPT) $(EXTRA_CFLAGS)
62+
CONFIGURE_CFLAGS= @CFLAGS@
63+
CONFIGURE_CPPFLAGS= @CPPFLAGS@
64+
CONFIGURE_LDFLAGS= @LDFLAGS@
65+
# Avoid assigning CFLAGS, LDFLAGS, etc. so users can use them on the
66+
# command line to append to these values without stomping the pre-set
67+
# values.
68+
PY_CFLAGS= $(BASECFLAGS) $(OPT) $(CONFIGURE_CFLAGS) $(CFLAGS) $(EXTRA_CFLAGS)
6369
# Both CPPFLAGS and LDFLAGS need to contain the shell's value for setup.py to
6470
# be able to build extension modules using the directories specified in the
6571
# environment variables
66-
CPPFLAGS= -I. -IInclude -I$(srcdir)/Include @CPPFLAGS@
67-
LDFLAGS= @LDFLAGS@
72+
PY_CPPFLAGS= -I. -IInclude -I$(srcdir)/Include $(CONFIGURE_CPPFLAGS) $(CPPFLAGS)
73+
PY_LDFLAGS= $(CONFIGURE_LDFLAGS) $(LDFLAGS)
6874
LDLAST= @LDLAST@
6975
SGI_ABI= @SGI_ABI@
7076
CCSHARED= @CCSHARED@
@@ -73,7 +79,7 @@ ARFLAGS= @ARFLAGS@
7379
# Extra C flags added for building the interpreter object files.
7480
CFLAGSFORSHARED=@CFLAGSFORSHARED@
7581
# C flags used for building the interpreter object files
76-
PY_CFLAGS= $(CFLAGS) $(CPPFLAGS) $(CFLAGSFORSHARED) -DPy_BUILD_CORE
82+
PY_CORE_CFLAGS= $(PY_CFLAGS) $(PY_CPPFLAGS) $(CFLAGSFORSHARED) -DPy_BUILD_CORE
7783

7884

7985
# Machine-dependent subdirectories
@@ -411,7 +417,7 @@ coverage:
411417

412418
# Build the interpreter
413419
$(BUILDPYTHON): Modules/python.o $(LIBRARY) $(LDLIBRARY)
414-
$(LINKCC) $(LDFLAGS) $(LINKFORSHARED) -o $@ \
420+
$(LINKCC) $(PY_LDFLAGS) $(LINKFORSHARED) -o $@ \
415421
Modules/python.o \
416422
$(BLDLIBRARY) $(LIBS) $(MODLIBS) $(SYSLIBS) $(LDLAST)
417423

@@ -422,8 +428,8 @@ platform: $(BUILDPYTHON)
422428
# Build the shared modules
423429
sharedmods: $(BUILDPYTHON)
424430
@case $$MAKEFLAGS in \
425-
*s*) $(RUNSHARED) CC='$(CC)' LDSHARED='$(BLDSHARED)' LDFLAGS='$(LDFLAGS)' OPT='$(OPT)' ./$(BUILDPYTHON) -E $(srcdir)/setup.py -q build;; \
426-
*) $(RUNSHARED) CC='$(CC)' LDSHARED='$(BLDSHARED)' LDFLAGS='$(LDFLAGS)' OPT='$(OPT)' ./$(BUILDPYTHON) -E $(srcdir)/setup.py build;; \
431+
*s*) $(RUNSHARED) CC='$(CC)' LDSHARED='$(BLDSHARED)' LDFLAGS='$(PY_LDFLAGS)' OPT='$(OPT)' ./$(BUILDPYTHON) -E $(srcdir)/setup.py -q build;; \
432+
*) $(RUNSHARED) CC='$(CC)' LDSHARED='$(BLDSHARED)' LDFLAGS='$(PY_LDFLAGS)' OPT='$(OPT)' ./$(BUILDPYTHON) -E $(srcdir)/setup.py build;; \
427433
esac
428434

429435
# Build static library
@@ -440,18 +446,18 @@ $(LIBRARY): $(LIBRARY_OBJS)
440446

441447
libpython$(VERSION).so: $(LIBRARY_OBJS)
442448
if test $(INSTSONAME) != $(LDLIBRARY); then \
443-
$(LDSHARED) $(LDFLAGS) -Wl,-h$(INSTSONAME) -o $(INSTSONAME) $(LIBRARY_OBJS) $(MODLIBS) $(SHLIBS) $(LIBC) $(LIBM) $(LDLAST); \
449+
$(LDSHARED) $(PY_LDFLAGS) -Wl,-h$(INSTSONAME) -o $(INSTSONAME) $(LIBRARY_OBJS) $(MODLIBS) $(SHLIBS) $(LIBC) $(LIBM) $(LDLAST); \
444450
$(LN) -f $(INSTSONAME) $@; \
445451
else \
446-
$(LDSHARED) $(LDFLAGS) -o $@ $(LIBRARY_OBJS) $(MODLIBS) $(SHLIBS) $(LIBC) $(LIBM) $(LDLAST); \
452+
$(LDSHARED) $(PY_LDFLAGS) -o $@ $(LIBRARY_OBJS) $(MODLIBS) $(SHLIBS) $(LIBC) $(LIBM) $(LDLAST); \
447453
fi
448454

449455
libpython$(VERSION).dylib: $(LIBRARY_OBJS)
450-
$(CC) -dynamiclib -Wl,-single_module $(LDFLAGS) -undefined dynamic_lookup -Wl,-install_name,$(prefix)/lib/libpython$(VERSION).dylib -Wl,-compatibility_version,$(VERSION) -Wl,-current_version,$(VERSION) -o $@ $(LIBRARY_OBJS) $(SHLIBS) $(LIBC) $(LIBM) $(LDLAST); \
456+
$(CC) -dynamiclib -Wl,-single_module $(PY_LDFLAGS) -undefined dynamic_lookup -Wl,-install_name,$(prefix)/lib/libpython$(VERSION).dylib -Wl,-compatibility_version,$(VERSION) -Wl,-current_version,$(VERSION) -o $@ $(LIBRARY_OBJS) $(SHLIBS) $(LIBC) $(LIBM) $(LDLAST); \
451457

452458

453459
libpython$(VERSION).sl: $(LIBRARY_OBJS)
454-
$(LDSHARED) $(LDFLAGS) -o $@ $(LIBRARY_OBJS) $(MODLIBS) $(SHLIBS) $(LIBC) $(LIBM) $(LDLAST)
460+
$(LDSHARED) $(PY_LDFLAGS) -o $@ $(LIBRARY_OBJS) $(MODLIBS) $(SHLIBS) $(LIBC) $(LIBM) $(LDLAST)
455461

456462
# Copy up the gdb python hooks into a position where they can be automatically
457463
# loaded by gdb during Lib/test/test_gdb.py
@@ -497,7 +503,7 @@ $(PYTHONFRAMEWORKDIR)/Versions/$(VERSION)/$(PYTHONFRAMEWORK): \
497503
# for a shared core library; otherwise, this rule is a noop.
498504
$(DLLLIBRARY) libpython$(VERSION).dll.a: $(LIBRARY_OBJS)
499505
if test -n "$(DLLLIBRARY)"; then \
500-
$(LDSHARED) $(LDFLAGS) -Wl,--out-implib=$@ -o $(DLLLIBRARY) $^ \
506+
$(LDSHARED) $(PY_LDFLAGS) -Wl,--out-implib=$@ -o $(DLLLIBRARY) $^ \
501507
$(LIBS) $(MODLIBS) $(SYSLIBS) $(LDLAST); \
502508
else true; \
503509
fi
@@ -541,18 +547,18 @@ Modules/getbuildinfo.o: $(PARSER_OBJS) \
541547
$(SIGNAL_OBJS) \
542548
$(MODOBJS) \
543549
$(srcdir)/Modules/getbuildinfo.c
544-
$(CC) -c $(PY_CFLAGS) -DSVNVERSION="\"`LC_ALL=C $(SVNVERSION)`\"" -o $@ $(srcdir)/Modules/getbuildinfo.c
550+
$(CC) -c $(PY_CORE_CFLAGS) -DSVNVERSION="\"`LC_ALL=C $(SVNVERSION)`\"" -o $@ $(srcdir)/Modules/getbuildinfo.c
545551

546552
Modules/getpath.o: $(srcdir)/Modules/getpath.c Makefile
547-
$(CC) -c $(PY_CFLAGS) -DPYTHONPATH='"$(PYTHONPATH)"' \
553+
$(CC) -c $(PY_CORE_CFLAGS) -DPYTHONPATH='"$(PYTHONPATH)"' \
548554
-DPREFIX='"$(prefix)"' \
549555
-DEXEC_PREFIX='"$(exec_prefix)"' \
550556
-DVERSION='"$(VERSION)"' \
551557
-DVPATH='"$(VPATH)"' \
552558
-o $@ $(srcdir)/Modules/getpath.c
553559

554560
Modules/python.o: $(srcdir)/Modules/python.c
555-
$(MAINCC) -c $(PY_CFLAGS) -o $@ $(srcdir)/Modules/python.c
561+
$(MAINCC) -c $(PY_CORE_CFLAGS) -o $@ $(srcdir)/Modules/python.c
556562

557563
$(IO_OBJS): $(IO_H)
558564

@@ -561,7 +567,7 @@ $(GRAMMAR_H) $(GRAMMAR_C): $(PGEN) $(GRAMMAR_INPUT)
561567
-$(PGEN) $(GRAMMAR_INPUT) $(GRAMMAR_H) $(GRAMMAR_C)
562568

563569
$(PGEN): $(PGENOBJS)
564-
$(CC) $(OPT) $(LDFLAGS) $(PGENOBJS) $(LIBS) -o $(PGEN)
570+
$(CC) $(OPT) $(PY_LDFLAGS) $(PGENOBJS) $(LIBS) -o $(PGEN)
565571

566572
Parser/grammar.o: $(srcdir)/Parser/grammar.c \
567573
$(srcdir)/Include/token.h \
@@ -581,10 +587,10 @@ $(AST_C): $(AST_ASDL) $(ASDLGEN_FILES)
581587
Python/compile.o Python/symtable.o Python/ast.o: $(GRAMMAR_H) $(AST_H)
582588

583589
Python/getplatform.o: $(srcdir)/Python/getplatform.c
584-
$(CC) -c $(PY_CFLAGS) -DPLATFORM='"$(MACHDEP)"' -o $@ $(srcdir)/Python/getplatform.c
590+
$(CC) -c $(PY_CORE_CFLAGS) -DPLATFORM='"$(MACHDEP)"' -o $@ $(srcdir)/Python/getplatform.c
585591

586592
Python/importdl.o: $(srcdir)/Python/importdl.c
587-
$(CC) -c $(PY_CFLAGS) -I$(DLINCLDIR) -o $@ $(srcdir)/Python/importdl.c
593+
$(CC) -c $(PY_CORE_CFLAGS) -I$(DLINCLDIR) -o $@ $(srcdir)/Python/importdl.c
588594

589595
Objects/unicodectype.o: $(srcdir)/Objects/unicodectype.c \
590596
$(srcdir)/Objects/unicodetype_db.h
@@ -1130,7 +1136,7 @@ config.status: $(srcdir)/configure
11301136

11311137
# Some make's put the object file in the current directory
11321138
.c.o:
1133-
$(CC) -c $(PY_CFLAGS) -o $@ $<
1139+
$(CC) -c $(PY_CORE_CFLAGS) -o $@ $<
11341140

11351141
# Run reindent on the library
11361142
reindent:

Misc/NEWS

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1530,6 +1530,12 @@ Extension Modules
15301530
Build
15311531
-----
15321532

1533+
- Issue #9189: Make a user-specified CFLAGS, CPPFLAGS, or LDFLAGS
1534+
setting override the configure and makefile defaults, without
1535+
deleting options the user didn't intend to override. Developers
1536+
should no longer need to specify OPT or EXTRA_CFLAGS, although those
1537+
variables are still present for backward-compatibility.
1538+
15331539
- Issue #8854: Fix finding Visual Studio 2008 on Windows x64.
15341540

15351541
- Issue #1759169, #8864: Drop _XOPEN_SOURCE on Solaris, define it for

Modules/makesetup

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ sed -e 's/[ ]*#.*//' -e '/^[ ]*$/d' |
219219
case $doconfig in
220220
no) cc="$cc \$(CCSHARED) \$(CFLAGS) \$(CPPFLAGS)";;
221221
*)
222-
cc="$cc \$(PY_CFLAGS)";;
222+
cc="$cc \$(PY_CORE_CFLAGS)";;
223223
esac
224224
rule="$obj: $src; $cc $cpps -c $src -o $obj"
225225
echo "$rule" >>$rulesf

configure

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1929,11 +1929,11 @@ else
19291929
cat confdefs.h - <<_ACEOF >conftest.$ac_ext
19301930
/* end confdefs.h. */
19311931
$ac_includes_default
1932+
enum { N = $2 / 2 - 1 };
19321933
int
19331934
main ()
19341935
{
1935-
static int test_array [1 - 2 * !(enum { N = $2 / 2 - 1 };
1936-
0 < ($ac_type) ((((($ac_type) 1 << N) << N) - 1) * 2 + 1))];
1936+
static int test_array [1 - 2 * !(0 < ($ac_type) ((((($ac_type) 1 << N) << N) - 1) * 2 + 1))];
19371937
test_array [0] = 0
19381938
19391939
;
@@ -1944,11 +1944,11 @@ if ac_fn_c_try_compile "$LINENO"; then :
19441944
cat confdefs.h - <<_ACEOF >conftest.$ac_ext
19451945
/* end confdefs.h. */
19461946
$ac_includes_default
1947+
enum { N = $2 / 2 - 1 };
19471948
int
19481949
main ()
19491950
{
1950-
static int test_array [1 - 2 * !(enum { N = $2 / 2 - 1 };
1951-
($ac_type) ((((($ac_type) 1 << N) << N) - 1) * 2 + 1)
1951+
static int test_array [1 - 2 * !(($ac_type) ((((($ac_type) 1 << N) << N) - 1) * 2 + 1)
19521952
< ($ac_type) ((((($ac_type) 1 << N) << N) - 1) * 2 + 2))];
19531953
test_array [0] = 0
19541954
@@ -3158,9 +3158,12 @@ then
31583158
(it is also a good idea to do 'make clean' before compiling)" "$LINENO" 5
31593159
fi
31603160
3161-
# If the user set CFLAGS, use this instead of the automatically
3162-
# determined setting
3163-
preset_cflags="$CFLAGS"
3161+
# Don't let AC_PROG_CC set the default CFLAGS. It normally sets -g -O2
3162+
# when the compiler supports them, but we don't always want -O2, and
3163+
# we set -g later.
3164+
if test -z "$CFLAGS"; then
3165+
CFLAGS=
3166+
fi
31643167
ac_ext=c
31653168
ac_cpp='$CPP $CPPFLAGS'
31663169
ac_compile='$CC -c $CFLAGS $CPPFLAGS conftest.$ac_ext >&5'
@@ -3952,10 +3955,6 @@ ac_compile='$CC -c $CFLAGS $CPPFLAGS conftest.$ac_ext >&5'
39523955
ac_link='$CC -o conftest$ac_exeext $CFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5'
39533956
ac_compiler_gnu=$ac_cv_c_compiler_gnu
39543957
3955-
if test ! -z "$preset_cflags"
3956-
then
3957-
CFLAGS=$preset_cflags
3958-
fi
39593958
39603959
39613960

configure.in

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -461,14 +461,13 @@ then
461461
(it is also a good idea to do 'make clean' before compiling)])
462462
fi
463463

464-
# If the user set CFLAGS, use this instead of the automatically
465-
# determined setting
466-
preset_cflags="$CFLAGS"
467-
AC_PROG_CC
468-
if test ! -z "$preset_cflags"
469-
then
470-
CFLAGS=$preset_cflags
464+
# Don't let AC_PROG_CC set the default CFLAGS. It normally sets -g -O2
465+
# when the compiler supports them, but we don't always want -O2, and
466+
# we set -g later.
467+
if test -z "$CFLAGS"; then
468+
CFLAGS=
471469
fi
470+
AC_PROG_CC
472471

473472
AC_SUBST(CXX)
474473
AC_SUBST(MAINCC)

0 commit comments

Comments
 (0)