Add configure script

This commit is contained in:
2025-07-29 21:04:07 +03:00
parent 92fab9dbba
commit 682abc5664
8 changed files with 286 additions and 267 deletions

4
.gitignore vendored
View File

@@ -81,3 +81,7 @@ _deps
# Exclude temporary and backup files
*.tmp
*.bak
# Postconfigured Makefile
[Mm]akefile
[Cc]onfig.h

View File

@@ -1,24 +0,0 @@
# Determine OS
OS = $(shell uname)
MAKEFILE = gnu
ifeq ($(OS), Darwin)
MAKEFILE = osx
endif
ifeq ($(OS), windows32)
MAKEFILE = mingw
endif
# Targets
default:
$(MAKE) -f Makefile.$(MAKEFILE)
all:
$(MAKE) -f Makefile.$(MAKEFILE) all
dist:
$(MAKE) -f Makefile.$(MAKEFILE) dist
clean:
$(MAKE) -f Makefile.$(MAKEFILE) clean

View File

@@ -1,49 +0,0 @@
# GNU makefile
# User configuration
DESTDIR ?= /local
CFLAGS ?= -fPIC -O2 -Iinclude
LDFLAGS ?= -lm
TARGET = bh
ENABLE_MT = 1
# System configuration
include Makefile.srcs
SRCS += $(SRCS_POSIX)
ifeq ($(ENABLE_MT), 1)
SRCS += $(SRCS_POSIX_MT)
CFLAGS += -pthread
LDFLAGS += -lpthread
else
SRCS += $(SRCS_DUMMY_MT)
endif
INCDIR ?= $(DESTDIR)/usr/include
INSTALLDIR ?= $(DESTDIR)/usr/lib
STATICLIB = lib$(TARGET).a
SHAREDLIB = lib$(TARGET).so
OBJS = $(SRCS:.c=.o)
# Targets
all: $(STATICLIB) $(SHAREDLIB)
dist: $(STATICLIB) $(SHAREDLIB)
mkdir -p dist
cp *.a dist/
cp *.so dist/
$(STATICLIB): $(OBJS)
$(AR) r $@ $(OBJS)
$(SHAREDLIB): $(OBJS)
$(CC) -s -shared $(LDFLAGS) -o $@ $(OBJS)
.c.o:
$(CC) $(CFLAGS) -c $< -o $@
clean:
-rm -rf $(OBJS) $(STATICLIB) $(SHAREDLIB) dist

View File

@@ -1,50 +0,0 @@
# GNU makefile
# User configuration
DESTDIR ?= /local
CFLAGS ?= -fPIC -O2 -Iinclude
LDFLAGS ?= -lm
TARGET = bh
ENABLE_MT = 1
CC = gcc
# System configuration
include Makefile.srcs
SRCS += $(SRCS_WIN32)
ifeq ($(ENABLE_MT), 1)
SRCS += $(SRCS_WIN32_MT)
CFLAGS += -pthread
LDFLAGS += -lpthread
else
SRCS += $(SRCS_DUMMY_MT)
endif
INCDIR ?= $(DESTDIR)/usr/include
INSTALLDIR ?= $(DESTDIR)/usr/lib
STATICLIB = lib$(TARGET).a
SHAREDLIB = lib$(TARGET).so
OBJS = $(SRCS:.c=.o)
# Targets
all: $(STATICLIB) $(SHAREDLIB)
dist: $(STATICLIB) $(SHAREDLIB)
mkdir -p dist
cp *.a dist/
cp *.so dist/
$(STATICLIB): $(OBJS)
$(AR) r $@ $(OBJS)
$(SHAREDLIB): $(OBJS)
$(CC) -s -shared $(LDFLAGS) -o $@ $(OBJS)
.c.o:
$(CC) $(CFLAGS) -c $< -o $@
clean:
-rm -rf $(OBJS) $(STATICLIB) $(SHAREDLIB) dist

View File

@@ -1,50 +0,0 @@
# GNU makefile
# User configuration
DESTDIR ?= /local
CFLAGS ?= -fPIC -O2 -Iinclude
LDFLAGS ?= -lm
TARGET = bh
ENABLE_MT = 1
CC = clang
# System configuration
include Makefile.srcs
SRCS += $(SRCS_POSIX)
ifeq ($(ENABLE_MT), 1)
SRCS += $(SRCS_POSIX_MT)
CFLAGS += -pthread
LDFLAGS += -lpthread
else
SRCS += $(SRCS_DUMMY_MT)
endif
INCDIR ?= $(DESTDIR)/usr/include
INSTALLDIR ?= $(DESTDIR)/usr/lib
STATICLIB = lib$(TARGET).a
SHAREDLIB = lib$(TARGET).so
OBJS = $(SRCS:.c=.o)
# Targets
all: $(STATICLIB) $(SHAREDLIB)
dist: $(STATICLIB) $(SHAREDLIB)
mkdir -p dist
cp *.a dist/
cp *.so dist/
$(STATICLIB): $(OBJS)
$(AR) r $@ $(OBJS)
$(SHAREDLIB): $(OBJS)
$(CC) -s -shared $(LDFLAGS) -o $@ $(OBJS)
.c.o:
$(CC) $(CFLAGS) -c $< -o $@
clean:
-rm -rf $(OBJS) $(STATICLIB) $(SHAREDLIB) dist

View File

@@ -1,92 +0,0 @@
# Source files
SRCS = src/Algo.c \
src/Args.c \
src/Buffer.c \
src/Bytes.c \
src/Color.c \
src/Hashmap.c \
src/IO.c \
src/Math/Box2f.c \
src/Math/Box3f.c \
src/Math/Line.c \
src/Math/Mat3f.c \
src/Math/Mat4f.c \
src/Math/Misc.c \
src/Math/Plane.c \
src/Math/Quat4f.c \
src/Math/Ray2f.c \
src/Math/Ray3f.c \
src/Math/Vec2f.c \
src/Math/Vec2i.c \
src/Math/Vec3f.c \
src/Math/Vec3i.c \
src/Math/Vec4f.c \
src/Math/Vec4i.c \
src/Platform/Spinlock.c \
src/Queue.c \
src/String/Float.c \
src/String/Int.c \
src/String/Unicode.c \
src/Util.c
SRCS_DUMMY = src/Platform/Dummy/File.c
SRCS_DUMMY_MT = src/Platform/Dummy/Condition.c \
src/Platform/Dummy/Mutex.c \
src/Platform/Dummy/Semaphore.c \
src/Platform/Dummy/Thread.c \
src/Platform/Dummy/Tss.c
SRCS_POSIX = src/Platform/Posix/File.c
SRCS_POSIX_MT = src/Platform/Posix/Condition.c \
src/Platform/Posix/Mutex.c \
src/Platform/Posix/Semaphore.c \
src/Platform/Posix/Thread.c \
src/Platform/Posix/Tss.c
SRCS_WIN32 = src/Platform/Win32/File.c
SRCS_WIN32_MT = src/Platform/Win32/Condition.c \
src/Platform/Win32/Mutex.c \
src/Platform/Win32/Semaphore.c \
src/Platform/Win32/Thread.c \
src/Platform/Win32/Tss.c
INCS = include/BH/Algo.h \
include/BH/Args.h \
include/BH/Common.h \
include/BH/Hashmap.h \
include/BH/IO.h \
include/BH/Math/Box2f.h \
include/BH/Math/Box3f.h \
include/BH/Math/Line.h \
include/BH/Math/Mat3f.h \
include/BH/Math/Mat4f.h \
include/BH/Math/Misc.h \
include/BH/Math/Plane.h \
include/BH/Math/Quat.h \
include/BH/Math/Ray2f.h \
include/BH/Math/Ray3f.h \
include/BH/Math/Vec2f.h \
include/BH/Math/Vec2i.h \
include/BH/Math/Vec3f.h \
include/BH/Math/Vec3i.h \
include/BH/Math/Vec4f.h \
include/BH/Math/Vec4i.h \
include/BH/Queue.h \
include/BH/String.h \
include/BH/Thread.h \
include/BH/Unicode.h \
include/BH/Util.h
INCS_DUMMY_MT = src/Platform/Dummy/Thread.h
INCS_POSIX_MT = src/Platform/Posix/Thread.h
INCS_WIN32_MT = src/Platform/Win32/Thread.h
SRCS_UNIT = unit/src/Unit.c
INCS_UNIT = unit/include/BH/Unit.h

279
configure vendored Executable file
View File

@@ -0,0 +1,279 @@
#!/bin/sh
# Configure script by blankhex
# Global variables
source_path=""
cc=$CC
ar=$AR
enable_dynamic="no"
enable_mt="no"
enable_lfs="no"
enable_tests="no"
use_clock_gettime="no"
if [ -z "$cc" ]; then cc=gcc; fi
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"
}
# Option parsing
for option do
case $option in
--cc=*) assign_option "$option"; ;;
--ar=*) assign_option "$option"; ;;
--extra-cflags=*) assign_option "$option" extra_cflags; ;;
--extra-ldflags=*) assign_option "$option" extra_ldflags; ;;
--platform=*) assign_option "$option"; ;;
--enable-dynamic|--enable-dynamic=*) assign_toggle "$option" enable_dynamic yes; ;;
--enable-mt|--enable-mt=*) assign_toggle "$option" enable_mt yes; ;;
--enable-lfs|--enable-lfs=*) assign_toggle "$option" enable_lfs yes; ;;
--enable-tests|--enable-tests=*) assign_toggle "$option" enable_tests yes; ;;
--use-clock_gettime|--use-clock_gettime=*) assign_toggle "$option" use_clock_gettime yes; ;;
--source-path=*) assign_option "$option" source_path; ;;
--help|-h) display_help="yes"; ;;
*) echo "configure: WARNING: unrecognized option $option"; ;;
esac
done
# Help message
show_help() {
cat << EOF
Usage: configure [OPTIONS]
Options:
--help Print this message
--source-path= Path to the source code
--cc=CC C compiler
--ar=AR Library archiver
--extra-cflags= Extra compiler flags
--extra-ldflags= Extra linker flags
--platform=[Posix|Win32|Dummy] Specify target platform
--enable-dynamic[=yes|no] Make dynamic library
--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
}
if [ -n "$display_help" ]; then show_help; fi
# Detect OS and platform, because it wasn't specified
os="Unknown"
if [ -z "$platform" ]; then
case $(uname) in
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
# Make build magic
cflags="-fPIC -I${source_path}include -I."
ldflags="-lm -shared"
cflags="$cflags $extra_cflags"
ldflags="$ldflags $extra_ldflags"
echo "OS: $os"
echo "Platform: $platform"
echo "Source path: $source_path"
echo "CC: $cc"
echo "AR: $ar"
echo "CFLAGS: $cflags"
echo "LDFLAGS: $ldflags"
echo "Enable multithreading: $enable_mt"
echo "Enable long file support: $enable_lfs"
echo "Enable tests: $enable_tests"
echo "Build dynamic library: $enable_dynamic"
echo "Use clock_gettime: $use_clock_gettime"
# Make sure that build directories exist
mkdir src src/Platform src/Math src/String 2> /dev/null
# Build source list
sources="${source_path}src/Platform/Spinlock.c"
# Platform dependant sources
if [ "$platform" = "Posix" ]; then
exe=""
sources="$sources:${source_path}src/Platform/Posix/File.c"
mkdir src/Platform/Posix 2> /dev/null
elif [ "$platform" = "Win32" ]; then
exe=".exe"
sources="$sources:${source_path}src/Platform/Win32/File.c"
mkdir src/Platform/Win32 2> /dev/null
else
exe=""
sources="$sources:${source_path}src/Platform/Dummy/File.c"
mkdir src/Platform/Dummy 2> /dev/null
fi
if [ "$platform" = "Posix" ] && [ "$enable_mt" = "yes" ]; then
sources="$sources:${source_path}src/Platform/Posix/Condition.c"
sources="$sources:${source_path}src/Platform/Posix/Mutex.c"
sources="$sources:${source_path}src/Platform/Posix/Semaphore.c"
sources="$sources:${source_path}src/Platform/Posix/Thread.c"
sources="$sources:${source_path}src/Platform/Posix/Tss.c"
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
# Platform independant sources
for file in ${source_path}src/*.c; do
sources="$sources:$file"
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
tests=""
mkdir test test/src 2> /dev/null
for file in "${source_path}test/src/*.c"; do
if [ -n "$tests" ]; then tests="$tests:$file"
else tests="$file"; fi
done
fi
# Generate variables
echo "CC=$cc" > Makefile
echo "CFLAGS=$cflags" >> Makefile
echo "LDFLAGS=$ldflags" >> Makefile
echo "STATICLIB=libbh.a" >> Makefile
if [ "$enable_dynamic" = "yes" ]; then
echo "DYNAMICLIB=libbh.so" >> Makefile
fi
# Set a list of objects
printf "OBJS=" >> Makefile
(
IFS=":"
for source in $sources; do
object=${source%%.c}.o
object=${object#${source_path}}
echo " \\" >> Makefile
printf " %s" "$object" >> Makefile
done
)
printf "\n\n" >> Makefile
# Check if dynamic linking is enabled and add rules
if [ "$enable_dynamic" = "yes" ]; then
printf "all: static dynamic\n\n" >> Makefile
printf "dynamic: \$(DYNAMICLIB)\n\n" >> Makefile
printf "\$(DYNAMICLIB): \$(OBJS)\n" >> Makefile
printf "\t\$(CC) \$(LDFLAGS) \$(OBJS) -o \$(DYNAMICLIB)\n\n" >> Makefile
else
printf "all: static\n\n" >> Makefile
fi
# Add rules for static linking
printf "static: \$(STATICLIB)\n\n" >> Makefile
printf "\$(STATICLIB): \$(OBJS)\n" >> Makefile
printf "\t\$(AR) r \$(STATICLIB) \$(OBJS)\n\n" >> Makefile
# Generate rules for tests
if [ "$enable_tests" = "yes" ]; then
(
IFS=":"
for source in $tests; do
object=${source%%.c}${exe}
object=${object#${source_path}}
printf "%s: %s \$(STATICLIB)\n" $object $source >> Makefile
printf "\t\$(CC) \$(CFLAGS) -I${source_path}unit/include %s %s \$(STATICLIB) -o %s\n\n" "$source" "${source_path}unit/src/Unit.c" "$object" >> Makefile
done
)
printf "tests: " >> Makefile
(
IFS=":"
for source in $tests; do
object=${source%%.c}${exe}
object=${object#${source_path}}
echo " \\" >> Makefile
printf " %s" "$object" >> Makefile
done
)
printf "\n\n" >> Makefile
fi
# Generate rules for main objects
(
IFS=":"
for source in $sources; do
object=${source%%.c}.o
object=${object#${source_path}}
printf "$object: $source\n" >> Makefile
printf "\t\$(CC) -c \$(CFLAGS) \$< -o \$@\n\n" >> Makefile
done
)
printf "\n\n" >> Makefile
# Generate clean rules
printf "clean:\n" >> Makefile
printf "\t-rm -f \$(OBJS) \$(STATICLIB)" >> Makefile
if [ "$enable_dynamic" = "yes" ]; then
printf " \$(DYNAMICLIB)" >> Makefile
fi
if [ "$enable_tests" = "yes" ]; then
(
IFS=":"
for source in $tests; do
object=${source%%.c}${exe}
object=${object#${source_path}}
printf " %s" "$object" >> Makefile
done
)
fi
printf "\n\n" >> Makefile
# Generate internal config.h
printf "#ifndef BH_SRC_CONFIG_H\n#define BH_SRC_CONFIG_H\n\n" > Config.h
if [ "$use_clock_gettime" ]; then printf "#define BH_USE_CLOCK_GETTIME\n" >> Config.h; fi
if [ "$enable_lfs" ]; then printf "#define BH_ENABLE_LFS\n" >> Config.h; fi
printf "\n#endif /* BH_SRC_CONFIG_H */\n" >> Config.h

View File

@@ -2,6 +2,7 @@
#define BH_PLATFORM_POSIX_TIMESPEC_H
#include <Config.h>
#include <BH/Thread.h>
#include <sys/time.h>
#include <time.h>
@@ -10,7 +11,7 @@
static int convertToTimespec(struct timespec *ts,
uint32_t timeout)
{
#ifdef USE_CLOCK_GETTIME
#if (_POSIX_TIMERS > 0) || defined(BH_USE_CLOCK_GETTIME)
if (clock_gettime(CLOCK_REALTIME, ts))
return BH_ERROR;
#else