github.com/chenzhuoyu/iasm@v0.9.1/cmd/iasm/preprocessor.go (about)

     1  package main
     2  
     3  import (
     4      `bytes`
     5      `os`
     6      `os/exec`
     7  )
     8  
     9  const (
    10      defaultCpp = "/usr/bin/cpp"
    11  )
    12  
    13  func preprocess(name string, defs []string) (string, error) {
    14      var err error
    15      var cpp string
    16      var out bytes.Buffer
    17  
    18      /* find the CPP command */
    19      if cpp = os.Getenv("CPP"); cpp == "" {
    20          cpp = defaultCpp
    21      }
    22  
    23      /* command arguments */
    24      args := []string {
    25          "-CC",
    26          "-nostdinc",
    27      }
    28  
    29      /* build definations */
    30      for _, def := range defs {
    31          args = append(args, "-D" + def)
    32      }
    33  
    34      /* construct the preprocessor command */
    35      cmd := exec.Command(
    36          defaultCpp,
    37          append(args, name)...,
    38      )
    39  
    40      /* bind stdio */
    41      cmd.Stdin  = nil
    42      cmd.Stdout = &out
    43      cmd.Stderr = os.Stderr
    44  
    45      /* run the preprocessor */
    46      if err = cmd.Run(); err != nil {
    47          return "", err
    48      } else {
    49          return string(out.Bytes()), nil
    50      }
    51  }