github.com/kidsbmilk/gofrontend_all@v0.0.0-20220701224323-6479d5976c5d/libgo/match.sh (about)

     1  #!/bin/sh
     2  
     3  # Copyright 2016 The Go Authors. All rights reserved.
     4  # Use of this source code is governed by a BSD-style
     5  # license that can be found in the LICENSE file.
     6  
     7  # Given a source directory, returns the non-test Go files that should
     8  # be built for this target.  This implements Go's build constraints in
     9  # a shell script.  There is similar code in testsuite/gotest.
    10  
    11  set -e
    12  
    13  LANG=C
    14  LC_ALL=C
    15  LC_CTYPE=C
    16  export LANG LC_ALL LC_CTYPE
    17  
    18  srcdir=""
    19  goarch=""
    20  goos=""
    21  extrafiles=""
    22  cmdlinetag="nosuchtag"
    23  cgotag="cgo"
    24  
    25  for arg; do
    26      case "x$arg" in
    27  	x--srcdir)
    28  	    srcdir=$2
    29  	    shift
    30  	    shift
    31  	    ;;
    32  	x--srcdir=*)
    33  	    srcdir=`echo $1 | sed -e 's/^--srcdir=//'`
    34  	    shift
    35  	    ;;
    36  	x--goarch)
    37  	    goarch=$2
    38  	    shift
    39  	    shift
    40  	    ;;
    41  	x--goarch=*)
    42  	    goarch=`echo $1 | sed -e 's/^--goarch=//'`
    43  	    shift
    44  	    ;;
    45  	x--goos)
    46  	    goos=$2
    47  	    shift
    48  	    shift
    49  	    ;;
    50  	x--goos=*)
    51  	    goos=`echo $1 | sed -e 's/^--goos=//'`
    52  	    shift
    53  	    ;;
    54  	x--extrafiles)
    55  	    extrafiles=$2
    56  	    shift
    57  	    shift
    58  	    ;;
    59  	x--extrafiles=*)
    60  	    extrafiles=`echo $1 | sed -e 's/^--extrafiles=//'`
    61  	    shift
    62  	    ;;
    63  	x--tag)
    64  	    cmdlinetag=$2
    65  	    shift
    66  	    shift
    67  	    ;;
    68  	x--tag=*)
    69  	    cmdlinetag=`echo $1 | sed -e 's/^--tag=//'`
    70  	    shift
    71  	    ;;
    72  	x--nocgo)
    73  	    cgotag="nosuchtag"
    74  	    shift
    75  	    ;;
    76  	*)
    77  	    echo 1>&2 "unknown argument $arg"
    78  	    exit 1
    79  	    ;;
    80      esac
    81  done
    82  
    83  cd $srcdir
    84  
    85  gofiles=
    86  for f in *.go; do
    87      case $f in
    88  	*_test.go)
    89  	    ;;
    90  	*.go)
    91  	    gofiles="$gofiles $f"
    92  	    ;;
    93      esac
    94  done
    95  
    96  if test "$gofiles" = ""; then
    97      echo 1>&2 "no non-test .go files in $srcdir"
    98      exit 1
    99  fi
   100  
   101  gobuild() {
   102      line=$(echo "$1" | sed -e 's|//go:build ||')
   103      line=$(echo "$line" | sed -e 's/go1\.[0-9][0-9]*/1/g' -e 's/goexperiment\./goexperiment/')
   104      line=" $line "
   105      wrap='[ ()!&|]'
   106      for ones in $goarch $goos $cgotag $cmdlinetag gccgo goexperimentfieldtrack; do
   107  	line=$(echo "$line" | sed -e "s/\\(${wrap}\\)${ones}\\(${wrap}\\)/"'\11\2/g')
   108      done
   109      # 386 is a special case since it looks like a number to the shell.
   110      # We need it to be 0 if it's not $goarch.
   111      if test "$goarch" != "386"; then
   112  	line=$(echo "$line" | sed -e "s/\\(${wrap}\\)386\\(${wrap}\\)/\10\2/g")
   113      fi
   114      (($line))
   115  }
   116  
   117  matched=
   118  for f in $gofiles; do
   119      tag1=`echo $f | sed -e 's/^.*_\([^_]*\).go$/\1/'`
   120      tag2=`echo $f | sed -e 's/^.*_\([^_]*\)_[^_]*.go$/\1/'`
   121      if test x$tag1 = x$f; then
   122  	tag1=
   123      fi
   124      if test x$tag2 = x$f; then
   125  	tag2=
   126      fi
   127  
   128      case "$tag1" in
   129  	"") ;;
   130  	$goarch) ;;
   131  	$goos) ;;
   132  	aix | android | darwin | dragonfly | freebsd | illumos | hurd | ios | js | linux | nacl | netbsd | openbsd | plan9 | solaris | windows | zos)
   133  	    tag1=nonmatchingtag
   134  	    ;;
   135  	386 | amd64 | amd64p32 | arm | armbe | arm64 | arm64be | alpha | ia64 | m68k | mips | mipsle | mips64 | mips64le | mips64p32 | mips64p32le | nios2 | ppc | ppc64 | ppc64le | riscv | riscv64 | s390 | s390x | sh | shbe | sparc | sparc64 | wasm)
   136  	    tag1=nonmatchingtag
   137  	    ;;
   138  	*)
   139  	    # File name like x_amd64_random.go, where tag1=random.
   140  	    # Don't match based on tag2.
   141  	    tag2=
   142  	    ;;
   143      esac
   144  
   145      case "$tag2" in
   146  	"") ;;
   147  	$goarch) ;;
   148  	$goos) ;;
   149  	aix | android | darwin | dragonfly | freebsd | hurd | ios | illumos | js | linux | nacl | netbsd | openbsd | plan9 | solaris | windows | zos)
   150  	    tag2=nonmatchingtag
   151  	    ;;
   152  	386 | amd64 | amd64p32 | arm | armbe | arm64 | arm64be | alpha | ia64 | m68k | mips | mipsle | mips64 | mips64le | mips64p32 | mips64p32le | nios2 | ppc | ppc64 | ppc64le | riscv | riscv64 | s390 | s390x | sh | shbe | sparc | sparc64 | wasm)
   153  	    tag2=nonmatchingtag
   154  	    ;;
   155      esac
   156  
   157      if test x$tag1 = xnonmatchingtag -o x$tag2 = xnonmatchingtag; then
   158  	continue
   159      fi
   160  
   161      # Check for go:build line
   162      build=$(sed '/^package /q' < $f | grep '^//go:build ' | cat)
   163      if test -n "$build"; then
   164  	if $(gobuild "$build"); then
   165  	    matched="$matched $srcdir/$f"
   166  	fi
   167  	continue
   168      fi
   169  
   170      # No go:build line, check for +build lines.
   171      # Pipe through cat so that `set -e` doesn't affect fgrep.
   172      tags=`sed '/^package /q' < $f | grep '^// *+build ' | cat`
   173      omatch=true
   174      first=true
   175      match=false
   176      for tag in $tags; do
   177  	case $tag in
   178  	    "//")
   179  	    ;;
   180  	    "+build" | "//+build")
   181  		if test "$first" = "true"; then
   182  		    first=false
   183  		elif test "$match" = "false"; then
   184  		    omatch=false
   185  		fi
   186  		match=false
   187  		;;
   188  	    $goos | $goarch | $cgotag | $cmdlinetag | "gccgo" | "goexperiment.fieldtrack" | go1.[0-9] | go1.[0-9][0-9])
   189  		match=true
   190  		;;
   191  	    "!"$goos | "!"$goarch | "!"$cgotag | "!"$cmdlinetag | "!gccgo" | "!goexperiment.fieldtrack" | "!"go1.[0-9] | "!"go1.1[0-7])
   192  		;;
   193  	    *,*)
   194  		cmatch=true
   195  		for ctag in `echo $tag | sed -e 's/,/ /g'`; do
   196  		    case $ctag in
   197  			$goos | $goarch | $cgotag | $cmdlinetag | "gccgo" | "goexperiment.fieldtrack" | go1.[0-9] | go1.[0-9][0-9])
   198  			;;
   199  			"!"$goos | "!"$goarch | "!"$cgotag | "!"$cmdlinetag | "!gccgo" | "!goexperiment.fieldtrack" | "!"go1.[0-9] | "!"go1.1[0-7])
   200  			    cmatch=false
   201  			    ;;
   202  			"!"*)
   203  			    ;;
   204  			*)
   205  			    cmatch=false
   206  			    ;;
   207  		    esac
   208  		done
   209  		if test "$cmatch" = "true"; then
   210  		    match=true
   211  		fi
   212  		;;
   213  	    "!"*)
   214  		match=true
   215  		;;
   216  	esac
   217      done
   218  
   219      if test "$match" = "false" -a "$first" = "false"; then
   220  	omatch=false
   221      fi
   222  
   223      if test "$omatch" = "true"; then
   224  	matched="$matched $srcdir/$f"
   225      fi
   226  done
   227  
   228  echo $matched $extrafiles
   229  
   230  exit 0