github.com/tinygo-org/tinygo@v0.31.3-0.20240404173401-90b0bf646c27/builder/darwin-libsystem.go (about)

     1  package builder
     2  
     3  import (
     4  	"path/filepath"
     5  	"strings"
     6  
     7  	"github.com/tinygo-org/tinygo/compileopts"
     8  	"github.com/tinygo-org/tinygo/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("TINYGOROOT"), "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  }