-
Notifications
You must be signed in to change notification settings - Fork 3
/
extract_compile_command.cc
122 lines (109 loc) · 3.73 KB
/
extract_compile_command.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
* Copyright 2016 The Kythe Authors. All rights reserved.
* Modified by NTechLab ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http:https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <algorithm>
#include <string>
#include <vector>
#include "rapidjson/stringbuffer.h"
#include "rapidjson/writer.h"
#include "google/protobuf/io/coded_stream.h"
#include "google/protobuf/io/zero_copy_stream.h"
#include "google/protobuf/io/zero_copy_stream_impl.h"
#include "google/protobuf/stubs/common.h"
#include "extra_actions_base.pb.h"
namespace {
bool ReadExtraAction(const char *path, blaze::ExtraActionInfo *info, blaze::CppCompileInfo *cpp_info)
{
int fd = open(path, O_RDONLY, S_IREAD | S_IWRITE);
if (fd < 0) {
perror("Failed to open input: ");
return false;
}
google::protobuf::io::FileInputStream file_input(fd);
file_input.SetCloseOnDelete(true);
google::protobuf::io::CodedInputStream input(&file_input);
if (!info->ParseFromCodedStream(&input))
return false;
if (!info->HasExtension(blaze::CppCompileInfo::cpp_compile_info))
return false;
*cpp_info = info->GetExtension(blaze::CppCompileInfo::cpp_compile_info);
return true;
}
std::string JoinCommand(const std::vector<std::string> &command)
{
std::string output;
if (command.empty())
return output;
// TODO(shahms): Deal with embedded spaces and quotes.
auto iter = command.begin();
output = *iter++;
for (; iter != command.end(); ++iter) {
output += " " + *iter;
}
return output;
}
std::string FormatCompilationCommand(const std::string &source_file, const std::vector<std::string> &command)
{
rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
writer.StartObject();
writer.Key("file");
writer.String(source_file.c_str());
writer.Key("directory");
writer.String("@BAZEL_ROOT@");
writer.Key("command");
writer.String(JoinCommand(command).c_str());
writer.EndObject();
return buffer.GetString();
}
}; // namespace
int main(int argc, char *argv[])
{
if (argc != 3) {
fprintf(stderr, "Usage: %s extra-action-file output-file\n", argv[0]);
return 1;
}
const char *extra_action_file = argv[1];
const char *output_file = argv[2];
blaze::ExtraActionInfo info;
blaze::CppCompileInfo cpp_info;
if (!ReadExtraAction(extra_action_file, &info, &cpp_info))
return 1;
std::vector<std::string> args;
args.push_back(cpp_info.tool());
args.insert(args.end(), cpp_info.compiler_option().begin(), cpp_info.compiler_option().end());
if (std::find(args.begin(), args.end(), "-c") == args.end()) {
args.push_back("-c");
args.push_back(cpp_info.source_file());
}
if (std::find(args.begin(), args.end(), "-o") == args.end()) {
args.push_back("-o");
args.push_back(cpp_info.output_file());
}
FILE *fh = fopen(output_file, "w");
if (!fh) {
fprintf(stderr, "Failed to open output file (%s) for writing\n", output_file);
return 1;
}
fputs(FormatCompilationCommand(cpp_info.source_file(), args).c_str(), fh);
fclose(fh);
return 0;
}