1#!/bin/sh
2# Compile a C# program.
3
4# Copyright (C) 2003-2005 Free Software Foundation, Inc.
5# Written by Bruno Haible <bruno@clisp.org>, 2003.
6#
7# This program is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation; either version 2, or (at your option)
10# any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program; if not, write to the Free Software Foundation,
19# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
20
21# This uses the same choices as csharpcomp.c, but instead of relying on the
22# environment settings at run time, it uses the environment variables
23# present at configuration time.
24#
25# This is a separate shell script, because the various C# compilers have
26# different command line options.
27#
28# Usage: /bin/sh csharpcomp.sh [OPTION] SOURCE.cs ... RES.resource ...
29# Options:
30#   -o PROGRAM.exe  or  -o LIBRARY.dll
31#                     set the output assembly name
32#   -L DIRECTORY      search for C# libraries also in DIRECTORY
33#   -l LIBRARY        reference the C# library LIBRARY.dll
34#   -O                optimize
35#   -g                generate debugging information
36
37# func_tmpdir
38# creates a temporary directory.
39# Sets variable
40# - tmp             pathname of freshly created temporary directory
41func_tmpdir ()
42{
43  # Use the environment variable TMPDIR, falling back to /tmp. This allows
44  # users to specify a different temporary directory, for example, if their
45  # /tmp is filled up or too small.
46  : ${TMPDIR=/tmp}
47  {
48    # Use the mktemp program if available. If not available, hide the error
49    # message.
50    tmp=`(umask 077 && mktemp -d -q "$TMPDIR/gtXXXXXX") 2>/dev/null` &&
51    test -n "$tmp" && test -d "$tmp"
52  } ||
53  {
54    # Use a simple mkdir command. It is guaranteed to fail if the directory
55    # already exists.  $RANDOM is bash specific and expands to empty in shells
56    # other than bash, ksh and zsh.  Its use does not increase security;
57    # rather, it minimizes the probability of failure in a very cluttered /tmp
58    # directory.
59    tmp=$TMPDIR/gt$$-$RANDOM
60    (umask 077 && mkdir "$tmp")
61  } ||
62  {
63    echo "$0: cannot create a temporary directory in $TMPDIR" >&2
64    { (exit 1); exit 1; }
65  }
66}
67
68sed_quote_subst='s/\([|&;<>()$`"'"'"'*?[#~=% 	\\]\)/\\\1/g'
69options_cscc=
70options_mcs=
71options_csc="-nologo"
72sources=
73while test $# != 0; do
74  case "$1" in
75    -o)
76      case "$2" in
77        *.dll)
78          options_cscc="$options_cscc -shared"
79          options_mcs="$options_mcs -target:library"
80          options_csc="$options_csc -target:library"
81          ;;
82        *.exe)
83          options_csc="$options_csc -target:exe"
84          ;;
85      esac
86      options_cscc="$options_cscc -o "`echo "$2" | sed -e "$sed_quote_subst"`
87      options_mcs="$options_mcs -o "`echo "$2" | sed -e "$sed_quote_subst"`
88      options_csc="$options_csc -out:"`echo "$2" | sed -e "$sed_quote_subst"`
89      shift
90      ;;
91    -L)
92      options_cscc="$options_cscc -L "`echo "$2" | sed -e "$sed_quote_subst"`
93      options_mcs="$options_mcs -L "`echo "$2" | sed -e "$sed_quote_subst"`
94      options_csc="$options_csc -lib:"`echo "$2" | sed -e "$sed_quote_subst"`
95      shift
96      ;;
97    -l)
98      options_cscc="$options_cscc -l "`echo "$2" | sed -e "$sed_quote_subst"`
99      options_mcs="$options_mcs -r "`echo "$2" | sed -e "$sed_quote_subst"`
100      options_csc="$options_csc -reference:"`echo "$2" | sed -e "$sed_quote_subst"`
101      shift
102      ;;
103    -O)
104      options_cscc="$options_cscc -O"
105      options_csc="$options_csc -optimize+"
106      ;;
107    -g)
108      options_cscc="$options_cscc -g"
109      options_mcs="$options_mcs -g"
110      options_csc="$options_csc -debug+"
111      ;;
112    -*)
113      echo "csharpcomp: unknown option '$1'" 1>&2
114      exit 1
115      ;;
116    *.resource)
117      options_cscc="$options_cscc -fresources="`echo "$1" | sed -e "$sed_quote_subst"`
118      options_mcs="$options_mcs -resource:"`echo "$1" | sed -e "$sed_quote_subst"`
119      options_csc="$options_csc -resource:"`echo "$1" | sed -e "$sed_quote_subst"`
120      ;;
121    *.cs)
122      sources="$sources "`echo "$1" | sed -e "$sed_quote_subst"`
123      ;;
124    *)
125      echo "csharpcomp: unknown type of argument '$1'" 1>&2
126      exit 1
127      ;;
128  esac
129  shift
130done
131
132if test -n "@HAVE_CSCC@"; then
133  test -z "$CSHARP_VERBOSE" || echo cscc $options_cscc $sources
134  exec cscc $options_cscc $sources
135else
136  if test -n "@HAVE_MCS@"; then
137    # mcs prints it errors and warnings to stdout, not stderr. Furthermore it
138    # adds a useless line "Compilation succeeded..." at the end. Correct both.
139    sed_drop_success_line='${
140/^Compilation succeeded/d
141}'
142    func_tmpdir
143    trap 'rm -rf "$tmp"' 1 2 3 15
144    test -z "$CSHARP_VERBOSE" || echo mcs $options_mcs $sources
145    mcs $options_mcs $sources > "$tmp"/mcs.err
146    result=$?
147    sed -e "$sed_drop_success_line" < "$tmp"/mcs.err >&2
148    rm -rf "$tmp"
149    exit $result
150  else
151    if test -n "@HAVE_CSC@"; then
152      test -z "$CSHARP_VERBOSE" || echo csc $options_csc $sources
153      exec csc $options_csc $sources
154    else
155      echo 'C# compiler not found, try installing pnet, then reconfigure' 1>&2
156      exit 1
157    fi
158  fi
159fi
160