bdwgc.go raw

   1  package builder
   2  
   3  // The well-known conservative Boehm-Demers-Weiser GC.
   4  // This file provides a way to compile this GC for use with Moxie.
   5  
   6  import (
   7  	"path/filepath"
   8  	"strings"
   9  
  10  	"moxie/goenv"
  11  )
  12  
  13  var BoehmGC = Library{
  14  	name: "bdwgc",
  15  	cflags: func(target, headerPath string) []string {
  16  		libdir := filepath.Join(goenv.Get("MOXIEROOT"), "lib/bdwgc")
  17  		flags := []string{
  18  			// use a modern environment
  19  			"-DUSE_MMAP",              // mmap is available
  20  			"-DUSE_MUNMAP",            // return memory to the OS using munmap
  21  			"-DGC_BUILTIN_ATOMIC",     // use compiler intrinsics for atomic operations
  22  			"-DNO_EXECUTE_PERMISSION", // don't make the heap executable
  23  
  24  			// specific flags for Moxie
  25  			"-DALL_INTERIOR_POINTERS",  // scan interior pointers (needed for Go)
  26  			"-DIGNORE_DYNAMIC_LOADING", // we don't support dynamic loading at the moment
  27  			"-DNO_GETCONTEXT",          // musl doesn't support getcontext()
  28  			"-DGC_DISABLE_INCREMENTAL", // don't mess with SIGSEGV and such
  29  
  30  			// Use a minimal environment.
  31  			"-DNO_MSGBOX_ON_ERROR", // don't call MessageBoxA on Windows
  32  			"-DDONT_USE_ATEXIT",
  33  			"-DNO_GETENV",          // smaller binary, more predictable configuration
  34  			"-DNO_CLOCK",           // don't use system clock
  35  			"-DNO_DEBUGGING",       // reduce code size
  36  			"-DGC_NO_FINALIZATION", // finalization is not used at the moment
  37  
  38  			// Special flag to work around the lack of __data_start in ld.lld.
  39  			// TODO: try to fix this in LLVM/lld directly so we don't have to
  40  			// work around it anymore.
  41  			"-DGC_DONT_REGISTER_MAIN_STATIC_DATA",
  42  
  43  			// Do not scan the stack. We have our own mechanism to do this.
  44  			"-DSTACK_NOT_SCANNED",
  45  			"-DNO_PROC_STAT",  // we scan the stack manually (don't read /proc/self/stat on Linux)
  46  			"-DSTACKBOTTOM=0", // dummy value, we scan the stack manually
  47  
  48  			// Assertions can be enabled while debugging GC issues.
  49  			//"-DGC_ASSERTIONS",
  50  
  51  			// We use our own way of dealing with threads (that is a bit hacky).
  52  			// See src/runtime/gc_boehm.go.
  53  			//"-DGC_THREADS",
  54  			//"-DTHREAD_LOCAL_ALLOC",
  55  
  56  			"-I" + libdir + "/include",
  57  		}
  58  		return flags
  59  	},
  60  	needsLibc: true,
  61  	sourceDir: func() string {
  62  		return filepath.Join(goenv.Get("MOXIEROOT"), "lib/bdwgc")
  63  	},
  64  	librarySources: func(target string, _ bool) ([]string, error) {
  65  		sources := []string{
  66  			"allchblk.c",
  67  			"alloc.c",
  68  			"blacklst.c",
  69  			"dbg_mlc.c",
  70  			"dyn_load.c",
  71  			"headers.c",
  72  			"mach_dep.c",
  73  			"malloc.c",
  74  			"mark.c",
  75  			"mark_rts.c",
  76  			"misc.c",
  77  			"new_hblk.c",
  78  			"os_dep.c",
  79  			"reclaim.c",
  80  		}
  81  		if strings.Split(target, "-")[2] == "windows" {
  82  			// Due to how the linker on Windows works (that doesn't allow
  83  			// undefined functions), we need to include these extra files.
  84  			sources = append(sources,
  85  				"mallocx.c",
  86  				"ptr_chck.c",
  87  			)
  88  		}
  89  		return sources, nil
  90  	},
  91  }
  92