fork/rules_rust
git clone https://git.julia.blue/fork/rules_rust
03a70796
0.8.0
1"""Unittest to verify contents and ordering of rust stdlib in rust_library() CcInfo""" 2 3load ( "@bazel_skylib//lib:unittest.bzl" , "analysistest" , "asserts" ) 4load ( "//rust:defs.bzl" , "rust_library" ) 5 6def _categorize_library ( name ): 7"""Given an rlib name, guess if it's std, core, or alloc.""" 8if "std" in name : 9return "std" 10if "core" in name : 11return "core" 12if "alloc" in name : 13return "alloc" 14if "compiler_builtins" in name : 15return "compiler_builtins" 16return "other" 17 18def _dedup_preserving_order ( list ): 19"""Given a list, deduplicate its elements preserving order.""" 20r = [] 21seen = {} 22for e in list : 23if e in seen : 24 continue 25seen [ e ] = 1 26r . append ( e ) 27return r 28 29def _stdlibs ( tut ): 30"""Given a target, return the list of its standard rust libraries.""" 31libs = [ 32lib . static_library 33for li in tut [ CcInfo ]. linking_context . linker_inputs . to_list () 34for lib in li . libraries 35] 36stdlibs = [ lib for lib in libs if ( tut . label . name not in lib . basename )] 37return stdlibs 38 39def _libstd_ordering_test_impl ( ctx ): 40env = analysistest . begin ( ctx ) 41tut = analysistest . target_under_test ( env ) 42stdlib_categories = [ _categorize_library ( lib . basename ) for lib in _stdlibs ( tut )] 43set_to_check = _dedup_preserving_order ([ lib for lib in stdlib_categories if lib != "other" ]) 44asserts . equals ( env , [ "std" , "core" , "compiler_builtins" , "alloc" ], set_to_check ) 45return analysistest . end ( env ) 46 47libstd_ordering_test = analysistest . make ( _libstd_ordering_test_impl ) 48 49def _libstd_panic_test_impl ( ctx ): 50# The libraries panic_unwind and panic_abort are alternatives. 51# Check that they don't occur together. 52env = analysistest . begin ( ctx ) 53tut = analysistest . target_under_test ( env ) 54stdlibs = _stdlibs ( tut ) 55has_panic_unwind = [ lib for lib in stdlibs if "panic_unwind" in lib . basename ] 56if has_panic_unwind : 57has_panic_abort = [ lib for lib in stdlibs if "panic_abort" in lib . basename ] 58asserts . false ( env , has_panic_abort ) 59 60return analysistest . end ( env ) 61 62libstd_panic_test = analysistest . make ( _libstd_panic_test_impl ) 63 64def _native_dep_test (): 65rust_library ( 66name = "some_rlib" , 67srcs = [ "some_rlib.rs" ], 68edition = "2018" , 69) 70 71libstd_ordering_test ( 72name = "libstd_ordering_test" , 73target_under_test = ":some_rlib" , 74) 75 76libstd_panic_test ( 77name = "libstd_panic_test" , 78target_under_test = ":some_rlib" , 79) 80 81def stdlib_suite ( name ): 82"""Entry-point macro called from the BUILD file. 83 84Args: 85name: Name of the macro. 86""" 87_native_dep_test () 88 89native . test_suite ( 90name = name , 91tests = [ 92":libstd_ordering_test" , 93], 94)