OILS / build / ninja-rules-cpp.sh View on Github | oils.pub

438 lines, 256 significant
1#!/usr/bin/env bash
2#
3# Actions invoked by build.ninja, which is generated by ./NINJA-config.sh.
4#
5# It's distributed with the tarball and invoked by _build/oils.sh, so
6# it's written in a /bin/sh style. But we're not using /bin/sh yet.
7#
8# And some non-Ninja wrappers.
9#
10# Usage:
11# build/ninja-rules-cpp.sh <function name>
12#
13# Env variables:
14# BASE_CXXFLAGS= default flags passed to all compiler invocations
15# CXXFLAGS= additional flags
16# OILS_CXX_VERBOSE=1 show compiler command lines
17# TIME_TSV_OUT=file compile_one and link output rows to this TSV file
18
19set -o nounset
20set -o errexit
21# For /bin/sh portability
22#eval 'set -o pipefail'
23
24REPO_ROOT=$(cd "$(dirname $0)/.."; pwd)
25
26. build/common.sh # for $BASE_CXXFLAGS
27. build/dev-shell.sh # python2 in $PATH
28
29# for HAVE_READLINE, READLINE_DIR, and STRIP_FLAGS
30if ! . _build/detected-config.sh; then
31 die "Can't find _build/detected-config.sh. Run './configure'"
32fi
33
34line_count() {
35 local out=$1
36 shift # rest are inputs
37 wc -l "$@" | sort -n | tee $out
38}
39
40#
41# Mutable GLOBALS
42#
43
44cxx='' # compiler
45flags='' # compile flags
46link_flags='' # link flags
47
48#
49# Functions to set them
50#
51
52setglobal_cxx() {
53 local compiler=$1
54
55 case $compiler in
56 (clang) cxx=$CLANGXX ;;
57 # Note: we could get rid of this "alias", and use 'c++' everywhere
58 (cxx) cxx='c++' ;;
59
60 # e.g. could be cosmoc++
61 (*) cxx=$compiler ;;
62 esac
63}
64
65setglobal_compile_flags() {
66 ### Set flags based on $variant $more_cxx_flags and $dotd
67
68 local variant=$1
69 local more_cxx_flags=$2 # e.g. NINJA_subgraph.py sets -D CPP_UNIT_TEST
70 local dotd=${3:-}
71
72 # flags from Ninja/shell respected
73 flags="$BASE_CXXFLAGS -I $REPO_ROOT $more_cxx_flags"
74
75 # Flags from env
76 # Similar to
77 # - GNU make - https://www.gnu.org/software/make/manual/html_node/Implicit-Variables.html
78 # CXXFLAGS "Extra flags to give to the C++ compiler"
79 # - CMake - https://cmake.org/cmake/help/latest/envvar/CXXFLAGS.html
80 # "Add default compilation flags to be used when compiling CXX (C++) files."
81
82 local env_flags=${CXXFLAGS:-}
83 if test -n "$env_flags"; then
84 flags="$flags $env_flags"
85 fi
86
87 if test -n "$READLINE_DIR"; then
88 flags="$flags -I${READLINE_DIR}/include"
89 fi
90
91 case $variant in
92 *+bumpleak|*+bumproot)
93 ;;
94 *+bigint)
95 flags="$flags -D MARK_SWEEP -D BIGINT"
96 ;;
97 *)
98 flags="$flags -D MARK_SWEEP"
99 ;;
100 esac
101
102 # First half of variant: what affects ALL translation units
103
104 case $variant in
105 dbg*)
106 flags="$flags -O0 -g"
107 ;;
108
109 asan*)
110 # CLEAN_PROCESS_EXIT avoids spurious memory leaks
111 flags="$flags -O0 -g -fsanitize=address -D CLEAN_PROCESS_EXIT"
112 ;;
113
114 tsan*)
115 flags="$flags -O0 -g -fsanitize=thread"
116 ;;
117
118 ubsan*)
119 # Extra flag to make it fatal
120 # https://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html
121
122 flags="$flags -O0 -g -fsanitize=undefined -fno-sanitize-recover=null"
123 ;;
124
125 opt*)
126 flags="$flags -O2 -g -D OPTIMIZED"
127 ;;
128
129 coverage*)
130 # source-based coverage is more precise than say sanitizer-based
131 # https://clang.llvm.org/docs/SourceBasedCodeCoverage.html
132 flags="$flags -O0 -g -fprofile-instr-generate -fcoverage-mapping"
133 ;;
134
135 uftrace*)
136 # -O0 creates a A LOT more data. But sometimes we want to see the
137 # structure of the code.
138 # NewStr(), OverAllocatedStr(), StrFromC() etc. are not inlined
139 # Ditto vector::size(), std::forward, len(), etc.
140
141 local opt='-O0'
142 #local opt='-O2'
143 flags="$flags $opt -g -pg"
144 ;;
145
146 (*)
147 die "Invalid variant $variant"
148 ;;
149 esac
150
151 # for cxx-dbg32, cxx-opt32+bumpleak, etc.
152 case $variant in
153 *32*)
154 flags="$flags -m32"
155 ;;
156 esac
157
158 # OPTIONAL second half of variant: for the application
159
160 case $variant in
161 *+gcalways)
162 flags="$flags -D GC_ALWAYS"
163 ;;
164
165 *+tcmalloc)
166 flags="$flags -D TCMALLOC"
167 ;;
168
169 *+bumpleak)
170 flags="$flags -D BUMP_LEAK"
171 ;;
172 *+bumproot)
173 flags="$flags -D BUMP_LEAK -D BUMP_ROOT"
174 ;;
175
176 *+bumpsmall)
177 # the pool allocator should approximate opt+bumpsmall (which doesn't support GC)
178 flags="$flags -D BUMP_ROOT -D BUMP_SMALL -D NO_POOL_ALLOC"
179 ;;
180
181 *+nopool)
182 flags="$flags -D NO_POOL_ALLOC"
183 ;;
184 esac
185
186 # needed to strip unused symbols
187 # https://stackoverflow.com/questions/6687630/how-to-remove-unused-c-c-symbols-with-gcc-and-ld
188
189 # Note: -ftlo doesn't do anything for size?
190
191 flags="$flags -fdata-sections -ffunction-sections"
192
193 # https://ninja-build.org/manual.html#ref_headers
194 if test -n "$dotd"; then
195 flags="$flags -MD -MF $dotd"
196 fi
197}
198
199setglobal_link_flags() {
200 local variant=$1
201 local more_link_flags=${2:-} # from NINJA_subgraph.py, e.g. Souffle datalog
202
203 link_flags="$more_link_flags" # initialize
204
205 # Linker flags based on build variant
206 local variant_flags=''
207 case $variant in
208 # Must REPEAT these flags, otherwise we lose sanitizers / coverage
209 asan*)
210 variant_flags='-fsanitize=address'
211 ;;
212
213 tcmalloc)
214 # Need to tell the dynamic loader where to find tcmalloc
215 variant_flags='-ltcmalloc -Wl,-rpath,/usr/local/lib'
216 ;;
217
218 tsan)
219 variant_flags='-fsanitize=thread'
220 ;;
221 ubsan*)
222 variant_flags='-fsanitize=undefined'
223 ;;
224 coverage*)
225 variant_flags='-fprofile-instr-generate -fcoverage-mapping'
226 ;;
227 esac
228
229 if test -n "$variant_flags"; then
230 link_flags="$link_flags $variant_flags"
231 fi
232
233 # More build variant flags, and GNU readline flags
234 case $variant in
235 # TODO: 32-bit variants can't handle -l readline right now.
236 *32*)
237 link_flags="$link_flags -m32"
238 ;;
239
240 *)
241 if test "$HAVE_READLINE" = 1; then
242 link_flags="$link_flags -lreadline"
243 fi
244 if test -n "$READLINE_DIR"; then
245 link_flags="$link_flags -L${READLINE_DIR}/lib"
246 fi
247 ;;
248 esac
249
250 # Detected by ./configure
251 if test -n "${STRIP_FLAGS:-}"; then
252 link_flags="$link_flags -Wl,$STRIP_FLAGS"
253 fi
254
255 # Set by user/packager
256 local env_flags=${LDFLAGS:-}
257 if test -n "$env_flags"; then
258 link_flags="$link_flags $env_flags"
259 fi
260}
261
262compile_one() {
263 ### Compile one translation unit. Invoked by build.ninja
264
265 local compiler=$1
266 local variant=$2
267 local more_cxx_flags=$3
268 local in=$4
269 local out=$5
270 local dotd=${6:-} # optional .d file
271
272 setglobal_compile_flags "$variant" "$more_cxx_flags" "$dotd"
273
274 case $out in
275 _build/preprocessed/*)
276 flags="$flags -E"
277 ;;
278
279 # DISABLE spew for mycpp-generated code. mycpp/pea could flag this at the
280 # PYTHON level, rather than doing it at the C++ level.
281 _build/obj/*/_gen/bin/oils_for_unix.mycpp.o)
282 flags="$flags -Wno-unused-variable -Wno-unused-but-set-variable"
283 ;;
284 esac
285
286 if test "$compiler" = 'clang'; then
287 # 2024-08 - Clang needs -stdlib=libc++ for some reason
288 # https://stackoverflow.com/questions/26333823/clang-doesnt-see-basic-headers
289 # https://stackoverflow.com/questions/19774778/when-is-it-necessary-to-use-the-flag-stdlib-libstdc
290
291 # But don't do it for clang-coverage* builds, because the CI machine
292 # doesn't like it? This makes it fail on the release machine - sigh
293 case $variant in
294 coverage*) ;; # include coverage+bumpleak
295 *) flags="$flags -stdlib=libc++" ;;
296 esac
297
298 # TODO: exactly when is -fPIC needed? Clang needs it sometimes?
299 if test $variant != 'opt'; then
300 flags="$flags -fPIC"
301 fi
302 fi
303
304 # this flag is only valid in Clang, doesn't work in continuous build
305 if test "$compiler" = 'clang'; then
306 flags="$flags -ferror-limit=10"
307 fi
308
309 setglobal_cxx $compiler
310
311 # The command we want to run. Not using arrays because this is POSIX shell
312 set -- "$cxx" $flags -o "$out" -c "$in"
313
314 if test -n "${OILS_CXX_VERBOSE:-}"; then
315 echo '__' "$@" >&2
316 fi
317
318 # Maybe add timing prefix
319 if test -n "${TIME_TSV_OUT:-}"; then
320 set -- \
321 benchmarks/time_.py --tsv \
322 --out "$TIME_TSV_OUT" --append \
323 --rusage --field compile_one --field $out -- \
324 "$@"
325 else
326 # Show timing info on the most expensive translation unit
327 case $in in
328 */oils_for_unix.*)
329 # Must have external 'time', and it must have -f
330 # Notes: OS X has no time -f
331 # busybox time supports -f but not --format.
332 if command time -f '%e %M' true >/dev/null; then
333 set -- \
334 time -f "$out { elapsed: %e, max_RSS: %M }" -- \
335 "$@"
336 fi
337 ;;
338 esac
339 fi
340
341 # Run the command
342 "$@"
343}
344
345link() {
346 ### Link a binary. Invoked by build.ninja
347
348 local compiler=$1
349 local variant=$2
350 local more_link_flags=$3 # Used by Souffle datalog rules
351 local out=$4
352 shift 4
353 # rest are inputs
354
355 setglobal_link_flags "$variant" "$more_link_flags"
356
357 setglobal_cxx "$compiler"
358
359 if test "$compiler" = 'clang'; then
360 case $variant in
361 coverage*) ;; # include coverage+bumpleak
362 *) link_flags="$link_flags -stdlib=libc++"
363 esac
364 fi
365
366 # The command we want to run. Not using arrays because this is POSIX shell
367 #
368 # IMPORTANT: Flags like -ltcmalloc have to come AFTER objects! Weird but
369 # true.
370 set -- "$cxx" -o "$out" "$@" $link_flags
371
372 if test -n "${OILS_CXX_VERBOSE:-}"; then
373 echo '__' "$@" >&2
374 fi
375
376 # Maybe add timing prefix
377 if test -n "${TIME_TSV_OUT:-}"; then
378 set -- \
379 benchmarks/time_.py --tsv \
380 --out "$TIME_TSV_OUT" --append \
381 --rusage --field link --field $out -- \
382 "$@"
383 fi
384
385 # Run the command
386 "$@"
387}
388
389compile_and_link() {
390 ### This function is no longer used; use 'compile_one' and 'link'
391
392 local compiler=$1
393 local variant=$2
394 local more_cxx_flags=$3
395 local out=$4
396 shift 4
397
398 setglobal_compile_flags "$variant" "$more_cxx_flags" "" # no dotd
399
400 setglobal_link_flags $variant
401
402 setglobal_cxx $compiler
403
404 if test -n "${OILS_CXX_VERBOSE:-}"; then
405 echo "__ $cxx -o $out $flags $@ $link_flags" >&2
406 fi
407
408 "$cxx" -o "$out" $flags "$@" $link_flags
409}
410
411strip_() {
412 ### Invoked by ninja
413
414 local in=$1
415 local stripped=$2
416 local symbols=${3:-}
417
418 strip -o $stripped $in
419
420 if test -n "$symbols"; then
421 objcopy --only-keep-debug $in $symbols
422 objcopy --add-gnu-debuglink=$symbols $stripped
423 fi
424}
425
426symlink() {
427 local dir=$1
428 local in=$2
429 local out=$3
430
431 cd $dir
432 ln -s -f -v $in $out
433}
434
435# test/cpp-unit.sh sources this
436if test $(basename $0) = 'ninja-rules-cpp.sh'; then
437 "$@"
438fi