blob: 746317373459e6b9a3a08948f0112a59bde2015e [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",
82 ],
83 restricted_to = ["@//tools:k8"],
84 deps = [
85 ":slicot",
86 "@python_repo//:python2.7_f2py",
87 "@python_repo//:python2.7_lib",
88 ],
Austin Schuh1f9aeb42015-11-12 23:34:49 -080089)
90
Brian Silverman6470f442018-08-05 12:08:16 -070091# 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 Schuh1f9aeb42015-11-12 23:34:49 -080093cc_binary(
Brian Silverman6470f442018-08-05 12:08:16 -070094 name = "slycot/_fortranwrapper.so",
95 linkshared = True,
96 linkstatic = True,
97 restricted_to = ["@//tools:k8"],
98 deps = [
99 ":slicot",
100 ":slycot_c",
101 ],
Austin Schuh1f9aeb42015-11-12 23:34:49 -0800102)
103
104# Generate the _wrapper file which loads _fortranwrapper and pretends.
105genrule(
Brian Silverman6470f442018-08-05 12:08:16 -0700106 name = "_wrapper",
107 outs = ["slycot/_wrapper.py"],
108 cmd = "echo \"from slycot._fortranwrapper import *\" > $(OUTS)",
109 output_to_bindir = True,
Austin Schuh1f9aeb42015-11-12 23:34:49 -0800110)
111
112# Now present a python library for slycot
113py_library(
Brian Silverman6470f442018-08-05 12:08:16 -0700114 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 Schuh1f9aeb42015-11-12 23:34:49 -0800130)
Brian Silvermand3ad1652018-02-18 22:16:29 -0500131
132f2c_library(
Brian Silverman6470f442018-08-05 12:08:16 -0700133 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 Silverman1720b7a2018-09-02 17:53:46 -0700139 ] + compiler_select({
140 "clang": [
141 ],
142 "gcc": [
143 "-Wno-discarded-qualifiers",
144 ],
145 }),
Brian Silverman6470f442018-08-05 12:08:16 -0700146 visibility = ["//visibility:public"],
147 deps = [
148 "@clapack",
149 ],
Brian Silvermand3ad1652018-02-18 22:16:29 -0500150)