[PATCH] D105896: [flang][driver] Fix output filename generation in `flang`

Andrzej Warzynski via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 13 08:31:53 PDT 2021


awarzynski created this revision.
Herald added a reviewer: sscalpone.
awarzynski requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

In the `flang` bash script, we need to be careful _when_ to use <output>
from `flang -c -o <output> <input>` when generating the relocatable
output file name.

In particular, we should use it in this case:

  compilation only
  flang -c -o <output> <input>

but leave it for the final executable here:

  compile, assemble and link
  flang  -o <output> <input>

This change is implemented in `get_relocatable_name`.

I've also taken the liberty to fix how errors from sub-commands are
reported (without this change, `flang` always returns `0` on failure).
This is implemented in `main`.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D105896

Files:
  flang/tools/f18/flang.in


Index: flang/tools/f18/flang.in
===================================================================
--- flang/tools/f18/flang.in
+++ flang/tools/f18/flang.in
@@ -264,11 +264,12 @@
 }
 
 # === get_relocatable_name ======================================================
-#
-# Generate a name for the output object file based on the source file, e.g.
+# This method generates the name of the output file for the compilation phase
+# (triggered with `-c`). If the user of this script is only interested in
+# compilation (`flang -c`), use $OUTPUT_FILE provided that it was defined.
+# Otherwise, use the usual heuristics:
 #   * file.f --> file.o
 #   * file.c --> file.o
-# If OUTPUT_FILE is defined, use that instead.
 #
 # INPUTS:
 #   $1 - input source file for which to generate the output name
@@ -276,7 +277,7 @@
 get_relocatable_name() {
   local -r src_file=$1
 
-  if [[ ! -z ${OUTPUT_FILE:+x} ]]; then
+  if [[ $COMPILE_ONLY == "True" ]] && [[ ! -z ${OUTPUT_FILE:+x} ]]; then
     out_file="$OUTPUT_FILE"
   else
     current_ext=${src_file##*.}
@@ -333,10 +334,11 @@
   [[ ! -z ${MODULE_DIR} ]] && flang_options+=("-module-dir ${MODULE_DIR}")
   [[ ! -z ${INTRINSICS_MOD_DIR} ]] && flang_options+=("-intrinsics-module-directory ${INTRINSICS_MOD_DIR}")
   for idx in "${!fortran_source_files[@]}"; do
-    if ! "$wd/bin/@FLANG_DEFAULT_DRIVER@" "${flang_options[@]}" "${fortran_source_files[$idx]}" -o "${unparsed_file}_${idx}.f90"
-    then status=$?
-         echo flang: in "$PWD", @FLANG_DEFAULT_DRIVER@ failed with exit status $status: "$wd/bin/@FLANG_DEFAULT_DRIVER@" "${flang_options[@]}" "$@" >&2
-         exit $status
+    "$wd/bin/@FLANG_DEFAULT_DRIVER@" "${flang_options[@]}" "${fortran_source_files[$idx]}" -o "${unparsed_file}_${idx}.f90"
+    ret_status=$?
+    if [[ $ret_status != 0 ]]; then
+         echo flang: in "$PWD", @FLANG_DEFAULT_DRIVER@ failed with exit status "$ret_status": "$wd/bin/@FLANG_DEFAULT_DRIVER@" "${flang_options[@]}" "$@" >&2
+         exit "$ret_status"
     fi
   done
 
@@ -348,10 +350,11 @@
     # below. As a result, we cannot rely on the compiler-generated output name.
     out_obj_file=$(get_relocatable_name "${fortran_source_files[$idx]}")
 
-    if ! $ext_fc "-c" "${ext_fc_options[@]}" "${unparsed_file}_${idx}.f90" "-o" "${out_obj_file}"
-    then status=$?
-      echo flang: in "$PWD", "$ext_fc" failed with exit status $status: "$ext_fc" "${ext_fc_options[@]}" "$@" >&2
-         exit $status
+    $ext_fc "-c" "${ext_fc_options[@]}" "${unparsed_file}_${idx}.f90" "-o" "${out_obj_file}"
+    ret_status=$?
+    if [[ $ret_status != 0 ]]; then
+      echo flang: in "$PWD", "$ext_fc" failed with exit status "$ret_status": "$ext_fc" "${ext_fc_options[@]}" "$@" >&2
+         exit "$ret_status"
     fi
     object_files+=(${out_obj_file})
   done
@@ -368,10 +371,11 @@
     # $ext_fc_options). Hence we need to use `get_relocatable_name`.
     out_obj_file=$(get_relocatable_name "${other_source_files[$idx]}")
 
-    if ! $ext_fc "-c" "${ext_fc_options[@]}" "${other_source_files[${idx}]}" "-o" "${out_obj_file}"
-    then status=$?
-      echo flang: in "$PWD", "$ext_fc" failed with exit status $status: "$ext_fc" "${ext_fc_options[@]}" "$@" >&2
-         exit $status
+    $ext_fc "-c" "${ext_fc_options[@]}" "${other_source_files[${idx}]}" "-o" "${out_obj_file}"
+    ret_status=$?
+    if [[ $ret_status != 0 ]]; then
+      echo flang: in "$PWD", "$ext_fc" failed with exit status "$ret_status": "$ext_fc" "${ext_fc_options[@]}" "$@" >&2
+         exit "$ret_status"
     fi
     object_files+=(${out_obj_file})
   done
@@ -389,10 +393,11 @@
       output_definition=""
     fi
 
-    if ! $ext_fc "${ext_fc_options[@]}" "${object_files[@]}" "${lib_files[@]}" ${output_definition:+$output_definition}
-    then status=$?
-         echo flang: in "$PWD", "$ext_fc" failed with exit status $status: "$ext_fc" "${ext_fc_options[@]}" "$@" >&2
-         exit $status
+    $ext_fc "${ext_fc_options[@]}" "${object_files[@]}" "${lib_files[@]}" ${output_definition:+$output_definition}
+    ret_status=$?
+    if [[ $ret_status != 0 ]]; then
+         echo flang: in "$PWD", "$ext_fc" failed with exit status "$ret_status": "$ext_fc" "${ext_fc_options[@]}" "$@" >&2
+         exit "$ret_status"
     fi
   fi
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D105896.358273.patch
Type: text/x-patch
Size: 4273 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210713/e84379ce/attachment.bin>


More information about the llvm-commits mailing list