blob: 9853c40748489a8aa7e5eb5e571c010d8951cb63 [file] [log] [blame]
Austin Schuh1f9aeb42015-11-12 23:34:49 -08001# TODO(austin): I bet this is wrong.
2licenses(['restricted'])
3
Brian Silvermand3ad1652018-02-18 22:16:29 -05004load('@//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(
12 name = '_fortranwrapper_pyf',
13 srcs = ['slycot/src/_wrapper.pyf'],
14 outs = ['slycot/src/_fortranwrapper.pyf'],
Austin Schuh9d92e6b2017-10-17 01:19:38 -070015 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.
22_f2py_f2cmap_contents = '''{
23"integer": {
24 "check m>=0": "long",
25 "check n>=0": "long",
26 "check p>=0": "long",
27 "": "long",
28},
29"logical": {
30 "": "long",
31},
32}'''
33
Austin Schuh1f9aeb42015-11-12 23:34:49 -080034# Now generate the module wrapper.
35genrule(
36 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'],
Brian Silvermand3ad1652018-02-18 22:16:29 -050045 cmd = '\n'.join([
46 'cat > .f2py_f2cmap <<END',
47 _f2py_f2cmap_contents,
48 'END',
49 'readlink -f .f2py_f2cmap',
50 ' '.join([
51 '/usr/bin/python',
52 '/usr/bin/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 ]),
62 ]),
Austin Schuh9d92e6b2017-10-17 01:19:38 -070063 restricted_to = ['@//tools:k8'],
Austin Schuh1f9aeb42015-11-12 23:34:49 -080064)
65
66# Build it.
67cc_library(
68 name = 'slycot_c',
69 srcs = [
70 ':_fortranwrappermodule',
71 ],
72 deps = [
Brian Silvermand3ad1652018-02-18 22:16:29 -050073 ':slicot',
Philipp Schrader9fc87e02018-03-10 20:36:39 -080074 '@python_repo//:python2.7_lib',
75 '@python_repo//:python2.7_f2py',
Austin Schuh1f9aeb42015-11-12 23:34:49 -080076 ],
77 copts = [
78 '-Wno-error',
79 '-Wno-incompatible-pointer-types-discards-qualifiers',
80 '-Wno-cast-align',
81 '-Wno-unused-parameter',
82 '-Wno-missing-field-initializers',
83 '-Wno-unused-function',
84 ],
Austin Schuh9d92e6b2017-10-17 01:19:38 -070085 restricted_to = ['@//tools:k8'],
Austin Schuh1f9aeb42015-11-12 23:34:49 -080086)
87
Brian Silvermand3ad1652018-02-18 22:16:29 -050088# Link it all together. Make sure it is dynamically linked so it can be
89# loaded by the Python interpreter.
Austin Schuh1f9aeb42015-11-12 23:34:49 -080090cc_binary(
91 name = '_fortranwrapper.so',
92 deps = [
Brian Silvermand3ad1652018-02-18 22:16:29 -050093 ':slicot',
Austin Schuh1f9aeb42015-11-12 23:34:49 -080094 ':slycot_c',
95 ],
Brian Silvermand1c19fb2016-07-07 00:10:57 -070096 linkstatic = False,
Brian Silvermand3ad1652018-02-18 22:16:29 -050097 linkshared = True,
Austin Schuh9d92e6b2017-10-17 01:19:38 -070098 restricted_to = ['@//tools:k8'],
Austin Schuh1f9aeb42015-11-12 23:34:49 -080099)
100
101# Generate the _wrapper file which loads _fortranwrapper and pretends.
102genrule(
103 name = '_wrapper',
104 outs = ['slycot/_wrapper.py'],
Brian Silvermand1c19fb2016-07-07 00:10:57 -0700105 cmd = 'echo "from external.slycot_repo._fortranwrapper import *" > $(OUTS)',
106 output_to_bindir = True,
Austin Schuh1f9aeb42015-11-12 23:34:49 -0800107)
108
109# Now present a python library for slycot
110py_library(
111 name = 'slycot',
112 srcs = [
113 'slycot/analysis.py',
114 'slycot/examples.py',
115 'slycot/__init__.py',
116 'slycot/math.py',
117 'slycot/synthesis.py',
118 'slycot/transform.py',
119 ':_wrapper',
120 ],
121 data = [
122 ':_fortranwrapper.so',
123 ],
124 visibility = ['//visibility:public'],
Austin Schuh9d92e6b2017-10-17 01:19:38 -0700125 restricted_to = ['@//tools:k8'],
Austin Schuh1f9aeb42015-11-12 23:34:49 -0800126)
Brian Silvermand3ad1652018-02-18 22:16:29 -0500127
128f2c_library(
129 name = 'slicot',
130 visibility = ['//visibility:public'],
131 srcs = glob(['slycot/src/*.f']),
132 copts = [
133 # This gets triggered because it doesn't realize xerbla doesn't return.
134 # TODO(Brian): Try and get __attribute__((noreturn)) on xerbla somehow.
135 '-Wno-uninitialized',
136 ],
137 deps = [
138 '@clapack',
139 ],
140)