Skip to content

Commit 2bf7026

Browse files
committed
Add way to set compiler for cross target and also bump major version
1 parent bf0740b commit 2bf7026

File tree

3 files changed

+42
-11
lines changed

3 files changed

+42
-11
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "StaticCompiler"
22
uuid = "81625895-6c0f-48fc-b932-11a18313743c"
33
authors = ["Tom Short and contributors"]
4-
version = "0.6.2"
4+
version = "0.7.0"
55

66
[deps]
77
Clang_jll = "0ee61d77-7f21-5576-8119-9fcc46b10100"

src/StaticCompiler.jl

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ using StaticTools
1313
using StaticTools: @symbolcall, @c_str, println
1414
using Core: MethodTable
1515
using Base:BinaryPlatforms.Platform, BinaryPlatforms.HostPlatform, BinaryPlatforms.arch, BinaryPlatforms.os_str, BinaryPlatforms.libc_str
16-
16+
using Base:BinaryPlatforms.platform_dlext
1717
export load_function, compile_shlib, compile_executable
1818
export static_code_llvm, static_code_typed, static_llvm_module, static_code_native
1919
export @device_override, @print_and_throw
@@ -294,7 +294,12 @@ function generate_executable(funcs::Union{Array,Tuple}, path=tempname(), name=fi
294294
exec_path = joinpath(path, filename)
295295
_, obj_path = generate_obj(funcs, path, filename; demangle, target, kwargs...)
296296
# Pick a compiler
297-
cc = Sys.isapple() ? `cc` : clang()
297+
if !isnothing(target.compiler)
298+
cc = `$(target.compiler)`
299+
else
300+
cc = Sys.isapple() ? `cc` : clang()
301+
end
302+
298303
# Compile!
299304
if Sys.isapple()
300305
# Apple no longer uses _start, so we can just specify a custom entry
@@ -372,12 +377,19 @@ function generate_shlib(funcs::Union{Array,Tuple}, path::String=tempname(), file
372377
target::StaticTarget=StaticTarget(),
373378
kwargs...
374379
)
375-
376-
lib_path = joinpath(path, "$filename.$(Libdl.dlext)")
380+
if !isnothing(target.platform)
381+
lib_path = joinpath(path, "$filename.$(platform_dlext(target.platform))")
382+
else
383+
lib_path = joinpath(path, "$filename.$(Libdl.dlext)")
384+
end
377385

378386
_, obj_path = generate_obj(funcs, path, filename; target, demangle, kwargs...)
379387
# Pick a Clang
380-
cc = Sys.isapple() ? `cc` : clang()
388+
if !isnothing(target.compiler)
389+
cc = `$(target.compiler)`
390+
else
391+
cc = Sys.isapple() ? `cc` : clang()
392+
end
381393
# Compile!
382394
run(`$cc -shared $cflags $obj_path -o $lib_path `)
383395

src/target.jl

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,37 @@ Beware that currently the compilation assumes that the code is on the host so pl
1818
```
1919
does not behave as expected.
2020
By default `StaticTarget()` is the native target.
21+
22+
For cross-compilation of executables and shared libraries, one also needs to call `set_compiler!` with the path to a valid C compiler
23+
for the target platform. For example, to cross-compile for aarch64 using a compiler from homebrew, one can use:
24+
```julia
25+
set_compiler!(StaticTarget(parse(Platform,"aarch64-gnu-linux")), "/opt/homebrew/bin/aarch64-unknown-linux-gnu-gcc")
26+
```
2127
"""
22-
struct StaticTarget
28+
mutable struct StaticTarget
2329
platform::Union{Platform,Nothing}
2430
tm::LLVM.TargetMachine
31+
compiler::Union{String,Nothing}
2532
end
2633

2734
clean_triple(platform::Platform) = arch(platform) * os_str(platform) * libc_str(platform)
2835
StaticTarget() = StaticTarget(HostPlatform(), unsafe_string(LLVM.API.LLVMGetHostCPUName()), unsafe_string(LLVM.API.LLVMGetHostCPUFeatures()))
29-
StaticTarget(platform::Platform) = StaticTarget(platform, LLVM.TargetMachine(LLVM.Target(triple = clean_triple(platform)), clean_triple(platform)))
30-
StaticTarget(platform::Platform, cpu::String) = StaticTarget(platform, LLVM.TargetMachine(LLVM.Target(triple = clean_triple(platform)), clean_triple(platform), cpu))
31-
StaticTarget(platform::Platform, cpu::String, features::String) = StaticTarget(platform, LLVM.TargetMachine(LLVM.Target(triple = clean_triple(platform)), clean_triple(platform), cpu, features))
32-
StaticTarget(triple::String, cpu::String, features::String) = StaticTarget(nothing, LLVM.TargetMachine(LLVM.Target(triple = triple), triple, cpu, features))
36+
StaticTarget(platform::Platform) = StaticTarget(platform, LLVM.TargetMachine(LLVM.Target(triple = clean_triple(platform)), clean_triple(platform)), nothing)
37+
StaticTarget(platform::Platform, cpu::String) = StaticTarget(platform, LLVM.TargetMachine(LLVM.Target(triple = clean_triple(platform)), clean_triple(platform), cpu), nothing)
38+
StaticTarget(platform::Platform, cpu::String, features::String) = StaticTarget(platform, LLVM.TargetMachine(LLVM.Target(triple = clean_triple(platform)), clean_triple(platform), cpu, features), nothing)
39+
40+
function StaticTarget(triple::String, cpu::String, features::String)
41+
platform = tryparse(Platform, triple)
42+
StaticTarget(platform, LLVM.TargetMachine(LLVM.Target(triple = triple), triple, cpu, features), nothing)
43+
end
44+
45+
"""
46+
Set the compiler for cross compilation
47+
```julia
48+
set_compiler!(StaticTarget(parse(Platform,"aarch64-gnu-linux")), "/opt/homebrew/bin/aarch64-elf-gcc")
49+
```
50+
"""
51+
set_compiler!(target::StaticTarget, compiler::String) = (target.compiler = compiler)
3352

3453
"""
3554
```julia

0 commit comments

Comments
 (0)