Skip to content

Commit e365093

Browse files
authored
Increase flexibility for init C/header files in create_library (#847)
* Increase flexibility for init C/header files in `create_library` * Add init headers to include path when compiling init files
1 parent aae3f3e commit e365093

File tree

1 file changed

+44
-13
lines changed

1 file changed

+44
-13
lines changed

src/PackageCompiler.jl

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ const TLS_SYNTAX = VERSION >= v"1.7.0-DEV.1205" ? `-DNEW_DEFINE_FAST_TLS_SYNTAX`
2828
const DEFAULT_EMBEDDING_WRAPPER = @path joinpath(@__DIR__, "embedding_wrapper.c")
2929
const DEFAULT_JULIA_INIT = @path joinpath(@__DIR__, "julia_init.c")
3030
const DEFAULT_JULIA_INIT_HEADER = @path joinpath(@__DIR__, "julia_init.h")
31+
default_julia_init() = String(DEFAULT_JULIA_INIT)
32+
default_julia_init_header() = String(DEFAULT_JULIA_INIT_HEADER)
3133

3234
# See https://github.com/JuliaCI/julia-buildbot/blob/489ad6dee5f1e8f2ad341397dc15bb4fce436b26/master/inventory.py
3335
function default_app_cpu_target()
@@ -500,6 +502,7 @@ function create_sysimage(packages::Union{Nothing, Symbol, Vector{String}, Vector
500502
# Internal args
501503
base_sysimage::Union{Nothing, String}=nothing,
502504
julia_init_c_file=nothing,
505+
julia_init_h_file=nothing,
503506
version=nothing,
504507
soname=nothing,
505508
compat_level::String="major",
@@ -604,7 +607,23 @@ function create_sysimage(packages::Union{Nothing, Symbol, Vector{String}, Vector
604607
incremental)
605608
object_files = [object_file]
606609
if julia_init_c_file !== nothing
607-
push!(object_files, compile_c_init_julia(julia_init_c_file, basename(sysimage_path)))
610+
if julia_init_c_file isa String
611+
julia_init_c_file = [julia_init_c_file]
612+
end
613+
mktempdir() do include_dir
614+
if julia_init_h_file !== nothing
615+
if julia_init_h_file isa String
616+
julia_init_h_file = [julia_init_h_file]
617+
end
618+
for f in julia_init_h_file
619+
cp(f, joinpath(include_dir, basename(f)))
620+
end
621+
end
622+
for f in julia_init_c_file
623+
filename = compile_c_init_julia(f, basename(sysimage_path), include_dir)
624+
push!(object_files, filename)
625+
end
626+
end
608627
end
609628
create_sysimg_from_object_file(object_files,
610629
sysimage_path;
@@ -665,12 +684,12 @@ function get_extra_linker_flags(version, compat_level, soname)
665684
return extra
666685
end
667686

668-
function compile_c_init_julia(julia_init_c_file::String, sysimage_name::String)
687+
function compile_c_init_julia(julia_init_c_file::String, sysimage_name::String, include_dir::String)
669688
@debug "Compiling $julia_init_c_file"
670689
flags = Base.shell_split(cflags())
671690

672691
o_init_file = splitext(julia_init_c_file)[1] * ".o"
673-
cmd = `-c -O2 -DJULIAC_PROGRAM_LIBNAME=$(repr(sysimage_name)) $TLS_SYNTAX $(bitflag()) $flags $(march()) -o $o_init_file $julia_init_c_file`
692+
cmd = `-c -O2 -I$include_dir -DJULIAC_PROGRAM_LIBNAME=$(repr(sysimage_name)) $TLS_SYNTAX $(bitflag()) $flags $(march()) -o $o_init_file $julia_init_c_file`
674693
run_compiler(cmd)
675694
return o_init_file
676695
end
@@ -927,8 +946,13 @@ compiler (can also include extra arguments to the compiler, like `-g`).
927946
928947
- `header_files::Vector{String}`: A list of header files to include in the library bundle.
929948
930-
- `julia_init_c_file::String`: File to include in the system image with functions for
931-
initializing julia from external code.
949+
- `julia_init_c_file::::Union{String, Vector{String}}`: A file or list of files to include
950+
in the system image with functions for initializing Julia from external code
951+
(default: `PackageCompiler.default_julia_init()`).
952+
953+
- `julia_init_h_file::::Union{String, Vector{String}}`: A file or list of files to include
954+
in the library bundle, with declarations for the functions defined in the file(s) provided
955+
via `julia_init_c_file` (default: `PackageCompiler.default_julia_init_header()`).
932956
933957
- `version::VersionNumber`: Library version number. Added to the sysimg `.so` name
934958
on Linux, and the `.dylib` name on Apple platforms, and with `compat_level`, used to
@@ -964,7 +988,8 @@ function create_library(package_or_project::String,
964988
filter_stdlibs::Bool=false,
965989
force::Bool=false,
966990
header_files::Vector{String} = String[],
967-
julia_init_c_file::String=String(DEFAULT_JULIA_INIT),
991+
julia_init_c_file::Union{String, Vector{String}}=default_julia_init(),
992+
julia_init_h_file::Union{String, Vector{String}}=default_julia_init_header(),
968993
version::Union{String,VersionNumber,Nothing}=nothing,
969994
compat_level::String="major",
970995
cpu_target::String=default_app_cpu_target(),
@@ -977,10 +1002,14 @@ function create_library(package_or_project::String,
9771002

9781003
warn_official()
9791004

980-
julia_init_h_file = String(DEFAULT_JULIA_INIT_HEADER)
981-
982-
if !(julia_init_h_file in header_files)
983-
push!(header_files, julia_init_h_file)
1005+
# Add init header files to list of bundled header files if not already present
1006+
if julia_init_h_file isa String
1007+
julia_init_h_file = [julia_init_h_file]
1008+
end
1009+
for f in julia_init_h_file
1010+
if !(f in header_files)
1011+
push!(header_files, f)
1012+
end
9841013
end
9851014

9861015
if version isa String
@@ -1014,8 +1043,8 @@ function create_library(package_or_project::String,
10141043

10151044
create_sysimage_workaround(ctx, sysimg_path, precompile_execution_file,
10161045
precompile_statements_file, incremental, filter_stdlibs, cpu_target;
1017-
sysimage_build_args, include_transitive_dependencies, julia_init_c_file, version,
1018-
soname, script)
1046+
sysimage_build_args, include_transitive_dependencies, julia_init_c_file,
1047+
julia_init_h_file, version, soname, script)
10191048

10201049
if version !== nothing && Sys.isunix()
10211050
cd(dirname(sysimg_path)) do
@@ -1075,7 +1104,8 @@ function create_sysimage_workaround(
10751104
cpu_target::String;
10761105
sysimage_build_args::Cmd,
10771106
include_transitive_dependencies::Bool,
1078-
julia_init_c_file::Union{Nothing,String},
1107+
julia_init_c_file::Union{Nothing,String,Vector{String}},
1108+
julia_init_h_file::Union{Nothing,String,Vector{String}},
10791109
version::Union{Nothing,VersionNumber},
10801110
soname::Union{Nothing,String},
10811111
script::Union{Nothing,String}
@@ -1107,6 +1137,7 @@ function create_sysimage_workaround(
11071137
cpu_target,
11081138
base_sysimage,
11091139
julia_init_c_file,
1140+
julia_init_h_file,
11101141
version,
11111142
soname,
11121143
sysimage_build_args,

0 commit comments

Comments
 (0)