Skip to content

Commit b857cd5

Browse files
authored
Add contrib/print_sorted_stdlibs.jl (#38414)
We shouldn't have to manually insert stdlibs into `sysimg.jl`; make a tool to do the work for us automatically.
1 parent 4ab7ffa commit b857cd5

File tree

2 files changed

+124
-41
lines changed

2 files changed

+124
-41
lines changed

base/sysimg.jl

Lines changed: 49 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -19,48 +19,56 @@ Base.init_load_path()
1919
if Base.is_primary_base_module
2020
# load some stdlib packages but don't put their names in Main
2121
let
22-
# Stdlibs manually sorted in top down order
22+
# Stdlibs sorted in dependency, then alphabetical, order by contrib/print_sorted_stdlibs.jl
2323
stdlibs = [
24-
# No deps
25-
:Base64,
26-
:CRC32c,
27-
:SHA,
28-
:FileWatching,
29-
:Unicode,
30-
:Mmap,
31-
:Serialization,
32-
:Libdl,
33-
:Printf,
34-
:Markdown,
35-
:NetworkOptions,
36-
:LibGit2,
37-
:Logging,
38-
:Sockets,
39-
:Profile,
40-
:Dates,
41-
:DelimitedFiles,
42-
:Random,
43-
:UUIDs,
44-
:Future,
45-
:LinearAlgebra,
46-
:SparseArrays,
47-
:SuiteSparse,
48-
:Distributed,
49-
:SharedArrays,
50-
:TOML,
51-
:Artifacts,
52-
:Pkg,
53-
:Test,
54-
:REPL,
55-
:Statistics,
56-
:MozillaCACerts_jll,
57-
:LibCURL_jll,
58-
:LibCURL,
59-
:Downloads,
60-
:ArgTools,
61-
:Tar,
62-
]
63-
24+
# No dependencies
25+
:ArgTools,
26+
:Artifacts,
27+
:Base64,
28+
:CRC32c,
29+
:FileWatching,
30+
:Libdl,
31+
:Logging,
32+
:Mmap,
33+
:MozillaCACerts_jll,
34+
:NetworkOptions,
35+
:SHA,
36+
:Serialization,
37+
:Sockets,
38+
:Unicode,
39+
40+
# 1-depth packages
41+
:DelimitedFiles,
42+
:LibCURL_jll,
43+
:LinearAlgebra,
44+
:Markdown,
45+
:Printf,
46+
:Random,
47+
:Tar,
48+
49+
# 2-depth packages
50+
:Dates,
51+
:Distributed,
52+
:Future,
53+
:InteractiveUtils,
54+
:LibCURL,
55+
:LibGit2,
56+
:Profile,
57+
:SparseArrays,
58+
:UUIDs,
59+
60+
# 3-depth packages
61+
:Downloads,
62+
:REPL,
63+
:SharedArrays,
64+
:Statistics,
65+
:SuiteSparse,
66+
:TOML,
67+
:Test,
68+
69+
# 4-depth packages
70+
:Pkg,
71+
]
6472
maxlen = reduce(max, textwidth.(string.(stdlibs)); init=0)
6573

6674
tot_time_stdlib = 0.0

contrib/print_sorted_stdlibs.jl

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#!/usr/bin/env julia
2+
using TOML
3+
4+
# Default to the `stdlib/vX.Y` directory
5+
STDLIB_DIR = get(ARGS, 1, joinpath(@__DIR__, "..", "usr", "share", "julia", "stdlib"))
6+
vXYdirs = readdir(STDLIB_DIR)
7+
if length(vXYdirs) == 1 && match(r"v\d\.\d", vXYdirs[1]) !== nothing
8+
STDLIB_DIR = joinpath(STDLIB_DIR, vXYdirs[1])
9+
end
10+
11+
project_deps = Dict{String,Set{String}}()
12+
for project_dir in readdir(STDLIB_DIR, join=true)
13+
files = readdir(project_dir)
14+
if "Project.toml" in files
15+
project = TOML.parsefile(joinpath(project_dir, "Project.toml"))
16+
17+
if !haskey(project, "name")
18+
continue
19+
end
20+
name = project["name"]
21+
deps = Set(collect(keys(get(project, "deps", Dict{String,String}()))))
22+
project_deps[name] = deps
23+
end
24+
end
25+
26+
#println("Found $(length(keys(project_deps))) stdlib projects")
27+
28+
function project_depth(project)
29+
deps = project_deps[project]
30+
if isempty(deps)
31+
return 0
32+
end
33+
34+
depth = 1
35+
while !all(isempty(project_deps[d]) for d in deps)
36+
depth += 1
37+
38+
if depth > 100
39+
error("Failed to converge while finding project depth for $(project)!")
40+
end
41+
42+
new_deps = Set{String}()
43+
for d in deps
44+
union!(new_deps, project_deps[d])
45+
end
46+
deps = new_deps
47+
end
48+
return depth
49+
end
50+
51+
project_depths = Dict(p => project_depth(p) for p in keys(project_deps))
52+
53+
function project_isless(p1, p2)
54+
if project_depths[p1] != project_depths[p2]
55+
return isless(project_depths[p1], project_depths[p2])
56+
end
57+
return isless(p1, p2)
58+
end
59+
60+
sorted_projects = sort(collect(keys(project_depths)), lt=project_isless)
61+
62+
# Print out sorted projects, ready to be pasted into `sysimg.jl`
63+
last_depth = 0
64+
println(" # Stdlibs sorted in dependency, then alphabetical, order by contrib/print_sorted_stdlibs.jl")
65+
println(" stdlibs = [")
66+
println(" # No dependencies")
67+
for p in sorted_projects
68+
if project_depths[p] != last_depth
69+
global last_depth = project_depths[p]
70+
println()
71+
println(" # $(last_depth)-depth packages")
72+
end
73+
println(" :$(p),")
74+
end
75+
println(" ]")

0 commit comments

Comments
 (0)