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") |
Brian Silverman | 1720b7a | 2018-09-02 17:53:46 -0700 | [diff] [blame] | 5 | load("@//tools/build_rules:select.bzl", "compiler_select") |
Austin Schuh | 1f9aeb4 | 2015-11-12 23:34:49 -0800 | [diff] [blame] | 6 | |
| 7 | # We can't create _wrapper.so in the slycot folder, and can't move it. |
| 8 | # The best way I found to do this is to modify _wrapper.pyf to instead generate |
| 9 | # a _fortranwrapper.so library, and then place a _wrapper.py file in slycot/ |
| 10 | # which loads _fortranwrapper from the correct location. This means that I |
| 11 | # don't need to modify the repository. |
| 12 | genrule( |
Brian Silverman | 6470f44 | 2018-08-05 12:08:16 -0700 | [diff] [blame] | 13 | name = "_fortranwrapper_pyf", |
| 14 | srcs = ["slycot/src/_wrapper.pyf"], |
| 15 | outs = ["slycot/src/_fortranwrapper.pyf"], |
| 16 | cmd = "cat $(SRCS) | sed 's/_wrapper/_fortranwrapper/' > $(OUTS)", |
| 17 | restricted_to = ["@//tools:k8"], |
Austin Schuh | 1f9aeb4 | 2015-11-12 23:34:49 -0800 | [diff] [blame] | 18 | ) |
| 19 | |
Brian Silverman | d3ad165 | 2018-02-18 22:16:29 -0500 | [diff] [blame] | 20 | # The contents of the file telling f2py how to translate various types. The |
| 21 | # format doesn't seem to be very well-documented, but this seems to make all the |
| 22 | # argument types match up. |
Brian Silverman | 6470f44 | 2018-08-05 12:08:16 -0700 | [diff] [blame] | 23 | _f2py_f2cmap_contents = """{ |
Brian Silverman | d3ad165 | 2018-02-18 22:16:29 -0500 | [diff] [blame] | 24 | "integer": { |
| 25 | "check m>=0": "long", |
| 26 | "check n>=0": "long", |
| 27 | "check p>=0": "long", |
| 28 | "": "long", |
| 29 | }, |
| 30 | "logical": { |
| 31 | "": "long", |
| 32 | }, |
Brian Silverman | 6470f44 | 2018-08-05 12:08:16 -0700 | [diff] [blame] | 33 | }""" |
Brian Silverman | d3ad165 | 2018-02-18 22:16:29 -0500 | [diff] [blame] | 34 | |
Austin Schuh | 1f9aeb4 | 2015-11-12 23:34:49 -0800 | [diff] [blame] | 35 | # Now generate the module wrapper. |
| 36 | genrule( |
Brian Silverman | 6470f44 | 2018-08-05 12:08:16 -0700 | [diff] [blame] | 37 | name = "_fortranwrappermodule", |
| 38 | srcs = [ |
| 39 | "slycot/src/analysis.pyf", |
| 40 | "slycot/src/synthesis.pyf", |
| 41 | "slycot/src/_fortranwrapper.pyf", |
| 42 | "slycot/src/math.pyf", |
| 43 | "slycot/src/transform.pyf", |
| 44 | ], |
| 45 | outs = ["_fortranwrappermodule.c"], |
| 46 | cmd = "\n".join([ |
| 47 | "cat > .f2py_f2cmap <<END", |
| 48 | _f2py_f2cmap_contents, |
| 49 | "END", |
| 50 | "readlink -f .f2py_f2cmap", |
| 51 | " ".join([ |
| 52 | "$(location @python_repo//: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 | ]), |
Brian Silverman | d3ad165 | 2018-02-18 22:16:29 -0500 | [diff] [blame] | 62 | ]), |
Brian Silverman | 6470f44 | 2018-08-05 12:08:16 -0700 | [diff] [blame] | 63 | restricted_to = ["@//tools:k8"], |
| 64 | tools = [ |
| 65 | "@python_repo//:f2py", |
| 66 | ], |
Austin Schuh | 1f9aeb4 | 2015-11-12 23:34:49 -0800 | [diff] [blame] | 67 | ) |
| 68 | |
| 69 | # Build it. |
| 70 | cc_library( |
Brian Silverman | 6470f44 | 2018-08-05 12:08:16 -0700 | [diff] [blame] | 71 | name = "slycot_c", |
| 72 | srcs = [ |
| 73 | ":_fortranwrappermodule", |
| 74 | ], |
| 75 | copts = [ |
| 76 | "-Wno-error", |
| 77 | "-Wno-incompatible-pointer-types-discards-qualifiers", |
| 78 | "-Wno-cast-align", |
| 79 | "-Wno-unused-parameter", |
| 80 | "-Wno-missing-field-initializers", |
| 81 | "-Wno-unused-function", |
| 82 | ], |
| 83 | restricted_to = ["@//tools:k8"], |
| 84 | deps = [ |
| 85 | ":slicot", |
| 86 | "@python_repo//:python2.7_f2py", |
| 87 | "@python_repo//:python2.7_lib", |
| 88 | ], |
Austin Schuh | 1f9aeb4 | 2015-11-12 23:34:49 -0800 | [diff] [blame] | 89 | ) |
| 90 | |
Brian Silverman | 6470f44 | 2018-08-05 12:08:16 -0700 | [diff] [blame] | 91 | # Link it all together. Make sure all the deps get static linked into a single |
| 92 | # shared object, which will then be loaded by the Python interpreter. |
Austin Schuh | 1f9aeb4 | 2015-11-12 23:34:49 -0800 | [diff] [blame] | 93 | cc_binary( |
Brian Silverman | 6470f44 | 2018-08-05 12:08:16 -0700 | [diff] [blame] | 94 | name = "slycot/_fortranwrapper.so", |
| 95 | linkshared = True, |
| 96 | linkstatic = True, |
| 97 | restricted_to = ["@//tools:k8"], |
| 98 | deps = [ |
| 99 | ":slicot", |
| 100 | ":slycot_c", |
| 101 | ], |
Austin Schuh | 1f9aeb4 | 2015-11-12 23:34:49 -0800 | [diff] [blame] | 102 | ) |
| 103 | |
| 104 | # Generate the _wrapper file which loads _fortranwrapper and pretends. |
| 105 | genrule( |
Brian Silverman | 6470f44 | 2018-08-05 12:08:16 -0700 | [diff] [blame] | 106 | name = "_wrapper", |
| 107 | outs = ["slycot/_wrapper.py"], |
| 108 | cmd = "echo \"from slycot._fortranwrapper import *\" > $(OUTS)", |
| 109 | output_to_bindir = True, |
Austin Schuh | 1f9aeb4 | 2015-11-12 23:34:49 -0800 | [diff] [blame] | 110 | ) |
| 111 | |
| 112 | # Now present a python library for slycot |
| 113 | py_library( |
Brian Silverman | 6470f44 | 2018-08-05 12:08:16 -0700 | [diff] [blame] | 114 | name = "slycot", |
| 115 | srcs = [ |
| 116 | "slycot/__init__.py", |
| 117 | "slycot/analysis.py", |
| 118 | "slycot/examples.py", |
| 119 | "slycot/math.py", |
| 120 | "slycot/synthesis.py", |
| 121 | "slycot/transform.py", |
| 122 | ":_wrapper", |
| 123 | ], |
| 124 | data = [ |
| 125 | ":slycot/_fortranwrapper.so", |
| 126 | ], |
| 127 | imports = ["."], |
| 128 | restricted_to = ["@//tools:k8"], |
| 129 | visibility = ["//visibility:public"], |
Austin Schuh | 1f9aeb4 | 2015-11-12 23:34:49 -0800 | [diff] [blame] | 130 | ) |
Brian Silverman | d3ad165 | 2018-02-18 22:16:29 -0500 | [diff] [blame] | 131 | |
| 132 | f2c_library( |
Brian Silverman | 6470f44 | 2018-08-05 12:08:16 -0700 | [diff] [blame] | 133 | name = "slicot", |
| 134 | srcs = glob(["slycot/src/*.f"]), |
| 135 | copts = [ |
| 136 | # This gets triggered because it doesn't realize xerbla doesn't return. |
| 137 | # TODO(Brian): Try and get __attribute__((noreturn)) on xerbla somehow. |
| 138 | "-Wno-uninitialized", |
Brian Silverman | 1720b7a | 2018-09-02 17:53:46 -0700 | [diff] [blame] | 139 | ] + compiler_select({ |
| 140 | "clang": [ |
| 141 | ], |
| 142 | "gcc": [ |
| 143 | "-Wno-discarded-qualifiers", |
| 144 | ], |
| 145 | }), |
Brian Silverman | 6470f44 | 2018-08-05 12:08:16 -0700 | [diff] [blame] | 146 | visibility = ["//visibility:public"], |
| 147 | deps = [ |
| 148 | "@clapack", |
| 149 | ], |
Brian Silverman | d3ad165 | 2018-02-18 22:16:29 -0500 | [diff] [blame] | 150 | ) |