~tomio2009/imagep/trunk

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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/usr/bin/env python
from distutils.core import setup, Extension
from distutils import util
#from distutils import sysconfig
from distutils.dist import Distribution
from distutils.ccompiler import get_default_compiler
import os, sys

#We have some extension to be compiled into a dynamic lib. No real python C API
#is required, so we have to hack the setup a bit
#Let us go around: compile, link, then take the dynamic lib and put it
#to the install as a data file. Csource.py should pick it up.
PkgName="ImageP"
SourceList =['src/SimpleFilter', 'src/bwlabel','src/MinMaxMean',\
                'src/RankFilter', 'src/SimpleFilter1D', 'src/hit_miss',\
                'src/Perimeter',\
                'src/PeakFind', 'src/SimpleErode', 'src/SimpleDilate','src/DistanceFilter']
CSourceList = []
for i in SourceList:
    txt = '%s.c' %i
    CSourceList.append(txt)
#end for

#some general declaration, variables:
ccname = get_default_compiler()
#where are the site packages:
#We need to tell where the package should go
#this should be the install directory of the whole part...
#Unfortunately the sysconfig seems not to be the right answer:
#pathcore = os.path.join(sysconfig.get_python_lib(), "ImageP")
#create an empty distribution, and figure out its install path:
testinst= Distribution()
testinsti= testinst.get_command_obj('install')
testinsti.ensure_finalized()
pathcore= os.path.join(testinsti.install_purelib, PkgName)

libname = 'Csources'
datafile = None

def build_src(libname, datafile, pathcore):
#    global libname
#    global datafile
#    global pathcore

    if ccname == 'msvc':
        #experimenting showed this working on a win7 pc
        #from distutils.msvc9compiler import MSVCCompiler as CC
        from distutils.msvccompiler import MSVCCompiler as CC
        compile_flags = ["/Ox", "/MD"]
        objext = '.obj'
        #needs testing!
        libdirs = []
        link_args = ["/DLL","/nologo","/INCREMENTAL:NO"]
        libname = "lib%s" %libname
        deffile = "/DEF:.\\src\\%s.def" %libname
        link_postargs = [deffile]
        outfile = "%s.dll" %libname
        datafile = [(pathcore,[outfile])]

    elif ccname == 'bcpp':
        print( "sorry, I do not know what to do with Borland C")
        CC = None

    elif ccname == 'emx':
        from distutils.emxccompiler import UnixCCompiler as CC
        print("sorry, I do not know what to do with EMX")
        CC = None

    elif ccname == 'mwerks':
        from distutils.mwerkscompiler import UnixCCompiler as CC
        print("sorry, I do not know what to do with mwerks Code warrior")
        CC = None

    else:
        #let us assume that cygwin and mingw needs the same options as gcc
        if ccname == 'cygwin' or ccname=='mingw32':
            from distutils.cygwinccompiler import UnixCCompiler as CC
            outfile = "lib%s.dll" %libname
        else:
            #only 'unix' is left:
            from distutils.unixccompiler import UnixCCompiler as CC

        #compiler options:
        objext = '.o'
        compile_flags=["-Wall","-O3", "-fpic"]
        libdirs = []

        #linking is:
        #gcc -shared -Wl,-soname,$OUTPUTFILE -o $OUTPUTFILE -l$LIB
        #On mac:
        #gcc -shared -dynamiclib MinMaxMean.c PeakFind.c Perimeter.c RankFilter.c SimpleDilate.c SimpleErode.c SimpleFilter.c SimpleFilter1D.c bwlabel.c -o libCsources.dylib

        #Is it a Mac?
        if "darwin" in sys.platform:
            outfile = "lib%s.so" %libname
            #outfile = "lib%s.dynlib" %libname
            print( "output: %s" %outfile)
            soname = "-dynamiclib"
            link_args=[soname]
        else:
            #Linux hopefully:
            outfile = "lib%s.so" %libname
            soname = "-Wl,-soname,%s" %outfile
            link_args=["-shared",soname]

        link_postargs=None
        #here we define where to copy and what:
        datafile = [(pathcore,[outfile])]

    #now try our best:
    try:

        print( "Calling the C compiler on the sources:")
        cc = CC()
        cc.compile(CSourceList, extra_postargs=compile_flags)
        print( "Done.")

        objlist = []
        for i in SourceList:
            txt = "%s%s" %(i, objext)
            objlist.append(txt)
        #end for

        print( "Calling the linker")

        #cc.link('',objlist, outfile, library_dirs=libdirs,\
        #I guess this way it should work:
        cc.link_shared_lib(objlist, libname, library_dirs=libdirs,\
                    extra_preargs=link_args, extra_postargs=link_postargs)

        print("building is done")

    except:
        print("Building the C extension failed.")
        print("Building without the C module:")
        datafile = None
    #End try

    return (libname, datafile)
#end build_src

#On windows one can still use a precompiled dll, if that works...
# dllfile ="src/Csources.dll"
# if os.path.isfile(dllfile):
#     print "dll found"

args = sys.argv
if len(args) > 1:
    if 'build' in args or 'install' in args:
        libname, datafile = build_src(libname, datafile, pathcore)

    elif 'clean' in args:
        from glob import glob
        lst = list()
        lst.append(glob('libCsources*'))
        lst.append(glob('src/*.o'))
        lst.append(glob('src/*.obj'))
        lst.append(glob('ImageP/*.pyc'))

        print("cleaning up:")
        for l in lst:
            for i in l:
                print("delete:", i)
                os.remove(i)
#end if args

print("dynamic lib: ",datafile)
#now back to the normal setup:
setup(name=PkgName,
        version='0.32b',
        description='Package for particle tracking etc. in images',
            author='Tomio',
        author_email='tamas.haraszti@uni-heidelberg.de',
        url='https://launchpad.net/imagep',
        ext_modules = None,
    #in this case we get all as a subpackage of ImageP:
    #the sources sit in the ImageP subfolder, C sources in src
        package_dir={PkgName:'ImageP'},
        packages=[PkgName],
        data_files=datafile,
        )