1 # Copyright (c) 2010 Vladimir Prus.
2 # Copyright (c) 2013 Steven Watanabe
3 # Copyright (c) 2021 Rene Ferdinand Rivera Morell
5 # Use, modification and distribution is subject to the Boost Software
6 # License Version 1.0. (See accompanying file LICENSE.txt or
7 # https://www.bfgroup.xyz/b2/LICENSE.txt)
17 import virtual-target ;
23 project.initialize $(__name__) ;
24 .project = [ project.current ] ;
27 feature.feature ac.print-text : : free ;
29 rule generate-include ( target : sources * : properties * )
31 print.output $(target) ;
32 local text = [ property.select <ac.print-text> : $(properties) ] ;
35 print.text $(text:G=) : true ;
39 local header = [ property.select <include> : $(properties) ] ;
40 print.text "#include <$(header:G=)>\n" : true ;
44 rule generate-main ( target : sources * : properties * )
46 print.output $(target) ;
47 print.text "int main() {}" : true ;
50 rule find-include-path ( properties : header : provided-path ? : test-source ? )
52 if $(provided-path) && [ path.exists [ path.root $(header) $(provided-path) ] ]
54 return $(provided-path) ;
58 local a = [ class.new action : ac.generate-include : [ property-set.create <include>$(header) <ac.print-text>$(test-source) ] ] ;
59 # Create a new CPP target named after the header.
60 # Replace dots (".") in target basename for portability.
61 local basename = [ regex.replace $(header:D=) "[.]" "_" ] ;
62 local header-target = $(header:S=:B=$(basename)) ;
63 local cpp = [ class.new file-target $(header-target:S=.cpp) exact : CPP : $(.project) : $(a) ] ;
64 cpp = [ virtual-target.register $(cpp) ] ;
66 local result = [ generators.construct $(.project) $(header-target) : OBJ : $(properties) : $(cpp) : true ] ;
67 configure.maybe-force-rebuild $(result[2-]) ;
69 for local t in $(result[2-])
71 jam-targets += [ $(t).actualize ] ;
73 if [ UPDATE_NOW $(jam-targets) : [ modules.peek configure : .log-fd ]
81 rule construct-library ( name : property-set : provided-path ? )
83 local lib-props = [ $(property-set).add-raw <name>$(name) <search>$(provided-path) ] ;
84 return [ generators.construct $(.project) lib-$(name)
85 : SEARCHED_LIB : $(lib-props) : : true ] ;
89 rule find-library ( properties : names + : provided-path ? )
92 if [ $(properties).get <link> ] = shared
94 link-opts = <link>shared <link>static ;
98 link-opts = <link>static <link>shared ;
102 local names-iter = $(names) ;
103 properties = [ $(properties).refine [ property-set.create $(link-opts[1]) ] ] ;
106 local name = $(names-iter[1]) ;
107 local lib = [ construct-library $(name) : $(properties) : $(provided-path) ] ;
108 local a = [ class.new action : ac.generate-main :
109 [ property-set.empty ] ] ;
110 local main.cpp = [ virtual-target.register
111 [ class.new file-target main-$(name).cpp exact : CPP : $(.project) : $(a) ] ] ;
112 $(main.cpp).root true ;
113 local test = [ generators.construct $(.project) $(name) : EXE
114 : [ $(properties).add $(lib[1]) ] : $(main.cpp) $(lib[2-])
116 configure.maybe-force-rebuild $(test[2-]) ;
120 jam-targets += [ $(t).actualize ] ;
122 if [ UPDATE_NOW $(jam-targets) : [ modules.peek configure : .log-fd ]
125 result = $(name) $(link-opts[1]) ;
126 names-iter = ; link-opts = ; # break
128 names-iter = $(names-iter[2-]) ;
130 link-opts = $(link-opts[2-]) ;
135 class ac-library : basic-target
139 import virtual-target ;
142 import config-cache ;
145 rule __init__ ( name : project : requirements * : include-path ? : library-path ? : library-name ? )
147 basic-target.__init__ $(name) : $(project) : : $(requirements) ;
149 reconfigure $(include-path) : $(library-path) : $(library-name) ;
152 rule set-header ( header )
154 self.header = $(header) ;
157 rule set-default-names ( names + )
159 self.default-names = $(names) ;
162 rule set-header-test ( source )
164 self.header-test = $(source) ;
167 rule reconfigure ( include-path ? : library-path ? : library-name ? )
169 if $(include-path) || $(library-path) || $(library-name)
171 check-not-configured ;
173 self.include-path = $(include-path) ;
174 self.library-path = $(library-path) ;
175 self.library-name = $(library-name) ;
179 rule set-target ( target )
181 check-not-configured ;
182 self.target = $(target) ;
185 rule check-not-configured ( )
187 if $(self.include-path) || $(self.library-path) || $(self.library-name) || $(self.target)
189 errors.user-error [ name ] "is already configured" ;
193 rule construct ( name : sources * : property-set )
197 return [ $(self.target).generate $(property-set) ] ;
201 local use-environment ;
202 if ! $(self.library-name) && ! $(self.include-path) && ! $(self.library-path)
204 use-environment = true ;
206 local libnames = $(self.library-name) ;
207 if ! $(libnames) && $(use-environment)
209 libnames = [ os.environ $(name:U)_NAME ] ;
210 # Backward compatibility only.
211 libnames ?= [ os.environ $(name:U)_BINARY ] ;
213 libnames ?= $(self.default-names) ;
215 local include-path = $(self.include-path) ;
216 if ! $(include-path) && $(use-environment)
218 include-path = [ os.environ $(name:U)_INCLUDE ] ;
221 local library-path = $(self.library-path) ;
222 if ! $(library-path) && $(use-environment)
224 library-path = [ os.environ $(name:U)_LIBRARY_PATH ] ;
225 # Backwards compatibility only
226 library-path ?= [ os.environ $(name:U)_LIBPATH ] ;
229 local relevant = [ property.select [ configure.get-relevant-features ] <link> :
230 [ $(property-set).raw ] ] ;
231 local min = [ property.as-path [ SORT [ feature.minimize $(relevant) ] ] ] ;
233 local key = ac-library-$(name)-$(relevant:J=-) ;
234 local lookup = [ config-cache.get $(key) ] ;
238 if $(lookup) = missing
240 configure.log-library-search-result $(name) : "no (cached)" $(min) ;
241 return [ property-set.empty ] ;
245 local includes = $(lookup[1]) ;
246 if $(includes) = %default
250 local library = [ ac.construct-library $(lookup[2]) :
251 [ $(property-set).refine [ property-set.create $(lookup[3]) ] ] : $(library-path) ] ;
252 configure.log-library-search-result $(name) : "yes (cached)" $(min) ;
253 return [ $(library[1]).add-raw <include>$(includes) ] $(library[2-]) ;
258 local includes = [ ac.find-include-path $(property-set) : $(self.header) : $(include-path) : $(self.header-test) ] ;
259 local library = [ ac.find-library $(property-set) : $(libnames) : $(library-path) ] ;
260 if $(includes) && $(library)
262 config-cache.set $(key) : $(includes) $(library) ;
263 if $(includes) = %default
267 library = [ ac.construct-library $(library[1]) :
268 [ $(property-set).refine [ property-set.create $(library[2]) ] ] : $(library-path) ] ;
269 configure.log-library-search-result $(name) : "yes" $(min) ;
270 return [ $(library[1]).add-raw <include>$(includes) ] $(library[2-]) ;
274 config-cache.set $(key) : missing ;
275 configure.log-library-search-result $(name) : "no" $(min) ;
276 return [ property-set.empty ] ;
283 class check-library-worker
285 import property-set ;
289 rule __init__ ( target : true-properties * : false-properties * )
291 self.target = $(target) ;
292 self.true-properties = $(true-properties) ;
293 self.false-properties = $(false-properties) ;
296 rule check ( properties * )
299 local t = [ targets.current ] ;
300 local p = [ $(t).project ] ;
301 local ps = [ property-set.create $(properties) ] ;
302 ps = [ $(ps).propagated ] ;
304 [ targets.generate-from-reference $(self.target) : $(p) : $(ps) ] ;
307 choosen = $(self.true-properties) ;
311 choosen = $(self.false-properties) ;
313 return [ property.evaluate-conditionals-in-context $(choosen) :
318 rule check-library ( target : true-properties * : false-properties * )
320 local instance = [ class.new check-library-worker $(target) :
321 $(true-properties) : $(false-properties) ] ;
322 return <conditional>@$(instance).check
323 [ property.evaluate-conditional-relevance
324 $(true-properties) $(false-properties)
325 : [ configure.get-relevant-features ] <link> ] ;