blob: 01d9511c74b40827471e62b641e0743d10b9acda [file] [log] [blame]
Yun Peng09dd8c02017-07-21 15:57:05 +02001// Copyright 2017 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
15#include <sstream>
16#include <string>
17#include <vector>
18
19#include "src/tools/launcher/bash_launcher.h"
20#include "src/tools/launcher/util/launcher_util.h"
21
22namespace bazel {
23namespace launcher {
24
25using std::ostringstream;
26using std::string;
27using std::vector;
28
Yun Peng54c5c5c2017-08-24 17:40:22 +020029static constexpr const char* BASH_BIN_PATH = "bash_bin_path";
30
Yun Peng09dd8c02017-07-21 15:57:05 +020031ExitCode BashBinaryLauncher::Launch() {
32 string bash_binary = this->GetLaunchInfoByKey(BASH_BIN_PATH);
33 // If specified bash binary path doesn't exist, then fall back to
34 // bash.exe and hope it's in PATH.
Laszlo Csomor837e1b32017-08-03 10:50:29 +020035 if (!DoesFilePathExist(bash_binary.c_str())) {
Yun Peng09dd8c02017-07-21 15:57:05 +020036 bash_binary = "bash.exe";
37 }
38
39 vector<string> origin_args = this->GetCommandlineArguments();
40 ostringstream bash_command;
Yun Penge6d20772017-08-18 15:40:54 +020041 string bash_main_file = GetBinaryPathWithoutExtension(origin_args[0]);
Yun Peng09dd8c02017-07-21 15:57:05 +020042 bash_command << GetEscapedArgument(bash_main_file);
43 for (int i = 1; i < origin_args.size(); i++) {
44 bash_command << ' ';
45 bash_command << GetEscapedArgument(origin_args[i]);
46 }
47
48 vector<string> args;
49 args.push_back("-c");
50 args.push_back(bash_command.str());
51 return this->LaunchProcess(bash_binary, args);
52}
53
54} // namespace launcher
55} // namespace bazel