@@ -53,6 +53,45 @@ function find_all_in_cache_path(mod::Symbol)
53
53
paths
54
54
end
55
55
56
+ const r_compilable = r" ^#\s *pragma\s +compile(\s +true)?\s *$"
57
+ const r_ncompilable = r" ^#\s *pragma\s +compile\s +false\s *$"
58
+
59
+ # return true if "#pragma compilable [true]" appears in the file before
60
+ # any Julia code, false for "#pragma compile false", default otherwise
61
+ function compilable (path:: AbstractString , default:: Bool = false )
62
+ return open (path, " r" ) do f
63
+ for line in eachline (f)
64
+ s = lstrip (line)
65
+ if ! isempty (s)
66
+ if s[1 ] == ' #'
67
+ ismatch (r_compilable, s) && return true
68
+ ismatch (r_ncompilable, s) && return false
69
+ else
70
+ return default
71
+ end
72
+ end
73
+ end
74
+ return default
75
+ end
76
+ end
77
+
78
+ # compile path on node 1 if path is #pragma compilable,
79
+ # returning the cachefile path, or nothing otherwise
80
+ function autocompile_on_node1 (mod:: Symbol , path:: AbstractString )
81
+ if myid () == 1
82
+ if compilable (path)
83
+ if isinteractive ()
84
+ info (" Compiling module $mod from $path ..." )
85
+ end
86
+ return compile (mod)
87
+ else
88
+ return nothing
89
+ end
90
+ else
91
+ return remotecall_fetch (1 , autocompile_on_node1, mod, path)
92
+ end
93
+ end
94
+
56
95
function _include_from_serialized (content:: Vector{UInt8} )
57
96
return ccall (:jl_restore_incremental_from_buf , Any, (Ptr{Uint8},Int), content, sizeof (content))
58
97
end
@@ -166,6 +205,19 @@ function require(mod::Symbol)
166
205
name = string (mod)
167
206
path = find_in_node_path (name, source_dir (), 1 )
168
207
path === nothing && throw (ArgumentError (" $name not found in path" ))
208
+
209
+ if last || nprocs () == 1
210
+ cachefile = autocompile_on_node1 (mod, path)
211
+ if cachefile != = nothing
212
+ if nothing === _require_from_serialized (1 , cachefile, last)
213
+ warn (" require failed to create a precompiled cache file" )
214
+ else
215
+ return
216
+ end
217
+ end
218
+ end
219
+
220
+ # could not compile, just include(path)
169
221
if last && myid () == 1 && nprocs () > 1
170
222
# broadcast top-level import/using from node 1 (only)
171
223
content = open (readall, path)
@@ -289,6 +341,7 @@ function compile(name::ByteString)
289
341
myid () == 1 || error (" can only compile from node 1" )
290
342
path = find_in_path (name)
291
343
path === nothing && throw (ArgumentError (" $name not found in path" ))
344
+ ! compilable (path, true ) && throw (ArgumentError (" $name has #pragma compile false" ))
292
345
cachepath = LOAD_CACHE_PATH[1 ]
293
346
if ! isdir (cachepath)
294
347
mkpath (cachepath)
0 commit comments