summaryrefslogtreecommitdiff
path: root/setup.py
blob: 1985420d1a3326dd99c4a9787a037f3a0e392e10 (plain)
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
#!/usr/bin/env python

import distutils.core      as dic
import distutils.dir_util  as dut
import distutils.file_util as fut
import distutils.sysconfig as dsy
import io
import os
import subprocess
import sys

pycairo_version        = '1.10.1'
cairo_version_required = '1.10.2'
python_version_required = (3,0)
pkgconfig_file = 'py3cairo.pc'
config_file    = 'src/config.h'


def call(command):
  pipe = subprocess.Popen(command, shell=True,
                          stdout=subprocess.PIPE,
                          stderr=subprocess.PIPE)
  pipe.wait()
  return pipe

def pkg_config_version_check(pkg, version):
  check = '%s >= %s' % (pkg, version)
  pipe = call("pkg-config --print-errors --exists '%s'" % (check,))
  if pipe.returncode == 0:
    print(check, ' Successful')
  else:
    print(check, ' Failed')
    raise SystemExit(pipe.stderr.read().decode())

def pkg_config_parse(opt, pkg):
  check = "pkg-config %s %s" % (opt, pkg)
  pipe = call("pkg-config %s %s" % (opt, pkg))
  if pipe.returncode != 0:
    print(check, ' Failed')
    raise SystemExit(pipe.stderr.read().decode())

  output = pipe.stdout.read()
  output = output.decode() # get the str
  opt = opt[-2:]
  return [x.lstrip(opt) for x in output.split()]


def createPcFile(PcFile):
  print('creating %s' % PcFile)
  with open(PcFile, 'w') as fo:
    fo.write ("""\
prefix=%s

Name: Pycairo
Description: Python 3 bindings for cairo
Version: %s
Requires: cairo
Cflags: -I${prefix}/include/pycairo
Libs:
""" % (sys.prefix, pycairo_version)
            )

def createConfigFile(ConfigFile):
  print('creating %s' % ConfigFile)
  v = pycairo_version.split('.')

  with open(ConfigFile, 'w') as fo:
    fo.write ("""\
// Configuration header created by setup.py - do not edit
#ifndef _CONFIG_H
#define _CONFIG_H 1

#define PYCAIRO_VERSION_MAJOR %s
#define PYCAIRO_VERSION_MINOR %s
#define PYCAIRO_VERSION_MICRO %s
#define VERSION "%s"

#endif // _CONFIG_H
""" % (v[0], v[1], v[2], pycairo_version)
            )


if sys.version_info < python_version_required:
  raise SystemExit('Error: Python >= %s is required' %
                   (python_version_required,))

pkg_config_version_check ('cairo', cairo_version_required)
if sys.platform == 'win32':
  runtime_library_dirs = []
else:
  runtime_library_dirs = pkg_config_parse('--libs-only-L', 'cairo')

createPcFile(pkgconfig_file)
createConfigFile(config_file)


cairo = dic.Extension(
  name = 'cairo._cairo',
  sources = ['src/cairomodule.c',
             'src/context.c',
             'src/font.c',
             'src/matrix.c',
             'src/path.c',
             'src/pattern.c',
             'src/region.c',
             'src/surface.c',
             ],
  include_dirs = pkg_config_parse('--cflags-only-I', 'cairo'),
  library_dirs = pkg_config_parse('--libs-only-L', 'cairo'),
  libraries    = pkg_config_parse('--libs-only-l', 'cairo'),
  runtime_library_dirs = runtime_library_dirs,
  )

dic.setup(
  name = "pycairo",
  version = pycairo_version,
  description = "python interface for cairo",
  ext_modules = [cairo],
  data_files = [
    ('include/pycairo', ['src/py3cairo.h']),
    ('lib/pkgconfig', [pkgconfig_file]),
    (os.path.join(dsy.get_python_lib(), 'cairo'),
     ['src/__init__.py']),
    ],
  )