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 zlib library
9#
10# After 'using zlib', the following targets are available:
11#
12# /zlib//zlib -- The zlib library
13
14import project ;
15import ac ;
16import errors ;
17import feature ;
18import "class" : new ;
19import targets ;
20import path ;
21import modules ;
22import indirect ;
23import os ;
24import property ;
25import property-set ;
26
27header = zlib.h ;
28names = z zlib zll zdll ;
29
30sources = adler32.c compress.c
31     crc32.c deflate.c gzclose.c gzio.c gzlib.c gzread.c gzwrite.c
32     infback.c inffast.c inflate.c inftrees.c trees.c uncompr.c zutil.c ;
33
34library-id = 0 ;
35
36if --debug-configuration in [ modules.peek : ARGV ]
37{
38    .debug =  true ;
39}
40
41# Initializes the zlib library.
42#
43# zlib can be configured either to use pre-existing binaries
44# or to build the library from source.
45#
46# Options for configuring a prebuilt zlib::
47#
48#   <search>
49#       The directory containing the zlib binaries.
50#   <name>
51#       Overrides the default library name.
52#   <include>
53#       The directory containing the zlib headers.
54#
55# If none of these options is specified, then the environmental
56# variables ZLIB_LIBRARY_PATH, ZLIB_NAME, and ZLIB_INCLUDE will
57# be used instead.
58#
59# Options for building zlib from source::
60#
61#   <source>
62#       The zlib source directory.  Defaults to the environmental variable
63#       ZLIB_SOURCE.
64#   <tag>
65#       A rule which computes the actual name of the compiled
66#       libraries based on the build properties.  Ignored
67#       when using precompiled binaries.
68#   <build-name>
69#       The base name to use for the compiled library.  Ignored
70#       when using precompiled binaries.
71#
72# Examples::
73#
74#   # Find zlib in the default system location
75#   using zlib ;
76#   # Build zlib from source
77#   using zlib : 1.2.7 : <source>/home/steven/zlib-1.2.7 ;
78#   # Find zlib in /usr/local
79#   using zlib : 1.2.7
80#     : <include>/usr/local/include <search>/usr/local/lib ;
81#   # Build zlib from source for msvc and find
82#   # prebuilt binaries for gcc.
83#   using zlib : 1.2.7 : <source>C:/Devel/src/zlib-1.2.7 : <toolset>msvc ;
84#   using zlib : 1.2.7 : : <toolset>gcc ;
85#
86rule init (
87    version ?
88    # The zlib version (currently ignored)
89
90    : options *
91    # A list of the options to use
92
93    : requirements *
94    # The requirements for the zlib target
95
96    : is-default ?
97    # Default configurations are only used when zlib
98    # has not yet been configured.  This option is
99    # deprecated.  A configuration will be treated
100    # as a default when none of <include>, <search>,
101    # <name>, and <source> are present.
102    )
103{
104    local caller = [ project.current ] ;
105
106    if ! $(.initialized)
107    {
108        .initialized = true ;
109
110        project.initialize $(__name__) ;
111        .project = [ project.current ] ;
112        project zlib ;
113    }
114
115    local library-path = [ feature.get-values <search> : $(options) ] ;
116    local include-path = [ feature.get-values <include> : $(options) ] ;
117    local source-path = [ feature.get-values <source> : $(options) ] ;
118    local library-name = [ feature.get-values <name> : $(options) ] ;
119    local tag = [ feature.get-values <tag> : $(options) ] ;
120    local build-name = [ feature.get-values <build-name> : $(options) ] ;
121
122    if ! $(library-path) && ! $(include-path) && ! $(source-path) && ! $(library-name)
123    {
124        is-default = true ;
125    }
126
127    condition = [ property-set.create $(requirements) ] ;
128    condition = [ property-set.create [ $(condition).base ] ] ;
129
130    # Ignore environmental ZLIB_SOURCE if this initialization
131    # requested to search for a specific pre-built library.
132    if $(library-path) || $(include-path) || $(library-name)
133    {
134        if $(source-path) || $(tag) || $(build-name)
135        {
136            errors.user-error "incompatible options for zlib:"
137                [ property.select <search> <include> <name> : $(options) ] "and"
138                [ property.select <source> <tag> <build-name> : $(options) ] ;
139        }
140    }
141    else
142    {
143        source-path ?= [ os.environ ZLIB_SOURCE ] ;
144        if $(source-path)
145        {
146            source-path = [ path.root [ path.make $(source-path) ]
147                                      [ path.pwd ] ] ;
148        }
149    }
150
151    if $(.configured.$(condition))
152    {
153        if $(is-default)
154        {
155            if $(.debug)
156            {
157                ECHO "notice: [zlib] zlib is already configured" ;
158            }
159        }
160        else
161        {
162            errors.user-error "zlib is already configured" ;
163        }
164        return ;
165    }
166    else if $(source-path)
167    {
168        build-name ?= z ;
169        library-id = [ CALC $(library-id) + 1 ] ;
170        tag = [ MATCH ^@?(.*)$ : $(tag) ] ;
171        if $(tag)
172        {
173            tag = [ indirect.make $(tag) : [ $(caller).project-module ] ] ;
174        }
175        sources = [ path.glob $(source-path) : $(sources) ] ;
176        if $(.debug)
177        {
178            ECHO "notice: [zlib] Building zlib from source as $(build-name)" ;
179            if $(condition)
180            {
181                ECHO "notice: [zlib] Condition" [ $(condition).raw ] ;
182            }
183            if $(sources)
184            {
185                ECHO "notice: [zlib] found zlib source in $(source-path)" ;
186            }
187            else
188            {
189                ECHO "warning: [zlib] could not find zlib source in $(source-path)" ;
190            }
191        }
192        local target ;
193        if $(sources)
194        {
195            target = [ targets.create-typed-target LIB : $(.project)
196              : $(build-name).$(library-id)
197              : $(sources)
198              : $(requirements)
199                <tag>@$(tag)
200                <include>$(source-path)
201                <toolset>msvc:<define>_CRT_SECURE_NO_DEPRECATE
202                <toolset>msvc:<define>_SCL_SECURE_NO_DEPRECATE
203                <link>shared:<define>ZLIB_DLL
204              :
205              : <include>$(source-path) ] ;
206        }
207
208        local mt = [ new ac-library zlib : $(.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    }
217    else
218    {
219        if $(.debug)
220        {
221            ECHO "notice: [zlib] Using pre-installed library" ;
222            if $(condition)
223            {
224                ECHO "notice: [zlib] Condition" [ $(condition).raw ] ;
225            }
226        }
227
228        local mt = [ new ac-library zlib : $(.project) : $(condition) :
229            $(include-path) : $(library-path) : $(library-name) ] ;
230        $(mt).set-header $(header) ;
231        $(mt).set-default-names $(names) ;
232        targets.main-target-alternative $(mt) ;
233    }
234    .configured.$(condition) = true ;
235}
236