#!/usr/bin/perl -w
# $Id: //info.ravenbrook.com/project/mps/version/1.113/test/test/script/platform#1 $
#
# Settings that depend on the platform, including C compiler and command syntax
#
# And the %identify hash of useful information
# to record in the test results.
#

1;

#
# Set lots of variables correctly, depending on the platform
# (which was determined in 'options')
#
# Currently, it should work correctly on NT, Solaris, Linux, MacOS X.
#

sub platform_settings {
    if ($PLATFORM =~ "^nt_") {
        &settings_nt();
        if ($PLATFORM =~ "^nt_x86_cap") {
            &settings_nt_cap();
        } elsif ($PLATFORM =~ "^nt_x86_coff") {
            &settings_nt_coff();
        }
    } elsif ($PLATFORM =~ "^SunOS") {
        &settings_unix();
        if ($PLATFORM =~ "sos8gp") {
            &settings_gprof();
        }
    } elsif ($PLATFORM =~ "^Linux") {
        &settings_unix();
        &settings_linux();
    } elsif ($PLATFORM =~ "^Mac_OS_10" || $PLATFORM =~ "^Darwin_") {
        &settings_unix();
        &settings_macosx();
    } elsif ($PLATFORM =~ "__unix") {
        &logcomment("I don't know anything specific about $PLATFORM --");
        &logcomment("using generic unix/gcc settings.");
        &settings_unix();
    } else {
        die "Sorry: I don't know how to use ".$PLATFORM."\n";
    }
}


sub settings_nt {
 $dirsep = "\\";
 $cc_command = "cl";
# following line used to include /DMMQA_VERS_$MPS_INTERFACE_VERSION
 $cc_opts = "/nologo /DWIN32 /D_WINDOWS /W3 /Zi /Oy- /MD /DMMQA_PROD_$MPS_PRODUCT";
 $cc_link = "$obj_dir/platform.obj";
 $cc_link_opts = "/link /NODEFAULTLIB:LIBCMT /NODEFAULTLIB:LIBCMTD /NODEFAULTLIB:LIBC /NODEFAULTLIB:LIBCD /NODEFAULTLIB:MSVCRTD /DEFAULTLIB:MSVCRT /debugtype:both /pdb:none /debug:full";
 $cc_include = "/I$testlib_dir /I$MPS_INCLUDE_DIR /I$obj_dir";
 $cc_def = "/D";
 $cc_defeq = "=";
 $cc_preonly = "/EP";
 $cc_conly = "/c";
 $cc_obj = "/Fo";
 $cc_exe = "/Fe";
 $cc_objandexe = 1;
 $obj_suffix = ".obj";
 $try_command = "";
 $catcommand = "$script_dir/ntx86bin/cat.exe";
 $comwrap = "\"";
 $comwrapend = "\"";
 $stdout_red = ">";
 $stdout_dup = "| $script_dir/ntx86bin/tee.exe";
 $stdin_red = "<";
 $stdboth_red = ">%s 2>&1";
 $quotestring = \&nt_quotestring;
 $platmailfile = \&nt_mailfile;
 $stringscommand = "$script_dir/ntx86bin/strings.exe -20 -c";
 $preprocommand = "$cc_command /nologo $cc_preonly";
 $exesuff = ".exe";
 %ignored_headers = ();
}

sub settings_nt_cap {
 $cc_opts = "$cc_opts /Gh";
 $cc_link = "$cc_link CAP.lib";
 $cc_link_opts = "/link /NODEFAULTLIB:LIBCMT /NODEFAULTLIB:LIBCMTD /NODEFAULTLIB:LIBC /NODEFAULTLIB:LIBCD /NODEFAULTLIB:MSVCRTD /DEFAULTLIB:MSVCRT /debug:full /debugtype:both /pdb:none";
}

sub settings_nt_coff {
 $cc_link_opts = "/link /NODEFAULTLIB:LIBCMT /NODEFAULTLIB:LIBCMTD /NODEFAULTLIB:LIBC /NODEFAULTLIB:LIBCD /NODEFAULTLIB:MSVCRTD /DEFAULTLIB:MSVCRT /debugtype:coff /debug:full";
}


sub settings_unix {
 $dirsep = "/";
 $cc_link = "$obj_dir/platform.o -lm";
 $cc_link_opts = "-z muldefs";
 $cc_command = "gcc";
 $cc_opts = "-ansi -pedantic -Wall -Wstrict-prototypes ".
  "-Winline -Waggregate-return -Wnested-externs -Wcast-qual ".
  "-Wshadow -Wmissing-prototypes -Wcast-align ".
  "-O -g -ggdb3 ".
  "-DMMQA_PROD_$MPS_PRODUCT";
 $cc_include = "-I$testlib_dir -I$MPS_INCLUDE_DIR -I$obj_dir";
 $cc_def = "-D";
 $cc_defeq = "=";
 $cc_preonly = "-E";
 $cc_conly = "-c";
 $cc_obj = "-o ";
 $cc_exe = "-o ";
 $cc_objandexe = 0;
 $obj_suffix = ".o";
 $try_command = "sh -c ";
 $catcommand = "cat";
 $comwrap = "sh -c \"ulimit -c 0; ";
 $comwrapend = "\"";
 $stdout_red = ">";
 $stdout_dup = "| tee";
 $stdin_red = "<";
 $stdboth_red = ">%s 2>&1";
 $quotestring = \&unix_quotestring;
 $platmailfile = \&unix_mailfile;
 $stringscommand = "strings";
 $preprocommand = "$cc_command $cc_preonly";
 $exesuff = "";
 %ignored_headers = ("mpswin.h" => 1, "mpsw3.h" => 1);
}


sub settings_gprof {
 $cc_opts = "-pg ".$cc_opts;
}


sub settings_linux {
 $cc_link = $cc_link . " -lpthread";
}


sub settings_macosx {
    $cc_command = "cc";
    $cc_link = "$obj_dir/platform.o";
    $cc_link_opts =~ s/-z muldefs//;
    # See comments in impl.gmk.xcppgc and impl.h.osxc
    $cc_opts =~ s/-Wstrict-prototypes//;
    $cc_opts =~ s/-ggdb3//;
    $cc_opts .= " -fprofile-arcs -ftest-coverage";
    $cc_opts .= " -Wno-unused -Wno-long-long -D__inline__=";
    $stdboth_red = ">&%s";
    $preprocommand = "$cc_command $cc_preonly";
}


#
# Wrapping up strings to quote them in system calls
#

sub nt_quotestring {
 return $_[0];
}

sub unix_quotestring {
 local ($_) = @_;
 s/'/'\''/;
 return "'".$_."'";
}

#
# Subroutines to send email (for archiving test results).
#

sub nt_mailfile {
 local ($file, $to, $subj, $from) = @_;
 
 if (exists $ENV{"USERNAME"}) {
  $from = $ENV{"USERNAME"};
 } else {
  $from = "mm-qa";
 }
 &mysystem(
  "$script_dir/ntx86bin/blat.exe ".
  "$file -server mailhost -f $from -t $to -s \"$subj\" -q");
}

sub unix_mailfile {
 local ($file, $to, $subj) = @_;

 &mysystem(
  "{ echo 'To: $to'; echo 'Subject: $subj'; cat $file; } | /usr/lib/sendmail -t");
}


#
# %identify records useful information to present in the test results
# &identify sets it up.
#

sub identify {
 %identify = ();
 if ($PLATFORM =~ /^nt/) {
  &identify_nt;
 } elsif ($PLATFORM =~ /__unix/) {
  &identify_unix;
 }
 $identify{"time"} = localtime;
 $identify{"harness_version"} = $HARNESS_VERSION;
}


sub envvar {
 if (exists $ENV{$_[1]}) {
  $identify{$_[0]} = $ENV{$_[1]};
 }
}

sub comvar {
 my ($var, $com, $pat) = @_;
 if (open(COM, $com." 2>&1 |")) {
  while (<COM>) {
   chop;
   if ($pat eq "" || /$pat/) {
    $identify{$_[0]} = $_;
    last;
   }
  }
  close(COM);
 }
}


sub identify_nt {
 &envvar("machine", "COMPUTERNAME");
 &envvar("user", "USERNAME");
 &envvar("OS", "OS");
 &envvar("arch", "PROCESSOR_ARCHITECTURE");
 &comvar("c_version", "cl /?", "");
}


sub identify_unix {
 &comvar("machine", "uname -n", "");
 &comvar("user", "who am i", "");
 if (exists $identify{"user"}) {
  $identify{"user"} =~ s/\s.*//;
 }
 &comvar("c_version", "gcc -v", "version");
 &comvar("OS", "uname", "");
 &comvar("arch", "uname -a", "");
}
