1# Copyright (c) 2010 Vladimir Prus.
2# Copyright (c) 2013 Steven Watanabe
3#
4# Use, modification and distribution is subject to the Boost Software
5# License Version 1.0. (See accompanying file LICENSE_1_0.txt or
6# http://www.boost.org/LICENSE_1_0.txt)
7
8# Supports the libjpeg library
9#
10# After 'using libjpeg', the following targets are available:
11#
12# /libjpeg//libjpeg -- The libjpeg library
13
14import project ;
15import ac ;
16import errors ;
17import feature ;
18import "class" : new ;
19import targets ;
20import path ;
21import modules ;
22import indirect ;
23import property ;
24import property-set ;
25
26header = jpeglib.h ;
27
28# jpeglib.h requires stdio.h to be included first.
29header-test = "#include <stdio.h>\n#include <jpeglib.h>\n" ;
30
31names = jpeg ;
32
33sources =   jaricom.c jcapimin.c jcapistd.c jcarith.c jccoefct.c jccolor.c
34        jcdctmgr.c jchuff.c jcinit.c jcmainct.c jcmarker.c jcmaster.c
35        jcomapi.c jcparam.c jcprepct.c jcsample.c jctrans.c jdapimin.c
36        jdapistd.c jdarith.c jdatadst.c jdatasrc.c jdcoefct.c jdcolor.c
37        jddctmgr.c jdhuff.c jdinput.c jdmainct.c jdmarker.c jdmaster.c
38        jdmerge.c jdpostct.c jdsample.c jdtrans.c jerror.c jfdctflt.c
39        jfdctfst.c jfdctint.c jidctflt.c jidctfst.c jidctint.c jquant1.c ;
40
41library-id = 0 ;
42
43if --debug-configuration in [ modules.peek : ARGV ]
44{
45    .debug =  true ;
46}
47
48# Initializes the libjpeg library.
49#
50# libjpeg can be configured either to use pre-existing binaries
51# or to build the library from source.
52#
53# Options for configuring a prebuilt libjpeg::
54#
55#   <search>
56#       The directory containing the libjpeg binaries.
57#   <name>
58#       Overrides the default library name.
59#   <include>
60#       The directory containing the libjpeg headers.
61#
62# If none of these options is specified, then the environmental
63# variables LIBJPEG_LIBRARY_PATH, LIBJPEG_NAME, and LIBJPEG_INCLUDE will
64# be used instead.
65#
66# Options for building libjpeg from source::
67#
68#   <source>
69#       The libjpeg source directory.  Defaults to the environmental variable
70#       LIBJPEG_SOURCE.
71#   <tag>
72#       A rule which computes the actual name of the compiled
73#       libraries based on the build properties.  Ignored
74#       when using precompiled binaries.
75#   <build-name>
76#       The base name to use for the compiled library.  Ignored
77#       when using precompiled binaries.
78#
79# Examples::
80#
81#   # Find libjpeg in the default system location
82#   using libjpeg ;
83#   # Build libjpeg from source
84#   using libjpeg : 8c : <source>/home/steven/libjpeg-8c ;
85#   # Find libjpeg in /usr/local
86#   using libjpeg : 8c
87#     : <include>/usr/local/include <search>/usr/local/lib ;
88#   # Build libjpeg from source for msvc and find
89#   # prebuilt binaries for gcc.
90#   using libjpeg : 8c : <source>C:/Devel/src/libjpeg-8c : <toolset>msvc ;
91#   using libjpeg : 8c : : <toolset>gcc ;
92#
93rule init (
94    version ?
95    # The libjpeg version (currently ignored)
96
97    : options *
98    # A list of the options to use
99
100    : requirements *
101    # The requirements for the libjpeg target
102
103    : is-default ?
104    # Default configurations are only used when libjpeg
105    # has not yet been configured.  This option is
106    # deprecated.  A configuration will be treated
107    # as a default when none of <include>, <search>,
108    # <name>, and <source> are present.
109    )
110{
111    local caller = [ project.current ] ;
112
113    if ! $(.initialized)
114    {
115        .initialized = true ;
116
117        project.initialize $(__name__) ;
118        .project = [ project.current ] ;
119        project libjpeg ;
120    }
121
122    local library-path = [ feature.get-values <search> : $(options) ] ;
123    local include-path = [ feature.get-values <include> : $(options) ] ;
124    local source-path = [ feature.get-values <source> : $(options) ] ;
125    local library-name = [ feature.get-values <name> : $(options) ] ;
126    local tag = [ feature.get-values <tag> : $(options) ] ;
127    local build-name = [ feature.get-values <build-name> : $(options) ] ;
128
129    condition = [ property-set.create $(requirements) ] ;
130    condition = [ property-set.create [ $(condition).base ] ] ;
131
132    if ! $(library-path) && ! $(include-path) && ! $(source-path) && ! $(library-name)
133    {
134        is-default = true ;
135    }
136
137    # Ignore environmental LIBJPEG_SOURCE if this initialization
138    # requested to search for a specific pre-built library.
139    if $(library-path) || $(include-path) || $(library-name)
140    {
141        if $(source-path) || $(tag) || $(build-name)
142        {
143            errors.user-error "incompatible options for libjpeg:"
144                [ property.select <search> <include> <name> : $(options) ] "and"
145                [ property.select <source> <tag> <build-name> : $(options) ] ;
146        }
147    }
148    else
149    {
150        source-path ?= [ modules.peek : LIBJPEG_SOURCE ] ;
151    }
152
153    if $(.configured.$(condition))
154    {
155        if $(is-default)
156        {
157            if $(.debug)
158            {
159                ECHO "notice: [libjpeg] libjpeg is already configured" ;
160            }
161        }
162        else
163        {
164            errors.user-error "libjpeg is already configured" ;
165        }
166        return ;
167    }
168    else if $(source-path)
169    {
170        build-name ?= jpeg ;
171        library-id = [ CALC $(library-id) + 1 ] ;
172        tag = [ MATCH ^@?(.*)$ : $(tag) ] ;
173        if $(tag)
174        {
175            tag = [ indirect.make $(tag) : [ $(caller).project-module ] ] ;
176        }
177        sources = [ path.glob $(source-path) : $(sources) ] ;
178        if $(.debug)
179        {
180            ECHO "notice: [libjpeg] Building libjpeg from source as $(build-name)" ;
181            if $(condition)
182            {
183                ECHO "notice: [libjpeg] Condition" [ $(condition).raw ] ;
184            }
185            if $(sources)
186            {
187                ECHO "notice: [libjpeg] found libjpeg source in $(source-path)" ;
188            }
189            else
190            {
191                ECHO "warning: [libjpeg] could not find libjpeg source in $(source-path)" ;
192            }
193        }
194        local target ;
195        if $(sources) {
196            target = [ targets.create-typed-target LIB : $(.project)
197              : $(build-name).$(library-id)
198              : $(sources)
199              : $(requirements)
200                <tag>@$(tag)
201                <include>$(source-path)
202                <toolset>msvc:<define>_CRT_SECURE_NO_DEPRECATE
203                <toolset>msvc:<define>_SCL_SECURE_NO_DEPRECATE
204              :
205              : <include>$(source-path) ] ;
206        }
207
208        local mt = [ new ac-library libjpeg : $(.project) : $(condition) ] ;
209        $(mt).set-header $(header) ;
210        $(mt).set-default-names $(names) ;
211        if $(target)
212        {
213            $(mt).set-target $(target) ;
214        }
215        targets.main-target-alternative $(mt) ;
216    } else {
217        if $(.debug)
218        {
219            ECHO "notice: [libjpeg] Using pre-installed library" ;
220            if $(condition)
221            {
222                ECHO "notice: [libjpeg] Condition" [ $(condition).raw ] ;
223            }
224        }
225
226        local mt = [ new ac-library libjpeg : $(.project) : $(condition) :
227            $(include-path) : $(library-path) : $(library-name) : $(root) ] ;
228        $(mt).set-header $(header) ;
229        $(mt).set-header-test $(header-test) ;
230        $(mt).set-default-names $(names) ;
231        targets.main-target-alternative $(mt) ;
232    }
233    .configured.$(condition) = true ;
234}
235