Skip to content

Commit

Permalink
[Runner] Move some functions outside of giant `generate_compiler_wrap…
Browse files Browse the repository at this point in the history
…pers!` (#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
  • Loading branch information
giordano authored Feb 9, 2025
1 parent 9b5e88d commit bc1c293
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 140 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "BinaryBuilderBase"
uuid = "7f725544-6523-48cd-82d1-3fa08ff4056e"
authors = ["Elliot Saba <staticfloat@gmail.com>"]
version = "1.35.1"
version = "1.35.2"

[deps]
Bzip2_jll = "6e34b625-4abd-537c-b88f-471c36dfa7a0"
Expand Down
285 changes: 146 additions & 139 deletions src/Runner.jl
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,148 @@ function add_cxx_abi(p::AbstractPlatform, flags::Vector{String})
end
end

function wrapper(io::IO,
prog::String;
# Flags that are always prepended
flags::Vector{String} = String[],
# Flags that are prepended if we think we're compiling (e.g. no `-x assembler`)
compile_only_flags::Vector = String[],
# Flags that are postpended if we think we're linking (e.g. no `-c`)
link_only_flags::Vector = String[],
allow_ccache::Bool = true,
no_soft_float::Bool = false,
hash_args::Bool = false,
extra_cmds::String = "",
env::Dict{String,String} = Dict{String,String}(),
unsafe_flags = String[],
sanitize::Bool=false,
lock_microarchitecture::Bool=true,
)
write(io, """
#!/bin/bash
# This compiler wrapper script brought into existence by `generate_compiler_wrappers!()`
if [ "x\${SUPER_VERBOSE}" = "x" ]; then
vrun() { "\$@"; }
else
vrun() { echo -e "\\e[96m\$@\\e[0m" >&2; "\$@"; }
fi
ARGS=( "\$@" )
PRE_FLAGS=()
POST_FLAGS=()
""")

# Sometimes we need to look at the hash of our arguments
if hash_args
write(io, """
ARGS_HASH="\$(echo -n "\$*" | sha1sum | cut -c1-8)"
""")
end

# If we're given always-prepend flags, include them
if !isempty(flags)
println(io)
for cf in flags
println(io, "PRE_FLAGS+=( $cf )")
end
println(io)
end

# If we're given compile-only flags, include them only if `-x assembler` is not provided
if !isempty(compile_only_flags)
println(io)
println(io, "if [[ \" \${ARGS[@]} \" != *' -x assembler '* ]]; then")
for cf in compile_only_flags
println(io, " PRE_FLAGS+=( $cf )")
end
println(io, "fi")
println(io)
end

# If we're given link-only flags, include them only if `-c` or other link-disablers are not provided.
if !isempty(link_only_flags)
println(io)
println(io, "if [[ \" \${ARGS[@]} \" != *' -c '* ]] && [[ \" \${ARGS[@]} \" != *' -E '* ]] && [[ \" \${ARGS[@]} \" != *' -M '* ]] && [[ \" \${ARGS[@]} \" != *' -fsyntax-only '* ]]; then")
for lf in link_only_flags
println(io, " POST_FLAGS+=( $lf )")
end
println(io, "fi")
println(io)
end

# If we're given both -fsanitize= and -Wl,--no-undefined, then try turning
# the latter into a warning rather than an error.
if sanitize
println(io, """
if [[ " \${ARGS[@]} " == *"-Wl,--no-undefined"* ]]; then
PRE_FLAGS+=("-Wl,--warn-unresolved-symbols")
fi
""")
end

# Insert extra commands from the user (usually some kind of conditional setting
# of PRE_FLAGS and POST_FLAGS)
println(io)
write(io, extra_cmds)
println(io)

for (name, val) in env
write(io, "export $(name)=\"$(val)\"\n")
end

# TODO: improve this check
if lock_microarchitecture
write(io, raw"""
if [[ " ${ARGS[@]} " == *"-march="* ]]; then
echo "BinaryBuilder: Cannot force an architecture via -march" >&2
exit 1
fi
""")
println(io)
end

if no_soft_float
write(io, raw"""
if [[ " ${ARGS[@]} " == *"-mfloat-abi=soft"* ]]; then
echo "BinaryBuilder: ${target} platform does not support soft-float ABI (-mfloat-abi=soft)" >&2
exit 1
fi
""")
println(io)
end

if length(unsafe_flags) >= 1
write(io, """
if [[ "\${ARGS[@]}" =~ \"$(join(unsafe_flags, "\"|\""))\" ]]; then
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
exit 1
fi
""")
println(io)
end

if allow_ccache
write(io, """
# Override `\${CCACHE}` setting from the outside.
CCACHE=""
if [[ \${USE_CCACHE} == "true" ]]; then
CCACHE="ccache"
fi
""")
end
# Don't evaluate `${CCACHE}` at all if not allowed in the first place.
write(io, """
vrun $(allow_ccache ? "\${CCACHE} " : "")$(prog) "\${PRE_FLAGS[@]}" "\${ARGS[@]}" "\${POST_FLAGS[@]}"
""")
end

# Write out a bunch of common tools
for tool in (:cpp, :ld, :nm, :libtool, :objcopy, :objdump, :otool,
:strip, :install_name_tool, :dlltool, :windres, :winmc, :lipo)
@eval $(tool)(io::IO, p::AbstractPlatform) = $(wrapper)(io, string("/opt/", aatriplet(p), "/bin/", aatriplet(p), "-", $(string(tool))); allow_ccache=false)
end

"""
generate_compiler_wrappers!(platform::AbstractPlatform; bin_path::AbstractString,
host_platform::AbstractPlatform = $(repr(default_host_platform)),
Expand Down Expand Up @@ -198,140 +340,6 @@ function generate_compiler_wrappers!(platform::AbstractPlatform; bin_path::Abstr
target = aatriplet(platform)
host_target = aatriplet(host_platform)


function wrapper(io::IO,
prog::String;
# Flags that are always prepended
flags::Vector{String} = String[],
# Flags that are prepended if we think we're compiling (e.g. no `-x assembler`)
compile_only_flags::Vector = String[],
# Flags that are postpended if we think we're linking (e.g. no `-c`)
link_only_flags::Vector = String[],
allow_ccache::Bool = true,
no_soft_float::Bool = false,
hash_args::Bool = false,
extra_cmds::String = "",
env::Dict{String,String} = Dict{String,String}(),
unsafe_flags = String[])
write(io, """
#!/bin/bash
# This compiler wrapper script brought into existence by `generate_compiler_wrappers!()`
if [ "x\${SUPER_VERBOSE}" = "x" ]; then
vrun() { "\$@"; }
else
vrun() { echo -e "\\e[96m\$@\\e[0m" >&2; "\$@"; }
fi
ARGS=( "\$@" )
PRE_FLAGS=()
POST_FLAGS=()
""")

# Sometimes we need to look at the hash of our arguments
if hash_args
write(io, """
ARGS_HASH="\$(echo -n "\$*" | sha1sum | cut -c1-8)"
""")
end

# If we're given always-prepend flags, include them
if !isempty(flags)
println(io)
for cf in flags
println(io, "PRE_FLAGS+=( $cf )")
end
println(io)
end

# If we're given compile-only flags, include them only if `-x assembler` is not provided
if !isempty(compile_only_flags)
println(io)
println(io, "if [[ \" \${ARGS[@]} \" != *' -x assembler '* ]]; then")
for cf in compile_only_flags
println(io, " PRE_FLAGS+=( $cf )")
end
println(io, "fi")
println(io)
end

# If we're given link-only flags, include them only if `-c` or other link-disablers are not provided.
if !isempty(link_only_flags)
println(io)
println(io, "if [[ \" \${ARGS[@]} \" != *' -c '* ]] && [[ \" \${ARGS[@]} \" != *' -E '* ]] && [[ \" \${ARGS[@]} \" != *' -M '* ]] && [[ \" \${ARGS[@]} \" != *' -fsyntax-only '* ]]; then")
for lf in link_only_flags
println(io, " POST_FLAGS+=( $lf )")
end
println(io, "fi")
println(io)
end

# If we're given both -fsanitize= and -Wl,--no-undefined, then try turning
# the latter into a warning rather than an error.
if sanitize(platform) != nothing
println(io, """
if [[ " \${ARGS[@]} " == *"-Wl,--no-undefined"* ]]; then
PRE_FLAGS+=("-Wl,--warn-unresolved-symbols")
fi
""")
end

# Insert extra commands from the user (usually some kind of conditional setting
# of PRE_FLAGS and POST_FLAGS)
println(io)
write(io, extra_cmds)
println(io)

for (name, val) in env
write(io, "export $(name)=\"$(val)\"\n")
end

# TODO: improve this check
if lock_microarchitecture
write(io, raw"""
if [[ " ${ARGS[@]} " == *"-march="* ]]; then
echo "BinaryBuilder: Cannot force an architecture via -march" >&2
exit 1
fi
""")
println(io)
end

if no_soft_float
write(io, raw"""
if [[ " ${ARGS[@]} " == *"-mfloat-abi=soft"* ]]; then
echo "BinaryBuilder: ${target} platform does not support soft-float ABI (-mfloat-abi=soft)" >&2
exit 1
fi
""")
println(io)
end

if length(unsafe_flags) >= 1
write(io, """
if [[ "\${ARGS[@]}" =~ \"$(join(unsafe_flags, "\"|\""))\" ]]; then
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
exit 1
fi
""")
println(io)
end

if allow_ccache
write(io, """
# Override `\${CCACHE}` setting from the outside.
CCACHE=""
if [[ \${USE_CCACHE} == "true" ]]; then
CCACHE="ccache"
fi
""")
end
# Don't evaluate `${CCACHE}` at all if not allowed in the first place.
write(io, """
vrun $(allow_ccache ? "\${CCACHE} " : "")$(prog) "\${PRE_FLAGS[@]}" "\${ARGS[@]}" "\${POST_FLAGS[@]}"
""")
end

# Helper invocations
target_tool(io::IO, tool::String, args...; kwargs...) = wrapper(io, "/opt/$(target)/bin/$(target)-$(tool)", args...; kwargs...)
llvm_tool(io::IO, tool::String, args...; kwargs...) = wrapper(io, "/opt/$(host_target)/bin/llvm-$(tool)", args...; kwargs...)
Expand Down Expand Up @@ -637,6 +645,8 @@ function generate_compiler_wrappers!(platform::AbstractPlatform; bin_path::Abstr
no_soft_float=arch(p) in ("armv6l", "armv7l"),
# Override `LD_LIBRARY_PATH` to avoid external settings mess it up.
env=Dict("LD_LIBRARY_PATH"=>ld_library_path(platform, host_platform; csl_paths=false)),
sanitize=!isnothing(sanitize(p)),
lock_microarchitecture,
)
end

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

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

2 comments on commit bc1c293

@giordano
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/124616

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.35.2 -m "<description of version>" bc1c293e367714537136d22ece9d81a82731b009
git push origin v1.35.2

Please sign in to comment.