diff --git a/src/plugins/projectexplorer/buildconfiguration.cpp b/src/plugins/projectexplorer/buildconfiguration.cpp
index 7ae211d4a79b972432d0b07e4ea5748a09fff477..b157ba3d93ca66712ecaf19ef7b39612087d6e47 100644
--- a/src/plugins/projectexplorer/buildconfiguration.cpp
+++ b/src/plugins/projectexplorer/buildconfiguration.cpp
@@ -75,6 +75,8 @@ BuildConfiguration::BuildConfiguration(Target *target, Core::Id id) :
     connect(target, SIGNAL(kitChanged()),
             this, SLOT(handleKitUpdate()));
     connect(this, SIGNAL(environmentChanged()), this, SLOT(emitBuildDirectoryChanged()));
+
+    macroExpander()->registerSubProvider([target] { return target->macroExpander(); });
 }
 
 BuildConfiguration::BuildConfiguration(Target *target, BuildConfiguration *source) :
@@ -92,29 +94,8 @@ BuildConfiguration::BuildConfiguration(Target *target, BuildConfiguration *sourc
 
     connect(target, SIGNAL(kitChanged()),
             this, SLOT(handleKitUpdate()));
-}
-
-void BuildConfiguration::setupMacroExpander()
-{
-    Utils::MacroExpander *expander = macroExpander();
-
-    expander->registerSubProvider(
-            [this] { return target()->kit()->macroExpander(); });
-
-    // Legacy support.
-    expander->registerVariable(Constants::VAR_CURRENTPROJECT_NAME,
-            QCoreApplication::translate("ProjectExplorer", "Name of current project"),
-            [this] { return target()->project()->displayName(); });
-
-    expander->registerVariable(Constants::VAR_CURRENTBUILD_NAME,
-            QCoreApplication::translate("ProjectExplorer", "Name of current build"),
-            [this] { return displayName(); });
-
-    expander->registerVariable("sourceDir", tr("Source directory"),
-            [this] { return target()->project()->projectDirectory().toUserOutput(); });
 
-    expander->registerVariable("buildDir", tr("Build directory"),
-            [this] { return buildDirectory().toUserOutput(); });
+    macroExpander()->registerSubProvider([target] { return target->macroExpander(); });
 }
 
 BuildConfiguration::~BuildConfiguration()
diff --git a/src/plugins/projectexplorer/buildconfiguration.h b/src/plugins/projectexplorer/buildconfiguration.h
index 283b33a4e82c1d05a025d68efcc86a70f92bdcca..cc8b17ecdeb079ca68e710aa122cbc4951ff9412 100644
--- a/src/plugins/projectexplorer/buildconfiguration.h
+++ b/src/plugins/projectexplorer/buildconfiguration.h
@@ -107,7 +107,6 @@ private slots:
 
 private:
     void emitEnvironmentChanged();
-    void setupMacroExpander();
 
     bool m_clearSystemEnvironment;
     QList<Utils::EnvironmentItem> m_userEnvironmentChanges;
diff --git a/src/plugins/projectexplorer/deployconfiguration.cpp b/src/plugins/projectexplorer/deployconfiguration.cpp
index debe8b0be18c89248b969f77efc4ccdb89430458..b777a77f9e2aed889c1889a61233d97b8ea0e0b7 100644
--- a/src/plugins/projectexplorer/deployconfiguration.cpp
+++ b/src/plugins/projectexplorer/deployconfiguration.cpp
@@ -53,6 +53,7 @@ DeployConfiguration::DeployConfiguration(Target *target, Core::Id id) :
     m_stepList->setDefaultDisplayName(tr("Deploy"));
     //: Default DeployConfiguration display name
     setDefaultDisplayName(tr("Deploy locally"));
+    macroExpander()->registerSubProvider([target] { return target->macroExpander(); });
 }
 
 DeployConfiguration::DeployConfiguration(Target *target, DeployConfiguration *source) :
@@ -63,6 +64,7 @@ DeployConfiguration::DeployConfiguration(Target *target, DeployConfiguration *so
     // Do not clone stepLists here, do that in the derived constructor instead
     // otherwise BuildStepFactories might reject to set up a BuildStep for us
     // since we are not yet the derived class!
+    macroExpander()->registerSubProvider([target] { return target->macroExpander(); });
 }
 
 DeployConfiguration::~DeployConfiguration()
diff --git a/src/plugins/projectexplorer/runconfiguration.cpp b/src/plugins/projectexplorer/runconfiguration.cpp
index f9e6e348ec6a97c4e2e19f395cf74a4f36c3ffec..1d1b7b637b6bba0ade7425fe957d887ee8243c04 100644
--- a/src/plugins/projectexplorer/runconfiguration.cpp
+++ b/src/plugins/projectexplorer/runconfiguration.cpp
@@ -253,6 +253,8 @@ void RunConfiguration::addExtraAspect(IRunConfigurationAspect *aspect)
 void RunConfiguration::ctor()
 {
     connect(this, SIGNAL(enabledChanged()), this, SIGNAL(requestRunActionsUpdate()));
+
+    macroExpander()->registerSubProvider([this] { return target()->macroExpander(); });
 }
 
 /*!
diff --git a/src/plugins/projectexplorer/target.cpp b/src/plugins/projectexplorer/target.cpp
index 11b25292c67d43e5dee0e7be1e0f92c3d06d193f..32b1e52f64a55841cb28e996c5c4b5caaee45773 100644
--- a/src/plugins/projectexplorer/target.cpp
+++ b/src/plugins/projectexplorer/target.cpp
@@ -138,6 +138,29 @@ Target::Target(Project *project, Kit *k) :
             this, SLOT(handleKitUpdates(ProjectExplorer::Kit*)));
     connect(km, SIGNAL(kitRemoved(ProjectExplorer::Kit*)),
             this, SLOT(handleKitRemoval(ProjectExplorer::Kit*)));
+
+    Utils::MacroExpander *expander = macroExpander();
+
+    expander->registerSubProvider([this] { return kit()->macroExpander(); });
+
+    // Legacy support.
+    expander->registerVariable(Constants::VAR_CURRENTPROJECT_NAME,
+            QCoreApplication::translate("ProjectExplorer", "Name of current project"),
+            [project] { return project->displayName(); },
+            false);
+
+    expander->registerVariable(Constants::VAR_CURRENTBUILD_NAME,
+            QCoreApplication::translate("ProjectExplorer", "Name of current build"),
+            [this] { return activeBuildConfiguration() ? activeBuildConfiguration()->displayName() : QString(); },
+            false);
+
+    expander->registerVariable("sourceDir", tr("Source directory"),
+            [project] { return project->projectDirectory().toUserOutput(); });
+
+    expander->registerVariable("buildDir", tr("Build directory"),
+            [this] { return activeBuildConfiguration()
+                        ? activeBuildConfiguration()->buildDirectory().toUserOutput()
+                        : QString() ; });
 }
 
 Target::~Target()