Merge remote-tracking branch 'origin/pycurl' into planetlab-4_0-branch
[plcapi.git] / pycurl / setup.py
1 #! /usr/bin/env python
2 # -*- coding: iso-8859-1 -*-
3 # vi:ts=4:et
4 # $Id: setup.py,v 1.123 2005/02/17 10:13:23 mfx Exp $
5
6 """Setup script for the PycURL module distribution."""
7
8 PACKAGE = "pycurl"
9 PY_PACKAGE = "curl"
10 VERSION = "7.13.1"
11
12 import glob, os, re, sys, string
13 import distutils
14 from distutils.core import setup
15 from distutils.extension import Extension
16 from distutils.util import split_quoted
17 from distutils.version import LooseVersion
18
19 include_dirs = []
20 define_macros = []
21 library_dirs = []
22 libraries = []
23 runtime_library_dirs = []
24 extra_objects = []
25 extra_compile_args = []
26 extra_link_args = []
27
28
29 def scan_argv(s, default):
30     p = default
31     i = 1
32     while i < len(sys.argv):
33         arg = sys.argv[i]
34         if string.find(arg, s) == 0:
35             p = arg[len(s):]
36             assert p, arg
37             del sys.argv[i]
38         else:
39             i = i + 1
40     ##print sys.argv
41     return p
42
43
44 # append contents of an environment variable to library_dirs[]
45 def add_libdirs(envvar, sep, fatal=0):
46     v = os.environ.get(envvar)
47     if not v:
48         return
49     for dir in string.split(v, sep):
50         dir = string.strip(dir)
51         if not dir:
52             continue
53         dir = os.path.normpath(dir)
54         if os.path.isdir(dir):
55             if not dir in library_dirs:
56                 library_dirs.append(dir)
57         elif fatal:
58             print "FATAL: bad directory %s in environment variable %s" % (dir, envvar)
59             sys.exit(1)
60
61
62 if sys.platform == "win32":
63     # Windows users have to configure the CURL_DIR path parameter to match
64     # their cURL source installation.  The path set here is just an example
65     # and thus unlikely to match your installation.
66     CURL_DIR = r"c:\src\build\pycurl\curl-7.13.1"
67     CURL_DIR = scan_argv("--curl-dir=", CURL_DIR)
68     print "Using curl directory:", CURL_DIR
69     assert os.path.isdir(CURL_DIR), "please check CURL_DIR in setup.py"
70     include_dirs.append(os.path.join(CURL_DIR, "include"))
71     extra_objects.append(os.path.join(CURL_DIR, "lib", "libcurl.lib"))
72     extra_link_args.extend(["gdi32.lib", "winmm.lib", "ws2_32.lib",])
73     add_libdirs("LIB", ";")
74     if string.find(sys.version, "MSC") >= 0:
75         extra_compile_args.append("-O2")
76         extra_compile_args.append("-GF")        # enable read-only string pooling
77         extra_compile_args.append("-WX")        # treat warnings as errors
78         extra_link_args.append("/opt:nowin98")  # use small section alignment
79 else:
80     # Find out the rest the hard way
81     CURL_CONFIG = "curl-config"
82     CURL_CONFIG = scan_argv("--curl-config=", CURL_CONFIG)
83     d = os.popen("'%s' --version" % CURL_CONFIG).read()
84     if d:
85         d = string.strip(d)
86     if not d:
87         raise Exception, ("`%s' not found -- please install the libcurl development files" % CURL_CONFIG)
88     print "Using %s (%s)" % (CURL_CONFIG, d)
89     for e in split_quoted(os.popen("'%s' --cflags" % CURL_CONFIG).read()):
90         if e[:2] == "-I":
91             # do not add /usr/include
92             if not re.search(r"^\/+usr\/+include\/*$", e[2:]):
93                 include_dirs.append(e[2:])
94         else:
95             extra_compile_args.append(e)
96     for e in split_quoted(os.popen("'%s' --libs" % CURL_CONFIG).read()):
97         if e[:2] == "-l":
98             libraries.append(e[2:])
99         elif e[:2] == "-L":
100             library_dirs.append(e[2:])
101         else:
102             extra_link_args.append(e)
103     if not libraries:
104         libraries.append("curl")
105     # Add extra compile flag for MacOS X
106     if sys.platform[:-1] == "darwin":
107         extra_link_args.append("-flat_namespace")
108
109
110 ###############################################################################
111
112 def get_kw(**kw): return kw
113
114 ext = Extension(
115     name=PACKAGE,
116     sources=[
117         os.path.join("src", "pycurl.c"),
118     ],
119     include_dirs=include_dirs,
120     define_macros=define_macros,
121     library_dirs=library_dirs,
122     libraries=libraries,
123     runtime_library_dirs=runtime_library_dirs,
124     extra_objects=extra_objects,
125     extra_compile_args=extra_compile_args,
126     extra_link_args=extra_link_args,
127 )
128 ##print ext.__dict__; sys.exit(1)
129
130
131 ###############################################################################
132
133 # prepare data_files
134
135 def get_data_files():
136     # a list of tuples with (path to install to, a list of local files)
137     data_files = []
138     if sys.platform == "win32":
139         datadir = os.path.join("doc", PACKAGE)
140     else:
141         datadir = os.path.join("share", "doc", PACKAGE)
142     #
143     files = ["ChangeLog", "COPYING", "INSTALL", "README", "TODO",]
144     if files:
145         data_files.append((os.path.join(datadir), files))
146     files = glob.glob(os.path.join("doc", "*.html"))
147     if files:
148         data_files.append((os.path.join(datadir, "html"), files))
149     files = glob.glob(os.path.join("examples", "*.py"))
150     if files:
151         data_files.append((os.path.join(datadir, "examples"), files))
152     files = glob.glob(os.path.join("tests", "*.py"))
153     if files:
154         data_files.append((os.path.join(datadir, "tests"), files))
155     #
156     assert data_files
157     for install_dir, files in data_files:
158         assert files
159         for f in files:
160             assert os.path.isfile(f), (f, install_dir)
161     return data_files
162
163 ##print get_data_files(); sys.exit(1)
164
165
166 ###############################################################################
167
168 setup_args = get_kw(
169     name=PACKAGE,
170     version=VERSION,
171     description="PycURL -- cURL library module for Python",
172     author="Kjetil Jacobsen, Markus F.X.J. Oberhumer",
173     author_email="kjetilja@cs.uit.no, markus@oberhumer.com",
174     maintainer="Kjetil Jacobsen, Markus F.X.J. Oberhumer",
175     maintainer_email="kjetilja@cs.uit.no, markus@oberhumer.com",
176     url="http://pycurl.sourceforge.net/",
177     license="GNU Lesser General Public License (LGPL)",
178     data_files=get_data_files(),
179     ext_modules=[ext],
180     long_description="""
181 This module provides Python bindings for the cURL library.""",
182 )
183
184 if sys.version >= "2.2":
185     setup_args["packages"] = [PY_PACKAGE]
186     setup_args["package_dir"] = { PY_PACKAGE: os.path.join('python', 'curl') }
187
188
189 ##print distutils.__version__
190 if LooseVersion(distutils.__version__) > LooseVersion("1.0.1"):
191     setup_args["platforms"] = "All"
192 if LooseVersion(distutils.__version__) < LooseVersion("1.0.3"):
193     setup_args["licence"] = setup_args["license"]
194
195 if __name__ == "__main__":
196     for o in ext.extra_objects:
197         assert os.path.isfile(o), o
198     # We can live with the deprecationwarning for a while
199     apply(setup, (), setup_args)