@@ -11,7 +11,7 @@ use crate::{
1111 } ,
1212 Architecture , BuildConfig , CompilerConfig , CompilerVersion ,
1313} ;
14- use anyhow:: { bail, ensure, Result } ;
14+ use anyhow:: { bail, ensure, Context , Result } ;
1515use colored:: Colorize ;
1616use itertools:: { Either , Itertools } ;
1717use move_abigen:: { Abigen , AbigenOptions } ;
@@ -81,6 +81,8 @@ pub struct CompiledPackage {
8181 pub root_compiled_units : Vec < CompiledUnitWithSource > ,
8282 /// The output compiled bytecode for dependencies
8383 pub deps_compiled_units : Vec < ( PackageName , CompiledUnitWithSource ) > ,
84+ /// Bytecode dependencies of this compiled package
85+ pub bytecode_deps : BTreeMap < PackageName , NumericalAddress > ,
8486
8587 // Optional artifacts from compilation
8688 //
@@ -100,6 +102,7 @@ pub struct OnDiskPackage {
100102 pub compiled_package_info : CompiledPackageInfo ,
101103 /// Dependency names for this package.
102104 pub dependencies : Vec < PackageName > ,
105+ pub bytecode_deps : Vec < PackageName > ,
103106}
104107
105108#[ derive( Debug , Clone , Serialize , Deserialize ) ]
@@ -198,6 +201,8 @@ impl OnDiskCompiledPackage {
198201 compiled_package_info : self . package . compiled_package_info . clone ( ) ,
199202 root_compiled_units,
200203 deps_compiled_units,
204+ // TODO: support bytecode deps
205+ bytecode_deps : BTreeMap :: new ( ) ,
201206 compiled_docs,
202207 compiled_abis,
203208 } )
@@ -631,72 +636,75 @@ impl CompiledPackage {
631636 let effective_language_version = config. language_version . unwrap_or_default ( ) ;
632637 effective_compiler_version. check_language_support ( effective_language_version) ?;
633638
634- let ( file_map, all_compiled_units, optional_global_env) = match config
635- . compiler_version
636- . unwrap_or_default ( )
637- {
638- CompilerVersion :: V1 => {
639- let mut paths = src_deps;
640- paths. push ( sources_package_paths. clone ( ) ) ;
641- let compiler =
642- Compiler :: from_package_paths ( paths, bytecode_deps, flags, & known_attributes) ;
643- compiler_driver_v1 ( compiler) ?
644- } ,
645- version @ CompilerVersion :: V2_0 | version @ CompilerVersion :: V2_1 => {
646- let to_str_vec = |ps : & [ Symbol ] | {
647- ps. iter ( )
648- . map ( move |s| s. as_str ( ) . to_owned ( ) )
649- . collect :: < Vec < _ > > ( )
650- } ;
651- let mut global_address_map = BTreeMap :: new ( ) ;
652- for pack in std:: iter:: once ( & sources_package_paths)
653- . chain ( src_deps. iter ( ) )
654- . chain ( bytecode_deps. iter ( ) )
655- {
656- for ( name, val) in & pack. named_address_map {
657- if let Some ( old) = global_address_map. insert ( name. as_str ( ) . to_owned ( ) , * val)
658- {
659- if old != * val {
660- let pack_name = pack
661- . name
662- . map ( |s| s. as_str ( ) . to_owned ( ) )
663- . unwrap_or_else ( || "<unnamed>" . to_owned ( ) ) ;
664- bail ! (
639+ let ( file_map, all_compiled_units, optional_global_env) =
640+ match config. compiler_version . unwrap_or_default ( ) {
641+ CompilerVersion :: V1 => {
642+ let mut paths = src_deps;
643+ paths. push ( sources_package_paths. clone ( ) ) ;
644+ let compiler = Compiler :: from_package_paths (
645+ paths,
646+ bytecode_deps. clone ( ) ,
647+ flags,
648+ & known_attributes,
649+ ) ;
650+ compiler_driver_v1 ( compiler) ?
651+ } ,
652+ version @ CompilerVersion :: V2_0 | version @ CompilerVersion :: V2_1 => {
653+ let to_str_vec = |ps : & [ Symbol ] | {
654+ ps. iter ( )
655+ . map ( move |s| s. as_str ( ) . to_owned ( ) )
656+ . collect :: < Vec < _ > > ( )
657+ } ;
658+ let mut global_address_map = BTreeMap :: new ( ) ;
659+ for pack in std:: iter:: once ( & sources_package_paths)
660+ . chain ( src_deps. iter ( ) )
661+ . chain ( bytecode_deps. iter ( ) )
662+ {
663+ for ( name, val) in & pack. named_address_map {
664+ if let Some ( old) =
665+ global_address_map. insert ( name. as_str ( ) . to_owned ( ) , * val)
666+ {
667+ if old != * val {
668+ let pack_name = pack
669+ . name
670+ . map ( |s| s. as_str ( ) . to_owned ( ) )
671+ . unwrap_or_else ( || "<unnamed>" . to_owned ( ) ) ;
672+ bail ! (
665673 "found remapped address alias `{}` (`{} != {}`) in package `{}`\
666674 , please use unique address aliases across dependencies",
667675 name, old, val, pack_name
668676 )
677+ }
669678 }
670679 }
671680 }
672- }
673- let mut options = move_compiler_v2:: Options {
674- sources : sources_package_paths
675- . paths
676- . iter ( )
677- . map ( |path| path. as_str ( ) . to_owned ( ) )
678- . collect ( ) ,
679- sources_deps : src_deps. iter ( ) . flat_map ( |x| to_str_vec ( & x. paths ) ) . collect ( ) ,
680- dependencies : bytecode_deps
681- . iter ( )
682- . flat_map ( |x| to_str_vec ( & x. paths ) )
683- . collect ( ) ,
684- named_address_mapping : global_address_map
685- . into_iter ( )
686- . map ( |( k, v) | format ! ( "{}={}" , k, v) )
687- . collect ( ) ,
688- skip_attribute_checks,
689- known_attributes : known_attributes. clone ( ) ,
690- language_version : Some ( effective_language_version) ,
691- compiler_version : Some ( version) ,
692- compile_test_code : flags. keep_testing_functions ( ) ,
693- experiments : config. experiments . clone ( ) ,
694- ..Default :: default ( )
695- } ;
696- options = options. set_experiment ( Experiment :: ATTACH_COMPILED_MODULE , true ) ;
697- compiler_driver_v2 ( options) ?
698- } ,
699- } ;
681+ let mut options = move_compiler_v2:: Options {
682+ sources : sources_package_paths
683+ . paths
684+ . iter ( )
685+ . map ( |path| path. as_str ( ) . to_owned ( ) )
686+ . collect ( ) ,
687+ sources_deps : src_deps. iter ( ) . flat_map ( |x| to_str_vec ( & x. paths ) ) . collect ( ) ,
688+ dependencies : bytecode_deps
689+ . iter ( )
690+ . flat_map ( |x| to_str_vec ( & x. paths ) )
691+ . collect ( ) ,
692+ named_address_mapping : global_address_map
693+ . into_iter ( )
694+ . map ( |( k, v) | format ! ( "{}={}" , k, v) )
695+ . collect ( ) ,
696+ skip_attribute_checks,
697+ known_attributes : known_attributes. clone ( ) ,
698+ language_version : Some ( effective_language_version) ,
699+ compiler_version : Some ( version) ,
700+ compile_test_code : flags. keep_testing_functions ( ) ,
701+ experiments : config. experiments . clone ( ) ,
702+ ..Default :: default ( )
703+ } ;
704+ options = options. set_experiment ( Experiment :: ATTACH_COMPILED_MODULE , true ) ;
705+ compiler_driver_v2 ( options) ?
706+ } ,
707+ } ;
700708 let mut root_compiled_units = vec ! [ ] ;
701709 let mut deps_compiled_units = vec ! [ ] ;
702710 let obtain_package_name =
@@ -794,14 +802,24 @@ impl CompiledPackage {
794802 } ;
795803
796804 let compiled_package = CompiledPackage {
805+ root_compiled_units,
806+ deps_compiled_units,
807+ bytecode_deps : bytecode_deps
808+ . iter ( )
809+ . flat_map ( |package| {
810+ let name = package. name . unwrap ( ) ;
811+ package. paths . iter ( ) . map ( move |pkg_path| {
812+ get_addr_from_module_in_package ( name, pkg_path. as_str ( ) )
813+ . map ( |addr| ( name, addr) )
814+ } )
815+ } )
816+ . try_collect ( ) ?,
797817 compiled_package_info : CompiledPackageInfo {
798818 package_name : resolved_package. source_package . package . name ,
799819 address_alias_instantiation : resolved_package. resolution_table ,
800820 source_digest : Some ( resolved_package. source_digest ) ,
801821 build_flags : resolution_graph. build_options . clone ( ) ,
802822 } ,
803- root_compiled_units,
804- deps_compiled_units,
805823 compiled_docs,
806824 compiled_abis,
807825 } ;
@@ -888,6 +906,13 @@ impl CompiledPackage {
888906 . collect :: < BTreeSet < _ > > ( )
889907 . into_iter ( )
890908 . collect ( ) ,
909+ bytecode_deps : self
910+ . bytecode_deps
911+ . keys ( )
912+ . copied ( )
913+ . collect :: < BTreeSet < _ > > ( )
914+ . into_iter ( )
915+ . collect ( ) ,
891916 } ,
892917 } ;
893918
@@ -1138,3 +1163,23 @@ pub fn build_and_report_no_exit_v2_driver(
11381163 Some ( env) ,
11391164 ) )
11401165}
1166+
1167+ /// Returns the address of the module
1168+ fn get_addr_from_module_in_package ( pkg_name : Symbol , pkg_path : & str ) -> Result < NumericalAddress > {
1169+ // Read the bytecode file
1170+ let mut bytecode = Vec :: new ( ) ;
1171+ std:: fs:: File :: open ( pkg_path)
1172+ . context ( format ! ( "Failed to open bytecode file for {}" , pkg_path) )
1173+ . and_then ( |mut file| {
1174+ // read contents of the file into bytecode
1175+ std:: io:: Read :: read_to_end ( & mut file, & mut bytecode)
1176+ . context ( format ! ( "Failed to read bytecode file {}" , pkg_path) )
1177+ } )
1178+ . and_then ( |_| {
1179+ CompiledModule :: deserialize ( & bytecode) . context ( format ! (
1180+ "Failed to deserialize bytecode file for {}" ,
1181+ pkg_name
1182+ ) )
1183+ } )
1184+ . map ( |module| NumericalAddress :: from_account_address ( * module. self_addr ( ) ) )
1185+ }
0 commit comments