github.com/cloudwego/iasm@v0.2.0/cmd/iasm/preprocessor.go (about) 1 // 2 // Copyright 2024 CloudWeGo Authors 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 // 16 17 package main 18 19 import ( 20 `bytes` 21 `os` 22 `os/exec` 23 ) 24 25 const ( 26 defaultCpp = "/usr/bin/cpp" 27 ) 28 29 func preprocess(name string, defs []string) (string, error) { 30 var err error 31 var cpp string 32 var out bytes.Buffer 33 34 /* find the CPP command */ 35 if cpp = os.Getenv("CPP"); cpp == "" { 36 cpp = defaultCpp 37 } 38 39 /* command arguments */ 40 args := []string { 41 "-CC", 42 "-nostdinc", 43 } 44 45 /* build definations */ 46 for _, def := range defs { 47 args = append(args, "-D" + def) 48 } 49 50 /* construct the preprocessor command */ 51 cmd := exec.Command( 52 defaultCpp, 53 append(args, name)..., 54 ) 55 56 /* bind stdio */ 57 cmd.Stdin = nil 58 cmd.Stdout = &out 59 cmd.Stderr = os.Stderr 60 61 /* run the preprocessor */ 62 if err = cmd.Run(); err != nil { 63 return "", err 64 } else { 65 return string(out.Bytes()), nil 66 } 67 }