Skip to content

Commit bc1c293

Browse files
authored
[Runner] Move some functions outside of giant generate_compiler_wrappers! (#410)
* [Runner] Move some functions outside of giant `generate_compiler_wrappers!` * [Runner] Pass new arguments to `wrapper` which were previously taken from outer scope. Sigh
1 parent 9b5e88d commit bc1c293

File tree

2 files changed

+147
-140
lines changed

2 files changed

+147
-140
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "BinaryBuilderBase"
22
uuid = "7f725544-6523-48cd-82d1-3fa08ff4056e"
33
authors = ["Elliot Saba <staticfloat@gmail.com>"]
4-
version = "1.35.1"
4+
version = "1.35.2"
55

66
[deps]
77
Bzip2_jll = "6e34b625-4abd-537c-b88f-471c36dfa7a0"

src/Runner.jl

Lines changed: 146 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,148 @@ function add_cxx_abi(p::AbstractPlatform, flags::Vector{String})
157157
end
158158
end
159159

160+
function wrapper(io::IO,
161+
prog::String;
162+
# Flags that are always prepended
163+
flags::Vector{String} = String[],
164+
# Flags that are prepended if we think we're compiling (e.g. no `-x assembler`)
165+
compile_only_flags::Vector = String[],
166+
# Flags that are postpended if we think we're linking (e.g. no `-c`)
167+
link_only_flags::Vector = String[],
168+
allow_ccache::Bool = true,
169+
no_soft_float::Bool = false,
170+
hash_args::Bool = false,
171+
extra_cmds::String = "",
172+
env::Dict{String,String} = Dict{String,String}(),
173+
unsafe_flags = String[],
174+
sanitize::Bool=false,
175+
lock_microarchitecture::Bool=true,
176+
)
177+
write(io, """
178+
#!/bin/bash
179+
# This compiler wrapper script brought into existence by `generate_compiler_wrappers!()`
180+
181+
if [ "x\${SUPER_VERBOSE}" = "x" ]; then
182+
vrun() { "\$@"; }
183+
else
184+
vrun() { echo -e "\\e[96m\$@\\e[0m" >&2; "\$@"; }
185+
fi
186+
187+
ARGS=( "\$@" )
188+
PRE_FLAGS=()
189+
POST_FLAGS=()
190+
""")
191+
192+
# Sometimes we need to look at the hash of our arguments
193+
if hash_args
194+
write(io, """
195+
ARGS_HASH="\$(echo -n "\$*" | sha1sum | cut -c1-8)"
196+
""")
197+
end
198+
199+
# If we're given always-prepend flags, include them
200+
if !isempty(flags)
201+
println(io)
202+
for cf in flags
203+
println(io, "PRE_FLAGS+=( $cf )")
204+
end
205+
println(io)
206+
end
207+
208+
# If we're given compile-only flags, include them only if `-x assembler` is not provided
209+
if !isempty(compile_only_flags)
210+
println(io)
211+
println(io, "if [[ \" \${ARGS[@]} \" != *' -x assembler '* ]]; then")
212+
for cf in compile_only_flags
213+
println(io, " PRE_FLAGS+=( $cf )")
214+
end
215+
println(io, "fi")
216+
println(io)
217+
end
218+
219+
# If we're given link-only flags, include them only if `-c` or other link-disablers are not provided.
220+
if !isempty(link_only_flags)
221+
println(io)
222+
println(io, "if [[ \" \${ARGS[@]} \" != *' -c '* ]] && [[ \" \${ARGS[@]} \" != *' -E '* ]] && [[ \" \${ARGS[@]} \" != *' -M '* ]] && [[ \" \${ARGS[@]} \" != *' -fsyntax-only '* ]]; then")
223+
for lf in link_only_flags
224+
println(io, " POST_FLAGS+=( $lf )")
225+
end
226+
println(io, "fi")
227+
println(io)
228+
end
229+
230+
# If we're given both -fsanitize= and -Wl,--no-undefined, then try turning
231+
# the latter into a warning rather than an error.
232+
if sanitize
233+
println(io, """
234+
if [[ " \${ARGS[@]} " == *"-Wl,--no-undefined"* ]]; then
235+
PRE_FLAGS+=("-Wl,--warn-unresolved-symbols")
236+
fi
237+
""")
238+
end
239+
240+
# Insert extra commands from the user (usually some kind of conditional setting
241+
# of PRE_FLAGS and POST_FLAGS)
242+
println(io)
243+
write(io, extra_cmds)
244+
println(io)
245+
246+
for (name, val) in env
247+
write(io, "export $(name)=\"$(val)\"\n")
248+
end
249+
250+
# TODO: improve this check
251+
if lock_microarchitecture
252+
write(io, raw"""
253+
if [[ " ${ARGS[@]} " == *"-march="* ]]; then
254+
echo "BinaryBuilder: Cannot force an architecture via -march" >&2
255+
exit 1
256+
fi
257+
""")
258+
println(io)
259+
end
260+
261+
if no_soft_float
262+
write(io, raw"""
263+
if [[ " ${ARGS[@]} " == *"-mfloat-abi=soft"* ]]; then
264+
echo "BinaryBuilder: ${target} platform does not support soft-float ABI (-mfloat-abi=soft)" >&2
265+
exit 1
266+
fi
267+
""")
268+
println(io)
269+
end
270+
271+
if length(unsafe_flags) >= 1
272+
write(io, """
273+
if [[ "\${ARGS[@]}" =~ \"$(join(unsafe_flags, "\"|\""))\" ]]; then
274+
echo -e \"BinaryBuilder error: You used one or more of the unsafe flags: $(join(unsafe_flags, ", "))\\nThis is not allowed, please remove all unsafe flags from your build script to continue.\" >&2
275+
exit 1
276+
fi
277+
""")
278+
println(io)
279+
end
280+
281+
if allow_ccache
282+
write(io, """
283+
# Override `\${CCACHE}` setting from the outside.
284+
CCACHE=""
285+
if [[ \${USE_CCACHE} == "true" ]]; then
286+
CCACHE="ccache"
287+
fi
288+
""")
289+
end
290+
# Don't evaluate `${CCACHE}` at all if not allowed in the first place.
291+
write(io, """
292+
vrun $(allow_ccache ? "\${CCACHE} " : "")$(prog) "\${PRE_FLAGS[@]}" "\${ARGS[@]}" "\${POST_FLAGS[@]}"
293+
""")
294+
end
295+
296+
# Write out a bunch of common tools
297+
for tool in (:cpp, :ld, :nm, :libtool, :objcopy, :objdump, :otool,
298+
:strip, :install_name_tool, :dlltool, :windres, :winmc, :lipo)
299+
@eval $(tool)(io::IO, p::AbstractPlatform) = $(wrapper)(io, string("/opt/", aatriplet(p), "/bin/", aatriplet(p), "-", $(string(tool))); allow_ccache=false)
300+
end
301+
160302
"""
161303
generate_compiler_wrappers!(platform::AbstractPlatform; bin_path::AbstractString,
162304
host_platform::AbstractPlatform = $(repr(default_host_platform)),
@@ -198,140 +340,6 @@ function generate_compiler_wrappers!(platform::AbstractPlatform; bin_path::Abstr
198340
target = aatriplet(platform)
199341
host_target = aatriplet(host_platform)
200342

201-
202-
function wrapper(io::IO,
203-
prog::String;
204-
# Flags that are always prepended
205-
flags::Vector{String} = String[],
206-
# Flags that are prepended if we think we're compiling (e.g. no `-x assembler`)
207-
compile_only_flags::Vector = String[],
208-
# Flags that are postpended if we think we're linking (e.g. no `-c`)
209-
link_only_flags::Vector = String[],
210-
allow_ccache::Bool = true,
211-
no_soft_float::Bool = false,
212-
hash_args::Bool = false,
213-
extra_cmds::String = "",
214-
env::Dict{String,String} = Dict{String,String}(),
215-
unsafe_flags = String[])
216-
write(io, """
217-
#!/bin/bash
218-
# This compiler wrapper script brought into existence by `generate_compiler_wrappers!()`
219-
220-
if [ "x\${SUPER_VERBOSE}" = "x" ]; then
221-
vrun() { "\$@"; }
222-
else
223-
vrun() { echo -e "\\e[96m\$@\\e[0m" >&2; "\$@"; }
224-
fi
225-
226-
ARGS=( "\$@" )
227-
PRE_FLAGS=()
228-
POST_FLAGS=()
229-
""")
230-
231-
# Sometimes we need to look at the hash of our arguments
232-
if hash_args
233-
write(io, """
234-
ARGS_HASH="\$(echo -n "\$*" | sha1sum | cut -c1-8)"
235-
""")
236-
end
237-
238-
# If we're given always-prepend flags, include them
239-
if !isempty(flags)
240-
println(io)
241-
for cf in flags
242-
println(io, "PRE_FLAGS+=( $cf )")
243-
end
244-
println(io)
245-
end
246-
247-
# If we're given compile-only flags, include them only if `-x assembler` is not provided
248-
if !isempty(compile_only_flags)
249-
println(io)
250-
println(io, "if [[ \" \${ARGS[@]} \" != *' -x assembler '* ]]; then")
251-
for cf in compile_only_flags
252-
println(io, " PRE_FLAGS+=( $cf )")
253-
end
254-
println(io, "fi")
255-
println(io)
256-
end
257-
258-
# If we're given link-only flags, include them only if `-c` or other link-disablers are not provided.
259-
if !isempty(link_only_flags)
260-
println(io)
261-
println(io, "if [[ \" \${ARGS[@]} \" != *' -c '* ]] && [[ \" \${ARGS[@]} \" != *' -E '* ]] && [[ \" \${ARGS[@]} \" != *' -M '* ]] && [[ \" \${ARGS[@]} \" != *' -fsyntax-only '* ]]; then")
262-
for lf in link_only_flags
263-
println(io, " POST_FLAGS+=( $lf )")
264-
end
265-
println(io, "fi")
266-
println(io)
267-
end
268-
269-
# If we're given both -fsanitize= and -Wl,--no-undefined, then try turning
270-
# the latter into a warning rather than an error.
271-
if sanitize(platform) != nothing
272-
println(io, """
273-
if [[ " \${ARGS[@]} " == *"-Wl,--no-undefined"* ]]; then
274-
PRE_FLAGS+=("-Wl,--warn-unresolved-symbols")
275-
fi
276-
""")
277-
end
278-
279-
# Insert extra commands from the user (usually some kind of conditional setting
280-
# of PRE_FLAGS and POST_FLAGS)
281-
println(io)
282-
write(io, extra_cmds)
283-
println(io)
284-
285-
for (name, val) in env
286-
write(io, "export $(name)=\"$(val)\"\n")
287-
end
288-
289-
# TODO: improve this check
290-
if lock_microarchitecture
291-
write(io, raw"""
292-
if [[ " ${ARGS[@]} " == *"-march="* ]]; then
293-
echo "BinaryBuilder: Cannot force an architecture via -march" >&2
294-
exit 1
295-
fi
296-
""")
297-
println(io)
298-
end
299-
300-
if no_soft_float
301-
write(io, raw"""
302-
if [[ " ${ARGS[@]} " == *"-mfloat-abi=soft"* ]]; then
303-
echo "BinaryBuilder: ${target} platform does not support soft-float ABI (-mfloat-abi=soft)" >&2
304-
exit 1
305-
fi
306-
""")
307-
println(io)
308-
end
309-
310-
if length(unsafe_flags) >= 1
311-
write(io, """
312-
if [[ "\${ARGS[@]}" =~ \"$(join(unsafe_flags, "\"|\""))\" ]]; then
313-
echo -e \"BinaryBuilder error: You used one or more of the unsafe flags: $(join(unsafe_flags, ", "))\\nThis is not allowed, please remove all unsafe flags from your build script to continue.\" >&2
314-
exit 1
315-
fi
316-
""")
317-
println(io)
318-
end
319-
320-
if allow_ccache
321-
write(io, """
322-
# Override `\${CCACHE}` setting from the outside.
323-
CCACHE=""
324-
if [[ \${USE_CCACHE} == "true" ]]; then
325-
CCACHE="ccache"
326-
fi
327-
""")
328-
end
329-
# Don't evaluate `${CCACHE}` at all if not allowed in the first place.
330-
write(io, """
331-
vrun $(allow_ccache ? "\${CCACHE} " : "")$(prog) "\${PRE_FLAGS[@]}" "\${ARGS[@]}" "\${POST_FLAGS[@]}"
332-
""")
333-
end
334-
335343
# Helper invocations
336344
target_tool(io::IO, tool::String, args...; kwargs...) = wrapper(io, "/opt/$(target)/bin/$(target)-$(tool)", args...; kwargs...)
337345
llvm_tool(io::IO, tool::String, args...; kwargs...) = wrapper(io, "/opt/$(host_target)/bin/llvm-$(tool)", args...; kwargs...)
@@ -637,6 +645,8 @@ function generate_compiler_wrappers!(platform::AbstractPlatform; bin_path::Abstr
637645
no_soft_float=arch(p) in ("armv6l", "armv7l"),
638646
# Override `LD_LIBRARY_PATH` to avoid external settings mess it up.
639647
env=Dict("LD_LIBRARY_PATH"=>ld_library_path(platform, host_platform; csl_paths=false)),
648+
sanitize=!isnothing(sanitize(p)),
649+
lock_microarchitecture,
640650
)
641651
end
642652

@@ -650,6 +660,8 @@ function generate_compiler_wrappers!(platform::AbstractPlatform; bin_path::Abstr
650660
no_soft_float=arch(p) in ("armv6l", "armv7l"),
651661
# Override `LD_LIBRARY_PATH` to avoid external settings mess it up.
652662
env=Dict("LD_LIBRARY_PATH"=>ld_library_path(platform, host_platform; csl_paths=false)),
663+
sanitize=!isnothing(sanitize(p)),
664+
lock_microarchitecture,
653665
)
654666
end
655667

@@ -844,11 +856,6 @@ function generate_compiler_wrappers!(platform::AbstractPlatform; bin_path::Abstr
844856
env=Dict("LD_LIBRARY_PATH"=>ld_library_path(platform, host_platform; csl_paths=false)), allow_ccache=false,
845857
)
846858
end
847-
# Write out a bunch of common tools
848-
for tool in (:cpp, :ld, :nm, :libtool, :objcopy, :objdump, :otool,
849-
:strip, :install_name_tool, :dlltool, :windres, :winmc, :lipo)
850-
@eval $(tool)(io::IO, p::AbstractPlatform) = $(wrapper)(io, string("/opt/", aatriplet(p), "/bin/", aatriplet(p), "-", $(string(tool))); allow_ccache=false)
851-
end
852859
as(io::IO, p::AbstractPlatform) =
853860
wrapper(io, string("/opt/", aatriplet(p), "/bin/", aatriplet(p), "-as");
854861
allow_ccache=false,

0 commit comments

Comments
 (0)