###############################################################################
#
# helpers.tcl
#
# based on XilinxTclStore\tclapp\xilinx\modelsim\helpers.tcl
#
###############################################################################

package require Vivado 1.2014.1

package provide ::tclapp::aldec::common::helpers 1.15

namespace eval ::tclapp::aldec::common {

namespace eval helpers {

proc isIpProject {} {
	set fsFilter \
		   "FILESET_TYPE == \"SimulationSrcs\" \
		|| FILESET_TYPE == \"DesignSrcs\" \
        || FILESET_TYPE == \"BlockSrcs\""

	set ftFilter \
		  "FILE_TYPE == \"IP\" \
		|| FILE_TYPE == \"IPX\" \
        || FILE_TYPE == \"DSP Design Sources\" \
		|| FILE_TYPE == \"Block Designs\""

	foreach fsObject [ get_filesets -quiet -filter $fsFilter ] {
		set ip_files [ get_files -quiet -all -of_objects $fsObject -filter $ftFilter ]
		if { [ llength $ip_files ] > 0 } {
			return true
		}
	}

	return false
}

proc getXpmLibraries { } {
	set returnLibraries [ list ]

	set xpmLibraries [ get_property -quiet "XPM_LIBRARIES" [ current_project ] ]
	set autoXpmLibraries [ auto_detect_xpm -quiet -search_ips -no_set_property ]
	set allXpmLibraries [ concat $xpmLibraries $autoXpmLibraries ]

	if { [ llength $allXpmLibraries ] > 0 } {
		foreach library $allXpmLibraries {
			if { [ lsearch $returnLibraries $library ] == -1 } {
				lappend returnLibraries $library
			}
		}
	}

	return $returnLibraries
}

proc getCompiledLibraryLocation { } {
	variable properties

	set librariesLocation ""

	switch -- [ get_property target_simulator [ current_project ] ] {
		Riviera { set librariesLocation [ get_property COMPXLIB.RIVIERA_COMPILED_LIBRARY_DIR [ current_project ] ] }
		ActiveHDL { set librariesLocation [ get_property COMPXLIB.ACTIVEHDL_COMPILED_LIBRARY_DIR [ current_project ] ] }
	}

	if { $librariesLocation == "" || ![ file isfile [ file join $librariesLocation library.cfg ] ] } {
		set librariesLocation $properties(s_lib_map_path)
	}

	return $librariesLocation
}

proc getCompiledLibrariesFromFile { _librariesDirectory } {  
	set libraries [ list ]
	set file [ file normalize [ file join $_librariesDirectory ".cxl.stat" ] ]

	if { ![ file exists $file ] } {
		return $libraries
	}

	set fh 0
	if { [ catch { open $file r } fh ] } {
		return $libraries
	}

	set libraryData [ split [ read $fh ] "\n" ]
	close $fh

	foreach line $libraryData {
		set line [ string trim $line ]

		if { [string length $line] == 0 } {
			continue;
		}

		if { [ regexp {^#} $line ] } {
			continue;
		}

		set tokens [ split $line {,} ]
		set library [ lindex $tokens 0 ]
		
		if { [ lsearch -exact $libraries $library ] == -1 } {
			lappend libraries $library
		}
	}

	return $libraries
}

proc findCompiledLibraries { } {
	variable properties
	variable compiledLibraries

	set compiledLibraryLocation [getCompiledLibraryLocation]
	set referenceXpmLibrary 0

	if { [ llength [ getXpmLibraries ] ] > 0 } {
		if { [ get_param project.usePreCompiledXPMLibForSim ] } {
			set referenceXpmLibrary 1
		}
	}

	if { ( $properties(b_use_static_lib) ) && ( [ isIpProject ] || $referenceXpmLibrary) } {
		set localIpLibaries [ getLibrariesFromLocalRepo ]
		if { {} != $compiledLibraryLocation } {
			set libraries [ getCompiledLibrariesFromFile $compiledLibraryLocation ]

			foreach lib $libraries {
				if { [ lsearch -exact $localIpLibaries $lib ] != -1 } {
					continue
				} else {
					lappend compiledLibraries $lib
				}
			}
		}
	}
}

proc getLibrariesFromLocalRepo {} {
	set installRepo [ file normalize [ file join [ rdi::get_data_dir -quiet -datafile "ip" ] "ip" ] ]
	set installComps [ split [string map {\\ /} $installRepo] {/} ]
	set index [ lsearch -exact $installComps "IP_HEAD" ]

	if { $index == -1 } {
		set installDir $installRepo
	} else {
		set installDir [ join [ lrange $installComps $index end ] "/" ]
	}

	set libraryDict [ dict create ]
	foreach ipObject [get_ips -all -quiet] {
		if { {} == $ipObject } {
			continue
		}
  
		if { [ get_property -quiet is_locked $ipObject ] } {
			foreach fileObject [ get_files -quiet -all -of_objects $ipObject -filter {USED_IN=~"*ipstatic*"} ] {
				set lib [ get_property library $fileObject ]
				if { {xil_defaultlib} == $lib } {
					continue
				}

				dict append libraryDict $lib
			}
		} else {
			set ipDefObject [ get_ipdefs -quiet -all [ get_property -quiet ipdef $ipObject ] ]
			if { {} == $ipDefObject } {
				continue
			}

			set localRepo [ lindex [ get_property -quiet repository $ipDefObject ] 0 ]
			if { {} == $localRepo } {
				continue
			}

			set localRepo [ string map {\\ /} $localRepo ]
			if { {ip_repo} != [ file tail $localRepo ] } {
				continue
			}

			set localComps [ split $localRepo {/} ]
			set index [ lsearch -exact $localComps "IP_HEAD" ]
			if { $index == -1 } {
				set localDirectory $localRepo
			} else {
				set localDirectory [ join [ lrange $localComps $index end ] "/" ]
			}

			if { [ string equal -nocase $installDir $localDirectory] != 1 } {
				foreach fileObject [ get_files -quiet -all -of_objects $ipObject -filter {USED_IN=~"*ipstatic*"} ] {
					set lib [ get_property library $fileObject ]
					if { {xil_defaultlib} == $lib } {
						continue
					}
					
					dict append libraryDict $lib
				}
			}
		}
	}

	return [ dict keys $libraryDict ]
}

proc getDesignLibraries { _files } {
	set libraries [ list ]

	foreach file $_files {
		set fargs [ split $file {|} ]
		set type [ lindex $fargs 0 ]
		set file_type [ lindex $fargs 1 ]
		set library [ lindex $fargs 2 ]
		if { {} == $library } {
			continue;
		}

		if { [lsearch -exact $libraries $library] == -1 } {
			lappend libraries $library
		}
	}

	return $libraries
}

proc isDesignLibrary { _libraryName } {
	variable properties

	set designFiles $properties(designFiles)
	set designLibraries [ getDesignLibraries $designFiles ]

	foreach library $designLibraries {
		if { [ string compare -nocase $_libraryName $library ] == 0 } {
			return 1
		}
	}

	return 0
}

proc isBatchMode { } {
	variable properties

	set batchModeEnabled $properties(batch_mode_enabled)
	set onlyGenerateScripts $properties(only_generate_scripts)

	if { $batchModeEnabled || $onlyGenerateScripts } {
		return 1
	}

	return 0
}

proc findPrecompiledLibrary { } {
	variable precompiledLibrary

	set librariesLocation [ ::tclapp::aldec::common::helpers::getCompiledLibraryLocation ]
	if { $librariesLocation == "" } {
		return
	}

	set libraryCfgFile [ file join $librariesLocation library.cfg ]
	if { ![ file isfile $libraryCfgFile ] } {
		return
	}

	set libraryCfg [ open $libraryCfgFile r ]

	while { ! [eof $libraryCfg ] } {
		gets $libraryCfg line
		if { [ regexp {\s*([^\s]+)\s*=\s*\"?([^\s\"]+).*} $line tmp mapName mapPath ] } {

			if { [ file pathtype $mapPath ] != "absolute" } {
				set mapPath [ file join $librariesLocation $mapPath ]
			}

			if { ![ file isfile [ usf_file_normalize $mapPath ] ] } {
				continue
			}

			lappend precompiledLibrary $mapName
		}
	}

	close $libraryCfg
}

proc checkLibraryWasCompiled { _library } {
	variable precompiledLibrary

	foreach library $precompiledLibrary {
		if { [ string compare -nocase $_library $library ] == 0 } {
			return 1
		}
	}

	return 0
}

proc extractSuboreSystemVerilogPackageLibraries { _vlnv } {
	variable systemVerilogPackageLibraries

	set ipDef [ get_ipdefs -quiet -all -vlnv $_vlnv ]
	if { "" == $ipDef } {
		return
	}

	set ipXml [ get_property xml_file_name $ipDef ]
	set ipComp [ ipx::open_core -set_current false $ipXml ]

	foreach fileGroup [ ipx::get_file_groups -of $ipComp ] {
		set type [get_property type $fileGroup ]

		if { ([ string last "simulation" $type ] != -1) && ($type != "examples_simulation") } {
			set subLibraryCores [ get_property component_subcores $fileGroup ]
			set orderedSubCores [ list ]

			foreach subVlnv $subLibraryCores {
				set orderedSubCores [ linsert $orderedSubCores 0 $subVlnv ]
			}

			foreach subVlnv $orderedSubCores {
				extractSuboreSystemVerilogPackageLibraries $subVlnv
			}
			
			foreach staticFile [ ipx::get_files -filter {USED_IN=~"*ipstatic*"} -of $fileGroup ] {
				set fileEntry [ split $staticFile { } ]
				lassign $fileEntry file_key comp_ref file_group_name file_path
				set ipFile [ lindex $fileEntry 3 ]
				set fileType [ get_property type [ ipx::get_files $ipFile -of_objects $fileGroup ] ]

				if { {systemVerilogSource} == $fileType } {
					set library [ get_property library_name [ ipx::get_files $ipFile -of_objects $fileGroup ] ]
					if { ({} != $library) && ({xil_defaultlib} != $library) } {
						if { [ lsearch $systemVerilogPackageLibraries $library ] == -1 } {
							lappend systemVerilogPackageLibraries $library
						}
					}
				}
			}
		}
	}
}

proc findSystemVerilogPackageLibraries { _runDirectory } {
	variable systemVerilogPackageLibraries

	set tmpDirectory "$_runDirectory/_tmp_ip_comp_"
	set ipComps [ list ]

	foreach ip [ get_ips -all -quiet ] {
		set ipFile [ get_property ip_file $ip ]
		set ipFilename [ file rootname $ipFile ]
		append ipFilename ".xml"

		if { ![file exists $ipFilename] } {

			set ipFileObject [ get_files -all -quiet $ipFilename ]
			if { ({} != $ipFileObject) && ([ file exists $ipFileObject ]) } {
				set ipFilename [ extract_files -files [ list "$ipFileObject" ] -base_dir "$tmpDirectory" ]
			}

			if { ![file exists $ipFilename] } {
				continue;
			}
		}

		lappend ipComps $ipFilename
	}

	foreach ipXml $ipComps {
		set ipComp [ ipx::open_core -set_current false $ipXml ]
		set vlnv [ get_property vlnv $ipComp ]

		foreach fileGroup [ ipx::get_file_groups -of $ipComp ] {
			set type [ get_property type $fileGroup ]

			if { ([ string last "simulation" $type ] != -1) && ($type != "examples_simulation") } {
				set subLibraryCores [ get_property component_subcores $fileGroup ]
				if { [ llength $subLibraryCores ] == 0 } {
					continue
				}

				set orderedSubCores [ list ]
				foreach subVlnv $subLibraryCores {
					set orderedSubCores [ linsert $orderedSubCores 0 $subVlnv ]
				}

				foreach subVlnv $orderedSubCores {
					extractSuboreSystemVerilogPackageLibraries $subVlnv
				}

				foreach staticFile [ ipx::get_files -filter {USED_IN=~"*ipstatic*"} -of $fileGroup ] {
					set fileEntry [ split $staticFile { } ]
					lassign $fileEntry file_key comp_ref file_group_name file_path
					set ipFile [ lindex $fileEntry 3 ]
					set fileType [ get_property type [ ipx::get_files $ipFile -of_objects $fileGroup ] ]

					if { {systemVerilogSource} == $fileType } {
						set library [ get_property library_name [ ipx::get_files $ipFile -of_objects $fileGroup ] ]
						if { ({} != $library) && ({xil_defaultlib} != $library) } {
							if { [lsearch $systemVerilogPackageLibraries $library] == -1 } {
								lappend systemVerilogPackageLibraries $library
							}
						}
					}
				}
			}
		}

		ipx::unload_core $ipComp
	}

	if { [file exists $tmpDirectory] } {
		[ catch {file delete -force $tmpDirectory} error_msg ]
	}

	if { [ get_param "project.compileXilinxVipLocalForDesign" ] } {
		set filter "FILE_TYPE == \"SystemVerilog\""
		
		foreach systemVerilogFileObject [ get_files -quiet -compile_order sources -used_in simulation -of_objects [ current_fileset -simset ] -filter $filter ] {
			if { [lsearch -exact [list_property -quiet $systemVerilogFileObject] {LIBRARY}] != -1 } {
				set library [get_property -quiet "LIBRARY" $systemVerilogFileObject]
				if { {} != $library } {
					if { [ lsearch -exact $systemVerilogPackageLibraries $library ] == -1 } {
						lappend systemVerilogPackageLibraries $library
					}
				}
			}
		}
	}

	if { [ get_param "project.usePreCompiledXilinxVIPLibForSim" ] } {
		if { [ is_vip_ip_required ] } {
			lappend systemVerilogPackageLibraries "xilinx_vip"
		}
	}
}

proc getXilinxVipFiles {} {  
	variable systemVerilogPackageLibraries	

	set xv_files [list]
	if { [ llength $systemVerilogPackageLibraries ] == 0 } {
		return $xv_files
	}

	set xv_dir [ file normalize "[rdi::get_data_dir -quiet -datafile "xilinx_vip"]/xilinx_vip" ]
	set file "$xv_dir/xilinx_vip_pkg.list.f"
	if { ![ file exists $file ] } {
		send_msg_id SIM-[usf_aldec_getSimulatorName]-058 WARNING "File does not exist! '$file'\n"
		return $xv_files
	}

	set fh 0
	if { [catch {open $file r} fh] } {
		send_msg_id SIM-[usf_aldec_getSimulatorName]-058 WARNING "Failed to open file for read! '$file'\n"
		return $xv_files
	}

	set sv_file_data [ split [ read $fh ] "\n" ]
	close $fh

	foreach line $sv_file_data {
		if { [string length $line] == 0 } {
			continue;
		}

		if { [regexp {^#} $line] } {
			continue;
		}

		set file_path_str [ string map {\\ /} $line ]
		set replace "XILINX_VIVADO/data/xilinx_vip"
		set with "$xv_dir"
		regsub -all $replace $file_path_str $with file_path_str
		set file_path_str [ string trimleft $file_path_str {$} ]
		set sv_file_path [ string map {\\ /} $file_path_str ]

		if { [ file exists $sv_file_path ] } {
			lappend xv_files $sv_file_path
		}
	}

	return $xv_files
}

proc getVipIncludeDirs { } {
	variable systemVerilogPackageLibraries

	set includeDir {}
	if { [ llength $systemVerilogPackageLibraries ] > 0 } {
		set data_dir [ rdi::get_data_dir -quiet -datafile xilinx_vip ]
		set includeDir "${data_dir}/xilinx_vip/include"
		if { [ file exists $includeDir ] } {
			return $includeDir
		}
	}

	return $includeDir
}

proc setPreCompiledLibraries2DesignBrowser { _generateLaibraryMode } {
	variable generateLaibraryMode

	set generateLaibraryMode $_generateLaibraryMode
}

proc isGenerateLaibraryMode { } {
	variable generateLaibraryMode

	if { [ get_property target_simulator [ current_project ] ] == "ActiveHDL" } {

		if { [ info exists generateLaibraryMode ] } {
			return $generateLaibraryMode
		}

		return 1
	}

	return 0
}

proc usf_aldec_get_file_path_from_project { _file } {
  # Summary:
  # Argument Usage:
  # Return Value:

  upvar $_file file

  set tmp [lindex [get_files -quiet $file] 0]
  if { $tmp != "" } {
    set file $tmp
  }
}

proc usf_aldec_is_file_disabled { _file } {
  # Summary:
  # Argument Usage:
  # Return Value:

  set isDisabled 0
  catch { set isDisabled [get_property IS_USER_DISABLED [get_files -quiet $_file]] }
  return $isDisabled
}

proc usf_aldec_get_vivado_version {} {
  # Summary:
  # Argument Usage:
  # Return Value:

  if { [regexp {[0-9]{4}\.[0-9]+} [version -short] value] } {
    return $value
  } else {
    return 0.0
  }
}

proc usf_aldec_correctSetupArgs { args } {
  # Summary:
  # Argument Usage:
  # Return Value:

  # Projects created prior to Vivado 2016.1 with enabled "generate scripts only" option, and imported into newer Vivado, 
  # still pass -scripts_only switch, but it doesn't exist anymore in gui so we need to remove it from args
  if { [usf_aldec_get_vivado_version] < 2016.1 } {
    return
  }

  upvar $args _args
  regsub -- {-scripts_only} $_args "" _args
}

proc usf_aldec_appendSimulationCoverageOptions { _optionsList } {
  # Summary:
  # Argument Usage:
  # Return Value:

  variable properties

  upvar $_optionsList optionsList  
  set fileset_object [get_filesets $properties(simset)]

  set switches ""

  if { [get_property [usf_aldec_getPropertyName SIMULATE.STATEMENT_COVERAGE] $fileset_object] } {
    append switches "s"
  }
  if { [get_property [usf_aldec_getPropertyName SIMULATE.BRANCH_COVERAGE] $fileset_object] } {
    append switches "b"
  }
  if { [get_property [usf_aldec_getPropertyName SIMULATE.FUNCTIONAL_COVERAGE] $fileset_object] } {
    append switches "f"
  }
  if { [get_property [usf_aldec_getPropertyName SIMULATE.EXPRESSION_COVERAGE] $fileset_object] } {
    append switches "e"
  }
  if { [get_property [usf_aldec_getPropertyName SIMULATE.CONDITION_COVERAGE] $fileset_object] } {
    append switches "c"
  }
  if { [get_property [usf_aldec_getPropertyName SIMULATE.PATH_COVERAGE] $fileset_object] } {
    append switches "p"
  }
  if { [get_property [usf_aldec_getPropertyName SIMULATE.TOGGLE_COVERAGE] $fileset_object] } {
    append switches "t"
  }
  if { [get_property [usf_aldec_getPropertyName SIMULATE.ASSERTION_COVERAGE] $fileset_object] } {
    append switches "a"
  }
  if { [get_property [usf_aldec_getPropertyName SIMULATE.FSM_COVERAGE] $fileset_object] } {
    append switches "m"
  }

  if { $switches != "" } {
    lappend optionsList "-acdb -acdb_cov $switches"
  }
}

proc usf_aldec_appendCompilationCoverageOptions { _optionsList compiler } {
  # Summary:
  # Argument Usage:
  # Return Value:

  variable properties

  upvar $_optionsList optionsList
  set fileset_object [get_filesets $properties(simset)]

  # -------------- main options -------------

  set switches ""  

  if { [get_property [usf_aldec_getPropertyName COMPILE.STATEMENT_COVERAGE] $fileset_object] } {
    append switches "s"
  }
  if { [get_property [usf_aldec_getPropertyName COMPILE.BRANCH_COVERAGE] $fileset_object] } {
    append switches "b"
  }
  if { [get_property [usf_aldec_getPropertyName COMPILE.EXPRESSION_COVERAGE] $fileset_object] } {
    append switches "e"
  }
  if { [get_property [usf_aldec_getPropertyName COMPILE.CONDITION_COVERAGE] $fileset_object] } {
    append switches "c"
  }
  if { ( $compiler == "acom" || $compiler == "vcom" ) && [get_property [usf_aldec_getPropertyName COMPILE.PATH_COVERAGE] $fileset_object] } {
    append switches "p"
  }
  if { [get_property [usf_aldec_getPropertyName COMPILE.ASSERTION_COVERAGE] $fileset_object] } {
    append switches "a"
  }
  if { [get_property [usf_aldec_getPropertyName COMPILE.FSM_COVERAGE] $fileset_object] } {
    append switches "m"
  }

  if { $switches != "" } {
    lappend optionsList "-coverage $switches"
  }

  # --------- additional options ---------------

  set switches ""

  if { [get_property [usf_aldec_getPropertyName COMPILE.ENABLE_EXPRESSIONS_ON_SUBPROGRAM_ARGUMENTS] $fileset_object] } {
    lappend switches "args"
  }
  if { [get_property [usf_aldec_getPropertyName COMPILE.ENABLE_ATOMIC_EXPRESSIONS_IN_THE_CONDITIONAL_STATEMENTS] $fileset_object] } {
    lappend switches "implicit"
  }
  if { [get_property [usf_aldec_getPropertyName COMPILE.ENABLE_THE_EXPRESSIONS_CONSISTING_OF_ONE_VARIABLE_ONLY] $fileset_object] } {
    lappend switches "onevar"
  }
  if { [get_property [usf_aldec_getPropertyName COMPILE.ENABLE_THE_EXPRESSIONS_WITH_RELATIONAL_OPERATORS] $fileset_object] } {
    lappend switches "relational"
  }
  if { [get_property [usf_aldec_getPropertyName COMPILE.ENABLE_THE_EXPRESSIONS_RETURNING_VECTORS] $fileset_object] } {
    lappend switches "vectors"
  }
  if { [get_property [usf_aldec_getPropertyName COMPILE.ENABLE_FSM_SEQUENCES_IN_FSM_COVERAGE] $fileset_object] } {
    lappend switches "fsmsequence"
  }

  if { $switches != "" } {
    lappend optionsList "-coverage_options [join $switches +]"
  }
}

proc usf_aldec_getSimulatorName {} {
  # Summary:
  # Argument Usage:
  # Return Value:
  
  switch -- [get_property target_simulator [current_project]] {
    Riviera { return Riviera-PRO }
    ActiveHDL { return Active-HDL }
    default { error "Unknown target simulator" }
  }
}

proc usf_aldec_getLibraryPrefix {} {
  # Summary:
  # Argument Usage:
  # Return Value:
  
  switch -- [get_property target_simulator [current_project]] {
    Riviera { return "riviera/" }
    ActiveHDL { return "activehdl/" }
    default { error "Unknown target simulator" }
  }
}

proc usf_aldec_get_origin_dir_path { _path } {
  # Summary:
  # Argument Usage:
  # Return Value:

  if { [file pathtype $_path] == "relative" } {
    return "\$origin_dir/$_path"
  } else {
    return $_path
  }
}

proc usf_init_vars {} {
  # Summary: initializes global namespace vars
  # Argument Usage:
  # Return Value:

	variable a_sim_cache_all_bd_files
	variable a_sim_cache_parent_comp_files
	variable compiledLibraries [ list ]
	variable localDesignLibraries [ list ]
	variable systemVerilogPackageLibraries [ list ]
	variable compileOrderFilesUniq [ list ]
	variable precompiledLibrary [ list ]

	variable allDesignFiles
	variable properties
	variable generateLaibraryMode
	
	array unset a_sim_cache_all_bd_files
	array unset a_sim_cache_parent_comp_files

  set project                      [current_project]
  set properties(project_name)     [get_property "NAME" $project]
  set properties(project_dir)      [get_property "DIRECTORY" $project]
  set properties(is_managed)       [get_property "MANAGED_IP" $project]
  set properties(launch_directory) {}
  set properties(s_sim_top)        [get_property "TOP" [current_fileset -simset]]
  set properties(associatedLibrary) [get_property "DEFAULT_LIB" $project]
  
  # launch_simulation tcl task args
  set properties(simset)           [current_fileset -simset]
  set properties(mode)             "behavioral"
  set properties(s_type)                {}
  set properties(only_generate_scripts) 0
  set properties(s_comp_file)           {}
  set properties(use_absolute_paths)    0
  set properties(s_install_path)        {}
  set properties(s_lib_map_path)        {}
  set properties(batch_mode_enabled)    0
  set properties(s_int_os_type)         {}
  set properties(s_int_debug_mode)      0

  set properties(dynamic_repo_dir)   [get_property ip.user_files_dir [current_project]]
  set properties(ipstatic_dir)       [get_property sim.ipstatic.source_dir [current_project]]
  set properties(b_use_static_lib)   [get_property sim.ipstatic.use_precompiled_libs [current_project]]

  set data_dir [rdi::get_data_dir -quiet -datafile "ip/xilinx"]
  set properties(ip_repository_path) [file normalize [file join $data_dir "ip/xilinx"]]

  set properties(s_tool_bin_path)    {}

  set properties(sp_tcl_obj)         {}
  set properties(b_extract_ip_sim_files) 0

  # fileset compile order
  variable l_compile_order_files     [list]
  variable designFiles               [list]

  # ip static libraries
  variable l_ip_static_libs          [list]

  # ip file extension types
  variable l_valid_ip_extns          [list]
  set l_valid_ip_extns               [list ".xci" ".bd" ".slx"]

  # hdl file extension types
  variable valid_hdl_extensions          [list]
  set valid_hdl_extensions               [list ".vhd" ".vhdl" ".vhf" ".vho" ".v" ".vf" ".verilog" ".vr" ".vg" ".vb" ".tf" ".vlog" ".vp" ".vm" ".vh" ".h" ".svh" ".sv" ".veo"]
 
  # data file extension types 
  variable s_data_files_filter
  set s_data_files_filter            "FILE_TYPE == \"Data Files\" || FILE_TYPE == \"Memory Initialization Files\" || FILE_TYPE == \"Coefficient Files\""

  # embedded file extension types 
  variable s_embedded_files_filter
  set s_embedded_files_filter        "FILE_TYPE == \"BMM\" || FILE_TYPE == \"ElF\""

  # non-hdl data files filter
  variable s_non_hdl_data_files_filter
  set s_non_hdl_data_files_filter \
               "FILE_TYPE != \"Verilog\"                      && \
                FILE_TYPE != \"SystemVerilog\"                && \
                FILE_TYPE != \"Verilog Header\"               && \
                FILE_TYPE != \"Verilog Template\"             && \
                FILE_TYPE != \"VHDL\"                         && \
                FILE_TYPE != \"VHDL 2008\"                    && \
                FILE_TYPE != \"VHDL Template\"                && \
                FILE_TYPE != \"EDIF\"                         && \
                FILE_TYPE != \"NGC\"                          && \
                FILE_TYPE != \"IP\"                           && \
                FILE_TYPE != \"XCF\"                          && \
                FILE_TYPE != \"NCF\"                          && \
                FILE_TYPE != \"UCF\"                          && \
                FILE_TYPE != \"XDC\"                          && \
                FILE_TYPE != \"NGO\"                          && \
                FILE_TYPE != \"Waveform Configuration File\"  && \
                FILE_TYPE != \"BMM\"                          && \
                FILE_TYPE != \"ELF\""

  # simulation mode types
  variable a_sim_mode_types
  set a_sim_mode_types(behavioral)          {behav}
  set a_sim_mode_types(post-synthesis)      {synth}
  set a_sim_mode_types(post-implementation) {impl}
  set a_sim_mode_types(funcsim)             {func}
  set a_sim_mode_types(timesim)             {timing}

  set properties(s_flow_dir_key)            {behav}
  set properties(s_simulation_flow)         {behav_sim}
  set properties(s_netlist_mode)            {funcsim}

  # netlist file
  set properties(s_netlist_file)            {}
}

proc usf_create_options { simulator opts } {
  # Summary:
  # Argument Usage:
  # Return Value:

  # create options
  usf_create_fs_options_spec $simulator $opts

  if { ![get_property IS_READONLY [current_project]] } {
    # simulation fileset objects
    foreach fileset_object [get_filesets -filter {FILESET_TYPE == SimulationSrcs}] {
      usf_set_fs_options $fileset_object $simulator $opts
    }
  }
}

proc usf_create_fs_options_spec { simulator opts } {
  # Summary:
  # Argument Usage:
  # Return Value:

  # create properties on the fileset object
  foreach { row } $opts  {
    set name  [lindex $row 0]
    set type  [lindex $row 1]
    set value [lindex $row 2]
    set desc  [lindex $row 3]

    # setup property name
    set prop_name "${simulator}.${name}"

    set prop_name [string tolower $prop_name]

    # is registered already?
    if { [usf_is_option_registered_on_simulator $prop_name $simulator] } {
      continue;
    }

    # is enum type?
    if { {enum} == $type } {
      set e_value   [lindex $value 0]
      set e_default [lindex $value 1]
      set e_values  [lindex $value 2]
      # create enum property
      create_property -name "${prop_name}" -type $type -description $desc -enum_values $e_values -default_value $e_default -class fileset -no_register
    } elseif { {file} == $type } {
      set f_extns   [lindex $row 4]
      set f_desc    [lindex $row 5]
      # create file property
      set v_default $value
      create_property -name "${prop_name}" -type $type -description $desc -default_value $v_default -file_types $f_extns -display_text $f_desc -class fileset -no_register
    } else {
      set v_default $value
      create_property -name "${prop_name}" -type $type -description $desc -default_value $v_default -class fileset -no_register
    }
  }
  return 0
}

proc usf_set_fs_options { fileset_object simulator opts } {
  # Summary:
  # Argument Usage:
  # Return Value:

  foreach { row } $opts  {
    set name  [lindex $row 0]
    set type  [lindex $row 1]
    set value [lindex $row 2]
    set desc  [lindex $row 3]

    set prop_name "${simulator}.${name}"

    # is registered already?
    if { [usf_is_option_registered_on_simulator $prop_name $simulator] } {
      continue;
    }

    # is enum type?
    if { {enum} == $type } {
      set value   [lindex $value 0]
      set e_default [lindex $value 1]
      set e_values  [lindex $value 2]
      set_property -name "${prop_name}" -value $value -objects ${fileset_object}
    } else {
      set v_default $value
      set_property -name "${prop_name}" -value $value -objects ${fileset_object}
    }
  }
  return 0
}

proc usf_is_option_registered_on_simulator { prop_name simulator } {
  # Summary:
  # Argument Usage:
  # Return Value:

  set str_1 [string tolower $prop_name]
  # get registered options from simulator for the current simset
  foreach option_name [get_property "REGISTERED_OPTIONS" [get_simulators $simulator]] {
    set str_2 [string tolower $option_name]
    if { [string compare $str_1 $str_2] == 0 } {
      return true
    }
  }
  return false
}

proc usf_set_simulation_flow {} {
  # Summary:
  # Argument Usage:
  # Return Value:

  variable properties

  set fileset_object [get_filesets $properties(simset)]
  set simulation_flow {unknown}
  set type_dir {timing}
  if { {behavioral} == $properties(mode) } {
    if { ({functional} == $properties(s_type)) || ({timing} == $properties(s_type)) } {
      send_msg_id USF-[usf_aldec_getSimulatorName]-1 ERROR "Invalid simulation type '$properties(s_type)' specified. Please see 'simulate -help' for more details.\n"
      return 1
    }
    set simulation_flow "behav_sim"
    set properties(s_flow_dir_key) "behav"

    # set simulation and netlist mode on simset
    set_property sim_mode "behavioral" $fileset_object

  } elseif { {post-synthesis} == $properties(mode) } {
    if { ({functional} != $properties(s_type)) && ({timing} != $properties(s_type)) } {
      send_msg_id USF-[usf_aldec_getSimulatorName]-2 ERROR "Invalid simulation type '$properties(s_type)' specified. Please see 'simulate -help' for more details.\n"
      return 1
    }
    set simulation_flow "post_synth_sim"
    if { {functional} == $properties(s_type) } {
      set type_dir "func"
    }
    set properties(s_flow_dir_key) "synth/${type_dir}"

    # set simulation and netlist mode on simset
    set_property sim_mode "post-synthesis" $fileset_object
    if { {functional} == $properties(s_type) } {
      set_property "NL.MODE" "funcsim" $fileset_object
    }
    if { {timing} == $properties(s_type) } {
      set_property "NL.MODE" "timesim" $fileset_object
    }
  } elseif { ({post-implementation} == $properties(mode)) || ({timing} == $properties(mode)) } {
    if { ({functional} != $properties(s_type)) && ({timing} != $properties(s_type)) } {
      send_msg_id USF-[usf_aldec_getSimulatorName]-3 ERROR "Invalid simulation type '$properties(s_type)' specified. Please see 'simulate -help' for more details.\n"
      return 1
    }
    set simulation_flow "post_impl_sim"
    if { {functional} == $properties(s_type) } {
      set type_dir "func"
    }
    set properties(s_flow_dir_key) "impl/${type_dir}"

    # set simulation and netlist mode on simset
    set_property sim_mode "post-implementation" $fileset_object
    if { {functional} == $properties(s_type) } { set_property "NL.MODE" "funcsim" $fileset_object }
    if { {timing} == $properties(s_type) } { set_property "NL.MODE" "timesim" $fileset_object }
  } else {
    send_msg_id USF-[usf_aldec_getSimulatorName]-4 ERROR "Invalid simulation mode '$properties(mode)' specified. Please see 'simulate -help' for more details.\n"
    return 1
  }
  set properties(s_simulation_flow) $simulation_flow
  return 0
}

proc usf_extract_ip_files {} {
  # Summary:
  # Argument Usage:
  # Return Value:

  variable properties
  if { ![get_property corecontainer.enable [current_project]] } {
    return
  }
  set properties(b_extract_ip_sim_files) [get_property extract_ip_sim_files [current_project]]
  if { $properties(b_extract_ip_sim_files) } {
    foreach ip [get_ips -all -quiet] {
      set xci_ip_name "${ip}.xci"
      set xcix_ip_name "${ip}.xcix"
      set xcix_file_path [get_property core_container [get_files -quiet -all ${xci_ip_name}]]
      if { {} != $xcix_file_path } {
        [catch {rdi::extract_ip_sim_files -of_objects [get_files -quiet -all ${xcix_ip_name}]} err]
      }
    }
  }
}

proc usf_set_sim_tcl_obj {} {
  # Summary:
  # Argument Usage:
  # Return Value:

  variable properties
  set comp_file $properties(s_comp_file)
  if { {} != $comp_file } {
    # -of_objects <full-path-to-ip-composite-file>
    set properties(sp_tcl_obj) [get_files -all -quiet [list "$comp_file"]]
    set properties(s_sim_top) [file root [file tail $properties(sp_tcl_obj)]]
  } else {
    set properties(sp_tcl_obj) [get_filesets $::tclapp::aldec::common::helpers::properties(simset)]
    # set current simset
    if { {} == $properties(sp_tcl_obj) } {
      set properties(sp_tcl_obj) [current_fileset -simset]
    }
    set properties(s_sim_top) [get_property TOP [get_filesets $properties(sp_tcl_obj)]]
  }
  send_msg_id USF-[usf_aldec_getSimulatorName]-5 INFO "Simulation object is '$properties(sp_tcl_obj)'...\n"
  return 0
}

proc usf_write_design_netlist {} {
  # Summary:
  # Argument Usage:
  # Return Value:

  variable properties
  # is behavioral?, return
  if { {behav_sim} == $properties(s_simulation_flow) } {
    return
  }
  set extn [usf_get_netlist_extn 1]

  # generate netlist
  set net_filename     [usf_get_netlist_filename];append net_filename "$extn"
  set sdf_filename     [usf_get_netlist_filename];append sdf_filename ".sdf"
  set net_file         [usf_file_normalize [file join $properties(launch_directory) $net_filename]]
  set sdf_file         [usf_file_normalize [file join $properties(launch_directory) $sdf_filename]]
  set netlist_cmd_args [usf_get_netlist_writer_cmd_args $extn]
  set sdf_cmd_args     [usf_get_sdf_writer_cmd_args]
  set design_mode      [get_property DESIGN_MODE [current_fileset]]

  # check run status
  switch -regexp -- $properties(s_simulation_flow) {
    {post_synth_sim} {
      if { {RTL} == $design_mode } {
        if { [get_param "project.checkRunResultsForUnifiedSim"] } {
          set synth_run [current_run -synthesis]
          set status [get_property "STATUS" $synth_run]
          if { ([regexp -nocase {^synth_design complete} $status] != 1) } {
            send_msg_id USF-[usf_aldec_getSimulatorName]-6 ERROR \
               "Synthesis results not available! Please run 'Synthesis' from the GUI or execute 'launch_runs <synth>' command from the Tcl console and retry this operation.\n"
            return 1
          }
        }
      }

      if { {RTL} == $design_mode } {
        set synth_run [current_run -synthesis]
        set netlist $synth_run 
        # is design for the current synth run already opened in memory?
        set synth_design [get_designs -quiet $synth_run]
        if { {} != $synth_design } {
          # design already opened, set it current
          current_design $synth_design
        } else {
          if { [catch {open_run $synth_run -name $netlist} open_error] } {
            #send_msg_id USF-[usf_aldec_getSimulatorName]-7 WARNING "open_run failed:$open_err"
          } else {
            current_design $netlist
          }
        }
      } elseif { {GateLvl} == $design_mode } {
        set netlist "rtl_1"
        # is design already opened in memory?
        set synth_design [get_designs -quiet $netlist]
        if { {} != $synth_design } {
          # design already opened, set it current
          current_design $synth_design
        } else {
          # open the design
          link_design -name $netlist
        }
      } else {
        send_msg_id USF-[usf_aldec_getSimulatorName]-8 ERROR "Unsupported design mode found while opening the design for netlist generation!\n"
        return 1
      }

      set design_in_memory [current_design]
      send_msg_id USF-[usf_aldec_getSimulatorName]-9 INFO "Writing simulation netlist file for design '$design_in_memory'..."
      # write netlist/sdf
      set wv_args "-nolib $netlist_cmd_args -file $net_file"
      if { {functional} == $properties(s_type) } {
        set wv_args "-mode funcsim $wv_args"
      } elseif { {timing} == $properties(s_type) } {
        set wv_args "-mode timesim $wv_args"
      }
      if { {.v} == $extn } {
        send_msg_id USF-[usf_aldec_getSimulatorName]-10 INFO "write_verilog $wv_args"
        eval "write_verilog $wv_args"
      } else {
        send_msg_id USF-[usf_aldec_getSimulatorName]-11 INFO "write_vhdl $wv_args"
        eval "write_vhdl $wv_args"
      }
      if { {timing} == $properties(s_type) } {
        send_msg_id USF-[usf_aldec_getSimulatorName]-12 INFO "Writing SDF file..."
        set ws_args "-mode timesim $sdf_cmd_args -file $sdf_file"
        send_msg_id USF-[usf_aldec_getSimulatorName]-13 INFO "write_sdf $ws_args"
        eval "write_sdf $ws_args"
      }
      set properties(s_netlist_file) $net_file
    }
    {post_impl_sim} {
      set impl_run [current_run -implementation]
      set netlist $impl_run
      if { [get_param "project.checkRunResultsForUnifiedSim"] } {
        set status [get_property "STATUS" $impl_run]
        if { ![get_property can_open_results $impl_run] } {
          send_msg_id USF-[usf_aldec_getSimulatorName]-14 ERROR \
             "Implementation results not available! Please run 'Implementation' from the GUI or execute 'launch_runs <impl>' command from the Tcl console and retry this operation.\n"
          return 1
        }
      }

      # is design for the current impl run already opened in memory?
      set impl_design [get_designs -quiet $impl_run]
      if { {} != $impl_design } {
        # design already opened, set it current
        current_design $impl_design
      } else {
        if { [catch {open_run $impl_run -name $netlist} open_err] } {
          #send_msg_id USF-[usf_aldec_getSimulatorName]-15 WARNING "open_run failed:$open_err"
        } else {
          current_design $impl_run
        }
      }

      set design_in_memory [current_design]
      send_msg_id USF-[usf_aldec_getSimulatorName]-16 INFO "Writing simulation netlist file for design '$design_in_memory'..."

      # write netlist/sdf
      set wv_args "-nolib $netlist_cmd_args -file $net_file"
      if { {functional} == $properties(s_type) } {
        set wv_args "-mode funcsim $wv_args"
      } elseif { {timing} == $properties(s_type) } {
        set wv_args "-mode timesim $wv_args"
      }
      if { {.v} == $extn } {
        send_msg_id USF-[usf_aldec_getSimulatorName]-17 INFO "write_verilog $wv_args"
        eval "write_verilog $wv_args"
      } else {
        send_msg_id USF-[usf_aldec_getSimulatorName]-18 INFO "write_vhdl $wv_args"
        eval "write_vhdl $wv_args"
      }
      if { {timing} == $properties(s_type) } {
        send_msg_id USF-[usf_aldec_getSimulatorName]-19 INFO "Writing SDF file..."
        set ws_args "-mode timesim $sdf_cmd_args -file $sdf_file"
        send_msg_id USF-[usf_aldec_getSimulatorName]-20 INFO "write_sdf $ws_args"
        eval "write_sdf $ws_args"
      }

      set properties(s_netlist_file) $net_file
    }
  }
  if { [file exist $net_file] } { send_msg_id USF-[usf_aldec_getSimulatorName]-21 INFO "Netlist generated:$net_file" }
  if { [file exist $sdf_file] } { send_msg_id USF-[usf_aldec_getSimulatorName]-22 INFO "SDF generated:$sdf_file" }
}

proc usf_xport_data_files { } {
  # Summary:
  # Argument Usage:
  # Return Value:

  variable properties
  variable s_data_files_filter
  variable s_non_hdl_data_files_filter
  set tcl_obj $properties(sp_tcl_obj)
  if { [usf_is_ip $tcl_obj] } {
    send_msg_id USF-[usf_aldec_getSimulatorName]-23 INFO "Inspecting IP design source files for '$properties(s_sim_top)'...\n"

    # export ip data files to run dir
    if { [get_param "project.copyDataFilesForSim"] } {
      set ip_filter "FILE_TYPE == \"IP\""
      set ip_name [file tail $tcl_obj]
      set data_files [list]
      set data_files [concat $data_files [get_files -all -quiet -of_objects [get_files -quiet *$ip_name] -filter $s_data_files_filter]]
      # non-hdl data files 
      foreach file [get_files -all -quiet -of_objects [get_files -quiet *$ip_name] -filter $s_non_hdl_data_files_filter] {
        if { [lsearch -exact [list_property $file] {IS_USER_DISABLED}] != -1 } {
          if { [get_property {IS_USER_DISABLED} $file] } {
            continue;
          }
        }
        lappend data_files $file
      }
      usf_export_data_files $data_files
    }
  } elseif { [usf_is_fileset $tcl_obj] } {
    send_msg_id USF-[usf_aldec_getSimulatorName]-24 INFO "Inspecting design source files for '$properties(s_sim_top)' in fileset '$tcl_obj'...\n"
    # export all fileset data files to run dir
    if { [get_param "project.copyDataFilesForSim"] } {
      usf_export_fs_data_files $s_data_files_filter
    }
    # export non-hdl data files to run dir
    usf_export_fs_non_hdl_data_files
  } else {
    send_msg_id USF-[usf_aldec_getSimulatorName]-25 INFO "Unsupported object source: $tcl_obj\n"
    return 1
  }
}

proc usf_uniquify_cmd_str { cmd_strs } {
  # Summary: Removes exact duplicate files (same file path)
  # Argument Usage:
  # Return Value:

  set cmd_str_set   [list]
  set uniq_cmd_strs [list]
  foreach str $cmd_strs {
    if { [lsearch -exact $cmd_str_set $str] == -1 } {
      lappend cmd_str_set $str
      lappend uniq_cmd_strs $str
    }
  }
  return $uniq_cmd_strs
}

proc usf_get_compile_order_files { } {
  # Summary:
  # Argument Usage:
  # Return Value:
  variable l_compile_order_files
  return [usf_uniquify_cmd_str $l_compile_order_files]
}

proc usf_get_top_library { } {
  # Summary:
  # Argument Usage:
  # Return Value:

  variable properties
  variable compileOrderFilesUniq 

  set flow    $properties(s_simulation_flow)
  set tcl_obj $properties(sp_tcl_obj)

  set src_mgmt_mode         [get_property "SOURCE_MGMT_MODE" [current_project]]
  set manual_compile_order  [expr {$src_mgmt_mode != "All"}]

  # was -of_objects <ip> specified?, fetch current fileset
  if { [usf_is_ip $tcl_obj] } {
    set tcl_obj [get_filesets $properties(simset)]
  }

  # 1. get the default top library set for the project
  set default_top_library $properties(associatedLibrary)

  # 2. get the library associated with the top file from the 'top_lib' property on the fileset
  set fs_top_library [get_property "TOP_LIB" [get_filesets $tcl_obj]]

  # 3. get the library associated with the last file in compile order
  set co_top_library {}
  if { ({behav_sim} == $flow) } {
    set filelist $compileOrderFilesUniq 
    if { [llength $filelist] > 0 } {
      set file_list [get_files -all [list "[lindex $filelist end]"]]
      if { [llength $file_list] > 0 } {
        set co_top_library [get_property "LIBRARY" [lindex $file_list 0]]
      }     
    }
  } elseif { ({post_synth_sim} == $flow) || ({post_impl_sim} == $flow) } {
    set file_list [get_files -quiet -compile_order sources -used_in synthesis_post -of_objects [get_filesets $tcl_obj]]
    if { [llength $file_list] > 0 } {
      set co_top_library [get_property "LIBRARY" [lindex $file_list end]]
    }
  }

  # 4. if default top library is set and the compile order file library is different
  #    than this default, return the compile order file library
  if { {} != $default_top_library } {
    # manual compile order, we just return the file set's top
    if { $manual_compile_order && ({} != $fs_top_library) } {
      return $fs_top_library
    }
    # compile order library is set and is different then the default
    if { ({} != $co_top_library) && ($default_top_library != $co_top_library) } {
      return $co_top_library
    } else {
      # worst case (default is set but compile order file library is empty or we failed to get the library for some reason)
      return $default_top_library
    }
  }

  # 5. default top library is empty at this point
  #    if fileset top library is set and the compile order file library is different
  #    than this default, return the compile order file library
  if { {} != $fs_top_library } {
    # manual compile order, we just return the file set's top
    if { $manual_compile_order } {
      return $fs_top_library
    }
    # compile order library is set and is different then the fileset
    if { ({} != $co_top_library) && ($fs_top_library != $co_top_library) } {
      return $co_top_library
    } else {
      # worst case (fileset library is set but compile order file library is empty or we failed to get the library for some reason)
      return $fs_top_library
    }
  }

  # 6. Both the default and fileset library are empty, return compile order library else xilinx default
  if { {} != $co_top_library } {
    return $co_top_library
  }

  return "xil_defaultlib"
}

proc usf_contains_vhdl { design_files } {
  # Summary:
  # Argument Usage:
  # Return Value:

  variable properties

  set flow $properties(s_simulation_flow)

  set b_vhdl_srcs 0
  foreach file $design_files {
    set type [lindex [split $file {|}] 0]
    switch $type {
      {VHDL} -
      {VHDL 2008} {
        set b_vhdl_srcs 1
      }
    }
  }

  if { (({post_synth_sim} == $flow) || ({post_impl_sim} == $flow)) && (!$b_vhdl_srcs) } {
    set extn [file extension $properties(s_netlist_file)]
    if { {.vhd} == $extn } {
      set b_vhdl_srcs 1
    }
  }

  return $b_vhdl_srcs
}

proc usf_contains_verilog { design_files } {
  # Summary:
  # Argument Usage:
  # Return Value:

  variable properties

  set flow $properties(s_simulation_flow)

  set b_verilog_srcs 0
  foreach file $design_files {
    set type [lindex [split $file {|}] 0]
    switch $type {
      {VERILOG} {
        set b_verilog_srcs 1
      }
    }
  }

  if { (({post_synth_sim} == $flow) || ({post_impl_sim} == $flow)) && (!$b_verilog_srcs) } {
    set extn [file extension $properties(s_netlist_file)]
    if { {.v} == $extn } {
      set b_verilog_srcs 1
    }
  }

  return $b_verilog_srcs
}

proc usf_is_fileset { tcl_obj } {
  # Summary:
  # Argument Usage:
  # Return Value:

  if {[regexp -nocase {^fileset_type} [rdi::get_attr_specs -quiet -object $tcl_obj -regexp .*FILESET_TYPE.*]]} {
    return 1
  }
  return 0
}

proc usf_append_define_generics { def_gen_list tool opts_arg } {
  # Summary:
  # Argument Usage:
  # Return Value:

  upvar $opts_arg opts
  set b_group_files [get_param "project.assembleFilesByLibraryForUnifiedSim"]

  foreach element $def_gen_list {
    set key_val_pair [split $element "="]
    set name [lindex $key_val_pair 0]
    set val  [lindex $key_val_pair 1]
    set str "+define+$name=" 
    if { $b_group_files } {    
      # escape '
      if { [regexp {'} $val] } {
        regsub -all {'} $val {\\'} val
      }
    }

    if { [string length $val] > 0 } {
      set str "$str\"$val\""
    }

    switch -regexp -- $tool {
      "vlog" { lappend opts "$str"  }
    }
  }
}

proc usf_append_generics { generic_list opts_arg } {
  # Summary:
  # Argument Usage:
  # Return Value:

  upvar $opts_arg opts

  foreach element $generic_list {
    set key_val_pair [split $element "="]
    set name [lindex $key_val_pair 0]
    set val  [lindex $key_val_pair 1]
    set str "-g$name="
    if { [string length $val] > 0 } {
      set str $str$val
    }
    lappend opts $str
  }
}

proc usf_compile_glbl_file { simulator b_load_glbl design_files } {
  # Summary:
  # Argument Usage:
  # Return Value:

  variable properties
  set fileset_object      [get_filesets $properties(simset)]
  set target_lang [get_property "TARGET_LANGUAGE" [current_project]]
  set flow        $properties(s_simulation_flow)
  if { [usf_contains_verilog $design_files] } {
    return 1
  }
  # target lang is vhdl and glbl is added as top for post-implementation and post-synthesis and load glbl set (default)
  if { ((({VHDL} == $target_lang) || ({VHDL 2008} == $target_lang)) && (({post_synth_sim} == $flow) || ({post_impl_sim} == $flow)) && $b_load_glbl) } {
    return 1
  }
  return 0
}

proc usf_copy_glbl_file {} {
  # Summary:
  # Argument Usage:
  # Return Value:

  variable properties
  set run_dir $properties(launch_directory)

  set target_glbl_file [usf_file_normalize [file join $run_dir "glbl.v"]]
  if { [file exists $target_glbl_file] } {
    return
  }

  set data_dir [rdi::get_data_dir -quiet -datafile verilog/src/glbl.v]
  set src_glbl_file [usf_file_normalize [file join $data_dir "verilog/src/glbl.v"]]

  if {[catch {file copy -force $src_glbl_file $run_dir} error_msg] } {
    send_msg_id USF-[usf_aldec_getSimulatorName]-26 WARNING "Failed to copy glbl file '$src_glbl_file' to '$run_dir' : $error_msg\n"
  }
}

proc usf_create_do_file { simulator do_filename } {
  # Summary:
  # Argument Usage:
  # Return Value:

  variable properties
  set fileset_object [current_fileset -simset]
  set top $::tclapp::aldec::common::helpers::properties(s_sim_top)
  set do_file [file join $properties(launch_directory) $do_filename]
  set fh_do 0
  if {[catch {open $do_file w} fh_do]} {
    send_msg_id USF-[usf_aldec_getSimulatorName]-27 ERROR "Failed to open file to write ($do_file)\n"
  } else {
    set time [get_property "RUNTIME" $fileset_object]
    puts $fh_do "run $time"
  }
  close $fh_do
}

proc usf_prepare_ip_for_simulation { } {
  # Summary:
  # Argument Usage:
  # Return Value:

  variable properties
  #if { [regexp {^post_} $properties(s_simulation_flow)] } {
  #  return
  #}
  variable properties
  # list of block filesets and corresponding runs to launch
  set fileset_objects        [list]
  set runs_to_launch [list]
  # target object (fileset or ip)
  set target_obj $properties(sp_tcl_obj)
  if { [usf_is_fileset $target_obj] } {
    set fs $target_obj
    # add specified fileset (expected simulation fileset)
    lappend fileset_objects $fs
    # add linked source fileset
    if { {SimulationSrcs} == [get_property "FILESET_TYPE" [get_filesets $fs]] } {
      set src_set [get_property "SOURCE_SET" [get_filesets $fs]]
      if { {} != $src_set } {
        lappend fileset_objects $src_set
      }
    }
    # add block filesets
    set filter "FILESET_TYPE == \"BlockSrcs\""
    foreach blk_fileset_object [get_filesets -filter $filter] {
      lappend fileset_objects $blk_fileset_object
    }
    set ip_filter "FILE_TYPE == \"IP\""
    foreach fileset_object $fileset_objects {
      set fs_name [get_property "NAME" [get_filesets $fileset_object]]
      send_msg_id USF-[usf_aldec_getSimulatorName]-28 INFO "Inspecting fileset '$fs_name' for IP generation...\n"
      # get ip composite files
      foreach comp_file [get_files -quiet -of_objects [get_filesets $fileset_object] -filter $ip_filter] {
        usf_generate_comp_file_for_simulation $comp_file runs_to_launch
      }
    }
    # fileset contains embedded sources? generate mem files
    if { [usf_is_embedded_flow] } {
      send_msg_id USF-[usf_aldec_getSimulatorName]-29 INFO "Design contains embedded sources, generating MEM files for simulation...\n"
      generate_mem_files $properties(launch_directory)
    }
  } elseif { [usf_is_ip $target_obj] } {
    set comp_file $target_obj
    usf_generate_comp_file_for_simulation $comp_file runs_to_launch
  } else {
    send_msg_id USF-[usf_aldec_getSimulatorName]-30 ERROR "Unknown target '$target_obj'!\n"
  }
  # generate functional netlist  
  if { [llength $runs_to_launch] > 0 } {
    send_msg_id USF-[usf_aldec_getSimulatorName]-31 INFO "Launching block-fileset run '$runs_to_launch'...\n"
    launch_runs $runs_to_launch

    foreach run $runs_to_launch {
      wait_on_run [get_property "NAME" [get_runs $run]]
    }
  }
  # update compile order
  if { {None} != [get_property "SOURCE_MGMT_MODE" [current_project]] } {
    foreach fs $fileset_objects {
      if { [usf_fs_contains_hdl_source $fs] } {
        update_compile_order -fileset [get_filesets $fs]
      }
    }
  }
}

proc usf_generate_mem_files_for_simulation { } {
  # Summary:
  # Argument Usage:
  # Return Value:

  variable properties

  if { [usf_is_fileset $properties(sp_tcl_obj)] } {
    # fileset contains embedded sources? generate mem files
    if { [usf_is_embedded_flow] } {
      send_msg_id USF-[usf_aldec_getSimulatorName]-32 INFO "Design contains embedded sources, generating MEM files for simulation...\n"
      generate_mem_files $properties(launch_directory)
    }
  }
}

proc usf_fs_contains_hdl_source { fs } {
  # Summary:
  # Argument Usage:
  # Return Value:

  variable l_valid_ip_extns
  variable valid_hdl_extensions

  set b_contains_hdl 0
  set tokens [split [find_top -fileset $fs -return_file_paths] { }]
  for {set i 0} {$i < [llength $tokens]} {incr i} {
    set top [string trim [lindex $tokens $i]];incr i
    set file [string trim [lindex $tokens $i]]
    if { ({} == $top) || ({} == $file) } { continue; }
    set extn [file extension $file]

    # skip ip's
    if { [lsearch -exact $l_valid_ip_extns $extn] >= 0 } { continue; }

    # check if any HDL sources present in fileset
    if { [lsearch -exact $valid_hdl_extensions $extn] >= 0 } {
      set b_contains_hdl 1
      break
    }
  }
  return $b_contains_hdl
}

proc usf_aldec_set_simulator_path {} {
  # Summary:
  # Argument Usage:
  # Return Value:

  variable properties

  set bin_path  {}
  set tool_name {} 
  set path_sep  {;}

  if {$::tcl_platform(platform) == "unix"} { set path_sep {:} }
  set install_path $properties(s_install_path)
  send_msg_id USF-[usf_aldec_getSimulatorName]-33 INFO "Finding simulator installation...\n"

  switch -- [get_property target_simulator [current_project]] {
    Riviera {
      set tool_extn {.bat}
      if {$::tcl_platform(platform) == "unix"} { set tool_extn {} }
      set tool_name "../rungui";append tool_name ${tool_extn}
      if { $install_path == "" } {
        set install_path [get_param "simulator.rivieraInstallPath"] 
      }
    }
    ActiveHDL {
      set tool_extn {.exe}
      if {$::tcl_platform(platform) == "unix"} { set tool_extn {} }    
      set tool_name "avhdl";append tool_name ${tool_extn}
      if { $install_path == "" } {
        set install_path [get_param "simulator.activehdlInstallPath"] 
      }
    }
  }

  if { {} == $install_path } {
    set bin_path [usf_get_bin_path $tool_name $path_sep]
    if { {} == $bin_path } {
      if { $properties(only_generate_scripts) } {
        set bin_path {<specify-simulator-tool-path>}
        send_msg_id USF-[usf_aldec_getSimulatorName]-34 WARNING \
          "Simulator executable path could not be located. Please make sure to set the path in the generated scripts manually before executing these scripts.\n"
      } else {
        send_msg_id USF-[usf_aldec_getSimulatorName]-35 ERROR \
          "Failed to locate '$tool_name' executable in the shell environment 'PATH' variable. Please source the settings script included with the installation and retry this operation again.\n"
        # IMPORTANT - *** DONOT MODIFY THIS ***
        error "_SIM_STEP_RUN_EXEC_ERROR_"
        # IMPORTANT - *** DONOT MODIFY THIS ***
        return 1
      }
    } else {
      send_msg_id USF-[usf_aldec_getSimulatorName]-36 INFO "Using simulator executables from '$bin_path'\n"
    }
  } else {
    set install_path [usf_file_normalize [string map {\\ /} $install_path]]
    set install_path [string trimright $install_path {/}]
    set bin_path $install_path
    set tool_path [file join $install_path $tool_name]
    # Couldn't find it at install path, so try inserting /bin.
    # This is a bit roundabout with new variables so we don't change the
    # originals. If this doesn't work, we want the error messages to report
    # based on the originals.
    set tool_bin_path {}
    if { ![file exists $tool_path] } {
      set tool_bin_path [file join $install_path "bin" $tool_name]
      if { [file exists $tool_bin_path] } {
        set tool_path $tool_bin_path
        set bin_path [file join $install_path "bin"]
      }
    }
    if { [file exists $tool_path] && ![file isdirectory $tool_path] } {
      send_msg_id USF-[usf_aldec_getSimulatorName]-37 INFO "Using simulator executables from '$tool_path'\n"
    } else {
      send_msg_id USF-[usf_aldec_getSimulatorName]-38 ERROR "Path to custom '$tool_name' executable program does not exist:$tool_path'\n"
    }
  }

  set properties(s_tool_bin_path) [string map {/ \\\\} $bin_path]
  if {$::tcl_platform(platform) == "unix"} {
    set properties(s_tool_bin_path) $bin_path
  }
}

proc usf_get_files_for_compilation { global_files_str_arg } {
  # Summary:
  # Argument Usage:
  # Return Value:

  variable properties
  upvar $global_files_str_arg global_files_str

  set sim_flow $properties(s_simulation_flow)
 
  set design_files [list]
  if { ({behav_sim} == $sim_flow) } {
    set design_files [usf_get_files_for_compilation_behav_sim $global_files_str]
  } elseif { ({post_synth_sim} == $sim_flow) || ({post_impl_sim} == $sim_flow) } {
    set design_files [usf_get_files_for_compilation_post_sim $global_files_str]
  }
  return $design_files
}

proc usf_get_files_for_compilation_behav_sim { global_files_str_arg } {
  # Summary:
  # Argument Usage:
  # Return Value:

  variable properties
  variable compiledLibraries
  variable systemVerilogPackageLibraries	
  variable l_compile_order_files
  upvar $global_files_str_arg global_files_str

  set files                 [list]
  set l_compile_order_files [list]
  set target_obj            $properties(sp_tcl_obj)
  set simset_obj            [get_filesets $::tclapp::aldec::common::helpers::properties(simset)]
  set linked_src_set        [get_property "SOURCE_SET" $simset_obj]
  set target_lang           [get_property "TARGET_LANGUAGE" [current_project]]
  set src_mgmt_mode         [get_property "SOURCE_MGMT_MODE" [current_project]]

  # get global include file paths
  set incl_file_paths [list]
  set incl_files      [list]
  send_msg_id USF-[usf_aldec_getSimulatorName]-39 INFO "Finding global include files..."
  usf_get_global_include_files incl_file_paths incl_files

  set global_incl_files $incl_files
  set global_files_str [usf_get_global_include_file_cmdstr incl_files]

  # verilog incl dir's and verilog headers directory path if any
  send_msg_id USF-[usf_aldec_getSimulatorName]-40 INFO "Finding include directories and verilog header directory paths..."
  set include_directories_options [list]
  set unique_directories [list]
  #puts [usf_get_verilog_header_paths]
  foreach dir [concat [usf_get_include_dirs] [usf_get_verilog_header_paths] [getVipIncludeDirs] ] {
    if { [lsearch -exact $unique_directories $dir] == -1 } {
      lappend unique_directories $dir
      lappend include_directories_options "\"+incdir+$dir\""
    }
  }

    if { ( [ lsearch -exact $compiledLibraries "xilinx_vip" ] == -1 ) } {
		if { [ llength $systemVerilogPackageLibraries ] > 0 } {
			set incl_dir_opts "\\\"+incdir+[getVipIncludeDirs]\\\""
			foreach file [ getXilinxVipFiles ] {
				set fileType "SystemVerilog"
				set g_files $global_files_str
				set cmd_str [ getFileCmdStr $file $fileType true $g_files incl_dir_opts "" "xilinx_vip" ]

				if { {} != $cmd_str } {
					lappend files $cmd_str
					lappend l_compile_order_files $file
				}
			}
		}
	} elseif { [ is_vip_ip_required ] } {
		set data_dir [rdi::get_data_dir -quiet -datafile xilinx_vip]
		set dir "${data_dir}/xilinx_vip/include"
		if { [lsearch -exact $unique_directories $dir] == -1 } {
			lappend unique_directories $dir
			lappend include_directories_options "\"+incdir+$dir\""
		}
	}

  # prepare command line args for fileset files
  if { [usf_is_fileset $target_obj] } {
    set used_in_val "simulation"
    switch [get_property "FILESET_TYPE" [get_filesets $target_obj]] {
      "DesignSrcs"     { set used_in_val "synthesis" }
      "SimulationSrcs" { set used_in_val "simulation"}
      "BlockSrcs"      { set used_in_val "synthesis" }
    }
    set xpm_libraries [get_property -quiet xpm_libraries [current_project]]
    foreach library $xpm_libraries {
      foreach file [rdi::get_xpm_files -library_name $library] {
        set file_type "SystemVerilog"
        set g_files $global_files_str
        set cmd_str [usf_get_file_cmd_str $file $file_type $g_files include_directories_options]
        if { {} != $cmd_str } {
          lappend files $cmd_str
          lappend l_compile_order_files $file
        }
      }
    }
    set b_add_sim_files 1
    # add files from block filesets
    if { {} != $linked_src_set } {
      if { [get_param project.addBlockFilesetFilesForUnifiedSim] } {
        usf_add_block_fs_files $global_files_str include_directories_options files l_compile_order_files
      }
    }
    # add files from simulation compile order
    if { {All} == $src_mgmt_mode } {
      send_msg_id USF-[usf_aldec_getSimulatorName]-41 INFO "Fetching design files from '$target_obj'..."
      foreach file [get_files -quiet -compile_order sources -used_in $used_in_val -of_objects [get_filesets $target_obj]] {
        if { [usf_is_global_include_file $global_files_str $file] } { continue }
        set file_type [ get_property "FILE_TYPE" $file ]
        if { ![ is_hdl_type $file_type ] } { continue }
        set g_files $global_files_str
        if { ({VHDL} == $file_type) || ({VHDL 2008} == $file_type) } { set g_files {} }
        set cmd_str [usf_get_file_cmd_str $file $file_type $g_files include_directories_options]
        if { {} != $cmd_str } {
          lappend files $cmd_str
          lappend l_compile_order_files $file
        }
      }
      set b_add_sim_files 0
    } else {
      # add files from SOURCE_SET property value
      if { {} != $linked_src_set } {
        set srcset_obj [get_filesets $linked_src_set]
        if { {} != $srcset_obj } {
          send_msg_id USF-[usf_aldec_getSimulatorName]-42 INFO "Fetching design files from '$srcset_obj'...(this may take a while)..."
          foreach file [get_files -quiet -compile_order sources -used_in $used_in_val -of_objects [get_filesets $srcset_obj]] {
            set file_type [get_property "FILE_TYPE" [lindex [get_files -quiet -all [list "$file"]] 0]]
            set is_type_hdl [is_hdl_type $file_type]
            if { !$is_type_hdl } { continue }
            set g_files $global_files_str
            if { ({VHDL} == $file_type) || ({VHDL 2008} == $file_type) } { set g_files {} }
            set cmd_str [usf_get_file_cmd_str $file $file_type $g_files include_directories_options]
            if { {} != $cmd_str } {
              lappend files $cmd_str
              lappend l_compile_order_files $file
            }
          }
        }
      }
    }

    if { $b_add_sim_files } {
      # add additional files from simulation fileset
      send_msg_id USF-[usf_aldec_getSimulatorName]-43 INFO "Fetching design files from '$properties(simset)'..."
      foreach file [get_files -quiet -all -of_objects [get_filesets $properties(simset)]] {
        set file_type [get_property "FILE_TYPE" [lindex [get_files -quiet -all [list "$file"]] 0]]
        set is_type_hdl [is_hdl_type $file_type]
        if { !$is_type_hdl } { continue }
        if { [get_property "IS_AUTO_DISABLED" [lindex [get_files -quiet -all [list "$file"]] 0]]} { continue }
        set g_files $global_files_str
        if { ({VHDL} == $file_type) || ({VHDL 2008} == $file_type) } { set g_files {} }
        set cmd_str [usf_get_file_cmd_str $file $file_type $g_files include_directories_options]
        if { {} != $cmd_str } {
          lappend files $cmd_str
          lappend l_compile_order_files $file
        }
      }
    }
  } elseif { [usf_is_ip $target_obj] } {
    # prepare command line args for fileset ip files
    send_msg_id USF-[usf_aldec_getSimulatorName]-44 INFO "Fetching design files from IP '$target_obj'..."
    set ip_filename [file tail $target_obj]
    foreach file [get_files -quiet -compile_order sources -used_in simulation -of_objects [get_files -quiet *$ip_filename]] {
      set file_type [get_property "FILE_TYPE" [lindex [get_files -quiet -all [list "$file"]] 0]]
      set is_type_hdl [is_hdl_type $file_type]
      if { !$is_type_hdl } { continue }
      set g_files $global_files_str
      if { ({VHDL} == $file_type) || ({VHDL 2008} == $file_type) } { set g_files {} }
      set cmd_str [usf_get_file_cmd_str $file $file_type $g_files include_directories_options]
      if { {} != $cmd_str } {
        lappend files $cmd_str
        lappend l_compile_order_files $file
      }
    }
  }
  return $files
}

proc usf_get_files_for_compilation_post_sim { global_files_str_arg } {
  # Summary:
  # Argument Usage:
  # Return Value:

  variable properties
  variable l_compile_order_files
  upvar $global_files_str_arg global_files_str

  set files         [list]
  set l_compile_order_files [list]
  set netlist_file  $properties(s_netlist_file)
  set target_obj    $properties(sp_tcl_obj)
  set target_lang   [get_property "TARGET_LANGUAGE" [current_project]]
  set src_mgmt_mode [get_property "SOURCE_MGMT_MODE" [current_project]]

  # get global include file paths
  set incl_file_paths [list]
  set incl_files      [list]
  usf_get_global_include_files incl_file_paths incl_files

  set global_incl_files $incl_files
  set global_files_str [usf_get_global_include_file_cmdstr incl_files]

  # verilog incl dir's and verilog headers directory path if any
  set include_directories_options [list]
  set unique_directories [list]
  foreach dir [concat [usf_get_include_dirs] [usf_get_verilog_header_paths] [getVipIncludeDirs]] {
    if { [lsearch -exact $unique_directories $dir] == -1 } {
      lappend unique_directories $dir
      lappend include_directories_options "\"+incdir+$dir\""
    }
  }

  if { {} != $netlist_file } {
    set file_type "Verilog"
    if { {.vhd} == [file extension $netlist_file] } {
      set file_type "VHDL"
    }
    set cmd_str [usf_get_file_cmd_str $netlist_file $file_type {} include_directories_options]
    if { {} != $cmd_str } {
      lappend files $cmd_str
      lappend l_compile_order_files $netlist_file
    }
  }

  # add testbench files if any
  #set vhdl_filter "USED_IN_SIMULATION == 1 && (FILE_TYPE == \"VHDL\" || FILE_TYPE == \"VHDL 2008\")"
  #foreach file [usf_get_testbench_files_from_ip $vhdl_filter] {
  #  if { [lsearch -exact [list_property $file] {FILE_TYPE}] == -1 } {
  #    continue;
  #  }
  #  #set file_type [get_property "FILE_TYPE" [lindex [get_files -quiet -all [list "$file"]] 0]]
  #  set file_type [get_property "FILE_TYPE" $file]
  #  set cmd_str [usf_get_file_cmd_str $file $file_type {} include_directories_options]
  #  if { {} != $cmd_str } {
  #    lappend files $cmd_str
  #    lappend l_compile_order_files $file
  #  }
  #}
  ##set verilog_filter "USED_IN_TESTBENCH == 1 && FILE_TYPE == \"Verilog\" && FILE_TYPE == \"Verilog Header\""
  #set verilog_filter "USED_IN_SIMULATION == 1 && (FILE_TYPE == \"Verilog\" || FILE_TYPE == \"SystemVerilog\")"
  #foreach file [usf_get_testbench_files_from_ip $verilog_filter] {
  #  if { [lsearch -exact [list_property $file] {FILE_TYPE}] == -1 } {
  #    continue;
  #  }
  #  #set file_type [get_property "FILE_TYPE" [lindex [get_files -quiet -all [list "$file"]] 0]]
  #  set file_type [get_property "FILE_TYPE" $file]
  #  set cmd_str [usf_get_file_cmd_str $file $file_type {} include_directories_options]
  #  if { {} != $cmd_str } {
  #    lappend files $cmd_str
  #    lappend l_compile_order_files $file
  #  }
  #}

  # prepare command line args for fileset files
  if { [usf_is_fileset $target_obj] } {

    # 851957 - if simulation and design source file tops are same (no testbench), skip adding simset files. Just pass the netlist above.
    set src_fs_top [get_property top [current_fileset]]
    set sim_fs_top [get_property top [get_filesets $properties(simset)]]
    if { $src_fs_top == $sim_fs_top } {
      return $files
    }

    # add additional files from simulation fileset
    set simset_files [get_files -compile_order sources -used_in synthesis_post -of_objects [get_filesets $properties(simset)]]
    foreach file $simset_files {
      set file_type [get_property "FILE_TYPE" [lindex [get_files -quiet -all [list "$file"]] 0]]
      set is_type_hdl [is_hdl_type $file_type]
      if { !$is_type_hdl } { continue }
      #if { [get_property "IS_AUTO_DISABLED" [lindex [get_files -quiet -all [list "$file"]] 0]]} { continue }
      set g_files $global_files_str
      if { ({VHDL} == $file_type) || ({VHDL 2008} == $file_type) } { set g_files {} }
      set cmd_str [usf_get_file_cmd_str $file $file_type $g_files include_directories_options]
      if { {} != $cmd_str } {
        lappend files $cmd_str
        lappend l_compile_order_files $file
      }
    }
  } elseif { [usf_is_ip $target_obj] } {
    # prepare command line args for fileset ip files
    set ip_filename [file tail $target_obj]
    foreach file [get_files -quiet -compile_order sources -used_in simulation -of_objects [get_files -quiet *$ip_filename]] {
      set file_type [get_property "FILE_TYPE" [lindex [get_files -quiet -all [list "$file"]] 0]]
      set is_type_hdl [is_hdl_type $file_type]
      if { !$is_type_hdl } { continue }
      set g_files $global_files_str
      if { ({VHDL} == $file_type) || ({VHDL} == $file_type) } { set g_files {} }
      set cmd_str [usf_get_file_cmd_str $file $file_type $g_files include_directories_options]
      if { {} != $cmd_str } {
        lappend files $cmd_str
        lappend l_compile_order_files $file
      }
    }
  }
  return $files
}

proc usf_add_block_fs_files { global_files_str include_directories_options_arg files_arg compile_order_files_arg } {
  # Summary:
  # Argument Usage:
  # Return Value:

  upvar $include_directories_options_arg include_directories_options
  upvar $files_arg files
  upvar $compile_order_files_arg compile_order_files

  set vhdl_filter "FILE_TYPE == \"VHDL\" || FILE_TYPE == \"VHDL 2008\""
  foreach file [usf_get_files_from_block_filesets $vhdl_filter] {
    set file_type [get_property "FILE_TYPE" [lindex [get_files -quiet -all [list "$file"]] 0]]
    set cmd_str [usf_get_file_cmd_str $file $file_type {} include_directories_options]
    if { {} != $cmd_str } {
      lappend files $cmd_str
      lappend compile_order_files $file
    }
  }
  set verilog_filter "FILE_TYPE == \"Verilog\" || FILE_TYPE == \"SystemVerilog\""
  foreach file [usf_get_files_from_block_filesets $verilog_filter] {
    set file_type [get_property "FILE_TYPE" [lindex [get_files -quiet -all [list "$file"]] 0]]
    set cmd_str [usf_get_file_cmd_str $file $file_type $global_files_str include_directories_options]
    if { {} != $cmd_str } {
      lappend files $cmd_str
      lappend compile_order_files $file
    }
  }
}

proc usf_is_global_include_file { global_files_str file_to_find } {
  # Summary:
  # Argument Usage:
  # Return Value:

  foreach g_file [split $global_files_str { }] {
    set g_file [string trim $g_file {\"}]
    if { [string compare $g_file $file_to_find] == 0 } {
      return true
    }
  }
  return false
}

proc usf_launch_script { step } {
  # Summary:
  # Argument Usage:
  # Return Value:

  variable properties
  set extn [usf_get_script_extn]
  set scr_file ${step}$extn
  set run_dir $properties(launch_directory)

  set shell_script_file [usf_file_normalize [file join $run_dir $scr_file]]
  usf_make_file_executable $shell_script_file

  if { $properties(only_generate_scripts) } {
    send_msg_id USF-[usf_aldec_getSimulatorName]-45 INFO "Script generated:[usf_file_normalize [file join $run_dir $scr_file]]"
    return 0
  }

  set b_wait 0
  if { $properties(batch_mode_enabled) } {
    set b_wait 1 
  }
  set faulty_run 0
  set cwd [pwd]
  cd $::tclapp::aldec::common::helpers::properties(launch_directory)
  send_msg_id USF-[usf_aldec_getSimulatorName]-46 INFO "Executing '[string toupper $step]' step in '$run_dir'"
  set results_log {}
  switch $step {
    {compile} -
    {elaborate} {
      if {[catch {rdi::run_program $scr_file} error_log]} {
        set faulty_run 1
      }
      # check errors
      if { [usf_aldec_check_errors $step results_log] } {
        set faulty_run 1
      }
    }
    {simulate} {
      set retval 0
      set error_log {}
      if { $b_wait } {
        set retval [catch {rdi::run_program $scr_file} error_log]
      } else {
        set retval [catch {rdi::run_program -no_wait $scr_file} error_log]
      }
      if { $retval } {
        send_msg_id USF-[usf_aldec_getSimulatorName]-47 ERROR "Failed to launch $scr_file:$error_log\n"
        set faulty_run 1
      }
    }
  }
  cd $cwd

  if { $faulty_run } {
    [catch {send_msg_id USF-[usf_aldec_getSimulatorName]-48 ERROR "'$step' step failed with error(s). Please check the Tcl console output or '$results_log' file for more information.\n"}]
    # IMPORTANT - *** DONOT MODIFY THIS ***
    error "_SIM_STEP_RUN_EXEC_ERROR_"
    # IMPORTANT - *** DONOT MODIFY THIS ***
    return 1
  }
  return 0
}

proc usf_write_shell_step_fn { fh } {
  # Summary:
  # Argument Usage:
  # Return Value:

  puts $fh "ExecStep()"
  puts $fh "\{"
  puts $fh "\"\$@\""
  puts $fh "RETVAL=\$?"
  puts $fh "if \[ \$RETVAL -ne 0 \]"
  puts $fh "then"
  puts $fh "exit \$RETVAL"
  puts $fh "fi"
  puts $fh "\}"
}

proc usf_resolve_uut_name { uut_arg } {
  # Summary:
  # Argument Usage:
  # Return Value:

  upvar $uut_arg uut
  set uut [string map {\\ /} $uut]
  # prepend slash
  if { ![string match "/*" $uut] } {
    set uut "/$uut"
  }
  # append *
  if { [string match "*/" $uut] } {
    set uut "${uut}*"
  }
  # append /*
  if { {/*} != [string range $uut end-1 end] } {
    set uut "${uut}/*"
  }
  return $uut
}

proc usf_get_script_extn {} {
  # Summary:
  # Argument Usage:
  # Return Value:

  set scr_extn ".bat"
  if {$::tcl_platform(platform) == "unix"} {
    set scr_extn ".sh"
  }
  return $scr_extn
}

proc usf_make_file_executable { file } {
  # Summary:
  # Argument Usage:
  # Return Value:

  if {$::tcl_platform(platform) == "unix"} {
    if {[catch {exec chmod a+x $file} error_msg] } {
      send_msg_id USF-[usf_aldec_getSimulatorName]-96 WARNING "Failed to change file permissions to executable ($file): $error_msg\n"
    }
  } else {
    if {[catch {exec attrib /D -R $file} error_msg] } {
      send_msg_id USF-[usf_aldec_getSimulatorName]-96 WARNING "Failed to change file permissions to executable ($file): $error_msg\n"
    }
  }
}

proc usf_aldec_get_platform {} {
  # Summary:
  # Argument Usage:
  # Return Value:

  variable properties

  switch -- [get_property target_simulator [current_project]] {
    Riviera {
      set tool_extn {.bat}
      if { $::tcl_platform(platform) == "unix" } { set tool_extn {} }
      set vsimsa [file join $properties(s_tool_bin_path) "../runvsimsa$tool_extn"]
      if { [file isfile $vsimsa] } {
        set in [open "| \"$vsimsa\" -version"]
        set data [read $in]
        close $in
        switch -regexp -- $data {
          "built for Windows " { return "win32" }
          "built for Windows64" { return "win64" }
          "built for Linux " { return "lnx32" }
          "built for Linux64" { return "lnx64" }
        }
      }
    }
    ActiveHDL {
      set avhdlXml [file join $properties(s_tool_bin_path) avhdl.xml]
      if { [file isfile $avhdlXml] } {
        set in [open $avhdlXml r]
        while { ![eof $in] } {
          gets $in line
          if { [regexp {UserConfigPath.+\\32-bit\\.+} $line] } {
            return "win32"
          } elseif { [regexp {UserConfigPath.+\\64-bit\\.+} $line] } {
            return "win64"
          }
        }
        close $in
      }
    }
  }
  
  send_msg_id USF-[usf_aldec_getSimulatorName]-49 WARNING "Failed to detect whether 32- or 64-bit version of [usf_aldec_getSimulatorName] is installed.\n"

  set fileset_object [get_filesets $properties(simset)]
  set platform {}
  set os $::tcl_platform(platform)
  set b_32_bit [get_property 32bit $fileset_object]
  if { {windows} == $os } {
    set platform "win64"
    if { $b_32_bit } {
      set platform "win32"
    }
  }

  if { {unix} == $os } {
    set platform "lnx64"
    if { $b_32_bit } {
      set platform "lnx32"
    }
  }
  return $platform
}

proc usf_is_axi_bfm_ip {} {
  # Summary: Finds VLNV property value for the IP and checks to see if the IP is AXI_BFM
  # Argument Usage:
  # Return Value:
  # true (1) if specified IP is axi_bfm, false (0) otherwise

  foreach ip [get_ips -all -quiet] {
    set ip_def [lindex [split [get_property "IPDEF" [get_ips -all -quiet $ip]] {:}] 2]
    set ip_def_obj [get_ipdefs -quiet -regexp .*${ip_def}.*]
    #puts ip_def_obj=$ip_def_obj
    if { {} != $ip_def_obj } {
      set value [get_property "VLNV" $ip_def_obj]
      #puts is_axi_bfm_ip=$value
      if { ([regexp -nocase {axi_bfm} $value]) || ([regexp -nocase {processing_system7} $value]) } {
        return 1
      }
    }
  }
  return 0
}

proc usf_get_simulator_lib_for_bfm {} {
  # Summary:
  # Argument Usage:
  # Return Value:

  if { [usf_aldec_get_vivado_version] >= 2017.1 } {
    return
  }

  set simulator_lib {}
  set xil           $::env(XILINX_VIVADO)
  set path_sep      {;}
  set lib_extn      {.dll}
  set platform      [::tclapp::aldec::common::helpers::usf_aldec_get_platform]

  if {$::tcl_platform(platform) == "unix"} { set path_sep {:} }
  if {$::tcl_platform(platform) == "unix"} { set lib_extn {.so} }

  set lib_name "libxil_riviera"; append lib_name $lib_extn

  if { {} != $xil } {
    append platform ".o"
    set lib_path {}
    send_msg_id USF-[usf_aldec_getSimulatorName]-50 INFO "Finding simulator library from 'XILINX_VIVADO'..."
    foreach path [split $xil $path_sep] {
      set file [usf_file_normalize [file join $path "lib" $platform $lib_name]]
      if { [file exists $file] } {
        send_msg_id USF-[usf_aldec_getSimulatorName]-51 INFO "Using library:'$file'"
        set simulator_lib $file
        break
      } else {
        send_msg_id USF-[usf_aldec_getSimulatorName]-52 WARNING "Library not found:'$file'"
      }
    }
  } else {
    send_msg_id USF-[usf_aldec_getSimulatorName]-53 ERROR "Environment variable 'XILINX_VIVADO' is not set!"
  }

  if { $simulator_lib == {} } {
    send_msg_id USF-[usf_aldec_getSimulatorName]-54 ERROR "Failed to locate simulator library from 'XILINX' environment variable."
  }

  return $simulator_lib
}
}

#
# Low level helper procs
# 
namespace eval ::tclapp::aldec::common::helpers {
proc usf_get_netlist_extn { warning } {
  # Summary:
  # Argument Usage:
  # Return Value:

  variable properties

  set extn {.v}
  set target_lang [get_property "TARGET_LANGUAGE" [current_project]]
  if { {VHDL} == $target_lang } {
    set extn {.vhd}
  }
  
  if { (({VHDL} == $target_lang) && ({timing} == $properties(s_type))) } {
    set extn {.v}
    if { $warning } {
      send_msg_id USF-[usf_aldec_getSimulatorName]-55 INFO "The target language is set to VHDL, it is not supported for simulation type '$properties(s_type)', using Verilog instead.\n"
    }
  }
  return $extn
}

proc usf_get_netlist_filename { } {
  # Summary:
  # Argument Usage:
  # Return Value:

  variable properties
  set filename $properties(s_sim_top)
  switch -regexp -- $properties(s_simulation_flow) {
    {behav_sim} { set filename [append filename "_behav"] }
    {post_synth_sim} -
    {post_impl_sim} {
      switch -regexp -- $properties(s_type) {
        {functional} { set filename [append filename "_func"] }
        {timing} {set filename [append filename "_time"] }
      }
    }
  }
  switch -regexp -- $properties(s_simulation_flow) {
    {post_synth_sim} { set filename [append filename "_synth"] }
    {post_impl_sim}  { set filename [append filename "_impl"] }
  }
  return $filename
}

proc usf_export_data_files { data_files } {
  # Summary: 
  # Argument Usage:
  # Return Value:

  variable properties
  variable l_target_simulator
  set export_dir $properties(launch_directory)
  if { [llength $data_files] > 0 } {
    set data_files [usf_remove_duplicate_files $data_files]
    foreach file $data_files {
      set extn [file extension $file]
      switch -- $extn {
        {.c} -
        {.zip} -
        {.hwh} -
        {.hwdef} -
        {.xml} {
          if { {} != [usf_get_top_ip_filename $file] } {
            continue
          }
        }
      }
      set target_file [file join $export_dir [file tail $file]]
      if { [get_param project.enableCentralSimRepo] } {
        set mem_init_dir [usf_file_normalize [file join $properties(dynamic_repo_dir) "mem_init_files"]]
        set data_file [extract_files -force -no_paths -files [list "$file"] -base_dir $mem_init_dir]
        if {[catch {file copy -force $data_file $export_dir} error_msg] } {
          send_msg_id USF-[usf_aldec_getSimulatorName]-56 WARNING "Failed to copy file '$data_file' to '$export_dir' : $error_msg\n"
        } else {
          send_msg_id USF-[usf_aldec_getSimulatorName]-57 INFO "Exported '$target_file'\n"
        }
      } else {
        set data_file [extract_files -force -no_paths -files [list "$file"] -base_dir $export_dir]
        send_msg_id USF-[usf_aldec_getSimulatorName]-58 INFO "Exported '$target_file'\n"
      }
    }
  }
}

proc usf_export_fs_data_files { filter } {
  # Summary: Copy fileset IP data files to output directory
  # Argument Usage:
  # Return Value:

  variable properties
  set fileset_object [get_filesets $::tclapp::aldec::common::helpers::properties(simset)]
  set data_files [list]
  # ip data files
  set ips [get_files -all -quiet -filter "FILE_TYPE == \"IP\""]
  foreach ip $ips {
    set ip_name [file tail $ip]
    foreach file [get_files -all -quiet -of_objects [get_files -quiet *$ip_name] -filter $filter] {
      lappend data_files $file
    }
  }
  set filesets [list]

  # block fileset data files
  lappend filesets [get_filesets -filter "FILESET_TYPE == \"BlockSrcs\""]

  # current source set fileset data files
  lappend filesets [current_fileset -srcset]

  # current simulation fileset data files
  lappend filesets [get_filesets $properties(simset)]

  # collect all fileset data files
  foreach fileset_object $filesets {
    foreach file [get_files -all -quiet -of_objects [get_filesets $fileset_object] -filter $filter] {
      lappend data_files $file
    }
  }
  usf_export_data_files $data_files
}

proc usf_export_fs_non_hdl_data_files {} {
  # Summary: Copy fileset IP data files to output directory
  # Argument Usage:
  # Return Value:

  variable properties
  variable s_non_hdl_data_files_filter

  set fileset_object [get_filesets $::tclapp::aldec::common::helpers::properties(simset)]
  set data_files [list]
  foreach file [get_files -all -quiet -of_objects [get_filesets $fileset_object] -filter $s_non_hdl_data_files_filter] {
    # skip user disabled (if the file supports is_user_disabled property
    if { [lsearch -exact [list_property $file] {IS_USER_DISABLED}] != -1 } {
      if { [get_property {IS_USER_DISABLED} $file] } {
        continue;
      }
    }
    lappend data_files $file
  }
  usf_export_data_files $data_files
}

proc usf_get_files_from_block_filesets { filter_type } {
  # Summary:
  # Argument Usage:
  # Return Value:

  set file_list [list]
  set filter "FILESET_TYPE == \"BlockSrcs\""
  set used_in_val "simulation"
  set fileset_objects [get_filesets -filter $filter]
  if { [llength $fileset_objects] > 0 } {
    send_msg_id USF-[usf_aldec_getSimulatorName]-59 INFO "Finding block fileset files..."
    foreach fileset_object $fileset_objects {
      set fs_name [get_property "NAME" $fileset_object]
      send_msg_id USF-[usf_aldec_getSimulatorName]-60 INFO "Inspecting fileset '$fs_name' for '$filter_type' files...\n"
      #set files [usf_remove_duplicate_files [get_files -quiet -compile_order sources -used_in $used_in_val -of_objects [get_filesets $fileset_object] -filter $filter_type]]
      set files [get_files -quiet -compile_order sources -used_in $used_in_val -of_objects [get_filesets $fileset_object] -filter $filter_type]
      if { [llength $files] == 0 } {
        send_msg_id USF-[usf_aldec_getSimulatorName]-61 INFO "No files found in '$fs_name'\n"
        continue
      } else {
        foreach file $files {
          lappend file_list $file
        }
      }
    }
  }
  return $file_list
}

proc usf_remove_duplicate_files { compile_order_files } {
  # Summary:
  # Argument Usage:
  # Return Value:

  set file_list [list]
  set compile_order [list]
  foreach file $compile_order_files {
    set normalized_file_path [usf_file_normalize [string map {\\ /} $file]]
    if { [lsearch -exact $file_list $normalized_file_path] == -1 } {
      lappend file_list $normalized_file_path
      lappend compile_order $file
    }
  }
  return $compile_order
}

proc usf_get_include_dirs { } {
  # Summary:
  # Argument Usage:
  # Return Value:

  variable properties
  set dir_names [list]
  set tcl_obj $properties(sp_tcl_obj)
  set incl_dirs [list]
  set incl_dir_str {}
  if { [usf_is_ip $tcl_obj] } {
    set incl_dir_str [usf_get_incl_dirs_from_ip $tcl_obj]
    set incl_dirs [split $incl_dir_str "|"]
  } else {
    #puts "DEBUG:" 
    #set temp [get_property "INCLUDE_DIRS" [get_filesets $tcl_obj]]
    #puts $temp
    set incl_dir_str [usf_resolve_incl_dir_property_value [get_property "INCLUDE_DIRS" [get_filesets $tcl_obj]]]
    set incl_prop_dirs [split $incl_dir_str "|"]

    # include dirs from design source set
    set linked_src_set [get_property "SOURCE_SET" [get_filesets $tcl_obj]]
    if { {} != $linked_src_set } {
      set src_fileset_object [get_filesets $linked_src_set]
      set dirs [usf_resolve_incl_dir_property_value [get_property "INCLUDE_DIRS" [get_filesets $src_fileset_object]]]
      foreach dir [split $dirs "|"] {
        if { [lsearch -exact $incl_prop_dirs $dir] == -1 } {
          lappend incl_prop_dirs $dir
        }
      }
    }

    foreach dir $incl_prop_dirs {
      if { $properties(use_absolute_paths) } {
        set dir "[usf_resolve_file_path $dir]"
      } else {
        set dir "[usf_aldec_get_origin_dir_path [usf_get_relative_file_path $dir $properties(launch_directory)]]"
      }
      lappend incl_dirs $dir
    }
  }
  foreach vh_dir $incl_dirs {
    set vh_dir [string trim $vh_dir {\{\}}]
    lappend dir_names $vh_dir
  }
  return [lsort -unique $dir_names]
}

proc usf_get_verilog_header_paths {} {
  # Summary:
  # Argument Usage:
  # Return Value:

  variable properties
  set simset_obj     [get_filesets $properties(simset)]
  set include_paths  [list]
  # 1. get paths for verilog header files (.vh, .h)
  usf_get_header_include_paths include_paths 
  # 2. add include dirs if any
  foreach dir [usf_get_include_dirs] {
    lappend include_paths $dir
  }
  # 3. uniquify paths (its quite possible that files marked with global include can be a "VERILOG_HEADER' as well collected in step 1)
  set final_unique_paths  [list]
  set incl_header_paths   [list]
  foreach path $include_paths {
    if { [lsearch -exact $final_unique_paths $path] == -1 } {
      lappend incl_header_paths $path
      lappend final_unique_paths $path
    }
  }
  return $incl_header_paths
}

proc usf_get_header_include_paths { incl_header_paths_arg } {
  # Summary:
  # Argument Usage:
  # Return Value:

  upvar $incl_header_paths_arg incl_header_paths
  variable properties
  set simset_obj     [get_filesets $properties(simset)]
  set unique_paths   [list]
  set linked_src_set [get_property "SOURCE_SET" $simset_obj]
  if { {} != $linked_src_set } {
    set srcset_obj [get_filesets $linked_src_set]
    if { {} != $srcset_obj } {
      usf_add_unique_incl_paths $srcset_obj unique_paths incl_header_paths
    }
  }
  usf_add_unique_incl_paths $simset_obj unique_paths incl_header_paths
  # add paths from block filesets
  set filter "FILESET_TYPE == \"BlockSrcs\""
  foreach blk_fileset_object [get_filesets -filter $filter] {
    set fs_name [get_property "NAME" [get_filesets $blk_fileset_object]]
    usf_add_unique_incl_paths $blk_fileset_object unique_paths incl_header_paths
  }
}

proc usf_add_unique_incl_paths { fileset_object unique_paths_arg incl_header_paths_arg } {
  # Summary:
  # Argument Usage:
  # Return Value:

  upvar $unique_paths_arg      unique_paths
  upvar $incl_header_paths_arg incl_header_paths
  variable properties
  set dir $properties(launch_directory)

  # setup the filter to include only header types enabled for simulation
  set filter "USED_IN_SIMULATION == 1 && (FILE_TYPE == \"Verilog Header\" || FILE_TYPE == \"Verilog/SystemVerilog Header\")"
  set vh_files [get_files -all -quiet -filter $filter]
  foreach vh_file $vh_files {
    set vh_file_obj [lindex [get_files -all -quiet [list "$vh_file"]] 0]
    if { [get_property "IS_GLOBAL_INCLUDE" $vh_file_obj] } {
      continue
    }
    # set vh_file [extract_files -files [list "$vh_file"] -base_dir $dir/ip_files]
    set vh_file [usf_xtract_file $vh_file]
    if { [get_param project.enableCentralSimRepo] } {
      set b_is_bd [usf_is_bd_file $vh_file ]
      set used_in_values [get_property "USED_IN" $vh_file_obj]
      if { [lsearch -exact $used_in_values "ipstatic"] == -1 } {
        set vh_file [usf_fetch_header_from_dynamic $vh_file $b_is_bd]
      } else {
        if { $b_is_bd } {
          set vh_file [usf_fetch_ipi_static_file $vh_file]
        } else {
          set vh_file_path [usf_fetch_ip_static_file $vh_file $vh_file_obj]
          if { $properties(b_use_static_lib) && [file exists $vh_file_path] || !$properties(b_use_static_lib) } {
            set vh_file $vh_file_path
          }
        }
      }
    }
    set file_path [usf_file_normalize [string map {\\ /} [file dirname $vh_file]]]
    if { [lsearch -exact $unique_paths $file_path] == -1 } {
      if { $properties(use_absolute_paths) } {
        set incl_file_path "[usf_resolve_file_path $file_path]"
      } else {
        set incl_file_path "[usf_aldec_get_origin_dir_path [usf_get_relative_file_path $file_path $dir]]"
      }
      lappend incl_header_paths $incl_file_path
      lappend unique_paths      $file_path
    }
  }
}

proc usf_get_global_include_files { incl_file_paths_arg incl_files_arg { ref_dir "true" } } {
  # Summary: find source files marked as global include
  # Argument Usage:
  # Return Value:

  upvar $incl_file_paths_arg incl_file_paths
  upvar $incl_files_arg      incl_files
  variable properties
  set filesets       [list]
  set dir            $properties(launch_directory)
  set simset_obj     [get_filesets $properties(simset)]
  set linked_src_set [get_property "SOURCE_SET" $simset_obj]
  set incl_files_set [list]

  if { {} != $linked_src_set } {
    lappend filesets $linked_src_set
  }
  lappend filesets $simset_obj
  set filter "FILE_TYPE == \"Verilog\" || FILE_TYPE == \"Verilog Header\" || FILE_TYPE == \"Verilog Template\""
  foreach fileset_object $filesets {
    set vh_files [get_files -quiet -all -of_objects [get_filesets $fileset_object] -filter $filter]
    foreach file $vh_files {
      # skip if not marked as global include
      if { ![get_property "IS_GLOBAL_INCLUDE" [lindex [get_files -quiet -all [list "$file"]] 0]] } {
        continue
      }

      # skip if marked user disabled
      if { [get_property "IS_USER_DISABLED" [lindex [get_files -quiet -all [list "$file"]] 0]] } {
        continue
      }

      set file [usf_xtract_file $file]
      set file [usf_file_normalize [string map {\\ /} $file]]
      if { [lsearch -exact $incl_files_set $file] == -1 } {
        lappend incl_files_set $file
        lappend incl_files     $file
        set incl_file_path [usf_file_normalize [string map {\\ /} [file dirname $file]]]
        if { $properties(use_absolute_paths) } {
          set incl_file_path "[usf_resolve_file_path $incl_file_path]"
        } else {
          if { $ref_dir } {
            set incl_file_path "[usf_aldec_get_origin_dir_path [usf_get_relative_file_path $incl_file_path $dir]]"
          }
        }
        lappend incl_file_paths $incl_file_path
      }
    }
  }
}

proc usf_get_incl_dirs_from_ip { tcl_obj } {
  # Summary:
  # Argument Usage:
  # Return Value:

  variable properties
  set launch_dir $properties(launch_directory)
  set ip_name [file tail $tcl_obj]
  set incl_dirs [list]
  set filter "FILE_TYPE == \"Verilog Header\""
  set vh_files [get_files -quiet -compile_order sources -used_in simulation -of_objects [get_files -quiet *$ip_name] -filter $filter]
  foreach file $vh_files {
    # set file [extract_files -files [list "$file"] -base_dir $launch_dir/ip_files]
    set file [usf_xtract_file $file]
    set dir [file dirname $file]
    if { [get_param project.enableCentralSimRepo] } {
      set b_static_ip_file 0
      set file_obj [lindex [get_files -quiet -all [list "$file"]] 0]
      set associated_library {}
      if { {} != $file_obj } {
        if { [lsearch -exact [list_property $file_obj] {LIBRARY}] != -1 } {
          set associated_library [get_property "LIBRARY" $file_obj]
        }
      }
      set file [usf_get_ip_file_from_repo $tcl_obj $file $associated_library $launch_dir b_static_ip_file]
      set dir [file dirname $file]
      # remove leading "./"
      if { [regexp {^\.\/} $dir] } {
        set dir [join [lrange [split $dir "/"] 1 end] "/"]
      }
    } else {
      if { $properties(use_absolute_paths) } {
        set dir "[usf_resolve_file_path $dir]"
      } else {
        set dir "[usf_aldec_get_origin_dir_path [usf_get_relative_file_path $dir $properties(launch_directory)]]"
      }
    }
    lappend incl_dirs $dir
  }
  set incl_dirs [join $incl_dirs "|"]
  return $incl_dirs
}

proc usf_file_normalize { _path } {
  # Summary: On Windows 'file normalize' changes a link path into a target path, which fools relative paths calculation. This procedure does simplified normalization for links on Windows.
  # Argument Usage:
  # Return Value:

  if { $::tcl_platform(platform) != {windows} } {
    return [file normalize $_path]
  }

  # check if it is a link
  set itIsLink 0
  set path {}

  foreach element [file split $_path] {
    set path [file join $path $element]
    if { [catch { file link $path }] } {
      continue
    }
    set itIsLink 1
    break
  }

  if { !$itIsLink } {
    return [file normalize $_path]
  }

  # do simplified normalization
  set elements {}
  foreach element [file split $_path] {
    if { $element == "." } {
      continue
    } elseif { $element == ".." } {
      set elements [lreplace $elements end end]
    } else {
      lappend elements $element
    }
  }

  set path {}
  foreach element $elements {
    set path [file join $path $element]
  }

  return $path
}

proc usf_get_relative_file_path { file_path_to_convert relative_to } {
  # Summary: Get the relative path wrt to path specified
  # Argument Usage:
  # file_path_to_convert: input file to make relative to specfied path
  # Return Value:
  # Relative path wrt the path specified

  # make sure we are dealing with a valid relative_to directory. If regular file or is not a directory, get directory
  if { [file isfile $relative_to] || ![file isdirectory $relative_to] } {
    set relative_to [file dirname $relative_to]
  }

  set cwd [usf_file_normalize [pwd]]
  # is relative_to "relative"? convert to absolute as well wrt cwd
  if { [file pathtype $relative_to] eq "relative" } {
    set relative_to [file join $cwd $relative_to]
  }

  # normalize
  set file_path         [usf_file_normalize $file_path_to_convert]
  set relative_to       [usf_file_normalize $relative_to]
  set file_comps        [file split $file_path]
  set relative_to_comps [file split $relative_to]
  set found_match       false
  set index             0
  set fc_comps_len      [llength $file_comps]
  set rt_comps_len      [llength $relative_to_comps]
  # compare each dir element of file_to_convert and relative_to, set the flag and
  # get the final index till these sub-dirs matched
  while { $::tcl_platform(platform) == {windows} && [string equal -nocase [lindex $file_comps $index] [lindex $relative_to_comps $index]] 
    || $::tcl_platform(platform) != {windows} && [string equal [lindex $file_comps $index] [lindex $relative_to_comps $index]] } {
    set found_match true
    incr index
    if { ($index == $fc_comps_len) || ($index == $rt_comps_len) } {
      break;
    }
  }

  # any common dirs found? convert path to relative
  if { $found_match } {
    set parent_dir_path ""
    set rel_index $index
    # keep traversing the relative_to dirs and build "../" levels
    while { [lindex $relative_to_comps $rel_index] != "" } {
      set parent_dir_path "../$parent_dir_path"
      incr rel_index
    }
    # at this point we have parent_dir_path setup with exact number of sub-dirs to go up
    #
    # now build up part of path which is relative to matched part
    set rel_path ""
    set rel_index $index
    while { [lindex $file_comps $rel_index] != "" } {
      set comps [lindex $file_comps $rel_index]
      if { $rel_path == "" } {
        # first dir
        set rel_path $comps
      } else {
        # append remaining dirs
        set rel_path "${rel_path}/$comps"
      }
      incr rel_index
    }
    # prepend parent dirs, this is the complete resolved path now
    set resolved_path "${parent_dir_path}${rel_path}"
    return $resolved_path
  }
  # no common dirs found, just return the normalized path
  return $file_path
}

proc usf_resolve_file_path { file_dir_path_to_convert } {
  # Summary: Make file path relative to origin_dir if relative component found
  # Argument Usage:
  # file_dir_path_to_convert: input file to make relative to specfied path
  # Return Value:
  # Relative path wrt the path specified

  variable properties
  set ref_dir [usf_file_normalize [string map {\\ /} $properties(launch_directory)]]
  set ref_comps [lrange [split $ref_dir "/"] 1 end]
  set file_comps [lrange [split [usf_file_normalize [string map {\\ /} $file_dir_path_to_convert]] "/"] 1 end]
  set index 1
  while { [lindex $ref_comps $index] == [lindex $file_comps $index] } {
    incr index
  }
  # is file path within reference dir? return relative path
  if { $index == [llength $ref_comps] } {
    return [usf_get_relative_file_path $file_dir_path_to_convert $ref_dir]
  }
  # return absolute
  return $file_dir_path_to_convert
}

proc usf_is_ip { tcl_obj } {
  # Summary:
  # Argument Usage:
  # Return Value:

  variable l_valid_ip_extns
  # check if ip file extension
  if { [lsearch -exact $l_valid_ip_extns [file extension $tcl_obj]] >= 0 } {
    return 1
  } else {
    # check if IP object
    if {[regexp -nocase {^ip} [get_property [rdi::get_attr_specs CLASS -object $tcl_obj] $tcl_obj]] } {
      return 1
    }
  }
  return 0
}

proc usf_is_embedded_flow {} {
  # Summary:
  # Argument Usage:
  # Return Value:

  variable s_embedded_files_filter
  set embedded_files [get_files -all -quiet -filter $s_embedded_files_filter]
  if { [llength $embedded_files] > 0 } {
    return 1
  }
  return 0
}

proc usf_get_compiler_name { file_type } {
  # Summary:
  # Argument Usage:
  # Return Value:

  variable properties
  set compiler ""
  if { ({VHDL} == $file_type) || ({VHDL 2008} == $file_type) } {
    set compiler "vcom"
  } elseif { ({Verilog} == $file_type) || ({SystemVerilog} == $file_type) || ({Verilog Header} == $file_type) } {
    set compiler "vlog"
  }
  return $compiler
}

proc usf_aldec_append_compiler_options { tool file_type opts_arg } {
  # Summary:
  # Argument Usage:
  # Return Value:

  upvar $opts_arg opts
  variable properties
  set fileset_object [get_filesets $properties(simset)]
  switch $tool {
    "vcom" {
      lappend opts "\{*\}\$${tool}_opts"
    }
    "vlog" {
      lappend opts "\{*\}\$${tool}_opts"
    }
  }
}

proc usf_aldec_getPropertyName { property } {
  # Summary:
  # Argument Usage:
  # Return Value:
  
  switch -- [get_property target_simulator [current_project]] {
    Riviera { return "RIVIERA.$property" }
    ActiveHDL { return "ACTIVEHDL.$property" }
  }
}

proc usf_aldec_get_compiler_standard_by_file_type { file_type } {
  # Summary:
  # Argument Usage:
  # Return Value:
  
  set fileset_object [get_filesets $::tclapp::aldec::common::helpers::properties(simset)]
  
  switch -nocase -regexp -- $file_type {
    "^VHDL$" {
      switch -- [get_property [usf_aldec_getPropertyName COMPILE.VHDL_SYNTAX] $fileset_object] {
        93 { return "-93" }
        2002 { return "-2002" }
        2008 { return "-2008" }
      }
    }
    "^VHDL 2008$" {
      return "-2008"
    }
    "^Verilog|Verilog Header$" {
      switch -- [get_property [usf_aldec_getPropertyName COMPILE.VLOG_SYNTAX] $fileset_object] {
        1995 { return "-v95" }
        2001 { return "-v2k" }
        2005 { return "-v2k5" }
      }
    }
    "^SystemVerilog$" {
      switch -- [get_property [usf_aldec_getPropertyName COMPILE.SV_SYNTAX] $fileset_object] {
        2005 { return "-sv2k5" }
        2009 { return "-sv2k9" }
        2012 {
          switch -- [get_property target_simulator [current_project]] {
            Riviera { return "" }
            ActiveHDL { return "-sv2k12" }
          }
        }
      }
    }
  }
}

proc usf_append_other_options { tool file_type global_files_str opts_arg } {
  # Summary:
  # Argument Usage:
  # Return Value:

  upvar $opts_arg opts
  variable properties
  set fileset_object [get_filesets $properties(simset)]
  switch $tool {
    "vlog" {
      # verilog defines
      set verilog_defines [list]
      set verilog_defines [get_property "VERILOG_DEFINE" [get_filesets $fileset_object]]
      if { [llength $verilog_defines] > 0 } {
        usf_append_define_generics $verilog_defines $tool opts
      }
    }
  }
}

proc usf_make_file_executable { file } {
  # Summary:
  # Argument Usage:
  # Return Value:

  if {$::tcl_platform(platform) == "unix"} {
    if {[catch {exec chmod a+x $file} error_msg] } {
      send_msg_id USF-[usf_aldec_getSimulatorName]-62 WARNING "Failed to change file permissions to executable ($file): $error_msg\n"
    }
  } else {
    if {[catch {exec attrib /D -R $file} error_msg] } {
      send_msg_id USF-[usf_aldec_getSimulatorName]-63 WARNING "Failed to change file permissions to executable ($file): $error_msg\n"
    }
  }
}

proc usf_generate_comp_file_for_simulation { comp_file runs_to_launch_arg } {
  # Summary:
  # Argument Usage:
  # Return Value:

  upvar $runs_to_launch_arg runs_to_launch
  set ts [get_property "SIMULATOR_LANGUAGE" [current_project]]
  set ip_filename [file tail $comp_file]
  set ip_name     [file root $ip_filename]
  # - does the ip support behavioral language?,
  #   -- if yes, then generate simulation products (if not generated by ip earlier)
  #   -- if not, then does ip synth checkpoint set?,
  #       --- if yes, then generate all products (if not generated by ip earlier)
  #       --- if not, then error with recommendation
  # and also generate the IP netlist
  if { [get_property "IS_IP_BEHAV_LANG_SUPPORTED" $comp_file] } {
    # does ip generated simulation products? if not, generate them
    if { ![get_property "IS_IP_GENERATED_SIM" $comp_file] } {
      send_msg_id USF-[usf_aldec_getSimulatorName]-64 INFO "Generating simulation products for IP '$ip_name'...\n"
      set delivered_targets [get_property delivered_targets [get_ips -quiet ${ip_name}]]
      if { [regexp -nocase {simulation} $delivered_targets] } {
        generate_target {simulation} [get_files [list "$comp_file"]] -force
      }
    } else {
      send_msg_id USF-[usf_aldec_getSimulatorName]-65 INFO "IP '$ip_name' is up-to-date for simulation\n"
    }
  } elseif { [get_property "GENERATE_SYNTH_CHECKPOINT" $comp_file] } {
    # make sure ip is up-to-date
    if { ![get_property "IS_IP_GENERATED" $comp_file] } {
      generate_target {all} [get_files [list "$comp_file"]] -force
      send_msg_id USF-[usf_aldec_getSimulatorName]-66 INFO "Generating functional netlist for IP '$ip_name'...\n"
      usf_generate_ip_netlist $comp_file runs_to_launch
    } else {
      send_msg_id USF-[usf_aldec_getSimulatorName]-67 INFO "IP '$ip_name' is upto date for all products\n"
    }
  } else {
    # at this point, ip doesnot support behavioral language and synth check point is false, so advise
    # users to select synthesized checkpoint option or set the "generate_synth_checkpoint' ip property.
    set simulator_lang [get_property "SIMULATOR_LANGUAGE" [current_project]]
    set error_msg "IP contains simulation files that do not support the current Simulation Language: '$simulator_lang'.\n"
    if { [get_property "IS_IP_SYNTH_TARGET_SUPPORTED" $comp_file] } {
      append error_msg "Resolution:-\n"
      append error_msg "1)\n"
      append error_msg "or\n2) Select the option Generate Synthesized Checkpoint (.dcp) in the Generate Output Products dialog\
                        to automatically create a matching simulation netlist, or set the 'GENERATE_SYNTH_CHECKPOINT' property on the core."
    } else {
      # no synthesis, so no recommendation to do a synth checkpoint.
    }
    send_msg_id USF-[usf_aldec_getSimulatorName]-68 WARNING "$error_msg\n"
    #return 1
  }
}

proc usf_generate_ip_netlist { comp_file runs_to_launch_arg } {
  # Summary:
  # Argument Usage:
  # Return Value:

  upvar $runs_to_launch_arg runs_to_launch
  set comp_file_obj [get_files [list "$comp_file"]]
  set comp_file_fs  [get_property "FILESET_NAME" $comp_file_obj]
  if { ![get_property "GENERATE_SYNTH_CHECKPOINT" $comp_file_obj] } {
    send_msg_id USF-[usf_aldec_getSimulatorName]-69 INFO "Generate synth checkpoint is 'false':$comp_file\n"
    # if synth checkpoint read-only, return
    if { [get_property "IS_IP_SYNTH_CHECKPOINT_READONLY" $comp_file_obj] } {
      send_msg_id USF-[usf_aldec_getSimulatorName]-70 WARNING "Synth checkpoint property is 'readonly' ... skipping:$comp_file\n"
      return
    }
    # set property to create a DCP/structural simulation file
    send_msg_id USF-[usf_aldec_getSimulatorName]-71 INFO "Setting synth checkpoint for generating simulation netlist:$comp_file\n"
    set_property "GENERATE_SYNTH_CHECKPOINT" true $comp_file_obj
  } else {
    send_msg_id USF-[usf_aldec_getSimulatorName]-72 INFO "Generate synth checkpoint is set:$comp_file\n"
  }
  # block fileset name is based on the basename of the IP
  set src_file [usf_file_normalize $comp_file]
  set ip_basename [file root [file tail $src_file]]
  # block-fileset may not be created at this point, so quiet if not found
  set block_fileset_object [get_filesets -quiet $ip_basename]
  # "block fileset" exists? if not create it
  if { {} == $block_fileset_object } {
    create_fileset -blockset "$ip_basename"
    set block_fileset_object [get_filesets $ip_basename]
    send_msg_id USF-[usf_aldec_getSimulatorName]-73 INFO "Block-fileset created:$block_fileset_object"
    # set fileset top
    set comp_file_top [get_property "IP_TOP" $comp_file_obj]
    set_property "TOP" $comp_file_top [get_filesets $ip_basename]
    # move sub-design to block-fileset
    send_msg_id USF-[usf_aldec_getSimulatorName]-74 INFO "Moving ip composite source(s) to '$ip_basename' fileset"
    move_files -fileset [get_filesets $ip_basename] [get_files -of_objects [get_filesets $comp_file_fs] $src_file] 
  }
  if { {BlockSrcs} != [get_property "FILESET_TYPE" $block_fileset_object] } {
    send_msg_id USF-[usf_aldec_getSimulatorName]-75 ERROR "Given source file is not associated with a design source fileset.\n"
    return 1
  }
  # construct block-fileset run for the netlist
  set run_name $ip_basename;append run_name "_synth_1"
  if { ![get_property "IS_INITIALIZED" [get_runs $run_name]] } {
    reset_run $run_name
  }
  lappend runs_to_launch $run_name
  send_msg_id USF-[usf_aldec_getSimulatorName]-76 INFO "Run scheduled for '$ip_basename':$run_name\n"
}

proc usf_get_testbench_files_from_ip { file_type_filter } {
  # Summary:
  # Argument Usage:
  # Return Value:

  set tb_filelist [list]
  set ip_filter "FILE_TYPE == \"IP\""
  foreach ip [get_files -all -quiet -filter $ip_filter] {
    set ip_name [file tail $ip]
    set tb_files [get_files -all -quiet -of_objects [get_files -quiet *$ip_name] -filter $file_type_filter]
    if { [llength $tb_files] > 0 } {
      foreach tb $tb_files {
        set tb_file_obj [lindex [get_files -all -quiet -of_objects [get_files -quiet *$ip_name] $tb] 0]
        if { {simulation testbench} == [get_property "USED_IN" $tb_file_obj] } {
          if { [lsearch -exact [list_property $tb_file_obj] {IS_USER_DISABLED}] != -1 } {
            if { [get_property {IS_USER_DISABLED} $tb_file_obj] } {
              continue;
            }
          }
          lappend tb_filelist $tb
        }
      }
    }
  }
  return $tb_filelist
}

proc usf_get_bin_path { tool_name path_sep } {
  # Summary:
  # Argument Usage:
  # Return Value:

  set path_value $::env(PATH)
  set bin_path {}
  foreach path [split $path_value $path_sep] {
    set exe_file [usf_file_normalize [file join $path $tool_name]]
    if { [file exists $exe_file] } {
      set bin_path $path
      break
    }
  }
  return $bin_path
}

proc usf_get_global_include_file_cmdstr { incl_files_arg } {
  # Summary:
  # Argument Usage:
  # Return Value:

  upvar $incl_files_arg incl_files
  variable properties
  set file_str [list]
  foreach file $incl_files {
    lappend file_str "\"$file\""
  }
  return [join $file_str " "]
}

proc findAllDesignFiles { } {
	variable allDesignFiles

	array unset allDesignFiles

	foreach fileObject [ get_files -quiet -all ] {
		set name [ get_property -quiet name $fileObject ]
		set allDesignFiles($name) $fileObject
	}
}

proc findCompileOrderFilesUniq { } {
	variable l_compile_order_files
	variable compileOrderFilesUniq

	set compileOrderFilesUniq [usf_uniquify_cmd_str $l_compile_order_files]
}

proc getFileCmdStr { file _fileType _xpm _globalFilesStr _includeDirsOptions { _xpmLibrary {} } { _xvLibrary {} } } {
    variable properties
	variable allDesignFiles

	upvar $_includeDirsOptions includeDirsOptions

	set launchDirectory $properties(launch_directory)
	set associatedLibrary $properties(associatedLibrary)
	set commandStr {}
	set fileObject {}

	if { [ info exists allDesignFiles($file) ] } {
		set fileObject $allDesignFiles($file)
	} else {
		set fileObject [ lindex [ get_files -quiet -all [ list "$file" ] ] 0 ]
	}

	if { {} != $fileObject } {
		if { [ lsearch -exact [ list_property -quiet $fileObject ] {LIBRARY} ] != -1 } {
			set associatedLibrary [ get_property "LIBRARY" $fileObject ]
		}
		
		if { [ get_param "project.enableCentralSimRepo" ] } {
			# # no op
		} else {
			if { $properties(b_extract_ip_sim_files) } {
				set ipPath [ get_property core_container $fileObject ]
				if { {} != $ipPath } {
					set ipName [ file root [ file tail $ipPath ] ]
					set ipDir [ get_property ip_extract_dir [ get_ips -all -quiet $ipName ] ]
					set ipFile "[xcs_get_relative_file_path $file $ipDir]"
					set ipFile [join [lrange [split $ipFile "/"] 1 end] "/"]
					set file [ file join $ipDir $ipFile ]
				} else {
				}
			}
		}
	} else {
		if { ($_xpm) && ([ string length $_xpmLibrary ] != 0)} {
			set associatedLibrary $_xpmLibrary
		}
	}

	if { {} != $_xvLibrary } {
		set associatedLibrary $_xvLibrary
	}

	set ipFile [ usf_get_top_ip_filename $file ]
	set b_static_ip_file 0
	set file [ usf_get_ip_file_from_repo $ipFile $file $associatedLibrary $launchDirectory b_static_ip_file ]

	if { [get_param "project.writeNativeScriptForUnifiedSimulation"] } {
	} else {
		regsub -all { } $file {\\\\ } file
	}

	set compiler [ usf_get_compiler_name $_fileType ]
	set argList [list]
	if { [ string length $compiler] > 0 } {
		lappend argList $compiler
		lappend argList [ usf_aldec_get_compiler_standard_by_file_type $_fileType ]
		usf_aldec_append_compiler_options $compiler $_fileType argList
		set argList [ linsert $argList end "-work $associatedLibrary" "$_globalFilesStr" ]
	}
	usf_append_other_options $compiler $_fileType $_globalFilesStr argList
  
	if { {vlog} == $compiler } {
		set argList [concat $argList $includeDirsOptions]
	}

	set file_str [join $argList " "]
	set type [usf_get_file_type_category $_fileType]
	set commandStr "$type|$_fileType|$associatedLibrary|$file_str|\"$file\"|$b_static_ip_file"
	return $commandStr
}

proc usf_get_file_cmd_str { file file_type global_files_str include_directories_options_arg } {
	variable properties
	variable allDesignFiles

	upvar $include_directories_options_arg include_directories_options

	set use_absolute_paths $properties(use_absolute_paths)
	set cmd_str {}
	set associated_library $properties(associatedLibrary)

  	if { [ info exists allDesignFiles($file) ] } {
		set file_obj $allDesignFiles($file)
	} else {
		set file_obj [ lindex [ get_files -quiet -all [ list "$file" ] ] 0 ]
	}

	if { {} != $file_obj } {
		if { [lsearch -exact [ list_property $file_obj ] {LIBRARY}] != -1 } {
			set associated_library [get_property "LIBRARY" $file_obj]
		}

		if { [ get_param "project.enableCentralSimRepo" ] } {
			# no op
		} else {
			if { $properties(b_extract_ip_sim_files) } {
				set xcix_ip_path [ get_property core_container $file_obj ]
				if { {} != $xcix_ip_path } {
					set ip_name [ file root [ file tail $xcix_ip_path ] ]
					set ip_ext_dir [ get_property ip_extract_dir [ get_ips -all -quiet $ip_name ] ]
					set ip_file "[usf_get_relative_file_path $file $ip_ext_dir]"

					set ip_file [ join [lrange [ split $ip_file "/" ] 1 end] "/" ]
					set file [ file join $ip_ext_dir $ip_file ]
				} else {
					# set file [extract_files -files [list "$file"] -base_dir $dir/ip_files]
				}
			}
		}
	}

	set b_static_ip_file 0
	set ip_file [ usf_get_top_ip_filename $file ]
	set file [usf_get_ip_file_from_repo $ip_file $file $associated_library $properties(launch_directory) b_static_ip_file]

	set compiler [usf_get_compiler_name $file_type]
	set arg_list [ list ]
	if { [string length $compiler] > 0 } {
		lappend arg_list $compiler
		lappend arg_list [ usf_aldec_get_compiler_standard_by_file_type $file_type ]
		usf_aldec_append_compiler_options $compiler $file_type arg_list
		set arg_list [linsert $arg_list end "-work $associated_library" "$global_files_str"]
	}
	usf_append_other_options $compiler $file_type $global_files_str arg_list

	if { {vlog} == $compiler } {
		set arg_list [concat $arg_list $include_directories_options]
	}

	set file_str [join $arg_list " "]
	set type [usf_get_file_type_category $file_type]
	set cmd_str "$type|$file_type|$associated_library|$file_str|$file|$b_static_ip_file"
	return $cmd_str
}

proc usf_get_top_ip_filename { src_file } {
	variable allDesignFiles

	set top_ip_file {}

   	if { [ info exists allDesignFiles($src_file) ] } {
		set file_obj $allDesignFiles($src_file)
	} else {
		set file_obj [ lindex [ get_files -quiet -all [ list "$src_file" ] ] 0 ]
	}

	if { {} == $file_obj } {
		set file_obj [ lindex [ get_files -all -quiet [ file tail $src_file ] ] 0 ]
	}

	if { {} == $file_obj } {
		return $top_ip_file
	}

	set props [ list_property -quiet $file_obj ]
	if { [ lsearch $props "PARENT_COMPOSITE_FILE" ] != -1 } {
		set top_ip_file [ usf_find_top_level_ip_file $src_file ]
	}

	return $top_ip_file
}

proc usf_get_file_type_category { file_type } {
  # Summary:
  # Argument Usage:
  # Return Value:

  set type {UNKNOWN}
  switch $file_type {
    {VHDL} -
    {VHDL 2008} {
      set type {VHDL}
    }
    {Verilog} -
    {SystemVerilog} -
    {Verilog Header} {
      set type {VERILOG}
    }
  }
  return $type
}

proc usf_get_netlist_writer_cmd_args { extn } {
  # Summary:
  # Argument Usage:
  # Return Value:

  variable properties
  set fileset_object                 [get_filesets $properties(simset)]
  set nl_cell                [get_property "NL.CELL" $fileset_object]
  set nl_incl_unisim_models  [get_property "NL.INCL_UNISIM_MODELS" $fileset_object]
  set nl_rename_top          [get_property "NL.RENAME_TOP" $fileset_object]
  set nl_sdf_anno            [get_property "NL.SDF_ANNO" $fileset_object]
  set nl_write_all_overrides [get_property "NL.WRITE_ALL_OVERRIDES" $fileset_object]
  set args                   [list]

  if { {} != $nl_cell }          { lappend args "-cell";lappend args $nl_cell }
  if { $nl_write_all_overrides } { lappend args "-write_all_overrides" }

  if { {} != $nl_rename_top } {
    if { {.v} == $extn } {
      lappend args "-rename_top";lappend args $nl_rename_top
    } elseif { {.vhd} == $extn } {
      lappend args "-rename_top";lappend args $nl_rename_top
    }
  }

  if { ({timing} == $properties(s_type)) } {
    if { $nl_sdf_anno } {
      lappend args "-sdf_anno true"
    } else {
      lappend args "-sdf_anno false"
    }
  }

  if { $nl_incl_unisim_models } { lappend args "-include_unisim" }
  lappend args "-force"
  set cmd_args [join $args " "]
  return $cmd_args
}

proc usf_get_sdf_writer_cmd_args { } {
  # Summary:
  # Argument Usage:
  # Return Value:

  variable properties
  set fileset_object            [get_filesets $properties(simset)]
  set nl_cell           [get_property "NL.CELL" $fileset_object]
  set nl_rename_top     [get_property "NL.RENAME_TOP" $fileset_object]
  set nl_process_corner [get_property "NL.PROCESS_CORNER" $fileset_object]
  set args              [list]

  if { {} != $nl_cell } {lappend args "-cell";lappend args $nl_cell}
  lappend args "-process_corner";lappend args $nl_process_corner
  if { {} != $nl_rename_top } {lappend "-rename_top_module";lappend args $nl_rename_top}
  lappend args "-force"
  set cmd_args [join $args " "]
  return $cmd_args
}

proc usf_aldec_check_errors { step results_log_arg } {
  # Summary:
  # Argument Usage:
  # Return Value:

  upvar $results_log_arg results_log
  
  variable properties
  set run_dir $properties(launch_directory)

  set retval 0
  set log [usf_file_normalize [file join $run_dir ${step}.log]]
  set fh 0
  if {[catch {open $log r} fh]} {
    send_msg_id USF-[usf_aldec_getSimulatorName]-77 WARNING "Failed to open file to read ($log)\n"
  } else {
    set log_data [read $fh]
    close $fh
    set log_data [split $log_data "\n"]
    foreach line $log_data {
      if { [regexp -nocase {ONERROR} $line]} {
        set results_log $log
        set retval 1
        break
      }
      if { [regexp -nocase {([0-9]+)\s+Errors} $line tmp errorsCount] && $errorsCount } {
        set results_log $log
        set retval 1
        break
      }
      if { [regexp -nocase {^Error:.+$} $line] } {
        set results_log $log
        set retval 1
        break
      }      
    }
  }
  if { $retval } {
    [catch {send_msg_id USF-[usf_aldec_getSimulatorName]-78 INFO "Step results log file:'$log'\n"}]
    return 1
  }
  return 0
}

proc usf_resolve_incl_dir_property_value { incl_dirs } {
  # Summary:
  # Argument Usage:
  # Return Value:

  set resolved_path {}
  set incl_dirs [string map {\\ /} $incl_dirs]
  set path_elem {} 
  set comps [split $incl_dirs { }]
  foreach elem $comps {
    # path element starts slash (/)? or drive (c:/)?
    if { [string match "/*" $elem] || [regexp {^[a-zA-Z]:} $elem] } {
      if { {} != $path_elem } {
        # previous path is complete now, add hash and append to resolved path string
        set path_elem "$path_elem|"
        append resolved_path $path_elem
      }
      # setup new path
      set path_elem "$elem"
    } else {
      # sub-dir with space, append to current path
      set path_elem "$path_elem $elem"
    }
  }
  append resolved_path $path_elem

  return $resolved_path
}

proc usf_find_files { src_files_arg filter } {
  # Summary:
  # Argument Usage:
  # Return Value:

  variable properties
  upvar $src_files_arg src_files

  set tcl_obj $properties(sp_tcl_obj)
  if { [usf_is_ip $tcl_obj] } {
    set ip_name [file tail $tcl_obj]
    foreach file [get_files -all -quiet -of_objects [get_files -quiet *$ip_name] -filter $filter] {
      if { [lsearch -exact [list_property $file] {IS_USER_DISABLED}] != -1 } {
        if { [get_property {IS_USER_DISABLED} $file] } {
          continue;
        }
      }
      set file [usf_file_normalize $file]
      if { $properties(use_absolute_paths) } {
        set file "[usf_resolve_file_path $file]"
      } else {
        set file "[usf_get_relative_file_path $file $properties(launch_directory)]"
      }
      lappend src_files $file
    }
  } elseif { [usf_is_fileset $tcl_obj] } {
    set filesets       [list]
    set simset_obj     [get_filesets $properties(simset)]

    lappend filesets $simset_obj
    set linked_src_set [get_property "SOURCE_SET" $simset_obj]
    if { {} != $linked_src_set } {
      lappend filesets $linked_src_set
    }

    # add block filesets
    set blk_filter "FILESET_TYPE == \"BlockSrcs\""
    foreach blk_fileset_object [get_filesets -filter $blk_filter] {
      lappend filesets $blk_fileset_object
    }

    foreach fileset_object $filesets {
      foreach file [get_files -quiet -of_objects [get_filesets $fileset_object] -filter $filter] {
        if { [lsearch -exact [list_property $file] {IS_USER_DISABLED}] != -1 } {
          if { [get_property {IS_USER_DISABLED} $file] } {
            continue;
          }
        }
        set file [usf_file_normalize $file]
        if { $properties(use_absolute_paths) } {
          set file "[usf_resolve_file_path $file]"
        } else {
          set file "[usf_get_relative_file_path $file $properties(launch_directory)]"
        }
        lappend src_files $file
      }
    }
  }
}

proc usf_xtract_file { file } {
  # Summary:
  # Argument Usage:
  # Return Value:

  if { [get_param "project.enableCentralSimRepo"] } {
    return $file
  }

  variable properties
  if { $properties(b_extract_ip_sim_files) } {
    set file_obj [lindex [get_files -quiet -all [list "$file"]] 0]
    set xcix_ip_path [get_property core_container $file_obj]
    if { {} != $xcix_ip_path } {
      set ip_name [file root [file tail $xcix_ip_path]]
      set ip_ext_dir [get_property ip_extract_dir [get_ips -all -quiet $ip_name]]
      set ip_file "[usf_get_relative_file_path $file $ip_ext_dir]"
      # remove leading "../"
      set ip_file [join [lrange [split $ip_file "/"] 1 end] "/"]
      set file [file join $ip_ext_dir $ip_file]
    }
  }
  return $file
}

proc usf_get_ip_file_from_repo { ip_file src_file library launch_dir is_static_ip_file_arg } {

	variable properties
	variable l_ip_static_libs
	upvar $is_static_ip_file_arg is_static_ip_file

	if { (![ get_param project.enableCentralSimRepo] ) || ({} == $ip_file) } {
		if { $properties(use_absolute_paths) } {
			set src_file "[usf_resolve_file_path $src_file]"
		} else {
			if { [file pathtype $src_file] == "absolute" } {
				set src_file "[usf_get_relative_file_path $src_file $launch_dir]"
			}
			set src_file "[usf_aldec_get_origin_dir_path $src_file]"
		}

		return $src_file
	}

	if { ({} != $properties(dynamic_repo_dir)) && ([ file exist $properties(dynamic_repo_dir) ]) } {
		set b_is_static 0
		set b_is_dynamic 0
		set src_file [ usf_get_source_from_repo $ip_file $src_file $launch_dir b_is_static b_is_dynamic ]
		set is_static_ip_file $b_is_static
		if { (!$b_is_static) && (!$b_is_dynamic) } {
			# send_msg_id USF-[usf_aldec_getSimulatorName]-79 "CRITICAL WARNING" "IP file is neither static or dynamic:'$src_file'\n"
		}

		if { $b_is_static } {
			set is_static_ip_file 1
			lappend l_ip_static_libs [string tolower $library]
		}
	}

	if { $properties(use_absolute_paths) } {
		set src_file "[usf_resolve_file_path $src_file]"
	} else {
		if { [file pathtype $src_file] == "absolute" } {
			set src_file "[usf_get_relative_file_path $src_file $launch_dir]"
		}
		set src_file "[usf_aldec_get_origin_dir_path $src_file]"
	}

  return $src_file
}

proc usf_get_source_from_repo { ip_file orig_src_file launch_dir b_is_static_arg b_is_dynamic_arg } {
	variable a_sim_cache_all_bd_files
	variable properties
	variable allDesignFiles
	variable compiledLibraries
	variable localDesignLibraries

	upvar $b_is_static_arg b_is_static
	upvar $b_is_dynamic_arg b_is_dynamic

	set src_file $orig_src_file

	set b_wrap_in_quotes 0
	if { [ regexp {\"} $src_file ] } {
		set b_wrap_in_quotes 1
		regsub -all {\"} $src_file {} src_file
	}

	set b_add_ref 0 
	if { [ regexp -nocase {^\$ref_dir} $src_file ] } {
		set b_add_ref 1
		set src_file [ string range $src_file 9 end ]
		set src_file "$src_file"
	}

	set filename [ file tail $src_file ]

	set ip_name [ file root [ file tail $ip_file ] ] 

	set full_src_file_path [ usf_find_file_from_compile_order $ip_name $src_file ]

	set full_src_file_obj {}
	if { [ info exists allDesignFiles($full_src_file_path) ] } {
		set full_src_file_obj $allDesignFiles($full_src_file_path)
	} else {
		set full_src_file_obj [ lindex [ get_files -all [ list "$full_src_file_path" ] ] 0 ]
	}
    if { {} == $full_src_file_obj } {
		return $orig_src_file
	}

	set dst_cip_file $full_src_file_path
	set used_in_values [ get_property "USED_IN" $full_src_file_obj ]
    set library [ get_property "LIBRARY" $full_src_file_obj ]
	set b_file_is_static 0

	if { [ lsearch -exact $used_in_values "ipstatic" ] == -1 } {
		set b_found_in_repo 0

		if { [ usf_is_core_container $ip_file $ip_name ] } {
			set dst_cip_file [ usf_get_dynamic_sim_file_core_container $full_src_file_path ]
		} else {
			set dst_cip_file [ usf_get_dynamic_sim_file_core_classic $full_src_file_path ]
		}
	} else {
		set b_file_is_static 1
	}

	set b_is_dynamic 1
	set b_is_bd_ip 0
    if { [ info exists a_sim_cache_all_bd_files($full_src_file_path) ] } {
		set b_is_bd_ip 1
	} else {
		set b_is_bd_ip [usf_is_bd_file $full_src_file_path ]

		if { $b_is_bd_ip } {
			set a_sim_cache_all_bd_files($full_src_file_path) $b_is_bd_ip
		}
	}

    set ip_static_file {}
	if { $b_file_is_static } {
		set ip_static_file $full_src_file_path
	}
  
	if { {} != $ip_static_file } {
		set b_is_static 0
		set b_is_dynamic 0
		set dst_cip_file $ip_static_file

		set b_process_file 1
		if { $properties(b_use_static_lib) } {
			if { [ lsearch -exact $compiledLibraries $library ] != -1 } {
				set b_process_file 0
				set b_is_static 1
			} else {
				if { [lsearch -exact $localDesignLibraries $library] == -1 } {
					lappend localDesignLibraries $library
				}
			}
		}
	
		if { $b_process_file } {
			if { $b_is_bd_ip } {
				set dst_cip_file [ usf_fetch_ipi_static_file $ip_static_file ]
			} else {
				set parent_comp_file [get_property parent_composite_file -quiet $full_src_file_obj ]
				set dst_cip_file [usf_find_ipstatic_file_path $ip_static_file $parent_comp_file]

				# skip if file exists
				if { ({} == $dst_cip_file) || (![ file exists $dst_cip_file ]) } {
					# if parent composite file is empty, extract to default ipstatic dir (the extracted path is expected to be
					# correct in this case starting from the library name (e.g fifo_generator_v13_0_0/hdl/fifo_generator_v13_0_rfs.vhd))
					if { {} == $parent_comp_file } {
						set dst_cip_file [extract_files -no_ip_dir -quiet -files [list "$ip_static_file"] -base_dir $properties(ipstatic_dir)]
						#puts extracted_file_no_pc=$dst_cip_file
					} else {
						# parent composite is not empty, so get the ip output dir of the parent composite and subtract it from source file
						set parent_ip_name [file root [file tail $parent_comp_file]]
						set ip_output_dir [get_property ip_output_dir [get_ips -all $parent_ip_name]]
						#puts src_ip_file=$ip_static_file

						# get the source ip file dir
						set src_ip_file_dir [file dirname $ip_static_file]

						# strip the ip_output_dir path from source ip file and prepend static dir
						set lib_dir [usf_get_sub_file_path $src_ip_file_dir $ip_output_dir]
						set target_extract_dir [usf_file_normalize [file join $properties(ipstatic_dir) $lib_dir]]
						#puts target_extract_dir=$target_extract_dir

						set dst_cip_file [extract_files -no_path -quiet -files [list "$ip_static_file"] -base_dir $target_extract_dir]
						#puts extracted_file_with_pc=$dst_cip_file
					}
				}
			}
		}
	}

	if { [file exist $dst_cip_file] } {
		if { $properties(use_absolute_paths) } {
			set dst_cip_file "[usf_resolve_file_path $dst_cip_file]"
		} else {
			if { $b_add_ref } {
				set dst_cip_file "\$ref_dir/[usf_get_relative_file_path $dst_cip_file $launch_dir]"
			} else {
				set dst_cip_file "[usf_get_relative_file_path $dst_cip_file $launch_dir]"
			}
		}
		if { $b_wrap_in_quotes } {
			set dst_cip_file "\"$dst_cip_file\""
		}
		set orig_src_file $dst_cip_file
	}

	return $orig_src_file
}

proc usf_find_top_level_ip_file { _file } {
	variable allDesignFiles
	variable a_sim_cache_parent_comp_files

	set comp_file $_file

	set MAX_PARENT_COMP_LEVELS 10
	set count 0
	while (1) {
		incr count
		if { $count > $MAX_PARENT_COMP_LEVELS } { break }

		if { [ info exists allDesignFiles($comp_file) ] } {
			set file_obj $allDesignFiles($comp_file)
		} else {
			set file_obj [ lindex [ get_files -quiet -all [ list "$comp_file" ] ] 0 ]
		}
		if { {} == $file_obj } {

			set file_name [ file tail $comp_file ]
			set file_obj [ lindex [ get_files -all "$file_name" ] 0 ]
			set comp_file $file_obj
		}
		if { [ info exists a_sim_cache_parent_comp_files($comp_file) ] } {
			break
		} else {
			set props [ list_property -quiet $file_obj ]
			if { [ lsearch $props "PARENT_COMPOSITE_FILE" ] == -1 } {
				break
			}	
		}

		set comp_file [get_property parent_composite_file -quiet $file_obj]
	}

	return $comp_file
}

proc usf_is_bd_file { src_file } {
	variable allDesignFiles
	variable a_sim_cache_parent_comp_files

	set comp_file $src_file
	set MAX_PARENT_COMP_LEVELS 10
	set count 0
	while (1) {
		incr count
		if { $count > $MAX_PARENT_COMP_LEVELS } { break }
		set file_obj {}
		if { [ info exists allDesignFiles($comp_file) ] } {
			set file_obj $allDesignFiles($comp_file)
		} else {
			set file_obj [ lindex [ get_files -all [ list "$comp_file" ] ] 0 ]
		}

		if { [ info exists a_sim_cache_parent_comp_files($comp_file) ] } {
			break
		} else {
			set props [list_property -quiet $file_obj]
			if { [ lsearch $props "PARENT_COMPOSITE_FILE" ] == -1 } {
				set a_sim_cache_parent_comp_files($comp_file) true
				break
			}
		}

		set comp_file [ get_property parent_composite_file -quiet $file_obj ]
	}

	if { {.bd} == [file extension $comp_file] } {
		return  1
	}

	return 0
}

proc usf_fetch_ip_static_file { file vh_file_obj } {
  # Summary:
  # Argument Usage:
  # Return Value:

  variable properties
#  if { $properties(b_use_static_lib) } {
#    return $file
#  }

  # /tmp/tp/tp.srcs/sources_1/ip/my_ip/bd_0/ip/ip_2/axi_infrastructure_v1_1_0/hdl/verilog/axi_infrastructure_v1_1_0_header.vh
  set src_ip_file $file
  set src_ip_file [string map {\\ /} $src_ip_file]
  #puts src_ip_file=$src_ip_file

  # get parent composite file path dir
  set comp_file [get_property parent_composite_file -quiet $vh_file_obj] 
  set comp_file_dir [file dirname $comp_file]
  set comp_file_dir [string map {\\ /} $comp_file_dir]
  # /tmp/tp/tp.srcs/sources_1/ip/my_ip/bd_0/ip/ip_2
  #puts comp_file_dir=$comp_file_dir

  # strip parent dir from file path dir
  set lib_file_path {}
  # axi_infrastructure_v1_1_0/hdl/verilog/axi_infrastructure_v1_1_0_header.vh

  set src_file_dirs  [file split [usf_file_normalize $src_ip_file]]
  set comp_file_dirs [file split [usf_file_normalize $comp_file_dir]]
  set src_file_len [llength $src_file_dirs]
  set comp_dir_len [llength $comp_file_dir]

  set index 1
  #puts src_file_dirs=$src_file_dirs
  #puts com_file_dirs=$comp_file_dirs
  while { [lindex $src_file_dirs $index] == [lindex $comp_file_dirs $index] } {
    incr index
    if { ($index == $src_file_len) || ($index == $comp_dir_len) } {
      break;
    }
  }
  set lib_file_path [join [lrange $src_file_dirs $index end] "/"]
  #puts lib_file_path=$lib_file_path

  set dst_cip_file [file join $properties(ipstatic_dir) $lib_file_path]
  # /tmp/tp/tp.ip_user_files/ipstatic/axi_infrastructure_v1_1_0/hdl/verilog/axi_infrastructure_v1_1_0_header.vh
  #puts dst_cip_file=$dst_cip_file
  return $dst_cip_file
}

proc usf_fetch_ipi_static_file { file } {
  # Summary:
  # Argument Usage:
  # Return Value:

  variable properties
  set src_ip_file $file

  if { $properties(b_use_static_lib) } {
    return $src_ip_file
  }

  set comps [lrange [split $src_ip_file "/"] 0 end]
  set to_match "xilinx.com"
  set index 0
  set b_found [usf_find_comp comps index $to_match]
  if { !$b_found } {
    set to_match "user_company"
    set b_found [usf_find_comp comps index $to_match]
  }
  if { !$b_found } {
    return $src_ip_file
  }

  set file_path_str [join [lrange $comps 0 $index] "/"]
  set ip_lib_dir "$file_path_str"

  #puts ip_lib_dir=$ip_lib_dir
  set ip_lib_dir_name [file tail $ip_lib_dir]
  set target_ip_lib_dir [file join $properties(ipstatic_dir) $ip_lib_dir_name]
  #puts target_ip_lib_dir=$target_ip_lib_dir

  # get the sub-dir path after "xilinx.com/xbip_utils_v3_0"
  set ip_hdl_dir [join [lrange $comps 0 $index] "/"]
  set ip_hdl_dir "$ip_hdl_dir"
  # /demo/ipshared/xilinx.com/xbip_utils_v3_0/hdl
  #puts ip_hdl_dir=$ip_hdl_dir
  incr index

  set ip_hdl_sub_dir [join [lrange $comps $index end] "/"]
  # /hdl/xbip_utils_v3_0_vh_rfs.vhd
  #puts ip_hdl_sub_dir=$ip_hdl_sub_dir

  set dst_cip_file [file join $target_ip_lib_dir $ip_hdl_sub_dir]
  #puts dst_cip_file=$dst_cip_file

  # repo static file does not exist? maybe generate_target or export_ip_user_files was not executed, fall-back to project src file
  if { ![file exists $dst_cip_file] } {
    return $src_ip_file
  }

  return $dst_cip_file
}

proc usf_get_dynamic_sim_file_core_container { src_file } {
  # Summary:
  # Argument Usage:
  # Return Value:
  variable properties

  set filename  [file tail $src_file]
  set file_dir  [file dirname $src_file]
  set file_obj  [lindex [get_files -all [list "$src_file"]] 0]
  set xcix_file [get_property core_container $file_obj]
  set core_name [file root [file tail $xcix_file]]

  set parent_comp_file      [get_property parent_composite_file -quiet $file_obj]
  set parent_comp_file_type [get_property file_type [lindex [get_files -all [list "$parent_comp_file"]] 0]]

  set ip_dir {}
  if { {Block Designs} == $parent_comp_file_type } {
    set ip_dir [file join [file dirname $xcix_file] $core_name]
  } else {
    set top_ip_file_name {}
    set ip_dir [usf_get_ip_output_dir_from_parent_composite $src_file top_ip_file_name]
  }
  set hdl_dir_file [usf_get_sub_file_path $file_dir $ip_dir]
  set repo_src_file [file join $properties(dynamic_repo_dir) "ip" $core_name $hdl_dir_file $filename]

  if { [file exists $repo_src_file] } {
    return $repo_src_file
  }

  #send_msg_id exportsim-Tcl-80 WARNING "Corresponding IP user file does not exist:'$repo_src_file'!, using default:'$src_file'"
  return $src_file
}

proc usf_get_dynamic_sim_file_core_classic { src_file } {
  # Summary:
  # Argument Usage:
  # Return Value:

  variable properties
  set filename  [file tail $src_file]
  set file_dir  [file dirname $src_file]
  set file_obj  [lindex [get_files -all [list "$src_file"]] 0]

  set top_ip_file_name {}
  set ip_dir [usf_get_ip_output_dir_from_parent_composite $src_file top_ip_file_name]
  set hdl_dir_file [usf_get_sub_file_path $file_dir $ip_dir]

  set top_ip_name [file root [file tail $top_ip_file_name]]
  set extn [file extension $top_ip_file_name]
  set repo_src_file {}
  set sub_dir "ip"
  if { {.bd} == $extn } {
    set sub_dir "bd"
  }
  set repo_src_file [file join $properties(dynamic_repo_dir) $sub_dir $top_ip_name $hdl_dir_file $filename]
  if { [file exists $repo_src_file] } {
    return $repo_src_file
  }
  return $src_file
}

proc usf_get_ip_output_dir_from_parent_composite { src_file top_ip_file_name_arg } {
  # Summary:
  # Argument Usage:
  # Return Value:

  upvar $top_ip_file_name_arg top_ip_file_name
  set comp_file $src_file
  set MAX_PARENT_COMP_LEVELS 10
  set count 0
  while (1) {
    incr count
    if { $count > $MAX_PARENT_COMP_LEVELS } { break }
    set file_obj [lindex [get_files -all -quiet [list "$comp_file"]] 0]
    set props [list_property $file_obj]
    if { [lsearch $props "PARENT_COMPOSITE_FILE"] == -1 } {
      break
    }
    set comp_file [get_property parent_composite_file -quiet $file_obj]
    #puts "+comp_file=$comp_file"
  }
  set top_ip_name [file root [file tail $comp_file]]
  set top_ip_file_name $comp_file

  set root_comp_file_type [get_property file_type [lindex [get_files -all [list "$comp_file"]] 0]]
  if { {Block Designs} == $root_comp_file_type } {
    set ip_output_dir [file dirname $comp_file]
  } else {
    set ip_output_dir [get_property ip_output_dir [get_ips -all $top_ip_name]]
  }
  return $ip_output_dir
}

proc usf_fetch_header_from_dynamic { vh_file b_is_bd } {
  # Summary:
  # Argument Usage:
  # Return Value:

  variable properties
  #puts vh_file=$vh_file
  set ip_file [usf_get_top_ip_filename $vh_file]
  if { {} == $ip_file } {
    return $vh_file
  }
  set ip_name [file root [file tail $ip_file]]
  #puts ip_name=$ip_name

  # if not core-container (classic), return original source file from project
  if { ![usf_is_core_container $ip_file $ip_name] } {
    return $vh_file
  }

  set vh_filename   [file tail $vh_file]
  set vh_file_dir   [file dirname $vh_file]
  set output_dir    [get_property IP_OUTPUT_DIR [lindex [get_ips -all $ip_name] 0]]
  set sub_file_path [usf_get_sub_file_path $vh_file_dir $output_dir]

  # construct full repo dynamic file path
  set sub_dir "ip"
  if { $b_is_bd } {
    set sub_dir "bd"
  }
  set vh_file [file join $properties(dynamic_repo_dir) $sub_dir $ip_name $sub_file_path $vh_filename]
  #puts vh_file=$vh_file

  return $vh_file
}

proc usf_get_sub_file_path { src_file_path dir_path_to_remove } {
  # Summary:
  # Argument Usage:
  # Return Value:

  set src_path_comps [file split [usf_file_normalize $src_file_path]]
  set dir_path_comps [file split [usf_file_normalize $dir_path_to_remove]]

  set src_path_len [llength $src_path_comps]
  set dir_path_len [llength $dir_path_comps]

  set index 1
  while { [lindex $src_path_comps $index] == [lindex $dir_path_comps $index] } {
    incr index
    if { ($index == $src_path_len) || ($index == $dir_path_len) } {
      break;
    }
  }
  set sub_file_path [join [lrange $src_path_comps $index end] "/"]
  return $sub_file_path
}

proc usf_is_core_container { ip_file ip_name } {
  # Summary:
  # Argument Usage:
  # Return Value:

  set b_is_container 1
  if { [get_property sim.use_central_dir_for_ips [current_project]] } {
    return $b_is_container
  }

  set file_extn [file extension $ip_file]
  #puts $ip_name=$file_extn

  # is this ip core-container? if not return 0 (classic)
  set value [string trim [get_property core_container [get_files -all -quiet ${ip_name}${file_extn}]]]
  if { {} == $value } {
    set b_is_container 0
  }
  return $b_is_container
}

proc usf_find_ipstatic_file_path { src_ip_file parent_comp_file } {
  # Summary:
  # Argument Usage:
  # Return Value:

  variable properties
  set dest_file {}
  set filename [file tail $src_ip_file]
  set file_obj [lindex [get_files -quiet -all [list "$src_ip_file"]] 0]
  if { {} == $file_obj } {
    set file_obj [lindex [get_files -quiet -all $filename] 0]
  }
  if { {} == $file_obj } {
    return $dest_file
  }

  if { {} == $parent_comp_file } {
    set library_name [get_property library $file_obj]
    set comps [lrange [split $src_ip_file "/"] 1 end]
    set index 0
    set b_found false
    set to_match $library_name
    set b_found [usf_find_comp comps index $to_match]
    if { $b_found } {
      set file_path_str [join [lrange $comps $index end] "/"]
      #puts file_path_str=$file_path_str
      set dest_file [usf_file_normalize [file join $properties(ipstatic_dir) $file_path_str]]
    }
  } else {
    set parent_ip_name [file root [file tail $parent_comp_file]]
    set ip_output_dir [get_property ip_output_dir [get_ips -all $parent_ip_name]]
    set src_ip_file_dir [file dirname $src_ip_file]
    set lib_dir [usf_get_sub_file_path $src_ip_file_dir $ip_output_dir]
    set target_extract_dir [usf_file_normalize [file join $properties(ipstatic_dir) $lib_dir]]
    set dest_file [file join $target_extract_dir $filename]
  }
  return $dest_file
}

proc usf_find_file_from_compile_order { ip_name src_file } {
  # Summary:
  # Argument Usage:
  # Return Value:

  variable compileOrderFilesUniq

  #puts src_file=$src_file
  set file [string map {\\ /} $src_file]

  set sub_dirs [list]
  set comps [lrange [split $file "/"] 1 end]
  foreach comp $comps {
    if { {.} == $comp } continue;
    if { {..} == $comp } continue;
    lappend sub_dirs $comp
  }
  set file_path_str [join $sub_dirs "/"]

  set str_to_replace "/{$ip_name}/"
  set str_replace_with "/${ip_name}/"
  regsub -all $str_to_replace $file_path_str $str_replace_with file_path_str
  #puts file_path_str=$file_path_str

  foreach file $compileOrderFilesUniq {
    set file [string map {\\ /} $file]
    #puts +co_file=$file
    if { [string match  *$file_path_str $file] } {
      set src_file $file
      break
    }
  }
  #puts out_file=$src_file
  return $src_file
}

proc usf_is_static_ip_lib { library } {
  # Summary:
  # Argument Usage:
  # Return Value:

  variable l_ip_static_libs
  set library [string tolower $library]
  if { [lsearch $l_ip_static_libs $library] != -1 } {
    return true
  }
  return false
}

proc usf_find_comp { comps_arg index_arg to_match } {
  # Summary:
  # Argument Usage:
  # Return Value:

  upvar $comps_arg comps
  upvar $index_arg index
  set index 0
  set b_found false
  foreach comp $comps {
    incr index
    if { $to_match != $comp } continue;
    set b_found true
    break
  }
  return $b_found
}

proc is_hdl_type { file_type } {
  return [expr { ({Verilog} == $file_type) || ({SystemVerilog} == $file_type) || ({VHDL} == $file_type) || ({VHDL 2008} == $file_type) }]
}

proc vip_ips {} {
    return [list "axi_vip" "axi4stream_vip"]
}

proc is_vip_ip_required {} {
  foreach ip_obj [get_ips -all -quiet] {
    set requires_vip [get_property -quiet requires_vip $ip_obj]
    if { $requires_vip } {
      return true
    }
  }

  foreach ip_obj [get_ips -all -quiet] {
    set ipdef [get_property -quiet IPDEF $ip_obj]
    set ip_name [lindex [split $ipdef ":"] 2]
    if { [lsearch -nocase [vip_ips] $ip_name] != -1 } {
      return true
    }
  }
  return false 
}

}

#
# not used currently
#
namespace eval ::tclapp::aldec::common::helpers {
proc usf_get_top { top_arg } {
  # Summary:
  # Argument Usage:
  # Return Value:

  upvar $top_arg top
  set fileset_object [get_filesets $properties(simset)]
  set fs_name [get_property "NAME" $fileset_object]
  set top [get_property "TOP" $fileset_object]
  if { {} == $top } {
    send_msg_id USF-[usf_aldec_getSimulatorName]-81 ERROR "Top module not set for fileset '$fs_name'. Please ensure that a valid \
       value is provided for 'top'. The value for 'top' can be set/changed using the 'Top Module Name' field under\
       'Project Settings', or using the 'set_property top' Tcl command (e.g. set_property top <name> \[current_fileset\])."
    return 1
  }
  return 0
}

}

}
