blob: 7da070ad05d49ea0ee39d50e72cf7d20247ee455 [file] [log] [blame]
lberki78651d42018-04-06 01:52:58 -07001// 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.
14package com.google.devtools.build.lib.analysis;
15
16import com.google.common.collect.ImmutableMap;
17import com.google.common.collect.ImmutableSet;
18import com.google.devtools.build.lib.analysis.config.BuildConfiguration;
19import com.google.devtools.build.lib.analysis.config.BuildConfiguration.Fragment;
20import com.google.devtools.build.lib.analysis.config.BuildOptions;
lberki78651d42018-04-06 01:52:58 -070021import com.google.devtools.build.lib.analysis.config.ConfigurationFragmentFactory;
22import com.google.devtools.build.lib.analysis.config.FragmentOptions;
lberki78651d42018-04-06 01:52:58 -070023import com.google.devtools.build.lib.util.OS;
24import com.google.devtools.build.lib.util.OptionsUtils.PathFragmentConverter;
25import com.google.devtools.build.lib.vfs.PathFragment;
26import com.google.devtools.common.options.Option;
27import com.google.devtools.common.options.OptionDocumentationCategory;
28import com.google.devtools.common.options.OptionEffectTag;
cushon02642c22018-06-28 01:58:56 -070029import com.google.devtools.common.options.OptionMetadataTag;
janakrd7bec2e2018-06-19 09:47:01 -070030import java.io.Serializable;
lberki78651d42018-04-06 01:52:58 -070031import javax.annotation.Nullable;
32
33/** A configuration fragment that tells where the shell is. */
34public class ShellConfiguration extends BuildConfiguration.Fragment {
lberki78651d42018-04-06 01:52:58 -070035 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;
cushon02642c22018-06-28 01:58:56 -070042 private final boolean useShBinaryStubScript;
lberki78651d42018-04-06 01:52:58 -070043
shahan2ed5ed92018-09-06 10:24:06 -070044 private ShellConfiguration(PathFragment shellExecutable, boolean useShBinaryStubScript) {
lberki78651d42018-04-06 01:52:58 -070045 this.shellExecutable = shellExecutable;
cushon02642c22018-06-28 01:58:56 -070046 this.useShBinaryStubScript = useShBinaryStubScript;
lberki78651d42018-04-06 01:52:58 -070047 }
48
49 public PathFragment getShellExecutable() {
50 return shellExecutable;
51 }
52
cushon02642c22018-06-28 01:58:56 -070053 public boolean useShBinaryStubScript() {
54 return useShBinaryStubScript;
55 }
56
lberki78651d42018-04-06 01:52:58 -070057 /** An option that tells Bazel where the shell is. */
lberki78651d42018-04-06 01:52:58 -070058 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
cushon02642c22018-06-28 01:58:56 -070076 @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
lberki78651d42018-04-06 01:52:58 -070085 @Override
86 public Options getHost() {
87 Options host = (Options) getDefault();
88 host.shellExecutable = shellExecutable;
cushon02642c22018-06-28 01:58:56 -070089 host.useShBinaryStubScript = useShBinaryStubScript;
lberki78651d42018-04-06 01:52:58 -070090 return host;
91 }
92 }
93
lberki78651d42018-04-06 01:52:58 -070094 /** 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) {
janakrd7bec2e2018-06-19 09:47:01 -0700101 return (ShellExecutableProvider & Serializable) (options) -> PathFragment.create(shell);
lberki78651d42018-04-06 01:52:58 -0700102 }
103
104 /** The loader for {@link ShellConfiguration}. */
105 public static class Loader implements ConfigurationFragmentFactory {
106 private final ShellExecutableProvider shellExecutableProvider;
lberki78651d42018-04-06 01:52:58 -0700107 private final ImmutableSet<Class<? extends FragmentOptions>> requiredOptions;
108
109 public Loader(ShellExecutableProvider shellExecutableProvider,
lberki78651d42018-04-06 01:52:58 -0700110 Class<? extends FragmentOptions>... requiredOptions) {
111 this.shellExecutableProvider = shellExecutableProvider;
lberki78651d42018-04-06 01:52:58 -0700112 this.requiredOptions = ImmutableSet.copyOf(requiredOptions);
113 }
114
115 @Nullable
116 @Override
gregce56eb80b2018-05-02 09:04:10 -0700117 public Fragment create(BuildOptions buildOptions) {
cushon02642c22018-06-28 01:58:56 -0700118 Options options = buildOptions.get(Options.class);
119 return new ShellConfiguration(
120 shellExecutableProvider.getShellExecutable(buildOptions),
121 options != null && options.useShBinaryStubScript);
lberki78651d42018-04-06 01:52:58 -0700122 }
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}