blob: e8b25c102e153fffa91b16f0f55254d782476a1c [file] [log] [blame]
Brian Silvermand3ad1652018-02-18 22:16:29 -05001load('@//tools/build_rules:select.bzl', 'compiler_select')
2
Austin Schuh1f9aeb42015-11-12 23:34:49 -08003def _single_fortran_object_impl(ctx):
4 toolchain_cflags = (ctx.fragments.cpp.compiler_options([]) +
5 ctx.fragments.cpp.c_options +
Austin Schuh3b3fee02015-11-26 17:26:32 -08006 ctx.fragments.cpp.unfiltered_compiler_options([]) +
7 ['-fPIC', '-Wno-maybe-uninitialized', '-Wno-unused-dummy-argument',
Austin Schuh2461e5e2015-11-28 13:23:45 -08008 '-Wno-conversion', '-Wno-unused-variable', '-Wno-character-truncation'])
Austin Schuh1f9aeb42015-11-12 23:34:49 -08009
10 cmd = toolchain_cflags + ['-c', ctx.file.src.path, '-o', ctx.outputs.pic_o.path]
11 filtered_cmd = []
Brian Silvermana4a4ea62015-11-27 10:27:53 -050012 # Strip out the C/C++/Clang specific flags.
Austin Schuh1f9aeb42015-11-12 23:34:49 -080013 exclude_flags = ['-fcolor-diagnostics',
14 '-Wswitch-enum',
15 '-Wpointer-arith',
16 '-Wcast-qual',
17 '-Wwrite-strings',
18 '-Wsign-compare',
19 '-Wformat=2',
20 '-Werror',
Austin Schuh3b3fee02015-11-26 17:26:32 -080021 '-Wextra',
Austin Schuh1f9aeb42015-11-12 23:34:49 -080022 '-Wno-builtin-macro-redefined',
Austin Schuhc65df862015-12-27 14:04:49 -080023 '-Wunused-local-typedefs',
Brian Silvermana4a4ea62015-11-27 10:27:53 -050024 '-D__has_feature(x)=0',
25 '-fmacro-backtrace-limit=0']
Austin Schuh1f9aeb42015-11-12 23:34:49 -080026
27 for flag in cmd:
Brian Silvermaned79de62015-11-28 18:10:17 -050028 if flag not in exclude_flags and not (flag.startswith('-fsanitize') or
29 flag.startswith('-fno-sanitize')):
Austin Schuh1f9aeb42015-11-12 23:34:49 -080030 filtered_cmd.append(flag)
31
32 ctx.action(
Brian Silvermanb2215d02015-11-23 19:10:15 -050033 inputs = [ctx.file.src] + ctx.files._cc_toolchain,
Austin Schuh1f9aeb42015-11-12 23:34:49 -080034 outputs = [ctx.outputs.pic_o],
35 mnemonic = "Fortran",
36 executable = ctx.fragments.cpp.compiler_executable,
37 arguments = filtered_cmd,
38 progress_message = 'Building %s' % ctx.outputs.pic_o.short_path,
39 )
40
Brian Silvermanb200c172017-01-02 17:35:35 -080041def _define_fortran_output(src):
42 if not src.name.endswith('.f'):
Austin Schuh1f9aeb42015-11-12 23:34:49 -080043 fail('Fortran files must end in \'.f\'', 'src')
44
Brian Silvermanb200c172017-01-02 17:35:35 -080045 fortran_file_base = src.name[:-2]
Austin Schuh1f9aeb42015-11-12 23:34:49 -080046 return {
47 'pic_o': fortran_file_base + '.pic.o',
48 }
49
50
51_single_fortran_object = rule(
52 implementation = _single_fortran_object_impl,
53 attrs = {
54 'src': attr.label(single_file=True, allow_files=FileType(['.f'])),
55 'cc_libs': attr.label_list(providers=['cc']),
Brian Silvermanb2215d02015-11-23 19:10:15 -050056 # TODO(Brian): Replace this with something more fine-grained from the
57 # configuration fragment or something.
58 '_cc_toolchain': attr.label(
Brian Silverman385cd982015-12-20 18:26:32 -050059 default = Label('@//tools/cpp:toolchain'),
Brian Silvermanb2215d02015-11-23 19:10:15 -050060 ),
Austin Schuh1f9aeb42015-11-12 23:34:49 -080061 },
62 outputs = _define_fortran_output,
63 fragments = [
64 'cpp',
65 ],
66)
67
68def fortran_library(name, srcs, deps = [], visibility = None):
69 """Builds a shared library from a set of fortran files.
70
71 Args:
72 srcs: list of fortran files ending in .f
73 deps: cc_library or fortran_library dependencies.
74 """
75 pic_o_files = []
76 for src in srcs:
77 pic_o_file = src[:-2] + '.pic.o'
Brian Silvermanb2215d02015-11-23 19:10:15 -050078 _single_fortran_object(
79 name = name + '_' + pic_o_file,
80 src = src,
81 visibility = ['//visibility:private'],
Austin Schuh9d92e6b2017-10-17 01:19:38 -070082 restricted_to = ['@//tools:k8'],
Brian Silvermanb2215d02015-11-23 19:10:15 -050083 )
Austin Schuh1f9aeb42015-11-12 23:34:49 -080084 pic_o_files.append(pic_o_file)
85
86 native.cc_library(
87 name = name,
88 deps = deps,
89 srcs = pic_o_files,
90 linkopts = [
91 '-lgfortran',
92 ],
93 visibility = visibility,
Austin Schuh9d92e6b2017-10-17 01:19:38 -070094 restricted_to = ['@//tools:k8'],
Austin Schuh1f9aeb42015-11-12 23:34:49 -080095 )
Brian Silvermand3ad1652018-02-18 22:16:29 -050096
97f2c_copts = compiler_select({
98 'clang': [
99 '-Wno-incompatible-pointer-types-discards-qualifiers',
100 # Clang appears to be a bit over-eager about this and the comma operator.
101 '-Wno-sometimes-uninitialized',
102 ],
103 'gcc': [
104 # TODO(Brian): Remove this once we can actually disable all the warnings.
105 # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=43245 isn't fixed in our
106 # roborio toolchain yet, so we can't for now.
107 '-Wno-error',
108 ],
109}) + [
110 # f2c appears to know what it's doing without adding extra ().
111 '-Wno-parentheses',
112
113 '-Wno-unused-parameter',
114 '-Wno-missing-field-initializers',
115 '-Wno-unused-variable',
116]
117'''Copts to use when compiling f2c-generated files.
118
119This is useful when building externally-f2ced files.'''
120
121def f2c_library(name, srcs, copts = [], **kwargs):
122 '''Converts Fortran code to C and then compiles it.
123
124 Attrs:
125 srcs: .f source files
126 **kwargs: passed to native.cc_library
127 '''
128 c_srcs = [f[:-2] + '.c' for f in srcs]
129
130 out_dir = c_srcs[0].split('/')[:-1]
131 for c_src in c_srcs:
132 if c_src.split('/')[:-1] != out_dir:
133 # Need to figure out how to make multiple f2c calls or something to
134 # support this, and we haven't had a use case yet.
135 fail('Multiple output directories not supported', 'srcs')
136
137 native.genrule(
138 name = '_%s_f2c' % name,
139 visibility = ['//visibility:private'],
140 srcs = srcs,
141 outs = c_srcs,
142 tools = [
143 '@f2c',
144 ],
145 cmd = '$(location @f2c) -d$(@D)/%s $(SRCS)' % ('/'.join(out_dir),),
146 )
147 native.cc_library(
148 name = name,
149 srcs = c_srcs,
150 copts = f2c_copts + copts,
151 **kwargs
152 )