[go: up one dir, main page]

Menu

[b23f34]: / configure  Maximize  Restore  History

Download this file

240 lines (191 with data), 5.6 kB

#!/bin/bash

# http://blog.faustocarrera.com.ar/post/60750937265/bash-compare-software-versions
function compare_versions() {
  # compare_versions min_version current_version
	ver_a=$1
	ver_b=$2
	versions="$ver_a $ver_b"
	echo $versions | awk '{
		# convert to arrays
		split($1, aArr, ".")
		split($2, bArr, ".")
		# Translate in decimal
		aDec = aArr[1] * 10^12 + aArr[2] * 10^8 + aArr[3] * 1^4 + aArr[4]
		bDec = bArr[1] * 10^12 + bArr[2] * 10^8 + bArr[3] * 1^4 + bArr[4]
		# compare versions
		if (bDec >= aDec) { print "1" }
		if (bDec < aDec) { print "0" }
	}'
}

COLIBRO_LIB_DIR=lib
COLIBRO_INC_DIR=include
EXTERN_LIBS_DIR=extern

libs_required=("sqlite3" "zlib" "polarssl" "firedns" "math")
# -1 = system
libs_version=("3.7.9" "1.2.3.4" "1.3.1" "1.0" "-1")
libs_count=${#libs_required[*]}

lib_dir="/usr/include"

libs_make=()

index=0
while [ $index -lt $libs_count ]
do
  library=${libs_required[$index]}
  present=0
  local_lib=0

  printf "Check if '%s' is present ... " $library

  # Check local library
  if [ -d $EXTERN_LIBS_DIR/$library ]
  then
    present=1
    local_lib=1
  fi

  # Check system library if not local
  if [ $present -eq 0 ]
  then
    lib_array=$(echo `whereis $library` | tr " " "\n" )
    
    # Check library folder
    if [ -d $lib_dir/$library ]
    then
      present=1
    else
      # Check library header file
      
      for package in ${lib_array[*]}
      do
        if [ "$package" == "$lib_dir/$library.h" ]
        then
          present=1
        fi
      done
    fi
  fi
  
  # Get the min lib version
  lib_version_min=${libs_version[$index]}
  
  # Lib found?
  if [ $present -eq 0 ]
  then
    printf "no\n\n"
    
    printf "Please install '%s' library developer files on your system or run './download-missing.sh'\n" $library
    printf "\n\n"
    exit
    
  else
    printf "yes"
    if [ $local_lib -eq 1 ]
    then
      printf ", %s/%s/" $EXTERN_LIBS_DIR $library
    fi
  fi
  
  # Need to check version, only if nessesary
  if [[ $lib_version_min != "-1" && $local_lib -eq 0 ]]
  then
    printf "\n"
    printf "Check version of '%s' ... " $library
    
    version_installed=`pkg-config --silence-errors --modversion $library`
    if [ -z $version_installed ]
    then
      # Search in dpkg database
      versions_installed=`dpkg-query -W "*$library*-dev*" | awk '{ print $2 }'`
      
      for version in ${versions_installed[*]}
      do
        version_installed=$version
        
        version_result=$(compare_versions $lib_version_min $version)
        if [ $version_result -eq "1" ]
        then
          # Found compatible lib version
          break
        fi
      done
    else
      version_result=$(compare_versions $lib_version_min $version_installed)
    fi
    
    if [ $version_result == "1" ]
    then
      printf "%s >= %s" $version_installed $lib_version_min
    else
      printf "error >= %s needed, %s installed\n" $lib_version_min $version_installed
      printf "\nTry to run './download-missing.sh', this may fix the problem\n\n"
      exit
    fi
  fi
  
  if [ $local_lib -eq 1 ]
  then
    # Add $library to libs_make array
    libs_make[$[${#libs_make[@]}+1]]=$library
    printf " with make"
  fi
  printf "\n"
  
  ((index++))
done

printf "\nLibraries added to 'make' process:\n"
for x in ${libs_make[*]}
do
  printf "%s\n" $x
done
printf "\n"

# ----- Colibro makefile -----
printf "Generate Makefile ... "
printf "

#
# Makefile for libcolibro (stolen from firedns) (requires GNU make)
#

prefix = /usr/local
exec_prefix = \$(prefix)

bindir = \$(exec_prefix)/bin
libdir = \$(prefix)/lib
includedir = \$(prefix)/include
externlibsdir = extern
local_lib = lib

SRCS := \$(wildcard src/*.c)
OBJS := \$(SRCS:.c=.o)
LOBJS := \$(OBJS:.o=.lo)
EXSRCS := \$(wildcard examples/*.c)
EXOBJS := \$(EXSRCS:.c=.o)
EXBINS := \$(EXSRCS:.c=)

EXTERN_LIBS_INC := \$(addprefix -I, \$(wildcard \$(externlibsdir)/*/include))
# EXTRALIBS := ./extra-libs/polarssl/include

CFLAGS  = -Os -std=c99 -D_GNU_SOURCE

# Add more extra-libs here
INC     = -I./include \$(EXTERN_LIBS_INC)
AR      = \$(CROSS_COMPILE)ar
RANLIB  = \$(CROSS_COMPILE)ranlib
OBJCOPY = \$(CROSS_COMPILE)objcopy

ALL_INCLUDES := \$(sort \$(wildcard include/*.h include/*/*.h))

MY_LIBS = \$(local_lib)/libcolibro.a
ALL_LIBS = \$(MY_LIBS)

EXTERN_LIBS_MAKE := \$(addsuffix /_make, \$(wildcard \$(externlibsdir)/*))
EXTERN_LIBS_CLEAN := \$(addsuffix /_clean, \$(wildcard \$(externlibsdir)/*))

all: \$(ALL_LIBS) \$(EXTERN_LIBS_MAKE)
	@echo \"----------------------------------\"
	@echo \"Link your application with:\"
	@echo \"      '-lcolibro -lsqlite3 -lz -lpolarssl -lm'\"
	@echo \"----------------------------------\"

examples: \$(EXBINS)

install: \$(ALL_LIBS:lib/%%=\$(DESTDIR)\$(libdir)/%%) \$(ALL_INCLUDES:include/%%=\$(DESTDIR)\$(includedir)/%%) \$(ALL_TOOLS:tools/%%=\$(DESTDIR)\$(bindir)/%%)

clean: \$(EXTERN_LIBS_CLEAN)
	rm -rf \$(local_lib)/
	rm -f \$(OBJS)
	rm -f \$(LOBJS)
	rm -f \$(EXOBJS)
	rm -f \$(EXBINS)
	rm -f \$(ALL_LIBS) \$(local_lib)/*.[ao] \$(local_lib)/*.so

extern/%%/_make:
	cd \$(dir \$@) && make

extern/%%/_clean:
	cd \$(dir \$@) && make clean

%%.out: %%.o \$(ALL_LIBS)
	\$(CC) -o \$@ \$< -l:\$(local_lib)/libcolibro.a \$(LDFLAGS)

%%.o: %%.c
	\$(CC) \$(CFLAGS) \$(INC) -c -o \$@ \$<

\$(local_lib)/libcolibro.a: \$(OBJS)
	rm -f \$@
	mkdir \$(local_lib)
	\$(AR) cr \$@ \$(OBJS)
	\$(RANLIB) \$@

\$(DESTDIR)\$(bindir)/%%: tools/%%
	install -D \$< \$@

\$(DESTDIR)\$(prefix)/%%: %%
	install -D -m 644 \$< \$@

.PHONY: all clean install examples
"  > Makefile
printf "done\n"

printf "\nRun 'make' now\n"

exit