github.com/tardisgo/tardisgo@v0.0.0-20161119180838-e0dd9a7e46b5/haxe/runhaxe.go (about)

     1  package haxe
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"os/exec"
     7  	"runtime"
     8  	"strings"
     9  )
    10  
    11  // RunHaxe runs the operating system commands to compile and run haxe code for testing
    12  func RunHaxe(allFlag *string, LoadTestZipFS bool, TestFS string) {
    13  	results := make(chan resChan)
    14  	switch *allFlag {
    15  	case "": // NoOp
    16  	case "all", "bench":
    17  		//for _, dir := range dirs {
    18  		//	err := os.RemoveAll(dir)
    19  		//	if err != nil {
    20  		//		fmt.Println("Error deleting existing '" + dir + "' directory: " + err.Error())
    21  		//	}
    22  		//}
    23  
    24  		var targets [][][]string
    25  		if *allFlag == "bench" {
    26  			targets = allBenchmark // fast execution time
    27  		} else {
    28  			targets = allCompile // fast compile time
    29  		}
    30  		for _, cmd := range targets {
    31  			go doTarget(cmd, results, LoadTestZipFS, TestFS)
    32  		}
    33  		for _ = range targets {
    34  			r := <-results
    35  			fmt.Println(r.output)
    36  			if (r.err != nil || len(strings.TrimSpace(r.output)) == 0) && *allFlag != "bench" {
    37  				os.Exit(1) // exit with an error if the test fails, but not for benchmarking
    38  			}
    39  			r.backChan <- true
    40  		}
    41  
    42  	case "math": // which is faster for the test with correct math processing, cpp or js?
    43  		//err := os.RemoveAll("tardis/cpp")
    44  		//if err != nil {
    45  		//	fmt.Println("Error deleting existing '" + "tardis/cpp" + "' directory: " + err.Error())
    46  		//}
    47  		mathCmds := [][][]string{
    48  			[][]string{
    49  				[]string{"haxe", "-main", "tardis.Go", "-cp", "tardis", "-dce", "full", "-D", "inlinepointers", "-cpp", "tardis/cpp"},
    50  				[]string{"echo", `"CPP:"`},
    51  				[]string{"time", "./tardis/cpp/Go"},
    52  			},
    53  			[][]string{
    54  				[]string{"haxe", "-main", "tardis.Go", "-cp", "tardis", "-dce", "full", "-D", "inlinepointers", "-D", "fullunsafe", "-js", "tardis/go-fu.js"},
    55  				[]string{"echo", `"Node/JS using fullunsafe memory mode (js dataview):"`},
    56  				[]string{"time", "node", "tardis/go-fu.js"},
    57  			},
    58  		}
    59  		for _, cmd := range mathCmds {
    60  			go doTarget(cmd, results, LoadTestZipFS, TestFS)
    61  		}
    62  		for _ = range mathCmds {
    63  			r := <-results
    64  			fmt.Println(r.output)
    65  			if r.err != nil {
    66  				os.Exit(1) // exit with an error if the test fails
    67  			}
    68  			r.backChan <- true
    69  		}
    70  
    71  	case "interp", "cpp", "cs", "js", "jsfu", "java", "flash": // for running tests
    72  		switch *allFlag {
    73  		case "interp":
    74  			go doTarget([][]string{
    75  				[]string{"echo", ``}, // Output from this line is ignored
    76  				[]string{"echo", `"Neko (haxe --interp):"`},
    77  				[]string{"time", "haxe", "-main", "tardis.Go", "-cp", "tardis", "--interp"},
    78  			}, results, LoadTestZipFS, TestFS)
    79  		case "cpp":
    80  			go doTarget([][]string{
    81  				[]string{"haxe", "-main", "tardis.Go", "-cp", "tardis", "-dce", "full", "-D", "inlinepointers", "-cpp", "tardis/cpp"},
    82  				[]string{"echo", `"CPP:"`},
    83  				[]string{"time", "./tardis/cpp/Go"},
    84  			}, results, LoadTestZipFS, TestFS)
    85  		case "cs":
    86  			go doTarget([][]string{
    87  				[]string{"haxe", "-main", "tardis.Go", "-cp", "tardis", "-dce", "full", "-D", "inlinepointers", "-cs", "tardis/cs"},
    88  				[]string{"echo", `"CS:"`},
    89  				[]string{"time", "mono", "./tardis/cs/bin/Go.exe"},
    90  			}, results, LoadTestZipFS, TestFS)
    91  		case "js":
    92  			go doTarget([][]string{
    93  				[]string{"haxe", "-main", "tardis.Go", "-cp", "tardis", "-dce", "full", "-D", "inlinepointers", "-D", "uselocalfunctions", "-js", "tardis/go.js"},
    94  				[]string{"echo", `"Node/JS:"`},
    95  				[]string{"time", "node", "tardis/go.js"},
    96  			}, results, LoadTestZipFS, TestFS)
    97  		case "jsfu":
    98  			go doTarget([][]string{
    99  				[]string{"haxe", "-main", "tardis.Go", "-cp", "tardis", "-dce", "full", "-D", "inlinepointers", "-D", "uselocalfunctions", "-D", "fullunsafe", "-js", "tardis/go-fu.js"},
   100  				[]string{"echo", `"Node/JS using fullunsafe memory mode (js dataview):"`},
   101  				[]string{"time", "node", "tardis/go-fu.js"},
   102  			}, results, LoadTestZipFS, TestFS)
   103  		case "java":
   104  			go doTarget([][]string{
   105  				[]string{"haxe", "-main", "tardis.Go", "-cp", "tardis", "-dce", "full", "-D", "inlinepointers", "-java", "tardis/java"},
   106  				[]string{"echo", `"Java:"`},
   107  				[]string{"time", "java", "-jar", "tardis/java/Go.jar"},
   108  			}, results, LoadTestZipFS, TestFS)
   109  		case "flash":
   110  			go doTarget([][]string{
   111  				[]string{"haxe", "-main", "tardis.Go", "-cp", "tardis", "-dce", "full", "-D", "inlinepointers", "-swf", "tardis/go.swf"},
   112  				[]string{"echo", `"Flash:"`},
   113  				[]string{"time", "open", "tardis/go.swf"},
   114  			}, results, LoadTestZipFS, TestFS)
   115  		}
   116  		r := <-results
   117  		fmt.Println(r.output)
   118  		if r.err != nil {
   119  			os.Exit(1) // exit with an error if the test fails
   120  		}
   121  		r.backChan <- true
   122  
   123  	default:
   124  		panic("invalid value for -haxe flag: " + *allFlag)
   125  	}
   126  }
   127  
   128  //var dirs = []string{"tardis/cpp", "tardis/java", "tardis/cs" /*, "tardis/php"*/}
   129  
   130  var allCompile = [][][]string{
   131  	[][]string{
   132  		[]string{"haxe", "-main", "tardis.Go", "-cp", "tardis", "-dce", "full", "-D", "inlinepointers", "-cpp", "tardis/cpp"},
   133  		[]string{"echo", `"CPP:"`},
   134  		[]string{"time", "./tardis/cpp/Go"},
   135  	},
   136  	[][]string{
   137  		[]string{"haxe", "-main", "tardis.Go", "-cp", "tardis", "-dce", "full", "-D", "inlinepointers", "-java", "tardis/java"},
   138  		[]string{"echo", `"Java:"`},
   139  		[]string{"time", "java", "-jar", "tardis/java/Go.jar"},
   140  	},
   141  	[][]string{
   142  		[]string{"haxe", "-main", "tardis.Go", "-cp", "tardis", "-dce", "full", "-D", "inlinepointers", "-cs", "tardis/cs"},
   143  		[]string{"echo", `"CS:"`},
   144  		[]string{"time", "mono", "./tardis/cs/bin/Go.exe"},
   145  	},
   146  	[][]string{
   147  		[]string{"haxe", "-main", "tardis.Go", "-cp", "tardis", "-dce", "full", "-D", "inlinepointers", "-D", "uselocalfunctions", "-js", "tardis/go.js"},
   148  		[]string{"echo", `"Node/JS:"`},
   149  		[]string{"time", "node", "tardis/go.js"},
   150  	},
   151  }
   152  var allBenchmark = [][][]string{
   153  	[][]string{
   154  		[]string{"haxe", "-main", "tardis.Go", "-cp", "tardis", "-dce", "full" /*, "-D", "nulltempvars"*/, "-D", "inlinepointers" /*, "-D", "abstractobjects"*/, "-cpp", "tardis/cpp-bench"},
   155  		[]string{"echo", `"CPP (bench):"`},
   156  		[]string{"time", "./tardis/cpp-bench/Go"},
   157  	},
   158  	[][]string{
   159  		[]string{"haxe", "-main", "tardis.Go", "-cp", "tardis", "-dce", "full" /*, "-D", "nulltempvars"*/, "-D", "inlinepointers" /*, "-D", "abstractobjects"*/, "-java", "tardis/java-bench"},
   160  		[]string{"echo", `"Java (bench):"`},
   161  		[]string{"time", "java", "-jar", "tardis/java-bench/Go.jar"},
   162  	},
   163  	[][]string{
   164  		[]string{"haxe", "-main", "tardis.Go", "-cp", "tardis", "-dce", "full" /*, "-D", "nulltempvars"*/, "-D", "inlinepointers" /*, "-D", "abstractobjects"*/, "-cs", "tardis/cs-bench"},
   165  		[]string{"echo", `"CS (bench):"`},
   166  		[]string{"time", "mono", "./tardis/cs-bench/bin/Go.exe"},
   167  	},
   168  	[][]string{
   169  		[]string{"haxe", "-main", "tardis.Go", "-cp", "tardis", "-dce", "full" /*, "-D", "nulltempvars"*/, "-D", "inlinepointers" /*, "-D", "abstractobjects" */, "-D", "jsinit", "-D", "uselocalfunctions", "-js", "tardis/go-bench.js"},
   170  		[]string{"echo", `"Node/JS (bench):"`},
   171  		[]string{"time", "node", "tardis/go-bench.js"},
   172  	},
   173  	// as this mode is no longer used for testing, remove it from the "all" tests
   174  	//[][]string{
   175  	//	[]string{"haxe", "-main", "tardis.Go", "-cp", "tardis", "-dce", "full", "-D", "fullunsafe", "-js", "tardis/go-fu.js"},
   176  	//	[]string{"echo", `"Node/JS using fullunsafe memory mode (js dataview):"`},
   177  	//	[]string{"time", "node", "tardis/go-fu.js"},
   178  	//},
   179  	// Cannot automate testing for SWF so removed
   180  	//[][]string{
   181  	//	[]string{"haxe", "-main", "tardis.Go", "-cp", "tardis", "-dce", "full", "-swf", "tardis/go.swf"},
   182  	//	[]string{"echo", `"Opening swf file (Chrome as a file association for swf works to test on OSX):"` + "\n"},
   183  	//	[]string{"open", "tardis/go.swf"},
   184  	//},
   185  	// PHP will never be a reliable target, so removed
   186  	//[][]string{
   187  	//	[]string{"haxe", "-main", "tardis.Go", "-cp", "tardis", "-dce", "full", "-php", "tardis/php", "--php-prefix", "tgo"},
   188  	//	[]string{"echo", `"PHP:"`},
   189  	//	[]string{"time", "php", "tardis/php/index.php"},
   190  	//},
   191  	// Seldom works, so removed
   192  	//[][]string{
   193  	//	[]string{"haxe", "-main", "tardis.Go", "-cp", "tardis", "-dce", "full", "-neko", "tardis/go.n"},
   194  	//	[]string{"echo", `"Neko (does not work for large code):"`},
   195  	//	[]string{"time", "neko", "tardis/go.n"},
   196  	//},
   197  	// only really useful for testing, so can be run from the command line
   198  	//[][]string{
   199  	//	[]string{"echo", ``}, // Output from this line is ignored
   200  	//	[]string{"echo", `"Neko (haxe --interp):"`},
   201  	//	[]string{"time", "haxe", "-main", "tardis.Go", "-cp", "tardis", "-dce", "full", "--interp"},
   202  	//},
   203  }
   204  
   205  type resChan struct {
   206  	output   string
   207  	err      error
   208  	backChan chan bool
   209  }
   210  
   211  func doTarget(cl [][]string, results chan resChan, LoadTestZipFS bool, TestFS string) {
   212  	res := ""
   213  	var lastErr error
   214  	for j, c := range cl {
   215  		if lastErr != nil {
   216  			break
   217  		}
   218  		exe := c[0]
   219  		if exe == "echo" {
   220  			res += c[1] + "\n"
   221  		} else {
   222  			_, err := exec.LookPath(exe)
   223  			if err != nil {
   224  				res += "TARDISgo error - executable not found: " + exe + "\n"
   225  				exe = "" // nothing to execute
   226  			}
   227  			if exe == "time" && c[1] == "node" && runtime.GOOS == "linux" {
   228  				c[1] = "nodejs" // for Ubuntu
   229  			}
   230  			if (exe == "haxe" || (exe == "time" && c[1] == "haxe")) && LoadTestZipFS {
   231  				c = append(c, "-resource")
   232  				c = append(c, TestFS)
   233  			}
   234  			if exe != "" {
   235  				out := []byte{}
   236  				out, lastErr = exec.Command(exe, c[1:]...).CombinedOutput()
   237  				if lastErr != nil {
   238  					out = append(out, []byte(lastErr.Error())...)
   239  				}
   240  				if j > 0 { // ignore the output from the compile phase
   241  					res += string(out)
   242  				}
   243  			}
   244  		}
   245  	}
   246  	bc := make(chan bool)
   247  	results <- resChan{res, lastErr, bc}
   248  	<-bc
   249  }