blob: 393de226bc1142ccb78f341bc33ed824f5346484 [file] [log] [blame]
Austin Schuh1f9aeb42015-11-12 23:34:49 -08001# TODO(austin): I bet this is wrong.
Brian Silverman6470f442018-08-05 12:08:16 -07002licenses(["restricted"])
Austin Schuh1f9aeb42015-11-12 23:34:49 -08003
Brian Silverman6470f442018-08-05 12:08:16 -07004load("@//tools/build_rules:fortran.bzl", "f2c_library")
Brian Silverman1720b7a2018-09-02 17:53:46 -07005load("@//tools/build_rules:select.bzl", "compiler_select")
Austin Schuh1f9aeb42015-11-12 23:34:49 -08006
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.
12genrule(
Brian Silverman6470f442018-08-05 12:08:16 -070013 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 Schuh1f9aeb42015-11-12 23:34:49 -080018)
19
Brian Silvermand3ad1652018-02-18 22:16:29 -050020# 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 Silverman6470f442018-08-05 12:08:16 -070023_f2py_f2cmap_contents = """{
Brian Silvermand3ad1652018-02-18 22:16:29 -050024"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 Silverman6470f442018-08-05 12:08:16 -070033}"""
Brian Silvermand3ad1652018-02-18 22:16:29 -050034
Austin Schuh1f9aeb42015-11-12 23:34:49 -080035# Now generate the module wrapper.
36genrule(
Brian Silverman6470f442018-08-05 12:08:16 -070037 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 Silvermand3ad1652018-02-18 22:16:29 -050062 ]),
Brian Silverman6470f442018-08-05 12:08:16 -070063 restricted_to = ["@//tools:k8"],
64 tools = [
65 "@python_repo//:f2py",
66 ],
Austin Schuh1f9aeb42015-11-12 23:34:49 -080067)
68
69# Build it.
70cc_library(
Brian Silverman6470f442018-08-05 12:08:16 -070071 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 Schuhf6b94632019-02-02 22:11:27 -080082 "-Wno-unused-but-set-variable",
Brian Silverman6470f442018-08-05 12:08:16 -070083 ],
84 restricted_to = ["@//tools:k8"],
85 deps = [
86 ":slicot",
87 "@python_repo//:python2.7_f2py",
88 "@python_repo//:python2.7_lib",
89 ],
Austin Schuh1f9aeb42015-11-12 23:34:49 -080090)
91
Brian Silverman6470f442018-08-05 12:08:16 -070092# 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 Schuh1f9aeb42015-11-12 23:34:49 -080094cc_binary(
Brian Silverman6470f442018-08-05 12:08:16 -070095 name = "slycot/_fortranwrapper.so",
96 linkshared = True,
97 linkstatic = True,
98 restricted_to = ["@//tools:k8"],
99 deps = [
100 ":slicot",
101 ":slycot_c",
102 ],
Austin Schuh1f9aeb42015-11-12 23:34:49 -0800103)
104
105# Generate the _wrapper file which loads _fortranwrapper and pretends.
106genrule(
Brian Silverman6470f442018-08-05 12:08:16 -0700107 name = "_wrapper",
108 outs = ["slycot/_wrapper.py"],
109 cmd = "echo \"from slycot._fortranwrapper import *\" > $(OUTS)",
110 output_to_bindir = True,
Austin Schuh1f9aeb42015-11-12 23:34:49 -0800111)
112
113# Now present a python library for slycot
114py_library(
Brian Silverman6470f442018-08-05 12:08:16 -0700115 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 Schuh1f9aeb42015-11-12 23:34:49 -0800131)
Brian Silvermand3ad1652018-02-18 22:16:29 -0500132
133f2c_library(
Brian Silverman6470f442018-08-05 12:08:16 -0700134 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 Silverman1720b7a2018-09-02 17:53:46 -0700140 ] + compiler_select({
141 "clang": [
142 ],
143 "gcc": [
Austin Schuha581f742019-12-22 14:47:13 -0800144 "-Wno-unused-but-set-variable",
Brian Silverman1720b7a2018-09-02 17:53:46 -0700145 "-Wno-discarded-qualifiers",
146 ],
147 }),
Brian Silverman6470f442018-08-05 12:08:16 -0700148 visibility = ["//visibility:public"],
149 deps = [
150 "@clapack",
151 ],
Brian Silvermand3ad1652018-02-18 22:16:29 -0500152)