File: csharpcomp.sh.in

package info (click to toggle)
gettext 0.23.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 168,104 kB
  • sloc: ansic: 532,579; sh: 68,252; perl: 28,011; makefile: 9,066; lisp: 3,184; yacc: 1,055; java: 615; cs: 589; cpp: 397; objc: 343; sed: 79; tcl: 63; xml: 40; pascal: 11; php: 7; awk: 7
file content (247 lines) | stat: -rw-r--r-- 8,637 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#!/bin/sh
# Compile a C# program.

# Copyright (C) 2003-2024 Free Software Foundation, Inc.
# Written by Bruno Haible <bruno@clisp.org>, 2003.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

# This uses the same choices as csharpcomp.c, but instead of relying on the
# environment settings at run time, it uses the environment variables
# present at configuration time.
#
# This is a separate shell script, because the various C# compilers have
# different command line options.
#
# Usage: /bin/sh csharpcomp.sh [OPTION] SOURCE.cs ... RES.resource ...
# Options:
#   -o PROGRAM.exe  or  -o LIBRARY.dll
#                     set the output assembly name
#   -L DIRECTORY      search for C# libraries also in DIRECTORY
#   -l LIBRARY        reference the C# library LIBRARY.dll
#   -O                optimize
#   -g                generate debugging information

# func_tmpdir
# creates a temporary directory.
# Sets variable
# - tmp             pathname of freshly created temporary directory
func_tmpdir ()
{
  # Use the environment variable TMPDIR, falling back to /tmp. This allows
  # users to specify a different temporary directory, for example, if their
  # /tmp is filled up or too small.
  : "${TMPDIR=/tmp}"
  {
    # Use the mktemp program if available. If not available, hide the error
    # message.
    tmp=`(umask 077 && mktemp -d -q "$TMPDIR/gtXXXXXX") 2>/dev/null` &&
    test -n "$tmp" && test -d "$tmp"
  } ||
  {
    # Use a simple mkdir command. It is guaranteed to fail if the directory
    # already exists.  $RANDOM is bash specific and expands to empty in shells
    # other than bash, ksh and zsh.  Its use does not increase security;
    # rather, it minimizes the probability of failure in a very cluttered /tmp
    # directory.
    tmp=$TMPDIR/gt$$-$RANDOM
    (umask 077 && mkdir "$tmp")
  } ||
  {
    echo "$0: cannot create a temporary directory in $TMPDIR" >&2
    { (exit 1); exit 1; }
  }
}

# In order to construct a command that invokes csc, we need 'eval', because
# some of the arguments may contain spaces.
command_for_print=
command_for_eval=
options_csc_for_print=
options_csc_for_eval=
sources_csc_for_print=
sources_csc_for_eval=
# Protecting special characters, hiding them from 'eval':
# Double each backslash.
sed_protect_1='s/\\/\\\\/g'
# Escape each dollar, backquote, double-quote.
sed_protect_2a='s/\$/\\$/g'
sed_protect_2b='s/`/\\`/g'
sed_protect_2c='s/"/\\"/g'
# Add double-quotes at the beginning and end of the word.
sed_protect_3a='1s/^/"/'
sed_protect_3b='$s/$/"/'
func_add_word_to_command ()
{
  command_for_print="${command_for_print:+$command_for_print }$1"
  word_protected=`echo "$1" | sed -e "$sed_protect_1" -e "$sed_protect_2a" -e "$sed_protect_2b" -e "$sed_protect_2c" -e "$sed_protect_3a" -e "$sed_protect_3b"`
  command_for_eval="${command_for_eval:+$command_for_eval }$word_protected"
}
func_add_word_to_options_csc ()
{
  options_csc_for_print="${options_csc_for_print:+$options_csc_for_print }$1"
  word_protected=`echo "$1" | sed -e "$sed_protect_1" -e "$sed_protect_2a" -e "$sed_protect_2b" -e "$sed_protect_2c" -e "$sed_protect_3a" -e "$sed_protect_3b"`
  options_csc_for_eval="${options_csc_for_eval:+$options_csc_for_eval }$word_protected"
}
func_add_word_to_sources_csc ()
{
  sources_csc_for_print="${sources_csc_for_print:+$sources_csc_for_print }$1"
  word_protected=`echo "$1" | sed -e "$sed_protect_1" -e "$sed_protect_2a" -e "$sed_protect_2b" -e "$sed_protect_2c" -e "$sed_protect_3a" -e "$sed_protect_3b"`
  sources_csc_for_eval="${sources_csc_for_eval:+$sources_csc_for_eval }$word_protected"
}

sed_quote_subst='s/\([|&;<>()$`"'"'"'*?[#~=% 	\\]\)/\\\1/g'

options_mcs=
sources=
func_add_word_to_options_csc "-nologo"
while test $# != 0; do
  case "$1" in
    -o)
      case "$2" in
        *.dll)
          options_mcs="$options_mcs -target:library"
          func_add_word_to_options_csc "-target:library"
          ;;
        *.exe)
          func_add_word_to_options_csc "-target:exe"
          ;;
      esac
      options_mcs="$options_mcs -out:"`echo "$2" | sed -e "$sed_quote_subst"`
      # On Windows, assume that 'dotnet' and 'csc' are native Windows programs,
      # not Cygwin programs.
      arg="$2"
      case "@build_os@" in
        cygwin*)
          arg=`cygpath -w "$arg"`
          ;;
      esac
      func_add_word_to_options_csc "-out:$arg"
      shift
      ;;
    -L)
      options_mcs="$options_mcs -lib:"`echo "$2" | sed -e "$sed_quote_subst"`
      # On Windows, assume that 'dotnet' and 'csc' are native Windows programs,
      # not Cygwin programs.
      arg="$2"
      case "@build_os@" in
        cygwin*)
          arg=`cygpath -w "$arg"`
          ;;
      esac
      func_add_word_to_options_csc "-lib:$arg"
      shift
      ;;
    -l)
      options_mcs="$options_mcs -reference:"`echo "$2" | sed -e "$sed_quote_subst"`
      func_add_word_to_options_csc "-reference:$2.dll"
      shift
      ;;
    -O)
      func_add_word_to_options_csc "-optimize+"
      ;;
    -g)
      options_mcs="$options_mcs -debug"
      func_add_word_to_options_csc "-debug+"
      ;;
    -*)
      echo "csharpcomp: unknown option '$1'" 1>&2
      exit 1
      ;;
    *.resources)
      options_mcs="$options_mcs -resource:"`echo "$1" | sed -e "$sed_quote_subst"`
      # On Windows, assume that 'dotnet' and 'csc' are native Windows programs,
      # not Cygwin programs.
      arg="$1"
      case "@build_os@" in
        cygwin*)
          arg=`cygpath -w "$arg"`
          ;;
      esac
      func_add_word_to_options_csc "-resource:$arg"
      ;;
    *.cs)
      sources="$sources "`echo "$1" | sed -e "$sed_quote_subst"`
      # On Windows, assume that 'dotnet' and 'csc' are native Windows programs,
      # not Cygwin programs.
      arg="$1"
      case "@build_os@" in
        cygwin*)
          arg=`cygpath -w "$arg"`
          ;;
      esac
      func_add_word_to_sources_csc "$arg"
      ;;
    *)
      echo "csharpcomp: unknown type of argument '$1'" 1>&2
      exit 1
      ;;
  esac
  shift
done

if test -n "@HAVE_MCS@"; then
  # mcs prints it errors and warnings to stdout, not stderr. Furthermore it
  # adds a useless line "Compilation succeeded..." at the end. Correct both.
  sed_drop_success_line='${
/^Compilation succeeded/d
}'
  func_tmpdir
  trap 'rm -rf "$tmp"' HUP INT QUIT TERM
  test -z "$CSHARP_VERBOSE" || echo mcs $options_mcs $sources 1>&2
  mcs $options_mcs $sources > "$tmp"/mcs.err
  result=$?
  sed -e "$sed_drop_success_line" < "$tmp"/mcs.err >&2
  rm -rf "$tmp"
  exit $result
else
  if test -n "@HAVE_DOTNET_SDK@"; then
    dotnet_runtime_dir=`dotnet --list-runtimes | sed -n -e 's/Microsoft.NETCore.App \([^ ]*\) \[\(.*\)\].*/\2\/\1/p' | sed -e 1q`
    dotnet_sdk_dir=`dotnet --list-sdks | sed -e 's/\([^ ]*\) \[\(.*\)\].*/\2\/\1/p' | sed -e 1q`
    # Add -lib and -reference options, so that the compiler finds Object, Console, String, etc.
    arg="$dotnet_runtime_dir"
    case "@build_os@" in
      cygwin*)
        arg=`cygpath -w "$arg"`
        ;;
    esac
    func_add_word_to_options_csc "-lib:$arg"
    for file in `cd "$dotnet_runtime_dir" && echo [ABCDEFGHIJKLMNOPQRSTUVWXYZ]*.dll`; do
      case "$file" in
        *.Native.*) ;;
        *) func_add_word_to_options_csc "-reference:$file" ;;
      esac
    done
    func_add_word_to_command dotnet
    csc="$dotnet_sdk_dir/Roslyn/bincore/csc.dll"
    case "@build_os@" in
      cygwin*)
        csc=`cygpath -w "$csc"`
        ;;
    esac
    func_add_word_to_command "$csc"
    test -z "$CSHARP_VERBOSE" || echo "$command_for_print $options_csc_for_print $sources_csc_for_print" 1>&2
    eval "$command_for_eval $options_csc_for_eval $sources_csc_for_eval"
    exit $?
  else
    if test -n "@HAVE_DOTNET_CSC@" || test -n "@HAVE_CSC@"; then
      test -z "$CSHARP_VERBOSE" || echo "csc $options_csc_for_print $sources_csc_for_print" 1>&2
      eval "csc $options_csc_for_eval $sources_csc_for_eval"
      exit $?
    else
      echo 'C# compiler not found, try installing mono or dotnet, then reconfigure' 1>&2
      exit 1
    fi
  fi
fi