| Index: SConstruct | 
| diff --git a/SConstruct b/SConstruct | 
| index 24ad04cac10cdd532f56fe87b9d8eb06d66ef782..55adfe94700cf9a7592accaeefc8465a2cae5a7a 100755 | 
| --- a/SConstruct | 
| +++ b/SConstruct | 
| @@ -1251,6 +1251,10 @@ def GetIrtNexe(env, chrome_irt=False): | 
|  | 
| pre_base_env.AddMethod(GetIrtNexe) | 
|  | 
| +def GetTLSEdit(env): | 
| +  return env['BUILD_ENV'].File('${STAGING_DIR}/tls_edit${PROGSUFFIX}') | 
| + | 
| +pre_base_env.AddMethod(GetTLSEdit) | 
|  | 
| def CommandValidatorTestNacl(env, name, image, | 
| validator_flags=None, | 
| @@ -3609,7 +3613,14 @@ def MakeBuildEnv(): | 
|  | 
| build_env = make_env_func(build_platform) | 
| build_env['IS_BUILD_ENV'] = True | 
| -  build_env['BUILD_SCONSCRIPTS'] = [] | 
| + | 
| +  # Building tls_edit depends on gio, platform, and validator_ragel. | 
| +  build_env['BUILD_SCONSCRIPTS'] = [ | 
| +    # KEEP THIS SORTED PLEASE | 
| +    'src/shared/gio/build.scons', | 
| +    'src/shared/platform/build.scons', | 
| +    'src/trusted/validator_ragel/build.scons', | 
| +    ] | 
|  | 
| # The build environment is only used for intermediate steps and should | 
| # not be creating any targets. Aliases are used as means to add targets | 
| @@ -3618,17 +3629,29 @@ def MakeBuildEnv(): | 
| # override the alias function and essentially stub it out. | 
| build_env.Alias = lambda env, target, source=[], actions=None, **kw : [] | 
|  | 
| -  dbg_build_env, opt_build_env = GenerateOptimizationLevels(build_env) | 
| +  return build_env | 
| + | 
| +def LinkBuildEnv(selected_envs): | 
| +  build_env_map = { | 
| +    'opt': opt_build_env, | 
| +    'dbg': dbg_build_env, | 
| +    } | 
|  | 
| -  return opt_build_env | 
| +  # We need to find the optimization level in order to know which | 
| +  # build environment we want to use | 
| +  opt_level = None | 
| +  for env in selected_envs: | 
| +    if 'OPTIMIZATION_LEVEL' in env: | 
| +      if env['OPTIMIZATION_LEVEL']: | 
| +        opt_level = env['OPTIMIZATION_LEVEL'] | 
| +        break | 
|  | 
| -def LinkBuildEnv(selected_envs, build_env): | 
| +  build_env = build_env_map.get(opt_level, opt_build_env) | 
| for env in selected_envs: | 
| env['BUILD_ENV'] = build_env | 
|  | 
| -  # If platform is not the same as the host environment, add the targets | 
| -  #  of the host environment so scons can find them | 
| -  if GetBuildPlatform() != GetTargetPlatform(): | 
| +  if (opt_level not in build_env_map or | 
| +      GetBuildPlatform() != GetTargetPlatform()): | 
| selected_envs.append(build_env) | 
|  | 
| def DumpEnvironmentInfo(selected_envs): | 
| @@ -3694,10 +3717,16 @@ if nacl_env in selected_envs: | 
| if nacl_irt_test_env in selected_envs and nacl_env not in selected_envs: | 
| selected_envs.append(nacl_env) | 
|  | 
| - | 
| DumpEnvironmentInfo(selected_envs) | 
| LinkTrustedEnv(selected_envs) | 
| -LinkBuildEnv(selected_envs, MakeBuildEnv()) | 
| + | 
| +# When building nacl, any intermediate build tool that is used during the | 
| +# build process must be built using the current build environment, not the | 
| +# target. Create a build environment for this purpose and link it into | 
| +# the selected environments | 
| +dbg_build_env, opt_build_env = GenerateOptimizationLevels(MakeBuildEnv()) | 
| +LinkBuildEnv(selected_envs) | 
| + | 
| # This must happen after LinkTrustedEnv, since that is where TRUSTED_ENV | 
| # is finally set, and env.UsingEmulator() checks TRUSTED_ENV for the emulator. | 
| # This must also happen before BuildEnvironments. | 
|  |