Index: port1.0/portconfigure.tcl
===================================================================
diff --git a/trunk/base/src/port1.0/portconfigure.tcl b/trunk/base/src/port1.0/portconfigure.tcl
--- a/trunk/base/src/port1.0/portconfigure.tcl	(revision 102867)
+++ b/trunk/base/src/port1.0/portconfigure.tcl	(working copy)
@@ -2,7 +2,7 @@
 # portconfigure.tcl
 # $Id$
 #
-# Copyright (c) 2007 - 2012 The MacPorts Project
+# Copyright (c) 2007 - 2013 The MacPorts Project
 # Copyright (c) 2007 Markus W. Weissmann <mww@macports.org>
 # Copyright (c) 2002 - 2003 Apple Inc.
 # All rights reserved.
@@ -186,10 +186,12 @@
 default configure.f90           {[portconfigure::configure_get_compiler f90]}
 default configure.fc            {[portconfigure::configure_get_compiler fc]}
 default configure.javac         {[portconfigure::configure_get_compiler javac]}
-default configure.compiler      {[portconfigure::configure_get_default_compiler]}
 default compiler.fallback       {[portconfigure::get_compiler_fallback]}
 default compiler.blacklist      {}
 default compiler.whitelist      {}
+# configure.compiler has been moved towards the end of the file so that
+# all procs are available when its option_proc is triggered on initial
+# write.
 
 set_ui_prefix
 
@@ -757,6 +759,77 @@
     return $ret
 }
 
+# Helper function for set_compiler_dependencies.  Used to force
+# "depends_FOO" to act like "depends_FOO-append"; otherwise portfiles
+# would overwrite the automatic compiler dependencies with their
+# "depends_FOO" statements.
+proc portconfigure::hijack_option {option} {
+    if {[exists $option]} {
+        return
+    }
+    interp alias {} $option {} handle_option-append $option
+}
+
+# Automatically add the necessary dependency when setting the compiler.
+proc portconfigure::set_compiler_dependencies {option action args} {
+    global option_defaults configure.compiler portconfigure::cc_should_eval \
+        portconfigure::compiler_port portconfigure::compiler_name_map
+
+    if {![info exists cc_should_eval]} {
+        set cc_should_eval yes
+    }
+    set cc_is_default [info exists option_defaults(configure.compiler)]
+
+    switch $option {
+        configure.compiler {
+            if {$action eq "read" && !($cc_is_default && $cc_should_eval)} {
+                return
+            }
+            if {[info exists compiler_port]} {
+                depends_lib-delete port:$compiler_port
+                depends_build-delete port:$compiler_port
+                depends_skip_archcheck-delete port:$compiler_port
+                unset compiler_port
+            }
+            # The default value requires substitution before use.
+            set compiler [subst ${configure.compiler}]
+            if {![compiler_is_port $compiler]} {
+                return
+            }
+            set compiler_port $compiler_name_map($compiler)
+            if {[string match "macports-gcc-*" $compiler]} {
+                hijack_option depends_lib
+                depends_lib-append port:$compiler_port
+            } else {
+                hijack_option depends_build
+                depends_build-append port:$compiler_port
+            }
+            if {[arch_flag_supported $compiler]} {
+                hijack_option depends_skip_archcheck
+                depends_skip_archcheck-append $compiler_port
+            }
+            set cc_should_eval no
+        }
+        default {
+            if {$action eq "read" || !$cc_is_default} {
+                return
+            }
+            set cc_should_eval yes
+            # This is solely to trigger the variable trace.
+            expr {${configure.compiler}}
+        }
+    }
+}
+
+# Set traces on all values that might affect the compiler selection.
+option_proc compiler.fallback  portconfigure::set_compiler_dependencies
+option_proc compiler.blacklist portconfigure::set_compiler_dependencies
+option_proc compiler.whitelist portconfigure::set_compiler_dependencies
+option_proc configure.compiler portconfigure::set_compiler_dependencies
+
+default configure.compiler {[portconfigure::configure_get_default_compiler]}
+
+
 proc portconfigure::configure_main {args} {
     global [info globals]
     global worksrcpath use_configure use_autoreconf use_autoconf use_automake use_xmkmf
