blob: 368b87dcd836b134f12047afa09919d350abe7b1 [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")
Austin Schuh1f9aeb42015-11-12 23:34:49 -08005
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.
11genrule(
Brian Silverman6470f442018-08-05 12:08:16 -070012 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 Schuh1f9aeb42015-11-12 23:34:49 -080017)
18
Brian Silvermand3ad1652018-02-18 22:16:29 -050019# 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 Silverman6470f442018-08-05 12:08:16 -070022_f2py_f2cmap_contents = """{
Brian Silvermand3ad1652018-02-18 22:16:29 -050023"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 Silverman6470f442018-08-05 12:08:16 -070032}"""
Brian Silvermand3ad1652018-02-18 22:16:29 -050033
Austin Schuh1f9aeb42015-11-12 23:34:49 -080034# Now generate the module wrapper.
35genrule(
Brian Silverman6470f442018-08-05 12:08:16 -070036 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 Silvermand3ad1652018-02-18 22:16:29 -050061 ]),
Brian Silverman6470f442018-08-05 12:08:16 -070062 restricted_to = ["@//tools:k8"],
63 tools = [
64 "@python_repo//:f2py",
65 ],
Austin Schuh1f9aeb42015-11-12 23:34:49 -080066)
67
68# Build it.
69cc_library(
Brian Silverman6470f442018-08-05 12:08:16 -070070 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 Schuh1f9aeb42015-11-12 23:34:49 -080088)
89
Brian Silverman6470f442018-08-05 12:08:16 -070090# 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 Schuh1f9aeb42015-11-12 23:34:49 -080092cc_binary(
Brian Silverman6470f442018-08-05 12:08:16 -070093 name = "slycot/_fortranwrapper.so",
94 linkshared = True,
95 linkstatic = True,
96 restricted_to = ["@//tools:k8"],
97 deps = [
98 ":slicot",
99 ":slycot_c",
100 ],
Austin Schuh1f9aeb42015-11-12 23:34:49 -0800101)
102
103# Generate the _wrapper file which loads _fortranwrapper and pretends.
104genrule(
Brian Silverman6470f442018-08-05 12:08:16 -0700105 name = "_wrapper",
106 outs = ["slycot/_wrapper.py"],
107 cmd = "echo \"from slycot._fortranwrapper import *\" > $(OUTS)",
108 output_to_bindir = True,
Austin Schuh1f9aeb42015-11-12 23:34:49 -0800109)
110
111# Now present a python library for slycot
112py_library(
Brian Silverman6470f442018-08-05 12:08:16 -0700113 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 Schuh1f9aeb42015-11-12 23:34:49 -0800129)
Brian Silvermand3ad1652018-02-18 22:16:29 -0500130
131f2c_library(
Brian Silverman6470f442018-08-05 12:08:16 -0700132 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 Silvermand3ad1652018-02-18 22:16:29 -0500143)