11import os
2- import sys
32import distutils .ccompiler
43import tempfile
54import shutil
65
76import scorep .helper
7+ from scorep .helper import print_err
8+
9+
10+ def _print_info (msg ):
11+ """Print an info message with a prefix"""
12+ print_err ("scorep: " + msg )
813
914
1015def generate_subsystem_lib_name ():
@@ -24,20 +29,19 @@ def generate_ld_preload(scorep_config):
2429 @return ld_preload string which needs to be passed to LD_PRELOAD
2530 """
2631
27- (_ , preload , _ ) = scorep .helper .call (
28- ["scorep-config" ] + scorep_config + ["--user" , "--preload-libs" ])
29- return preload
32+ (_ , preload , _ ) = scorep .helper .call (["scorep-config" ] + scorep_config + ["--preload-libs" ])
33+ return preload .strip ()
3034
3135
32- def generate_subsystem_code (config = [] ):
36+ def generate_subsystem_code (config ):
3337 """
3438 Generates the data needed to be preloaded.
3539 """
3640
37- scorep_config = ["scorep-config" ] + config + [ "--user" ]
41+ scorep_config = ["scorep-config" ] + config
3842
39- (retrun_code , _ , _ ) = scorep .helper .call (scorep_config )
40- if retrun_code != 0 :
43+ (return_code , _ , _ ) = scorep .helper .call (scorep_config )
44+ if return_code != 0 :
4145 raise ValueError (
4246 "given config {} is not supported" .format (scorep_config ))
4347 (_ , scorep_adapter_init , _ ) = scorep .helper .call (
@@ -75,9 +79,7 @@ def generate(scorep_config, keep_files=False):
7579
7680 temp_dir = tempfile .mkdtemp (prefix = "scorep." )
7781 if keep_files :
78- sys .stderr .write (
79- "Score-P files are keept at: {}\n " .format (temp_dir ))
80- sys .stderr .flush ()
82+ _print_info ("Score-P files are kept at: " + temp_dir )
8183
8284 with open (temp_dir + "/scorep_init.c" , "w" ) as f :
8385 f .write (scorep_adapter_init )
@@ -99,35 +101,50 @@ def generate(scorep_config, keep_files=False):
99101 return (subsystem_lib_name , temp_dir )
100102
101103
102- def init_environment (scorep_config = [] , keep_files = False ):
104+ def init_environment (scorep_config , keep_files = False , verbose = False ):
103105 """
104- Set the inital needed environmet variables, to get everythin up an running.
105- As a few variables interact with LD env vars, the programms needs to be restarted after this.
106+ Set the inital needed environment variables, to get everything up an running.
107+ As a few variables interact with LD env vars, the program needs to be restarted after this.
106108
107109 @param scorep_config configuration flags for score-p
108110 @param keep_files whether to keep the generated files, or not.
111+ @param verbose Set to True to output information about config used and environment variables set.
109112 """
110113
111114 if "libscorep" in os .environ .get ("LD_PRELOAD" , "" ):
112- raise RuntimeError (
113- "Score-P is already loaded. This should not happen at this point" )
115+ raise RuntimeError ("Score-P is already loaded. This should not happen at this point" )
116+
117+ if "--user" not in scorep_config :
118+ scorep_config .append ("--user" )
114119
115- subsystem_lib_name , temp_dir = scorep .subsystem .generate (
116- scorep_config , keep_files )
120+ if verbose :
121+ _print_info ("Score-P config: %s" % scorep_config )
122+
123+ old_env = os .environ .copy ()
124+
125+ subsystem_lib_name , temp_dir = generate (scorep_config , keep_files )
117126 scorep_ld_preload = generate_ld_preload (scorep_config )
118127
119128 scorep .helper .add_to_ld_library_path (temp_dir )
120129
121130 preload_str = scorep_ld_preload + " " + subsystem_lib_name
122- if "LD_PRELOAD" in os .environ :
123- sys .stderr .write (
124- "LD_PRELOAD is already specified. If Score-P is already loaded this might lead to errors." )
131+ if os .environ .get ("LD_PRELOAD" ):
132+ print_err ("LD_PRELOAD is already specified. If Score-P is already loaded this might lead to errors." )
125133 preload_str = os .environ ["LD_PRELOAD" ] + " " + preload_str
126134 os .environ ["SCOREP_LD_PRELOAD_BACKUP" ] = os .environ ["LD_PRELOAD" ]
127135 else :
128136 os .environ ["SCOREP_LD_PRELOAD_BACKUP" ] = ""
129137 os .environ ["LD_PRELOAD" ] = preload_str
130138
139+ if verbose :
140+ for var in ("LD_LIBRARY_PATH" , "LD_PRELOAD" ):
141+ # Shorten the setting to e.g.: FOO=new:$FOO
142+ old_val = old_env .get (var )
143+ new_val = os .environ [var ]
144+ if old_val :
145+ new_val = new_val .replace (old_val , '$' + var )
146+ _print_info ('%s="%s"' % (var , new_val ))
147+
131148
132149def reset_preload ():
133150 """
0 commit comments