Refactor configure, add install targets

This commit is contained in:
2025-09-02 10:24:05 +03:00
parent a4c5ce98ce
commit 5dc36b222f

505
configure vendored
View File

@@ -1,306 +1,361 @@
#!/bin/sh #!/bin/sh
# Configure script by blankhex # Configure script by blankhex
# Global variables # Default values
source_path=$(dirname "$0") cc=${CC:-gcc}
cc=$CC ld=${LD:-gcc}
ar=$AR ar=${AR:-ar}
staticlib=libbh.a source_path="$(dirname "$0")"
dynamiclib=libbh.so prefix_path=${PREFIX:-/usr/local}
exe="" enable_shared="no"
enable_dynamic="no"
enable_mt="yes" enable_mt="yes"
enable_lfs="no" enable_lfs="no"
enable_tests="yes" enable_tests="yes"
enable_pic="yes"
use_clock_gettime="no" use_clock_gettime="no"
arflags=${ARFLAGS:-cr}
cflags=${CFLAGS}
ldflags=${LDFLAGS}
ldlibs=${LDLIBS}
ldshared=${LDSHARED:--shared}
platform=""
display_help="no"
if [ -z "$cc" ]; then cc="gcc"; fi # Parse arguments
if [ -z "$ld" ]; then ld="gcc"; fi
if [ -z "$ar" ]; then ar="ar"; fi
# Internal functions
assign_option() {
set -- "${1#--}" "$2"
set -- "${2:-${1%%=*}}" "${1#*=}"
eval "$1=\"$2\""
}
assign_toggle() {
set -- "${1#--}" "$2" "$3"
set -- "${2:-${1%%=*}}" "${1#*=}" "$3" "$1"
if test "$4" = "$2"; then set -- "$1" "$3"; fi
eval "$1=$2"
}
add_trailing_slash() {
set -- "$1" "$(printf "%s" "$1" | tail -c 1)"
if [ "$2" = "/" ]; then echo "$1";
else echo "$1/"; fi
}
# Option parsing
for option do for option do
case $option in case $option in
--cc=*) assign_option "$option"; ;; --cc=*) cc="${option#--cc=}" ;;
--ar=*) assign_option "$option"; ;; --ar=*) ar="${option#--ar=}" ;;
--extra-cflags=*) assign_option "$option" extra_cflags; ;; --ld=*) ld="${option#--ld=}" ;;
--extra-ldflags=*) assign_option "$option" extra_ldflags; ;; --arflags=*) arflags="${option#--arflags=}" ;;
--extra-ldlibs=*) assign_option "$option" extra_ldlibs; ;; --cflags=*) cflags="${option#--cflags=}" ;;
--platform=*) assign_option "$option"; ;; --ldflags=*) ldflags="${option#--ldflags=}" ;;
--enable-dynamic|--enable-dynamic=*) assign_toggle "$option" enable_dynamic yes; ;; --ldlibs=*) ldlibs="${option#--ldlibs=}" ;;
--enable-mt|--enable-mt=*) assign_toggle "$option" enable_mt yes; ;; --ldshared=*) ldshared="${option#--ldshared=}" ;;
--enable-lfs|--enable-lfs=*) assign_toggle "$option" enable_lfs yes; ;; --platform=*) platform="${option#--platform=}" ;;
--enable-tests|--enable-tests=*) assign_toggle "$option" enable_tests yes; ;; --prefix=*) prefix_path="${option#--prefix=}" ;;
--use-clock_gettime|--use-clock_gettime=*) assign_toggle "$option" use_clock_gettime yes; ;; --enable-shared|--enable-shared=yes) enable_shared="yes" ;;
--source=*) assign_option "$option" source_path; ;; --enable-shared=no) enable_shared="no" ;;
--help|-h) display_help="yes"; ;; --enable-mt|--enable-mt=yes) enable_mt="yes" ;;
*) echo "configure: WARNING: unrecognized option $option"; ;; --enable-mt=no) enable_mt="no" ;;
--enable-lfs|--enable-lfs=yes) enable_lfs="yes" ;;
--enable-lfs=no) enable_lfs="no" ;;
--enable-tests|--enable-tests=yes) enable_tests="yes" ;;
--enable-tests=no) enable_tests="no" ;;
--use-clock_gettime|--use-clock_gettime=yes) use_clock_gettime="yes" ;;
--use-clock_gettime=no) use_clock_gettime="no" ;;
--source=*) source_path="${option#--source=}" ;;
--enable-pic|--with-pic=yes) enable_pic="yes" ;;
--enable-pic=no) enable_pic="no" ;;
--help|-h) display_help="yes" ;;
*) echo "configure: WARNING unrecognized option $option" ;;
esac esac
done done
# Help message if [ "$display_help" = "yes" ]; then
show_help() { cat << EOF
cat << EOF
Usage: configure [OPTIONS] Usage: configure [OPTIONS]
Options: Options:
--help Print this message --help Print this message
--source= Path to the source code --source= Path to the source code
--cc=CC C compiler --cc=CC C compiler
--ld=LD Linker
--ar=AR Library archiver --ar=AR Library archiver
--extra-cflags= Extra compiler flags --arflags= Archiver flags
--extra-ldflags= Extra linker flags --cflags= Compiler flags
--extra-ldlibs= Extra libraries --ldflags= Linker flags
--ldlibs= Extra libraries
--ldshared= Linker flags for shared libraries
--platform=[Posix|Win32|Dummy] Specify target platform --platform=[Posix|Win32|Dummy] Specify target platform
--enable-dynamic[=yes|no] Make dynamic library --prefix= Install prefix
--enable-shared[=yes|no] Make shared library
--enable-pic[=yes|no] Enable position independent code (PIC)
--enable-mt[=yes|no] Enable multithreading support --enable-mt[=yes|no] Enable multithreading support
--enable-lfs[=yes|no] Enable large file support --enable-lfs[=yes|no] Enable large file support
--enable-tests[=yes|no] Enable unit tests --enable-tests[=yes|no] Enable unit tests
--use-clock_gettime[=yes|no] Use of clock_gettime regardless of the support --use-clock_gettime[=yes|no] Use of clock_gettime regardless of the support
EOF EOF
exit 1 exit 1
}
if [ -n "$display_help" ]; then show_help; fi
# Add trailing slash if needed
if [ -n "$source_path" ]; then
source_path=$(add_trailing_slash "$source_path")
fi fi
# Detect OS and platform, because it wasn't specified # Ensure that source_path ends on /
os="Unknown" case "$source_path" in
if [ -z "$platform" ]; then */) ;;
case $(uname) in *) source_path="$source_path/" ;;
Darwin) os="MacOS"; platform="Posix"; ;;
Linux) os="Linux"; platform="Posix"; ;;
*BSD) os="BSD"; platform="Posix"; ;;
CYGWIN*|MINGW*|MSYS*) os="Windows"; platform="Win32"; ;;
*) os="Unknown"; platform="Dummy"; ;;
esac
fi
# Validate platforms
case $platform in
Posix) ;;
Win32) ;;
Dummy) ;;
*) echo "configure: WARNING: unrecognized platform $platform"; platform=Dummy; ;;
esac esac
# Make build magic # Detect and validate OS
cflags="-fPIC -I${source_path}include -I." os="Unknown"
ldflags="" case "${platform:-$(uname)}" in
ldlibs="-lm" Posix|Win32|Dummy) ;;
Darwin*) os="MacOS"; platform="Posix" ;;
Linux*) os="Linux"; platform="Posix" ;;
*BSD) os="BSD"; platform="Posix" ;;
CYGWIN*|MINGW*|MSYS*) os="Windows"; platform="Win32" ;;
*) platform="Dummy" ;;
esac
# Make sure that build directories exist case "$platform" in
mkdir src src/Platform src/Math src/String 2> /dev/null Posix|Win32|Dummy) ;;
*) echo "configure: WARNING: unsupported platform '$platform'"; platform="Dummy" ;;
esac
# Build source list # Platform-specific settings
sources="${source_path}src/Platform/Spinlock.c" exe_suffix=""
dll_prefix="libbh"
# Platform dependant sources dll_suffix=".so"
if [ "$platform" = "Posix" ]; then if [ "$platform" = "Win32" ]; then
sources="$sources:${source_path}src/Platform/Posix/File.c" exe_suffix=".exe"
mkdir src/Platform/Posix 2> /dev/null dll_prefix="bh"
elif [ "$platform" = "Win32" ]; then dll_suffix=".dll"
exe=".exe"
dynamiclib="bh.dll"
sources="$sources:${source_path}src/Platform/Win32/File.c"
mkdir src/Platform/Win32 2> /dev/null
else
sources="$sources:${source_path}src/Platform/Dummy/File.c"
mkdir src/Platform/Dummy 2> /dev/null
fi fi
if [ "$platform" = "Posix" ] && [ "$enable_mt" = "yes" ]; then # Build flags
sources="$sources:${source_path}src/Platform/Posix/Condition.c" cflags="-I${source_path}include -I. $cflags"
sources="$sources:${source_path}src/Platform/Posix/Mutex.c" ldlibs="-lm $ldlibs"
sources="$sources:${source_path}src/Platform/Posix/Semaphore.c"
sources="$sources:${source_path}src/Platform/Posix/Thread.c" if [ "$enable_pic" = "yes" ]; then
sources="$sources:${source_path}src/Platform/Posix/Tss.c" cflags="$cflags -fPIC"
fi
if [ "$enable_pic" = "no" ] && [ "$enable_shared" = "yes" ]; then
echo "configure: WARNING: requested shared library while PIC is disabled"
echo "configure: WARNING: expect build failure"
fi
if [ "$enable_mt" = "yes" ] && [ "$platform" = "Posix" ]; then
ldlibs="$ldlibs -pthread" ldlibs="$ldlibs -pthread"
mkdir src/Platform/Posix 2> /dev/null
elif [ "$platform" = "Win32" ] && [ "$enable_mt" = "yes" ]; then
sources="$sources:${source_path}src/Platform/Win32/Condition.c"
sources="$sources:${source_path}src/Platform/Win32/Mutex.c"
sources="$sources:${source_path}src/Platform/Win32/Semaphore.c"
sources="$sources:${source_path}src/Platform/Win32/Thread.c"
sources="$sources:${source_path}src/Platform/Win32/Tss.c"
mkdir src/Platform/Win32 2> /dev/null
else
sources="$sources:${source_path}src/Platform/Dummy/Condition.c"
sources="$sources:${source_path}src/Platform/Dummy/Mutex.c"
sources="$sources:${source_path}src/Platform/Dummy/Semaphore.c"
sources="$sources:${source_path}src/Platform/Dummy/Thread.c"
sources="$sources:${source_path}src/Platform/Dummy/Tss.c"
mkdir src/Platform/Dummy 2> /dev/null
fi fi
# Platform independant sources # Create directories
for file in "${source_path}src"/*.c; do mkdir src src/Platform src/Math src/String 2>/dev/null
sources="$sources:$file" mkdir src/Platform/Posix src/Platform/Win32 src/Platform/Dummy 2>/dev/null
done
for file in "${source_path}src/Math"/*.c; do
sources="$sources:$file"
done
for file in "${source_path}src/String"/*.c; do
sources="$sources:$file"
done
# Tests
if [ "$enable_tests" = "yes" ]; then if [ "$enable_tests" = "yes" ]; then
tests="" mkdir test test/src unit unit/src 2>/dev/null
mkdir test test/src 2> /dev/null fi
for file in "${source_path}test/src"/*.c; do
if [ -n "$tests" ]; then tests="$tests:$file" library=""
else tests="$file"; fi add_source() { library="${library}${library:+:}$1"; }
# Main source
for file in "${source_path}"src/*.c; do
add_source "$file"
done
for file in "${source_path}"src/Math/*.c; do
add_source "$file"
done
for file in "${source_path}"src/String/*.c; do
add_source "$file"
done
# Platform specific code
add_source "${source_path}src/Platform/Spinlock.c"
case "$platform" in
Posix) add_source "${source_path}src/Platform/Posix/File.c" ;;
Win32) add_source "${source_path}src/Platform/Win32/File.c" ;;
*) add_source "${source_path}src/Platform/Dummy/File.c" ;;
esac
if [ "$enable_mt" = "yes" ]; then
case "$platform" in
Posix)
for item in Condition Mutex Semaphore Thread Tss; do
add_source "${source_path}src/Platform/Posix/${item}.c"
done
;;
Win32)
for item in Condition Mutex Semaphore Thread Tss; do
add_source "${source_path}src/Platform/Win32/${item}.c"
done
;;
*)
for item in Condition Mutex Semaphore Thread Tss; do
add_source "${source_path}src/Platform/Dummy/${item}.c"
done
;;
esac
else
for item in Condition Mutex Semaphore Thread Tss; do
add_source "${source_path}src/Platform/Dummy/${item}.c"
done done
fi fi
# Add extra flags # Tests
cflags="$cflags $extra_cflags" tests=""
ldflags="$ldflags $extra_ldflags" unit=""
ldlibs="$ldlibs $extra_ldlibs" add_test() { tests="${tests}${tests:+:}$1"; }
# Makefile generation if [ "$enable_tests" = "yes" ]; then
unit="${source_path}unit/src/Unit.c"
for file in "${source_path}"test/src/*.c; do
add_test "$file"
done
fi
# Generate Makefile
{ {
# Generate variables
echo "CC=$cc" echo "CC=$cc"
echo "LD=$ld"
echo "AR=$ar" echo "AR=$ar"
echo "ARFLAGS=$arflags"
echo "CFLAGS=$cflags" echo "CFLAGS=$cflags"
echo "LDFLAGS=$ldflags" echo "LDFLAGS=$ldflags"
echo "LDLIBS=$ldlibs" echo "LDLIBS=$ldlibs"
echo "STATICLIB=$staticlib" echo "LDSHARED=$ldshared"
echo "PREFIX=$prefix_path"
echo "STATICLIB=libbh.a"
if [ "$enable_dynamic" = "yes" ]; then # Shared library
echo "DYNAMICLIB=$dynamiclib" if [ "$enable_shared" = "yes" ]; then
fi printf "SHAREDLIB=${dll_prefix}${dll_suffix}\n"
printf "\nall: static shared\n"
printf "\nshared: \${SHAREDLIB}\n"
# Set a list of objects printf "\n\$(SHAREDLIB): "
printf "\nOBJS=" ( IFS=":"; for item in $library; do item="${item#$source_path}"; printf "%s " "${item%.c}.o"; done; )
( printf "\n\t\$(LD) \$(LDFLAGS) \$(LDSHARED) -o \$@ "
IFS=":" ( IFS=":"; for item in $library; do item="${item#$source_path}"; printf "%s " "${item%.c}.o"; done; )
for source in $sources; do printf "\$(LDLIBS)\n"
object=${source%%.c}.o
object=${object#${source_path}}
echo " \\"
printf " %s" "$object"
done
)
printf "\n\n"
# Check if dynamic linking is enabled and add rules
if [ "$enable_dynamic" = "yes" ]; then
printf "all: static dynamic\n\n"
printf "dynamic: \$(DYNAMICLIB)\n\n"
printf "\$(DYNAMICLIB): \$(OBJS)\n"
printf "\t\$(CC) \$(LDFLAGS) -shared \$(OBJS) -o \$(DYNAMICLIB) \$(LDLIBS)\n\n"
else else
printf "all: static\n\n" printf "\nall: static\n"
fi fi
# Add rules for static linking # Static library
printf "static: \$(STATICLIB)\n\n" printf "\nstatic: \${STATICLIB}\n"
printf "\$(STATICLIB): \$(OBJS)\n" printf "\n\$(STATICLIB): "
printf "\t\$(AR) r \$(STATICLIB) \$(OBJS)\n\n" ( IFS=":"; for item in $library; do item="${item#$source_path}"; printf "%s " "${item%.c}.o"; done; )
printf "\n\t\$(AR) \$(ARFLAGS) \$@ "
( IFS=":"; for item in $library; do item="${item#$source_path}"; printf "%s " "${item%.c}.o"; done; )
printf "\n"
# Generate rules for tests # Compile sources
(
IFS=":";
for item in $library; do
object="${item#$source_path}"
printf "\n%s: %s\n" "${object%.c}.o" "$item"
printf "\t\$(CC) \$(CFLAGS) -c -o \$@ %s\n" "$item"
done
)
# Tests
if [ "$enable_tests" = "yes" ]; then if [ "$enable_tests" = "yes" ]; then
( (
IFS=":" unit_obj="${unit#$source_path}"
for source in $tests; do printf "\n%s: %s\n" "${unit_obj%.c}.o" "$unit"
object=${source%%.c}${exe} printf "\t\$(CC) \$(CFLAGS) -I${source_path}unit/include -c -o \$@ %s\n" "$unit"
object=${object#${source_path}}
printf "%s: %s \$(STATICLIB)\n" $object $source IFS=":";
printf "\t\$(CC) \$(CFLAGS) -I${source_path}unit/include \$(LDFLAGS) %s %s \$(STATICLIB) -o %s \$(LDLIBS)\n\n" "$source" "${source_path}unit/src/Unit.c" "$object" for item in $tests; do
done object="${item#$source_path}"
) printf "\n%s: %s %s \$(STATICLIB)\n" "${object%.c}${exe_suffix}" "${object%.c}.o" "${unit_obj%.c}.o"
printf "tests: " printf "\t\$(LD) \$(LDFLAGS) -o \$@ %s %s \$(STATICLIB) \$(LDLIBS)\n" "${object%.c}.o" "${unit_obj%.c}.o"
(
IFS=":" printf "\n%s: %s\n" "${object%.c}.o" "$item"
for source in $tests; do printf "\t\$(CC) \$(CFLAGS) -I${source_path}unit/include -c -o \$@ %s\n" "$item"
object=${source%%.c}${exe} done
object=${object#${source_path}}
echo " \\" printf "\ntest: "
printf " %s" "$object" for item in $tests; do
done object="${item#$source_path}"
) printf "%s " "${object%.c}${exe_suffix}"
printf "\n\n" done
echo
for item in $tests; do
object="${item#$source_path}"
printf "\t./%s\n" "${object%.c}${exe_suffix}"
done
echo
)
fi fi
# Generate rules for main objects # Clean
printf "\nclean:\n"
printf "\t-rm -f %s\n" "\$(STATICLIB)"
if [ "$enable_shared" = "yes" ]; then
printf "\t-rm -f %s\n" "\$(SHAREDLIB)"
fi
( (
IFS=":" IFS=":"
for source in $sources; do for item in $library; do
object=${source%%.c}.o item="${item#$source_path}";
object=${object#${source_path}} printf "\t-rm -f %s\n" "${item%.c}.o"
printf "$object: $source\n"
printf "\t\$(CC) -c \$(CFLAGS) %s -o %s\n\n" "$source" "$object"
done done
)
printf "\n\n"
# Generate clean rules if [ "$enable_tests" = "yes" ]; then
printf "clean:\n" unit_obj="${unit#$source_path}"
printf "\t-rm -f \$(OBJS) \$(STATICLIB)" printf "\t-rm -f %s\n" "${unit_obj%.c}.o"
if [ "$enable_dynamic" = "yes" ]; then for item in $tests; do
printf " \$(DYNAMICLIB)" item="${item#$source_path}"
printf "\t-rm -f %s\n" "${item%.c}.o"
printf "\t-rm -f %s\n" "${item%.c}${exe_suffix}"
done
fi
)
# Install
printf "\ninstall: \$(STATICLIB) "
if [ "$enable_shared" = "yes" ]; then
printf "\$(SHAREDLIB)"
fi
printf "\n\tinstall -d \$(DESTDIR)\$(PREFIX)/lib/\n"
printf "\tinstall -d \$(DESTDIR)\$(PREFIX)/include/BH/Math\n"
printf "\tinstall -m 644 \$(STATICLIB) \$(DESTDIR)\$(PREFIX)/lib\n"
if [ "$enable_shared" = "yes" ]; then
printf "\tinstall -m 644 \$(SHAREDLIB) \$(DESTDIR)\$(PREFIX)/lib\n"
fi
for file in "${source_path}"include/BH/*.h; do
printf "\tinstall -m 644 \"%s\" \$(DESTDIR)\$(PREFIX)/include/BH\n" "$file"
done
for file in "${source_path}"include/BH/Math/*.h; do
printf "\tinstall -m 644 \"%s\" \$(DESTDIR)\$(PREFIX)/include/BH/Math\n" "$file"
done
# Uninstall
printf "\nuninstall:\n"
printf "\trm -rf \$(DESTDIR)\$(PREFIX)/include/BH\n"
printf "\trm -rf \$(DESTDIR)\$(PREFIX)/lib/\$(STATICLIB)\n"
if [ "$enabled_shared" = "yes" ]; then
printf "\trm -rf \$(DESTDIR)\$(PREFIX)/lib/\$(SHAREDLIB)\n"
fi fi
if [ "$enable_tests" = "yes" ]; then
(
IFS=":"
for source in $tests; do
object=${source%%.c}${exe}
object=${object#${source_path}}
printf " %s" "$object"
done
)
fi
printf "\n\n"
} > Makefile } > Makefile
# Generate internal config.h # Generate Config.h
{ {
printf "#ifndef BH_SRC_CONFIG_H\n#define BH_SRC_CONFIG_H\n\n" printf "#ifndef BH_SRC_CONFIG_H\n"
if [ "$use_clock_gettime" = "yes" ]; then printf "#define BH_USE_CLOCK_GETTIME\n"; fi printf "#define BH_SRC_CONFIG_H\n\n"
if [ "$enable_lfs" = "yes" ]; then printf "#define BH_ENABLE_LFS\n"; fi if [ "$use_clock_gettime" = "yes" ]; then
printf "#define BH_USE_CLOCK_GETTIME\n";
fi
if [ "$enable_lfs" = "yes" ]; then
printf "#define BH_ENABLE_LFS\n";
fi
printf "\n#endif /* BH_SRC_CONFIG_H */\n" printf "\n#endif /* BH_SRC_CONFIG_H */\n"
} > Config.h } > Config.h
echo " --- General information --- "
echo "OS: $os" echo "OS: $os"
echo "Platform: $platform" echo "Platform: $platform"
echo "Source path: $source_path" echo "Source path: $source_path"
echo
echo " --- Makefile variables --- "
echo "CC: $cc" echo "CC: $cc"
echo "LD: $ld"
echo "AR: $ar" echo "AR: $ar"
echo "ARFLAGS: $arflags"
echo "CFLAGS: $cflags" echo "CFLAGS: $cflags"
echo "LDFLAGS: $ldflags" echo "LDFLAGS: $ldflags"
echo "LDLIBS: $ldlibs" echo "LDLIBS: $ldlibs"
echo "LDSHARED: $ldshared"
echo "PREFIX: $prefix_path"
echo
echo " --- Enabled options --- "
echo "Enable multithreading: $enable_mt" echo "Enable multithreading: $enable_mt"
echo "Enable long file support: $enable_lfs" echo "Enable long file support: $enable_lfs"
echo "Enable tests: $enable_tests" echo "Enable tests: $enable_tests"
echo "Build dynamic library: $enable_dynamic" echo "Enable PIC: $enable_pic"
echo "Build shared library: $enable_shared"
echo "Use clock_gettime: $use_clock_gettime" echo "Use clock_gettime: $use_clock_gettime"