2 # get-py-info.py: get various Python info (for building)
4 ######################################################################
6 # Copyright (c) 2002-2006 CollabNet. All rights reserved.
8 # This software is licensed as described in the file COPYING, which
9 # you should have received as part of this distribution. The terms
10 # are also available at http://subversion.tigris.org/license-1.html.
11 # If newer versions of this license are posted there, you may use a
12 # newer version instead, at your option.
14 ######################################################################
16 # This should be loaded/run by the appropriate Python, rather than executed
17 # directly as a program. In other words, you should:
19 # $ python2 get-py-info.py --includes
26 print 'USAGE: python %s WHAT' % sys
.argv
[0]
27 print ' Returns information about how to build Python extensions.'
28 print ' WHAT may be one of:'
29 print " --includes : return -I include flags"
30 print " --compile : return a compile command"
31 print " --link : return a link command"
32 print " --libs : return just the library options for linking"
35 if len(sys
.argv
) != 2:
39 from distutils
import sysconfig
41 # No information available
45 if sys
.argv
[1] == '--includes':
46 inc
= sysconfig
.get_python_inc()
47 plat
= sysconfig
.get_python_inc(plat_specific
=1)
51 print "-I%s -I%s" % (inc
, plat
)
54 if sys
.argv
[1] == '--compile':
55 cc
, basecflags
, opt
, ccshared
= \
56 sysconfig
.get_config_vars('CC', 'BASECFLAGS', 'OPT', 'CCSHARED')
58 opt
= basecflags
+ ' ' + opt
59 print cc
, opt
, ccshared
62 def add_option(options
, name
, value
=None):
63 """Add option to list of options"""
68 def add_option_if_missing(options
, name
, value
=None):
69 """Add option to list of options, if it is not already present"""
70 if options
.count(name
) == 0 and options
.count("-Wl,%s" % name
) == 0:
71 add_option(options
, name
, value
)
74 """Get list of Python linker options"""
76 # Initialize config variables
77 assert os
.name
== "posix"
78 options
= sysconfig
.get_config_var('LDSHARED').split()
79 fwdir
= sysconfig
.get_config_var('PYTHONFRAMEWORKDIR')
81 if fwdir
and fwdir
!= "no-framework":
83 # Setup the framework prefix
84 fwprefix
= sysconfig
.get_config_var('PYTHONFRAMEWORKPREFIX')
85 if fwprefix
!= "/System/Library/Frameworks":
86 add_option_if_missing(options
, "-F%s" % fwprefix
)
88 # Load in the framework
89 fw
= sysconfig
.get_config_var('PYTHONFRAMEWORK')
90 add_option(options
, "-framework", fw
)
92 elif sys
.platform
== 'darwin':
94 # Load bundles from python
95 python_exe
= os
.path
.join(sysconfig
.get_config_var("BINDIR"),
96 sysconfig
.get_config_var('PYTHON'))
97 add_option_if_missing(options
, "-bundle_loader", python_exe
)
99 elif sys
.platform
== 'cygwin' or sys
.platform
.startswith('openbsd'):
101 # Add flags to build against the Python library (also necessary
102 # for Darwin, but handled elsewhere).
104 # Find the path to the library, and add a flag to include it as a
105 # library search path.
106 shared_libdir
= sysconfig
.get_config_var('LIBDIR')
107 static_libdir
= sysconfig
.get_config_var('LIBPL')
108 ldlibrary
= sysconfig
.get_config_var('LDLIBRARY')
109 if os
.path
.exists(os
.path
.join(shared_libdir
, ldlibrary
)):
110 if shared_libdir
!= '/usr/lib':
111 add_option_if_missing(options
, '-L%s' % shared_libdir
)
112 elif os
.path
.exists(os
.path
.join(static_libdir
, ldlibrary
)):
113 add_option_if_missing(options
, "-L%s" % static_libdir
)
115 # Add a flag to build against the library itself.
116 python_version
= sysconfig
.get_config_var('VERSION')
117 add_option_if_missing(options
, "-lpython%s" % python_version
)
122 """Get list of Python library options"""
123 link_command
= link_options()
126 # Extract library-related options from link command
127 for i
in range(len(link_command
)):
128 option
= link_command
[i
]
129 if (not option
.startswith("-L:") and option
.startswith("-L") or
130 option
.startswith("-Wl,") or option
.startswith("-l") or
131 option
.startswith("-F") or option
== "-bundle" or
132 option
== "-flat_namespace"):
133 options
.append(option
)
134 elif (option
== "-undefined" or option
== "-bundle_loader" or
135 option
== "-framework"):
136 options
.append(option
)
137 options
.append(link_command
[i
+1])
141 if sys
.argv
[1] == '--link':
142 print " ".join(link_options())
145 if sys
.argv
[1] == '--libs':
146 print " ".join(lib_options())