-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsetup.py
162 lines (132 loc) · 4.93 KB
/
setup.py
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
#----------------------------------------------------------------------#
# distutils setup script for compiling python extensions #
#----------------------------------------------------------------------#
"""
Compilation command: python setup.py build_ext
Hugo Raguet, adapted by Loic Landrieu, 2020
"""
from distutils.core import setup, Extension
from distutils.command.build import build
from distutils.ccompiler import new_compiler
import numpy
import shutil # for rmtree, os.rmdir can only remove _empty_ directory
import os
import re
### targets and compile options ###
include_dirs = [numpy.get_include(), # find the Numpy headers
"../include"]
# compilation and linkage options
# MIN_OPS_PER_THREAD roughly controls parallelization, see doc in README.md
if os.name == 'nt': # windows
extra_compile_args = ["/std:c++11", "/openmp",
"-DMIN_OPS_PER_THREAD=10000"]
extra_link_args = ["/lgomp"]
elif os.name == 'posix': # linux
extra_compile_args = ["-std=c++11", "-fopenmp",
"-DMIN_OPS_PER_THREAD=10000"]
extra_link_args = ["-lgomp"]
else:
raise NotImplementedError('OS not yet supported.')
### auxiliary functions ###
class build_class(build):
def initialize_options(self):
build.initialize_options(self)
self.build_lib = "bin"
def run(self):
build_path = self.build_lib
def purge(dir, pattern):
for f in os.listdir(dir):
if re.search(pattern, f):
os.remove(os.path.join(dir, f))
### ============ GRID GRAPH ============ ###
# ensure right working directory
tmp_work_dir = os.path.realpath(os.curdir)
os.chdir(os.path.join(os.path.realpath(os.path.dirname(__file__)), './grid-graph/python'))
name = "grid_graph"
if not os.path.exists("bin"):
os.mkdir("bin")
# remove previously compiled lib
purge("bin/", "grid_graph")
### compilation ###
mod = Extension(
name,
# list source files
["cpython/grid_graph_cpy.cpp",
"../src/edge_list_to_forward_star.cpp",
"../src/grid_to_graph.cpp"],
include_dirs=include_dirs,
extra_compile_args=extra_compile_args,
extra_link_args=extra_link_args)
setup(name=name, ext_modules=[mod], cmdclass=dict(build=build_class))
### postprocessing ###
try:
shutil.rmtree("build") # remove temporary compilation products
except FileNotFoundError:
pass
### ============ MULTILABEL POTRACE ============ ###
os.chdir('../../multilabel-potrace/python')
name = "multilabel_potrace_svg"
include_dirs = [numpy.get_include(), # find the Numpy headers
"../include", "../include/potrace"]
if not os.path.exists("bin"):
os.mkdir("bin")
if not os.path.exists("build"):
os.mkdir("build")
# remove previously compiled lib
purge("bin/", "multilabel_potrace_svg")
### compilation ###
# compile potrace sources with C compiler
compiler = new_compiler()
C_sources = ["../src/potrace/trace.c", "../src/potrace/curve.c"]
compiler.compile(C_sources, include_dirs=include_dirs)
extra_objects = []
object_extension = ".o" if os.name == "posix" else ".obj"
for o in C_sources:
obj = os.path.join("build", os.path.splitext(os.path.split(o)[1])[0]+object_extension)
shutil.move(os.path.splitext(o)[0]+object_extension, obj)
extra_objects.append(obj)
# compile multilabel potrace with C++ compiler and create module
mod = Extension(
name,
# list source files
["../src/multilabel_potrace_svg.cpp",
"../src/multilabel_potrace.cpp",
"./cpython/multilabel_potrace_svg_cpy.cpp"],
include_dirs=include_dirs,
extra_objects=extra_objects,
extra_compile_args=extra_compile_args,
extra_link_args=extra_link_args)
setup(name=name, ext_modules=[mod], cmdclass=dict(build=build_class))
### postprocessing ###
try:
shutil.rmtree("build") # remove temporary compilation products
except FileNotFoundError:
pass
### ============ PARALLEL CUT PURSUIT ============ ###
os.chdir('../../parallel-cut-pursuit/python')
name = "cp_kmpp_d0_dist_cpy"
include_dirs = [numpy.get_include(), # find the Numpy headers
"../include"]
### preprocessing ###
# ensure right working directory
if not os.path.exists("bin"):
os.mkdir("bin")
# remove previously compiled lib
purge("bin/", "cp_kmpp_d0_dist_cpy")
### compilation ###
mod = Extension(
name,
# list source files
["cpython/cp_kmpp_d0_dist_cpy.cpp", "../src/cp_kmpp_d0_dist.cpp",
"../src/cut_pursuit_d0.cpp", "../src/cut_pursuit.cpp",
"../src/maxflow.cpp"],
include_dirs=include_dirs,
extra_compile_args=extra_compile_args,
extra_link_args=extra_link_args)
setup(name=name, ext_modules=[mod], cmdclass=dict(build=build_class))
### postprocessing ###
try:
shutil.rmtree("build") # remove temporary compilation products
except FileNotFoundError:
pass
os.chdir(tmp_work_dir) # get back to initial working directory