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