Skip to content

Commit 0a0a226

Browse files
add Base.isprecompiled
1 parent a8ef873 commit 0a0a226

File tree

4 files changed

+59
-1
lines changed

4 files changed

+59
-1
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ New library functions
5353
* `fourthroot(x)` is now defined in `Base.Math` and can be used to compute the fourth root of `x`.
5454
It can also be accessed using the unicode character ``, which can be typed by `\fourthroot<tab>` ([#48899]).
5555
* `Libc.memmove`, `Libc.memset`, and `Libc.memcpy` are now defined, whose functionality matches that of their respective C calls.
56+
* `Base.isprecompiled(pkg::PkgId)` to identify whether a package has already been precompiled ([#50218]).
5657

5758
New library features
5859
--------------------

base/loading.jl

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1364,6 +1364,59 @@ end
13641364

13651365
# End extensions
13661366

1367+
# should sync with the types of arguments of `stale_cachefile`
1368+
const StaleCacheKey = Tuple{Base.PkgId, UInt128, String, String}
1369+
1370+
"""
1371+
Base.isprecompiled(pkg::PkgId)
1372+
1373+
Returns whether a given PkgId within the active project is precompiled.
1374+
1375+
!!! compat "Julia 1.10"
1376+
This function requires at least Julia 1.10.
1377+
"""
1378+
function isprecompiled(pkg::PkgId;
1379+
stale_cache::Dict{StaleCacheKey,Bool}=Dict{StaleCacheKey, Bool}(),
1380+
cachepaths::Vector{String}=Base.find_all_in_cache_path(pkg),
1381+
sourcepath::String=Base.locate_package(pkg)
1382+
)
1383+
isnothing(sourcepath) && error("Cannot locate source for $(repr(PkgId))")
1384+
for path_to_try in cachepaths
1385+
staledeps = stale_cachefile(sourcepath, path_to_try, ignore_loaded = true)
1386+
if staledeps === true
1387+
continue
1388+
end
1389+
staledeps, _ = staledeps::Tuple{Vector{Any}, Union{Nothing, String}}
1390+
# finish checking staledeps module graph
1391+
for i in 1:length(staledeps)
1392+
dep = staledeps[i]
1393+
dep isa Module && continue
1394+
modpath, modkey, modbuild_id = dep::Tuple{String, PkgId, UInt128}
1395+
modpaths = find_all_in_cache_path(modkey)
1396+
for modpath_to_try in modpaths::Vector{String}
1397+
stale_cache_key = (modkey, modbuild_id, modpath, modpath_to_try)::StaleCacheKey
1398+
if get!(() -> stale_cachefile(stale_cache_key..., ignore_loaded=true) === true,
1399+
stale_cache, stale_cache_key)
1400+
continue
1401+
end
1402+
@goto check_next_dep
1403+
end
1404+
@goto check_next_path
1405+
@label check_next_dep
1406+
end
1407+
try
1408+
# update timestamp of precompilation file so that it is the first to be tried by code loading
1409+
touch(path_to_try)
1410+
catch ex
1411+
# file might be read-only and then we fail to update timestamp, which is fine
1412+
ex isa IOError || rethrow()
1413+
end
1414+
return true
1415+
@label check_next_path
1416+
end
1417+
return false
1418+
end
1419+
13671420
# loads a precompile cache file, after checking stale_cachefile tests
13681421
function _tryrequire_from_serialized(modkey::PkgId, build_id::UInt128)
13691422
assert_havelock(require_lock)

doc/src/base/base.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,7 @@ Base.identify_package
462462
Base.locate_package
463463
Base.require
464464
Base.compilecache
465+
Base.isprecompiled
465466
```
466467

467468
## Internals

test/precompile.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,10 @@ precompile_test_harness("code caching") do dir
655655
precompile(getelsize, (Vector{Int32},))
656656
end
657657
""")
658-
Base.compilecache(Base.PkgId(string(Cache_module)))
658+
pkgid = Base.PkgId(string(Cache_module))
659+
@test !Base.isprecompiled(pkgid)
660+
Base.compilecache(pkgid)
661+
@test Base.isprecompiled(pkgid)
659662
@eval using $Cache_module
660663
M = getfield(@__MODULE__, Cache_module)
661664
# Test that this cache file "owns" all the roots

0 commit comments

Comments
 (0)