Austin Schuh | 1f9aeb4 | 2015-11-12 23:34:49 -0800 | [diff] [blame] | 1 | def _single_fortran_object_impl(ctx): |
| 2 | toolchain_cflags = (ctx.fragments.cpp.compiler_options([]) + |
| 3 | ctx.fragments.cpp.c_options + |
Austin Schuh | 3b3fee0 | 2015-11-26 17:26:32 -0800 | [diff] [blame] | 4 | ctx.fragments.cpp.unfiltered_compiler_options([]) + |
| 5 | ['-fPIC', '-Wno-maybe-uninitialized', '-Wno-unused-dummy-argument', |
Austin Schuh | 2461e5e | 2015-11-28 13:23:45 -0800 | [diff] [blame] | 6 | '-Wno-conversion', '-Wno-unused-variable', '-Wno-character-truncation']) |
Austin Schuh | 1f9aeb4 | 2015-11-12 23:34:49 -0800 | [diff] [blame] | 7 | |
| 8 | cmd = toolchain_cflags + ['-c', ctx.file.src.path, '-o', ctx.outputs.pic_o.path] |
| 9 | filtered_cmd = [] |
Brian Silverman | a4a4ea6 | 2015-11-27 10:27:53 -0500 | [diff] [blame] | 10 | # Strip out the C/C++/Clang specific flags. |
Austin Schuh | 1f9aeb4 | 2015-11-12 23:34:49 -0800 | [diff] [blame] | 11 | exclude_flags = ['-fcolor-diagnostics', |
| 12 | '-Wswitch-enum', |
| 13 | '-Wpointer-arith', |
| 14 | '-Wcast-qual', |
| 15 | '-Wwrite-strings', |
| 16 | '-Wsign-compare', |
| 17 | '-Wformat=2', |
| 18 | '-Werror', |
Austin Schuh | 3b3fee0 | 2015-11-26 17:26:32 -0800 | [diff] [blame] | 19 | '-Wextra', |
Austin Schuh | 1f9aeb4 | 2015-11-12 23:34:49 -0800 | [diff] [blame] | 20 | '-Wno-builtin-macro-redefined', |
Brian Silverman | a4a4ea6 | 2015-11-27 10:27:53 -0500 | [diff] [blame] | 21 | '-D__has_feature(x)=0', |
| 22 | '-fmacro-backtrace-limit=0'] |
Austin Schuh | 1f9aeb4 | 2015-11-12 23:34:49 -0800 | [diff] [blame] | 23 | |
| 24 | for flag in cmd: |
| 25 | if flag not in exclude_flags: |
| 26 | filtered_cmd.append(flag) |
| 27 | |
| 28 | ctx.action( |
Brian Silverman | b2215d0 | 2015-11-23 19:10:15 -0500 | [diff] [blame] | 29 | inputs = [ctx.file.src] + ctx.files._cc_toolchain, |
Austin Schuh | 1f9aeb4 | 2015-11-12 23:34:49 -0800 | [diff] [blame] | 30 | outputs = [ctx.outputs.pic_o], |
| 31 | mnemonic = "Fortran", |
| 32 | executable = ctx.fragments.cpp.compiler_executable, |
| 33 | arguments = filtered_cmd, |
| 34 | progress_message = 'Building %s' % ctx.outputs.pic_o.short_path, |
| 35 | ) |
| 36 | |
| 37 | def _define_fortran_output(attrs): |
| 38 | if not attrs.src.name.endswith('.f'): |
| 39 | fail('Fortran files must end in \'.f\'', 'src') |
| 40 | |
| 41 | fortran_file_base = attrs.src.name[:-2] |
| 42 | return { |
| 43 | 'pic_o': fortran_file_base + '.pic.o', |
| 44 | } |
| 45 | |
| 46 | |
| 47 | _single_fortran_object = rule( |
| 48 | implementation = _single_fortran_object_impl, |
| 49 | attrs = { |
| 50 | 'src': attr.label(single_file=True, allow_files=FileType(['.f'])), |
| 51 | 'cc_libs': attr.label_list(providers=['cc']), |
Brian Silverman | b2215d0 | 2015-11-23 19:10:15 -0500 | [diff] [blame] | 52 | # TODO(Brian): Replace this with something more fine-grained from the |
| 53 | # configuration fragment or something. |
| 54 | '_cc_toolchain': attr.label( |
| 55 | default = Label('//tools/cpp:toolchain'), |
| 56 | ), |
Austin Schuh | 1f9aeb4 | 2015-11-12 23:34:49 -0800 | [diff] [blame] | 57 | }, |
| 58 | outputs = _define_fortran_output, |
| 59 | fragments = [ |
| 60 | 'cpp', |
| 61 | ], |
| 62 | ) |
| 63 | |
| 64 | def fortran_library(name, srcs, deps = [], visibility = None): |
| 65 | """Builds a shared library from a set of fortran files. |
| 66 | |
| 67 | Args: |
| 68 | srcs: list of fortran files ending in .f |
| 69 | deps: cc_library or fortran_library dependencies. |
| 70 | """ |
| 71 | pic_o_files = [] |
| 72 | for src in srcs: |
| 73 | pic_o_file = src[:-2] + '.pic.o' |
Brian Silverman | b2215d0 | 2015-11-23 19:10:15 -0500 | [diff] [blame] | 74 | _single_fortran_object( |
| 75 | name = name + '_' + pic_o_file, |
| 76 | src = src, |
| 77 | visibility = ['//visibility:private'], |
| 78 | ) |
Austin Schuh | 1f9aeb4 | 2015-11-12 23:34:49 -0800 | [diff] [blame] | 79 | pic_o_files.append(pic_o_file) |
| 80 | |
| 81 | native.cc_library( |
| 82 | name = name, |
| 83 | deps = deps, |
| 84 | srcs = pic_o_files, |
| 85 | linkopts = [ |
| 86 | '-lgfortran', |
| 87 | ], |
| 88 | visibility = visibility, |
| 89 | ) |