[flang-commits] [flang] 739f49e - [flang][driver] Fix how output filename is generated

Andrzej Warzynski via flang-commits flang-commits at lists.llvm.org
Fri Jul 9 11:15:23 PDT 2021


Author: Andrzej Warzynski
Date: 2021-07-09T19:15:12+01:00
New Revision: 739f49ed12a4d94cef07c7071e2ad6dbb93834f3

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

LOG: [flang][driver] Fix how output filename is generated

Currently, `flang -c file.f` generates `flang_unparsed_source_file_0.o`.
This is incorrect. This patch:
  * simplifies the logic around output filename generation, and
  * makes sure that `file.o` is produced instead of e.g.
    `flang_unparsed_source_file_0.o` when using the `-c` flag

The output filename generation is moved to a dedicated function. I've
also made a few minor improvements, e.g. marked variables as local,
added comments, refined error messages.

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

Added: 
    

Modified: 
    flang/tools/f18/flang.in

Removed: 
    


################################################################################
diff  --git a/flang/tools/f18/flang.in b/flang/tools/f18/flang.in
index 9620821dc1d3..603f43c1ac22 100755
--- a/flang/tools/f18/flang.in
+++ b/flang/tools/f18/flang.in
@@ -23,7 +23,6 @@ MODULE_DIR=""
 INTRINSICS_MOD_DIR=""
 COMPILE_ONLY="False"
 PREPROCESS_ONLY="False"
-TEMP_OUTPUT="flang_temp_out_"
 PRINT_VERSION="False"
 
 # === parse_args ==============================================================
@@ -106,7 +105,7 @@ parse_args()
 
       else
         # CASE 3: Unsupported
-        echo "ERROR: unrecognised option format $1"
+        echo "ERROR: unrecognised option format: \`$1\`. Perhaps non-existent file?"
         exit 1
       fi
   done
@@ -258,12 +257,37 @@ preprocess() {
   for idx in "${!other_srcs[@]}"; do
     if ! $ext_fc -E "${opts[@]}" "${other_srcs[$idx]}" ${output_definition:+$output_definition}
     then status=$?
-         echo flang: in "$PWD", flang-new failed with exit status $status: "$wd/bin/flang-new" "${flang_options[@]}" "$@" >&2
+         echo flang: in "$PWD", flang-new failed with exit status $status: "$wd/bin/flang-new" "${opts[@]}" "$@" >&2
          exit $status
     fi
   done
 }
 
+# === get_relocatable_name ======================================================
+#
+# Generate a name for the output object file based on the source file, e.g.
+#   * 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
+# =============================================================================
+get_relocatable_name() {
+  local -r src_file=$1
+
+  if [[ ! -z ${OUTPUT_FILE:+x} ]]; then
+    out_file="$OUTPUT_FILE"
+  else
+    current_ext=${src_file##*.}
+    new_ext="o"
+
+    out_file=$(basename "${src_file}" "$current_ext")${new_ext}
+  fi
+
+  echo "$out_file"
+}
+
 # === main ====================================================================
 # Main entry point for this script
 # =============================================================================
@@ -275,10 +299,10 @@ main() {
     exit 0
   fi
 
-  fortran_source_files=()
-  other_source_files=()
-  object_files=()
-  lib_files=()
+  local fortran_source_files=()
+  local other_source_files=()
+  local object_files=()
+  local lib_files=()
   categorise_files INPUT_FILES fortran_source_files other_source_files object_files lib_files
 
   if [[ $PREPROCESS_ONLY == "True" ]]; then
@@ -292,9 +316,9 @@ main() {
   # flag that the driver will see (otherwise it assumes compiler/toolchain
   # driver mode).`f18` will just ignore this flag when uparsing, so it's fine
   # to add it here unconditionally.
-  flang_options=("-fc1")
+  local flang_options=("-fc1")
   # Options for the external Fortran Compiler
-  ext_fc_options=()
+  local ext_fc_options=()
   categorise_opts OPTIONS flang_options ext_fc_options
 
   local -r wd=$(cd "$(dirname "$0")/.." && pwd)
@@ -319,19 +343,17 @@ main() {
   # STEP 2: Compile Fortran Source Files
   readonly ext_fc="${F18_FC:-gfortran}"
   for idx in "${!fortran_source_files[@]}"; do
-    # Use the value of $OUTPUT_FILE for the output file iff `-c` was used.
-    if [[ ! -z ${OUTPUT_FILE:+x} ]] && [[ $COMPILE_ONLY == "True" ]]; then
-      output_definition="-o $OUTPUT_FILE"
-    elif [[ $COMPILE_ONLY == "False" ]]; then
-      output_definition="-o ${TEMP_OUTPUT}_${idx}"
-    fi
+    # We always have to specify the output name with `-o <out_obj_file>`. This
+    # is because we are using the unparsed rather than the original source file
+    # 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" ${output_definition:+$output_definition}
+    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
     fi
-    object_files+=(${TEMP_OUTPUT}_${idx})
+    object_files+=(${out_obj_file})
   done
 
   # Delete the unparsed files
@@ -341,19 +363,17 @@ main() {
 
   # STEP 3: Compile Other Source Files
   for idx in "${!other_source_files[@]}"; do
-    # Use the value of $OUTPUT_FILE for the output file iff `-c` was used.
-    if [[ ! -z ${OUTPUT_FILE:+x} ]] && [[ $COMPILE_ONLY == "True" ]]; then
-      output_definition="-o $OUTPUT_FILE"
-    elif [[ $COMPILE_ONLY == "False" ]]; then
-      output_definition="-o ${TEMP_OUTPUT}_${idx}"
-    fi
+    # We always specify the output name with `-o <out_obj_file>`. The user
+    # might have used `-o`, but we never add it to $OPTIONS (or
+    # $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}]}" ${output_definition:+$output_definition}
+    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
     fi
-    object_files+=(${TEMP_OUTPUT}_${idx})
+    object_files+=(${out_obj_file})
   done
 
   # STEP 4: Link
@@ -378,7 +398,7 @@ main() {
 
   # Delete intermediate object files
   for idx in "${!fortran_source_files[@]}"; do
-    rm "${TEMP_OUTPUT}_${idx}"
+    rm "${object_files[$idx]}"
   done
 }
 


        


More information about the flang-commits mailing list