blob: d55f1c1217ebd5d48d870640403306c3d789c0bb [file] [log] [blame]
Brian Silverman8dd8a052018-09-02 17:08:31 -07001load("@//tools/build_rules:select.bzl", "compiler_select")
Brian Silvermand3ad1652018-02-18 22:16:29 -05002
Brian Silvermand3ad1652018-02-18 22:16:29 -05003f2c_copts = compiler_select({
Brian Silverman8dd8a052018-09-02 17:08:31 -07004 "clang": [
5 "-Wno-incompatible-pointer-types-discards-qualifiers",
6 # Clang appears to be a bit over-eager about this and the comma operator.
7 "-Wno-sometimes-uninitialized",
8 ],
9 "gcc": [
10 # TODO(Brian): Remove this once we can actually disable all the warnings.
11 # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=43245 isn't fixed in our
12 # roborio toolchain yet, so we can't for now.
13 "-Wno-error",
14 ],
Brian Silvermand3ad1652018-02-18 22:16:29 -050015}) + [
Brian Silverman8dd8a052018-09-02 17:08:31 -070016 # f2c appears to know what it's doing without adding extra ().
17 "-Wno-parentheses",
18 "-Wno-unused-parameter",
19 "-Wno-missing-field-initializers",
20 "-Wno-unused-variable",
Brian Silvermand3ad1652018-02-18 22:16:29 -050021]
Brian Silvermand3ad1652018-02-18 22:16:29 -050022
Brian Silverman8dd8a052018-09-02 17:08:31 -070023"""Copts to use when compiling f2c-generated files.
24
25This is useful when building externally-f2ced files."""
Brian Silvermand3ad1652018-02-18 22:16:29 -050026
27def f2c_library(name, srcs, copts = [], **kwargs):
Austin Schuh8e17be92019-12-24 09:32:11 -080028 """Converts Fortran code to C and then compiles it.
Brian Silvermand3ad1652018-02-18 22:16:29 -050029
Austin Schuh8e17be92019-12-24 09:32:11 -080030 Attrs:
31 srcs: .f source files
32 **kwargs: passed to native.cc_library
33 """
34 c_srcs = [f[:-2] + ".c" for f in srcs]
Brian Silvermand3ad1652018-02-18 22:16:29 -050035
Austin Schuh8e17be92019-12-24 09:32:11 -080036 out_dir = c_srcs[0].split("/")[:-1]
37 for c_src in c_srcs:
38 if c_src.split("/")[:-1] != out_dir:
39 # Need to figure out how to make multiple f2c calls or something to
40 # support this, and we haven't had a use case yet.
41 fail("Multiple output directories not supported", "srcs")
Brian Silvermand3ad1652018-02-18 22:16:29 -050042
Austin Schuh8e17be92019-12-24 09:32:11 -080043 native.genrule(
44 name = "_%s_f2c" % name,
45 visibility = ["//visibility:private"],
46 srcs = srcs,
47 outs = c_srcs,
48 tools = [
49 "@f2c",
50 "@//tools/build_rules:quiet_success",
51 ],
52 cmd = " ".join([
53 "$(location @//tools/build_rules:quiet_success)",
54 "$(location @f2c)",
55 "-d$(@D)/%s" % ("/".join(out_dir),),
56 "$(SRCS)",
57 ]),
58 )
59 native.cc_library(
60 name = name,
61 srcs = c_srcs,
62 copts = f2c_copts + copts,
63 **kwargs
64 )