1 # Copyright Rene Rivera 2015
2 # Distributed under the Boost Software License, Version 1.0.
3 # (See accompanying file LICENSE_1_0.txt or copy at
4 # http://www.boost.org/LICENSE_1_0.txt)
12 # Add a location, i.e. directory, where to search for libraries.
13 # The optional 'prefix' indicates which rooted-prefixes the new
14 # search dir applies to. The prefix defaults to '/'.
15 rule add-location ( dir prefix ? : base-dir ? )
21 # Dir path of caller to base paths from.
22 caller-module ?= [ CALLER_MODULE ] ;
23 local caller-dir = [ modules.peek $(caller-module) : __file__ ] ;
24 caller-dir = $(caller-dir:D) ;
26 base-dir ?= $(caller-dir) ;
28 .search-path-prefix += $(prefix) ;
29 .search-path.$(prefix) += [ path.root [ path.root $(dir) $(base-dir) ] [ path.pwd ] ] ;
32 # Declares additional definitions of a modular library target external
33 # to the modular library build itself. This makes it possible to externally
34 # define modular libraries without modifying the library. The passed in
35 # values are added on demand when the named library is first declared.
37 name : sources * : requirements * : default-build * :
38 usage-requirements * )
40 .external.($(name)).sources = $(sources) ;
41 .external.($(name)).requirements = $(requirements) ;
42 .external.($(name)).default-build = $(default-build) ;
43 .external.($(name)).usage-requirements = $(usage-requirements) ;
46 # Find, and declare, any modular libraries referenced in the target-refs.
47 # This will both load the modular libraries, and declare/manufacture
48 # the modular libraries as needed.
49 rule find ( target-refs + )
53 local caller-mod = [ CALLER_MODULE ] ;
54 local caller-dir = [ modules.peek $(caller-mod) : __file__ ] ;
55 caller-dir = $(caller-dir:D) ;
56 caller-dir = [ path.root $(caller-dir) [ path.pwd ] ] ;
59 for local target-ref in $(target-refs)
61 result-refs += [ resolve-reference $(target-ref)
62 : $(caller-mod) $(caller-dir) ] ;
65 return $(result-refs) ;
68 ##############################################################################
70 local rule resolve-reference ( target-ref : caller-mod caller-dir ? )
72 # ECHO %%% modular.resolve-target-ref $(target-ref) :: $(caller-mod) $(caller-dir) ;
75 caller-dir = [ modules.peek $(caller-mod) : __file__ ] ;
76 caller-dir = $(caller-dir:D) ;
77 caller-dir = [ path.root $(caller-dir) [ path.pwd ] ] ;
79 local result-ref = $(target-ref) ;
80 local ref = [ MATCH ^(.*)//.* : $(target-ref:G=) ] ;
81 # if ! ( $(ref) in $(.target-refs) )
83 # .target-refs += $(ref) ;
86 for local prefix in $(.search-path-prefix)
90 local search-match = [ MATCH ^($(prefix))/(.*)$ : $(ref) ] ;
91 search-prefix = $(search-match[1]) ;
92 search-sub = $(search-match[2]) ;
98 local found = [ path.glob $(.search-path.$(search-prefix)) : $(search-sub) ] ;
102 local lib-ref = [ regex.split $(search-sub) / ] ;
103 lib-ref = $(search-prefix)/$(lib-ref[1]) ;
104 local lib-path = [ path.relative-to $(caller-dir) $(found) ] ;
105 define-library $(lib-ref) $(caller-mod) : $(lib-path) ;
109 return $(result-ref) ;
112 local rule define-library ( name caller-module ? : root )
114 # ECHO ~~~ modular.library $(name) $(caller-module) :: $(root) :: $(depends) ;
118 # Dir path of caller to base paths from.
119 caller-module ?= [ CALLER_MODULE ] ;
120 local caller-dir = [ modules.peek $(caller-module) : __file__ ] ;
121 caller-dir = $(caller-dir:D) ;
123 # Find the various parts of the library.
124 local lib-dir = [ path.root [ path.root $(root) $(caller-dir) ] [ path.pwd ] ] ;
125 local lib-contents = [ path.glob $(lib-dir) : "include" "build" ] ;
126 lib-contents = $(lib-contents:D=) ;
128 # "include" dir for library..
130 if "include" in $(lib-contents)
132 include-dir = $(root)/include ;
135 # If it has a build dir, i.e. it has targets to build,
136 # we root the project at the build dir to make it easy
137 # to refer to the build targets. This mirrors the regular
138 # Boost organization of the project aliases.
139 if "build" in $(lib-contents)
141 root = $(root)/build ;
145 # Shadow target declarations so that we can alter build targets
146 # to work in the standalone modular structure.
147 local lib-location = [ path.root [ path.make $(root) ] $(caller-dir) ] ;
148 local lib-module-name = [ project.module-name $(lib-location) ] ;
149 local modular-rules = [ RULENAMES modular-rules ] ;
150 IMPORT modular-rules : $(modular-rules) : $(lib-module-name) : $(modular-rules) ;
152 # Load/create/declare library project.
153 local lib-module = [ project.find $(root) : $(caller-dir) ] ;
156 # If the find was unable to load the project we synthesize it.
157 lib-module = [ project.load $(lib-location) : synthesize ] ;
159 local lib-target = [ project.target $(lib-module) ] ;
160 if ! [ modules.peek $(lib-module) : __library__ ]
162 modules.poke $(lib-module) : __library__ : $(name) ;
163 for local type in [ modules.peek type : .types ]
165 main-rule-name = [ type.type-to-rule-name $(type) ] ;
166 IMPORT modular-rules : main-target-rule : $(lib-module-name) : $(main-rule-name) ;
170 # Declare project alternate ID.
171 modules.call-in $(caller-module) : use-project $(name) : $(root) ;
173 # Create a "library" target that has basic usage info if needed.
174 if ! [ $(lib-target).has-alternative-for-target library ]
176 include-dir = [ path.relative-to $(root) $(include-dir) ] ;
178 project.push-current $(lib-target) ;
180 # Declare the library alias.
181 modules.call-in $(lib-module) : library
185 : # Usage Requirements
186 <include>$(include-dir)
189 project.pop-current ;
193 local rule process-args ( )
195 if ! $(.did-process-args)
197 .did-process-args = yes ;
198 local argv = [ modules.peek : ARGV ] ;
199 local dirs = [ MATCH ^--modular-search-dir=(.*)$ : $(argv) ] ;
200 for local dir in $(dirs)
202 add-location $(dir) : [ path.pwd ] ;
207 rule apply-external (
208 mod : field : values * )
211 local name = [ modules.peek $(mod) : __library__ ] ;
212 values += $(.external.($(name)).$(field)) ;
213 for local value in $(values)
215 result += [ resolve-reference $(value) : $(mod) ] ;
227 # Avoids any form of installation for Boost modules.
228 rule boost-install ( libraries * ) { }
230 # Generic typed target rule to pre-process main target
231 # declarations to make them work within the standalone
233 rule main-target-rule (
234 name : sources * : requirements * : default-build * :
235 usage-requirements * )
237 local mod = [ CALLER_MODULE ] ;
239 # ECHO @@@ [[$(mod)]] modular-rules.main-target-rule $(name) :: $(sources) :: $(requirements) :: $(default-build) :: $(usage-requirements) ;
241 # First discover the required target type based on the exact alias used to
243 local bt = [ BACKTRACE 1 ] ;
244 local rulename = $(bt[4]) ;
245 local target-type = [ type.type-from-rule-name $(rulename) ] ;
246 return [ targets.create-typed-target $(target-type) : [ project.current ] :
247 $(name) : $(sources) : $(requirements) : $(default-build) :
248 $(usage-requirements) ] ;
251 rule lib ( names + : sources * : requirements * : default-build * :
252 usage-requirements * )
254 local mod = [ CALLER_MODULE ] ;
255 requirements += <use>library ;
256 usage-requirements += <use>library ;
258 # ECHO @@@ [[$(mod)]] modular-rules.lib $(names) :: $(sources) :: $(requirements) :: $(default-build) :: $(usage-requirements) ;
259 return [ builtin.lib $(names) : $(sources) : $(requirements) : $(default-build) : $(usage-requirements) ] ;
262 rule alias ( name : sources * : requirements * : default-build * :
263 usage-requirements * )
265 local mod = [ CALLER_MODULE ] ;
267 # ECHO @@@ [[$(mod)]] modular-rules.alias $(name) :: $(sources) :: $(requirements) :: $(default-build) :: $(usage-requirements) ;
268 return [ alias.alias $(name) : $(sources) : $(requirements) : $(default-build) : $(usage-requirements) ] ;
271 rule library ( name ? : sources * : requirements * : default-build * :
272 usage-requirements * )
276 local mod = [ CALLER_MODULE ] ;
277 sources = [ modular.apply-external $(mod) : sources : $(sources) ] ;
278 requirements = [ modular.apply-external $(mod) : requirements : $(requirements) ] ;
279 default-build = [ modular.apply-external $(mod) : default-build : $(default-build) ] ;
280 usage-requirements = [ modular.apply-external $(mod) : usage-requirements : $(usage-requirements) ] ;
284 # ECHO @@@ [[$(mod)]] modular-rules.library $(name) :: $(sources) :: $(requirements) :: $(default-build) :: $(usage-requirements) ;
285 return [ alias.alias $(name) : $(sources) : $(requirements) : $(default-build) : $(usage-requirements) ] ;