Brian Silverman | d3ad165 | 2018-02-18 22:16:29 -0500 | [diff] [blame^] | 1 | load('@//tools/build_rules:select.bzl', 'compiler_select') |
| 2 | |
Austin Schuh | 1f9aeb4 | 2015-11-12 23:34:49 -0800 | [diff] [blame] | 3 | def _single_fortran_object_impl(ctx): |
| 4 | toolchain_cflags = (ctx.fragments.cpp.compiler_options([]) + |
| 5 | ctx.fragments.cpp.c_options + |
Austin Schuh | 3b3fee0 | 2015-11-26 17:26:32 -0800 | [diff] [blame] | 6 | ctx.fragments.cpp.unfiltered_compiler_options([]) + |
| 7 | ['-fPIC', '-Wno-maybe-uninitialized', '-Wno-unused-dummy-argument', |
Austin Schuh | 2461e5e | 2015-11-28 13:23:45 -0800 | [diff] [blame] | 8 | '-Wno-conversion', '-Wno-unused-variable', '-Wno-character-truncation']) |
Austin Schuh | 1f9aeb4 | 2015-11-12 23:34:49 -0800 | [diff] [blame] | 9 | |
| 10 | cmd = toolchain_cflags + ['-c', ctx.file.src.path, '-o', ctx.outputs.pic_o.path] |
| 11 | filtered_cmd = [] |
Brian Silverman | a4a4ea6 | 2015-11-27 10:27:53 -0500 | [diff] [blame] | 12 | # Strip out the C/C++/Clang specific flags. |
Austin Schuh | 1f9aeb4 | 2015-11-12 23:34:49 -0800 | [diff] [blame] | 13 | 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 Schuh | 3b3fee0 | 2015-11-26 17:26:32 -0800 | [diff] [blame] | 21 | '-Wextra', |
Austin Schuh | 1f9aeb4 | 2015-11-12 23:34:49 -0800 | [diff] [blame] | 22 | '-Wno-builtin-macro-redefined', |
Austin Schuh | c65df86 | 2015-12-27 14:04:49 -0800 | [diff] [blame] | 23 | '-Wunused-local-typedefs', |
Brian Silverman | a4a4ea6 | 2015-11-27 10:27:53 -0500 | [diff] [blame] | 24 | '-D__has_feature(x)=0', |
| 25 | '-fmacro-backtrace-limit=0'] |
Austin Schuh | 1f9aeb4 | 2015-11-12 23:34:49 -0800 | [diff] [blame] | 26 | |
| 27 | for flag in cmd: |
Brian Silverman | ed79de6 | 2015-11-28 18:10:17 -0500 | [diff] [blame] | 28 | if flag not in exclude_flags and not (flag.startswith('-fsanitize') or |
| 29 | flag.startswith('-fno-sanitize')): |
Austin Schuh | 1f9aeb4 | 2015-11-12 23:34:49 -0800 | [diff] [blame] | 30 | filtered_cmd.append(flag) |
| 31 | |
| 32 | ctx.action( |
Brian Silverman | b2215d0 | 2015-11-23 19:10:15 -0500 | [diff] [blame] | 33 | inputs = [ctx.file.src] + ctx.files._cc_toolchain, |
Austin Schuh | 1f9aeb4 | 2015-11-12 23:34:49 -0800 | [diff] [blame] | 34 | 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 Silverman | b200c17 | 2017-01-02 17:35:35 -0800 | [diff] [blame] | 41 | def _define_fortran_output(src): |
| 42 | if not src.name.endswith('.f'): |
Austin Schuh | 1f9aeb4 | 2015-11-12 23:34:49 -0800 | [diff] [blame] | 43 | fail('Fortran files must end in \'.f\'', 'src') |
| 44 | |
Brian Silverman | b200c17 | 2017-01-02 17:35:35 -0800 | [diff] [blame] | 45 | fortran_file_base = src.name[:-2] |
Austin Schuh | 1f9aeb4 | 2015-11-12 23:34:49 -0800 | [diff] [blame] | 46 | 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 Silverman | b2215d0 | 2015-11-23 19:10:15 -0500 | [diff] [blame] | 56 | # TODO(Brian): Replace this with something more fine-grained from the |
| 57 | # configuration fragment or something. |
| 58 | '_cc_toolchain': attr.label( |
Brian Silverman | 385cd98 | 2015-12-20 18:26:32 -0500 | [diff] [blame] | 59 | default = Label('@//tools/cpp:toolchain'), |
Brian Silverman | b2215d0 | 2015-11-23 19:10:15 -0500 | [diff] [blame] | 60 | ), |
Austin Schuh | 1f9aeb4 | 2015-11-12 23:34:49 -0800 | [diff] [blame] | 61 | }, |
| 62 | outputs = _define_fortran_output, |
| 63 | fragments = [ |
| 64 | 'cpp', |
| 65 | ], |
| 66 | ) |
| 67 | |
| 68 | def 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 Silverman | b2215d0 | 2015-11-23 19:10:15 -0500 | [diff] [blame] | 78 | _single_fortran_object( |
| 79 | name = name + '_' + pic_o_file, |
| 80 | src = src, |
| 81 | visibility = ['//visibility:private'], |
Austin Schuh | 9d92e6b | 2017-10-17 01:19:38 -0700 | [diff] [blame] | 82 | restricted_to = ['@//tools:k8'], |
Brian Silverman | b2215d0 | 2015-11-23 19:10:15 -0500 | [diff] [blame] | 83 | ) |
Austin Schuh | 1f9aeb4 | 2015-11-12 23:34:49 -0800 | [diff] [blame] | 84 | 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 Schuh | 9d92e6b | 2017-10-17 01:19:38 -0700 | [diff] [blame] | 94 | restricted_to = ['@//tools:k8'], |
Austin Schuh | 1f9aeb4 | 2015-11-12 23:34:49 -0800 | [diff] [blame] | 95 | ) |
Brian Silverman | d3ad165 | 2018-02-18 22:16:29 -0500 | [diff] [blame^] | 96 | |
| 97 | f2c_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 | |
| 119 | This is useful when building externally-f2ced files.''' |
| 120 | |
| 121 | def 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 | ) |