@@ -9,6 +9,7 @@ use builders::{
99 create_ext2_image, create_initfs_image, parse_service_index,
1010} ;
1111
12+ #[ allow( unused) ]
1213fn main ( ) {
1314 let manifest_dir = PathBuf :: from ( env:: var ( "CARGO_MANIFEST_DIR" ) . unwrap ( ) ) ;
1415
@@ -105,3 +106,92 @@ fn main() {
105106 println ! ( " ramfs/ -> {}" , initfs_image_path. display( ) ) ;
106107 println ! ( " fs/ -> {}" , ext2_image_path. display( ) ) ;
107108}
109+
110+ fn find_service_bin ( manifest_dir : & Path , name : & str ) -> Option < PathBuf > {
111+ let profile = env:: var ( "PROFILE" ) . unwrap_or_else ( |_| "debug" . to_string ( ) ) ;
112+ let profile_dir = if profile == "release" { "release" } else { "debug" } ;
113+
114+ if let Ok ( target_dir) = env:: var ( "CARGO_TARGET_DIR" ) {
115+ let target_dir = PathBuf :: from ( target_dir) ;
116+ let candidate = target_dir
117+ . join ( "x86_64-unknown-none" )
118+ . join ( profile_dir)
119+ . join ( name) ;
120+ if candidate. is_file ( ) {
121+ return Some ( candidate) ;
122+ }
123+ }
124+
125+ let candidates = [
126+ manifest_dir
127+ . join ( "target/x86_64-unknown-none" )
128+ . join ( profile_dir)
129+ . join ( name) ,
130+ manifest_dir
131+ . join ( "src/services" )
132+ . join ( name)
133+ . join ( "target/x86_64-unknown-none" )
134+ . join ( profile_dir)
135+ . join ( name) ,
136+ ] ;
137+
138+ candidates. into_iter ( ) . find ( |p| p. is_file ( ) )
139+ }
140+
141+ fn build_service ( manifest_dir : & Path , name : & str ) -> Option < PathBuf > {
142+ if env:: var ( "SWIFTCORE_SKIP_SHELL_BUILD" ) . ok ( ) . as_deref ( ) == Some ( "1" ) {
143+ return None ;
144+ }
145+
146+ let svc_dir = manifest_dir. join ( "src/services" ) . join ( name) ;
147+ if !svc_dir. is_dir ( ) {
148+ return None ;
149+ }
150+
151+ let profile = env:: var ( "PROFILE" ) . unwrap_or_else ( |_| "debug" . to_string ( ) ) ;
152+ let mut cmd = Command :: new ( "cargo" ) ;
153+ cmd. current_dir ( & svc_dir)
154+ . env ( "SWIFTCORE_SKIP_SHELL_BUILD" , "1" )
155+ . args ( [ "build" , "--target" , "x86_64-unknown-none" ] ) ;
156+
157+ if profile == "release" {
158+ cmd. arg ( "--release" ) ;
159+ }
160+
161+ let status = cmd
162+ . status ( ) ;
163+
164+ match status {
165+ Ok ( s) if s. success ( ) => find_service_bin ( manifest_dir, name) ,
166+ Ok ( _) => None ,
167+ Err ( _) => None ,
168+ }
169+ }
170+
171+ fn copy_service ( manifest_dir : & Path , name : & str , stage_dir : & Path ) {
172+ let env_key = match name {
173+ "shell" => "SWIFTCORE_SHELL_BIN" ,
174+ "keyboard" => "SWIFTCORE_KEYBOARD_BIN" ,
175+ _ => "SWIFTCORE_SERVICE_BIN" ,
176+ } ;
177+
178+ let env_bin = env:: var ( env_key) . ok ( ) . and_then ( |p| {
179+ let path = PathBuf :: from ( p) ;
180+ if path. is_file ( ) { Some ( path) } else { None }
181+ } ) ;
182+
183+ let bin = env_bin
184+ . or_else ( || find_service_bin ( manifest_dir, name) )
185+ . or_else ( || build_service ( manifest_dir, name) ) ;
186+
187+ if let Some ( bin) = bin {
188+ let dest = stage_dir. join ( format ! ( "{}.service" , name) ) ;
189+ let _ = fs:: copy ( & bin, & dest) ;
190+ } else {
191+ println ! (
192+ "cargo:warning=initfs: {} binary not found; set {} or build service crate" ,
193+ name, env_key
194+ ) ;
195+ }
196+ }
197+ * /
0 commit comments