| 1 |
#!/bin/sh |
| 2 |
# Copyright 2016 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 |
# Attempt to create a subdirectory in builds/ suitable for |
| 17 |
# building nsync on this machine. Works on many Unix-like systems, |
| 18 |
# but for some you should create the Makefile by hand. |
| 19 |
# |
| 20 |
# Requires a Bourne shell, echo, expr, mkdir, uname, rm, |
| 21 |
# and, if the -diff option is used, diff. |
| 22 |
|
| 23 |
usage="usage: mkmakefile.sh [-diff|-out] [-dir <build_dir>] |
| 24 |
[-os <os>] [-arch <arch>] [-cc <cc>] |
| 25 |
[-sem futex|sem_t|mutex] |
| 26 |
[-atomic c++11|c11|asm|os] |
| 27 |
[-lrt] |
| 28 |
|
| 29 |
Create a build directory <build_dir> for the given operating system, |
| 30 |
machine architecture, and C compiler. |
| 31 |
Values not given explicitly are guessed. |
| 32 |
|
| 33 |
If -sem is given, it forces the use of futex, sem_t, or |
| 34 |
pthread_{mutex,cond}_t to implement a semaphore. |
| 35 |
|
| 36 |
If -atomic is given, it requests the use of C++11, C11, OS-supplied, or |
| 37 |
assembler-implemented atomics. Otherwise, atomics are usually supplied |
| 38 |
by compiler intrinsics. |
| 39 |
|
| 40 |
The flag -lrt indicates that librt is needed to provide the |
| 41 |
clock_gettime call. |
| 42 |
|
| 43 |
If -out is given, the makefile is written to stdout. |
| 44 |
If -diff is given, the makefile is compared with the |
| 45 |
one present in the directory. |
| 46 |
" |
| 47 |
|
| 48 |
# Compilers this script knows about. |
| 49 |
compilers="gcc clang g++ clang++ tcc lcc decc pcc tendracc" |
| 50 |
|
| 51 |
os= |
| 52 |
arch= |
| 53 |
cc= |
| 54 |
dir= |
| 55 |
action=create |
| 56 |
sem= |
| 57 |
libs= |
| 58 |
atomics= |
| 59 |
|
| 60 |
while [ $# -gt 0 ]; do |
| 61 |
arg="${1-}" |
| 62 |
case "$arg" in |
| 63 |
-arch) arch="${2?"$usage"}"; shift;; |
| 64 |
-atomic) atomics="${2?"$usage"}"; shift;; |
| 65 |
-cc) cc="${2?"$usage"}"; shift;; |
| 66 |
-diff) action=diff;; |
| 67 |
-dir) dir="${2?"$usage"}"; shift;; |
| 68 |
-lrt) case "$libs" in "") libs="-lrt";; *) libs="$libs -lrt";; esac;; |
| 69 |
-os) os="${2?"$usage"}"; shift;; |
| 70 |
-out) action=out;; |
| 71 |
-sem) sem="${2?"$usage"}"; shift;; |
| 72 |
*) echo "$usage" >&2; exit 2;; |
| 73 |
esac |
| 74 |
shift |
| 75 |
done |
| 76 |
|
| 77 |
# Find the root of the nsync tree. |
| 78 |
for root_dir in . .. ../.. ../../.. ../../../..; do |
| 79 |
if [ -f "$root_dir/public/nsync.h" ]; then |
| 80 |
break |
| 81 |
fi |
| 82 |
done |
| 83 |
if [ ! -f "$root_dir/public/nsync.h" ]; then |
| 84 |
echo "must be invoked within the nsync tree" >&2 |
| 85 |
exit 2 |
| 86 |
fi |
| 87 |
|
| 88 |
# Find the normalized operating system name. |
| 89 |
case "$os" in |
| 90 |
"") os=`uname -s` |
| 91 |
case "$os" in |
| 92 |
[lL][iI][nN][uU][xX]) os=linux;; |
| 93 |
[nN][eE][tT][bB][sS][dD]) os=netbsd;; |
| 94 |
[fF][rR][eE][eE][bB][sS][dD]) os=freebsd;; |
| 95 |
[oO][pP][eE][nN][bB][sS][dD]) os=openbsd;; |
| 96 |
[dD][aA][rR][wW][iI][nN]) os=macos;; |
| 97 |
[cC][yY][gG][wW][iI][nN]*) os=cygwin;; |
| 98 |
[oO][sS][fF]1) os=osf1;; |
| 99 |
[iI][rR][iI][xX]64) os=irix64;; |
| 100 |
[uU][lL][tT][rR][iI][xX]) os=ultrix;; |
| 101 |
esac;; |
| 102 |
esac |
| 103 |
|
| 104 |
# Find the normalized processor architecture name. |
| 105 |
case "$arch" in |
| 106 |
"") arch=`uname -m` |
| 107 |
case "$arch" in |
| 108 |
[iI][3456]86|[xX]86|[xX]86_32) arch=x86_32;; |
| 109 |
[aA][mM][dD]64|[xX]86_64) arch=x86_64;; |
| 110 |
[iI][pP]35) arch=mips;; |
| 111 |
[rR][iI][sS][cC]) arch=mips;; |
| 112 |
[vV][aA][xX]) arch=vax;; |
| 113 |
esac;; |
| 114 |
esac |
| 115 |
|
| 116 |
# Find a C compiler. |
| 117 |
case "$cc" in |
| 118 |
"") for x in cc $compilers; do |
| 119 |
path="$PATH" |
| 120 |
while [ -n "$path" -a ! -n "$cc" ]; do |
| 121 |
d=`expr "$path" : '\([^:]*\)'` |
| 122 |
path=`expr "$path" : '[^:]*:\(.*\)'` |
| 123 |
if [ -x "$d/$x" ]; then |
| 124 |
cc="$x" |
| 125 |
fi |
| 126 |
done |
| 127 |
done |
| 128 |
case "$cc" in |
| 129 |
"") echo "can't find C compiler: use -cc flag" >&2 |
| 130 |
exit 2;; |
| 131 |
esac;; |
| 132 |
esac |
| 133 |
|
| 134 |
# Work out what sort of C compiler it is. |
| 135 |
cplusplus= |
| 136 |
case "$cc" in *++*) cplusplus=c++;; esac |
| 137 |
cc_type2="$cc" |
| 138 |
cc_type= |
| 139 |
case "$cc_type2" in *" "*) cc_type2=`expr "$cc_type2" : '\([^ ]*\) .*$'`;; esac |
| 140 |
case "$cc_type2" in */*) cc_type2=`expr "$cc_type2" : '[^\([^ ]*\) .*$'`;; esac |
| 141 |
case "$cc_type2" in |
| 142 |
cc) if version=`cc -v 2>&1 || cc -V 2>&1`; then |
| 143 |
cc_type2=`echo "$version" | while read line; do |
| 144 |
case "$line" in |
| 145 |
"gcc "*) echo gcc; break;; |
| 146 |
"Apple LLVM "*) echo clang; break;; |
| 147 |
"clang "*|*" clang "*) echo clang; break;; |
| 148 |
"tcc "*) echo tcc; break;; |
| 149 |
"lcc "*) echo lcc; break;; |
| 150 |
"Compaq C "*|"DEC C "*) echo decc; break;; |
| 151 |
esac |
| 152 |
done` |
| 153 |
fi |
| 154 |
cc_type="$cc_type2";; |
| 155 |
g++) cc_type=gcc;; |
| 156 |
clang++) cc_type=clang;; |
| 157 |
*) cc_type="$cc_type2";; |
| 158 |
esac |
| 159 |
|
| 160 |
# Find a build directory. |
| 161 |
case "$dir" in |
| 162 |
"") dir="$root_dir/builds/$arch.$os.$cc_type2" |
| 163 |
case "$sem" in ?*) dir="$dir.sem-$sem";; esac |
| 164 |
case "$atomics" in ?*) dir="$dir.atm-$atomics";; esac |
| 165 |
case "$libs" in *-lrt*) dir="$dir.lrt";; esac |
| 166 |
;; |
| 167 |
esac |
| 168 |
|
| 169 |
case "$action" in |
| 170 |
create) if [ -d "$dir" ]; then echo "$dir exists" >&2; exit 2; fi;; |
| 171 |
diff) if [ ! -d "$dir" ]; then echo "$dir doesn't exist" >&2; exit 2; fi;; |
| 172 |
esac |
| 173 |
|
| 174 |
# Get C preprocessor flags. |
| 175 |
cppflags="-D_POSIX_C_SOURCE=200809L" # Sometimes needed; doesn't hurt. |
| 176 |
# gcc's TLS doesn't work on OpenBSD or Irix. |
| 177 |
case "$arch.$os.$cc_type" in |
| 178 |
*.openbsd.gcc|*.openbsd.clang|*.irix64.gcc) |
| 179 |
cppflags="$cppflags -I../../platform/gcc_no_tls";; |
| 180 |
esac |
| 181 |
first="$cc_type" |
| 182 |
second="$os" |
| 183 |
case "$atomics" in |
| 184 |
c11) cppflags="-DNSYNC_ATOMIC_C11 -I../../platform/c11 $cppflags";; |
| 185 |
c++11) cppflags="-I../../platform/atomic_ind $cppflags";; |
| 186 |
os) first="$os" second="$cc_type";; |
| 187 |
esac |
| 188 |
for x in "$first" "$second" "$arch" posix; do |
| 189 |
if [ -d "$root_dir/platform/$x" ]; then |
| 190 |
cppflags="$cppflags -I../../platform/$x" |
| 191 |
fi |
| 192 |
done |
| 193 |
|
| 194 |
# If not using gcc/clang, or explcitly requested, attempt to find an assembly |
| 195 |
# language set of atomic ops. |
| 196 |
asm_atomics=false |
| 197 |
case "$cc_type.$atomics.$os" in |
| 198 |
*.c11.*|*.c++11.*) ;; |
| 199 |
gcc..irix64) asm_atomics=true;; |
| 200 |
gcc..*|clang..*) ;; |
| 201 |
*..*|*.asm.*) asm_atomics=true;; |
| 202 |
esac |
| 203 |
atomic_ind= |
| 204 |
atomic_c= |
| 205 |
atomic_s= |
| 206 |
case "$asm_atomics" in |
| 207 |
true) for x in `cd "$root_dir"; echo "platform/$arch/src/nsync_atm_$arch."[csS]`; do |
| 208 |
if [ -f "$root_dir/$x" ]; then |
| 209 |
atomic_ind="-I../../platform/atomic_ind " |
| 210 |
case "$x" in |
| 211 |
*.[sS]) atomic_s="../../$x";; |
| 212 |
*) atomic_c="../../$x";; |
| 213 |
esac |
| 214 |
break |
| 215 |
fi |
| 216 |
done;; |
| 217 |
esac |
| 218 |
|
| 219 |
# Some platforms take -pthread flag, and others -pthread. |
| 220 |
ldflags= |
| 221 |
use_pthread=false |
| 222 |
case "$cc_type.$os" in |
| 223 |
*.irix64) ;; |
| 224 |
gcc.*|clang.*|tcc.*|decc.*|pcc.*) use_pthread=true;; |
| 225 |
esac |
| 226 |
case "$use_pthread" in |
| 227 |
true) cppflags="$cppflags -pthread" |
| 228 |
case "$ldflags" in |
| 229 |
"") ldflags="-pthread";; |
| 230 |
*) ldflags="$ldflags -pthread";; |
| 231 |
esac;; |
| 232 |
*) case "$libs" in |
| 233 |
"") libs="-lpthread";; |
| 234 |
*) libs="$libs -lpthread";; |
| 235 |
esac;; |
| 236 |
esac |
| 237 |
|
| 238 |
# Which semaphore implementation to use. |
| 239 |
case "$os.$sem" in |
| 240 |
linux.|linux.futex) semfile="../../platform/linux/src/nsync_semaphore_futex.c ";; |
| 241 |
*.|*.futex) semfile="../../platform/posix/src/nsync_semaphore_mutex.c ";; |
| 242 |
*) semfile="../../platform/posix/src/nsync_semaphore_$sem.c ";; |
| 243 |
esac |
| 244 |
|
| 245 |
# Some platforms don't have clock_gettime. |
| 246 |
clock_gettime_src= |
| 247 |
case "$os" in |
| 248 |
macos) clock_gettime_src="../../platform/posix/src/clock_gettime.c ";; |
| 249 |
esac |
| 250 |
|
| 251 |
case "$cc_type" in |
| 252 |
pcc) # Compilers like pcc can't pass/return structs properly. Use an integer for the time. |
| 253 |
time_rep_src="../../platform/num_time/src/time_rep.c" |
| 254 |
cppflags="-DNSYNC_USE_INT_TIME=int64_t -I../../platform/num_time $cppflags";; |
| 255 |
*) time_rep_src="../../platform/posix/src/time_rep.c";; |
| 256 |
esac |
| 257 |
|
| 258 |
# Platform-specific files. |
| 259 |
platform_c="$atomic_c$clock_gettime_src$semfile../../platform/posix/src/per_thread_waiter.c ../../platform/posix/src/yield.c $time_rep_src ../../platform/posix/src/nsync_panic.c" |
| 260 |
platform_s="$atomic_s" |
| 261 |
sp= |
| 262 |
platform_o= |
| 263 |
for x in $platform_s $platform_c; do |
| 264 |
o=`expr "$x" : '.*/\([^/]*\)[.][^/]*$'`.o |
| 265 |
platform_o="$platform_o$sp$o" |
| 266 |
sp=" " |
| 267 |
done |
| 268 |
test_platform_c="../../platform/posix/src/start_thread.c" |
| 269 |
sp= |
| 270 |
test_platform_o= |
| 271 |
for x in $test_platform_c; do |
| 272 |
o=`expr "$x" : '.*/\([^/]*\)[.][^/]*$'`.o |
| 273 |
test_platform_o="$test_platform_o$sp$o" |
| 274 |
sp=" " |
| 275 |
done |
| 276 |
|
| 277 |
makefile=` |
| 278 |
case "$cc" in |
| 279 |
cc) ;; |
| 280 |
*) echo "CC=$cc";; |
| 281 |
esac |
| 282 |
case "$cc_type.$cplusplus" in |
| 283 |
gcc.) echo "PLATFORM_CPPFLAGS=$atomic_ind$cppflags" |
| 284 |
echo "PLATFORM_CFLAGS=-Werror -Wall -Wextra -ansi -pedantic" |
| 285 |
case "$ldflags" in ?*) echo "PLATFORM_LDFLAGS=$ldflags";; esac |
| 286 |
case "$libs" in ?*) echo "PLATFORM_LIBS=$libs";; esac |
| 287 |
case "$atomics" in |
| 288 |
c++11) echo "PLATFORM_CXXFLAGS=-Werror -Wall -Wextra -std=c++11 -pedantic" |
| 289 |
echo "PLATFORM_CXX=../../platform/c_from_c++11/src/nsync_atm_c++.cc" |
| 290 |
echo "MKDEP_DEPEND=mkdep" |
| 291 |
echo 'MKDEP=./mkdep ${CC} -E -c++=-std=c++11' |
| 292 |
platform_o="nsync_atm_c++.o $platform_o";; |
| 293 |
*) echo 'MKDEP=${CC} -M';; |
| 294 |
esac |
| 295 |
echo "PLATFORM_C=$platform_c" |
| 296 |
case "$platform_s" in ?*) echo "PLATFORM_S=$platform_s";; esac |
| 297 |
echo "PLATFORM_OBJS=$platform_o" |
| 298 |
echo "TEST_PLATFORM_C=$test_platform_c" |
| 299 |
echo "TEST_PLATFORM_OBJS=$test_platform_o" |
| 300 |
;; |
| 301 |
clang.) echo "PLATFORM_CPPFLAGS=$atomic_ind$cppflags" |
| 302 |
echo "PLATFORM_CFLAGS=-Werror -Wall -Wextra -ansi -pedantic -Wno-unneeded-internal-declaration" |
| 303 |
case "$ldflags" in ?*) echo "PLATFORM_LDFLAGS=$ldflags";; esac |
| 304 |
case "$libs" in ?*) echo "PLATFORM_LIBS=$libs";; esac |
| 305 |
case "$atomics" in |
| 306 |
c++11) echo "PLATFORM_CXXFLAGS=-Werror -Wall -Wextra -std=c++11 -pedantic" |
| 307 |
echo "PLATFORM_CXX=../../platform/c_from_c++11/src/nsync_atm_c++.cc" |
| 308 |
echo "MKDEP_DEPEND=mkdep" |
| 309 |
echo 'MKDEP=./mkdep ${CC} -E -c++=-std=c++11' |
| 310 |
platform_o="nsync_atm_c++.o $platform_o";; |
| 311 |
*) echo 'MKDEP=${CC} -M';; |
| 312 |
esac |
| 313 |
echo "PLATFORM_C=$platform_c" |
| 314 |
case "$platform_s" in ?*) echo "PLATFORM_S=$platform_s";; esac |
| 315 |
echo "PLATFORM_OBJS=$platform_o" |
| 316 |
echo "TEST_PLATFORM_C=$test_platform_c" |
| 317 |
echo "TEST_PLATFORM_OBJS=$test_platform_o" |
| 318 |
;; |
| 319 |
gcc.c++) echo "PLATFORM_CPPFLAGS=-DNSYNC_ATOMIC_CPP11 -I../../platform/c++11 $cppflags" |
| 320 |
echo "PLATFORM_CFLAGS=-x c++ -std=c++11 -Werror -Wall -Wextra -pedantic" |
| 321 |
case "$ldflags" in ?*) echo "PLATFORM_LDFLAGS=$ldflags";; esac |
| 322 |
case "$libs" in ?*) echo "PLATFORM_LIBS=$libs";; esac |
| 323 |
echo 'MKDEP=${CC} -M -x c++ -std=c++11' |
| 324 |
echo "PLATFORM_C=$platform_c" |
| 325 |
case "$platform_s" in ?*) echo "PLATFORM_S=$platform_s";; esac |
| 326 |
echo "PLATFORM_OBJS=$platform_o" |
| 327 |
echo "TEST_PLATFORM_C=$test_platform_c" |
| 328 |
echo "TEST_PLATFORM_OBJS=$test_platform_o" |
| 329 |
;; |
| 330 |
clang.c++) echo "PLATFORM_CPPFLAGS=-DNSYNC_ATOMIC_CPP11 -I../../platform/c++11 $cppflags" |
| 331 |
echo "PLATFORM_CFLAGS=-x c++ -std=c++11 -Werror -Wall -Wextra -pedantic -Wno-unneeded-internal-declaration" |
| 332 |
case "$ldflags" in ?*) echo "PLATFORM_LDFLAGS=$ldflags";; esac |
| 333 |
case "$libs" in ?*) echo "PLATFORM_LIBS=$libs";; esac |
| 334 |
echo 'MKDEP=${CC} -M -x c++ -std=c++11' |
| 335 |
echo "PLATFORM_C=$platform_c" |
| 336 |
case "$platform_s" in ?*) echo "PLATFORM_S=$platform_s";; esac |
| 337 |
echo "PLATFORM_OBJS=$platform_o" |
| 338 |
echo "TEST_PLATFORM_C=$test_platform_c" |
| 339 |
echo "TEST_PLATFORM_OBJS=$test_platform_o" |
| 340 |
;; |
| 341 |
tcc.) echo "PLATFORM_CPPFLAGS=$atomic_ind$cppflags" |
| 342 |
echo "PLATFORM_CFLAGS=-Werror -Wall" |
| 343 |
case "$ldflags" in ?*) echo "PLATFORM_LDFLAGS=$ldflags";; esac |
| 344 |
case "$libs" in ?*) echo "PLATFORM_LIBS=$libs";; esac |
| 345 |
echo "MKDEP_DEPEND=mkdep" |
| 346 |
echo 'MKDEP=./mkdep ${CC} -E' |
| 347 |
echo "PLATFORM_C=$platform_c" |
| 348 |
case "$platform_s" in ?*) echo "PLATFORM_S=$platform_s";; esac |
| 349 |
echo "PLATFORM_OBJS=$platform_o" |
| 350 |
echo "TEST_PLATFORM_C=$test_platform_c" |
| 351 |
echo "TEST_PLATFORM_OBJS=$test_platform_o" |
| 352 |
;; |
| 353 |
lcc.) echo "PLATFORM_CPPFLAGS=$atomic_ind$cppflags" |
| 354 |
case "$libs" in ?*) echo "PLATFORM_LIBS=$libs";; esac |
| 355 |
echo "MKDEP_DEPEND=mkdep" |
| 356 |
echo 'MKDEP=./mkdep ${CC} -E' |
| 357 |
echo "PLATFORM_C=$platform_c" |
| 358 |
case "$platform_s" in ?*) echo "PLATFORM_S=$platform_s";; esac |
| 359 |
echo "PLATFORM_OBJS=$platform_o" |
| 360 |
echo "TEST_PLATFORM_C=$test_platform_c" |
| 361 |
echo "TEST_PLATFORM_OBJS=$test_platform_o" |
| 362 |
;; |
| 363 |
decc.) echo "PLATFORM_CPPFLAGS=$atomic_ind$cppflags -Duintmax_t=uint64_t" |
| 364 |
echo "PLATFORM_CFLAGS=-std1" |
| 365 |
case "$ldflags" in ?*) echo "PLATFORM_LDFLAGS=$ldflags";; esac |
| 366 |
case "$libs" in |
| 367 |
?*) echo "PLATFORM_LIBS=$libs -lrt";; |
| 368 |
*) echo "PLATFORM_LIBS=-lrt";; |
| 369 |
esac |
| 370 |
echo 'MKDEP=${CC} -M' |
| 371 |
echo "PLATFORM_C=$platform_c" |
| 372 |
case "$platform_s" in ?*) echo "PLATFORM_S=$platform_s";; esac |
| 373 |
echo "PLATFORM_OBJS=$platform_o" |
| 374 |
echo "TEST_PLATFORM_C=$test_platform_c" |
| 375 |
echo "TEST_PLATFORM_OBJS=$test_platform_o" |
| 376 |
;; |
| 377 |
pcc.) echo "PLATFORM_CPPFLAGS=$atomic_ind$cppflags" |
| 378 |
case "$ldflags" in ?*) echo "PLATFORM_LDFLAGS=$ldflags";; esac |
| 379 |
case "$libs" in ?*) echo "PLATFORM_LIBS=$libs";; esac |
| 380 |
echo 'MKDEP=${CC} -M' |
| 381 |
echo "PLATFORM_C=$platform_c" |
| 382 |
case "$platform_s" in ?*) echo "PLATFORM_S=$platform_s";; esac |
| 383 |
echo "PLATFORM_OBJS=$platform_o" |
| 384 |
echo "TEST_PLATFORM_C=$test_platform_c" |
| 385 |
echo "TEST_PLATFORM_OBJS=$test_platform_o" |
| 386 |
;; |
| 387 |
*) echo "PLATFORM_CPPFLAGS=$atomic_ind$cppflags" |
| 388 |
echo "PLATFORM_LIBS=$libs" |
| 389 |
echo "MKDEP_DEPEND=mkdep" |
| 390 |
echo 'MKDEP=./mkdep ${CC} -E' |
| 391 |
echo "PLATFORM_C=$platform_c" |
| 392 |
case "$platform_s" in ?*) echo "PLATFORM_S=$platform_s";; esac |
| 393 |
echo "PLATFORM_OBJS=$platform_o" |
| 394 |
echo "TEST_PLATFORM_C=$test_platform_c" |
| 395 |
echo "TEST_PLATFORM_OBJS=$test_platform_o" |
| 396 |
;; |
| 397 |
esac |
| 398 |
echo |
| 399 |
echo "include ../../platform/posix/make.common" |
| 400 |
echo "include dependfile" |
| 401 |
` |
| 402 |
|
| 403 |
case "$action" in |
| 404 |
out) echo "$makefile";; |
| 405 |
diff) echo "$makefile" | diff - "$dir/Makefile";; |
| 406 |
create) mkdir -p "$dir" || exit 2 |
| 407 |
> "$dir/dependfile" |
| 408 |
echo "$makefile" > "$dir/Makefile" |
| 409 |
echo "Created $dir/Makefile";; |
| 410 |
esac |