Skip to content

Commit 2245ac4

Browse files
committed
Add cflags option to compile_executable & co
1 parent d183a16 commit 2245ac4

1 file changed

Lines changed: 30 additions & 15 deletions

File tree

src/StaticCompiler.jl

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,10 @@ Hello, world!
227227
```
228228
"""
229229
function compile_executable(f, types=(), path::String="./", name=GPUCompiler.safe_name(repr(f));
230-
filename=name,
231-
kwargs...)
230+
filename=name,
231+
cflags=``,
232+
kwargs...
233+
)
232234

233235
tt = Base.to_tuple_type(types)
234236
# tt == Tuple{} || tt == Tuple{Int, Ptr{Ptr{UInt8}}} || error("input type signature $types must be either () or (Int, Ptr{Ptr{UInt8}})")
@@ -239,7 +241,7 @@ function compile_executable(f, types=(), path::String="./", name=GPUCompiler.saf
239241
# Would be nice to use a compiler pass or something to check if there are any heap allocations or references to globals
240242
# Keep an eye on https://github.com/JuliaLang/julia/pull/43747 for this
241243

242-
generate_executable(f, tt, path, name, filename; kwargs...)
244+
generate_executable(f, tt, path, name, filename; cflags=cflags, kwargs...)
243245

244246
joinpath(abspath(path), filename)
245247
end
@@ -252,8 +254,10 @@ compile_shlib(f, types::Tuple, path::String, name::String=repr(f); filename::Str
252254
As `compile_executable`, but compiling to a standalone `.dylib`/`.so` shared library.
253255
"""
254256
function compile_shlib(f, types=(), path::String="./", name=GPUCompiler.safe_name(repr(f));
255-
filename=name,
256-
kwargs...)
257+
filename=name,
258+
cflags=``,
259+
kwargs...
260+
)
257261

258262
tt = Base.to_tuple_type(types)
259263
isconcretetype(tt) || error("input type signature $types is not concrete")
@@ -263,8 +267,7 @@ function compile_shlib(f, types=(), path::String="./", name=GPUCompiler.safe_nam
263267

264268
# Would be nice to use a compiler pass or something to check if there are any heap allocations or references to globals
265269
# Keep an eye on https://github.com/JuliaLang/julia/pull/43747 for this
266-
267-
generate_shlib(f, tt, path, name, filename; kwargs...)
270+
generate_shlib(f, tt, path, name, filename; cflags=cflags, kwargs...)
268271

269272
joinpath(abspath(path), filename * "." * Libdl.dlext)
270273
end
@@ -348,7 +351,10 @@ test (generic function with 1 method)
348351
julia> path, name = StaticCompiler.generate_executable(test, Tuple{Int64}, "./scratch")
349352
```
350353
"""
351-
function generate_executable(f, tt, path::String = tempname(), name = GPUCompiler.safe_name(repr(f)), filename::String=string(name); kwargs...)
354+
function generate_executable(f, tt, path=tempname(), name=GPUCompiler.safe_name(repr(f)), filename=string(name);
355+
cflags=``,
356+
kwargs...
357+
)
352358
mkpath(path)
353359
obj_path = joinpath(path, "$filename.o")
354360
exec_path = joinpath(path, filename)
@@ -366,7 +372,7 @@ function generate_executable(f, tt, path::String = tempname(), name = GPUCompile
366372
if Sys.isapple()
367373
# Apple no longer uses _start, so we can just specify a custom entry
368374
entry = "_julia_$name"
369-
run(`$cc -e $entry $obj_path -o $exec_path`)
375+
run(`$cc -e $entry $cflags $obj_path -o $exec_path`)
370376
else
371377
# Write a minimal wrapper to avoid having to specify a custom entry
372378
wrapper_path = joinpath(path, "wrapper.c")
@@ -377,7 +383,7 @@ function generate_executable(f, tt, path::String = tempname(), name = GPUCompile
377383
return 0;
378384
}""")
379385
close(f)
380-
run(`$cc $wrapper_path $obj_path -o $exec_path`)
386+
run(`$cc $wrapper_path $cflags $obj_path -o $exec_path`)
381387
# Clean up
382388
run(`rm $wrapper_path`)
383389
end
@@ -416,7 +422,11 @@ julia> ccall(StaticCompiler.generate_shlib_fptr(path, name), Float64, (Int64,),
416422
5.256496109495593
417423
```
418424
"""
419-
function generate_shlib(f, tt, path::String = tempname(), name = GPUCompiler.safe_name(repr(f)), filename::String=name; kwargs...)
425+
function generate_shlib(f, tt, path=tempname(), name=GPUCompiler.safe_name(repr(f)), filename=name;
426+
cflags=``,
427+
kwargs...
428+
)
429+
420430
mkpath(path)
421431
obj_path = joinpath(path, "$filename.o")
422432
lib_path = joinpath(path, "$filename.$(Libdl.dlext)")
@@ -430,7 +440,7 @@ function generate_shlib(f, tt, path::String = tempname(), name = GPUCompiler.saf
430440
# Pick a Clang
431441
cc = Sys.isapple() ? `cc` : clang()
432442
# Compile!
433-
run(`$cc -shared -o $lib_path $obj_path`)
443+
run(`$cc -shared $cflags $obj_path -o $lib_path`)
434444

435445
path, name
436446
end
@@ -503,22 +513,27 @@ function generate_obj(funcs::Array, path::String = tempname(), filenamebase::Str
503513
path, obj_path
504514
end
505515

506-
function generate_shlib(funcs::Array, path::String = tempname(), filename::String="libfoo"; demangle=false, kwargs...)
516+
function generate_shlib(funcs::Array, path::String = tempname(), filename::String="libfoo";
517+
demangle=false,
518+
cflags=``,
519+
kwargs...
520+
)
507521

508522
lib_path = joinpath(path, "$filename.$(Libdl.dlext)")
509523

510524
_,obj_path = generate_obj(funcs, path, filename; demangle=demangle, kwargs...)
511525
# Pick a Clang
512526
cc = Sys.isapple() ? `cc` : clang()
513527
# Compile!
514-
run(`$cc -shared -o $lib_path $obj_path`)
528+
run(`$cc -shared $cflags $obj_path -o $lib_path `)
515529

516530
path, name
517531
end
518532

519533
function compile_shlib(funcs::Array, path::String="./";
520534
filename="libfoo",
521535
demangle=false,
536+
cflags=``,
522537
kwargs...)
523538
for func in funcs
524539
f, types = func
@@ -532,7 +547,7 @@ function compile_shlib(funcs::Array, path::String="./";
532547
# Would be nice to use a compiler pass or something to check if there are any heap allocations or references to globals
533548
# Keep an eye on https://github.com/JuliaLang/julia/pull/43747 for this
534549

535-
generate_shlib(funcs, path, filename; demangle=demangle, kwargs...)
550+
generate_shlib(funcs, path, filename; demangle=demangle, cflags=cflags, kwargs...)
536551

537552
joinpath(abspath(path), filename * "." * Libdl.dlext)
538553
end

0 commit comments

Comments
 (0)