build.sh 7.24 KB
Newer Older
1 2
#!/bin/sh

3
set -e
4

5 6
DT_SRC_DIR=$(dirname "$0")
DT_SRC_DIR=$(cd "$DT_SRC_DIR" && pwd -P)
7

8 9 10 11
# ---------------------------------------------------------------------------
# Set default values to option vars
# ---------------------------------------------------------------------------

12 13 14 15 16 17 18 19
INSTALL_PREFIX_DEFAULT="/opt/darktable"
INSTALL_PREFIX="$INSTALL_PREFIX_DEFAULT"
BUILD_TYPE_DEFAULT="RelWithDebInfo"
BUILD_TYPE="$BUILD_TYPE_DEFAULT"
BUILD_DIR_DEFAULT="$DT_SRC_DIR/build"
BUILD_DIR="$BUILD_DIR_DEFAULT"
BUILD_GENERATOR_DFEAULT="Unix Makefiles"
BUILD_GENERATOR="$BUILD_GENERATOR_DFEAULT"
20
MAKE_TASKS=-1
21 22 23 24 25
ADDRESS_SANITIZER=0
DO_CONFIG=1
DO_BUILD=1
DO_INSTALL=0
SUDO=""
26 27 28

PRINT_HELP=0

29 30 31 32 33
FEATURES="FLICKR LIBSECRET KWALLET OPENMP OPENCL UNITY TETHERING GEO LUA OPENEXR WEBP"

# prepare a lowercase version with a space before and after
# it's very important for parse_feature, has no impact in for loop expansions
FEATURES_=$(for i in $FEATURES ; do printf " $(printf $i|tr A-Z a-z) "; done)
34 35

# ---------------------------------------------------------------------------
36
# Parsing functions
37 38 39 40
# ---------------------------------------------------------------------------

parse_feature()
{
41 42
	local feature="$1"
	local value="$2"
43

44 45 46 47 48
	if printf "$FEATURES_" | grep -q " $feature " ; then
		eval "FEAT_$(printf $feature|tr a-z A-Z)"=$value
	else
		printf "warning: unknown feature '$feature'\n"
	fi
49 50
}

51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
parse_args()
{
	while [ "$#" -ge 1 ] ; do
		option="$1"
		case $option in
		--prefix)
			INSTALL_PREFIX="$2"
			shift
			;;
		--build-type|--buildtype)
			BUILD_TYPE="$2"
			shift
			;;
		--build-dir)
			BUILD_DIR="$2"
			shift
			;;
		--build-generator)
			BUILD_GENERATOR="$2"
			shift
			;;
		-j|--jobs)
73
			MAKE_TASKS=$(printf "%d" "$2" >/dev/null 2>&1 && printf "$2" || printf "$MAKE_TASKS")
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
			shift
			;;
		--enable-*)
			feature=${option#--enable-}
			parse_feature "$feature" 1
			;;
		--disable-*)
			feature=${option#--disable-}
			parse_feature "$feature" 0
			;;
		--asan)
			ADDRESS_SANITIZER=1
			;;
		--skip-config)
			DO_CONFIG=0
			;;
		--skip-build)
			DO_BUILD=0
			;;
		--install)
			DO_INSTALL=1
			;;
		--sudo)
			SUDO="sudo "
			;;
		-h|--help)
			PRINT_HELP=1
			;;
		*)
			echo "warning: ignoring unknown option $option"
			;;
		esac
106
		shift
107 108
	done
}
109 110

# ---------------------------------------------------------------------------
111
# Help
112 113
# ---------------------------------------------------------------------------

114 115 116 117
print_help()
{
	cat <<EOF
$(basename $0) [OPTIONS]
118 119 120

Options:
Installation:
121 122 123
   --prefix         <string>  Install directory prefix
                              (default: $INSTALL_PREFIX_DEFAULT)
   --sudo                     Use sudo when doing the install
124 125

Build:
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
   --build-dir      <string>  Building directory
                              (default: $BUILD_DIR_DEFAULT)
   --build-type     <string>  Build type (Release, Debug, RelWithDebInfo)
                              (default: $BUILD_TYPE_DEFAULT)
   --build-generator <string> Build tool
                              (default: Unix Makefiles)

-j --jobs <integer>           Number of tasks
                              (default: number of CPUs)

   --asan                     Enable address sanitizer options
                              (default: disabled)

Actual actions:
   --skip-build               Configure but exit before building the binaries
                              (default: disabled)
   --install                  After building the binaries, install them
                              (default: disabled)
144 145

Features:
146 147 148 149
By default cmake will enable the features it autodetects on the build machine.
Specifying the option on the command line forces the feature on or off.
All these options have a --disable-* equivalent.
$(for i in $FEATURES_ ; do printf "    --enable-$i\n"; done)
150 151 152 153 154

Extra:
-h --help                Print help message
EOF

155
}
156

157 158 159
# ---------------------------------------------------------------------------
# utility functions
# ---------------------------------------------------------------------------
160

161 162 163 164 165 166 167 168 169
num_cpu()
{
	local ncpu
	local platform=$(uname -s)

	case "$platform" in
	SunOS)
		ncpu=$(/usr/sbin/psrinfo |wc -l)
		;;
170
	Linux|MINGW64*)
171
		if [ -r /proc/cpuinfo ]; then
172
			ncpu=$(grep -c "^processor" /proc/cpuinfo)
173
		elif [ -x /sbin/sysctl ]; then
174 175 176
			ncpu=$(/sbin/sysctl -n hw.ncpu 2>/dev/null)
			if [ $? -neq 0 ]; then
				ncpu=-1
177
			fi
178
		fi
179
		;;
180 181 182
	Darwin)
		ncpu=$(/usr/sbin/sysctl -n machdep.cpu.core_count 2>/dev/null)
		;;
183 184 185 186 187 188 189 190
	*)
		printf "warning: unable to determine number of CPUs on $platform\n"
		ncpu=-1
		;;
	esac

	if [ $ncpu -lt 1 ] ; then
		ncpu=1
191
	fi
192 193
	printf "$ncpu"
}
194

195 196 197 198 199 200 201 202 203 204 205 206 207 208
make_name()
{
	local make="make"
	local platform=$(uname -s)

	case "$platform" in
	SunOS)
		PATH="/usr/gnu/bin:$PATH"
		export PATH
		make="gmake"
		;;
	esac
	printf "$make"
}
209

210 211 212 213 214 215
features_set_to_autodetect()
{
	for i in $FEATURES; do
		eval FEAT_$i=-1
	done
}
216

217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
cmake_boolean_option()
{
	name=$1
	value=$2
	case $value in
	-1)
		# Do nothing
		;;
	0)
		CMAKE_MORE_OPTIONS="$CMAKE_MORE_OPTIONS -D${name}=Off"
		;;
	1)
		CMAKE_MORE_OPTIONS="$CMAKE_MORE_OPTIONS -D${name}=On"
		;;
	esac
}

234 235 236 237
# ---------------------------------------------------------------------------
# Let's process the user's wishes
# ---------------------------------------------------------------------------

238 239 240
MAKE_TASKS=$(num_cpu)
MAKE=$(make_name)

241 242 243 244 245 246 247 248
features_set_to_autodetect
parse_args "$@"

if [ $PRINT_HELP -ne 0 ] ; then
	print_help
	exit 1
fi

249
CMAKE_MORE_OPTIONS=""
250 251 252
for i in $FEATURES; do
	eval cmake_boolean_option USE_$i \$FEAT_$i
done
253

254 255 256 257
# Some people might need this, but ignore if unset in environment
CMAKE_PREFIX_PATH=${CMAKE_PREFIX_PATH:-}
CMAKE_MORE_OPTIONS="${CMAKE_MORE_OPTIONS} ${CMAKE_PREFIX_PATH}"

258 259 260 261
# ---------------------------------------------------------------------------
# Let's go
# ---------------------------------------------------------------------------

262 263
mkdir -p "$BUILD_DIR"

264 265 266 267 268 269
cat <<EOF
Darktable build script

Building directory:  $BUILD_DIR
Installation prefix: $INSTALL_PREFIX
Build type:          $BUILD_TYPE
270 271
Build generator:     $BUILD_GENERATOR
Build tasks:         $MAKE_TASKS
272 273 274 275


EOF

276 277 278 279
if [ $ADDRESS_SANITIZER -ne 0 ] ; then
	ASAN_FLAGS="CFLAGS=\"-fsanitize=address -fno-omit-frame-pointer\""
	ASAN_FLAGS="$ASAN_FLAGS CXXFLAGS=\"-fsanitize=address -fno-omit-frame-pointer\""
	ASAN_FLAGS="$ASAN_FLAGS LDFLAGS=\"-fsanitize=address\" "
280 281
fi

282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300

cmd_config="${ASAN_FLAGS}cmake -G \"$BUILD_GENERATOR\" -DCMAKE_INSTALL_PREFIX=${INSTALL_PREFIX} -DCMAKE_BUILD_TYPE=${BUILD_TYPE} ${CMAKE_MORE_OPTIONS} \"$DT_SRC_DIR\""
cmd_build="cmake --build "$BUILD_DIR" -- -j$MAKE_TASKS"
cmd_install="${SUDO}cmake --build \"$BUILD_DIR\" --target install -- -j$MAKE_TASKS"


OLDPWD="$(pwd)"

if [ $DO_CONFIG -eq 0 ] ; then
	cat <<EOF
The script would have configured, built, and installed with these commands:
\$ $(printf "$cmd_config")
\$ $(printf "$cmd_build")
\$ $(printf "$cmd_install")
EOF
	exit 0
fi

# configure the build
301
cd "$BUILD_DIR"
302 303
eval "$cmd_config"
cd "$OLDPWD"
304

305

306
if [ $DO_BUILD -eq 0 ] ; then
307
	cat <<EOF
308 309 310 311
The darktable configuration is finished.
To build and install darktable you need to type:
\$ $(printf "$cmd_build")
\$ $(printf "$cmd_install")
312
EOF
313
	exit 0
314
fi
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330

# build the binaries
eval "$cmd_build"

if [ $DO_INSTALL -eq 0 ] ; then
	cat <<EOF
darktable finished building.
To actually install darktable you need to type:
\$ $(printf "$cmd_install")
EOF
	exit 0
fi

# install the binaries
eval "$cmd_install"