diff -ur tcl.trunk.old/unix/tcl.m4 tcl.trunk.new/unix/tcl.m4
--- tcl.trunk.old/unix/tcl.m4	2015-04-04 13:47:53.000000000 +0200
+++ tcl.trunk.new/unix/tcl.m4	2015-04-05 23:10:48.000000000 +0200
@@ -1093,6 +1093,19 @@
     TCL_TRIM_DOTS='`echo ${VERSION} | tr -d .`'
     ECHO_VERSION='`echo ${VERSION}`'
     TCL_LIB_VERSIONS_OK=ok
+
+    # Store any custom debug and optimization flags (supplied on the
+    # command line, in a config.site script, or otherwise from the
+    # environment). To override CFLAGS_DEBUG and CFLAGS_OPTIMIZE later.
+    AC_ARG_VAR(CFLAGS_DEBUG, [debugging flags for the compiler])
+    CUSTOM_CFLAGS_DEBUG="$CFLAGS_DEBUG"
+    AC_ARG_VAR(CFLAGS_OPTIMIZE, [optimization flags for the compiler])
+    CUSTOM_CFLAGS_OPTIMIZE="$CFLAGS_OPTIMIZE"
+    AC_ARG_VAR(LDFLAGS_DEBUG, [debugging flags passed to the linker (use -Wl,...)])
+    CUSTOM_LDFLAGS_DEBUG="$LDFLAGS_DEBUG"
+    AC_ARG_VAR(LDFLAGS_OPTIMIZE, [optimization flags passed to the linker (use -Wl,...)])
+    CUSTOM_LDFLAGS_OPTIMIZE="$LDFLAGS_OPTIMIZE"
+
     CFLAGS_DEBUG=-g
     AS_IF([test "$GCC" = yes], [
 	CFLAGS_OPTIMIZE=-O2
@@ -1983,6 +1996,20 @@
 	    ;;
     esac
 
+    # Use any custom debug and optimization flags
+    AS_IF([test -n "$CUSTOM_CFLAGS_DEBUG"], [
+	CFLAGS_DEBUG=$CUSTOM_CFLAGS_DEBUG
+    ])
+    AS_IF([test -n "$CUSTOM_CFLAGS_OPTIMIZE"], [
+	CFLAGS_OPTIMIZE=$CUSTOM_CFLAGS_OPTIMIZE
+    ])
+    AS_IF([test -n "$CUSTOM_LDFLAGS_DEBUG"], [
+	LDFLAGS_DEBUG=$CUSTOM_LDFLAGS_DEBUG
+    ])
+    AS_IF([test -n "$CUSTOM_LDFLAGS_OPTIMIZE"], [
+	LDFLAGS_OPTIMIZE=$CUSTOM_LDFLAGS_OPTIMIZE
+    ])
+
     AS_IF([test "$do64bit" = yes -a "$do64bit_ok" = no], [
 	AC_MSG_WARN([64bit support being disabled -- don't know magic for this platform])
     ])
@@ -2102,6 +2129,10 @@
 		    [Defined when compiler supports casting to union type.])
 	fi
 
+    # Note: precious variables like CFLAGS, LDFLAGS, CFLAGS_DEBUG and
+    # CFLAGS_OPTIMIZE are substituted automatically, and do not need an
+    # explicit AC_SUBST here.
+    
     # FIXME: This subst was left in only because the TCL_DL_LIBS
     # entry in tclConfig.sh uses it. It is not clear why someone
     # would use TCL_DL_LIBS instead of TCL_LIBS.
@@ -2111,14 +2142,8 @@
     AC_SUBST(PLAT_OBJS)
     AC_SUBST(PLAT_SRCS)
     AC_SUBST(LDAIX_SRC)
-    AC_SUBST(CFLAGS)
-    AC_SUBST(CFLAGS_DEBUG)
-    AC_SUBST(CFLAGS_OPTIMIZE)
     AC_SUBST(CFLAGS_WARNING)
 
-    AC_SUBST(LDFLAGS)
-    AC_SUBST(LDFLAGS_DEBUG)
-    AC_SUBST(LDFLAGS_OPTIMIZE)
     AC_SUBST(CC_SEARCH_FLAGS)
     AC_SUBST(LD_SEARCH_FLAGS)
 
diff -ur tcl.trunk.old/win/tcl.m4 tcl.trunk.new/win/tcl.m4
--- tcl.trunk.old/win/tcl.m4	2015-04-04 13:47:53.000000000 +0200
+++ tcl.trunk.new/win/tcl.m4	2015-04-05 23:02:49.000000000 +0200
@@ -670,6 +670,19 @@
     fi
 
     AC_MSG_CHECKING([compiler flags])
+
+    # Store any custom debug and optimization flags (supplied on the
+    # command line, in a config.site script, or otherwise from the
+    # environment). To override CFLAGS_DEBUG and CFLAGS_OPTIMIZE later.
+    AC_ARG_VAR(CFLAGS_DEBUG, [debugging flags for the compiler])
+    CUSTOM_CFLAGS_DEBUG="$CFLAGS_DEBUG"
+    AC_ARG_VAR(CFLAGS_OPTIMIZE, [optimization flags for the compiler])
+    CUSTOM_CFLAGS_OPTIMIZE="$CFLAGS_OPTIMIZE"
+    AC_ARG_VAR(LDFLAGS_DEBUG, [debugging flags passed to the linker (use -Wl,...)])
+    CUSTOM_LDFLAGS_DEBUG="$LDFLAGS_DEBUG"
+    AC_ARG_VAR(LDFLAGS_OPTIMIZE, [optimization flags passed to the linker (use -Wl,...)])
+    CUSTOM_LDFLAGS_OPTIMIZE="$LDFLAGS_OPTIMIZE"
+
     if test "${GCC}" = "yes" ; then
 	SHLIB_LD=""
 	SHLIB_LD_LIBS='${LIBS}'
@@ -966,8 +979,8 @@
 	MAKE_EXE="\${CC} -Fe\[$]@"
 	LIBPREFIX=""
 
-	CFLAGS_DEBUG="${CFLAGS_DEBUG} -D_CRT_SECURE_NO_DEPRECATE -D_CRT_NONSTDC_NO_DEPRECATE"
-	CFLAGS_OPTIMIZE="${CFLAGS_OPTIMIZE} -D_CRT_SECURE_NO_DEPRECATE -D_CRT_NONSTDC_NO_DEPRECATE"
+	AC_DEFINE(_CRT_SECURE_NO_DEPRECATE)
+	AC_DEFINE(_CRT_NONSTDC_NO_DEPRECATE)
 
 	EXTRA_CFLAGS=""
 	CFLAGS_WARNING="-W3"
@@ -1088,10 +1101,26 @@
 	fi
     fi
 
+    # Use any custom debug and optimization flags
+    if test -n "$CUSTOM_CFLAGS_DEBUG"; then
+	CFLAGS_DEBUG=$CUSTOM_CFLAGS_DEBUG
+    fi
+    if test -n "$CUSTOM_CFLAGS_OPTIMIZE"; then
+	CFLAGS_OPTIMIZE=$CUSTOM_CFLAGS_OPTIMIZE
+    fi
+    if test -n "$CUSTOM_LDFLAGS_DEBUG"; then
+	LDFLAGS_DEBUG=$CUSTOM_LDFLAGS_DEBUG
+    fi
+    if test -n "$CUSTOM_LDFLAGS_OPTIMIZE"; then
+	LDFLAGS_OPTIMIZE=$CUSTOM_LDFLAGS_OPTIMIZE
+    fi
+
+    # Note: precious variables like CFLAGS, LDFLAGS, CFLAGS_DEBUG and
+    # CFLAGS_OPTIMIZE are substituted automatically, and do not need an
+    # explicit AC_SUBST here.
+
     # DL_LIBS is empty, but then we match the Unix version
     AC_SUBST(DL_LIBS)
-    AC_SUBST(CFLAGS_DEBUG)
-    AC_SUBST(CFLAGS_OPTIMIZE)
     AC_SUBST(CFLAGS_WARNING)
 ])
 
