Skip to content

Commit 208e037

Browse files
committed
add compiling with juliac
1 parent 61330c4 commit 208e037

File tree

4 files changed

+89
-21
lines changed

4 files changed

+89
-21
lines changed

compile/README.md

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
Minimal C bindings for JetReconstruction.jl
44

5-
- [C-header](include/JetReconstruction.h)
6-
- shared library compiled with [PackageCompiler.jl](https://github.com/JuliaLang/PackageCompiler.jl)
5+
- [C-header](include/JetReconstruction.h.in)
6+
- shared library compiled with [PackageCompiler.jl](https://github.com/JuliaLang/PackageCompiler.jl) or juliac
77

88
## Building library
99

@@ -13,6 +13,16 @@ To build the library, run the following command from the package root directory:
1313
julia --project=compile compile/build.jl
1414
```
1515

16+
> [!NOTE]
17+
> Since Julia 1.12 `--juliac` can be specified to use the juliac compiler instead of PackageCompiler.
18+
> Before Julia 1.12, nightlies can be used instead:
19+
>
20+
> ```sh
21+
> julia +nightly --project=compile compile/build.jl --juliac
22+
> ```
23+
>
24+
> Packes compiled with `PackageCompiler.jl` will have `JETRECONSTRUCTION_COMPILER_PACKAGECOMPILER` defined. Packages compiled with `juliac` will have `JETRECONSTRUCTION_COMPILER_JULIAC` defined.
25+
1626
## Usage example
1727
1828
### Example source file
@@ -21,10 +31,16 @@ Example usage of C bindings in an application:
2131
2232
```C
2333
#include "JetReconstruction.h"
24-
#include "julia_init.h" /*Should be automatically generated by PackageCompiler.jl and distributed together with the "JetReconstruction.h" header file*/
34+
35+
/*Should be automatically generated by PackageCompiler.jl and distributed together with the "JetReconstruction.h" header file*/
36+
#ifdef JETRECONSTRUCTION_COMPILER_PACKAGECOMPILER
37+
#include "julia_init.h"
38+
#endif
2539
2640
int main(int argc, char *argv[]) {
27-
init_julia(argc, argv); /*initialization of julia runtime*/
41+
#ifdef JETRECONSTRUCTION_COMPILER_PACKAGECOMPILER
42+
init_julia(0, NULL); /*initialization of julia runtime*/
43+
#endif
2844
2945
/*Prepare array of pseudo jets*/
3046
size_t particles_len;
@@ -47,7 +63,9 @@ int main(int argc, char *argv[]) {
4763
/*Use the cluster sequence in your application
4864
then free memory allocations done by library*/
4965
jetreconstruction_ClusterSequence_free_members(&cluster_seq);
66+
#ifdef JETRECONSTRUCTION_COMPILER_PACKAGECOMPILER
5067
shutdown_julia(0); /*teardown of julia runtime*/
68+
#endif
5169
return 0;
5270
}
5371

compile/build.jl

Lines changed: 57 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,20 @@ function parse_args(args)
1414
help = "Directory to save the compiled library"
1515
arg_type = String
1616
default = joinpath(splitdir(@__DIR__) |> first, "JetReconstructionCompiled")
17+
18+
"--juliac"
19+
help = "Use juliac compiler"
20+
action = :store_true
1721
end
1822

1923
return ArgParse.parse_args(args, s)
2024
end
2125

26+
function cp_file(input_dir, basename, output_dir)
27+
cp(joinpath(input_dir, basename),
28+
joinpath(output_dir, basename); force = true)
29+
end
30+
2231
function configure_file(template_path::String, output_path::String,
2332
replacements::Dict{String, String})
2433
template = read(template_path, String)
@@ -30,36 +39,69 @@ function configure_file(template_path::String, output_path::String,
3039
end
3140
end
3241

42+
function compile_w_packagecompiler(source_dir, output_dir)
43+
return @elapsed PackageCompiler.create_library(source_dir, output_dir;
44+
lib_name = "jetreconstruction",
45+
precompile_execution_file = [joinpath(@__DIR__,
46+
"precompile_execution.jl")],
47+
incremental = false,
48+
filter_stdlibs = true,
49+
force = true)
50+
end
51+
52+
function compile_w_juliac(source_dir, output_dir)
53+
julia_path = joinpath(Sys.BINDIR, Base.julia_exename())
54+
juliac_path = joinpath(Sys.BINDIR, "..", "share", "julia", "juliac.jl")
55+
jetreconstruction_path = joinpath(source_dir, "src", "JetReconstruction.jl")
56+
lib_dir = joinpath(output_dir, "lib")
57+
mkpath(lib_dir)
58+
output_lib = joinpath(lib_dir, "libjetreconstruction.so")
59+
command = "$(julia_path) --project=$(source_dir) $(juliac_path) --experimental --trim=no --compile-ccallable --output-lib $(output_lib) $(jetreconstruction_path)"
60+
@info command
61+
return @elapsed run(`$(split(command))`)
62+
end
63+
3364
function (@main)(args)
3465
parsed_args = parse_args(args)
3566
source_dir = parsed_args["source-dir"]
3667
output_dir = parsed_args["output-dir"]
3768

3869
@info "Compiling package from $source_dir"
3970
@info "Creating library in $output_dir"
40-
PackageCompiler.create_library(source_dir, output_dir;
41-
lib_name = "jetreconstruction",
42-
header_files = [joinpath(@__DIR__, "include",
43-
"JetReconstruction.h")],
44-
precompile_execution_file = [joinpath(@__DIR__,
45-
"precompile_execution.jl")],
46-
incremental = false,
47-
filter_stdlibs = true,
48-
force = true)
71+
72+
compilation_time = if parsed_args["juliac"]
73+
@info "Using juliac compiler"
74+
compile_w_juliac(source_dir, output_dir)
75+
else
76+
@info "Using PackageCompiler compiler"
77+
compile_w_packagecompiler(source_dir, output_dir)
78+
end
79+
@info "Compiled in $(compilation_time) seconds"
80+
compiler = parsed_args["juliac"] ? "JETRECONSTRUCTION_COMPILER_JULIAC" :
81+
"JETRECONSTRUCTION_COMPILER_PACKAGECOMPILER"
82+
83+
includes_input = joinpath(@__DIR__, "include")
84+
includes_output = joinpath(output_dir, "include")
85+
mkpath(includes_output)
86+
@info "Copying header files to $includes_output"
87+
configure_file(joinpath(includes_input, "JetReconstruction.h.in"),
88+
joinpath(includes_output, "JetReconstruction.h"),
89+
Dict("JETRECONSTRUCTION_COMPILER" => compiler))
4990

5091
cmake_input = joinpath(@__DIR__, "cmake", "JetReconstruction")
5192
cmake_output = joinpath(output_dir, "lib", "cmake", "JetReconstruction")
93+
@info "Copying CMake files to $cmake_output"
94+
mkpath(cmake_output)
5295

5396
version = pkgversion(JetReconstruction)
5497
cmake_project_version = "$(version.major).$(version.minor).$(version.patch)"
5598

56-
@info "Copying CMake file to $cmake_output"
57-
mkpath(cmake_output)
58-
cp_file(input_dir, basename, output_dir) = cp(joinpath(input_dir, basename),
59-
joinpath(output_dir, basename))
60-
cp_file(cmake_input, "JetReconstructionConfig.cmake", cmake_output)
61-
cp_file(cmake_input, "JetReconstructionTargets.cmake", cmake_output)
99+
configure_file(joinpath(cmake_input, "JetReconstructionConfig.cmake.in"),
100+
joinpath(cmake_output, "JetReconstructionConfig.cmake"),
101+
Dict("JETRECONSTRUCTION_COMPILER" => compiler))
62102
configure_file(joinpath(cmake_input, "JetReconstructionConfigVersion.cmake.in"),
63103
joinpath(cmake_output, "JetReconstructionConfigVersion.cmake"),
64104
Dict("PROJECT_VERSION" => cmake_project_version))
105+
cp_file(cmake_input, "JetReconstructionTargets.cmake", cmake_output)
106+
return 0
65107
end

compile/cmake/JetReconstruction/JetReconstructionConfig.cmake renamed to compile/cmake/JetReconstruction/JetReconstructionConfig.cmake.in

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Config file for the JetReconstruction.jl package
22
# Manualy adjusted from standard cmake generated config file
3+
4+
set(JETRECONSTRUCTION_COMPILER "@JETRECONSTRUCTION_COMPILER@")
5+
36
get_filename_component(PACKAGE_PREFIX_DIR "${CMAKE_CURRENT_LIST_DIR}/../../../" ABSOLUTE)
47

58
macro(set_and_check _var _file)
@@ -26,8 +29,10 @@ endmacro()
2629
# the imported targets created later.
2730
set_and_check(JetReconstruction_INCLUDE_DIR "${PACKAGE_PREFIX_DIR}/include")
2831

29-
# - Create path to installed read-only data files (e.g. yaml files)
30-
set_and_check(JetReconstruction_DATA_DIR "${PACKAGE_PREFIX_DIR}/share/julia")
32+
if (${JETRECONSTRUCTION_COMPILER} STREQUAL "JETRECONSTRUCTION_COMPILER_PACKAGECOMPILER")
33+
# - Create path to installed read-only data files (e.g. yaml files)
34+
set_and_check(JetReconstruction_DATA_DIR "${PACKAGE_PREFIX_DIR}/share/julia")
35+
endif()
3136

3237
# - Include the targets file to create the imported targets that a client can
3338
# link to (libraries) or execute (programs)

compile/include/JetReconstruction.h renamed to compile/include/JetReconstruction.h.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ extern "C" {
2121

2222
#include <stddef.h>
2323

24+
/** Compiler used to compiler JetReconstruction.jl package */
25+
#define @JETRECONSTRUCTION_COMPILER@ 1
26+
2427
/*Special states in a history.*/
2528

2629
/**Special history state: Invalid child for this jet, meaning it did not

0 commit comments

Comments
 (0)