darwin-libsystem.go raw

   1  package builder
   2  
   3  import (
   4  	"path/filepath"
   5  	"strings"
   6  
   7  	"moxie/compileopts"
   8  	"moxie/goenv"
   9  )
  10  
  11  // Create a job that builds a Darwin libSystem.dylib stub library. This library
  12  // contains all the symbols needed so that we can link against it, but it
  13  // doesn't contain any real symbol implementations.
  14  func makeDarwinLibSystemJob(config *compileopts.Config, tmpdir string) *compileJob {
  15  	return &compileJob{
  16  		description: "compile Darwin libSystem.dylib",
  17  		run: func(job *compileJob) (err error) {
  18  			arch := strings.Split(config.Triple(), "-")[0]
  19  			job.result = filepath.Join(tmpdir, "libSystem.dylib")
  20  			objpath := filepath.Join(tmpdir, "libSystem.o")
  21  			inpath := filepath.Join(goenv.Get("MOXIEROOT"), "lib/macos-minimal-sdk/src", arch, "libSystem.s")
  22  
  23  			// Compile assembly file to object file.
  24  			flags := []string{
  25  				"-nostdlib",
  26  				"--target=" + config.Triple(),
  27  				"-c",
  28  				"-o", objpath,
  29  				inpath,
  30  			}
  31  			if config.Options.PrintCommands != nil {
  32  				config.Options.PrintCommands("clang", flags...)
  33  			}
  34  			err = runCCompiler(flags...)
  35  			if err != nil {
  36  				return err
  37  			}
  38  
  39  			// Link object file to dynamic library.
  40  			platformVersion := strings.TrimPrefix(strings.Split(config.Triple(), "-")[2], "macosx")
  41  			flags = []string{
  42  				"-flavor", "darwin",
  43  				"-demangle",
  44  				"-dynamic",
  45  				"-dylib",
  46  				"-arch", arch,
  47  				"-platform_version", "macos", platformVersion, platformVersion,
  48  				"-install_name", "/usr/lib/libSystem.B.dylib",
  49  				"-o", job.result,
  50  				objpath,
  51  			}
  52  			if config.Options.PrintCommands != nil {
  53  				config.Options.PrintCommands("ld.lld", flags...)
  54  			}
  55  			return link("ld.lld", flags...)
  56  		},
  57  	}
  58  }
  59