Debian Codegraph @

tools/detect.sh

raw
1 #!/bin/sh
2 # Copyright 2018 Google Inc.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 # Detect platform properties based on the cc command given as first argument,
17 # then invoke the make command that follows on the command line with variables
18 # NSYNC_OS, NSYNC_OS_TYPE, NSYNC_CC, and NSYNC_ARCH indicating those
19 # properties.
20 #
21 # This script must use _only_ properties derived from the compiler supplied,
22 # becuase that may be a cross compiler, targeting a platform quite different
23 # from the host.
24 #
25 # detect.sh will work for most Unix-like platforms. Uses the path to the detect.c
26 # header if specified; otherwise it assumes that it is called "detect.c" in the same
27 # directory as the detect.sh script.
28
29 usage="usage: detect.sh \\
30 [-target_lang <target_lang_type>] \\
31 [-target_arch <target_arch_type>] \\
32 [-target_cc <target_cc_type>] \\
33 [-target_os <target_os_type>] \\
34 [-nsync_base <path_to_nsync_root>] \\
35 [-detect_path <path_to_detect.c>] \\
36 <cc_command> <make_command> [make_args...]
37
38 It is not usually necessary to specify options. The target compiler, operating system, language, and architecture
39 types are normally deduced by running the supplied compiler.
40
41 The paths are normally inferred from the structure of the nsync tree and the
42 fact that this script is usually invoked from a child of the 'builds'
43 directory.
44 "
45
46 detect_base=detect.c
47
48 detect_path=
49 nsync_base=../..
50 target_lang=
51 target_arch=
52 target_cc=
53 target_os=
54 while
55 arg="$1"
56 case "$arg" in
57 -*) found=false
58 for flag in target_lang target_arch target_cc target_os detect_path nsync_base; do
59 case "$arg" in
60 "-$flag") eval $flag='"${2?"$usage"}"'; shift; found=true; break;;
61 -"$flag"=*) s=`expr "$arg" : '^[^=]*=\(.*\)$'`; eval $flag="'$s'"; found=true; break;;
62 esac
63 done
64 case "$found" in false) echo "$usage" >&2; exit 2;; esac
65 ;;
66 *) break;;
67 esac
68 do
69 shift
70 done
71
72 case $# in [01]) echo "$usage" >&2; exit 2;; esac
73 cc="$1"
74 make="$2"
75 shift
76 shift
77
78 case "$detect_path" in
79 "") case "$0" in
80 */*) detect_path=`expr "$0" : '^\(.*/\)[^/]*$'`"$detect_base";;
81 *) path="$PATH":.
82 while [ ! -f "$detect_path" -a -n "$path" ]; do
83 detect_path=`expr "$path" : '^\([^:]*\).*$'`"/$detect_base"
84 path=`expr "$path" : '^[^:]*:\(.*\)$'`
85 done;;
86 esac
87 esac
88
89 if [ ! -f "$detect_path" ]; then
90 echo "$0: can't find $detect_base; maybe use -detect_path flag?" >&2; exit 2
91 fi
92
93 # Set variables detected_* from detect.c
94 eval `$cc -E "$detect_path" | sed -n 's,^\(detected_.*=.*\)X,\1,p'`
95
96 case "$detected_cc.$detected_lang" in
97 pcc.c11) # pcc lies when it claims to implement c11
98 detected_lang=c;;
99 esac
100
101 # Allow overrides from the command line.
102 case "$target_lang" in ?*) detected_lang="$target_lang";; esac
103 case "$target_arch" in ?*) detected_arch="$target_arch";; esac
104 case "$target_cc" in ?*) detected_cc="$target_cc";; esac
105 case "$target_os" in ?*) detected_os="$target_os";; esac
106
107 extra_c="$DETECT_EXTRA_CFLAGS"
108 extra_objs=
109 extra_s=
110 extra_cpp_flags="$DETECT_EXTRA_CPPFLAGS"
111 extra_libs="$DETECT_EXTRA_LIBS"
112
113 # If we can't get atomics from the OS or the compiler, try to find an assembly
114 # language implementation based on the architecture, or one provided by the
115 # language library.
116 if [ ! -f "$nsync_base/platform/$detected_os/atomic.h" -a ! -f "$nsync_base/platform/$detected_cc/atomic.h" ]; then
117 case "$detected_lang" in
118 c11) extra_cpp_flags="$extra_cpp_flags -DNSYNC_ATOMIC_C11 -I../../platform/c11";;
119 c++11) extra_cpp_flags="$extra_cpp_flags -DNSYNC_USE_CPP11_TIMEPOINT -DNSYNC_ATOMIC_CPP11 -I../../platform/c++11";;
120 *) # Try for assembly language.
121 for x in "$nsync_base/platform/$detected_arch/src/nsync_atm_$detected_arch".[sS]; do
122 if [ -f "$x" ]; then
123 extra_cpp_flags="$extra_cpp_flags -I$nsync_base/platform/atomic_ind"
124 extra_s="$extra_s $x"
125 extra_objs="$extra_objs nsync_atm_$detected_arch.o"
126 break
127 fi
128 done;;
129 esac
130 fi
131
132 # Some systems are special in various ways.
133 case "$detected_arch.$detected_os.$detected_cc" in
134 *.macos.*) # Some MacOS versions don't provide clock_gettime().
135 extra_c="$extra_c $nsync_base/platform/posix/src/clock_gettime.c"
136 extra_obj="$extra_objs clock_gettime.o";;
137 *.linux.*) # Some Linux boxes keep clock_gettime() in librt.
138 extra_libs="$extra_libs -lrt";;
139 esac
140
141 # Some compilers support cc -M, but others do not.
142 case "$detected_cc" in
143 gcc|clang|decc) mkdep="$cc -M";;
144 *) mkdep="$nsync_base/tools/mkdep.sh $cc -E";;
145 esac
146 case "$detected_cc" in
147 lcc) extra_c="$extra_c ../../platform/posix/src/time_rep.c"
148 extra_objs="$extra_objs time_rep.o"
149 pthread=
150 cflags=
151 ldflags=-lpthread
152 opt=OPT=;;
153 tendracc)
154 extra_c="$extra_c ../../platform/posix/src/time_rep.c"
155 extra_objs="$extra_objs time_rep.o"
156 pthread=
157 cflags=
158 ldflags=-lpthread
159 opt=OPT=;;
160 pcc) extra_c="$extra_c ../../platform/num_time/src/time_rep.c"
161 extra_objs="$extra_objs time_rep.o"
162 extra_cpp_flags="$extra_cpp_flags -I../../platform/num_time -DNSYNC_USE_INT_TIME=int64_t"
163 pthread=-pthread
164 cflags=
165 ldflags=-pthread
166 opt=;;
167 *) extra_c="$extra_c ../../platform/posix/src/time_rep.c"
168 extra_objs="$extra_objs time_rep.o"
169 pthread=-pthread
170 cflags=
171 ldflags=-pthread
172 opt=;;
173 esac
174
175 # Invoke command from command line, with various arguments inserted.
176 "$make" -e \
177 "MKDEP=$mkdep" \
178 "NSYNC_EXTRA_INC_DIR=$extra_cpp_flags" "NSYNC_EXTRA_PLATFORM_C=$extra_c" "PLATFORM_S=$extra_s" \
179 "NSYNC_EXTRA_OBJS=$extra_objs" "NSYNC_OS=$detected_os" \
180 "NSYNC_OS_TYPE=posix" "NSYNC_CC=$detected_cc" "NSYNC_ARCH=$detected_arch" \
181 "NSYNC_PTHREAD=$pthread" \
182 "PLATFORM_CFLAGS=$cflags" \
183 "PLATFORM_LIBS=$extra_libs" \
184 "PLATFORM_LDFLAGS=$ldflags" \
185 $opt \
186 ${1+"$@"}