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", |
Austin Schuh | f6b9463 | 2019-02-02 22:11:27 -0800 | [diff] [blame] | 82 | "-Wno-unused-but-set-variable", |
Brian Silverman | 6470f44 | 2018-08-05 12:08:16 -0700 | [diff] [blame] | 83 | ], |
| 84 | restricted_to = ["@//tools:k8"], |
| 85 | deps = [ |
| 86 | ":slicot", |
| 87 | "@python_repo//:python2.7_f2py", |
| 88 | "@python_repo//:python2.7_lib", |
| 89 | ], |
Austin Schuh | 1f9aeb4 | 2015-11-12 23:34:49 -0800 | [diff] [blame] | 90 | ) |
| 91 | |
Brian Silverman | 6470f44 | 2018-08-05 12:08:16 -0700 | [diff] [blame] | 92 | # Link it all together. Make sure all the deps get static linked into a single |
| 93 | # shared object, which will then be loaded by the Python interpreter. |
Austin Schuh | 1f9aeb4 | 2015-11-12 23:34:49 -0800 | [diff] [blame] | 94 | cc_binary( |
Brian Silverman | 6470f44 | 2018-08-05 12:08:16 -0700 | [diff] [blame] | 95 | name = "slycot/_fortranwrapper.so", |
| 96 | linkshared = True, |
| 97 | linkstatic = True, |
| 98 | restricted_to = ["@//tools:k8"], |
| 99 | deps = [ |
| 100 | ":slicot", |
| 101 | ":slycot_c", |
| 102 | ], |
Austin Schuh | 1f9aeb4 | 2015-11-12 23:34:49 -0800 | [diff] [blame] | 103 | ) |
| 104 | |
| 105 | # Generate the _wrapper file which loads _fortranwrapper and pretends. |
| 106 | genrule( |
Brian Silverman | 6470f44 | 2018-08-05 12:08:16 -0700 | [diff] [blame] | 107 | name = "_wrapper", |
| 108 | outs = ["slycot/_wrapper.py"], |
| 109 | cmd = "echo \"from slycot._fortranwrapper import *\" > $(OUTS)", |
| 110 | output_to_bindir = True, |
Austin Schuh | 1f9aeb4 | 2015-11-12 23:34:49 -0800 | [diff] [blame] | 111 | ) |
| 112 | |
| 113 | # Now present a python library for slycot |
| 114 | py_library( |
Brian Silverman | 6470f44 | 2018-08-05 12:08:16 -0700 | [diff] [blame] | 115 | name = "slycot", |
| 116 | srcs = [ |
| 117 | "slycot/__init__.py", |
| 118 | "slycot/analysis.py", |
| 119 | "slycot/examples.py", |
| 120 | "slycot/math.py", |
| 121 | "slycot/synthesis.py", |
| 122 | "slycot/transform.py", |
| 123 | ":_wrapper", |
| 124 | ], |
| 125 | data = [ |
| 126 | ":slycot/_fortranwrapper.so", |
| 127 | ], |
| 128 | imports = ["."], |
| 129 | restricted_to = ["@//tools:k8"], |
| 130 | visibility = ["//visibility:public"], |
Austin Schuh | 1f9aeb4 | 2015-11-12 23:34:49 -0800 | [diff] [blame] | 131 | ) |
Brian Silverman | d3ad165 | 2018-02-18 22:16:29 -0500 | [diff] [blame] | 132 | |
| 133 | f2c_library( |
Brian Silverman | 6470f44 | 2018-08-05 12:08:16 -0700 | [diff] [blame] | 134 | name = "slicot", |
| 135 | srcs = glob(["slycot/src/*.f"]), |
| 136 | copts = [ |
| 137 | # This gets triggered because it doesn't realize xerbla doesn't return. |
| 138 | # TODO(Brian): Try and get __attribute__((noreturn)) on xerbla somehow. |
| 139 | "-Wno-uninitialized", |
Brian Silverman | 1720b7a | 2018-09-02 17:53:46 -0700 | [diff] [blame] | 140 | ] + compiler_select({ |
| 141 | "clang": [ |
| 142 | ], |
| 143 | "gcc": [ |
Austin Schuh | a581f74 | 2019-12-22 14:47:13 -0800 | [diff] [blame] | 144 | "-Wno-unused-but-set-variable", |
Brian Silverman | 1720b7a | 2018-09-02 17:53:46 -0700 | [diff] [blame] | 145 | "-Wno-discarded-qualifiers", |
| 146 | ], |
| 147 | }), |
Brian Silverman | 6470f44 | 2018-08-05 12:08:16 -0700 | [diff] [blame] | 148 | visibility = ["//visibility:public"], |
| 149 | deps = [ |
| 150 | "@clapack", |
| 151 | ], |
Brian Silverman | d3ad165 | 2018-02-18 22:16:29 -0500 | [diff] [blame] | 152 | ) |