Skip to content

Commit 56fda2b

Browse files
committed
[Runner] Move some functions outside of giant generate_compiler_wrappers!
1 parent 9b5e88d commit 56fda2b

File tree

2 files changed

+140
-140
lines changed

2 files changed

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

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-
335340
# Helper invocations
336341
target_tool(io::IO, tool::String, args...; kwargs...) = wrapper(io, "/opt/$(target)/bin/$(target)-$(tool)", args...; kwargs...)
337342
llvm_tool(io::IO, tool::String, args...; kwargs...) = wrapper(io, "/opt/$(host_target)/bin/llvm-$(tool)", args...; kwargs...)
@@ -844,11 +849,6 @@ function generate_compiler_wrappers!(platform::AbstractPlatform; bin_path::Abstr
844849
env=Dict("LD_LIBRARY_PATH"=>ld_library_path(platform, host_platform; csl_paths=false)), allow_ccache=false,
845850
)
846851
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
852852
as(io::IO, p::AbstractPlatform) =
853853
wrapper(io, string("/opt/", aatriplet(p), "/bin/", aatriplet(p), "-as");
854854
allow_ccache=false,

0 commit comments

Comments
 (0)