lberki | 78651d4 | 2018-04-06 01:52:58 -0700 | [diff] [blame] | 1 | // Copyright 2018 The Bazel Authors. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | package com.google.devtools.build.lib.analysis; |
| 15 | |
| 16 | import com.google.common.collect.ImmutableMap; |
| 17 | import com.google.common.collect.ImmutableSet; |
| 18 | import com.google.devtools.build.lib.analysis.config.BuildConfiguration; |
| 19 | import com.google.devtools.build.lib.analysis.config.BuildConfiguration.Fragment; |
| 20 | import com.google.devtools.build.lib.analysis.config.BuildOptions; |
lberki | 78651d4 | 2018-04-06 01:52:58 -0700 | [diff] [blame] | 21 | import com.google.devtools.build.lib.analysis.config.ConfigurationFragmentFactory; |
| 22 | import com.google.devtools.build.lib.analysis.config.FragmentOptions; |
lberki | 78651d4 | 2018-04-06 01:52:58 -0700 | [diff] [blame] | 23 | import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec; |
| 24 | import com.google.devtools.build.lib.util.OS; |
| 25 | import com.google.devtools.build.lib.util.OptionsUtils.PathFragmentConverter; |
| 26 | import com.google.devtools.build.lib.vfs.PathFragment; |
| 27 | import com.google.devtools.common.options.Option; |
| 28 | import com.google.devtools.common.options.OptionDocumentationCategory; |
| 29 | import com.google.devtools.common.options.OptionEffectTag; |
cushon | 02642c2 | 2018-06-28 01:58:56 -0700 | [diff] [blame^] | 30 | import com.google.devtools.common.options.OptionMetadataTag; |
janakr | d7bec2e | 2018-06-19 09:47:01 -0700 | [diff] [blame] | 31 | import java.io.Serializable; |
lberki | 78651d4 | 2018-04-06 01:52:58 -0700 | [diff] [blame] | 32 | import javax.annotation.Nullable; |
| 33 | |
| 34 | /** A configuration fragment that tells where the shell is. */ |
lberki | 8626623 | 2018-04-11 00:33:42 -0700 | [diff] [blame] | 35 | @AutoCodec |
lberki | 78651d4 | 2018-04-06 01:52:58 -0700 | [diff] [blame] | 36 | public class ShellConfiguration extends BuildConfiguration.Fragment { |
lberki | 78651d4 | 2018-04-06 01:52:58 -0700 | [diff] [blame] | 37 | private static final ImmutableMap<OS, PathFragment> OS_SPECIFIC_SHELL = |
| 38 | ImmutableMap.<OS, PathFragment>builder() |
| 39 | .put(OS.WINDOWS, PathFragment.create("c:/tools/msys64/usr/bin/bash.exe")) |
| 40 | .put(OS.FREEBSD, PathFragment.create("/usr/local/bin/bash")) |
| 41 | .build(); |
| 42 | |
| 43 | private final PathFragment shellExecutable; |
cushon | 02642c2 | 2018-06-28 01:58:56 -0700 | [diff] [blame^] | 44 | private final boolean useShBinaryStubScript; |
lberki | 78651d4 | 2018-04-06 01:52:58 -0700 | [diff] [blame] | 45 | |
cushon | 02642c2 | 2018-06-28 01:58:56 -0700 | [diff] [blame^] | 46 | public ShellConfiguration(PathFragment shellExecutable, boolean useShBinaryStubScript) { |
lberki | 78651d4 | 2018-04-06 01:52:58 -0700 | [diff] [blame] | 47 | this.shellExecutable = shellExecutable; |
cushon | 02642c2 | 2018-06-28 01:58:56 -0700 | [diff] [blame^] | 48 | this.useShBinaryStubScript = useShBinaryStubScript; |
lberki | 78651d4 | 2018-04-06 01:52:58 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | public PathFragment getShellExecutable() { |
| 52 | return shellExecutable; |
| 53 | } |
| 54 | |
cushon | 02642c2 | 2018-06-28 01:58:56 -0700 | [diff] [blame^] | 55 | public boolean useShBinaryStubScript() { |
| 56 | return useShBinaryStubScript; |
| 57 | } |
| 58 | |
lberki | 78651d4 | 2018-04-06 01:52:58 -0700 | [diff] [blame] | 59 | /** An option that tells Bazel where the shell is. */ |
| 60 | @AutoCodec(strategy = AutoCodec.Strategy.PUBLIC_FIELDS) |
| 61 | public static class Options extends FragmentOptions { |
| 62 | @Option( |
| 63 | name = "shell_executable", |
| 64 | converter = PathFragmentConverter.class, |
| 65 | defaultValue = "null", |
| 66 | documentationCategory = OptionDocumentationCategory.UNCATEGORIZED, |
| 67 | effectTags = {OptionEffectTag.LOADING_AND_ANALYSIS}, |
| 68 | help = |
| 69 | "Absolute path to the shell executable for Bazel to use. If this is unset, but the " |
| 70 | + "BAZEL_SH environment variable is set on the first Bazel invocation (that starts " |
| 71 | + "up a Bazel server), Bazel uses that. If neither is set, Bazel uses a hard-coded " |
| 72 | + "default path depending on the operating system it runs on (Windows: " |
| 73 | + "c:/tools/msys64/usr/bin/bash.exe, FreeBSD: /usr/local/bin/bash, all others: " |
| 74 | + "/bin/bash). Note that using a shell that is not compatible with bash may lead " |
| 75 | + "to build failures or runtime failures of the generated binaries." |
| 76 | ) |
| 77 | public PathFragment shellExecutable; |
| 78 | |
cushon | 02642c2 | 2018-06-28 01:58:56 -0700 | [diff] [blame^] | 79 | @Option( |
| 80 | name = "experimental_use_sh_binary_stub_script", |
| 81 | documentationCategory = OptionDocumentationCategory.UNDOCUMENTED, |
| 82 | effectTags = {OptionEffectTag.AFFECTS_OUTPUTS}, |
| 83 | metadataTags = {OptionMetadataTag.EXPERIMENTAL}, |
| 84 | defaultValue = "false", |
| 85 | help = "If enabled, use a stub script for sh_binary targets.") |
| 86 | public boolean useShBinaryStubScript; |
| 87 | |
lberki | 78651d4 | 2018-04-06 01:52:58 -0700 | [diff] [blame] | 88 | @Override |
| 89 | public Options getHost() { |
| 90 | Options host = (Options) getDefault(); |
| 91 | host.shellExecutable = shellExecutable; |
cushon | 02642c2 | 2018-06-28 01:58:56 -0700 | [diff] [blame^] | 92 | host.useShBinaryStubScript = useShBinaryStubScript; |
lberki | 78651d4 | 2018-04-06 01:52:58 -0700 | [diff] [blame] | 93 | return host; |
| 94 | } |
| 95 | } |
| 96 | |
lberki | 78651d4 | 2018-04-06 01:52:58 -0700 | [diff] [blame] | 97 | /** the part of {@link ShellConfiguration} that determines where the shell is. */ |
| 98 | public interface ShellExecutableProvider { |
| 99 | PathFragment getShellExecutable(BuildOptions options); |
| 100 | } |
| 101 | |
| 102 | /** A shell executable whose path is hard-coded. */ |
| 103 | public static ShellExecutableProvider hardcodedShellExecutable(String shell) { |
janakr | d7bec2e | 2018-06-19 09:47:01 -0700 | [diff] [blame] | 104 | return (ShellExecutableProvider & Serializable) (options) -> PathFragment.create(shell); |
lberki | 78651d4 | 2018-04-06 01:52:58 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | /** The loader for {@link ShellConfiguration}. */ |
| 108 | public static class Loader implements ConfigurationFragmentFactory { |
| 109 | private final ShellExecutableProvider shellExecutableProvider; |
lberki | 78651d4 | 2018-04-06 01:52:58 -0700 | [diff] [blame] | 110 | private final ImmutableSet<Class<? extends FragmentOptions>> requiredOptions; |
| 111 | |
| 112 | public Loader(ShellExecutableProvider shellExecutableProvider, |
lberki | 78651d4 | 2018-04-06 01:52:58 -0700 | [diff] [blame] | 113 | Class<? extends FragmentOptions>... requiredOptions) { |
| 114 | this.shellExecutableProvider = shellExecutableProvider; |
lberki | 78651d4 | 2018-04-06 01:52:58 -0700 | [diff] [blame] | 115 | this.requiredOptions = ImmutableSet.copyOf(requiredOptions); |
| 116 | } |
| 117 | |
| 118 | @Nullable |
| 119 | @Override |
gregce | 56eb80b | 2018-05-02 09:04:10 -0700 | [diff] [blame] | 120 | public Fragment create(BuildOptions buildOptions) { |
cushon | 02642c2 | 2018-06-28 01:58:56 -0700 | [diff] [blame^] | 121 | Options options = buildOptions.get(Options.class); |
| 122 | return new ShellConfiguration( |
| 123 | shellExecutableProvider.getShellExecutable(buildOptions), |
| 124 | options != null && options.useShBinaryStubScript); |
lberki | 78651d4 | 2018-04-06 01:52:58 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | public static PathFragment determineShellExecutable( |
| 128 | OS os, Options options, PathFragment defaultShell) { |
| 129 | if (options.shellExecutable != null) { |
| 130 | return options.shellExecutable; |
| 131 | } |
| 132 | |
| 133 | // Honor BAZEL_SH env variable for backwards compatibility. |
| 134 | String path = System.getenv("BAZEL_SH"); |
| 135 | if (path != null) { |
| 136 | return PathFragment.create(path); |
| 137 | } |
| 138 | // TODO(ulfjack): instead of using the OS Bazel runs on, we need to use the exec platform, |
| 139 | // which may be different for remote execution. For now, this can be overridden with |
| 140 | // --shell_executable, so at least there's a workaround. |
| 141 | PathFragment result = OS_SPECIFIC_SHELL.get(os); |
| 142 | return result != null ? result : defaultShell; |
| 143 | } |
| 144 | |
| 145 | @Override |
| 146 | public Class<? extends Fragment> creates() { |
| 147 | return ShellConfiguration.class; |
| 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public ImmutableSet<Class<? extends FragmentOptions>> requiredOptions() { |
| 152 | return requiredOptions; |
| 153 | } |
| 154 | } |
| 155 | } |