This repository has been archived on 2026-04-17. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
bhlib/configure

376 lines
12 KiB
Plaintext
Raw Normal View History

2025-07-29 21:04:07 +03:00
#!/bin/sh
# Configure script by blankhex
# Default values
cc=${CC:-gcc}
ld=${LD:-gcc}
ar=${AR:-ar}
source_path="$(dirname "$0")"
prefix_path=${PREFIX:-/usr/local}
enable_shared="no"
enable_mt="yes"
2025-07-29 21:04:07 +03:00
enable_lfs="no"
enable_tests="yes"
enable_pic="yes"
2025-07-29 21:04:07 +03:00
use_clock_gettime="no"
arflags=${ARFLAGS:-cr}
cflags=${CFLAGS}
ldflags=${LDFLAGS}
ldlibs=${LDLIBS}
ldshared=${LDSHARED:--shared}
cflagsmt=${CFLAGSMT:--pthread}
ldflagsmt=${LDFLAGSMT:--pthread}
ldlibsmt=${LDLIBSMT}
platform=""
display_help="no"
# Parse arguments
2025-07-29 21:04:07 +03:00
for option do
case $option in
--cc=*) cc="${option#--cc=}" ;;
--ar=*) ar="${option#--ar=}" ;;
--ld=*) ld="${option#--ld=}" ;;
--arflags=*) arflags="${option#--arflags=}" ;;
--cflags=*) cflags="${option#--cflags=}" ;;
--ldflags=*) ldflags="${option#--ldflags=}" ;;
--ldlibs=*) ldlibs="${option#--ldlibs=}" ;;
--ldshared=*) ldshared="${option#--ldshared=}" ;;
--cflagsmt=*) cflagsmt="${option#--cflagsmt=}" ;;
--ldflagsmt=*) ldflagsmt="${option#--ldflagsmt=}" ;;
--ldlibsmt=*) ldlibsmt="${option#--ldlibsmt=}" ;;
--platform=*) platform="${option#--platform=}" ;;
--prefix=*) prefix_path="${option#--prefix=}" ;;
--enable-shared|--enable-shared=yes) enable_shared="yes" ;;
--enable-shared=no) enable_shared="no" ;;
--enable-mt|--enable-mt=yes) enable_mt="yes" ;;
--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" ;;
2025-07-29 21:04:07 +03:00
esac
done
if [ "$display_help" = "yes" ]; then
cat << EOF
2025-07-29 21:04:07 +03:00
Usage: configure [OPTIONS]
Options:
--help Print this message
2025-07-29 21:29:22 +03:00
--source= Path to the source code
2025-07-29 21:04:07 +03:00
--cc=CC C compiler
--ld=LD Linker
2025-07-29 21:04:07 +03:00
--ar=AR Library archiver
--arflags= Archiver flags
--cflags= Compiler flags
--ldflags= Linker flags
--ldlibs= Linker libraries
--ldshared= Linker flags for shared libraries
--cflagsmt= Compiler flags for multithreading
--ldflagsmt= Linker flags for multithreading
--ldlibsmt= Linker libraries for multithreading
2025-07-29 21:04:07 +03:00
--platform=[Posix|Win32|Dummy] Specify target platform
--prefix= Install prefix
--enable-shared[=yes|no] Make shared library
--enable-pic[=yes|no] Enable position independent code (PIC)
2025-07-29 21:04:07 +03:00
--enable-mt[=yes|no] Enable multithreading support
--enable-lfs[=yes|no] Enable large file support
--enable-tests[=yes|no] Enable unit tests
--use-clock_gettime[=yes|no] Use of clock_gettime regardless of the support
EOF
exit 1
fi
# Ensure that source_path ends on /
case "$source_path" in
*/) ;;
*) source_path="$source_path/" ;;
esac
# Detect and validate OS
2025-07-29 21:04:07 +03:00
os="Unknown"
case "${platform:-$(uname)}" in
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
2025-07-29 21:04:07 +03:00
case "$platform" in
Posix|Win32|Dummy) ;;
*) echo "configure: WARNING: unsupported platform '$platform'"; platform="Dummy" ;;
2025-07-29 21:04:07 +03:00
esac
# Platform-specific settings
exe_suffix=""
dll_prefix="libbh"
dll_suffix=".so"
if [ "$platform" = "Win32" ]; then
exe_suffix=".exe"
dll_prefix="bh"
dll_suffix=".dll"
fi
# Build flags
cflags="-I${source_path}include -I. $cflags"
ldlibs="-lm $ldlibs"
if [ "$enable_pic" = "yes" ]; then
cflags="$cflags -fPIC"
2025-07-29 21:04:07 +03:00
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" ]; then
cflags="$cflags $cflagsmt"
ldflags="$ldflags $ldflagsmt"
ldlibs="$ldlibs $ldlibsmt"
2025-07-29 21:04:07 +03:00
fi
# Create directories
mkdir src src/Platform src/Math src/String 2>/dev/null
mkdir src/Platform/Posix src/Platform/Win32 src/Platform/Dummy 2>/dev/null
if [ "$enable_tests" = "yes" ]; then
mkdir test test/src unit unit/src 2>/dev/null
fi
2025-07-29 21:04:07 +03:00
library=""
add_source() { library="${library}${library:+:}$1"; }
2025-07-29 21:04:07 +03:00
# 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"
2025-07-29 21:04:07 +03:00
done
# Platform specific code
add_source "${source_path}src/Platform/Spinlock.c"
case "$platform" in
2025-09-21 22:07:54 +03:00
Posix) add_source "${source_path}src/Platform/Posix/File.c" ;
add_source "${source_path}src/Platform/Posix/Timer.c" ;;
Win32) add_source "${source_path}src/Platform/Win32/File.c" ;
add_source "${source_path}src/Platform/Win32/Timer.c" ;;
*) add_source "${source_path}src/Platform/Dummy/File.c" ;
add_source "${source_path}src/Platform/Dummy/Timer.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
fi
2025-07-29 21:04:07 +03:00
# Tests
tests=""
unit=""
add_test() { tests="${tests}${tests:+:}$1"; }
2025-07-29 21:04:07 +03:00
if [ "$enable_tests" = "yes" ]; then
unit="${source_path}unit/src/Unit.c"
for file in "${source_path}"test/src/*.c; do
add_test "$file"
2025-07-29 21:04:07 +03:00
done
fi
# Generate Makefile
2025-07-30 18:25:33 +03:00
{
echo "CC=$cc"
echo "LD=$ld"
2025-07-30 18:25:33 +03:00
echo "AR=$ar"
echo "ARFLAGS=$arflags"
2025-07-30 18:25:33 +03:00
echo "CFLAGS=$cflags"
echo "LDFLAGS=$ldflags"
2025-08-28 16:49:59 +03:00
echo "LDLIBS=$ldlibs"
echo "LDSHARED=$ldshared"
echo "PREFIX=$prefix_path"
echo "STATICLIB=libbh.a"
# Shared library
if [ "$enable_shared" = "yes" ]; then
printf "SHAREDLIB=${dll_prefix}${dll_suffix}\n"
printf "\nall: static shared\n"
printf "\nshared: \${SHAREDLIB}\n"
printf "\n\$(SHAREDLIB): "
( IFS=":"; for item in $library; do item="${item#$source_path}"; printf "%s " "${item%.c}.o"; done; )
printf "\n\t\$(LD) \$(LDFLAGS) \$(LDSHARED) -o \$@ "
( IFS=":"; for item in $library; do item="${item#$source_path}"; printf "%s " "${item%.c}.o"; done; )
printf "\$(LDLIBS)\n"
2025-07-30 18:25:33 +03:00
else
printf "\nall: static\n"
2025-07-30 18:25:33 +03:00
fi
2025-07-29 21:04:07 +03:00
# Static library
printf "\nstatic: \${STATICLIB}\n"
printf "\n\$(STATICLIB): "
( 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"
2025-07-29 21:04:07 +03:00
# Compile sources
2025-07-30 18:25:33 +03:00
(
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"
2025-07-30 18:25:33 +03:00
done
)
# Tests
if [ "$enable_tests" = "yes" ]; then
(
unit_obj="${unit#$source_path}"
printf "\n%s: %s\n" "${unit_obj%.c}.o" "$unit"
printf "\t\$(CC) \$(CFLAGS) -I${source_path}unit/include -c -o \$@ %s\n" "$unit"
IFS=":";
for item in $tests; do
object="${item#$source_path}"
printf "\n%s: %s %s \$(STATICLIB)\n" "${object%.c}${exe_suffix}" "${object%.c}.o" "${unit_obj%.c}.o"
printf "\t\$(LD) \$(LDFLAGS) -o \$@ %s %s \$(STATICLIB) \$(LDLIBS)\n" "${object%.c}.o" "${unit_obj%.c}.o"
printf "\n%s: %s\n" "${object%.c}.o" "$item"
printf "\t\$(CC) \$(CFLAGS) -I${source_path}unit/include -c -o \$@ %s\n" "$item"
done
printf "\ntest: "
for item in $tests; do
object="${item#$source_path}"
printf "%s " "${object%.c}${exe_suffix}"
done
echo
for item in $tests; do
object="${item#$source_path}"
printf "\t./%s\n" "${object%.c}${exe_suffix}"
done
echo
)
2025-07-30 18:25:33 +03:00
fi
2025-07-29 21:04:07 +03:00
# Clean
printf "\nclean:\n"
printf "\t-rm -f %s\n" "\$(STATICLIB)"
if [ "$enable_shared" = "yes" ]; then
printf "\t-rm -f %s\n" "\$(SHAREDLIB)"
fi
2025-07-30 18:25:33 +03:00
(
IFS=":"
for item in $library; do
item="${item#$source_path}";
printf "\t-rm -f %s\n" "${item%.c}.o"
2025-07-30 18:25:33 +03:00
done
if [ "$enable_tests" = "yes" ]; then
unit_obj="${unit#$source_path}"
printf "\t-rm -f %s\n" "${unit_obj%.c}.o"
for item in $tests; do
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
2025-07-30 18:25:33 +03:00
)
2025-07-29 21:04:07 +03:00
# 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"
2025-07-30 18:25:33 +03:00
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
2025-07-29 21:04:07 +03:00
# 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"
2025-07-30 18:25:33 +03:00
fi
2025-07-30 18:25:33 +03:00
} > Makefile
2025-07-29 21:04:07 +03:00
# Generate Config.h
2025-07-30 18:25:33 +03:00
{
printf "#ifndef BH_SRC_CONFIG_H\n"
printf "#define BH_SRC_CONFIG_H\n\n"
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
2025-07-30 18:25:33 +03:00
printf "\n#endif /* BH_SRC_CONFIG_H */\n"
} > Config.h
2025-08-28 16:49:59 +03:00
echo " --- General information --- "
2025-08-28 16:49:59 +03:00
echo "OS: $os"
echo "Platform: $platform"
echo "Source path: $source_path"
echo
echo " --- Makefile variables --- "
2025-08-28 16:49:59 +03:00
echo "CC: $cc"
echo "LD: $ld"
2025-08-28 16:49:59 +03:00
echo "AR: $ar"
echo "ARFLAGS: $arflags"
2025-08-28 16:49:59 +03:00
echo "CFLAGS: $cflags"
echo "LDFLAGS: $ldflags"
echo "LDLIBS: $ldlibs"
echo "LDSHARED: $ldshared"
echo "PREFIX: $prefix_path"
echo
echo " --- Enabled options --- "
2025-08-28 16:49:59 +03:00
echo "Enable multithreading: $enable_mt"
echo "Enable long file support: $enable_lfs"
echo "Enable tests: $enable_tests"
echo "Enable PIC: $enable_pic"
echo "Build shared library: $enable_shared"
2025-08-28 16:49:59 +03:00
echo "Use clock_gettime: $use_clock_gettime"