File: python.jam

package info (click to toggle)
boost-build 2.0-m11-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 4,624 kB
  • ctags: 2,387
  • sloc: ansic: 12,978; python: 5,209; xml: 4,782; cpp: 555; yacc: 456; sh: 237; makefile: 71
file content (547 lines) | stat: -rw-r--r-- 17,737 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
# Copyright 2004 Vladimir Prus.
# Distributed under the Boost Software License, Version 1.0. (See
# accompanying file LICENSE_1_0.txt or copy at
# http://www.boost.org/LICENSE_1_0.txt)

# Support for Python and the the Boost.Python library.
#
# This module defines
#
# - a project 'python' with a target 'python' in it, that corresponds
#   to the python library
#
# - a main target rule 'python-extension' which can be used
#   to build a python extension.
#
# Extensions that use Boost.Python must explicitly link to it.

# Known problems: 
# - the directory where extension is generated is different from V2
# - the ext + py -> pyd_run_output generator is declared to take 
#   SHARED_LIB, not PYTHON_EXTENSION. That's because we reuse 
#   'lib-target-class', which creates SHARED_LIB explicitly.




import type ;
import testing ;
import generators ;
import project ;
import errors ;
import targets ;
import "class" : new ;
import os ;
import common ;
import toolset : flags ;

# Make this module a project
project.initialize $(__name__) ;
project python ;

# Save the project so that if 'init' is called several
# times we define new targets in the python project,
# not in whatever project we were called by.
.project = [ project.current ] ;

# Dynamic linker lib. Necessary to specify it explicitly 
# on some platforms.
lib dl ;  
# This contains 'openpty' function need by python. Again, on
# some system need to pass this to linker explicitly.
lib util ;


# Initializes the Python toolset.
# - version -- the version of Python to use. Should be in Major.Minor format,
#   for example 2.3
# - 'root' -- the install root for Python
# - 'includes' -- the include path to Python headers. If empty, will be
#        guessed from 'root'
# - 'libraries' -- the path to Python libraries. If empty, will be guessed
#        from 'root'
# - 'cygwin-condition' -- if specified, should be a set of properties which
#   are present when we're building with cygwin gcc. 
#   This argument is not used yet.
#
# Example usage:
#
#   using python 2.3 ;  # Use default root
#   using python 2.3 : /usr/local ; # Root specified, include and lib paths
#                                   # will be guessed
#    
rule init ( version ? : root ? : includes ? : libraries ? 
    : cygwin-condition * )
{
    .configured = true ;
    
    project.push-current $(.project) ;
               
    if [ os.name ] = NT
    {
        init-nt $(version) : $(root) : $(includes) : $(libraries) : $(cygwin-condition) ;
    }
    else if [ os.name ] = MACOSX
    {
        init-mac $(version) : $(root) : $(includes) : $(libraries) ;
    } 
    else if [ modules.peek : UNIX ]
    {
        init-unix $(version) : $(root) : $(includes) : $(libraries) : $(cygwin-condition) ;
    }
    
    project.pop-current ;
}

rule init-unix ( version ? : root ? : includes ? : libraries ? : condition * )
{
    root ?= /usr ;
    includes ?= $(root)/include/python$(version) ;
    libraries ?= $(root)/lib/python$(version)/config ;
    
    # Find the 'python' binary, which is used for testing.
    # Look first in $(root)/bin, then in PATH.    
    local interpreter = [ common.get-invocation-command 
        python : python : : $(root)/bin : path-last ] ;
    
    if $(interpreter:D) != $(root)/bin
    {            
        ECHO "warning: was expecting Python interpreter in " $(root)/bin ;
        ECHO "warning: found only in PATH:" $(interpreter) ;
    }        
                
    if --debug-configuration in [ modules.peek : ARGV ]
    {
        ECHO "notice: Python include path is" $(includes) ;
        ECHO "notice: Python library path is" $(libraries) ;
        ECHO "notice: Python interpreter is" $(interpreter) ;                
    }
    
    # If not specific condition is specified, set global value
    # If condition is specified, set PYTHON on target. It will
    # override the global value.
    if ! $(condition)
    {
        PYTHON = $(interpreter) ;
    }
    else
    {              
        flags python.capture-output PYTHON $(condition:J=/) : $(interpreter) ;
    }   

    
    local extra-libs ;
    
    switch [ os.name ]
    {
        case SOLARIS : extra-libs = dl      ;
        case OSF :     extra-libs =         ;
        case QNXTO :   extra-libs = util    ;
        case * :       extra-libs = dl util ;
    }
    
    # On Linux, we don't want to link either Boost.Python or
    # Python extensions to libpython, so that when extensions
    # loaded in the interpreter, the symbols in the interpreter
    # are used. If we linked to libpython, we'd get duplicate
    # symbols. So declare two targets -- one for headers and another
    # for library.   
    alias python_for_extensions
                 :  
                 : $(condition)
                 :
                 : <include>$(includes) 
                 ;
    
    alias python
                 : $(extra-libs)
                 : $(condition)
                 :
                 : <include>$(includes) 
                   <library-path>$(libraries)
                   <find-shared-library>python$(version)
                 ;
    
    # NOTES:
    # - V1 had logic to force intel to use gcc's runtime.
    #   Note sure why that was needed, with icc 8.0 extensions
    #   built with intel are loaded by python without problems.
    # - There was 'python-static-multithread' logic. Don't know
    #   what it affected, so can't test.
    
    # TODO: need to figure out when the following code is needed:
    # for builtin extensions only or in some other cases too.

    
    #    if [ modules.peek $(OS) ] = OSF
    #    {
    #        PYTHON_PROPERTIES += <*><*><linkflags>"-expect_unresolved 'Py*' -expect_unresolved '_Py*'" ;
    #    }
    #    else if [ modules.peek $(OS) ] = AIX
    #    {
    #        PYTHON_PROPERTIES 
    #          += <*><*><linkflags>"-Wl,-bI:$(PYTHON_LIB_PATH)/python.exp"
    #            <*><*><find-library>pthreads ;
    #    }
}

rule init-mac ( version : root ? : includes ? : libraries ? )
{
    if ! $(root)
    {
        if [ GLOB /System/Library/Frameworks : Python.framework ]
        {
            root = /System/Library/Frameworks/Python.framework/Versions/$(version) ;
        }
        else
        {
            root = /Library/Frameworks/Python.framework/Versions/$(version) ;
        }
    }   

    # includes ?= $(PYTHON_ROOT)/include/python$(PYTHON_VERSION) ;
    includes ?= $(root)/include/python$(version) ;
    libraries ?= $(root)/lib/python$(version)/config ;

    # Find the 'python' binary, which is used for testing.
    # Look first in $(root)/bin, then in PATH.
    local interpreter = [ common.get-invocation-command 
        python : python : : $(root)/bin : path-last ] ;
    
    # debug support
    if --debug-configuration in [ modules.peek : ARGV ]
    {
        ECHO "notice: Python include path is" $(includes) ;
        ECHO "notice: Python library path is" $(libraries) ;
        ECHO "notice: Python interpreter is" $(interpreter) ;        
    }
    
    flags python.capture-output PYTHON : $(interpreter) ;
    
    PYTHON_FRAMEWORK ?= $(root) ;
    while $(PYTHON_FRAMEWORK:D=) && $(PYTHON_FRAMEWORK:D=) != Python.framework
    {
        PYTHON_FRAMEWORK = $(PYTHON_FRAMEWORK:D) ;
    }
    PYTHON_FRAMEWORK = $(PYTHON_FRAMEWORK:D)/Python ;

    alias python_for_extensions
      : 
      :
      : 
      : <include>$(includes)
      ;  
    
    alias python
      :
      : <os>MACOSX <toolset>darwin
      :
      : <include>$(includes) <framework>$(PYTHON_FRAMEWORK)
      ;  
}

.alias-defined = ;
rule init-nt ( version : root ? : includes ? : libraries ? : cygwin-condition ? )
{
#    PYTHON_PROPERTIES =
#      boost-python-disable-borland
#      select-nt-python-includes
#      <runtime-link>dynamic
#      <sysinclude>@boost
#      <$(gcc-compilers)><*><define>USE_DL_IMPORT
#      ;

    if ! $(cygwin-condition)
    {        
        root ?= c:/tools/python ;
        
        local PATH = [ modules.peek : PATH ] ;
        local PATH = [ modules.peek : Path ] ;        
        
        PYTHON_LIB_PATH ?= $(root)/libs [ GLOB $(root) : PCbuild ] ;
        
        PYTHON_INCLUDES ?= $(root)/include [ GLOB $(root) : PC ] ;
        
        PYTHON_DLL ?= [ GLOB $(PATH) $(Path) : python$(PYTHON_VERSION_NODOT).dll ] ;
        PYTHON_DEBUG_DLL ?= [ GLOB $(PATH) $(Path) : python$(PYTHON_VERSION_NODOT)_d.dll ] ;
        PYTHON_IMPORT_LIB ?= [ GLOB $(PYTHON_LIB_PATH) : libpython$(PYTHON_VERSION_NODOT).* ] ;
        PYTHON_DEBUG_IMPORT_LIB ?= [ GLOB $(PYTHON_LIB_PATH) : libpython$(PYTHON_VERSION_NODOT).* ] ;        
        
        
        local interpreter = [ common.get-invocation-command 
            python : python : : $(root)/bin 
                                          $(root)
                                          $(root)/PCBuild
                                            : path-last ] ;
        
        if --debug-configuration in [ modules.peek : ARGV ]
        {
            ECHO "notice: Python include path is" $(includes) ;
            ECHO "notice: Python library path is" $(libraries) ;
            ECHO "notice: Python interpreter is" $(interpreter) ;        
        }
        
        flags python.capture-output PYTHON : $(interpreter) ;

                
        # This is mingw-specific V1 code. I don't yet understand
        # why mingw must be specially-cased.
        #local lib = $(PYTHON_IMPORT_LIB) ;
        #if <define>BOOST_DEBUG_PYTHON in $(properties)
        #{
        #    lib = $(PYTHON_DEBUG_IMPORT_LIB) ;
        #}
        #lib ?= $(PYTHON_DLL) ;
        #if <define>BOOST_DEBUG_PYTHON in $(properties)
        #{
        #    lib ?= $(PYTHON_DEBUG_DLL) ;
        #}
        #properties += <library-file>$(lib) ;
        #}
        
        properties += <library-path>$(PYTHON_LIB_PATH) ;
            
        # msvc compilers auto-find the python library
        # declare two alternatives -- one for msvc and another
        # for the rest of the world
        alias python 
          :
          : <toolset>msvc 
          :
          : <library-path>$(PYTHON_LIB_PATH) 
            <include>$(PYTHON_INCLUDES)
          ;
                       
        local lib = python$(PYTHON_VERSION_NODOT) ;
        # TODO: don't support BOOST_DEBUG_PYTHON yet.        
        #    if <define>BOOST_DEBUG_PYTHON in $(properties)
        #    {
        #            lib = python$(PYTHON_VERSION_NODOT)_d ;
        #    }
        
        alias python
          :
          :
          :
          : <library-path>$(PYTHON_LIB_PATH)    
            <include>$(PYTHON_INCLUDES)
            <find-library>$(lib) ;                
    }
    else
    {        
        root ?= /usr ;  
        if $(root) = /usr
        {
            CYGWIN_PYTHON_DLL_PATH ?= /bin ;
        }
        else
        {
            CYGWIN_PYTHON_DLL_PATH ?= $(root)/bin ;
        }
        CYGWIN_PYTHON_LIB_PATH ?= $(CYGWIN_PYTHON_ROOT)/lib/python$(version)/config ;
      
        CYGWIN_PYTHON_DEBUG_VERSION ?= $(version) ;
        CYGWIN_PYTHON_DEBUG_ROOT ?= /usr/local/pydebug ;
        CYGWIN_PYTHON_DEBUG_DLL_PATH ?= $(CYGWIN_PYTHON_DEBUG_ROOT)/bin ;
        CYGWIN_PYTHON_DEBUG_LIB_PATH ?= $(CYGWIN_PYTHON_DEBUG_ROOT)/lib/python$(CYGWIN_PYTHON_DEBUG_VERSION)/config ;        
        
        local properties ;
        # TODO: don't support BOOST_DEBUG_PYTHON yet.                
        #if <define>BOOST_DEBUG_PYTHON in $(properties)
        #{
        #    properties += <library-path>$(CYGWIN_PYTHON_DEBUG_LIB_PATH) <find-library>python$(CYGWIN_PYTHON_DEBUG_VERSION).dll ;
        #}
        #else
        #{
        properties += <library-path>$(CYGWIN_PYTHON_LIB_PATH) 
                      <find-library>python$(CYGWIN_PYTHON_VERSION).dll ;
        
        properties += <include>$(root)/include/python$(version) ;          
          
        #}
        alias python
          :
          : $(cygwin-condition)
          :
          : $(properties)
          ;        
    }    
    
    if ! $(.alias-defined)
    {
        .alias-defined = true ;
        alias python_for_extensions : python ;
    }    
}


rule configured ( ) 
{
     return $(.configured) ;
}
        
type.register PYTHON_EXTENSION : : SHARED_LIB ;
# We can't give "dll" suffix to PYTHON_EXTENSION, because
# we would not know what "a.dll" is: python extenstion or
# ordinary library. Therefore, we specify only suffixes
# used for generation of targets.
type.set-generated-target-suffix PYTHON_EXTENSION : : so ;
type.set-generated-target-suffix PYTHON_EXTENSION : <os>NT : dll ;
type.set-generated-target-suffix PYTHON_EXTENSION : <os>CYGWIN : so ;

# Unset 'lib' prefix for PYTHON_EXTENSION
type.set-generated-target-prefix PYTHON_EXTENSION : : "" ;

rule python-extension ( name : sources * : requirements * : default-build * : 
                        usage-requirements * )
{
    requirements += <use>/python//python_for_extensions ;
        
    # TODO: handle the following V1 code
    #if $(OS) = MACOSX && $(toolset) = darwin
    #{
    #    if <target-type>PYD in $(properties)
    #    {
    #        properties += <link-format>bundle ;
    #    }
    #    properties += <framework>$(PYTHON_FRAMEWORK) ;
    #}    
               
#        <metrowerks><*><cxxflags>"-inline deferred" 
#      <cwpro8><*><cxxflags>"-inline deferred"  # added for internal testing purposes
#        <cxx><*><sysinclude>@boost/boost/compatibility/cpp_c_headers
#         <define>BOOST_PYTHON_DYNAMIC_LIB
    
    
#    PYTHON_PROPERTIES += 
#        <sysinclude>@boost
#          <stlport-iostream>on
#            select-python-library
    
#      boost-python-disable-borland
#      select-nt-python-includes
#      <runtime-link>dynamic
#      <sysinclude>@boost
#      <$(gcc-compilers)><*><define>USE_DL_IMPORT
#          <sysinclude>$(PYTHON_INCLUDES)
            
    local project = [ project.current ] ;

    
    targets.main-target-alternative
      [ new typed-target $(name) : $(project) : PYTHON_EXTENSION
        : [ targets.main-target-sources $(sources) : $(name) ]
        : [ targets.main-target-requirements $(requirements) : $(project) ] 
        : [ targets.main-target-default-build $(default-build) : $(project) ] 
      ] ;
}                            

IMPORT python : python-extension : : python-extension ;

# Support for testing
type.register PY : py ;
type.register RUN_PYD_OUTPUT ;
#type.set-generated-target-suffix RUN_PYD : : run ;
type.register RUN_PYD : : TEST ;

class python-test-generator : generator
{
    import set ;
    
    rule __init__ ( * : * )
    {
        generator.__init__ $(1) : $(2) : $(3) : $(4) : $(5) : $(6) : $(7) : $(8) : $(9) ;
        self.composing = true ;
    }
        
    rule run ( project name ? : property-set : sources * : multiple ? )
    {       
        local python ;
        for local s in $(sources)
        {
            if [ $(s).type ] = PY
            {
                python = $(s) ;
            }
        }
        
        local extensions ;
        for local s in $(sources)
        {
            if [ $(s).type ] = PYTHON_EXTENSION
            {
                extensions += $(s) ;
            }
        }
                
        local libs ;
        for local s in $(sources)
        {
            if [ type.is-derived [ $(s).type ] LIB ] 
              && ! $(s) in $(extensions)
            {
                libs += $(s) ;
            }
        }
        
        local new-sources ;
        for local s in $(sources)
        {
            if [ type.is-derived [ $(s).type ] CPP ] 
            {
                local name = [ utility.basename [ $(s).name ] ] ;
                if $(name) = [ utility.basename [ $(python).name ] ]
                {
                    name = $(name)_ext ;
                }          
                local extension = [ generators.construct $(project) $(name) :
                  PYTHON_EXTENSION : $(property-set) : $(s) $(libs) ] ;

                # The important part of usage requirements returned from
                # PYTHON_EXTENSION genrator are xdll-path properties that
                # will allow to find python extension at runtime.
                property-set = [ $(property-set).add $(extension[1]) ] ;
                # Ignore usage requirements. We're top-level generator and
                # nobody is going to use us.
                new-sources += $(extension[2-]) ;
            }
        }

            
        result = [ construct-result $(python) $(extensions) $(new-sources) 
          : $(project) $(name) : $(property-set) ] ;        
    }    
}

generators.register 
  [ new python-test-generator python.capture-output : : RUN_PYD_OUTPUT ] ;

generators.register-standard testing.expect-success 
  : RUN_PYD_OUTPUT : RUN_PYD ;


rule capture-output ( target : sources * : properties * )
{
    # Setup up proper DLL search path.
    # Here, $(sources[1]) is python module and $(sources[2]) is
    # DLL. Only $(sources[1]) is passed to testing.capture-output,
    # so RUN_PATH variable on $(sources[2]) is not consulted. Move it
    # over explicitly.
    RUN_PATH on $(sources[1]) = [ on $(sources[2]) return $(RUN_PATH) ] ;
    PYTHONPATH = [ on $(sources[2]) return $(LOCATE) ] ;
    testing.capture-output $(target) : $(sources[1]) : $(properties) ;
    local c = [ common.prepend-path-variable-command PYTHONPATH : $(PYTHONPATH) ] ;
    LAUNCHER on $(target) = $(c) [ on $(target) return $(PYTHON) ] ;
}

rule bpl-test ( name : sources * : requirements * )
{        
    sources ?= $(name).py $(name).cpp ;
    return [ testing.make-test 
        run-pyd : $(sources) /boost/python//boost_python
          : $(requirements) : $(name) ] ;
}

IMPORT $(__name__) : bpl-test : : bpl-test ;