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.util.OS; |
| 24 | import com.google.devtools.build.lib.util.OptionsUtils.PathFragmentConverter; |
| 25 | import com.google.devtools.build.lib.vfs.PathFragment; |
| 26 | import com.google.devtools.common.options.Option; |
| 27 | import com.google.devtools.common.options.OptionDocumentationCategory; |
| 28 | import com.google.devtools.common.options.OptionEffectTag; |
cushon | 02642c2 | 2018-06-28 01:58:56 -0700 | [diff] [blame] | 29 | import com.google.devtools.common.options.OptionMetadataTag; |
janakr | d7bec2e | 2018-06-19 09:47:01 -0700 | [diff] [blame] | 30 | import java.io.Serializable; |
lberki | 78651d4 | 2018-04-06 01:52:58 -0700 | [diff] [blame] | 31 | import javax.annotation.Nullable; |
| 32 | |
| 33 | /** A configuration fragment that tells where the shell is. */ |
| 34 | public class ShellConfiguration extends BuildConfiguration.Fragment { |
lberki | 78651d4 | 2018-04-06 01:52:58 -0700 | [diff] [blame] | 35 | private static final ImmutableMap<OS, PathFragment> OS_SPECIFIC_SHELL = |
| 36 | ImmutableMap.<OS, PathFragment>builder() |
| 37 | .put(OS.WINDOWS, PathFragment.create("c:/tools/msys64/usr/bin/bash.exe")) |
| 38 | .put(OS.FREEBSD, PathFragment.create("/usr/local/bin/bash")) |
| 39 | .build(); |
| 40 | |
| 41 | private final PathFragment shellExecutable; |
cushon | 02642c2 | 2018-06-28 01:58:56 -0700 | [diff] [blame] | 42 | private final boolean useShBinaryStubScript; |
lberki | 78651d4 | 2018-04-06 01:52:58 -0700 | [diff] [blame] | 43 | |
shahan | 2ed5ed9 | 2018-09-06 10:24:06 -0700 | [diff] [blame] | 44 | private ShellConfiguration(PathFragment shellExecutable, boolean useShBinaryStubScript) { |
lberki | 78651d4 | 2018-04-06 01:52:58 -0700 | [diff] [blame] | 45 | this.shellExecutable = shellExecutable; |
cushon | 02642c2 | 2018-06-28 01:58:56 -0700 | [diff] [blame] | 46 | this.useShBinaryStubScript = useShBinaryStubScript; |
lberki | 78651d4 | 2018-04-06 01:52:58 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | public PathFragment getShellExecutable() { |
| 50 | return shellExecutable; |
| 51 | } |
| 52 | |
cushon | 02642c2 | 2018-06-28 01:58:56 -0700 | [diff] [blame] | 53 | public boolean useShBinaryStubScript() { |
| 54 | return useShBinaryStubScript; |
| 55 | } |
| 56 | |
lberki | 78651d4 | 2018-04-06 01:52:58 -0700 | [diff] [blame] | 57 | /** An option that tells Bazel where the shell is. */ |
lberki | 78651d4 | 2018-04-06 01:52:58 -0700 | [diff] [blame] | 58 | public static class Options extends FragmentOptions { |
| 59 | @Option( |
| 60 | name = "shell_executable", |
| 61 | converter = PathFragmentConverter.class, |
| 62 | defaultValue = "null", |
| 63 | documentationCategory = OptionDocumentationCategory.UNCATEGORIZED, |
| 64 | effectTags = {OptionEffectTag.LOADING_AND_ANALYSIS}, |
| 65 | help = |
| 66 | "Absolute path to the shell executable for Bazel to use. If this is unset, but the " |
| 67 | + "BAZEL_SH environment variable is set on the first Bazel invocation (that starts " |
| 68 | + "up a Bazel server), Bazel uses that. If neither is set, Bazel uses a hard-coded " |
| 69 | + "default path depending on the operating system it runs on (Windows: " |
| 70 | + "c:/tools/msys64/usr/bin/bash.exe, FreeBSD: /usr/local/bin/bash, all others: " |
| 71 | + "/bin/bash). Note that using a shell that is not compatible with bash may lead " |
| 72 | + "to build failures or runtime failures of the generated binaries." |
| 73 | ) |
| 74 | public PathFragment shellExecutable; |
| 75 | |
cushon | 02642c2 | 2018-06-28 01:58:56 -0700 | [diff] [blame] | 76 | @Option( |
| 77 | name = "experimental_use_sh_binary_stub_script", |
| 78 | documentationCategory = OptionDocumentationCategory.UNDOCUMENTED, |
| 79 | effectTags = {OptionEffectTag.AFFECTS_OUTPUTS}, |
| 80 | metadataTags = {OptionMetadataTag.EXPERIMENTAL}, |
| 81 | defaultValue = "false", |
| 82 | help = "If enabled, use a stub script for sh_binary targets.") |
| 83 | public boolean useShBinaryStubScript; |
| 84 | |
lberki | 78651d4 | 2018-04-06 01:52:58 -0700 | [diff] [blame] | 85 | @Override |
| 86 | public Options getHost() { |
| 87 | Options host = (Options) getDefault(); |
| 88 | host.shellExecutable = shellExecutable; |
cushon | 02642c2 | 2018-06-28 01:58:56 -0700 | [diff] [blame] | 89 | host.useShBinaryStubScript = useShBinaryStubScript; |
lberki | 78651d4 | 2018-04-06 01:52:58 -0700 | [diff] [blame] | 90 | return host; |
| 91 | } |
| 92 | } |
| 93 | |
lberki | 78651d4 | 2018-04-06 01:52:58 -0700 | [diff] [blame] | 94 | /** the part of {@link ShellConfiguration} that determines where the shell is. */ |
| 95 | public interface ShellExecutableProvider { |
| 96 | PathFragment getShellExecutable(BuildOptions options); |
| 97 | } |
| 98 | |
| 99 | /** A shell executable whose path is hard-coded. */ |
| 100 | public static ShellExecutableProvider hardcodedShellExecutable(String shell) { |
janakr | d7bec2e | 2018-06-19 09:47:01 -0700 | [diff] [blame] | 101 | return (ShellExecutableProvider & Serializable) (options) -> PathFragment.create(shell); |
lberki | 78651d4 | 2018-04-06 01:52:58 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | /** The loader for {@link ShellConfiguration}. */ |
| 105 | public static class Loader implements ConfigurationFragmentFactory { |
| 106 | private final ShellExecutableProvider shellExecutableProvider; |
lberki | 78651d4 | 2018-04-06 01:52:58 -0700 | [diff] [blame] | 107 | private final ImmutableSet<Class<? extends FragmentOptions>> requiredOptions; |
| 108 | |
| 109 | public Loader(ShellExecutableProvider shellExecutableProvider, |
lberki | 78651d4 | 2018-04-06 01:52:58 -0700 | [diff] [blame] | 110 | Class<? extends FragmentOptions>... requiredOptions) { |
| 111 | this.shellExecutableProvider = shellExecutableProvider; |
lberki | 78651d4 | 2018-04-06 01:52:58 -0700 | [diff] [blame] | 112 | this.requiredOptions = ImmutableSet.copyOf(requiredOptions); |
| 113 | } |
| 114 | |
| 115 | @Nullable |
| 116 | @Override |
gregce | 56eb80b | 2018-05-02 09:04:10 -0700 | [diff] [blame] | 117 | public Fragment create(BuildOptions buildOptions) { |
cushon | 02642c2 | 2018-06-28 01:58:56 -0700 | [diff] [blame] | 118 | Options options = buildOptions.get(Options.class); |
| 119 | return new ShellConfiguration( |
| 120 | shellExecutableProvider.getShellExecutable(buildOptions), |
| 121 | options != null && options.useShBinaryStubScript); |
lberki | 78651d4 | 2018-04-06 01:52:58 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | public static PathFragment determineShellExecutable( |
| 125 | OS os, Options options, PathFragment defaultShell) { |
| 126 | if (options.shellExecutable != null) { |
| 127 | return options.shellExecutable; |
| 128 | } |
| 129 | |
| 130 | // Honor BAZEL_SH env variable for backwards compatibility. |
| 131 | String path = System.getenv("BAZEL_SH"); |
| 132 | if (path != null) { |
| 133 | return PathFragment.create(path); |
| 134 | } |
| 135 | // TODO(ulfjack): instead of using the OS Bazel runs on, we need to use the exec platform, |
| 136 | // which may be different for remote execution. For now, this can be overridden with |
| 137 | // --shell_executable, so at least there's a workaround. |
| 138 | PathFragment result = OS_SPECIFIC_SHELL.get(os); |
| 139 | return result != null ? result : defaultShell; |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public Class<? extends Fragment> creates() { |
| 144 | return ShellConfiguration.class; |
| 145 | } |
| 146 | |
| 147 | @Override |
| 148 | public ImmutableSet<Class<? extends FragmentOptions>> requiredOptions() { |
| 149 | return requiredOptions; |
| 150 | } |
| 151 | } |
| 152 | } |