Sun, 01 Oct 2023 13:56:42 +0200
improve Makefiles
universe@71 | 1 | #!/bin/sh |
universe@71 | 2 | |
universe@71 | 3 | # create temporary directory |
universe@71 | 4 | TEMP_DIR=".tmp-`uname -n`" |
universe@71 | 5 | rm -Rf "$TEMP_DIR" |
universe@71 | 6 | if mkdir -p "$TEMP_DIR"; then |
universe@71 | 7 | : |
universe@71 | 8 | else |
universe@71 | 9 | echo "Cannot create tmp dir $TEMP_DIR" |
universe@71 | 10 | echo "Abort" |
universe@71 | 11 | exit 1 |
universe@71 | 12 | fi |
universe@71 | 13 | touch "$TEMP_DIR/options" |
universe@71 | 14 | touch "$TEMP_DIR/features" |
universe@71 | 15 | |
universe@71 | 16 | # define standard variables |
universe@71 | 17 | PREFIX=/usr |
universe@71 | 18 | EPREFIX= |
universe@71 | 19 | BINDIR= |
universe@71 | 20 | SBINDIR= |
universe@71 | 21 | LIBDIR= |
universe@71 | 22 | LIBEXECDIR= |
universe@71 | 23 | DATADIR= |
universe@71 | 24 | SYSCONFDIR= |
universe@71 | 25 | SHAREDSTATEDIR= |
universe@71 | 26 | LOCALSTATEDIR= |
universe@71 | 27 | INCLUDEDIR= |
universe@71 | 28 | INFODIR= |
universe@71 | 29 | MANDIR= |
universe@71 | 30 | |
universe@71 | 31 | # custom variables |
universe@71 | 32 | BUILDDIR=`pwd`/build |
universe@71 | 33 | |
universe@71 | 34 | # features |
universe@71 | 35 | |
universe@71 | 36 | # clean abort |
universe@71 | 37 | abort_configure() |
universe@71 | 38 | { |
universe@71 | 39 | rm -Rf "$TEMP_DIR" |
universe@71 | 40 | exit 1 |
universe@71 | 41 | } |
universe@71 | 42 | |
universe@71 | 43 | # help text |
universe@71 | 44 | printhelp() |
universe@71 | 45 | { |
universe@71 | 46 | echo "Usage: $0 [OPTIONS]..." |
universe@71 | 47 | cat << __EOF__ |
universe@71 | 48 | Installation directories: |
universe@71 | 49 | --prefix=PREFIX path prefix for architecture-independent files |
universe@71 | 50 | [/usr] |
universe@71 | 51 | --exec-prefix=EPREFIX path prefix for architecture-dependent files |
universe@71 | 52 | [PREFIX] |
universe@71 | 53 | |
universe@71 | 54 | --bindir=DIR user executables [EPREFIX/bin] |
universe@71 | 55 | --sbindir=DIR system admin executables [EPREFIX/sbin] |
universe@71 | 56 | --libexecdir=DIR program executables [EPREFIX/libexec] |
universe@71 | 57 | --sysconfdir=DIR system configuration files [PREFIX/etc] |
universe@71 | 58 | --sharedstatedir=DIR modifiable architecture-independent data [PREFIX/com] |
universe@71 | 59 | --localstatedir=DIR modifiable single-machine data [PREFIX/var] |
universe@71 | 60 | --libdir=DIR object code libraries [EPREFIX/lib] |
universe@71 | 61 | --includedir=DIR C header files [PREFIX/include] |
universe@71 | 62 | --datarootdir=DIR read-only arch.-independent data root [PREFIX/share] |
universe@71 | 63 | --datadir=DIR read-only architecture-independent data [DATAROOTDIR] |
universe@71 | 64 | --infodir=DIR info documentation [DATAROOTDIR/info] |
universe@71 | 65 | --mandir=DIR man documentation [DATAROOTDIR/man] |
universe@71 | 66 | |
universe@71 | 67 | __EOF__ |
universe@71 | 68 | } |
universe@71 | 69 | |
universe@71 | 70 | # |
universe@71 | 71 | # parse arguments |
universe@71 | 72 | # |
universe@71 | 73 | BUILD_TYPE="default" |
universe@71 | 74 | for ARG in "$@" |
universe@71 | 75 | do |
universe@71 | 76 | case "$ARG" in |
universe@71 | 77 | "--prefix="*) PREFIX=${ARG#--prefix=} ;; |
universe@71 | 78 | "--exec-prefix="*) EPREFIX=${ARG#--exec-prefix=} ;; |
universe@71 | 79 | "--bindir="*) BINDIR=${ARG#----bindir=} ;; |
universe@71 | 80 | "--sbindir="*) SBINDIR=${ARG#--sbindir=} ;; |
universe@71 | 81 | "--libdir="*) LIBDIR=${ARG#--libdir=} ;; |
universe@71 | 82 | "--libexecdir="*) LIBEXECDIR=${ARG#--libexecdir=} ;; |
universe@71 | 83 | "--datadir="*) DATADIR=${ARG#--datadir=} ;; |
universe@71 | 84 | "--sysconfdir="*) SYSCONFDIR=${ARG#--sysconfdir=} ;; |
universe@71 | 85 | "--sharedstatedir="*) SHAREDSTATEDIR=${ARG#--sharedstatedir=} ;; |
universe@71 | 86 | "--localstatedir="*) LOCALSTATEDIR=${ARG#--localstatedir=} ;; |
universe@71 | 87 | "--includedir="*) INCLUDEDIR=${ARG#--includedir=} ;; |
universe@71 | 88 | "--infodir="*) INFODIR=${ARG#--infodir=} ;; |
universe@71 | 89 | "--mandir"*) MANDIR=${ARG#--mandir} ;; |
universe@71 | 90 | "--help"*) printhelp; abort_configure ;; |
universe@71 | 91 | "--debug") BUILD_TYPE="debug" ;; |
universe@71 | 92 | "--release") BUILD_TYPE="release" ;; |
universe@71 | 93 | "-"*) echo "unknown option: $ARG"; abort_configure ;; |
universe@71 | 94 | esac |
universe@71 | 95 | done |
universe@71 | 96 | |
universe@71 | 97 | |
universe@71 | 98 | # set dir variables |
universe@71 | 99 | : ${EPREFIX:="$PREFIX"} |
universe@71 | 100 | : ${BINDIR:="$EPREFIX/bin"} |
universe@71 | 101 | : ${SBINDIR:="$EPREFIX/sbin"} |
universe@71 | 102 | : ${LIBDIR:="$EPREFIX/lib"} |
universe@71 | 103 | : ${LIBEXECDIR:="$EPREFIX/libexec"} |
universe@71 | 104 | : ${DATADIR:="$PREFIX/share"} |
universe@71 | 105 | : ${SYSCONFDIR:="$PREFIX/etc"} |
universe@71 | 106 | : ${SHAREDSTATEDIR:="$PREFIX/com"} |
universe@71 | 107 | : ${LOCALSTATEDIR:="$PREFIX/var"} |
universe@71 | 108 | : ${INCLUDEDIR:="$PREFIX/include"} |
universe@71 | 109 | : ${INFODIR:="$PREFIX/info"} |
universe@71 | 110 | : ${MANDIR:="$PREFIX/man"} |
universe@71 | 111 | |
universe@71 | 112 | # Test for availability of pkg-config |
universe@71 | 113 | PKG_CONFIG=`command -v pkg-config` |
universe@71 | 114 | : ${PKG_CONFIG:="false"} |
universe@71 | 115 | |
universe@71 | 116 | # Simple uname based platform detection |
universe@71 | 117 | # $PLATFORM is used for platform dependent dependency selection |
universe@71 | 118 | OS=`uname -s` |
universe@71 | 119 | OS_VERSION=`uname -r` |
universe@71 | 120 | printf "detect platform... " |
universe@71 | 121 | if [ "$OS" = "SunOS" ]; then |
universe@71 | 122 | PLATFORM="solaris sunos unix svr4" |
universe@71 | 123 | fi |
universe@71 | 124 | if [ "$OS" = "Linux" ]; then |
universe@71 | 125 | PLATFORM="linux unix" |
universe@71 | 126 | fi |
universe@71 | 127 | if [ "$OS" = "FreeBSD" ]; then |
universe@71 | 128 | PLATFORM="freebsd bsd unix" |
universe@71 | 129 | fi |
universe@71 | 130 | if [ "$OS" = "Darwin" ]; then |
universe@71 | 131 | PLATFORM="macos osx bsd unix" |
universe@71 | 132 | fi |
universe@71 | 133 | if echo "$OS" | grep -i "MINGW" > /dev/null; then |
universe@71 | 134 | PLATFORM="windows mingw" |
universe@71 | 135 | fi |
universe@71 | 136 | : ${PLATFORM:="unix"} |
universe@71 | 137 | |
universe@71 | 138 | PLATFORM_NAME=`echo "$PLATFORM" | cut -f1 -d' ' -` |
universe@71 | 139 | echo "$PLATFORM_NAME" |
universe@71 | 140 | |
universe@71 | 141 | isplatform() |
universe@71 | 142 | { |
universe@71 | 143 | for p in $PLATFORM |
universe@71 | 144 | do |
universe@71 | 145 | if [ "$p" = "$1" ]; then |
universe@71 | 146 | return 0 |
universe@71 | 147 | fi |
universe@71 | 148 | done |
universe@71 | 149 | return 1 |
universe@71 | 150 | } |
universe@71 | 151 | notisplatform() |
universe@71 | 152 | { |
universe@71 | 153 | for p in $PLATFORM |
universe@71 | 154 | do |
universe@71 | 155 | if [ "$p" = "$1" ]; then |
universe@71 | 156 | return 1 |
universe@71 | 157 | fi |
universe@71 | 158 | done |
universe@71 | 159 | return 0 |
universe@71 | 160 | } |
universe@71 | 161 | |
universe@71 | 162 | |
universe@71 | 163 | # generate vars.mk |
universe@71 | 164 | cat > "$TEMP_DIR/vars.mk" << __EOF__ |
universe@71 | 165 | PREFIX=$PREFIX |
universe@71 | 166 | EPREFIX=$EPREFIX |
universe@71 | 167 | BINDIR=$BINDIR |
universe@71 | 168 | SBINDIR=$SBINDIR |
universe@71 | 169 | LIBDIR=$LIBDIR |
universe@71 | 170 | LIBEXECDIR=$LIBEXECDIR |
universe@71 | 171 | DATADIR=$DATADIR |
universe@71 | 172 | SYSCONFDIR=$SYSCONFDIR |
universe@71 | 173 | SHAREDSTATEDIR=$SHAREDSTATEDIR |
universe@71 | 174 | LOCALSTATEDIR=$LOCALSTATEDIR |
universe@71 | 175 | INCLUDEDIR=$INCLUDEDIR |
universe@71 | 176 | INFODIR=$INFODIR |
universe@71 | 177 | MANDIR=$MANDIR |
universe@71 | 178 | BUILDDIR=$BUILDDIR |
universe@71 | 179 | __EOF__ |
universe@71 | 180 | sort -u -o "$TEMP_DIR/vars.mk" "$TEMP_DIR/vars.mk" |
universe@71 | 181 | |
universe@71 | 182 | |
universe@71 | 183 | # toolchain detection utilities |
universe@71 | 184 | . make/toolchain.sh |
universe@71 | 185 | |
universe@71 | 186 | # |
universe@71 | 187 | # DEPENDENCIES |
universe@71 | 188 | # |
universe@71 | 189 | |
universe@71 | 190 | # check languages |
universe@71 | 191 | lang_c= |
universe@71 | 192 | lang_cpp= |
universe@71 | 193 | if detect_c_compiler ; then |
universe@71 | 194 | lang_c=1 |
universe@71 | 195 | fi |
universe@71 | 196 | |
universe@71 | 197 | # create buffer for make variables required by dependencies |
universe@71 | 198 | echo > "$TEMP_DIR/make.mk" |
universe@71 | 199 | |
universe@71 | 200 | test_pkg_config() |
universe@71 | 201 | { |
universe@71 | 202 | if "$PKG_CONFIG" --exists "$1" ; then : |
universe@71 | 203 | else return 1 ; fi |
universe@71 | 204 | if [ -z "$2" ] || "$PKG_CONFIG" --atleast-version="$2" "$1" ; then : |
universe@71 | 205 | else return 1 ; fi |
universe@71 | 206 | if [ -z "$3" ] || "$PKG_CONFIG" --exact-version="$3" "$1" ; then : |
universe@71 | 207 | else return 1 ; fi |
universe@71 | 208 | if [ -z "$4" ] || "$PKG_CONFIG" --max-version="$4" "$1" ; then : |
universe@71 | 209 | else return 1 ; fi |
universe@71 | 210 | return 0 |
universe@71 | 211 | } |
universe@71 | 212 | |
universe@71 | 213 | dependency_error_archiver() |
universe@71 | 214 | { |
universe@71 | 215 | printf "checking for archiver... " |
universe@71 | 216 | # dependency archiver |
universe@71 | 217 | while true |
universe@71 | 218 | do |
universe@71 | 219 | if command -v ar > /dev/null ; then |
universe@71 | 220 | : |
universe@71 | 221 | else |
universe@71 | 222 | break |
universe@71 | 223 | fi |
universe@71 | 224 | cat >> $TEMP_DIR/make.mk << __EOF__ |
universe@71 | 225 | # Dependency: archiver |
universe@71 | 226 | AR = ar |
universe@71 | 227 | ARFLAGS = -r |
universe@71 | 228 | |
universe@71 | 229 | __EOF__ |
universe@71 | 230 | echo yes |
universe@71 | 231 | return 1 |
universe@71 | 232 | done |
universe@71 | 233 | |
universe@71 | 234 | echo no |
universe@71 | 235 | return 0 |
universe@71 | 236 | } |
universe@71 | 237 | dependency_error_rmdir() |
universe@71 | 238 | { |
universe@71 | 239 | printf "checking for rmdir... " |
universe@71 | 240 | # dependency rmdir |
universe@71 | 241 | while true |
universe@71 | 242 | do |
universe@71 | 243 | if command -v rm > /dev/null ; then |
universe@71 | 244 | : |
universe@71 | 245 | else |
universe@71 | 246 | break |
universe@71 | 247 | fi |
universe@71 | 248 | cat >> $TEMP_DIR/make.mk << __EOF__ |
universe@71 | 249 | # Dependency: rmdir |
universe@71 | 250 | RM = rm |
universe@71 | 251 | RMFLAGS = -f -R |
universe@71 | 252 | |
universe@71 | 253 | __EOF__ |
universe@71 | 254 | echo yes |
universe@71 | 255 | return 1 |
universe@71 | 256 | done |
universe@71 | 257 | |
universe@71 | 258 | echo no |
universe@71 | 259 | return 0 |
universe@71 | 260 | } |
universe@71 | 261 | dependency_error_ncurses() |
universe@71 | 262 | { |
universe@71 | 263 | printf "checking for ncurses... " |
universe@71 | 264 | # dependency ncurses |
universe@71 | 265 | while true |
universe@71 | 266 | do |
universe@71 | 267 | if [ -z "$PKG_CONFIG" ]; then |
universe@71 | 268 | break |
universe@71 | 269 | fi |
universe@71 | 270 | if test_pkg_config "ncursesw" "" "" "" ; then |
universe@71 | 271 | TEMP_CFLAGS="$TEMP_CFLAGS `"$PKG_CONFIG" --cflags ncursesw`" |
universe@71 | 272 | TEMP_LDFLAGS="$TEMP_LDFLAGS `"$PKG_CONFIG" --libs ncursesw`" |
universe@71 | 273 | else |
universe@71 | 274 | break |
universe@71 | 275 | fi |
universe@71 | 276 | echo yes |
universe@71 | 277 | return 1 |
universe@71 | 278 | done |
universe@71 | 279 | |
universe@71 | 280 | echo no |
universe@71 | 281 | return 0 |
universe@71 | 282 | } |
universe@71 | 283 | dependency_error_mkdir() |
universe@71 | 284 | { |
universe@71 | 285 | printf "checking for mkdir... " |
universe@71 | 286 | # dependency mkdir |
universe@71 | 287 | while true |
universe@71 | 288 | do |
universe@71 | 289 | if command -v mkdir > /dev/null ; then |
universe@71 | 290 | : |
universe@71 | 291 | else |
universe@71 | 292 | break |
universe@71 | 293 | fi |
universe@71 | 294 | cat >> $TEMP_DIR/make.mk << __EOF__ |
universe@71 | 295 | # Dependency: mkdir |
universe@71 | 296 | MKDIR = mkdir |
universe@71 | 297 | MKDIRFLAGS = -p |
universe@71 | 298 | |
universe@71 | 299 | __EOF__ |
universe@71 | 300 | echo yes |
universe@71 | 301 | return 1 |
universe@71 | 302 | done |
universe@71 | 303 | |
universe@71 | 304 | echo no |
universe@71 | 305 | return 0 |
universe@71 | 306 | } |
universe@71 | 307 | |
universe@71 | 308 | |
universe@71 | 309 | |
universe@71 | 310 | |
universe@71 | 311 | # start collecting dependency information |
universe@71 | 312 | echo > "$TEMP_DIR/flags.mk" |
universe@71 | 313 | |
universe@71 | 314 | DEPENDENCIES_FAILED= |
universe@71 | 315 | ERROR=0 |
universe@71 | 316 | # unnamed dependencies |
universe@71 | 317 | TEMP_CFLAGS= |
universe@71 | 318 | TEMP_CXXFLAGS= |
universe@71 | 319 | TEMP_LDFLAGS= |
universe@71 | 320 | while true |
universe@71 | 321 | do |
universe@71 | 322 | while true |
universe@71 | 323 | do |
universe@71 | 324 | if [ -z "$lang_c" ] ; then |
universe@71 | 325 | ERROR=1 |
universe@71 | 326 | break |
universe@71 | 327 | fi |
universe@71 | 328 | |
universe@71 | 329 | cat >> "$TEMP_DIR/make.mk" << __EOF__ |
universe@71 | 330 | LIB_EXT = .a |
universe@71 | 331 | |
universe@71 | 332 | __EOF__ |
universe@71 | 333 | break |
universe@71 | 334 | done |
universe@71 | 335 | break |
universe@71 | 336 | done |
universe@71 | 337 | |
universe@71 | 338 | # add general dependency flags to flags.mk |
universe@71 | 339 | echo "# general flags" >> "$TEMP_DIR/flags.mk" |
universe@71 | 340 | if [ -n "${TEMP_CFLAGS}" ]; then |
universe@71 | 341 | echo "CFLAGS += $TEMP_CFLAGS" >> "$TEMP_DIR/flags.mk" |
universe@71 | 342 | fi |
universe@71 | 343 | if [ -n "${TEMP_CXXFLAGS}" ]; then |
universe@71 | 344 | echo "CXXFLAGS += $TEMP_CXXFLAGS" >> "$TEMP_DIR/flags.mk" |
universe@71 | 345 | fi |
universe@71 | 346 | if [ -n "${TEMP_LDFLAGS}" ]; then |
universe@71 | 347 | echo "LDFLAGS += $TEMP_LDFLAGS" >> "$TEMP_DIR/flags.mk" |
universe@71 | 348 | fi |
universe@71 | 349 | |
universe@71 | 350 | # |
universe@71 | 351 | # OPTION VALUES |
universe@71 | 352 | # |
universe@71 | 353 | |
universe@71 | 354 | # |
universe@71 | 355 | # TARGETS |
universe@71 | 356 | # |
universe@71 | 357 | |
universe@71 | 358 | echo >> "$TEMP_DIR/flags.mk" |
universe@71 | 359 | # Target: default |
universe@71 | 360 | echo "# flags for target default" >> "$TEMP_DIR/flags.mk" |
universe@71 | 361 | TEMP_CFLAGS= |
universe@71 | 362 | TEMP_CXXFLAGS= |
universe@71 | 363 | TEMP_LDFLAGS= |
universe@71 | 364 | |
universe@71 | 365 | if dependency_error_archiver; then |
universe@71 | 366 | DEPENDENCIES_FAILED="$DEPENDENCIES_FAILED archiver " |
universe@71 | 367 | ERROR=1 |
universe@71 | 368 | fi |
universe@71 | 369 | if dependency_error_rmdir; then |
universe@71 | 370 | DEPENDENCIES_FAILED="$DEPENDENCIES_FAILED rmdir " |
universe@71 | 371 | ERROR=1 |
universe@71 | 372 | fi |
universe@71 | 373 | if dependency_error_ncurses; then |
universe@71 | 374 | DEPENDENCIES_FAILED="$DEPENDENCIES_FAILED ncurses " |
universe@71 | 375 | ERROR=1 |
universe@71 | 376 | fi |
universe@71 | 377 | if dependency_error_mkdir; then |
universe@71 | 378 | DEPENDENCIES_FAILED="$DEPENDENCIES_FAILED mkdir " |
universe@71 | 379 | ERROR=1 |
universe@71 | 380 | fi |
universe@71 | 381 | |
universe@71 | 382 | # Features |
universe@71 | 383 | |
universe@71 | 384 | |
universe@71 | 385 | if [ -n "${TEMP_CFLAGS}" ]; then |
universe@71 | 386 | echo "CFLAGS += $TEMP_CFLAGS" >> "$TEMP_DIR/flags.mk" |
universe@71 | 387 | fi |
universe@71 | 388 | if [ -n "${TEMP_CXXFLAGS}" ]; then |
universe@71 | 389 | echo "CXXFLAGS += $TEMP_CXXFLAGS" >> "$TEMP_DIR/flags.mk" |
universe@71 | 390 | fi |
universe@71 | 391 | if [ "$BUILD_TYPE" = "debug" ]; then |
universe@71 | 392 | echo 'CFLAGS += ${DEBUG_CC_FLAGS}' >> "$TEMP_DIR/flags.mk" |
universe@71 | 393 | echo 'CXXFLAGS += ${DEBUG_CXX_FLAGS}' >> "$TEMP_DIR/flags.mk" |
universe@71 | 394 | fi |
universe@71 | 395 | if [ "$BUILD_TYPE" = "release" ]; then |
universe@71 | 396 | echo 'CFLAGS += ${RELEASE_CC_FLAGS}' >> "$TEMP_DIR/flags.mk" |
universe@71 | 397 | echo 'CXXFLAGS += ${RELEASE_CXX_FLAGS}' >> "$TEMP_DIR/flags.mk" |
universe@71 | 398 | fi |
universe@71 | 399 | if [ -n "${TEMP_LDFLAGS}" ]; then |
universe@71 | 400 | echo "LDFLAGS += $TEMP_LDFLAGS" >> "$TEMP_DIR/flags.mk" |
universe@71 | 401 | fi |
universe@71 | 402 | |
universe@71 | 403 | |
universe@71 | 404 | # final result |
universe@71 | 405 | if [ $ERROR -ne 0 ]; then |
universe@71 | 406 | echo |
universe@71 | 407 | echo "Error: Unresolved dependencies" |
universe@71 | 408 | echo "$DEPENDENCIES_FAILED" |
universe@71 | 409 | abort_configure |
universe@71 | 410 | fi |
universe@71 | 411 | |
universe@71 | 412 | echo "configure finished" |
universe@71 | 413 | echo |
universe@71 | 414 | echo "Build Config:" |
universe@71 | 415 | echo " PREFIX: $PREFIX" |
universe@71 | 416 | echo " TOOLCHAIN: $TOOLCHAIN_NAME" |
universe@71 | 417 | echo |
universe@71 | 418 | |
universe@71 | 419 | # generate the config.mk file |
universe@71 | 420 | cat > "$TEMP_DIR/config.mk" << __EOF__ |
universe@71 | 421 | # |
universe@71 | 422 | # config.mk generated by configure |
universe@71 | 423 | # |
universe@71 | 424 | |
universe@71 | 425 | __EOF__ |
universe@71 | 426 | write_toolchain_defaults "$TEMP_DIR/toolchain.mk" |
universe@71 | 427 | cat "$TEMP_DIR/vars.mk" "$TEMP_DIR/toolchain.mk" "$TEMP_DIR/flags.mk" "$TEMP_DIR/make.mk" > config.mk |
universe@71 | 428 | rm -Rf "$TEMP_DIR" |
universe@71 | 429 | |
universe@71 | 430 |