@@ -515,6 +515,178 @@ impl Step for Llvm {
515
515
return res;
516
516
}
517
517
518
+ if target. contains ( "wasi" ) {
519
+ let wasi_sysroot = env:: var ( "WASI_SYSROOT" ) . expect ( "WASI_SYSROOT not set" ) ;
520
+ let wasi_sdk_path = std:: path:: Path :: new ( & wasi_sysroot) . join ( "../../" ) . canonicalize ( ) . expect ( "invalid WASI_SYSROOT" ) ;
521
+ let wasi_sysroot = format ! ( "--sysroot={wasi_sysroot}" ) ;
522
+ let wasi_sdk_path = wasi_sdk_path. to_str ( ) . expect ( "invalid WASI_SYSROOT" ) ;
523
+ let wasi_target = target. triple . to_string ( ) ;
524
+ let wasi_cflags = String :: from ( "" ) ;
525
+ let wasi_ldflags = String :: from ( "" ) ;
526
+ let wasi_target_llvm = target. triple . to_string ( ) ;
527
+ let wasi_cflags_llvm = format ! ( "{wasi_cflags} -pthread" ) ;
528
+ let wasi_ldflags_llvm = wasi_ldflags;
529
+ // LLVM has some (unreachable in our configuration) calls to mmap.
530
+ let wasi_cflags_llvm = format ! ( "{wasi_cflags_llvm} -D_WASI_EMULATED_MMAN" ) ;
531
+ let wasi_ldflags_llvm = format ! ( "{wasi_ldflags_llvm} -lwasi-emulated-mman" ) ;
532
+ // Depending on the code being compiled, both Clang and LLD can consume unbounded amounts of memory.
533
+ let wasi_ldflags_llvm = format ! ( "{wasi_ldflags_llvm} -Wl,--max-memory=4294967296" ) ;
534
+ // Compiling C++ code requires a lot of stack space and can overflow and corrupt the heap.
535
+ // (For example, `#include <iostream>` alone does it in a build with the default stack size.)
536
+ let wasi_ldflags_llvm = format ! ( "{wasi_ldflags_llvm} -Wl,-z,stack-size=1048576 -Wl,--stack-first" ) ;
537
+ // Some of the host APIs that are statically required by LLVM (notably threading) are dynamically
538
+ // never used. An LTO build removes imports of these APIs, simplifying deployment
539
+ let wasi_cflags_llvm = format ! ( "{wasi_cflags_llvm} -flto" ) ;
540
+ let wasi_ldflags_llvm = format ! ( "{wasi_ldflags_llvm} -flto -Wl,--strip-all" ) ;
541
+
542
+ // We need two toolchain files: one for the compiler itself (which needs threads at the moment since
543
+ // -DLLVM_ENABLE_THREADS=OFF is kind of broken), and one for the runtime libs.
544
+ cfg. define ( "WASI" , "TRUE" )
545
+ . define ( "CMAKE_SYSTEM_NAME" , "Generic" )
546
+ . define ( "CMAKE_SYSTEM_VERSION" , "1" )
547
+ . define ( "CMAKE_SYSTEM_PROCESSOR" , "wasm32" )
548
+ . define ( "CMAKE_EXECUTABLE_SUFFIX" , ".wasm" )
549
+ . define ( "CMAKE_FIND_ROOT_PATH_MODE_PROGRAM" , "NEVER" )
550
+ . define ( "CMAKE_FIND_ROOT_PATH_MODE_LIBRARY" , "ONLY" )
551
+ . define ( "CMAKE_FIND_ROOT_PATH_MODE_INCLUDE" , "ONLY" )
552
+ . define ( "CMAKE_FIND_ROOT_PATH_MODE_PACKAGE" , "ONLY" )
553
+ . define ( "CMAKE_C_COMPILER" , format ! ( "{wasi_sdk_path}/bin/{wasi_target}-clang" ) )
554
+ . define ( "CMAKE_CXX_COMPILER" , format ! ( "{wasi_sdk_path}/bin/{wasi_target}-clang++" ) )
555
+ . define ( "CMAKE_LINKER" , format ! ( "{wasi_sdk_path}/bin/wasm-ld" ) )
556
+ . define ( "CMAKE_AR" , format ! ( "{wasi_sdk_path}/bin/ar" ) )
557
+ . define ( "CMAKE_RANLIB" , format ! ( "{wasi_sdk_path}/bin/ranlib" ) )
558
+ . define ( "CMAKE_C_COMPILER_TARGET" , & wasi_target_llvm)
559
+ . define ( "CMAKE_CXX_COMPILER_TARGET" , & wasi_target_llvm)
560
+ . define ( "CMAKE_C_FLAGS" , format ! ( "{wasi_sysroot} {wasi_cflags_llvm}" ) )
561
+ . define ( "CMAKE_CXX_FLAGS" , format ! ( "{wasi_sysroot} {wasi_cflags_llvm}" ) )
562
+ . define ( "CMAKE_EXE_LINKER_FLAGS" , wasi_ldflags_llvm)
563
+ . define ( "LLVM_BUILD_SHARED_LIBS" , "OFF" )
564
+ . define ( "LLVM_ENABLE_PIC" , "OFF" )
565
+ . define ( "LLVM_BUILD_STATIC" , "ON" )
566
+ // .define("LLVM_ENABLE_THREADS", "OFF")
567
+ . define ( "LLVM_ENABLE_THREADS" , "ON" )
568
+ . define ( "LLVM_BUILD_RUNTIME" , "OFF" )
569
+ . define ( "LLVM_BUILD_TOOLS" , "OFF" )
570
+ . define ( "LLVM_INCLUDE_UTILS" , "OFF" )
571
+ . define ( "LLVM_BUILD_UTILS" , "OFF" )
572
+ . define ( "LLVM_INCLUDE_RUNTIMES" , "OFF" )
573
+ . define ( "LLVM_INCLUDE_EXAMPLES" , "OFF" )
574
+ . define ( "LLVM_INCLUDE_TESTS" , "OFF" )
575
+ . define ( "LLVM_INCLUDE_BENCHMARKS" , "OFF" )
576
+ . define ( "LLVM_INCLUDE_DOCS" , "OFF" )
577
+ . define ( "LLVM_TOOL_BUGPOINT_BUILD" , "OFF" )
578
+ . define ( "LLVM_TOOL_BUGPOINT_PASSES_BUILD" , "OFF" )
579
+ . define ( "LLVM_TOOL_DSYMUTIL_BUILD" , "OFF" )
580
+ . define ( "LLVM_TOOL_DXIL_DIS_BUILD" , "OFF" )
581
+ . define ( "LLVM_TOOL_GOLD_BUILD" , "OFF" )
582
+ . define ( "LLVM_TOOL_LLC_BUILD" , "OFF" )
583
+ . define ( "LLVM_TOOL_LLI_BUILD" , "OFF" )
584
+ // .define("LLVM_TOOL_LLVM_AR_BUILD", "ON")
585
+ // .define("LLVM_TOOL_LLVM_AS_BUILD", "ON")
586
+ . define ( "LLVM_TOOL_LLVM_AS_FUZZER_BUILD" , "OFF" )
587
+ . define ( "LLVM_TOOL_LLVM_BCANALYZER_BUILD" , "OFF" )
588
+ . define ( "LLVM_TOOL_LLVM_CAT_BUILD" , "OFF" )
589
+ . define ( "LLVM_TOOL_LLVM_CFI_VERIFY_BUILD" , "OFF" )
590
+ . define ( "LLVM_TOOL_LLVM_CONFIG_BUILD" , "OFF" )
591
+ // .define("LLVM_TOOL_LLVM_COV_BUILD", "ON")
592
+ . define ( "LLVM_TOOL_LLVM_CVTRES_BUILD" , "OFF" )
593
+ . define ( "LLVM_TOOL_LLVM_CXXDUMP_BUILD" , "OFF" )
594
+ // .define("LLVM_TOOL_LLVM_CXXFILT_BUILD", "ON")
595
+ . define ( "LLVM_TOOL_LLVM_CXXMAP_BUILD" , "OFF" )
596
+ . define ( "LLVM_TOOL_LLVM_C_TEST_BUILD" , "OFF" )
597
+ . define ( "LLVM_TOOL_LLVM_DEBUGINFOD_BUILD" , "OFF" )
598
+ . define ( "LLVM_TOOL_LLVM_DEBUGINFOD_FIND_BUILD" , "OFF" )
599
+ . define ( "LLVM_TOOL_LLVM_DEBUGINFO_ANALYZER_BUILD" , "OFF" )
600
+ . define ( "LLVM_TOOL_LLVM_DIFF_BUILD" , "OFF" )
601
+ . define ( "LLVM_TOOL_LLVM_DIS_BUILD" , "OFF" )
602
+ . define ( "LLVM_TOOL_LLVM_DIS_FUZZER_BUILD" , "OFF" )
603
+ . define ( "LLVM_TOOL_LLVM_DLANG_DEMANGLE_FUZZER_BUILD" , "OFF" )
604
+ // .define("LLVM_TOOL_LLVM_DRIVER_BUILD", "ON")
605
+ // .define("LLVM_TOOL_LLVM_DWARFDUMP_BUILD", "ON")
606
+ . define ( "LLVM_TOOL_LLVM_DWARFUTIL_BUILD" , "OFF" )
607
+ . define ( "LLVM_TOOL_LLVM_DWP_BUILD" , "OFF" )
608
+ . define ( "LLVM_TOOL_LLVM_EXEGESIS_BUILD" , "OFF" )
609
+ . define ( "LLVM_TOOL_LLVM_EXTRACT_BUILD" , "OFF" )
610
+ . define ( "LLVM_TOOL_LLVM_GSYMUTIL_BUILD" , "OFF" )
611
+ . define ( "LLVM_TOOL_LLVM_IFS_BUILD" , "OFF" )
612
+ . define ( "LLVM_TOOL_LLVM_ISEL_FUZZER_BUILD" , "OFF" )
613
+ . define ( "LLVM_TOOL_LLVM_ITANIUM_DEMANGLE_FUZZER_BUILD" , "OFF" )
614
+ . define ( "LLVM_TOOL_LLVM_JITLINK_BUILD" , "OFF" )
615
+ . define ( "LLVM_TOOL_LLVM_JITLISTENER_BUILD" , "OFF" )
616
+ . define ( "LLVM_TOOL_LLVM_LIBTOOL_DARWIN_BUILD" , "OFF" )
617
+ . define ( "LLVM_TOOL_LLVM_LINK_BUILD" , "ON" )
618
+ . define ( "LLVM_TOOL_LLVM_LIPO_BUILD" , "OFF" )
619
+ . define ( "LLVM_TOOL_LLVM_LTO2_BUILD" , "OFF" )
620
+ . define ( "LLVM_TOOL_LLVM_LTO_BUILD" , "OFF" )
621
+ . define ( "LLVM_TOOL_LLVM_MCA_BUILD" , "OFF" )
622
+ . define ( "LLVM_TOOL_LLVM_MC_ASSEMBLE_FUZZER_BUILD" , "OFF" )
623
+ . define ( "LLVM_TOOL_LLVM_MC_BUILD" , "OFF" )
624
+ . define ( "LLVM_TOOL_LLVM_MC_DISASSEMBLE_FUZZER_BUILD" , "OFF" )
625
+ . define ( "LLVM_TOOL_LLVM_MICROSOFT_DEMANGLE_FUZZER_BUILD" , "OFF" )
626
+ . define ( "LLVM_TOOL_LLVM_ML_BUILD" , "OFF" )
627
+ . define ( "LLVM_TOOL_LLVM_MODEXTRACT_BUILD" , "OFF" )
628
+ . define ( "LLVM_TOOL_LLVM_MT_BUILD" , "OFF" )
629
+ . define ( "LLVM_TOOL_LLVM_NM_BUILD" , "OFF" )
630
+ // .define("LLVM_TOOL_LLVM_OBJCOPY_BUILD", "ON")
631
+ // .define("LLVM_TOOL_LLVM_OBJDUMP_BUILD", "ON")
632
+ . define ( "LLVM_TOOL_LLVM_OPT_FUZZER_BUILD" , "OFF" )
633
+ . define ( "LLVM_TOOL_LLVM_OPT_REPORT_BUILD" , "OFF" )
634
+ . define ( "LLVM_TOOL_LLVM_PDBUTIL_BUILD" , "OFF" )
635
+ . define ( "LLVM_TOOL_LLVM_PROFDATA_BUILD" , "OFF" )
636
+ . define ( "LLVM_TOOL_LLVM_PROFGEN_BUILD" , "OFF" )
637
+ . define ( "LLVM_TOOL_LLVM_RC_BUILD" , "OFF" )
638
+ . define ( "LLVM_TOOL_LLVM_READOBJ_BUILD" , "OFF" )
639
+ . define ( "LLVM_TOOL_LLVM_READTAPI_BUILD" , "OFF" )
640
+ . define ( "LLVM_TOOL_LLVM_REDUCE_BUILD" , "OFF" )
641
+ . define ( "LLVM_TOOL_LLVM_REMARKUTIL_BUILD" , "OFF" )
642
+ . define ( "LLVM_TOOL_LLVM_RTDYLD_BUILD" , "OFF" )
643
+ . define ( "LLVM_TOOL_LLVM_RUST_DEMANGLE_FUZZER_BUILD" , "OFF" )
644
+ . define ( "LLVM_TOOL_LLVM_SHLIB_BUILD" , "OFF" )
645
+ . define ( "LLVM_TOOL_LLVM_SIM_BUILD" , "OFF" )
646
+ . define ( "LLVM_TOOL_LLVM_SIZE_BUILD" , "OFF" )
647
+ . define ( "LLVM_TOOL_LLVM_SPECIAL_CASE_LIST_FUZZER_BUILD" , "OFF" )
648
+ . define ( "LLVM_TOOL_LLVM_SPLIT_BUILD" , "OFF" )
649
+ . define ( "LLVM_TOOL_LLVM_STRESS_BUILD" , "OFF" )
650
+ . define ( "LLVM_TOOL_LLVM_STRINGS_BUILD" , "OFF" )
651
+ . define ( "LLVM_TOOL_LLVM_SYMBOLIZER_BUILD" , "ON" )
652
+ . define ( "LLVM_TOOL_LLVM_TLI_CHECKER_BUILD" , "OFF" )
653
+ . define ( "LLVM_TOOL_LLVM_UNDNAME_BUILD" , "OFF" )
654
+ . define ( "LLVM_TOOL_LLVM_XRAY_BUILD" , "OFF" )
655
+ . define ( "LLVM_TOOL_LLVM_YAML_NUMERIC_PARSER_FUZZER_BUILD" , "OFF" )
656
+ . define ( "LLVM_TOOL_LLVM_YAML_PARSER_FUZZER_BUILD" , "OFF" )
657
+ . define ( "LLVM_TOOL_LTO_BUILD" , "OFF" )
658
+ . define ( "LLVM_TOOL_OBJ2YAML_BUILD" , "OFF" )
659
+ . define ( "LLVM_TOOL_OPT_BUILD" , "OFF" )
660
+ . define ( "LLVM_TOOL_OPT_VIEWER_BUILD" , "OFF" )
661
+ . define ( "LLVM_TOOL_REDUCE_CHUNK_LIST_BUILD" , "OFF" )
662
+ . define ( "LLVM_TOOL_REMARKS_SHLIB_BUILD" , "OFF" )
663
+ . define ( "LLVM_TOOL_SANCOV_BUILD" , "OFF" )
664
+ . define ( "LLVM_TOOL_SANSTATS_BUILD" , "OFF" )
665
+ . define ( "LLVM_TOOL_SPIRV_TOOLS_BUILD" , "OFF" )
666
+ . define ( "LLVM_TOOL_VERIFY_USELISTORDER_BUILD" , "OFF" )
667
+ . define ( "LLVM_TOOL_VFABI_DEMANGLE_FUZZER_BUILD" , "OFF" )
668
+ . define ( "LLVM_TOOL_XCODE_TOOLCHAIN_BUILD" , "OFF" )
669
+ . define ( "LLVM_TOOL_YAML2OBJ_BUILD" , "OFF" )
670
+ // .define("LLVM_ENABLE_PROJECTS", "clang;lld")
671
+ . define ( "LLVM_ENABLE_PROJECTS" , "" )
672
+ // .define("CLANG_ENABLE_ARCMT", "OFF")
673
+ // .define("CLANG_ENABLE_STATIC_ANALYZER", "OFF")
674
+ // .define("CLANG_INCLUDE_TESTS", "OFF")
675
+ // .define("CLANG_BUILD_TOOLS", "OFF")
676
+ // .define("CLANG_TOOL_CLANG_SCAN_DEPS_BUILD", "OFF")
677
+ // .define("CLANG_TOOL_CLANG_INSTALLAPI_BUILD", "OFF")
678
+ // .define("CLANG_BUILD_EXAMPLES", "OFF")
679
+ // .define("CLANG_INCLUDE_DOCS", "OFF")
680
+ // .define("CLANG_LINKS_TO_CREATE", "clang;clang++")
681
+ // .define("CLANG_LINKS_TO_CREATE", "")
682
+ . define ( "LLD_BUILD_TOOLS" , "OFF" )
683
+ . define ( "CMAKE_BUILD_TYPE" , "MinSizeRel" )
684
+ . define ( "HAVE_DLOPEN" , "" ) ;
685
+ } else {
686
+ cfg. define ( "LLVM_TOOL_LLVM_CONFIG_BUILD" , "ON" )
687
+ . define ( "LLVM_BUILD_TOOLS" , "ON" ) ;
688
+ }
689
+
518
690
cfg. build ( ) ;
519
691
520
692
// Helper to find the name of LLVM's shared library on darwin and linux.
0 commit comments