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 bzip library
9#
10# After 'using bzip2', the following targets are available:
11#
12# /bzip2//bzip2 -- The bzip library
13
14import project ;
15import ac ;
16import errors ;
17import feature ;
18import "class" : new ;
19import targets ;
20import path ;
21import modules ;
22import indirect ;
23import make ;
24import os ;
25import print ;
26import property ;
27import property-set ;
28
29header = bzlib.h ;
30names = bz2 ;
31
32sources = blocksort.c bzlib.c compress.c crctable.c
33          decompress.c huffman.c randtable.c ;
34
35library-id = 0 ;
36
37if --debug-configuration in [ modules.peek : ARGV ]
38{
39    .debug =  true ;
40}
41
42# Initializes the bzip library.
43#
44# bzip can be configured either to use pre-existing binaries
45# or to build the library from source.
46#
47# Options for configuring a prebuilt bzip::
48#
49#   <search>
50#       The directory containing the bzip binaries.
51#   <name>
52#       Overrides the default library name.
53#   <include>
54#       The directory containing the bzip headers.
55#
56# If none of these options is specified, then the environmental
57# variables BZIP2_LIBRARY_PATH, BZIP2_NAME, and BZIP2_INCLUDE will
58# be used instead.
59#
60# Options for building bzip from source::
61#
62#   <source>
63#       The bzip source directory.  Defaults to the environmental variable
64#       BZIP2_SOURCE.
65#   <tag>
66#       A rule which computes the actual name of the compiled
67#       libraries based on the build properties.  Ignored
68#       when using precompiled binaries.
69#   <build-name>
70#       The base name to use for the compiled library.  Ignored
71#       when using precompiled binaries.
72#
73# Examples::
74#
75#   # Find bzip in the default system location
76#   using bzip2 ;
77#   # Build bzip from source
78#   using bzip2 : 1.0.6 : <source>/home/sergey/src/bzip2-1.0.6 ;
79#   # Find bzip in /usr/local
80#   using bzip2 : 1.0.6
81#     : <include>/usr/local/include <search>/usr/local/lib ;
82#   # Build bzip from source for msvc and find
83#   # prebuilt binaries for gcc.
84#   using bzip2 : 1.0.6 : <source>C:/Devel/src/bzip2-1.0.6 : <toolset>msvc ;
85#   using bzip2 : 1.0.6 : : <toolset>gcc ;
86#
87rule init (
88    version ?
89    # The bzip version (currently ignored)
90
91    : options *
92    # A list of the options to use
93
94    : requirements *
95    # The requirements for the bzip target
96
97    : is-default ?
98    # Default configurations are only used when bzip
99    # has not yet been configured.  This option is
100    # deprecated.  A configuration will be treated
101    # as a default when none of <include>, <search>,
102    # <name>, and <source> are present.
103    )
104{
105    local caller = [ project.current ] ;
106
107    if ! $(.initialized)
108    {
109        .initialized = true ;
110
111        project.initialize $(__name__) ;
112        .project = [ project.current ] ;
113        project bzip2 ;
114    }
115
116    local library-path = [ feature.get-values <search> : $(options) ] ;
117    local include-path = [ feature.get-values <include> : $(options) ] ;
118    local source-path = [ feature.get-values <source> : $(options) ] ;
119    local library-name = [ feature.get-values <name> : $(options) ] ;
120    local tag = [ feature.get-values <tag> : $(options) ] ;
121    local build-name = [ feature.get-values <build-name> : $(options) ] ;
122
123    if ! $(library-path) && ! $(include-path) && ! $(source-path) && ! $(library-name)
124    {
125        is-default = true ;
126    }
127
128    condition = [ property-set.create $(requirements) ] ;
129    condition = [ property-set.create [ $(condition).base ] ] ;
130
131    local no-build-from-source ;
132    # Ignore environmental BZIP2_SOURCE if this initialization
133    # requested to search for a specific pre-built library.
134    if $(library-path) || $(include-path) || $(library-name)
135    {
136        if $(source-path) || $(tag) || $(build-name)
137        {
138            errors.user-error "incompatible options for bzip2:"
139                [ property.select <search> <include> <name> : $(options) ] "and"
140                [ property.select <source> <tag> <build-name> : $(options) ] ;
141        }
142    }
143    else
144    {
145        source-path ?= [ os.environ BZIP2_SOURCE ] ;
146        if $(source-path)
147        {
148            source-path = [ path.root [ path.make $(source-path) ]
149                                      [ path.pwd ] ] ;
150        }
151    }
152
153    if $(.configured.$(condition))
154    {
155        if $(is-default)
156        {
157            if $(.debug)
158            {
159                ECHO "notice: [bzip2] bzip is already configured" ;
160            }
161        }
162        else
163        {
164            errors.user-error "bzip is already configured" ;
165        }
166        return ;
167    }
168    else if $(source-path)
169    {
170        build-name ?= bz2 ;
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        def-file = [ path.glob $(source-path) : libbz2.def ] ;
179        if $(.debug)
180        {
181            ECHO "notice: [bzip2] Building bzip from source as $(build-name)" ;
182            if $(condition)
183            {
184                ECHO "notice: [bzip2] Condition" [ $(condition).raw ] ;
185            }
186            if $(sources)
187            {
188                ECHO "notice: [bzip2] found bzip source in $(source-path)" ;
189            }
190            else
191            {
192                ECHO "warning: [bzip2] could not find bzip source in $(source-path)" ;
193            }
194        }
195        local target ;
196        if $(sources)
197        {
198            if ! $(.def-file-target)
199            {
200                .def-file-target = [ targets.create-metatarget make-target-class
201                  : $(.project) : libbz2.def : $(def-file)
202                  : <action>@bzip2.make-bz2-def-file ]
203                  ;
204            }
205            target = [ targets.create-typed-target LIB : $(.project)
206              : $(build-name).$(library-id)
207              : $(sources)
208              : $(requirements)
209                <tag>@$(tag)
210                <include>$(source-path)
211                <toolset>msvc:<define>_CRT_SECURE_NO_DEPRECATE
212                <toolset>msvc:<define>_SCL_SECURE_NO_DEPRECATE
213                <link>shared:<def-file>libbz2.def
214              :
215              : <include>$(source-path) ] ;
216        }
217
218        local mt = [ new ac-library bzip2 : $(.project) : $(condition) ] ;
219        $(mt).set-header $(header) ;
220        $(mt).set-default-names $(names) ;
221        if $(target)
222        {
223            $(mt).set-target $(target) ;
224        }
225        targets.main-target-alternative $(mt) ;
226    }
227    else
228    {
229        if $(.debug)
230        {
231            ECHO "notice: [bzip2] Using pre-installed library" ;
232            if $(condition)
233            {
234                ECHO "notice: [bzip2] Condition" [ $(condition).raw ] ;
235            }
236        }
237
238        local mt = [ new ac-library bzip2 : $(.project) : $(condition) :
239            $(include-path) : $(library-path) : $(library-name) ] ;
240        $(mt).set-header $(header) ;
241        $(mt).set-default-names $(names) ;
242        targets.main-target-alternative $(mt) ;
243    }
244    .configured.$(condition) = true ;
245}
246
247if [ os.name ] = NT
248{
249    local rule read-file ( file )
250    {
251        return [ SPLIT_BY_CHARACTERS [ SHELL "type \"$(file:G=)\" 2>nul" ] : "\n" ] ;
252    }
253}
254else if [ os.name ] = VMS
255{
256    local rule read-file ( file )
257    {
258        return [ SPLIT_BY_CHARACTERS [ SHELL "PIPE TYPE $(file:W) 2>NL:" ] : "\n" ] ;
259    }
260}
261else
262{
263    local rule read-file ( file )
264    {
265        return [ SPLIT_BY_CHARACTERS [ SHELL "cat \"$(file:G=)\" 2>/dev/null" ]  : "\n" ] ;
266    }
267}
268
269rule make-bz2-def-file ( target : source : properties * )
270{
271    print.output $(target) ;
272    for local line in [ read-file $(source) ]
273    {
274        if ! [ MATCH "(LIBRARY[ \t]+LIBBZ2)" : $(line) ]
275        {
276            print.text $(line) : yes ;
277        }
278    }
279}
280