2024-07-05 23:23:09 +03:00
from waflib . Configure import conf
from waflib import Build
2024-08-13 00:04:47 +03:00
import os
2024-07-05 23:23:09 +03:00
@conf
def SAMBA_CHECK_RUST ( conf ) :
conf . find_program ( ' cargo ' , var = ' CARGO ' ,
2024-08-27 23:47:29 +03:00
mandatory = conf . env . enable_rust )
2024-07-05 23:23:09 +03:00
2024-08-13 17:53:33 +03:00
def vendor_sources ( bld , enabled = True ) :
# force-disable when we can't build rust modules, so
# every single call doesn't need to pass this in.
2024-08-27 23:47:29 +03:00
if not bld . env . enable_rust :
2024-08-13 17:53:33 +03:00
enabled = False
# Save time, no need to build rust when fuzzing
if bld . env . enable_fuzzing :
enabled = False
# Determine the vendor directory
vendor = bld . path . find_or_declare ( ' ./vendor ' )
# WAF dependencies can only be explicit files, not directories, so we touch
# a file to indicate vendoring has been completed.
vendor_exists = ' %s .exists ' % vendor
# Locate the source manifest file
source_manifest = bld . path . find_or_declare ( ' ../../../rust/Cargo.toml ' )
rule = [ ' $ {CARGO} ' , ' vendor ' ,
' --manifest-path=$ { SRC[0].abspath(env)} ' ,
' %s ' % vendor ,
' && touch %s ' % vendor_exists ]
bld . SAMBA_GENERATOR ( ' vendor.exists ' ,
' ' . join ( rule ) ,
source = source_manifest ,
target = vendor_exists ,
group = ' final ' ,
enabled = enabled )
Build . BuildContext . vendor_sources = vendor_sources
2024-08-13 00:04:47 +03:00
def find_sources ( source_dir , dep_crate ) :
sources = [ ]
for root , dirs , files in os . walk ( os . path . join ( source_dir , dep_crate ) ) :
for file in files :
if os . path . splitext ( file ) [ - 1 ] in [ ' .rs ' , ' .c ' , ' .h ' ] :
sources . append ( os . path . join ( root , file ) )
return sources
def SAMBA_RUST ( bld , rust_subdir , target_name , dep_crates = [ ] , enabled = True ) :
2024-07-05 23:23:09 +03:00
# force-disable when we can't build rust modules, so
# every single call doesn't need to pass this in.
2024-08-27 23:47:29 +03:00
if not bld . env . enable_rust :
2024-07-05 23:23:09 +03:00
enabled = False
# Save time, no need to build rust when fuzzing
if bld . env . enable_fuzzing :
enabled = False
release_flag = ' '
if bld . env . debug or bld . env . developer :
2024-08-12 23:25:12 +03:00
target = os . path . join ( ' debug ' , target_name )
2024-07-05 23:23:09 +03:00
else :
release_flag = ' --release '
2024-08-12 23:25:12 +03:00
target = os . path . join ( ' release ' , target_name )
2024-07-05 23:23:09 +03:00
target = bld . path . find_or_declare ( target )
2024-08-12 23:25:12 +03:00
# The Rust target directory is one directory above the located target
target_dir = os . path . join ( os . path . dirname ( ' %s ' % target ) , ' ../ ' )
2024-08-13 00:04:47 +03:00
# Try to determine the source directory
source_dir = os . path . abspath ( os . path . join ( target_dir , ' ../../../rust ' ) )
if not os . path . exists ( source_dir ) :
raise Exception ( ' Failed to determine rust source directory ' )
# Now determine the sources of each local crate
sources = find_sources ( source_dir , rust_subdir )
for dep_crate in dep_crates :
sources . extend ( find_sources ( source_dir , dep_crate ) )
sources = [ os . path . relpath ( p , source_dir ) for p in sources ]
2024-07-05 23:23:09 +03:00
2024-08-12 23:25:12 +03:00
rule = [ ' $ {CARGO} ' , ' build ' ,
' --manifest-path=$ { SRC[0].abspath(env)} ' ,
' --target-dir= %s ' % target_dir ,
release_flag ]
bld . SAMBA_GENERATOR ( target_name ,
2024-07-05 23:23:09 +03:00
' ' . join ( rule ) ,
2024-08-13 17:53:33 +03:00
source = ' %s /Cargo.toml vendor.exists %s ' % \
( rust_subdir , ' ' . join ( sources ) ) ,
2024-07-05 23:23:09 +03:00
target = target ,
group = ' final ' ,
enabled = enabled )
Build . BuildContext . SAMBA_RUST_LIBRARY = SAMBA_RUST
Build . BuildContext . SAMBA_RUST_BINARY = SAMBA_RUST