Home | History | Annotate | Download | only in csource
      1 // Copyright 2017 syzkaller project authors. All rights reserved.
      2 // Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
      3 
      4 // +build
      5 
      6 package main
      7 
      8 import (
      9 	"bytes"
     10 	"fmt"
     11 	"io/ioutil"
     12 	"os"
     13 	"regexp"
     14 )
     15 
     16 func main() {
     17 	out, err := os.Create("generated.go")
     18 	if err != nil {
     19 		failf("%v", err)
     20 	}
     21 	defer out.Close()
     22 	data, err := ioutil.ReadFile("../../executor/common.h")
     23 	if err != nil {
     24 		failf("%v", err)
     25 	}
     26 	for _, include := range []string{
     27 		"common_linux.h",
     28 		"common_akaros.h",
     29 		"common_bsd.h",
     30 		"common_fuchsia.h",
     31 		"common_windows.h",
     32 		"common_test.h",
     33 		"common_kvm_amd64.h",
     34 		"common_kvm_arm64.h",
     35 		"kvm.h",
     36 		"kvm.S.h",
     37 	} {
     38 		contents, err := ioutil.ReadFile("../../executor/" + include)
     39 		if err != nil {
     40 			failf("%v", err)
     41 		}
     42 		replace := []byte("#include \"" + include + "\"")
     43 		if bytes.Index(data, replace) == -1 {
     44 			failf("can't fine %v include", include)
     45 		}
     46 		data = bytes.Replace(data, replace, contents, -1)
     47 	}
     48 	for _, remove := range []string{
     49 		"(\n|^)\\s*//.*",
     50 		"\\s*//.*",
     51 	} {
     52 		data = regexp.MustCompile(remove).ReplaceAll(data, nil)
     53 	}
     54 	fmt.Fprintf(out, "// AUTOGENERATED FROM executor/common.h\n\n")
     55 	fmt.Fprintf(out, "package csource\n\nvar commonHeader = `\n")
     56 	out.Write(data)
     57 	fmt.Fprintf(out, "`\n")
     58 }
     59 
     60 func failf(msg string, args ...interface{}) {
     61 	fmt.Fprintf(os.Stderr, msg+"\n", args...)
     62 	os.Exit(1)
     63 }
     64