Austin Schuh | 1f9aeb4 | 2015-11-12 23:34:49 -0800 | [diff] [blame^] | 1 | # TODO(austin): I bet this is wrong. |
| 2 | licenses(['restricted']) |
| 3 | |
| 4 | load('/tools/build_rules/fortran', 'fortran_library') |
| 5 | |
| 6 | # We can't create _wrapper.so in the slycot folder, and can't move it. |
| 7 | # The best way I found to do this is to modify _wrapper.pyf to instead generate |
| 8 | # a _fortranwrapper.so library, and then place a _wrapper.py file in slycot/ |
| 9 | # which loads _fortranwrapper from the correct location. This means that I |
| 10 | # don't need to modify the repository. |
| 11 | genrule( |
| 12 | name = '_fortranwrapper_pyf', |
| 13 | srcs = ['slycot/src/_wrapper.pyf'], |
| 14 | outs = ['slycot/src/_fortranwrapper.pyf'], |
| 15 | cmd = 'cat $(SRCS) | sed \'s/_wrapper/_fortranwrapper/\' > $(OUTS)' |
| 16 | ) |
| 17 | |
| 18 | # Now generate the module wrapper. |
| 19 | genrule( |
| 20 | name = '_fortranwrappermodule', |
| 21 | srcs = [ |
| 22 | 'slycot/src/analysis.pyf', |
| 23 | 'slycot/src/synthesis.pyf', |
| 24 | 'slycot/src/_fortranwrapper.pyf', |
| 25 | 'slycot/src/math.pyf', |
| 26 | 'slycot/src/transform.pyf', |
| 27 | ], |
| 28 | outs = ['_fortranwrappermodule.c'], |
| 29 | cmd = '/usr/bin/python /usr/bin/f2py $(location :slycot/src/_fortranwrapper.pyf) --include-paths external/slycot_repo/slycot/src/ --coutput $(OUTS)', |
| 30 | ) |
| 31 | |
| 32 | # Build it. |
| 33 | cc_library( |
| 34 | name = 'slycot_c', |
| 35 | srcs = [ |
| 36 | ':_fortranwrappermodule', |
| 37 | ], |
| 38 | deps = [ |
| 39 | ':fortran_files', |
| 40 | '@usr_repo//:python2.7_lib', |
| 41 | '@usr_repo//:python2.7_f2py', |
| 42 | ], |
| 43 | copts = [ |
| 44 | '-Wno-error', |
| 45 | '-Wno-incompatible-pointer-types-discards-qualifiers', |
| 46 | '-Wno-cast-align', |
| 47 | '-Wno-unused-parameter', |
| 48 | '-Wno-missing-field-initializers', |
| 49 | '-Wno-unused-function', |
| 50 | ], |
| 51 | ) |
| 52 | |
| 53 | # Now actually build the fortran files. |
| 54 | fortran_library( |
| 55 | name = 'fortran_files', |
| 56 | srcs = glob(['slycot/src/*.f']), |
| 57 | ) |
| 58 | |
| 59 | # Link it all together. Make sure it is dynamically linked since I don't know |
| 60 | # how to build the fortran files in statically to a single .so yet, and I'm not |
| 61 | # sure bazel does either. |
| 62 | cc_binary( |
| 63 | name = '_fortranwrapper.so', |
| 64 | deps = [ |
| 65 | ':fortran_files', |
| 66 | ':slycot_c', |
| 67 | ], |
| 68 | linkopts = ['-shared', '-lblas', '-llapack'], |
| 69 | linkstatic=0, |
| 70 | ) |
| 71 | |
| 72 | # Generate the _wrapper file which loads _fortranwrapper and pretends. |
| 73 | genrule( |
| 74 | name = '_wrapper', |
| 75 | outs = ['slycot/_wrapper.py'], |
| 76 | cmd = 'echo "from _fortranwrapper import *" > $(OUTS)', |
| 77 | output_to_bindir=1, |
| 78 | ) |
| 79 | |
| 80 | # Now present a python library for slycot |
| 81 | py_library( |
| 82 | name = 'slycot', |
| 83 | srcs = [ |
| 84 | 'slycot/analysis.py', |
| 85 | 'slycot/examples.py', |
| 86 | 'slycot/__init__.py', |
| 87 | 'slycot/math.py', |
| 88 | 'slycot/synthesis.py', |
| 89 | 'slycot/transform.py', |
| 90 | ':_wrapper', |
| 91 | ], |
| 92 | data = [ |
| 93 | ':_fortranwrapper.so', |
| 94 | ], |
| 95 | visibility = ['//visibility:public'], |
| 96 | ) |