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', |
Austin Schuh | c65df86 | 2015-12-27 14:04:49 -0800 | [diff] [blame] | 21 | '-Wunused-local-typedefs', |
Brian Silverman | a4a4ea6 | 2015-11-27 10:27:53 -0500 | [diff] [blame] | 22 | '-D__has_feature(x)=0', |
| 23 | '-fmacro-backtrace-limit=0'] |
Austin Schuh | 1f9aeb4 | 2015-11-12 23:34:49 -0800 | [diff] [blame] | 24 | |
| 25 | for flag in cmd: |
Brian Silverman | ed79de6 | 2015-11-28 18:10:17 -0500 | [diff] [blame] | 26 | if flag not in exclude_flags and not (flag.startswith('-fsanitize') or |
| 27 | flag.startswith('-fno-sanitize')): |
Austin Schuh | 1f9aeb4 | 2015-11-12 23:34:49 -0800 | [diff] [blame] | 28 | filtered_cmd.append(flag) |
| 29 | |
| 30 | ctx.action( |
Brian Silverman | b2215d0 | 2015-11-23 19:10:15 -0500 | [diff] [blame] | 31 | inputs = [ctx.file.src] + ctx.files._cc_toolchain, |
Austin Schuh | 1f9aeb4 | 2015-11-12 23:34:49 -0800 | [diff] [blame] | 32 | outputs = [ctx.outputs.pic_o], |
| 33 | mnemonic = "Fortran", |
| 34 | executable = ctx.fragments.cpp.compiler_executable, |
| 35 | arguments = filtered_cmd, |
| 36 | progress_message = 'Building %s' % ctx.outputs.pic_o.short_path, |
| 37 | ) |
| 38 | |
| 39 | def _define_fortran_output(attrs): |
| 40 | if not attrs.src.name.endswith('.f'): |
| 41 | fail('Fortran files must end in \'.f\'', 'src') |
| 42 | |
| 43 | fortran_file_base = attrs.src.name[:-2] |
| 44 | return { |
| 45 | 'pic_o': fortran_file_base + '.pic.o', |
| 46 | } |
| 47 | |
| 48 | |
| 49 | _single_fortran_object = rule( |
| 50 | implementation = _single_fortran_object_impl, |
| 51 | attrs = { |
| 52 | 'src': attr.label(single_file=True, allow_files=FileType(['.f'])), |
| 53 | 'cc_libs': attr.label_list(providers=['cc']), |
Brian Silverman | b2215d0 | 2015-11-23 19:10:15 -0500 | [diff] [blame] | 54 | # TODO(Brian): Replace this with something more fine-grained from the |
| 55 | # configuration fragment or something. |
| 56 | '_cc_toolchain': attr.label( |
Brian Silverman | 385cd98 | 2015-12-20 18:26:32 -0500 | [diff] [blame] | 57 | default = Label('@//tools/cpp:toolchain'), |
Brian Silverman | b2215d0 | 2015-11-23 19:10:15 -0500 | [diff] [blame] | 58 | ), |
Austin Schuh | 1f9aeb4 | 2015-11-12 23:34:49 -0800 | [diff] [blame] | 59 | }, |
| 60 | outputs = _define_fortran_output, |
| 61 | fragments = [ |
| 62 | 'cpp', |
| 63 | ], |
| 64 | ) |
| 65 | |
| 66 | def fortran_library(name, srcs, deps = [], visibility = None): |
| 67 | """Builds a shared library from a set of fortran files. |
| 68 | |
| 69 | Args: |
| 70 | srcs: list of fortran files ending in .f |
| 71 | deps: cc_library or fortran_library dependencies. |
| 72 | """ |
| 73 | pic_o_files = [] |
| 74 | for src in srcs: |
| 75 | pic_o_file = src[:-2] + '.pic.o' |
Brian Silverman | b2215d0 | 2015-11-23 19:10:15 -0500 | [diff] [blame] | 76 | _single_fortran_object( |
| 77 | name = name + '_' + pic_o_file, |
| 78 | src = src, |
| 79 | visibility = ['//visibility:private'], |
| 80 | ) |
Austin Schuh | 1f9aeb4 | 2015-11-12 23:34:49 -0800 | [diff] [blame] | 81 | pic_o_files.append(pic_o_file) |
| 82 | |
| 83 | native.cc_library( |
| 84 | name = name, |
| 85 | deps = deps, |
| 86 | srcs = pic_o_files, |
| 87 | linkopts = [ |
| 88 | '-lgfortran', |
| 89 | ], |
| 90 | visibility = visibility, |
| 91 | ) |