Brian Silverman | 8dd8a05 | 2018-09-02 17:08:31 -0700 | [diff] [blame] | 1 | load("@//tools/build_rules:select.bzl", "compiler_select") |
Brian Silverman | d3ad165 | 2018-02-18 22:16:29 -0500 | [diff] [blame] | 2 | |
Brian Silverman | d3ad165 | 2018-02-18 22:16:29 -0500 | [diff] [blame] | 3 | f2c_copts = compiler_select({ |
Brian Silverman | 8dd8a05 | 2018-09-02 17:08:31 -0700 | [diff] [blame] | 4 | "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 Silverman | d3ad165 | 2018-02-18 22:16:29 -0500 | [diff] [blame] | 15 | }) + [ |
Brian Silverman | 8dd8a05 | 2018-09-02 17:08:31 -0700 | [diff] [blame] | 16 | # 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 Silverman | d3ad165 | 2018-02-18 22:16:29 -0500 | [diff] [blame] | 21 | ] |
Brian Silverman | d3ad165 | 2018-02-18 22:16:29 -0500 | [diff] [blame] | 22 | |
Brian Silverman | 8dd8a05 | 2018-09-02 17:08:31 -0700 | [diff] [blame] | 23 | """Copts to use when compiling f2c-generated files. |
| 24 | |
| 25 | This is useful when building externally-f2ced files.""" |
Brian Silverman | d3ad165 | 2018-02-18 22:16:29 -0500 | [diff] [blame] | 26 | |
| 27 | def f2c_library(name, srcs, copts = [], **kwargs): |
Austin Schuh | 8e17be9 | 2019-12-24 09:32:11 -0800 | [diff] [blame] | 28 | """Converts Fortran code to C and then compiles it. |
Brian Silverman | d3ad165 | 2018-02-18 22:16:29 -0500 | [diff] [blame] | 29 | |
Austin Schuh | 8e17be9 | 2019-12-24 09:32:11 -0800 | [diff] [blame] | 30 | 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 Silverman | d3ad165 | 2018-02-18 22:16:29 -0500 | [diff] [blame] | 35 | |
Austin Schuh | 8e17be9 | 2019-12-24 09:32:11 -0800 | [diff] [blame] | 36 | 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 Silverman | d3ad165 | 2018-02-18 22:16:29 -0500 | [diff] [blame] | 42 | |
Austin Schuh | 8e17be9 | 2019-12-24 09:32:11 -0800 | [diff] [blame] | 43 | 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 | ) |