[flang] Separate temporary and user-specified object files
authorAndrzej Warzynski <andrzej.warzynski@arm.com>
Tue, 4 Jan 2022 11:02:11 +0000 (11:02 +0000)
committerAndrzej Warzynski <andrzej.warzynski@arm.com>
Thu, 6 Jan 2022 16:38:39 +0000 (16:38 +0000)
This patch updates the `flang` bash scripts to differentiate between
object files provided by the user and intermediate object files
generated by the script. The latter are an "implementation detail" that
should not be visible to the end user (i.e. deleted before the scripts
exits). The former should be preserved.

Fixes https://github.com/flang-compiler/f18-llvm-project/issues/1348

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

flang/tools/f18/flang

index 25397f3..6176a75 100755 (executable)
@@ -123,14 +123,15 @@ parse_args()
 #   $2 - Fortran source files extracted from $1 (array, name reference)
 #   $3 - other source files extracted from $1 (array, name reference)
 #   $4 - object files extracted from $1 (array, name reference)
-#   $4 - lib files extracted from $1 (array, name reference)
+#   $5 - lib files extracted from $1 (array, name reference)
 # =============================================================================
 categorise_files()
 {
   local -n -r all_files=$1
   local -n fortran_sources=$2
   local -n other_sources=$3
-  local -n libs=$4
+  local -n objects=$4
+  local -n libs=$5
 
   for current_file in "${all_files[@]}"; do
     file_ext=${current_file##*.}
@@ -144,7 +145,7 @@ categorise_files()
     elif [[ $file_ext == "a" ]] || [[ $file_ext == "so" ]]; then
       libs+=($current_file)
     elif [[ $file_ext == "o" ]]; then
-      object_files+=($current_file)
+      objects+=($current_file)
     else
       other_sources+=($current_file)
     fi
@@ -330,6 +331,7 @@ main() {
     exit 0
   fi
 
+  # Source, object and library files provided by the user
   local fortran_source_files=()
   local other_source_files=()
   local object_files=()
@@ -388,6 +390,8 @@ main() {
 
   # STEP 2: Compile Fortran Source Files
   readonly ext_fc="${F18_FC:-gfortran}"
+  # Temporary object files generated by this script. To be deleted at the end.
+  local temp_object_files=()
   for idx in "${!fortran_source_files[@]}"; do
     # 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
@@ -402,7 +406,7 @@ main() {
       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})
+    temp_object_files+=(${out_obj_file})
   done
 
   # Delete the unparsed files
@@ -425,7 +429,7 @@ main() {
       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})
+    temp_object_files+=(${out_obj_file})
   done
 
   # STEP 4: Link
@@ -433,7 +437,7 @@ main() {
     exit 0;
   fi
 
-  if [[ ${#object_files[@]} -ge 1 ]]; then
+  if [[ ${#temp_object_files[@]} -ge 1 ]] || [[ ${#object_files[@]} -ge 1 ]] ; then
     # If $OUTPUT_FILE was specified, use it for the output name.
     if [[ ! -z ${OUTPUT_FILE:+x} ]]; then
       output_definition="-o $OUTPUT_FILE"
@@ -442,7 +446,7 @@ main() {
     fi
 
     set +e
-    $ext_fc "${ext_fc_options[@]}" "${object_files[@]}" "${lib_files[@]}" ${output_definition:+$output_definition}
+    $ext_fc "${ext_fc_options[@]}" "${object_files[@]}" "${temp_object_files[@]}" "${lib_files[@]}" ${output_definition:+$output_definition}
     ret_status=$?
     set -e
     if [[ $ret_status != 0 ]]; then
@@ -453,7 +457,7 @@ main() {
 
   # Delete intermediate object files
   for idx in "${!fortran_source_files[@]}"; do
-    rm "${object_files[$idx]}"
+    rm "${temp_object_files[$idx]}"
   done
 }