1111use std:: env;
1212use std:: fs;
1313use std:: path:: Path ;
14+ use std:: path:: PathBuf ;
1415use std:: process:: Command ;
1516
1617fn main ( ) {
1718 let source_directory = Path :: new ( "../../../eqwalizer/eqwalizer" ) ;
19+ let tools_dir = source_directory. join ( "tools" ) ;
1820 let out_dir = env:: var_os ( "OUT_DIR" ) . unwrap ( ) ;
1921 let eqwalizer_out_dir = Path :: new ( "../../../../../buck-out/eqwalizer/scala-3.6.4" ) ;
2022 let dest_path = Path :: new ( & out_dir) . join ( "eqwalizer" ) ;
2123 let extension;
24+ let java;
2225
2326 if let Some ( path) = env:: var_os ( "ELP_EQWALIZER_PATH" ) {
27+ java = "java" . into ( ) ;
2428 let from = Path :: new ( & path) ;
2529 extension = from
2630 . extension ( )
@@ -30,60 +34,75 @@ fn main() {
3034 . to_string ( ) ;
3135 fs:: copy ( from, dest_path) . expect ( "Copying precompiled eqwalizer failed" ) ;
3236 } else {
33- extension = "" . to_string ( ) ;
34- // Use the sbt wrapper on linux or otherwise require sbt to be installed
35- let sbt = fs:: canonicalize ( source_directory. join ( "./meta/sbt.sh" ) ) . unwrap ( ) ;
36- let output = Command :: new ( sbt)
37- . arg ( "assembly" )
38- . current_dir ( source_directory)
39- . env ( "EQWALIZER_USE_BUCK_OUT" , "true" )
40- . output ( )
41- . expect ( "failed to execute sbt assembly" ) ;
37+ let jar = build_jar ( source_directory, eqwalizer_out_dir) ;
4238
43- if !output. status . success ( ) {
44- let stdout =
45- String :: from_utf8 ( output. stdout ) . expect ( "valid utf8 output from sbt assembly" ) ;
46- let stderr =
47- String :: from_utf8 ( output. stderr ) . expect ( "valid utf8 output from sbt assembly" ) ;
48- panic ! ( "sbt assembly failed with stdout:\n {stdout}\n \n stderr:\n {stderr}" ) ;
39+ if env:: var ( "OPT_LEVEL" ) . unwrap_or_default ( ) == "0" {
40+ extension = "jar" . to_string ( ) ;
41+ java = if env:: var ( "CARGO_CFG_TARGET_OS" ) . unwrap ( ) == "linux" {
42+ fs:: canonicalize ( tools_dir. join ( "graalvm/bin/java" ) ) . unwrap ( )
43+ } else {
44+ "java" . into ( )
45+ } ;
46+ fs:: copy ( jar, dest_path) . expect ( "Copying fresh eqwalizer failed" ) ;
47+ } else {
48+ extension = "" . to_string ( ) ;
49+ java = "java" . into ( ) ;
50+ let image_path = build_native_image ( source_directory, eqwalizer_out_dir, jar) ;
51+ fs:: copy ( image_path, dest_path) . expect ( "Copying fresh eqwalizer failed" ) ;
4952 }
5053
51- let jar = fs:: canonicalize ( eqwalizer_out_dir. join ( "eqwalizer.jar" ) ) . unwrap ( ) ;
52-
53- let native_image =
54- fs:: canonicalize ( source_directory. join ( "./meta/native-image.sh" ) ) . unwrap ( ) ;
55- let image_path = fs:: canonicalize ( eqwalizer_out_dir)
56- . unwrap ( )
57- . join ( "eqwalizer" ) ;
58- let output = Command :: new ( native_image)
59- . current_dir ( source_directory)
60- . arg ( "-H:IncludeResources=application.conf" )
61- . arg ( "--no-server" )
62- . arg ( "--no-fallback" )
63- . arg ( "-jar" )
64- . arg ( jar)
65- . arg ( & image_path)
66- . output ( )
67- . expect ( "failed to execute native-image" ) ;
68-
69- if !output. status . success ( ) {
70- let stdout =
71- String :: from_utf8 ( output. stdout ) . expect ( "valid utf8 output from native-image" ) ;
72- let stderr =
73- String :: from_utf8 ( output. stderr ) . expect ( "valid utf8 output from native-image" ) ;
74- panic ! ( "native-image failed with stdout:\n {stdout}\n \n stderr:\n {stderr}" ) ;
75- }
76-
77- fs:: copy ( image_path, dest_path) . expect ( "Copying fresh eqwalizer failed" ) ;
78-
7954 rerun_if_changed ( source_directory. join ( "build.sbt" ) ) ;
8055 rerun_if_changed ( source_directory. join ( "src" ) ) ;
8156 }
8257
8358 println ! ( "cargo:rustc-env=ELP_EQWALIZER_EXT={extension}" ) ;
59+ println ! ( "cargo:rustc-env=ELP_EQWALIZER_JAVA={}" , java. display( ) ) ;
8460 println ! ( "cargo:rerun-if-env-changed=ELP_EQWALIZER_PATH" ) ;
8561}
8662
63+ fn build_native_image ( source_directory : & Path , eqwalizer_out_dir : & Path , jar : PathBuf ) -> PathBuf {
64+ let native_image = fs:: canonicalize ( source_directory. join ( "./meta/native-image.sh" ) ) . unwrap ( ) ;
65+ let image_path = fs:: canonicalize ( eqwalizer_out_dir)
66+ . unwrap ( )
67+ . join ( "eqwalizer" ) ;
68+ let output = Command :: new ( native_image)
69+ . current_dir ( source_directory)
70+ . arg ( "-H:IncludeResources=application.conf" )
71+ . arg ( "--no-server" )
72+ . arg ( "--no-fallback" )
73+ . arg ( "-jar" )
74+ . arg ( jar)
75+ . arg ( & image_path)
76+ . output ( )
77+ . expect ( "failed to execute native-image" ) ;
78+
79+ if !output. status . success ( ) {
80+ let stdout = String :: from_utf8 ( output. stdout ) . expect ( "valid utf8 output from native-image" ) ;
81+ let stderr = String :: from_utf8 ( output. stderr ) . expect ( "valid utf8 output from native-image" ) ;
82+ panic ! ( "native-image failed with stdout:\n {stdout}\n \n stderr:\n {stderr}" ) ;
83+ }
84+ image_path
85+ }
86+
87+ fn build_jar ( source_directory : & Path , eqwalizer_out_dir : & Path ) -> PathBuf {
88+ // Use the sbt wrapper on linux or otherwise require sbt to be installed
89+ let sbt = fs:: canonicalize ( source_directory. join ( "./meta/sbt.sh" ) ) . unwrap ( ) ;
90+ let output = Command :: new ( sbt)
91+ . arg ( "assembly" )
92+ . current_dir ( source_directory)
93+ . env ( "EQWALIZER_USE_BUCK_OUT" , "true" )
94+ . output ( )
95+ . expect ( "failed to execute sbt assembly" ) ;
96+
97+ if !output. status . success ( ) {
98+ let stdout = String :: from_utf8 ( output. stdout ) . expect ( "valid utf8 output from sbt assembly" ) ;
99+ let stderr = String :: from_utf8 ( output. stderr ) . expect ( "valid utf8 output from sbt assembly" ) ;
100+ panic ! ( "sbt assembly failed with stdout:\n {stdout}\n \n stderr:\n {stderr}" ) ;
101+ }
102+
103+ fs:: canonicalize ( eqwalizer_out_dir. join ( "eqwalizer.jar" ) ) . unwrap ( )
104+ }
105+
87106fn rerun_if_changed ( path : impl AsRef < Path > ) {
88107 println ! ( "cargo:rerun-if-changed={}" , path. as_ref( ) . display( ) ) ;
89108}
0 commit comments