2929from app .codegen .drivers_nim import (
3030 DEFAULT_COMPILATION_MODE ,
3131 COMPILATION_MODE_STATIC ,
32+ COMPILATION_MODE_SHARED_SCENES ,
3233 compiled_drivers ,
3334 compilation_mode_uses_shared_libraries ,
3435 driver_library_filename ,
3839)
3940from app .codegen .scene_nim import (
4041 compiled_frame_scenes ,
42+ scene_bundle_library_filename ,
4143 scene_library_filename ,
4244 scene_module_filename ,
4345 scene_module_suffix ,
@@ -230,9 +232,14 @@ def scene_library_paths(
230232 ) -> list [str ]:
231233 if not compilation_mode_uses_shared_libraries (compilation_mode ):
232234 return []
235+ compiled_scenes = compiled_frame_scenes (frame )
236+ if not compiled_scenes :
237+ return []
238+ if compilation_mode == COMPILATION_MODE_SHARED_SCENES :
239+ return [os .path .join (build_dir , "scenes" , scene_bundle_library_filename ())]
233240 return [
234241 os .path .join (build_dir , "scenes" , scene_module_suffix (scene ), scene_library_filename (scene ))
235- for scene in compiled_frame_scenes ( frame )
242+ for scene in compiled_scenes
236243 ]
237244
238245 @staticmethod
@@ -242,6 +249,10 @@ def scene_library_names(
242249 ) -> list [str ]:
243250 if not compilation_mode_uses_shared_libraries (compilation_mode ):
244251 return []
252+ if not compiled_frame_scenes (frame ):
253+ return []
254+ if compilation_mode == COMPILATION_MODE_SHARED_SCENES :
255+ return [scene_bundle_library_filename ()]
245256 return [scene_library_filename (scene ) for scene in compiled_frame_scenes (frame )]
246257
247258 async def _upload_frame_json (self , path : str ) -> None :
@@ -438,6 +449,9 @@ async def make_local_modifications(
438449 for scene in compiled_frame_scenes (frame ):
439450 with open (os .path .join (shared_scene_dir , scene_module_filename (scene )), "w" ) as sf :
440451 sf .write (write_scene_library_nim (scene ))
452+ if compilation_mode == COMPILATION_MODE_SHARED_SCENES :
453+ with open (os .path .join (source_dir , "src" , "scenes" , "scenes_bundle.nim" ), "w" ) as bf :
454+ bf .write (write_scenes_nim (frame , compilation_mode = COMPILATION_MODE_SHARED_SCENES ))
441455
442456 with open (os .path .join (source_dir , "src" , "scenes" , "scenes.nim" ), "w" ) as f :
443457 source = write_scenes_nim (frame , compilation_mode = compilation_mode )
@@ -828,6 +842,7 @@ async def create_local_build_archive(
828842 )
829843
830844 if compilation_mode_uses_shared_libraries (compilation_mode ):
845+ compiled_scenes = compiled_frame_scenes (frame )
831846 for driver in compiled_drivers (drivers ):
832847 driver_dir = os .path .join (build_dir , "drivers" , driver .name )
833848 os .makedirs (driver_dir , exist_ok = True )
@@ -866,23 +881,57 @@ async def create_local_build_archive(
866881 )
867882 driver_make_dirs .append (os .path .join ("drivers" , driver .name ))
868883
869- for scene in compiled_frame_scenes (frame ):
870- scene_dir_name = scene_module_suffix (scene )
871- scene_dir = os .path .join (build_dir , "scenes" , scene_dir_name )
884+ if compilation_mode == COMPILATION_MODE_SHARED :
885+ for scene in compiled_scenes :
886+ scene_dir_name = scene_module_suffix (scene )
887+ scene_dir = os .path .join (build_dir , "scenes" , scene_dir_name )
888+ os .makedirs (scene_dir , exist_ok = True )
889+ output_name = scene_library_filename (scene )
890+ await self .log ("stdout" , f"🔥 Generating C sources for scene { scene .get ('id' , 'default' )} ." )
891+ scene_cmd = (
892+ f"cd { source_dir } && { nim_path } compile --app:lib --os:linux --cpu:{ cpu } "
893+ f"--define:frameosSharedLibrary { ' ' .join (SHARED_LIBRARY_NIM_FLAGS )} "
894+ f"--compileOnly --genScript --nimcache:{ scene_dir } --out:{ output_name } "
895+ f"{ debug_options } src/scenes/shared/{ scene_module_filename (scene )} 2>&1"
896+ )
897+ scene_status , scene_out , scene_err = await exec_local_command (db , redis , frame , scene_cmd )
898+ if scene_status != 0 :
899+ raise Exception (
900+ f"Failed to generate scene library sources for { scene .get ('id' , 'default' )} : "
901+ f"{ scene_err or scene_out or 'see logs' } "
902+ )
903+ shutil .copy (nimbase_path , os .path .join (scene_dir , "nimbase.h" ))
904+
905+ scene_script_path = self ._find_compile_script (scene_dir )
906+ scene_linker_flags , scene_compiler_flags = self ._extract_compile_flags (
907+ scene_script_path , output_name
908+ )
909+ scene_linker_flags = self ._dedupe_preserve_order (
910+ scene_linker_flags + ["../../quickjs/libquickjs.a" ]
911+ )
912+ self ._write_driver_makefile (
913+ makefile_path = os .path .join (scene_dir , "Makefile" ),
914+ output_name = output_name ,
915+ linker_flags = scene_linker_flags ,
916+ compiler_flags = scene_compiler_flags ,
917+ library_kind = "scene" ,
918+ )
919+ scene_make_dirs .append (os .path .join ("scenes" , scene_dir_name ))
920+ elif compilation_mode == COMPILATION_MODE_SHARED_SCENES and compiled_scenes :
921+ scene_dir = os .path .join (build_dir , "scenes" )
872922 os .makedirs (scene_dir , exist_ok = True )
873- output_name = scene_library_filename ( scene )
874- await self .log ("stdout" , f "🔥 Generating C sources for scene { scene . get ( 'id' , 'default' ) } ." )
923+ output_name = scene_bundle_library_filename ( )
924+ await self .log ("stdout" , "🔥 Generating C sources for bundled shared scenes ." )
875925 scene_cmd = (
876926 f"cd { source_dir } && { nim_path } compile --app:lib --os:linux --cpu:{ cpu } "
877927 f"--define:frameosSharedLibrary { ' ' .join (SHARED_LIBRARY_NIM_FLAGS )} "
878928 f"--compileOnly --genScript --nimcache:{ scene_dir } --out:{ output_name } "
879- f"{ debug_options } src/scenes/shared/ { scene_module_filename ( scene ) } 2>&1"
929+ f"{ debug_options } src/scenes/scenes_bundle.nim 2>&1"
880930 )
881931 scene_status , scene_out , scene_err = await exec_local_command (db , redis , frame , scene_cmd )
882932 if scene_status != 0 :
883933 raise Exception (
884- f"Failed to generate scene library sources for { scene .get ('id' , 'default' )} : "
885- f"{ scene_err or scene_out or 'see logs' } "
934+ f"Failed to generate bundled scene library sources: { scene_err or scene_out or 'see logs' } "
886935 )
887936 shutil .copy (nimbase_path , os .path .join (scene_dir , "nimbase.h" ))
888937
@@ -891,7 +940,7 @@ async def create_local_build_archive(
891940 scene_script_path , output_name
892941 )
893942 scene_linker_flags = self ._dedupe_preserve_order (
894- scene_linker_flags + ["../../ quickjs/libquickjs.a" ]
943+ scene_linker_flags + ["../quickjs/libquickjs.a" ]
895944 )
896945 self ._write_driver_makefile (
897946 makefile_path = os .path .join (scene_dir , "Makefile" ),
@@ -900,7 +949,7 @@ async def create_local_build_archive(
900949 compiler_flags = scene_compiler_flags ,
901950 library_kind = "scene" ,
902951 )
903- scene_make_dirs .append (os . path . join ( "scenes" , scene_dir_name ) )
952+ scene_make_dirs .append ("scenes" )
904953
905954 self ._write_c_makefile (
906955 makefile_path = os .path .join (build_dir , "Makefile" ),
0 commit comments