-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathgcc_wrapper.lua
More file actions
165 lines (138 loc) · 4.99 KB
/
Copy pathgcc_wrapper.lua
File metadata and controls
165 lines (138 loc) · 4.99 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
-- match(.*(gcc|g\+\+|clang|clang\+\+).*)
-------------------------------------------------------------------------------
-- This is a re-implementation of the C++ class gcc_wrapper_t.
-------------------------------------------------------------------------------
require_std("io")
require_std("os")
require_std("string")
require_std("table")
require_std("bcache")
-------------------------------------------------------------------------------
-- Internal helper functions.
-------------------------------------------------------------------------------
local function make_preprocessor_cmd (args, preprocessed_file)
local preprocess_args = {}
-- Drop arguments that we do not want/need.
local drop_next_arg = false
for i, arg in ipairs(args) do
local drop_this_arg = drop_next_arg
drop_next_arg = false
if arg == "-c" then
drop_this_arg = true
elseif arg == "-o" then
drop_this_arg = true
drop_next_arg = true
end
if not drop_this_arg then
table.insert(preprocess_args, arg)
end
end
-- Append the required arguments for producing preprocessed output.
table.insert(preprocess_args, "-E")
table.insert(preprocess_args, "-P")
table.insert(preprocess_args, "-o")
table.insert(preprocess_args, preprocessed_file)
return preprocess_args
end
local function is_source_file (path)
local ext = bcache.get_extension(path):lower()
return (ext == ".cpp") or (ext == ".cc") or (ext == ".cxx") or (ext == ".c")
end
-------------------------------------------------------------------------------
-- Wrapper interface implementation.
-------------------------------------------------------------------------------
function get_capabilities ()
-- We can use hard links with GCC since it will never overwrite already
-- existing files.
return { "hard_links" }
end
function preprocess_source ()
-- Check if this is a compilation command that we support.
local is_object_compilation = false
local has_object_output = false
for i, arg in ipairs(ARGS) do
if arg == "-c" then
is_object_compilation = true
elseif arg == "-o" then
has_object_output = true
elseif arg:sub(1, 1) == "@" then
error("Response files are currently not supported.")
end
end
if (not is_object_compilation) or (not has_object_output) then
error("Unsupported complation command.")
end
-- Run the preprocessor step.
local preprocessed_file = os.tmpname()
local preprocessor_args = make_preprocessor_cmd(ARGS, preprocessed_file)
local result = bcache.run(preprocessor_args)
if result.return_code ~= 0 then
os.remove(preprocessed_file)
error("Preprocessing command was unsuccessful.")
end
-- Read and return the preprocessed file.
local f = assert(io.open(preprocessed_file, "rb"))
local preprocessed_source = f:read("*all")
f:close()
os.remove(preprocessed_file)
return preprocessed_source
end
function get_relevant_arguments ()
local filtered_args = {}
-- The first argument is the compiler binary without the path.
table.insert(filtered_args, bcache.get_file_part(ARGS[1]))
-- Note: We always skip the first arg since we have handled it already.
local skip_next_arg = true
for i, arg in ipairs(ARGS) do
if not skip_next_arg then
-- Does this argument specify a file (we don't want to hash those).
local is_arg_plus_file_name = (arg == "-I") or (arg == "-MF") or
(arg == "-MT") or (arg == "-MQ") or
(arg == "-o")
-- Generally unwanted argument (things that will not change how we go
-- from preprocessed code to binary object files)?
local first_two_chars = arg:sub(1, 2)
local is_unwanted_arg = (first_two_chars == "-I") or
(first_two_chars == "-D") or
(first_two_chars == "-M") or
is_source_file(arg)
if is_arg_plus_file_name then
skip_next_arg = true
elseif not is_unwanted_arg then
table.insert(filtered_args, arg)
end
else
skip_next_arg = false
end
end
return filtered_args
end
function get_program_id ()
-- TODO(m): Add things like executable file size too.
-- Get the version string for the compiler.
local result = bcache.run({ARGS[1], "--version"})
if result.return_code ~= 0 then
error("Unable to get the compiler version information string.")
end
return result.std_out
end
function get_build_files ()
local files = {}
local found_object_file = false
for i = 2, #ARGS do
local next_idx = i + 1
if (ARGS[i] == "-o") and (next_idx <= #ARGS) then
if found_object_file then
error("Only a single target object file can be specified.")
end
files["object"] = ARGS[next_idx]
found_object_file = true
elseif (ARGS[i] == "-ftest-coverage") then
error("Code coverage data is currently not supported.")
end
end
if not found_object_file then
error("Unable to get the target object file.")
end
return files
end