#!/usr/bin/python
#
# configure script for libLensFun
#

import tibs

# Define project name
tibs.PROJ = "lensfun"
# Define version number (major.minor.release)
tibs.VERSION = "0.2.5.1"
# Project home page
tibs.HOMEPAGE = "http://lensfun.berlios.de"

# A directory where configure will find the required libs on systems
# without pkg-config (e.g. windows) (only glib-2.0 needed so far)
tibs.OPTIONS.append (
    [ None, "sdkdir", "DIR", "global SDKDIR; SDKDIR = optarg",
      "Specify the directory with additional libraries\n"
      "for platforms without pkg-config." ])
tibs.ENVARS.append (
    [ "SDKDIR", "./glib-2.0", "The directory for additional libraries (default: ./glib-2.0)" ])
# Add a option to set/disable vectorization instructions
tibs.OPTIONS.append (
    [ None, "vectorization", "L", "global VECTORIZATION; VECTORIZATION = optarg",
      "Compile with vectorization instruction set (if supported) or\n"
      "disable vectorization (a list like 'SSE4,SSE2,SSE' or empty)." ])
tibs.VECTORIZATION = "yes";

tibs.start ()

if tibs.TARGET [0] == "windows":
    # On Windows we don't have pkg-config so we just have to hope
    # that user has a installed SDK somewhere where we can find it ...
    tibs.PKGCONFIG ["glib-2.0"] = {
        "modversion": "2.0",
        "cflags": tibs.TOOLKIT.include (tibs.SDKDIR + "/include/glib-2.0") + " " +
                  tibs.TOOLKIT.include (tibs.SDKDIR + "/lib/glib-2.0/include"),
        "libs": tibs.TOOLKIT.linklib ("glib-2.0", tibs.SDKDIR + "/lib")
    }
    tibs.PKGCONFIG ["libpng"] = {
        "modversion": "1.0",
        "cflags": tibs.TOOLKIT.include (tibs.SDKDIR + "/include"),
        "libs": tibs.TOOLKIT.linklib ("png", tibs.SDKDIR + "/lib")
    }

# Check for tools
tibs.check_program ("GNU Make", "make --version", ".*?([0-9\.]+).*", "3.81", True)
if tibs.check_program ("makedep", "makedep -V", ".*Version.*?([0-9\.]+).*", "0.1.0"):
    tibs.add_config_mak ("MAKEDEP", "makedep")
    makedep = "yes"
else:
    makedep = "no"

if tibs.check_program ("Doxygen", "doxygen --version", "([0-9\.]+)", "1.5.0"):
    tibs.add_config_mak ("DOXYGEN", "doxygen")
    doxygen = "yes"
else:
    doxygen = "no"

tibs.pkgconfig_check_library ("glib-2.0", "2.0",
    "this is the low-level library that libLensFun relies on")
tibs.check_header ("glib.h", tibs.CONFIG_MAK ["CFLAGS.GLIB_20"],
    "this is the low-level library that libLensFun relies on")

if tibs.pkgconfig_check_library ("libpng", "1.0"):
    # Check for zlib: first check for the standard libz.a name,
    # and then check for the bastard windoz's "zlib.lib".
    if not tibs.check_library ("zlib", None, None, None, "", tibs.TOOLKIT.linklib ("z")):
        print ("Re-trying with another library name ...")
        tibs.check_library ("zlib", None, None, None, "", tibs.TOOLKIT.linklib ("zlib"))

# On Windows we don't have regex.h, so use a open-source alternative instead.
# One day we'll switch to regex library from GLib
if not tibs.check_header ("regex.h"):
    tibs.add_config_mak ("NEED_REGEX")

# Check for CPU vectorization feature enable flags
if tibs.VECTORIZATION.strip ().upper () == "YES":
    if (tibs.TARGET [1] == "x86") or (tibs.TARGET [1] == "x86_64"):
        tibs.VECTORIZATION = ["SSE", "SSE2"]

if type (tibs.VECTORIZATION) == str:
    tibs.VECTORIZATION = tibs.VECTORIZATION.strip ().upper ().split (",");

CFLAGS_V = { "SSE" : "-msse", "SSE2" : "-msse2" }
for flag in tibs.VECTORIZATION:
    if not len (flag):
        continue
    if not flag in CFLAGS_V:
        tibs.abort_configure ("user requested vectorization\n" \
            "for instruction set `%s', which is not supported" % flag);
    if (not CFLAGS_V [flag]) or tibs.check_cflags (CFLAGS_V [flag], "V_CFLAGS"):
        tibs.add_config_h ("VECTORIZATION_" + flag);
        tibs.add_config_mak ("VECTORIZATION_" + flag, CFLAGS_V [flag]);

# Tell lensfun it's meant to be a static library
if not tibs.SHAREDLIBS:
    tibs.add_config_h ("CONF_LENSFUN_STATIC");

# Check for <endian.h>
tibs.check_header ("endian.h")

# Add a macro to know we're compiling lensfun, not a client library
tibs.add_config_h ("CONF_LENSFUN_INTERNAL")

tibs.add_config_mak ("TOOLKIT", tibs.COMPILER.upper ())
tibs.add_config_mak ("CFLAGS", tibs.CFLAGS)
tibs.add_config_mak ("CXXFLAGS", tibs.CXXFLAGS)
tibs.add_config_mak ("LDFLAGS", tibs.LDFLAGS)
tibs.add_config_mak ("LDLIBS", tibs.LIBS)
tibs.add_config_mak ("MODE", tibs.MODE)
tibs.add_config_mak ("GENFILES", "config.mak build/PKGBUILD " +
    "build/lensfun.spec include/config.h")

tibs.update_file ("include/config.h", tibs.get_config_h ())
tibs.update_file ("config.mak", tibs.get_config_mak ())

tibs.substmacros ("include/lensfun/lensfun.h.in")
tibs.substmacros ("build/PKGBUILD.in")
tibs.substmacros ("build/lensfun.spec.in")

print ("--------------------------------------------------------------------")
print ("Default build mode:                 " + tibs.MODE)
print ("Build doxygen documentation:        " + doxygen)
print ("Use the pre-installed makedep:      " + makedep)
print ("Target directory for binaries:      " + tibs.BINDIR)
print ("Target directory for data files:    " + tibs.DATADIR)
print ("Target directory for libraries:     " + tibs.LIBDIR)
print ("Target directory for include files: " + tibs.INCLUDEDIR)
print ("Target directory for documentation: " + tibs.DOCDIR)
print ("Use vector instruction set(-s):     " + ", ".join (x for x in tibs.VECTORIZATION))
print ("--------------------------------------------------------------------")
