github.com/Aayushi-Bansal/sys@v0.0.0-20180118120756-90d962a959d8/unix/linux/mksysnum.pl (about)

     1  #!/usr/bin/env perl
     2  # Copyright 2009 The Go Authors. All rights reserved.
     3  # Use of this source code is governed by a BSD-style
     4  # license that can be found in the LICENSE file.
     5  
     6  use strict;
     7  
     8  if($ENV{'GOARCH'} eq "" || $ENV{'GOOS'} eq "") {
     9  	print STDERR "GOARCH or GOOS not defined in environment\n";
    10  	exit 1;
    11  }
    12  
    13  # Check that we are using the new build system if we should
    14  if($ENV{'GOLANG_SYS_BUILD'} ne "docker") {
    15  	print STDERR "In the new build system, mksysnum should not be called directly.\n";
    16  	print STDERR "See README.md\n";
    17  	exit 1;
    18  }
    19  
    20  my $command = "$0 ". join(' ', @ARGV);
    21  
    22  print <<EOF;
    23  // $command
    24  // Code generated by the command above; see README.md. DO NOT EDIT.
    25  
    26  // +build $ENV{'GOARCH'},$ENV{'GOOS'}
    27  
    28  package unix
    29  
    30  const(
    31  EOF
    32  
    33  my $offset = 0;
    34  
    35  sub fmt {
    36  	my ($name, $num) = @_;
    37  	if($num > 999){
    38  		# ignore deprecated syscalls that are no longer implemented
    39  		# https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/include/uapi/asm-generic/unistd.h?id=refs/heads/master#n716
    40  		return;
    41  	}
    42  	$name =~ y/a-z/A-Z/;
    43  	$num = $num + $offset;
    44  	print "	SYS_$name = $num;\n";
    45  }
    46  
    47  my $prev;
    48  open(CC, "$ENV{'CC'} -E -dD @ARGV |") || die "can't run $ENV{'CC'}";
    49  while(<CC>){
    50  	if(/^#define __NR_Linux\s+([0-9]+)/){
    51  		# mips/mips64: extract offset
    52  		$offset = $1;
    53  	}
    54  	elsif(/^#define __NR(\w*)_SYSCALL_BASE\s+([0-9]+)/){
    55  		# arm: extract offset
    56  		$offset = $1;
    57  	}
    58  	elsif(/^#define __NR_syscalls\s+/) {
    59  		# ignore redefinitions of __NR_syscalls
    60  	}
    61  	elsif(/^#define __NR_(\w*)Linux_syscalls\s+/) {
    62  		# mips/mips64: ignore definitions about the number of syscalls
    63  	}
    64  	elsif(/^#define __NR_(\w+)\s+([0-9]+)/){
    65  		$prev = $2;
    66  		fmt($1, $2);
    67  	}
    68  	elsif(/^#define __NR3264_(\w+)\s+([0-9]+)/){
    69  		$prev = $2;
    70  		fmt($1, $2);
    71  	}
    72  	elsif(/^#define __NR_(\w+)\s+\(\w+\+\s*([0-9]+)\)/){
    73  		fmt($1, $prev+$2)
    74  	}
    75  	elsif(/^#define __NR_(\w+)\s+\(__NR_Linux \+ ([0-9]+)/){
    76  		fmt($1, $2);
    77  	}
    78  	elsif(/^#define __NR_(\w+)\s+\(__NR_SYSCALL_BASE \+ ([0-9]+)/){
    79  		fmt($1, $2);
    80  	}
    81  }
    82  
    83  print <<EOF;
    84  )
    85  EOF