Skip to content

Commit 5e2a8fd

Browse files
authored
Allow passing projects (and not just packages) to create_library (#848)
1 parent 6b0adaa commit 5e2a8fd

File tree

1 file changed

+31
-18
lines changed

1 file changed

+31
-18
lines changed

src/PackageCompiler.jl

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -848,10 +848,10 @@ end
848848
###########
849849

850850
"""
851-
create_library(package_dir::String, dest_dir::String; kwargs...)
851+
create_library(package_or_project::String, dest_dir::String; kwargs...)
852852
853-
Compile a library with the source in `package_dir` to the folder `dest_dir`.
854-
The folder `package_dir` should to contain a package with C-callable functions,
853+
Compile a library with the source in `package_or_project` to the folder `dest_dir`.
854+
The folder `package_or_project` should contain a package with C-callable functions,
855855
e.g.
856856
857857
```
@@ -868,6 +868,7 @@ Base.@ccallable function julia_cg(fptr::Ptr{Cvoid}, cx::Ptr{Cdouble}, cb::Ptr{Cd
868868
return 0
869869
end
870870
```
871+
Alternatively, it can contain a project with dependencies that have C-callable functions.
871872
872873
The library will be placed in the `lib` folder in `dest_dir` (or `bin` on Windows),
873874
and can be linked to and called into from C/C++ or other languages that can use C libraries.
@@ -944,7 +945,7 @@ compiler (can also include extra arguments to the compiler, like `-g`).
944945
- `sysimage_build_args::Cmd`: A set of command line options that is used in the Julia process building the sysimage,
945946
for example `-O1 --check-bounds=yes`.
946947
"""
947-
function create_library(package_dir::String,
948+
function create_library(package_or_project::String,
948949
dest_dir::String;
949950
lib_name=nothing,
950951
precompile_execution_file::Union{String, Vector{String}}=String[],
@@ -976,11 +977,15 @@ function create_library(package_dir::String,
976977
version = parse(VersionNumber, version)
977978
end
978979

979-
ctx = create_pkg_context(package_dir)
980-
ctx.env.pkg === nothing && error("expected package to have a `name` and `uuid`")
980+
ctx = create_pkg_context(package_or_project)
981+
if ctx.env.pkg === nothing && lib_name === nothing
982+
error("expected either package with a `name` and `uuid`, or non-empty `lib_name`")
983+
end
981984
Pkg.instantiate(ctx, verbose=true, allow_autoprecomp = false)
982985

983-
lib_name = something(lib_name, ctx.env.pkg.name)
986+
if lib_name === nothing
987+
lib_name = ctx.env.pkg.name
988+
end
984989
try_rm_dir(dest_dir; force)
985990
mkpath(dest_dir)
986991
stdlibs = filter_stdlibs ? gather_stdlibs_project(ctx; only_in_sysimage=false) : _STDLIBS
@@ -1064,7 +1069,6 @@ function create_sysimage_workaround(
10641069
soname::Union{Nothing,String},
10651070
script::Union{Nothing,String}
10661071
)
1067-
package_name = ctx.env.pkg.name
10681072
project = dirname(ctx.env.project_file)
10691073

10701074
if !incremental
@@ -1076,7 +1080,15 @@ function create_sysimage_workaround(
10761080
base_sysimage = nothing
10771081
end
10781082

1079-
create_sysimage([package_name]; sysimage_path, project,
1083+
if ctx.env.pkg === nothing
1084+
# If environment is not a package, create sysimage with all packages in project
1085+
packages = nothing
1086+
else
1087+
# Otherwise, only include package in sysimage
1088+
packages = [ctx.env.pkg.name]
1089+
end
1090+
1091+
create_sysimage(packages; sysimage_path, project,
10801092
incremental=true,
10811093
script=script,
10821094
precompile_execution_file,
@@ -1269,22 +1281,23 @@ end
12691281
function bundle_artifacts(ctx, dest_dir; include_lazy_artifacts::Bool)
12701282
pkgs = load_all_deps(ctx)
12711283

1272-
# Also want artifacts for the project itself
1273-
@assert ctx.env.pkg !== nothing
1274-
# This is kinda ugly...
1275-
ctx.env.pkg.path = dirname(ctx.env.project_file)
1276-
push!(pkgs, ctx.env.pkg)
1277-
12781284
# TODO: Allow override platform?
12791285
platform = Base.BinaryPlatforms.HostPlatform()
12801286
depot_path = joinpath(dest_dir, "share", "julia")
12811287
artifact_app_path = joinpath(depot_path, "artifacts")
12821288

1283-
bundled_artifacts = Pair{String, Vector{Pair{String, String}}}[]
1284-
1289+
source_paths_names = Tuple{String, String}[]
12851290
for pkg in pkgs
12861291
pkg_source_path = source_path(ctx, pkg)
12871292
pkg_source_path === nothing && continue
1293+
push!(source_paths_names, (pkg_source_path, pkg.name))
1294+
end
1295+
# Also want artifacts for the project itself
1296+
push!(source_paths_names, (dirname(ctx.env.project_file), ctx.env.project_file))
1297+
1298+
bundled_artifacts = Pair{String, Vector{Pair{String, String}}}[]
1299+
1300+
for (pkg_source_path, pkg_name) in source_paths_names
12881301
bundled_artifacts_pkg = Pair{String, String}[]
12891302
if isdefined(Pkg.Operations, :collect_artifacts)
12901303
for (artifacts_toml, artifacts) in _collect_artifacts(pkg_source_path; platform, include_lazy=include_lazy_artifacts)
@@ -1308,7 +1321,7 @@ function bundle_artifacts(ctx, dest_dir; include_lazy_artifacts::Bool)
13081321
end
13091322
end
13101323
if !isempty(bundled_artifacts_pkg)
1311-
push!(bundled_artifacts, pkg.name => bundled_artifacts_pkg)
1324+
push!(bundled_artifacts, pkg_name => bundled_artifacts_pkg)
13121325
end
13131326
end
13141327

0 commit comments

Comments
 (0)