[flang-commits] [flang] aa06f34 - [flang][driver] Fix output filename generation in `flang`

Andrzej Warzynski via flang-commits flang-commits at lists.llvm.org
Fri Jul 16 09:06:36 PDT 2021


Author: Andrzej Warzynski
Date: 2021-07-16T17:06:06+01:00
New Revision: aa06f34dac65cbfcbffd7cb3b033eadcd103ffdf

URL: https://github.com/llvm/llvm-project/commit/aa06f34dac65cbfcbffd7cb3b033eadcd103ffdf
DIFF: https://github.com/llvm/llvm-project/commit/aa06f34dac65cbfcbffd7cb3b033eadcd103ffdf.diff

LOG: [flang][driver] Fix output filename generation in `flang`

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`.

Differential Revision: https://reviews.llvm.org/D105896

Added: 
    

Modified: 
    flang/tools/f18/flang.in

Removed: 
    


################################################################################
diff  --git a/flang/tools/f18/flang.in b/flang/tools/f18/flang.in
index c8b6f457893a6..eb953c58d91a9 100755
--- a/flang/tools/f18/flang.in
+++ b/flang/tools/f18/flang.in
@@ -264,11 +264,12 @@ preprocess() {
 }
 
 # === 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 @@ preprocess() {
 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##*.}
@@ -339,10 +340,13 @@ main() {
   [[ ! -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_base}_${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
+    set +e
+    "$wd/bin/@FLANG_DEFAULT_DRIVER@" "${flang_options[@]}" "${fortran_source_files[$idx]}" -o "${unparsed_file_base}_${idx}.f90"
+    ret_status=$?
+    set -e
+    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
 
@@ -354,10 +358,13 @@ main() {
     # 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_base}_${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
+    set +e
+    $ext_fc "-c" "${ext_fc_options[@]}" "${unparsed_file_base}_${idx}.f90" "-o" "${out_obj_file}"
+    ret_status=$?
+    set -e
+    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
@@ -374,10 +381,13 @@ main() {
     # $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
+    set +e
+    $ext_fc "-c" "${ext_fc_options[@]}" "${other_source_files[${idx}]}" "-o" "${out_obj_file}"
+    ret_status=$?
+    set -e
+    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
@@ -395,10 +405,13 @@ main() {
       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
+    set +e
+    $ext_fc "${ext_fc_options[@]}" "${object_files[@]}" "${lib_files[@]}" ${output_definition:+$output_definition}
+    ret_status=$?
+    set -e
+    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
 


        


More information about the flang-commits mailing list