Skip to content

RFC: Improve the ability to override the cflags(), ldflags(), etc. #920

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 49 additions & 12 deletions src/PackageCompiler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,12 @@
r"stdlibs = \[(.*?)\]"s => string("stdlibs = [", join(":" .* stdlibs, ",\n"), "]"))
end

function create_fresh_base_sysimage(stdlibs::Vector{String}; cpu_target::String, sysimage_build_args::Cmd)
function create_fresh_base_sysimage(stdlibs::Vector{String}; cpu_target::String, sysimage_build_args::Cmd,

Check warning on line 223 in src/PackageCompiler.jl

View check run for this annotation

Codecov / codecov/patch

src/PackageCompiler.jl#L223

Added line #L223 was not covered by tests
cflags=cflags(),
ldflags=ldflags(),
ldlibs=ldlibs(),
march=march())

tmp = mktempdir()
sysimg_source_path = Base.find_source_file("sysimg.jl")
base_dir = dirname(sysimg_source_path)
Expand Down Expand Up @@ -269,7 +274,11 @@
tmp_sys_sl;
version=nothing,
soname=nothing,
compat_level="major")
compat_level="major",
cflags=cflags,
ldflags=ldflags,
ldlibs=ldlibs,
march=march)

finally
rm(new_sysimage_source_path; force=true)
Expand Down Expand Up @@ -517,6 +526,14 @@

- `sysimage_build_args::Cmd`: A set of command line options that is used in the Julia process building the sysimage,
for example `-O1 --check-bounds=yes`.

- `cflags::String`: The compiler flags for use when compiling C code that will interact with the Julia runtime.

- `ldflags::String`: The linker flags necessary for linking a C program with the Julia runtime.

- `ldlibs::String`: The linker flags for the specific Julia libraries that must be linked with a C program, e.g. "libjulia"

- `march::String`: The target machine architecture and extension used to compile the sysimg or library.
"""
function create_sysimage(packages::Union{Nothing, Symbol, Vector{String}, Vector{Symbol}}=nothing;
sysimage_path::String,
Expand All @@ -537,6 +554,10 @@
soname=nothing,
compat_level::String="major",
extra_precompiles::String = "",
cflags=cflags(),
ldflags=ldflags(),
ldlibs=ldlibs(),
march=march()
)
# We call this at the very beginning to make sure that the user has a compiler available. Therefore, if no compiler
# is found, we throw an error immediately, instead of making the user wait a while before the error is thrown.
Expand Down Expand Up @@ -575,7 +596,7 @@
error("cannot specify `base_sysimage` when `incremental=false`")
end
sysimage_stdlibs = filter_stdlibs ? gather_stdlibs_project(ctx) : stdlibs_in_sysimage()
base_sysimage = create_fresh_base_sysimage(sysimage_stdlibs; cpu_target, sysimage_build_args)
base_sysimage = create_fresh_base_sysimage(sysimage_stdlibs; cpu_target, sysimage_build_args, cflags, ldflags, ldlibs, march)

Check warning on line 599 in src/PackageCompiler.jl

View check run for this annotation

Codecov / codecov/patch

src/PackageCompiler.jl#L599

Added line #L599 was not covered by tests
else
base_sysimage = something(base_sysimage, unsafe_string(Base.JLOptions().image_file))
end
Expand Down Expand Up @@ -659,7 +680,11 @@
sysimage_path;
compat_level,
version,
soname)
soname,
cflags,
ldflags,
ldlibs,
march)

rm(object_file; force=true)

Expand All @@ -679,7 +704,11 @@
sysimage_path::String;
version,
compat_level::String,
soname::Union{Nothing, String})
soname::Union{Nothing, String},
cflags=cflags(),
ldflags=ldflags(),
ldlibs=ldlibs(),
march=march())

if soname === nothing && (Sys.isunix() && !Sys.isapple())
soname = basename(sysimage_path)
Expand All @@ -688,7 +717,7 @@
# Prevent compiler from stripping all symbols from the shared lib.
o_file_flags = Sys.isapple() ? `-Wl,-all_load $object_files` : `-Wl,--whole-archive $object_files -Wl,--no-whole-archive`
extra = get_extra_linker_flags(version, compat_level, soname)
cmd = `$(bitflag()) $(march()) -shared -L$(julia_libdir()) -L$(julia_private_libdir()) -o $sysimage_path $o_file_flags $(Base.shell_split(ldlibs())) $extra`
cmd = `$(bitflag()) $(march) -shared -L$(julia_libdir()) -L$(julia_private_libdir()) -o $sysimage_path $o_file_flags $(Base.shell_split(ldlibs)) $extra`

Check warning on line 720 in src/PackageCompiler.jl

View check run for this annotation

Codecov / codecov/patch

src/PackageCompiler.jl#L720

Added line #L720 was not covered by tests
run_compiler(cmd; cplusplus=true)
return nothing
end
Expand All @@ -714,12 +743,16 @@
return extra
end

function compile_c_init_julia(julia_init_c_file::String, sysimage_name::String, include_dir::String)
function compile_c_init_julia(julia_init_c_file::String, sysimage_name::String, include_dir::String;

Check warning on line 746 in src/PackageCompiler.jl

View check run for this annotation

Codecov / codecov/patch

src/PackageCompiler.jl#L746

Added line #L746 was not covered by tests
cflags=cflags(),
ldflags=ldflags(),
ldlibs=ldlibs(),
march=march())
@debug "Compiling $julia_init_c_file"
flags = Base.shell_split(cflags())
flags = Base.shell_split(cflags)

Check warning on line 752 in src/PackageCompiler.jl

View check run for this annotation

Codecov / codecov/patch

src/PackageCompiler.jl#L752

Added line #L752 was not covered by tests

o_init_file = splitext(julia_init_c_file)[1] * ".o"
cmd = `-c -I$include_dir -DJULIAC_PROGRAM_LIBNAME=$(repr(sysimage_name)) $TLS_SYNTAX $(bitflag()) $flags $(march()) -o $o_init_file $julia_init_c_file`
cmd = `-c -I$include_dir -DJULIAC_PROGRAM_LIBNAME=$(repr(sysimage_name)) $TLS_SYNTAX $(bitflag()) $flags $(march) -o $o_init_file $julia_init_c_file`

Check warning on line 755 in src/PackageCompiler.jl

View check run for this annotation

Codecov / codecov/patch

src/PackageCompiler.jl#L755

Added line #L755 was not covered by tests
run_compiler(cmd)
return o_init_file
end
Expand Down Expand Up @@ -897,11 +930,15 @@

function create_executable_from_sysimg(exe_path::String,
c_driver_program::String,
julia_main::String)
julia_main::String;
cflags=cflags(),
ldflags=ldflags(),
ldlibs=ldlibs(),
march=march())
c_driver_program = abspath(c_driver_program)
mkpath(dirname(exe_path))
flags = Base.shell_split(join((cflags(), ldflags(), ldlibs()), " "))
m = something(march(), ``)
flags = Base.shell_split(join((cflags, ldflags, ldlibs), " "))
m = something(march, ``)

Check warning on line 941 in src/PackageCompiler.jl

View check run for this annotation

Codecov / codecov/patch

src/PackageCompiler.jl#L940-L941

Added lines #L940 - L941 were not covered by tests
cmd = `-DJULIA_MAIN=\"$julia_main\" $TLS_SYNTAX $(bitflag()) $m -o $(exe_path) $(c_driver_program) $(rpath_executable()) $flags`
run_compiler(cmd)
return nothing
Expand Down