blob: c428d8ac083feefaef1c89334fe89261cede6a86 [file] [log] [blame]
Nigel Tao4c1826a2018-08-05 21:55:50 +10001#!/bin/bash -eu
2# Copyright 2018 The Wuffs Authors.
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# https://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# ----------------
17
18# See build-all.sh for commentary.
19
Nigel Taoeb670342021-10-07 14:59:13 +110020if [ ! -e wuffs-root-directory.txt ]; then
Nigel Tao10ae7042018-08-18 12:13:36 +100021 echo "$0 should be run from the Wuffs root directory."
22 exit 1
23fi
24
Nigel Taob2d598a2018-12-26 12:38:15 +110025CC=${CC:-gcc}
26CXX=${CXX:-g++}
Nigel Tao774340a2021-10-04 09:47:07 +110027LDFLAGS=${LDFLAGS:-}
Nigel Taob2d598a2018-12-26 12:38:15 +110028
Nigel Taoea4c85b2021-10-04 23:37:37 +110029# The "-fdata-sections -ffunction-sections -Wl,--gc-sections" produces smaller
30# binaries. See commit 41fce8a8 "Strip examples of unused data and functions".
Nigel Taoc27250f2022-10-28 17:14:32 +110031CFLAGS=${CFLAGS:--O3 -fdata-sections -ffunction-sections -Wall -Wl,--gc-sections}
32CXXFLAGS=${CXXFLAGS:--O3 -fdata-sections -ffunction-sections -Wall -Wl,--gc-sections}
Nigel Taoea4c85b2021-10-04 23:37:37 +110033
Nigel Tao10ae7042018-08-18 12:13:36 +100034mkdir -p gen/bin
35
36sources=$@
37if [ $# -eq 0 ]; then
38 sources=example/*
39fi
40
41for f in $sources; do
42 f=${f%/}
43 f=${f##*/}
44 if [ -z $f ]; then
45 continue
46 fi
47
Nigel Tao1b073492020-02-16 22:11:36 +110048 if [ $f = imageviewer ]; then
Nigel Tao8f1fe432020-01-15 14:22:48 +110049 # example/imageviewer is unusual in that needs additional libraries.
Nigel Tao774340a2021-10-04 09:47:07 +110050 echo "Building (C++) gen/bin/example-$f"
51 $CXX $CXXFLAGS example/$f/*.cc \
52 $LDFLAGS -lxcb -lxcb-image -lxcb-render -lxcb-render-util \
Nigel Tao68acf242021-10-03 13:59:09 +110053 -o gen/bin/example-$f
Nigel Tao10850c12021-04-01 00:14:10 +110054 elif [ $f = "sdl-imageviewer" ]; then
55 # example/sdl-imageviewer is unusual in that needs additional libraries.
Nigel Tao774340a2021-10-04 09:47:07 +110056 echo "Building (C++) gen/bin/example-$f"
57 $CXX $CXXFLAGS example/$f/*.cc \
58 $LDFLAGS -lSDL2 -lSDL2_image \
Nigel Tao68acf242021-10-03 13:59:09 +110059 -o gen/bin/example-$f
Nigel Taocf8884d2020-08-05 11:55:01 +100060 elif [ $f = "toy-genlib" ]; then
61 # example/toy-genlib is unusual in that it uses separately compiled
62 # libraries (built by "wuffs genlib", e.g. by running build-all.sh) instead
63 # of directly #include'ing Wuffs' .c files.
Nigel Taob2d598a2018-12-26 12:38:15 +110064 if [ -e gen/lib/c/$CC-static/libwuffs.a ]; then
Nigel Tao774340a2021-10-04 09:47:07 +110065 echo "Building (C) gen/bin/example-$f"
66 $CC $CFLAGS -static -I.. example/$f/*.c gen/lib/c/$CC-static/libwuffs.a \
67 $LDFLAGS -o gen/bin/example-$f
Nigel Taofce79372018-08-05 22:36:37 +100068 else
Nigel Tao10ae7042018-08-18 12:13:36 +100069 echo "Skipping gen/bin/example-$f; run \"wuffs genlib\" first"
Nigel Taofce79372018-08-05 22:36:37 +100070 fi
Nigel Tao10ae7042018-08-18 12:13:36 +100071 elif [ -e example/$f/*.c ]; then
Nigel Tao774340a2021-10-04 09:47:07 +110072 echo "Building (C) gen/bin/example-$f"
73 $CC $CFLAGS example/$f/*.c $LDFLAGS -o gen/bin/example-$f
Nigel Tao5396dbd2020-08-29 22:02:35 +100074 elif [ $f = "jsonfindptrs" ]; then
Nigel Tao774340a2021-10-04 09:47:07 +110075 echo "Building (C++) gen/bin/example-$f"
76 $CXX $CXXFLAGS -std=c++17 example/$f/*.cc $LDFLAGS -o gen/bin/example-$f
Nigel Tao1b073492020-02-16 22:11:36 +110077 elif [ -e example/$f/*.cc ]; then
Nigel Tao774340a2021-10-04 09:47:07 +110078 echo "Building (C++) gen/bin/example-$f"
79 $CXX $CXXFLAGS example/$f/*.cc $LDFLAGS -o gen/bin/example-$f
Nigel Tao4c1826a2018-08-05 21:55:50 +100080 fi
81done