Austin Schuh | 1f9aeb4 | 2015-11-12 23:34:49 -0800 | [diff] [blame] | 1 | # TODO(austin): I bet this is wrong. |
Brian Silverman | 6470f44 | 2018-08-05 12:08:16 -0700 | [diff] [blame] | 2 | licenses(["restricted"]) |
Austin Schuh | 1f9aeb4 | 2015-11-12 23:34:49 -0800 | [diff] [blame] | 3 | |
Brian Silverman | 6470f44 | 2018-08-05 12:08:16 -0700 | [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( |
Brian Silverman | 6470f44 | 2018-08-05 12:08:16 -0700 | [diff] [blame] | 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 | 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. |
Brian Silverman | 6470f44 | 2018-08-05 12:08:16 -0700 | [diff] [blame] | 22 | _f2py_f2cmap_contents = """{ |
Brian Silverman | d3ad165 | 2018-02-18 22:16:29 -0500 | [diff] [blame] | 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 | }, |
Brian Silverman | 6470f44 | 2018-08-05 12:08:16 -0700 | [diff] [blame] | 32 | }""" |
Brian Silverman | d3ad165 | 2018-02-18 22:16:29 -0500 | [diff] [blame] | 33 | |
Austin Schuh | 1f9aeb4 | 2015-11-12 23:34:49 -0800 | [diff] [blame] | 34 | # Now generate the module wrapper. |
| 35 | genrule( |
Brian Silverman | 6470f44 | 2018-08-05 12:08:16 -0700 | [diff] [blame] | 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"], |
| 45 | cmd = "\n".join([ |
| 46 | "cat > .f2py_f2cmap <<END", |
| 47 | _f2py_f2cmap_contents, |
| 48 | "END", |
| 49 | "readlink -f .f2py_f2cmap", |
| 50 | " ".join([ |
| 51 | "$(location @python_repo//:f2py)", |
| 52 | "$(location :slycot/src/_fortranwrapper.pyf)", |
| 53 | "--include-paths external/slycot_repo/slycot/src/", |
| 54 | "--coutput $(OUTS)", |
| 55 | ]), |
| 56 | " ".join([ |
| 57 | "sed", |
| 58 | "\"s/Generation date.*/Generation date: redacted/\"", |
| 59 | "-i $(OUTS)", |
| 60 | ]), |
Brian Silverman | d3ad165 | 2018-02-18 22:16:29 -0500 | [diff] [blame] | 61 | ]), |
Brian Silverman | 6470f44 | 2018-08-05 12:08:16 -0700 | [diff] [blame] | 62 | restricted_to = ["@//tools:k8"], |
| 63 | tools = [ |
| 64 | "@python_repo//:f2py", |
| 65 | ], |
Austin Schuh | 1f9aeb4 | 2015-11-12 23:34:49 -0800 | [diff] [blame] | 66 | ) |
| 67 | |
| 68 | # Build it. |
| 69 | cc_library( |
Brian Silverman | 6470f44 | 2018-08-05 12:08:16 -0700 | [diff] [blame] | 70 | name = "slycot_c", |
| 71 | srcs = [ |
| 72 | ":_fortranwrappermodule", |
| 73 | ], |
| 74 | copts = [ |
| 75 | "-Wno-error", |
| 76 | "-Wno-incompatible-pointer-types-discards-qualifiers", |
| 77 | "-Wno-cast-align", |
| 78 | "-Wno-unused-parameter", |
| 79 | "-Wno-missing-field-initializers", |
| 80 | "-Wno-unused-function", |
| 81 | ], |
| 82 | restricted_to = ["@//tools:k8"], |
| 83 | deps = [ |
| 84 | ":slicot", |
| 85 | "@python_repo//:python2.7_f2py", |
| 86 | "@python_repo//:python2.7_lib", |
| 87 | ], |
Austin Schuh | 1f9aeb4 | 2015-11-12 23:34:49 -0800 | [diff] [blame] | 88 | ) |
| 89 | |
Brian Silverman | 6470f44 | 2018-08-05 12:08:16 -0700 | [diff] [blame] | 90 | # Link it all together. Make sure all the deps get static linked into a single |
| 91 | # shared object, which will then be loaded by the Python interpreter. |
Austin Schuh | 1f9aeb4 | 2015-11-12 23:34:49 -0800 | [diff] [blame] | 92 | cc_binary( |
Brian Silverman | 6470f44 | 2018-08-05 12:08:16 -0700 | [diff] [blame] | 93 | name = "slycot/_fortranwrapper.so", |
| 94 | linkshared = True, |
| 95 | linkstatic = True, |
| 96 | restricted_to = ["@//tools:k8"], |
| 97 | deps = [ |
| 98 | ":slicot", |
| 99 | ":slycot_c", |
| 100 | ], |
Austin Schuh | 1f9aeb4 | 2015-11-12 23:34:49 -0800 | [diff] [blame] | 101 | ) |
| 102 | |
| 103 | # Generate the _wrapper file which loads _fortranwrapper and pretends. |
| 104 | genrule( |
Brian Silverman | 6470f44 | 2018-08-05 12:08:16 -0700 | [diff] [blame] | 105 | name = "_wrapper", |
| 106 | outs = ["slycot/_wrapper.py"], |
| 107 | cmd = "echo \"from slycot._fortranwrapper import *\" > $(OUTS)", |
| 108 | output_to_bindir = True, |
Austin Schuh | 1f9aeb4 | 2015-11-12 23:34:49 -0800 | [diff] [blame] | 109 | ) |
| 110 | |
| 111 | # Now present a python library for slycot |
| 112 | py_library( |
Brian Silverman | 6470f44 | 2018-08-05 12:08:16 -0700 | [diff] [blame] | 113 | name = "slycot", |
| 114 | srcs = [ |
| 115 | "slycot/__init__.py", |
| 116 | "slycot/analysis.py", |
| 117 | "slycot/examples.py", |
| 118 | "slycot/math.py", |
| 119 | "slycot/synthesis.py", |
| 120 | "slycot/transform.py", |
| 121 | ":_wrapper", |
| 122 | ], |
| 123 | data = [ |
| 124 | ":slycot/_fortranwrapper.so", |
| 125 | ], |
| 126 | imports = ["."], |
| 127 | restricted_to = ["@//tools:k8"], |
| 128 | visibility = ["//visibility:public"], |
Austin Schuh | 1f9aeb4 | 2015-11-12 23:34:49 -0800 | [diff] [blame] | 129 | ) |
Brian Silverman | d3ad165 | 2018-02-18 22:16:29 -0500 | [diff] [blame] | 130 | |
| 131 | f2c_library( |
Brian Silverman | 6470f44 | 2018-08-05 12:08:16 -0700 | [diff] [blame] | 132 | name = "slicot", |
| 133 | srcs = glob(["slycot/src/*.f"]), |
| 134 | copts = [ |
| 135 | # This gets triggered because it doesn't realize xerbla doesn't return. |
| 136 | # TODO(Brian): Try and get __attribute__((noreturn)) on xerbla somehow. |
| 137 | "-Wno-uninitialized", |
| 138 | ], |
| 139 | visibility = ["//visibility:public"], |
| 140 | deps = [ |
| 141 | "@clapack", |
| 142 | ], |
Brian Silverman | d3ad165 | 2018-02-18 22:16:29 -0500 | [diff] [blame] | 143 | ) |