# Copyright (c) 2010 Vladimir Prus.
# Copyright (c) 2013 Steven Watanabe
#
# Use, modification and distribution is subject to the Boost Software
# License Version 1.0. (See accompanying file LICENSE.txt or
# https://www.bfgroup.xyz/b2/LICENSE.txt)

# Supports the libjpeg library
#
# After 'using libjpeg', the following targets are available:
#
# /libjpeg//libjpeg -- The libjpeg library

import project ;
import ac ;
import errors ;
import feature ;
import "class" : new ;
import targets ;
import path ;
import modules ;
import indirect ;
import property ;
import property-set ;

header = jpeglib.h ;

# jpeglib.h requires stdio.h to be included first.
header-test = "#include <stdio.h>\n#include <jpeglib.h>\n" ;

names = jpeg ;

sources =   jaricom.c jcapimin.c jcapistd.c jcarith.c jccoefct.c jccolor.c
        jcdctmgr.c jchuff.c jcinit.c jcmainct.c jcmarker.c jcmaster.c
        jcomapi.c jcparam.c jcprepct.c jcsample.c jctrans.c jdapimin.c
        jdapistd.c jdarith.c jdatadst.c jdatasrc.c jdcoefct.c jdcolor.c
        jddctmgr.c jdhuff.c jdinput.c jdmainct.c jdmarker.c jdmaster.c
        jdmerge.c jdpostct.c jdsample.c jdtrans.c jerror.c jfdctflt.c
        jfdctfst.c jfdctint.c jidctflt.c jidctfst.c jidctint.c jquant1.c ;

library-id = 0 ;

if --debug-configuration in [ modules.peek : ARGV ]
{
    .debug =  true ;
}

# Initializes the libjpeg library.
#
# libjpeg can be configured either to use pre-existing binaries
# or to build the library from source.
#
# Options for configuring a prebuilt libjpeg::
#
#   <search>
#       The directory containing the libjpeg binaries.
#   <name>
#       Overrides the default library name.
#   <include>
#       The directory containing the libjpeg headers.
#   <dll-path>
#       Extra directories to add to library search paths of consumers during
#       runtime (multiple instances are allowed).
#
# If none of these options is specified, then the environmental
# variables LIBJPEG_LIBRARY_PATH, LIBJPEG_NAME, and LIBJPEG_INCLUDE will
# be used instead.
#
# Options for building libjpeg from source::
#
#   <source>
#       The libjpeg source directory.  Defaults to the environmental variable
#       LIBJPEG_SOURCE.
#   <tag>
#       A rule which computes the actual name of the compiled
#       libraries based on the build properties.  Ignored
#       when using precompiled binaries.
#   <build-name>
#       The base name to use for the compiled library.  Ignored
#       when using precompiled binaries.
#
# Examples::
#
#   # Find libjpeg in the default system location
#   using libjpeg ;
#   # Build libjpeg from source
#   using libjpeg : 8c : <source>/home/steven/libjpeg-8c ;
#   # Find libjpeg in /usr/local
#   using libjpeg : 8c
#     : <include>/usr/local/include <search>/usr/local/lib ;
#   # Build libjpeg from source for msvc and find
#   # prebuilt binaries for gcc.
#   using libjpeg : 8c : <source>C:/Devel/src/libjpeg-8c : <toolset>msvc ;
#   using libjpeg : 8c : : <toolset>gcc ;
#
rule init (
    version ?
    # The libjpeg version (currently ignored)

    : options *
    # A list of the options to use

    : requirements *
    # The requirements for the libjpeg target

    : is-default ?
    # Default configurations are only used when libjpeg
    # has not yet been configured.  This option is
    # deprecated.  A configuration will be treated
    # as a default when none of <include>, <search>,
    # <name>, and <source> are present.
    )
{
    local caller = [ project.current ] ;

    if ! $(.initialized)
    {
        .initialized = true ;

        project.initialize $(__name__) ;
        .project = [ project.current ] ;
        project libjpeg ;
    }

    local library-path = [ feature.get-values <search> : $(options) ] ;
    local include-path = [ feature.get-values <include> : $(options) ] ;
    local source-path = [ feature.get-values <source> : $(options) ] ;
    local library-name = [ feature.get-values <name> : $(options) ] ;
    local dll-paths = [ property.select <dll-path> : $(options) ] ;
    local tag = [ feature.get-values <tag> : $(options) ] ;
    local build-name = [ feature.get-values <build-name> : $(options) ] ;

    condition = [ property-set.create $(requirements) ] ;
    condition = [ property-set.create [ $(condition).base ] ] ;

    if ! $(library-path) && ! $(include-path) && ! $(source-path) && ! $(library-name)
    {
        is-default = true ;
    }

    # Ignore environmental LIBJPEG_SOURCE if this initialization
    # requested to search for a specific pre-built library.
    if $(library-path) || $(include-path) || $(library-name)
    {
        if $(source-path) || $(tag) || $(build-name)
        {
            errors.user-error "incompatible options for libjpeg:"
                [ property.select <search> <include> <name> : $(options) ] "and"
                [ property.select <source> <tag> <build-name> : $(options) ] ;
        }
    }
    else
    {
        source-path ?= [ modules.peek : LIBJPEG_SOURCE ] ;
    }

    if $(.configured.$(condition))
    {
        if $(is-default)
        {
            if $(.debug)
            {
                ECHO "notice: [libjpeg] libjpeg is already configured" ;
            }
        }
        else
        {
            errors.user-error "libjpeg is already configured" ;
        }
        return ;
    }
    else if $(source-path)
    {
        build-name ?= jpeg ;
        library-id = [ CALC $(library-id) + 1 ] ;
        tag = [ MATCH ^@?(.*)$ : $(tag) ] ;
        if $(tag)
        {
            tag = [ indirect.make $(tag) : [ $(caller).project-module ] ] ;
        }
        sources = [ path.glob $(source-path) : $(sources) ] ;
        if $(.debug)
        {
            ECHO "notice: [libjpeg] Building libjpeg from source as $(build-name)" ;
            if $(condition)
            {
                ECHO "notice: [libjpeg] Condition" [ $(condition).raw ] ;
            }
            if $(sources)
            {
                ECHO "notice: [libjpeg] found libjpeg source in $(source-path)" ;
            }
            else
            {
                ECHO "warning: [libjpeg] could not find libjpeg source in $(source-path)" ;
            }
        }
        local target ;
        if $(sources) {
            target = [ targets.create-typed-target LIB : $(.project)
              : $(build-name).$(library-id)
              : $(sources)
              : $(requirements)
                <tag>@$(tag)
                <include>$(source-path)
                <toolset>msvc:<define>_CRT_SECURE_NO_DEPRECATE
                <toolset>msvc:<define>_SCL_SECURE_NO_DEPRECATE
              :
              : <include>$(source-path) ] ;
        }

        local mt = [ new ac-library libjpeg : $(.project) : $(condition) ] ;
        $(mt).set-header $(header) ;
        $(mt).set-default-names $(names) ;
        $(mt).add-usage-requirements $(dll-paths) ;
        if $(target)
        {
            $(mt).set-target $(target) ;
        }
        targets.main-target-alternative $(mt) ;
    } else {
        if $(.debug)
        {
            ECHO "notice: [libjpeg] Using pre-installed library" ;
            if $(condition)
            {
                ECHO "notice: [libjpeg] Condition" [ $(condition).raw ] ;
            }
        }

        local mt = [ new ac-library libjpeg : $(.project) : $(condition) :
            $(include-path) : $(library-path) : $(library-name) : $(root) ] ;
        $(mt).set-header $(header) ;
        $(mt).set-header-test $(header-test) ;
        $(mt).set-default-names $(names) ;
        targets.main-target-alternative $(mt) ;
    }
    .configured.$(condition) = true ;
}
