forked from asheplyakov/buildcache
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathti_c6x_wrapper.lua
More file actions
197 lines (166 loc) · 5.92 KB
/
Copy pathti_c6x_wrapper.lua
File metadata and controls
197 lines (166 loc) · 5.92 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
-- match(.*cl6x.*)
-------------------------------------------------------------------------------
-- This is a wrapper for the TI C6000 DSP compiler.
-------------------------------------------------------------------------------
require_std("io")
require_std("os")
require_std("string")
require_std("table")
require_std("bcache")
-------------------------------------------------------------------------------
-- Internal helper functions.
-------------------------------------------------------------------------------
local function starts_with (s, substr)
return s:sub(1, #substr) == substr
end
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 == "--compile_only") or
starts_with(arg, "--output_file=") or
starts_with(arg, "-pp") or
starts_with(arg, "--preproc_") then
drop_this_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, "--preproc_only")
table.insert(preprocess_args, "--output_file=" .. preprocessed_file)
return preprocess_args
end
local function append_response_file (args, file_name)
-- Load the response file into a string.
local f = assert(io.open(file_name, "rb"))
local args_string = f:read("*all")
f:close()
args_string = args_string:gsub("\n", " ")
-- Split the arguments.
local new_args = bcache.split_args(args_string)
for i, arg in ipairs(new_args) do
table.insert(args, arg)
end
return args
end
-------------------------------------------------------------------------------
-- Wrapper interface implementation.
-------------------------------------------------------------------------------
function resolve_args ()
-- Iterate over all args and load any response files that we encounter.
local new_args = {}
for i, arg in ipairs(ARGS) do
local response_file
if starts_with(arg, "--cmd_file=") then
response_file = arg:sub(12)
elseif starts_with(arg, "-@") then
response_file = arg:sub(3)
end
if response_file ~= nil then
new_args = append_response_file(new_args, response_file)
else
table.insert(new_args, arg)
end
end
-- Replace the old args with the new args.
ARGS = new_args
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 == "--compile_only" then
is_object_compilation = true
elseif starts_with(arg, "--output_file=") then
has_object_output = true
elseif starts_with(arg, "--cmd_file=") or starts_with(arg, "-@") 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
-- 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
starts_with(arg, "--include") or
starts_with(arg, "--preinclude=") or
(first_two_chars == "-D") or
starts_with(arg, "--define=") or
starts_with(arg, "--c_file=") or
starts_with(arg, "--cpp_file=") or
starts_with(arg, "--output_file=")
if 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 help string from the compiler (it includes the version string).
local result = bcache.run({ARGS[1], "--help"})
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
local found_dep_file = false
for i = 2, #ARGS do
local next_idx = i + 1
if starts_with(ARGS[i], "--output_file=") then
if found_object_file then
error("Only a single target object file can be specified.")
end
files["object"] = ARGS[i]:sub(15)
found_object_file = true
elseif starts_with(ARGS[i], "-ppd=") or starts_with(ARGS[i], "--preproc_dependency=") then
if found_dep_file then
error("Only a single dependency file can be specified.")
end
local eq = ARGS[i]:find("=")
files["dep"] = ARGS[i]:sub(eq + 1)
found_dep_file = true
end
end
if not found_object_file then
error("Unable to get the target object file.")
end
return files
end