source: sasview/setup.py @ 95d7c4f

magnetic_scattrelease-4.2.2ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since 95d7c4f was 95d7c4f, checked in by Paul Kienzle <pkienzle@…>, 6 years ago

allow compile with tinycc (note: does yet run correctly)

  • Property mode set to 100755
File size: 17.6 KB
Line 
1# -*- coding: utf-8 -*-
2#!/usr/bin/env python
3
4"""
5    Setup for SasView
6    TODO: Add checks to see that all the dependencies are on the system
7"""
8from __future__ import print_function
9
10import os
11import subprocess
12import shutil
13import sys
14from distutils.command.build_ext import build_ext
15from distutils.core import Command
16
17import numpy as np
18from setuptools import Extension, setup
19
20if os.name == 'nt':
21    try:
22        import tinycc
23    except ImportError:
24        os.path.insert(os.path.insert(0, '..', 'tinycc'))
25        import tinycc
26    use_tinycc = True
27    sys.argv.append('--compiler=mingw32')
28    print(tinycc.TCC)
29else:
30    use_tinycc = False
31
32# Manage version number ######################################
33with open(os.path.join("src", "sas", "sasview", "__init__.py")) as fid:
34    for line in fid:
35        if line.startswith('__version__'):
36            VERSION = line.split('"')[1]
37            break
38    else:
39        raise ValueError("Could not find version in src/sas/sasview/__init__.py")
40##############################################################
41
42package_dir = {}
43package_data = {}
44packages = []
45ext_modules = []
46
47# Remove all files that should be updated by this setup
48# We do this here because application updates these files from .sasview
49# except when there is no such file
50# Todo : make this list generic
51# plugin_model_list = ['polynominal5.py', 'sph_bessel_jn.py',
52#                      'sum_Ap1_1_Ap2.py', 'sum_p1_p2.py',
53#                      'testmodel_2.py', 'testmodel.py',
54#                      'polynominal5.pyc', 'sph_bessel_jn.pyc',
55#                      'sum_Ap1_1_Ap2.pyc', 'sum_p1_p2.pyc',
56#                      'testmodel_2.pyc', 'testmodel.pyc', 'plugins.log']
57
58CURRENT_SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
59SASVIEW_BUILD = os.path.join(CURRENT_SCRIPT_DIR, "build")
60
61# TODO: build step should not be messing with existing installation!!
62sas_dir = os.path.join(os.path.expanduser("~"), '.sasview')
63if os.path.isdir(sas_dir):
64    f_path = os.path.join(sas_dir, "sasview.log")
65    if os.path.isfile(f_path):
66        os.remove(f_path)
67    f_path = os.path.join(sas_dir, "categories.json")
68    if os.path.isfile(f_path):
69        os.remove(f_path)
70    f_path = os.path.join(sas_dir, 'config', "custom_config.py")
71    if os.path.isfile(f_path):
72        os.remove(f_path)
73    #f_path = os.path.join(sas_dir, 'plugin_models')
74    # if os.path.isdir(f_path):
75    #     for f in os.listdir(f_path):
76    #         if f in plugin_model_list:
77    #             file_path =  os.path.join(f_path, f)
78    #             os.remove(file_path)
79
80
81# Optionally clean before build.
82dont_clean = 'update' in sys.argv
83if dont_clean:
84    sys.argv.remove('update')
85elif os.path.exists(SASVIEW_BUILD):
86    print("Removing existing build directory", SASVIEW_BUILD, "for a clean build")
87    shutil.rmtree(SASVIEW_BUILD)
88
89# 'sys.maxsize' and 64bit: Not supported for python2.5
90is_64bits = sys.maxsize > 2**32
91
92enable_openmp = False
93if sys.platform == 'darwin':
94    if not is_64bits:
95        # Disable OpenMP
96        enable_openmp = False
97    else:
98        # Newer versions of Darwin don't support openmp
99        try:
100            darwin_ver = int(os.uname()[2].split('.')[0])
101            if darwin_ver >= 12:
102                enable_openmp = False
103        except:
104            print("PROBLEM determining Darwin version")
105
106# Options to enable OpenMP
107copt = {'msvc': ['/openmp'],
108        'mingw32': ['-fopenmp'],
109        'unix': ['-fopenmp']}
110lopt = {'msvc': ['/MANIFEST'],
111        'mingw32': ['-fopenmp'],
112        'unix': ['-lgomp']}
113
114# Platform-specific link options
115platform_lopt = {'msvc': ['/MANIFEST']}
116platform_copt = {}
117
118# Set copts to get compile working on OS X >= 10.9 using clang
119if sys.platform == 'darwin':
120    try:
121        darwin_ver = int(os.uname()[2].split('.')[0])
122        if darwin_ver >= 13 and darwin_ver < 14:
123            platform_copt = {
124                'unix': ['-Wno-error=unused-command-line-argument-hard-error-in-future']}
125    except:
126        print("PROBLEM determining Darwin version")
127
128
129class DisableOpenMPCommand(Command):
130    description = "The version of MinGW that comes with Anaconda does not come with OpenMP :( "\
131                  "This commands means we can turn off compiling with OpenMP for this or any "\
132                  "other reason."
133    user_options = []
134
135    def initialize_options(self):
136        self.cwd = None
137
138    def finalize_options(self):
139        self.cwd = os.getcwd()
140        global enable_openmp
141        enable_openmp = False
142
143    def run(self):
144        pass
145
146
147_ = """
148C:\Users\pkienzle\AppData\Local\Continuum\Anaconda2\lib\site-packages\tinycc\tcc.exe -DMS_WIN64 -shared -s build\temp.win-amd64-2.7\Release\src\sas\sascalc\calculator\c_extensions\sld2i_module.o build\temp.win-amd64-2.7\Release\src\sas\sascalc\calculator\c_extensions\sld2i.o build\temp.win-amd64-2.7\Release\src\sas\sascalc\calculator\c_extensions\libfunc.o build\temp.win-amd64-2.7\Release\src\sas\sascalc\calculator\c_extensions\librefl.o build\temp.win-amd64-2.7\Release\src\sas\sascalc\calculator\c_extensions\sld2i.def -LC:\Users\pkienzle\AppData\Local\Continuum\Anaconda2\libs -LC:\Users\pkienzle\AppData\Local\Continuum\Anaconda2\PCbuild\amd64 -LC:\Users\pkienzle\AppData\Local\Continuum\Anaconda2\PC\VS9.0\amd64 -lpython27 -lmsvcr90 -o build\lib.win-amd64-2.7\sas\sascalc\calculator\core\sld2i.pyd
149"""
150
151class build_ext_subclass(build_ext):
152    def build_extensions(self):
153        if use_tinycc:
154            # Note: no -O option because not an optimizer
155            self.compiler.set_executables(
156                #archiver = ['ar', '-cr'],
157                compiler=[tinycc.TCC, '-DMS_WIN64', '-D__TINYCC__', '-Wall'],
158                #compiler_cxx=['g__', '-DMS_WIN64', '-Wall'],  # tinycc is not a C++ compiler
159                compiler_so=[tinycc.TCC, '-DMS_WIN64', '-D__TINYCC__', '-Wall'],  # and '-c'??
160                #dll_libraries=['msvcr90'],
161                linker_exe=[tinycc.TCC, '-DMS_WIN64'],
162                linker_so=[tinycc.TCC, '-DMS_WIN64', '-shared'],
163                #linker_dll=tinycc.TCC,
164            )
165            sysroot = os.path.dirname(os.path.realpath(sys.executable))
166            self.compiler.include_dirs = [os.path.join(sysroot, 'include')]
167            self.compiler.library_dirs = [sysroot]
168        # Get 64-bitness
169        c = self.compiler.compiler_type
170        print("Compiling with %s (64bit=%s)" % (c, str(is_64bits)))
171        #print("=== compiler attributes ===")
172        #print("\n".join("%s: %s"%(k, v) for k, v in sorted(self.compiler.__dict__.items())))
173        #print("=== build_ext attributes ===")
174        #print("\n".join("%s: %s"%(k, v) for k, v in self.__dict__.items()))
175        #sys.exit(1)
176
177        # OpenMP build options
178        if enable_openmp:
179            if c in copt:
180                for e in self.extensions:
181                    e.extra_compile_args = copt[c]
182            if c in lopt:
183                for e in self.extensions:
184                    e.extra_link_args = lopt[c]
185
186        # Platform-specific build options
187        if c in platform_lopt:
188            for e in self.extensions:
189                e.extra_link_args = platform_lopt[c]
190
191        if c in platform_copt:
192            for e in self.extensions:
193                e.extra_compile_args = platform_copt[c]
194
195        build_ext.build_extensions(self)
196
197
198class BuildSphinxCommand(Command):
199    description = "Build Sphinx documentation."
200    user_options = []
201
202    def initialize_options(self):
203        self.cwd = None
204
205    def finalize_options(self):
206        self.cwd = os.getcwd()
207
208    def run(self):
209        ''' First builds the sasmodels documentation if the directory
210        is present. Then builds the sasview docs.
211        '''
212        ### AJJ - Add code for building sasmodels docs here:
213        # check for doc path
214        SASMODELS_DOCPATH = os.path.abspath(os.path.join(os.getcwd(), '..', 'sasmodels', 'doc'))
215        print("========= check for sasmodels at", SASMODELS_DOCPATH, "============")
216        if os.path.exists(SASMODELS_DOCPATH):
217            if os.path.isdir(SASMODELS_DOCPATH):
218                # if available, build sasmodels docs
219                print("============= Building sasmodels model documentation ===============")
220                smdocbuild = subprocess.call(["make", "-C", SASMODELS_DOCPATH, "html"])
221        else:
222            # if not available warning message
223            print("== !!WARNING!! sasmodels directory not found. Cannot build model docs. ==")
224
225        #Now build sasview (+sasmodels) docs
226        sys.path.append("docs/sphinx-docs")
227        import build_sphinx
228        build_sphinx.rebuild()
229
230
231# sas module
232package_dir["sas"] = os.path.join("src", "sas")
233packages.append("sas")
234
235# sas module
236package_dir["sas.sasgui"] = os.path.join("src", "sas", "sasgui")
237packages.append("sas.sasgui")
238
239# sas module
240package_dir["sas.sascalc"] = os.path.join("src", "sas", "sascalc")
241packages.append("sas.sascalc")
242
243# sas.sascalc.invariant
244package_dir["sas.sascalc.invariant"] = os.path.join(
245    "src", "sas", "sascalc", "invariant")
246packages.extend(["sas.sascalc.invariant"])
247
248# sas.sasgui.guiframe
249guiframe_path = os.path.join("src", "sas", "sasgui", "guiframe")
250package_dir["sas.sasgui.guiframe"] = guiframe_path
251package_dir["sas.sasgui.guiframe.local_perspectives"] = os.path.join(
252    os.path.join(guiframe_path, "local_perspectives"))
253package_data["sas.sasgui.guiframe"] = ['images/*', 'media/*']
254packages.extend(
255    ["sas.sasgui.guiframe", "sas.sasgui.guiframe.local_perspectives"])
256# build local plugin
257for d in os.listdir(os.path.join(guiframe_path, "local_perspectives")):
258    if d not in ['.svn', '__init__.py', '__init__.pyc']:
259        package_name = "sas.sasgui.guiframe.local_perspectives." + d
260        packages.append(package_name)
261        package_dir[package_name] = os.path.join(
262            guiframe_path, "local_perspectives", d)
263
264# sas.sascalc.dataloader
265package_dir["sas.sascalc.dataloader"] = os.path.join(
266    "src", "sas", "sascalc", "dataloader")
267package_data["sas.sascalc.dataloader.readers"] = ['schema/*.xsd']
268packages.extend(["sas.sascalc.dataloader", "sas.sascalc.dataloader.readers",
269                 "sas.sascalc.dataloader.readers.schema"])
270
271
272# sas.sascalc.calculator
273gen_dir = os.path.join("src", "sas", "sascalc", "calculator", "c_extensions")
274package_dir["sas.sascalc.calculator.core"] = gen_dir
275package_dir["sas.sascalc.calculator"] = os.path.join(
276    "src", "sas", "sascalc", "calculator")
277packages.extend(["sas.sascalc.calculator", "sas.sascalc.calculator.core"])
278ext_modules.append(Extension("sas.sascalc.calculator.core.sld2i",
279                             sources=[
280                                 os.path.join(gen_dir, "sld2i_module.c"),
281                                 os.path.join(gen_dir, "sld2i.c"),
282                                 os.path.join(gen_dir, "libfunc.c"),
283                                 os.path.join(gen_dir, "librefl.c"),
284                             ],
285                             include_dirs=[gen_dir],
286                             )
287                   )
288
289# sas.sascalc.pr
290srcdir = os.path.join("src", "sas", "sascalc", "pr", "c_extensions")
291package_dir["sas.sascalc.pr.core"] = srcdir
292package_dir["sas.sascalc.pr"] = os.path.join("src", "sas", "sascalc", "pr")
293packages.extend(["sas.sascalc.pr", "sas.sascalc.pr.core"])
294ext_modules.append(Extension("sas.sascalc.pr.core.pr_inversion",
295                             sources=[os.path.join(srcdir, "Cinvertor.c"),
296                                      os.path.join(srcdir, "invertor.c"),
297                                      ],
298                             include_dirs=[],
299                             ))
300
301
302# sas.sascalc.file_converter
303mydir = os.path.join("src", "sas", "sascalc", "file_converter", "c_ext")
304package_dir["sas.sascalc.file_converter.core"] = mydir
305package_dir["sas.sascalc.file_converter"] = os.path.join(
306    "src", "sas", "sascalc", "file_converter")
307packages.extend(["sas.sascalc.file_converter",
308                 "sas.sascalc.file_converter.core"])
309ext_modules.append(Extension("sas.sascalc.file_converter.core.bsl_loader",
310                             sources=[os.path.join(mydir, "bsl_loader.c")],
311                             include_dirs=[np.get_include()],
312                             ))
313
314# sas.sascalc.corfunc
315package_dir["sas.sascalc.corfunc"] = os.path.join(
316    "src", "sas", "sascalc", "corfunc")
317
318packages.extend(["sas.sascalc.corfunc"])
319
320# sas.sascalc.fit
321package_dir["sas.sascalc.fit"] = os.path.join("src", "sas", "sascalc", "fit")
322packages.append("sas.sascalc.fit")
323
324# Perspectives
325package_dir["sas.sasgui.perspectives"] = os.path.join(
326    "src", "sas", "sasgui", "perspectives")
327package_dir["sas.sasgui.perspectives.pr"] = os.path.join(
328    "src", "sas", "sasgui", "perspectives", "pr")
329packages.extend(["sas.sasgui.perspectives", "sas.sasgui.perspectives.pr"])
330package_data["sas.sasgui.perspectives.pr"] = ['media/*']
331
332package_dir["sas.sasgui.perspectives.invariant"] = os.path.join(
333    "src", "sas", "sasgui", "perspectives", "invariant")
334packages.extend(["sas.sasgui.perspectives.invariant"])
335package_data['sas.sasgui.perspectives.invariant'] = [
336    os.path.join("media", '*')]
337
338package_dir["sas.sasgui.perspectives.fitting"] = os.path.join(
339    "src", "sas", "sasgui", "perspectives", "fitting")
340package_dir["sas.sasgui.perspectives.fitting.plugin_models"] = os.path.join(
341    "src", "sas", "sasgui", "perspectives", "fitting", "plugin_models")
342packages.extend(["sas.sasgui.perspectives.fitting",
343                 "sas.sasgui.perspectives.fitting.plugin_models"])
344package_data['sas.sasgui.perspectives.fitting'] = [
345    'media/*', 'plugin_models/*']
346
347packages.extend(["sas.sasgui.perspectives",
348                 "sas.sasgui.perspectives.calculator"])
349package_data['sas.sasgui.perspectives.calculator'] = ['images/*', 'media/*']
350
351package_dir["sas.sasgui.perspectives.corfunc"] = os.path.join(
352    "src", "sas", "sasgui", "perspectives", "corfunc")
353packages.extend(["sas.sasgui.perspectives.corfunc"])
354package_data['sas.sasgui.perspectives.corfunc'] = ['media/*']
355
356package_dir["sas.sasgui.perspectives.file_converter"] = os.path.join(
357    "src", "sas", "sasgui", "perspectives", "file_converter")
358packages.extend(["sas.sasgui.perspectives.file_converter"])
359package_data['sas.sasgui.perspectives.file_converter'] = ['media/*']
360
361# Data util
362package_dir["sas.sascalc.data_util"] = os.path.join(
363    "src", "sas", "sascalc", "data_util")
364packages.append("sas.sascalc.data_util")
365
366# Plottools
367package_dir["sas.sasgui.plottools"] = os.path.join(
368    "src", "sas", "sasgui", "plottools")
369packages.append("sas.sasgui.plottools")
370
371# # Last of the sas.models
372# package_dir["sas.models"] = os.path.join("src", "sas", "models")
373# packages.append("sas.models")
374
375EXTENSIONS = [".c", ".cpp"]
376
377
378def append_file(file_list, dir_path):
379    """
380    Add sources file to sources
381    """
382    for f in os.listdir(dir_path):
383        if os.path.isfile(os.path.join(dir_path, f)):
384            _, ext = os.path.splitext(f)
385            if ext.lower() in EXTENSIONS:
386                file_list.append(os.path.join(dir_path, f))
387        elif os.path.isdir(os.path.join(dir_path, f)) and \
388                not f.startswith("."):
389            sub_dir = os.path.join(dir_path, f)
390            for new_f in os.listdir(sub_dir):
391                if os.path.isfile(os.path.join(sub_dir, new_f)):
392                    _, ext = os.path.splitext(new_f)
393                    if ext.lower() in EXTENSIONS:
394                        file_list.append(os.path.join(sub_dir, new_f))
395
396
397# Comment out the following to avoid rebuilding all the models
398file_sources = []
399append_file(file_sources, gen_dir)
400
401# Wojtek's hacky way to add doc files while bundling egg
402# def add_doc_files(directory):
403#    paths = []
404#    for (path, directories, filenames) in os.walk(directory):
405#        for filename in filenames:
406#            paths.append(os.path.join(path, filename))
407#    return paths
408
409#doc_files = add_doc_files('doc')
410
411# SasView
412package_data['sas'] = ['logging.ini']
413package_data['sas.sasview'] = ['images/*',
414                               'media/*',
415                               'test/*.txt',
416                               'test/1d_data/*',
417                               'test/2d_data/*',
418                               'test/convertible_files/*',
419                               'test/coordinate_data/*',
420                               'test/image_data/*',
421                               'test/media/*',
422                               'test/other_files/*',
423                               'test/save_states/*',
424                               'test/sesans_data/*',
425                               'test/upcoming_formats/*',
426                               ]
427packages.append("sas.sasview")
428
429required = [
430    'bumps>=0.7.5.9', 'periodictable>=1.5.0', 'pyparsing<2.0.0',
431
432    # 'lxml>=2.2.2',
433    'lxml', 'h5py',
434
435    # The following dependecies won't install automatically, so assume them
436    # The numbers should be bumped up for matplotlib and wxPython as well.
437    # 'numpy>=1.4.1', 'scipy>=0.7.2', 'matplotlib>=0.99.1.1',
438    # 'wxPython>=2.8.11', 'pil',
439]
440
441if os.name == 'nt':
442    required.extend(['html5lib', 'reportlab'])
443else:
444    # 'pil' is now called 'pillow'
445    required.extend(['pillow'])
446
447# Set up SasView
448setup(
449    name="sasview",
450    version=VERSION,
451    description="SasView application",
452    author="SasView Team",
453    author_email="developers@sasview.org",
454    url="http://sasview.org",
455    license="PSF",
456    keywords="small-angle x-ray and neutron scattering analysis",
457    download_url="https://github.com/SasView/sasview.git",
458    package_dir=package_dir,
459    packages=packages,
460    package_data=package_data,
461    ext_modules=ext_modules,
462    install_requires=required,
463    zip_safe=False,
464    entry_points={
465        'console_scripts': [
466            "sasview = sas.sasview.sasview:run_gui",
467        ]
468    },
469    cmdclass={'build_ext': build_ext_subclass,
470              'docs': BuildSphinxCommand,
471              'disable_openmp': DisableOpenMPCommand}
472)
Note: See TracBrowser for help on using the repository browser.