Austin Schuh | 1f9aeb4 | 2015-11-12 23:34:49 -0800 | [diff] [blame] | 1 | # TODO(austin): I bet this is wrong. |
| 2 | licenses(['restricted']) |
| 3 | |
Brian Silverman | d3ad165 | 2018-02-18 22:16:29 -0500 | [diff] [blame^] | 4 | load('@//tools/build_rules:fortran.bzl', 'f2c_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 | |
Brian Silverman | d3ad165 | 2018-02-18 22:16:29 -0500 | [diff] [blame^] | 19 | # The contents of the file telling f2py how to translate various types. The |
| 20 | # format doesn't seem to be very well-documented, but this seems to make all the |
| 21 | # argument types match up. |
| 22 | _f2py_f2cmap_contents = '''{ |
| 23 | "integer": { |
| 24 | "check m>=0": "long", |
| 25 | "check n>=0": "long", |
| 26 | "check p>=0": "long", |
| 27 | "": "long", |
| 28 | }, |
| 29 | "logical": { |
| 30 | "": "long", |
| 31 | }, |
| 32 | }''' |
| 33 | |
Austin Schuh | 1f9aeb4 | 2015-11-12 23:34:49 -0800 | [diff] [blame] | 34 | # Now generate the module wrapper. |
| 35 | genrule( |
| 36 | name = '_fortranwrappermodule', |
| 37 | srcs = [ |
| 38 | 'slycot/src/analysis.pyf', |
| 39 | 'slycot/src/synthesis.pyf', |
| 40 | 'slycot/src/_fortranwrapper.pyf', |
| 41 | 'slycot/src/math.pyf', |
| 42 | 'slycot/src/transform.pyf', |
| 43 | ], |
| 44 | outs = ['_fortranwrappermodule.c'], |
Brian Silverman | d3ad165 | 2018-02-18 22:16:29 -0500 | [diff] [blame^] | 45 | cmd = '\n'.join([ |
| 46 | 'cat > .f2py_f2cmap <<END', |
| 47 | _f2py_f2cmap_contents, |
| 48 | 'END', |
| 49 | 'readlink -f .f2py_f2cmap', |
| 50 | ' '.join([ |
| 51 | '/usr/bin/python', |
| 52 | '/usr/bin/f2py', |
| 53 | '$(location :slycot/src/_fortranwrapper.pyf)', |
| 54 | '--include-paths external/slycot_repo/slycot/src/', |
| 55 | '--coutput $(OUTS)', |
| 56 | ]), |
| 57 | ' '.join([ |
| 58 | 'sed', |
| 59 | '"s/Generation date.*/Generation date: redacted/"', |
| 60 | '-i $(OUTS)', |
| 61 | ]), |
| 62 | ]), |
Austin Schuh | 9d92e6b | 2017-10-17 01:19:38 -0700 | [diff] [blame] | 63 | restricted_to = ['@//tools:k8'], |
Austin Schuh | 1f9aeb4 | 2015-11-12 23:34:49 -0800 | [diff] [blame] | 64 | ) |
| 65 | |
| 66 | # Build it. |
| 67 | cc_library( |
| 68 | name = 'slycot_c', |
| 69 | srcs = [ |
| 70 | ':_fortranwrappermodule', |
| 71 | ], |
| 72 | deps = [ |
Brian Silverman | d3ad165 | 2018-02-18 22:16:29 -0500 | [diff] [blame^] | 73 | ':slicot', |
Austin Schuh | 1f9aeb4 | 2015-11-12 23:34:49 -0800 | [diff] [blame] | 74 | '@usr_repo//:python2.7_lib', |
| 75 | '@usr_repo//:python2.7_f2py', |
| 76 | ], |
| 77 | copts = [ |
| 78 | '-Wno-error', |
| 79 | '-Wno-incompatible-pointer-types-discards-qualifiers', |
| 80 | '-Wno-cast-align', |
| 81 | '-Wno-unused-parameter', |
| 82 | '-Wno-missing-field-initializers', |
| 83 | '-Wno-unused-function', |
| 84 | ], |
Austin Schuh | 9d92e6b | 2017-10-17 01:19:38 -0700 | [diff] [blame] | 85 | restricted_to = ['@//tools:k8'], |
Austin Schuh | 1f9aeb4 | 2015-11-12 23:34:49 -0800 | [diff] [blame] | 86 | ) |
| 87 | |
Brian Silverman | d3ad165 | 2018-02-18 22:16:29 -0500 | [diff] [blame^] | 88 | # Link it all together. Make sure it is dynamically linked so it can be |
| 89 | # loaded by the Python interpreter. |
Austin Schuh | 1f9aeb4 | 2015-11-12 23:34:49 -0800 | [diff] [blame] | 90 | cc_binary( |
| 91 | name = '_fortranwrapper.so', |
| 92 | deps = [ |
Brian Silverman | d3ad165 | 2018-02-18 22:16:29 -0500 | [diff] [blame^] | 93 | ':slicot', |
Austin Schuh | 1f9aeb4 | 2015-11-12 23:34:49 -0800 | [diff] [blame] | 94 | ':slycot_c', |
| 95 | ], |
Brian Silverman | d1c19fb | 2016-07-07 00:10:57 -0700 | [diff] [blame] | 96 | linkstatic = False, |
Brian Silverman | d3ad165 | 2018-02-18 22:16:29 -0500 | [diff] [blame^] | 97 | linkshared = True, |
Austin Schuh | 9d92e6b | 2017-10-17 01:19:38 -0700 | [diff] [blame] | 98 | restricted_to = ['@//tools:k8'], |
Austin Schuh | 1f9aeb4 | 2015-11-12 23:34:49 -0800 | [diff] [blame] | 99 | ) |
| 100 | |
| 101 | # Generate the _wrapper file which loads _fortranwrapper and pretends. |
| 102 | genrule( |
| 103 | name = '_wrapper', |
| 104 | outs = ['slycot/_wrapper.py'], |
Brian Silverman | d1c19fb | 2016-07-07 00:10:57 -0700 | [diff] [blame] | 105 | cmd = 'echo "from external.slycot_repo._fortranwrapper import *" > $(OUTS)', |
| 106 | output_to_bindir = True, |
Austin Schuh | 1f9aeb4 | 2015-11-12 23:34:49 -0800 | [diff] [blame] | 107 | ) |
| 108 | |
| 109 | # Now present a python library for slycot |
| 110 | py_library( |
| 111 | name = 'slycot', |
| 112 | srcs = [ |
| 113 | 'slycot/analysis.py', |
| 114 | 'slycot/examples.py', |
| 115 | 'slycot/__init__.py', |
| 116 | 'slycot/math.py', |
| 117 | 'slycot/synthesis.py', |
| 118 | 'slycot/transform.py', |
| 119 | ':_wrapper', |
| 120 | ], |
| 121 | data = [ |
| 122 | ':_fortranwrapper.so', |
| 123 | ], |
| 124 | visibility = ['//visibility:public'], |
Austin Schuh | 9d92e6b | 2017-10-17 01:19:38 -0700 | [diff] [blame] | 125 | restricted_to = ['@//tools:k8'], |
Austin Schuh | 1f9aeb4 | 2015-11-12 23:34:49 -0800 | [diff] [blame] | 126 | ) |
Brian Silverman | d3ad165 | 2018-02-18 22:16:29 -0500 | [diff] [blame^] | 127 | |
| 128 | f2c_library( |
| 129 | name = 'slicot', |
| 130 | visibility = ['//visibility:public'], |
| 131 | srcs = glob(['slycot/src/*.f']), |
| 132 | copts = [ |
| 133 | # This gets triggered because it doesn't realize xerbla doesn't return. |
| 134 | # TODO(Brian): Try and get __attribute__((noreturn)) on xerbla somehow. |
| 135 | '-Wno-uninitialized', |
| 136 | ], |
| 137 | deps = [ |
| 138 | '@clapack', |
| 139 | ], |
| 140 | ) |