1- use alloc:: collections:: BTreeSet ;
2- use std:: fs;
3- use std:: path:: { Path , PathBuf } ;
4-
5- extern crate alloc;
6-
71fn main ( ) {
82 let target = std:: env:: var ( "CARGO_CFG_TARGET_OS" ) . unwrap ( ) ;
93 let capi_enabled = std:: env:: var_os ( "CARGO_FEATURE_CAPI" ) . is_some ( ) ;
@@ -17,14 +11,12 @@ fn main() {
1711 }
1812 "windows" => {
1913 if capi_enabled {
20- export_capi_symbols_for_windows_exe ( ) . unwrap_or_else ( |e| {
21- panic ! ( "Failed to generate Windows C-API exports definition: {e}" )
22- } ) ;
14+ export_capi_symbols_for_windows_exe ( ) ;
2315 }
2416
2517 println ! ( "cargo:rerun-if-changed=logo.ico" ) ;
2618 let mut res = winresource:: WindowsResource :: new ( ) ;
27- if Path :: new ( "logo.ico" ) . exists ( ) {
19+ if std :: path :: Path :: new ( "logo.ico" ) . exists ( ) {
2820 res. set_icon ( "logo.ico" ) ;
2921 } else {
3022 println ! ( "cargo:warning=logo.ico not found, skipping icon embedding" ) ;
@@ -40,133 +32,20 @@ fn main() {
4032 }
4133}
4234
43- fn export_capi_symbols_for_windows_exe ( ) -> Result < ( ) , String > {
44- let mut files = Vec :: new ( ) ;
45- let mut symbols = BTreeSet :: new ( ) ;
46- let capi_src = Path :: new ( "crates" ) . join ( "capi" ) . join ( "src" ) ;
47- println ! ( "cargo:rerun-if-changed={}" , capi_src. display( ) ) ;
48- collect_rs_files ( & capi_src, & mut files)
49- . map_err ( |e| format ! ( "failed to enumerate C-API sources: {e}" ) ) ?;
50-
51- for file in & files {
52- println ! ( "cargo:rerun-if-changed={}" , file. display( ) ) ;
53- let content = fs:: read_to_string ( file)
54- . map_err ( |e| format ! ( "failed to read {}: {e}" , file. display( ) ) ) ?;
55- extract_define_exception_statics_symbols ( & content, & mut symbols) ;
56- extract_no_mangle_symbols ( & content, & mut symbols) ;
57- }
58-
59- if symbols. is_empty ( ) {
60- return Err ( "no C-API symbols discovered" . to_owned ( ) ) ;
61- }
62-
63- let out_dir = std:: env:: var_os ( "OUT_DIR" ) . ok_or_else ( || "OUT_DIR is not set" . to_owned ( ) ) ?;
64- let def_path = PathBuf :: from ( out_dir) . join ( "rustpython-capi-exports.def" ) ;
65- let mut def = String :: from ( "EXPORTS\n " ) ;
66- for name in & symbols {
67- println ! ( "cargo:rustc-link-arg-bin=rustpython=/INCLUDE:{name}" ) ;
68- def. push_str ( name) ;
69- def. push ( '\n' ) ;
70- }
71- fs:: write ( & def_path, def)
72- . map_err ( |e| format ! ( "failed to write {}: {e}" , def_path. display( ) ) ) ?;
73-
74- println ! (
75- "cargo:warning=Windows C-API export list has {} symbols" ,
76- symbols. len( )
77- ) ;
35+ fn export_capi_symbols_for_windows_exe ( ) {
36+ let exports_def_filename = "rustpython-capi-exports.def" ;
37+ println ! ( "cargo:rerun-if-changed={exports_def_filename}" ) ;
7838
79- println ! (
80- "cargo:rustc-link-arg-bin=rustpython=/DEF:{}" ,
81- def_path. display( )
82- ) ;
83- Ok ( ( ) )
84- }
39+ let exports = std:: fs:: read_to_string ( exports_def_filename)
40+ . expect ( "Failed to read exports definition file" ) ;
8541
86- fn collect_rs_files ( dir : & Path , files : & mut Vec < PathBuf > ) -> std:: io:: Result < ( ) > {
87- for entry in fs:: read_dir ( dir) ? {
88- let entry = entry?;
89- let path = entry. path ( ) ;
90- if path. is_dir ( ) {
91- collect_rs_files ( & path, files) ?;
92- } else if path. extension ( ) . is_some_and ( |ext| ext == "rs" ) {
93- files. push ( path) ;
94- }
42+ for export in exports
43+ . lines ( )
44+ . skip ( 1 )
45+ . filter ( |line| !line. trim ( ) . is_empty ( ) )
46+ {
47+ println ! ( "cargo:rustc-link-arg-bin=rustpython=/INCLUDE:{export}" ) ;
9548 }
96- Ok ( ( ) )
97- }
98-
99- fn extract_define_exception_statics_symbols ( content : & str , symbols : & mut BTreeSet < String > ) {
100- const BLOCK_START : & str = "define_exception_statics!" ;
101- let Some ( start) = content. find ( BLOCK_START ) else {
102- return ;
103- } ;
104- let Some ( open) = content[ start..] . find ( '{' ) else {
105- return ;
106- } ;
107- let after_open = start + open + 1 ;
108- let Some ( close) = content[ after_open..] . find ( '}' ) else {
109- return ;
110- } ;
111- let block = & content[ after_open..after_open + close] ;
112- block. lines ( ) . for_each ( |line| {
113- let trimmed = line. trim_start ( ) ;
114- if let Some ( ( name, _) ) = trimmed. split_once ( "=>" ) {
115- let symbol: String = name
116- . trim ( )
117- . chars ( )
118- . take_while ( |c| c. is_ascii_alphanumeric ( ) || * c == '_' )
119- . collect ( ) ;
120- if !symbol. is_empty ( ) {
121- symbols. insert ( symbol) ;
122- }
123- }
124- } ) ;
125- }
126-
127- fn extract_no_mangle_symbols ( content : & str , symbols : & mut BTreeSet < String > ) {
128- let mut saw_no_mangle = false ;
129-
130- content. lines ( ) . for_each ( |line| {
131- let trimmed = line. trim ( ) ;
132- if trimmed == "#[unsafe(no_mangle)]" {
133- saw_no_mangle = true ;
134- return ;
135- }
136- if !saw_no_mangle {
137- return ;
138- }
139-
140- if trimmed. is_empty ( ) || trimmed. starts_with ( '#' ) || trimmed. starts_with ( "//" ) {
141- return ;
142- }
143-
144- if let Some ( rest) = trimmed. strip_prefix ( "pub static mut " ) {
145- let symbol: String = rest
146- . chars ( )
147- . take_while ( |c| c. is_ascii_alphanumeric ( ) || * c == '_' )
148- . collect ( ) ;
149- if !symbol. is_empty ( ) {
150- symbols. insert ( symbol) ;
151- }
152- } else if let Some ( rest) = trimmed. strip_prefix ( "pub static " ) {
153- let symbol: String = rest
154- . chars ( )
155- . take_while ( |c| c. is_ascii_alphanumeric ( ) || * c == '_' )
156- . collect ( ) ;
157- if !symbol. is_empty ( ) {
158- symbols. insert ( symbol) ;
159- }
160- } else if let Some ( rest) = trimmed. split_once ( "extern \" C\" fn " ) . map ( |( _, rest) | rest) {
161- let symbol: String = rest
162- . chars ( )
163- . take_while ( |c| c. is_ascii_alphanumeric ( ) || * c == '_' )
164- . collect ( ) ;
165- if !symbol. is_empty ( ) {
166- symbols. insert ( symbol) ;
167- }
168- }
16949
170- saw_no_mangle = false ;
171- } ) ;
50+ println ! ( "cargo:rustc-link-arg-bin=rustpython=/DEF:{exports_def_filename}" , ) ;
17251}
0 commit comments