1 # Copyright 2004 Vladimir Prus.
2 # Distributed under the Boost Software License, Version 1.0. (See
3 # accompanying file LICENSE_1_0.txt or copy at
4 # http://www.boost.org/LICENSE_1_0.txt)
6 # Support for Python and the the Boost.Python library.
10 # - a project 'python' with a target 'python' in it, that corresponds to the
13 # - a main target rule 'python-extension' which can be used to build a python
16 # Extensions that use Boost.Python must explicitly link to it.
24 import "class" : new ;
40 # Make this module a project.
41 project.initialize $(__name__) ;
44 # Save the project so that if 'init' is called several times we define new
45 # targets in the python project, not in whatever project we were called by.
46 .project = [ project.current ] ;
48 # Dynamic linker lib. Necessary to specify it explicitly on some platforms.
50 # This contains 'openpty' function need by python. Again, on some system need to
51 # pass this to linker explicitly.
53 # Python uses pthread symbols.
55 : <target-os>linux:<link>shared
58 # Extra library needed by phtread on some platforms.
61 # The pythonpath feature specifies additional elements for the PYTHONPATH
62 # environment variable, set by run-pyd. For example, pythonpath can be used to
63 # access Python modules that are part of the product being built, but are not
64 # installed in the development system's default paths.
65 feature.feature pythonpath : : free optional path ;
67 # The best configured version of Python 2 and 3.
71 # Initializes the Python toolset. Note that all parameters are optional.
73 # - version -- the version of Python to use. Should be in Major.Minor format,
74 # for example 2.3. Do not include the subminor version.
76 # - cmd-or-prefix: Preferably, a command that invokes a Python interpreter.
77 # Alternatively, the installation prefix for Python libraries and includes. If
78 # empty, will be guessed from the version, the platform's installation
79 # patterns, and the python executables that can be found in PATH.
81 # - includes: the include path to Python headers. If empty, will be guessed.
83 # - libraries: the path to Python library binaries. If empty, will be guessed.
84 # On MacOS/Darwin, you can also pass the path of the Python framework.
86 # - condition: if specified, should be a set of properties that are matched
87 # against the build configuration when B2 selects a Python
88 # configuration to use.
90 # - extension-suffix: A string to append to the name of extension modules before
91 # the true filename extension. Ordinarily we would just compute this based on
92 # the value of the <python-debugging> feature. However ubuntu's python-dbg
93 # package uses the windows convention of appending _d to debug-build extension
94 # modules. We have no way of detecting ubuntu, or of probing python for the
95 # "_d" requirement, and if you configure and build python using
96 # --with-pydebug, you'll be using the standard *nix convention. Defaults to ""
97 # (or "_d" when targeting windows and <python-debugging> is set).
101 # using python : 2.3 ;
102 # using python : 2.3 : /usr/local/bin/python ;
104 rule init ( version ? : cmd-or-prefix ? : includes * : libraries ?
105 : condition * : extension-suffix ? )
107 project.push-current $(.project) ;
109 debug-message Configuring python... ;
110 for local v in version cmd-or-prefix includes libraries condition
114 debug-message " user-specified $(v):" \"$($(v))\" ;
118 configure $(version) : $(cmd-or-prefix) : $(includes) : $(libraries) : $(condition) : $(extension-suffix) ;
120 project.pop-current ;
123 # A simpler version of SHELL that grabs stderr as well as stdout, but returns
124 # nothing if there was an error.
126 local rule shell-cmd ( cmd )
128 debug-message running command '$(cmd)" 2>&1"' ;
129 x = [ SHELL $(cmd)" 2>&1" : exit-status ] ;
141 # Try to identify Cygwin symlinks. Invoking such a file directly as an NT
142 # executable from a native Windows build of bjam would be fatal to the bjam
143 # process. One /can/ invoke them through sh.exe or bash.exe, if you can prove
144 # that those are not also symlinks. ;-)
146 # If a symlink is found returns non-empty; we try to extract the target of the
147 # symlink from the file and return that.
149 # Note: 1. only works on NT 2. path is a native path.
150 local rule is-cygwin-symlink ( path )
154 # Look for a file with the given path having the S attribute set, as cygwin
155 # symlinks do. /-C means "do not use thousands separators in file sizes."
156 local dir-listing = [ shell-cmd "DIR /-C /A:S \""$(path)"\"" ] ;
160 # Escape any special regex characters in the base part of the path.
161 local base-pat = [ regex.escape $(path:D=) : "].[()*+?|\\$^" : \\ ] ;
163 # Extract the file's size from the directory listing.
164 local size-of-system-file = [ MATCH "([0-9]+) "$(base-pat) : $(dir-listing) : 1 ] ;
166 # If the file has a reasonably small size, look for the special symlink
167 # identification text.
168 if $(size-of-system-file) && [ numbers.less $(size-of-system-file) 1000 ]
170 local link = [ SHELL "FIND /OFF \"!<symlink>\" \""$(path)"\" 2>&1" ] ;
176 is-symlink = [ MATCH ".*!<symlink>([^"$(nl)"]*)" : $(link[1]) : 1 ] ;
179 is-symlink = [ *nix-path-to-native $(is-symlink) ] ;
180 is-symlink = $(is-symlink:R=$(path:D)) ;
186 return $(is-symlink) ;
190 # Append ext to each member of names that does not contain '.'.
192 local rule default-extension ( names * : ext * )
195 for local n in $(names)
199 case *.* : result += $(n) ;
200 case * : result += $(n)$(ext) ;
207 # Tries to determine whether invoking "cmd" would actually attempt to launch a
210 # Note: only works on NT.
212 local rule invokes-cygwin-symlink ( cmd )
214 local dirs = $(cmd:D) ;
217 dirs = . [ os.executable-path ] ;
219 local base = [ default-extension $(cmd:D=) : .exe .cmd .bat ] ;
220 local paths = [ GLOB $(dirs) : $(base) ] ;
223 # Make sure we have not run into a Cygwin symlink. Invoking such a file
224 # as an NT executable would be fatal for the bjam process.
225 return [ is-cygwin-symlink $(paths[1]) ] ;
230 local rule debug-message ( message * )
232 if --debug-configuration in [ modules.peek : ARGV ]
234 ECHO "notice:" "[python-cfg]" $(message) ;
239 # Like W32_GETREG, except prepend HKEY_CURRENT_USER\SOFTWARE and
240 # HKEY_LOCAL_MACHINE\SOFTWARE to the first argument, returning the first result
241 # found. Also accounts for the fact that on 64-bit machines, 32-bit software has
242 # its own area, under SOFTWARE\Wow6432node.
244 local rule software-registry-value ( path : data ? )
247 for local root in HKEY_CURRENT_USER HKEY_LOCAL_MACHINE
249 for local x64elt in "" Wow6432node\\ # Account for 64-bit windows
253 result = [ W32_GETREG $(root)\\SOFTWARE\\$(x64elt)$(path) : $(data) ] ;
262 .windows-drive-letter-re = "^([A-Za-z]):[\\/](.*)" ;
263 .cygwin-drive-letter-re = "^/cygdrive/([a-z])/(.*)" ;
265 .working-directory = [ PWD ] ;
266 .working-drive-letter = [ SUBST $(.working-directory) $(.windows-drive-letter-re) $1 ] ;
267 .working-drive-letter ?= [ SUBST $(.working-directory) $(.cygwin-drive-letter-re) $1 ] ;
270 local rule windows-to-cygwin-path ( path )
272 # If path is rooted with a drive letter, rewrite it using the /cygdrive
274 local p = [ SUBST $(path:T) $(.windows-drive-letter-re) /cygdrive/$1/$2 ] ;
276 # Else if path is rooted without a drive letter, use the working directory.
277 p ?= [ SUBST $(path:T) ^/(.*) /cygdrive/$(.working-drive-letter:L)/$2 ] ;
279 # Else return the path unchanged.
280 return $(p:E=$(path:T)) ;
284 # :W only works in Cygwin builds of bjam. This one works on NT builds as well.
286 local rule cygwin-to-windows-path ( path )
288 path = $(path:R="") ; # strip any trailing slash
290 local drive-letter = [ SUBST $(path) $(.cygwin-drive-letter-re) "$1:/$2" ] ;
293 path = $(drive-letter) ;
295 else if $(path:R=/x) = $(path) # already rooted?
297 # Look for a cygwin mount that includes each head sequence in $(path).
298 local head = $(path) ;
303 local root = [ software-registry-value
304 "Cygnus Solutions\\Cygwin\\mounts v2\\"$(head) : native ] ;
308 path = $(tail:R=$(root)) ;
311 tail = $(tail:R=$(head:D=)) ;
323 return [ regex.replace $(path:R="") / \\ ] ;
327 # Convert a *nix path to native.
329 local rule *nix-path-to-native ( path )
333 path = [ cygwin-to-windows-path $(path) ] ;
339 # Convert an NT path to native.
341 local rule windows-path-to-native ( path )
349 return [ windows-to-cygwin-path $(path) ] ;
354 # Return nonempty if path looks like a windows path, i.e. it starts with a drive
355 # letter or contains backslashes.
357 local rule guess-windows-path ( path )
359 return [ SUBST $(path) "($(.windows-drive-letter-re)|.*([\\]).*)" $1 ] ;
363 local rule path-to-native ( paths * )
367 for local p in $(paths)
369 if [ guess-windows-path $(p) ]
371 result += [ windows-path-to-native $(p) ] ;
375 result += [ *nix-path-to-native $(p:T) ] ;
382 # Validate the version string and extract the major/minor part we care about.
384 local rule split-version ( version )
386 local major-minor = [ MATCH "^([0-9]+)\.([0-9]+)(.*)$" : $(version) : 1 2 3 ] ;
387 if ! $(major-minor[2]) || $(major-minor[3])
389 ECHO "Warning: \"using python\" expects a two part (major, minor) version number; got" $(version) instead ;
391 # Add a zero to account for the missing digit if necessary.
395 return $(major-minor[1]) $(major-minor[2]) ;
399 # Build a list of versions from 3.4 down to 1.5. Because bjam can not enumerate
400 # registry sub-keys, we have no way of finding a version with a 2-digit minor
401 # version, e.g. 2.10 -- let us hope that never happens.
403 .version-countdown = ;
404 for local v in [ numbers.range 15 34 ]
406 .version-countdown = [ SUBST $(v) (.)(.*) $1.$2 ] $(.version-countdown) ;
410 local rule windows-installed-pythons ( version ? )
412 version ?= $(.version-countdown) ;
415 for local v in $(version)
417 local install-path = [
418 software-registry-value "Python\\PythonCore\\"$(v)"\\InstallPath" ] ;
422 install-path = [ windows-path-to-native $(install-path) ] ;
423 debug-message Registry indicates Python $(v) installed at \"$(install-path)\" ;
426 interpreters += $(:E=python:R=$(install-path)) ;
428 return $(interpreters) ;
432 local rule darwin-installed-pythons ( version ? )
434 version ?= $(.version-countdown) ;
437 = [ GLOB /System/Library/Frameworks /Library/Frameworks
438 : Python.framework ] ;
440 return $(prefix)/Versions/$(version)/bin/python ;
444 # Assume "python-cmd" invokes a python interpreter and invoke it to extract all
445 # the information we care about from its "sys" module. Returns void if
448 local rule probe ( python-cmd )
450 # Avoid invoking a Cygwin symlink on NT.
454 skip-symlink = [ invokes-cygwin-symlink $(python-cmd) ] ;
459 debug-message -------------------------------------------------------------------- ;
460 debug-message \"$(python-cmd)\" would attempt to invoke a Cygwin symlink, ;
461 debug-message causing a bjam built for Windows to hang. ;
463 debug-message If you intend to target a Cygwin build of Python, please ;
464 debug-message replace the path to the link with the path to a real executable ;
465 debug-message "(guessing:" \"$(skip-symlink)\") "in" your 'using python' line ;
466 debug-message "in" user-config.jam or site-config.jam. Do not forget to escape ;
467 debug-message backslashes ;
468 debug-message -------------------------------------------------------------------- ;
472 # Prepare a List of Python format strings and expressions that can be
473 # used to print the constants we want from the sys module.
475 # We do not really want sys.version since that is a complicated string,
476 # so get the information from sys.version_info instead.
477 local format = "version=%d.%d" ;
478 local exprs = "version_info[0]" "version_info[1]" ;
480 for local s in $(sys-elements[2-])
486 # Invoke Python and ask it for all those values.
488 $(python-cmd)" -c \"from sys import *; print('"$(format:J=\\n)"' % ("$(exprs:J=,)"))\"" ;
490 local output = [ shell-cmd $(full-cmd) ] ;
493 # Parse the output to get all the results.
497 for s in $(sys-elements)
499 # These variables are expected to be declared local in the
500 # caller, so Jam's dynamic scoping will set their values there.
501 sys.$(s) = [ SUBST $(output) "\\<$(s)=([^$(nl)]+)" $1 ] ;
509 # Make sure the "libraries" and "includes" variables (in an enclosing scope)
510 # have a value based on the information given.
512 local rule compute-default-paths ( target-os : version ? : prefix ? :
515 exec-prefix ?= $(prefix) ;
517 if $(target-os) = windows
519 # The exec_prefix is where you're supposed to look for machine-specific
521 local default-library-path = $(exec-prefix)\\libs ;
522 local default-include-path = $(:E=Include:R=$(prefix)) ;
524 # If the interpreter was found in a directory called "PCBuild" or
525 # "PCBuild8," assume we're looking at a Python built from the source
526 # distro, and go up one additional level to the default root. Otherwise,
527 # the default root is the directory where the interpreter was found.
529 # We ask Python itself what the executable path is in case of
530 # intermediate symlinks or shell scripts.
531 local executable-dir = $(sys.executable:D) ;
533 if [ MATCH ^(PCBuild) : $(executable-dir:D=) ]
535 debug-message "This Python appears to reside in a source distribution;" ;
536 debug-message "prepending \""$(executable-dir)"\" to default library search path" ;
538 default-library-path = $(executable-dir) $(default-library-path) ;
540 default-include-path = $(:E=PC:R=$(executable-dir:D)) $(default-include-path) ;
542 debug-message "and \""$(default-include-path[1])"\" to default #include path" ;
545 libraries ?= $(default-library-path) ;
546 includes ?= $(default-include-path) ;
550 includes ?= $(prefix)/include/python$(version) ;
552 local lib = $(exec-prefix)/lib ;
553 libraries ?= $(lib)/python$(version)/config $(lib) ;
557 # The version of the python interpreter to use.
558 feature.feature python : : propagated symmetric ;
559 feature.feature python.interpreter : : free ;
561 toolset.flags python.capture-output PYTHON : <python.interpreter> ;
564 # Support for Python configured --with-pydebug
566 feature.feature python-debugging : off on : propagated ;
567 variant debug-python : debug : <python-debugging>on ;
570 # Return a list of candidate commands to try when looking for a Python
571 # interpreter. prefix is expected to be a native path.
573 local rule candidate-interpreters ( version ? : prefix ? : target-os )
575 local bin-path = bin ;
576 if $(target-os) = windows
578 # On Windows, look in the root directory itself and, to work with the
579 # result of a build-from-source, the PCBuild directory.
580 bin-path = PCBuild8 PCBuild "" ;
583 bin-path = $(bin-path:R=$(prefix)) ;
585 if $(target-os) in windows darwin
588 $(:E=python:R=$(bin-path)) # Relative to the prefix, if any
590 [ $(target-os)-installed-pythons $(version) ] # Standard install locations
595 # Search relative to the prefix, or if none supplied, in PATH.
596 local unversioned = $(:E=python:R=$(bin-path:E=)) ;
598 # If a version was specified, look for a python with that specific
599 # version appended before looking for one called, simply, "python"
600 return $(unversioned)$(version) $(unversioned) ;
605 # Compute system library dependencies for targets linking with static Python
608 # On many systems, Python uses libraries such as pthreads or libdl. Since static
609 # libraries carry no library dependency information of their own that the linker
610 # can extract, these extra dependencies have to be given explicitly on the link
611 # line of the client. The information about these dependencies is packaged into
612 # the "python" target below.
614 # Even where Python itself uses pthreads, it never allows extension modules to
615 # be entered concurrently (unless they explicitly give up the interpreter lock).
616 # Therefore, extension modules do not need the efficiency overhead of threadsafe
617 # code as produced by <threading>multi, and we handle libpthread along with
618 # other libraries here. Note: this optimization is based on an assumption that
619 # the compiler generates link-compatible code in both the single- and
620 # multi-threaded cases, and that system libraries do not change their ABIs
623 # Returns a list of usage-requirements that link to the necessary system
626 local rule system-library-dependencies ( target-os )
630 case s[uo][nl]* : # solaris, sun, sunos
631 # Add a librt dependency for the gcc toolset on SunOS (the sun
632 # toolset adds -lrt unconditionally). While this appears to
633 # duplicate the logic already in gcc.jam, it does not as long as
634 # we are not forcing <threading>multi.
636 # On solaris 10, distutils.sysconfig.get_config_var('LIBS') yields
637 # '-lresolv -lsocket -lnsl -lrt -ldl'. However, that does not seem
638 # to be the right list for extension modules. For example, on my
639 # installation, adding -ldl causes at least one test to fail because
640 # the library can not be found and removing it causes no failures.
642 # Apparently, though, we need to add -lrt for gcc.
643 return <toolset>gcc:<library>rt ;
645 case osf : return <library>pthread <toolset>gcc:<library>rt ;
648 case darwin : return ;
649 case windows : return ;
650 case haiku : return ;
652 case hpux : return <library>rt ;
653 case *bsd : return <library>pthread <toolset>gcc:<library>util ;
655 case aix : return <library>pthread <library>dl ;
657 case * : return <library>pthread <library>dl
658 <toolset>gcc:<library>util <toolset-intel:platform>linux:<library>util ;
663 # Define a version suffix for libraries depending on Python.
664 # For example, Boost.Python built for Python 2.7 uses the suffix "27"
665 rule version-suffix ( version )
667 local major-minor = [ split-version $(version) ] ;
668 local suffix = $(major-minor:J="") ;
672 # Declare a target to represent Python's library.
674 local rule declare-libpython-target ( version ? : requirements * )
676 # Compute the representation of Python version in the name of Python's
678 local lib-version = $(version) ;
679 if <target-os>windows in $(requirements)
681 local major-minor = [ split-version $(version) ] ;
682 lib-version = $(major-minor:J="") ;
683 if <python-debugging>on in $(requirements)
685 lib-version = $(lib-version)_d ;
691 ECHO *** "warning:" could not determine Python version, which will ;
692 ECHO *** "warning:" probably prevent us from linking with the python ;
693 ECHO *** "warning:" library. Consider explicitly passing the version ;
694 ECHO *** "warning:" to 'using python'. ;
698 lib python.lib : : <name>python$(lib-version) $(requirements) ;
702 # Implementation of init.
703 local rule configure ( version ? : cmd-or-prefix ? : includes * : libraries ? :
704 condition * : extension-suffix ? )
709 local interpreter-cmd ;
711 local target-os = [ feature.get-values target-os : $(condition) ] ;
712 target-os ?= [ feature.defaults target-os ] ;
713 target-os = $(target-os:G=) ;
715 if $(target-os) = windows && <python-debugging>on in $(condition)
717 extension-suffix ?= _d ;
719 extension-suffix ?= "" ;
723 if ! $(cmd-or-prefix) || [ GLOB $(cmd-or-prefix) : * ]
725 # If the user did not pass a command, whatever we got was a prefix.
726 prefix = $(cmd-or-prefix) ;
727 cmds-to-try = [ candidate-interpreters $(version) : $(prefix) : $(target-os) ] ;
731 # Work with the command the user gave us.
732 cmds-to-try = $(cmd-or-prefix) ;
734 # On Windows, do not nail down the interpreter command just yet in case
735 # the user specified something that turns out to be a cygwin symlink,
736 # which could bring down bjam if we invoke it.
737 if $(target-os) != windows
739 interpreter-cmd = $(cmd-or-prefix) ;
743 # Values to use in case we can not really find anything in the system.
744 local fallback-cmd = $(cmds-to-try[1]) ;
745 local fallback-version ;
747 # Anything left to find or check?
748 if ! ( $(interpreter-cmd) && $(version) && $(includes) && $(libraries) )
750 # Values to be extracted from python's sys module. These will be set by
751 # the probe rule, above, using Jam's dynamic scoping.
752 local sys-elements = version platform prefix exec_prefix executable ;
753 local sys.$(sys-elements) ;
755 # Compute the string Python's sys.platform needs to match. If not
756 # targeting Windows or cygwin we will assume only native builds can
757 # possibly run, so we will not require a match and we leave sys.platform
762 case windows : platform = win32 ;
763 case cygwin : platform = cygwin ;
769 local cmd = $(cmds-to-try[1]) ;
770 cmds-to-try = $(cmds-to-try[2-]) ;
772 debug-message Checking interpreter command \"$(cmd)\"... ;
775 fallback-version ?= $(sys.version) ;
777 # Check for version/platform validity.
778 for local x in version platform
780 if $($(x)) && $($(x)) != $(sys.$(x))
782 debug-message ...$(x) "mismatch (looking for"
783 $($(x)) but found $(sys.$(x))")" ;
790 debug-message ...requested configuration matched! ;
792 exec-prefix = $(sys.exec_prefix) ;
794 compute-default-paths $(target-os) : $(sys.version) :
795 $(sys.prefix) : $(sys.exec_prefix) ;
797 version = $(sys.version) ;
798 interpreter-cmd ?= $(cmd) ;
799 cmds-to-try = ; # All done.
804 debug-message ...does not invoke a working interpreter ;
809 # Check whether configuration succeeded.
810 if ! ( $(includes) && $(libraries) )
812 debug-message Python headers and libraries not found. ;
818 if ! $(interpreter-cmd)
820 fallback-cmd ?= python ;
821 debug-message No working Python interpreter found. ;
822 if [ os.name ] != NT || ! [ invokes-cygwin-symlink $(fallback-cmd) ]
824 interpreter-cmd = $(fallback-cmd) ;
825 debug-message falling back to \"$(interpreter-cmd)\" ;
829 includes = [ path-to-native $(includes) ] ;
830 libraries = [ path-to-native $(libraries) ] ;
832 debug-message "Details of this Python configuration:" ;
833 debug-message " interpreter command:" \"$(interpreter-cmd:E=<empty>)\" ;
834 debug-message " include path:" \"$(includes:E=<empty>)\" ;
835 debug-message " library path:" \"$(libraries:E=<empty>)\" ;
836 if $(target-os) = windows
838 debug-message " DLL search path:" \"$(exec-prefix:E=<empty>)\" ;
842 # Discover the presence of NumPy
844 debug-message "Checking for NumPy..." ;
845 local full-cmd = "import sys; sys.stderr = sys.stdout; import numpy; print(numpy.get_include())" ;
846 local full-cmd = $(interpreter-cmd)" -c \"$(full-cmd)\"" ;
847 debug-message "running command '$(full-cmd)'" ;
848 local result = [ SHELL $(full-cmd) : strip-eol : exit-status ] ;
852 .numpy-include = $(result[1]) ;
853 debug-message "NumPy enabled" ;
857 debug-message "NumPy disabled. Reason:" ;
858 debug-message " $(full-cmd) aborted with " ;
859 debug-message " $(result[1])" ;
863 # End autoconfiguration sequence.
866 # Normalize and dissect any version number.
870 major-minor = [ split-version $(version) ] ;
871 version = $(major-minor:J=.) ;
875 local target-requirements = $(condition) ;
877 # Add the version, if any, to the target requirements.
880 if ! $(version) in [ feature.values python ]
882 feature.extend python : $(version) ;
883 py$(major-minor[1])-version ?= $(version) ;
884 if $(py$(major-minor[1])-version) < $(version)
886 py$(major-minor[1])-version = $(version) ;
889 target-requirements += <python>$(version:E=default) ;
892 target-requirements += <target-os>$(target-os) ;
894 # See if we can find a framework directory on darwin.
895 local framework-directory ;
896 if $(target-os) = darwin
898 # Search upward for the framework directory.
899 local framework-directory = $(libraries[-1]) ;
900 while $(framework-directory:D=) && $(framework-directory:D=) != Python.framework
902 framework-directory = $(framework-directory:D) ;
905 if $(framework-directory:D=) = Python.framework
907 debug-message framework directory is \"$(framework-directory)\" ;
911 debug-message "no framework directory found; using library path" ;
912 framework-directory = ;
916 local dll-path = $(libraries) ;
918 # Make sure that we can find the Python DLL on Windows.
919 if ( $(target-os) = windows ) && $(exec-prefix)
921 dll-path += $(exec-prefix) ;
925 # Prepare usage requirements.
927 local usage-requirements = [ system-library-dependencies $(target-os) ] ;
928 usage-requirements += <include>$(includes) <python.interpreter>$(interpreter-cmd) ;
929 if <python-debugging>on in $(condition)
931 if $(target-os) = windows
933 # In pyconfig.h, Py_DEBUG is set if _DEBUG is set. If we define
934 # Py_DEBUG we will get multiple definition warnings.
935 usage-requirements += <define>_DEBUG ;
939 usage-requirements += <define>Py_DEBUG ;
943 # In case we added duplicate requirements from what the user specified.
944 target-requirements = [ sequence.unique $(target-requirements) ] ;
946 # Global, but conditional, requirements to give access to the interpreter
947 # for general utilities, like other toolsets, that run Python scripts.
948 toolset.add-requirements
949 "$(target-requirements:J=,):<python.interpreter>$(interpreter-cmd)" ;
951 # Register the right suffix for extensions.
952 register-extension-suffix $(extension-suffix) : $(target-requirements) ;
954 # Make sure that the python feature is always considered
955 # relevant for any targets that depend on python. Without
956 # this, it would only be considered relevant when there are
957 # multiple configurations defined within the same build.
958 target-requirements += <relevant>python ;
961 # Declare the "python" target. This should really be called
962 # python_for_embedding.
965 if $(framework-directory)
969 : $(target-requirements)
971 : $(usage-requirements) <framework>$(framework-directory)
976 declare-libpython-target $(version) : $(target-requirements) ;
978 # This is an evil hack. On, Windows, when Python is embedded, nothing
979 # seems to set up sys.path to include Python's standard library
980 # (http://article.gmane.org/gmane.comp.python.general/544986). The evil
981 # here, aside from the workaround necessitated by Python's bug, is that:
983 # a. we're guessing the location of the python standard library from the
984 # location of pythonXX.lib
986 # b. we're hijacking the <testing.launcher> property to get the
987 # environment variable set up, and the user may want to use it for
988 # something else (e.g. launch the debugger).
989 local set-PYTHONPATH ;
990 if $(target-os) = windows
992 set-PYTHONPATH = [ common.prepend-path-variable-command PYTHONPATH :
993 $(libraries:D)/Lib ] ;
998 : $(target-requirements)
1000 # Why python.lib must be listed here instead of along with the
1001 # system libs is a mystery, but if we do not do it, on cygwin,
1002 # -lpythonX.Y never appears in the command line (although it does on
1004 : $(usage-requirements)
1005 <testing.launcher>$(set-PYTHONPATH)
1006 <library-path>$(libraries) <dll-path>$(dll-path) <library>python.lib
1010 # On *nix, we do not want to link either Boost.Python or Python extensions
1011 # to libpython, because the Python interpreter itself provides all those
1012 # symbols. If we linked to libpython, we would get duplicate symbols. So
1013 # declare two targets -- one for building extensions and another for
1015 if $(target-os) in windows cygwin
1017 alias python_for_extensions : python : $(target-requirements) ;
1019 else if $(target-os) = darwin {
1020 alias python_for_extensions
1022 : $(target-requirements)
1024 : $(usage-requirements) <linkflags>"-undefined dynamic_lookup"
1027 # On AIX we need Python extensions and Boost.Python to import symbols from
1028 # the Python interpreter. Dynamic libraries opened with dlopen() do not
1029 # inherit the symbols from the Python interpreter.
1030 else if $(target-os) = aix
1032 alias python_for_extensions
1034 : $(target-requirements)
1036 : $(usage-requirements) <linkflags>"-Wl,-bI:$(libraries[1])/python.exp"
1041 alias python_for_extensions
1043 : $(target-requirements)
1045 : $(usage-requirements)
1051 # Conditional rule specification that will prevent building of a target
1052 # if there is no matching python configuration available with the given
1053 # required properties.
1054 rule require-py ( properties * )
1056 local py-ext-target = [ $(.project).find python_for_extensions : no-error ] ;
1057 if ! $(py-ext-target)
1061 local property-set = [ property-set.create $(properties) ] ;
1062 property-set = [ $(property-set).expand ] ;
1063 local py-ext-alternative = [ $(py-ext-target).select-alternatives $(property-set) ] ;
1064 if ! $(py-ext-alternative)
1073 return $(.configured) ;
1081 rule numpy-include ( )
1083 return $(.numpy-include) ;
1087 type.register PYTHON_EXTENSION : : SHARED_LIB ;
1090 local rule register-extension-suffix ( root : condition * )
1094 switch [ feature.get-values target-os : $(condition) ]
1096 case windows : suffix = pyd ;
1097 case cygwin : suffix = dll ;
1100 if [ feature.get-values python : $(condition) ] in 1.5 1.6 2.0 2.1 2.2 2.3 2.4
1109 case * : suffix = so ;
1112 type.set-generated-target-suffix PYTHON_EXTENSION : $(condition) : <$(root).$(suffix)> ;
1116 # Unset 'lib' prefix for PYTHON_EXTENSION
1117 type.set-generated-target-prefix PYTHON_EXTENSION : : "" ;
1120 rule python-extension ( name : sources * : requirements * : default-build * :
1121 usage-requirements * )
1125 requirements += <use>/python//python_for_extensions ;
1127 requirements += <suppress-import-lib>true ;
1129 local project = [ project.current ] ;
1131 targets.main-target-alternative
1132 [ new typed-target $(name) : $(project) : PYTHON_EXTENSION
1133 : [ targets.main-target-sources $(sources) : $(name) ]
1134 : [ targets.main-target-requirements $(requirements) : $(project) ]
1135 : [ targets.main-target-default-build $(default-build) : $(project) ]
1139 IMPORT python : python-extension : : python-extension ;
1141 # Support for testing.
1142 type.register PY : py ;
1143 type.register RUN_PYD_OUTPUT ;
1144 type.register RUN_PYD : : TEST ;
1147 class python-test-generator : generator
1151 rule __init__ ( * : * )
1153 generator.__init__ $(1) : $(2) : $(3) : $(4) : $(5) : $(6) : $(7) : $(8) : $(9) ;
1154 self.composing = true ;
1157 rule run ( project name ? : property-set : sources * : multiple ? )
1159 local pyversion = [ $(property-set).get <python> ] ;
1161 local other-pythons ;
1163 for local s in $(sources)
1165 if [ $(s).type ] = PY
1169 # First Python source ends up on command line.
1175 # Other Python sources become dependencies.
1176 other-pythons += $(s) ;
1182 for local s in $(sources)
1184 if [ $(s).type ] = PYTHON_EXTENSION
1186 extensions += $(s) ;
1191 for local s in $(sources)
1193 if [ type.is-derived [ $(s).type ] LIB ]
1194 && ! $(s) in $(extensions)
1201 for local s in $(sources)
1203 if [ type.is-derived [ $(s).type ] CPP ]
1205 local name = [ utility.basename [ $(s).name ] ] ;
1206 if $(name) = [ utility.basename [ $(python).name ] ]
1208 name = $(name)_ext ;
1210 local extension = [ generators.construct $(project) $(name) :
1211 PYTHON_EXTENSION : $(property-set) : $(s) $(libs) ] ;
1213 # The important part of usage requirements returned from
1214 # PYTHON_EXTENSION generator are xdll-path properties that will
1215 # allow us to find the python extension at runtime.
1216 property-set = [ $(property-set).add $(extension[1]) ] ;
1218 # Ignore usage requirements. We're a top-level generator and
1219 # nobody is going to use what we generate.
1220 new-sources += $(extension[2-]) ;
1224 property-set = [ $(property-set).add-raw <dependency>$(other-pythons) ] ;
1226 return [ construct-result $(python) $(extensions) $(new-sources) :
1227 $(project) $(name) : $(property-set) ] ;
1233 [ new python-test-generator python.capture-output : : RUN_PYD_OUTPUT ] ;
1235 generators.register-standard testing.expect-success
1236 : RUN_PYD_OUTPUT : RUN_PYD ;
1239 # There are two different ways of spelling OS names. One is used for [ os.name ]
1240 # and the other is used for the <host-os> and <target-os> properties. Until that
1241 # is remedied, this sets up a crude mapping from the latter to the former, that
1242 # will work *for the purposes of cygwin/NT cross-builds only*. Could not think
1243 # of a better name than "translate".
1245 .translate-os-windows = NT ;
1246 .translate-os-cygwin = CYGWIN ;
1247 local rule translate-os ( src-os )
1249 local x = $(.translate-os-$(src-os)) [ os.name ] ;
1254 # Extract the path to a single ".pyd" source. This is used to build the
1255 # PYTHONPATH for running bpl tests.
1257 local rule pyd-pythonpath ( source )
1259 return [ on $(source) return $(LOCATE) $(SEARCH) ] ;
1263 # The flag settings on testing.capture-output do not apply to python.capture
1264 # output at the moment. Redo this explicitly.
1265 toolset.flags python.capture-output ARGS <testing.arg> ;
1266 toolset.flags python.capture-output INPUT_FILES <testing.input-file> ;
1268 toolset.uses-features python.capture-output :
1269 <testing.launcher> <testing.execute> <dll-path> <xdll-path> <target-os>
1272 rule capture-output ( target : sources * : properties * )
1274 # Setup up a proper DLL search path. Here, $(sources[1]) is a python module
1275 # and $(sources[2]) is a DLL. Only $(sources[1]) is passed to
1276 # testing.capture-output, so RUN_PATH variable on $(sources[2]) is not
1277 # consulted. Move it over explicitly.
1278 RUN_PATH on $(sources[1]) = [ on $(sources[2-]) return $(RUN_PATH) ] ;
1280 PYTHONPATH = [ sequence.transform pyd-pythonpath : $(sources[2-]) ] ;
1281 PYTHONPATH += [ feature.get-values pythonpath : $(properties) ] ;
1283 # After test is run, we remove the Python module, but not the Python script.
1284 testing.capture-output $(target) : $(sources[1]) : $(properties) ;
1286 # PYTHONPATH is different; it will be interpreted by whichever Python is
1287 # invoked and so must follow path rules for the target os. The only OSes
1288 # where we can run python for other OSes currently are NT and CYGWIN so we
1289 # only need to handle those cases.
1290 local target-os = [ feature.get-values target-os : $(properties) ] ;
1291 # Oddly, host-os is not in properties, so grab the default value.
1292 local host-os = [ feature.defaults host-os ] ;
1293 host-os = $(host-os:G=) ;
1294 if $(target-os) != $(host-os) && $(target-os) in windows cygwin && $(host-os) in windows cygwin
1296 PYTHONPATH = [ sequence.transform $(host-os)-to-$(target-os)-path :
1299 local path-separator = [ os.path-separator [ translate-os $(target-os) ] ] ;
1300 local set-PYTHONPATH = [ common.variable-setting-command PYTHONPATH :
1301 $(PYTHONPATH:E=:J=$(path-separator)) ] ;
1302 LAUNCHER on $(target) = $(set-PYTHONPATH) [ on $(target) return \"$(PYTHON)\" ] ;
1306 rule bpl-test ( name : sources * : requirements * )
1309 sources ?= $(name).py $(name).cpp ;
1310 return [ testing.make-test run-pyd : $(sources) /boost/python//boost_python
1311 : $(requirements) : $(name) ] ;
1314 # The same as bpl-test but additionally require (and link to) boost_numpy.
1315 # Masked whenever NumPy is not enabled.
1316 rule numpy-test ( name : sources * : requirements * )
1318 numpy-include = [ python.numpy-include ] ;
1320 if ! $(.numpy) { requirements += <build>no ; }
1321 sources ?= $(name).py $(name).cpp ;
1322 name = [ regex.replace $(name) "[/]" "~" ] ;
1323 return [ testing.make-test run-pyd
1324 : $(sources) /boost/python//boost_numpy /boost/python//boost_python
1325 : $(requirements) <include>$(numpy-include)
1329 rule py-version ( n )
1331 return $(py$(n)-version) ;
1334 IMPORT $(__name__) : bpl-test : : bpl-test ;
1335 IMPORT $(__name__) : numpy-test : : numpy-test ;
1336 IMPORT $(__name__) : py-version : : py-version ;