# HG changeset patch
# User Leif Leonhardy <not.really@online.de>
# Date 1318168354 -7200
# Node ID 17d69430d27730cc35a55c90406136efd3faaa8f
# Parent 60153e03ae7c5946ac3539d248ce1e3002edf1b0
#11760: Avoid multiple definitions of SAGE_ROOT in pkg-config (.pc) files. Also cosmetic changes to sage-location.
diff --git a/sage-location b/sage-location
|
a
|
b
|
|
| 8 | 8 | location_file = os.path.join(SAGE_ROOT, 'local', 'lib', 'sage-current-location.txt') |
| 9 | 9 | flags_file = os.path.join(SAGE_ROOT, 'local', 'lib', 'sage-flags.txt') |
| 10 | 10 | |
| 11 | | # The flags we care about recording in the local/lib/sage-flags.txt file |
| | 11 | |
| | 12 | # The flags we care about recording in the local/lib/sage-flags.txt file. |
| 12 | 13 | # In SAGE_FAT_BINARY mode we only require that ['sse', 'sse2', '3d', |
| 13 | | # 'mmx', 'cmov'] be available, and in particular, don't require pni |
| 14 | | # or ssse3. |
| | 14 | # 'mmx', 'cmov'] be available, and in particular, we don't require pni |
| | 15 | # (Prescott New Instructions, including SSE3) or ssse3. |
| 15 | 16 | |
| 16 | | try: |
| 17 | | SAGE_FAT_BINARY = os.environ['SAGE_FAT_BINARY'] |
| 18 | | except: |
| 19 | | SAGE_FAT_BINARY = "" |
| | 17 | SAGE_FAT_BINARY = os.environ.get('SAGE_FAT_BINARY',"") |
| 20 | 18 | |
| 21 | 19 | if SAGE_FAT_BINARY == "yes": |
| 22 | 20 | FLAGS = ['sse', 'sse2', '3d', 'mmx', 'cmov'] |
| … |
… |
|
| 26 | 24 | |
| 27 | 25 | def write_location_file(): |
| 28 | 26 | """ |
| 29 | | Write the location file with the current SAGE_ROOT |
| | 27 | Write the location file with the current value of ``SAGE_ROOT``. |
| 30 | 28 | """ |
| 31 | 29 | # Write new location file. |
| 32 | 30 | O = open(location_file,'w') |
| … |
… |
|
| 35 | 33 | |
| 36 | 34 | def read_location_file(): |
| 37 | 35 | """ |
| 38 | | If the location file exists, return the real path contained in it. Otherwise, return None. |
| | 36 | If the location file exists, return the real path contained in it. |
| | 37 | Otherwise return ``None``. |
| 39 | 38 | """ |
| 40 | 39 | if os.path.exists(location_file): |
| 41 | | # The install moved and we had written the flags and location files. |
| | 40 | # The install moved and we had written the flags and location files. |
| 42 | 41 | O = open(location_file) |
| 43 | 42 | R = O.read().strip() |
| 44 | 43 | O.close() |
| … |
… |
|
| 46 | 45 | else: |
| 47 | 46 | return None |
| 48 | 47 | |
| | 48 | |
| 49 | 49 | def install_moved(): |
| 50 | 50 | """ |
| 51 | | Check whether or not this install of Sage moved. If it hasn't |
| 52 | | moved make sure the location and processor flags files are |
| 53 | | written. |
| | 51 | Check whether or not this installation of Sage has moved. If it hasn't |
| | 52 | moved, make sure the location and processor flags files are written. |
| 54 | 53 | |
| 55 | | Returns True if location possibly moved, and sets the global |
| 56 | | OLD_SAGE_ROOT variable to the old SAGE_ROOT path. |
| | 54 | Return ``True`` if location possibly changed, and set the global |
| | 55 | ``OLD_SAGE_ROOT`` variable to the old ``SAGE_ROOT`` path. |
| 57 | 56 | |
| 58 | | Returns False if location has not moved. |
| | 57 | Return ``False`` if the Sage installation hasn't moved. |
| 59 | 58 | """ |
| 60 | | # Write the flags file if it isn't there. |
| | 59 | # Write the flags file if it isn't there: |
| 61 | 60 | if not os.path.exists(flags_file): |
| 62 | 61 | f=open(flags_file,'w') |
| 63 | 62 | f.write(get_flags_info()) |
| … |
… |
|
| 73 | 72 | elif path != SAGE_ROOT: |
| 74 | 73 | # location moved |
| 75 | 74 | global OLD_SAGE_ROOT |
| 76 | | OLD_SAGE_ROOT=path |
| | 75 | OLD_SAGE_ROOT = path |
| 77 | 76 | write_location_file() |
| 78 | 77 | return True |
| 79 | 78 | else: |
| 80 | 79 | # path didn't change |
| 81 | 80 | return False |
| 82 | 81 | |
| | 82 | |
| 83 | 83 | def get_flags_info(): |
| 84 | 84 | """ |
| 85 | | Return a space-separated string that lists the flags supported by |
| 86 | | this CPU from /proc/cpuinfo. |
| | 85 | Return a space-separated string that lists the flags indicating what's |
| | 86 | supported by this CPU by reading ``/proc/cpuinfo``. |
| 87 | 87 | """ |
| 88 | 88 | try: |
| 89 | 89 | r = open('/proc/cpuinfo').read() |
| … |
… |
|
| 99 | 99 | return ' '.join(set(x for x in r.split() if x in FLAGS)) |
| 100 | 100 | |
| 101 | 101 | except IOError: |
| 102 | | # On a system without /proc/cpuinfo, so don't bother. In |
| 103 | | # particular, for non Linux systems I have no clue how to get |
| | 102 | # On a system without /proc/cpuinfo, so don't bother. In |
| | 103 | # particular, for non-Linux systems I have no clue how to get |
| 104 | 104 | # the processor flags, and we so far have never ever had any |
| 105 | 105 | # problem with processor flags on such machines. So we don't |
| 106 | 106 | # bother. |
| 107 | 107 | return '' |
| 108 | 108 | |
| | 109 | |
| 109 | 110 | def check_processor_flags(): |
| 110 | 111 | """ |
| 111 | 112 | Make sure all processor flags from the build machine are on this machine. |
| 112 | | If the sage-flags.txt file is missing, don't bother to do this check. |
| | 113 | If the ``sage-flags.txt`` file is missing, don't bother to do this check. |
| 113 | 114 | """ |
| 114 | 115 | if not os.path.exists(flags_file): return |
| 115 | 116 | # We check that the processor flags of the original build are a |
| … |
… |
|
| 134 | 135 | print "*"*70 |
| 135 | 136 | sys.exit(1) |
| 136 | 137 | |
| | 138 | |
| 137 | 139 | def update_library_files(): |
| 138 | 140 | """ |
| 139 | | Run ranlib on the library directory and manually change the path |
| 140 | | in .la library files. |
| | 141 | Run ``ranlib`` on all static libraries (``*.a``) in the library directory, |
| | 142 | and manually change the paths in all of ``libtool``'s ``.la`` library files. |
| 141 | 143 | """ |
| 142 | 144 | LIB = os.path.join(os.path.abspath(SAGE_ROOT), 'local', 'lib') |
| 143 | | # The .a files should be re-ranlib'd |
| 144 | | os.system('cd "%s"; ranlib *.a 1>/dev/null 2>/dev/null'%LIB) |
| | 145 | # The .a files should be re-ranlib'd: |
| | 146 | os.system('cd "%s"; ranlib *.a 1>/dev/null 2>/dev/null' % LIB) |
| 145 | 147 | |
| 146 | | # The .la files hardcode path info, so we manually fix the path |
| 147 | | # info |
| | 148 | # The .la files hardcode path info, so we manually fix the path info: |
| 148 | 149 | for F in os.listdir(LIB): |
| 149 | 150 | if os.path.splitext(F)[1]==".la": |
| 150 | 151 | G = open(os.path.join(LIB,F)).read() |
| 151 | | i = G.find('libdir=') |
| 152 | | j = i+8 + G[i+8:].find("'") |
| 153 | | z = G[i+8:j].strip().strip("'") |
| 154 | | i = z.rfind('local/') |
| 155 | | if i != -1: |
| | 152 | i = G.find('libdir=') |
| | 153 | j = i+8 + G[i+8:].find("'") |
| | 154 | z = G[i+8:j].strip().strip("'") |
| | 155 | i = z.rfind('local/') |
| | 156 | if i != -1: |
| 156 | 157 | z = z[:i] |
| 157 | 158 | H = G.replace(z, os.path.abspath(SAGE_ROOT) + '/') |
| 158 | 159 | open(os.path.join(LIB, F),'w').write(H) |
| … |
… |
|
| 160 | 161 | |
| 161 | 162 | def initialize_pkgconfig_files(): |
| 162 | 163 | """ |
| 163 | | Insert a sage_local variable in each pkg_config file and replace |
| 164 | | them to make the paths portable. |
| | 164 | Insert a ``SAGE_ROOT`` variable into each ``pkg-config`` file and replace |
| | 165 | occurrences of its current value by references to it (``${SAGE_ROOT}``). |
| | 166 | This way we only have to change the definition of ``SAGE_ROOT`` whenever |
| | 167 | the Sage installation has moved, i.e., paths have changed. |
| 165 | 168 | """ |
| | 169 | import re |
| 166 | 170 | LIB = os.path.join(os.path.abspath(SAGE_ROOT), 'local', 'lib') |
| 167 | | PKG = os.path.join(LIB,'pkgconfig') |
| | 171 | PKG = os.path.join(LIB, 'pkgconfig') |
| 168 | 172 | for name in os.listdir(PKG): |
| 169 | | filename=os.path.join(PKG,name) |
| | 173 | filename = os.path.join(PKG, name) |
| 170 | 174 | if os.path.splitext(filename)[1]==".pc": |
| 171 | 175 | with open(filename) as file: |
| 172 | 176 | config = file.read() |
| 173 | | |
| | 177 | |
| | 178 | if re.search("^SAGE_ROOT=", config, re.MULTILINE): |
| | 179 | # There's already a definition of SAGE_ROOT, |
| | 180 | # so skip this file. (Cf. #11760). |
| | 181 | continue |
| | 182 | |
| 174 | 183 | new_config = config.replace(os.path.abspath(SAGE_ROOT), "${SAGE_ROOT}") |
| 175 | | |
| 176 | | new_config = 'SAGE_ROOT=%s\n'%os.path.abspath(SAGE_ROOT)+new_config |
| 177 | | |
| | 184 | new_config = 'SAGE_ROOT=%s\n' % os.path.abspath(SAGE_ROOT) + new_config |
| 178 | 185 | with open(filename, 'w') as file: |
| 179 | 186 | file.write(new_config) |
| 180 | 187 | |
| 181 | | |
| 182 | 188 | |
| 183 | 189 | def update_pkgconfig_files(): |
| 184 | 190 | """ |
| 185 | | Change paths in package configuration files. |
| | 191 | Change / update paths in all ``pkg-config`` (``*.pc``) files pointing into |
| | 192 | the Sage installation tree, which is necessary whenever Sage has moved. |
| | 193 | Requires that the files have previously been processed by |
| | 194 | ``initialize_pkgconfig_files()``, in particular, each of them contains a |
| | 195 | definition of the variable ``SAGE_ROOT``, since only that is updated here. |
| 186 | 196 | """ |
| 187 | 197 | LIB = os.path.join(os.path.abspath(SAGE_ROOT), 'local', 'lib') |
| 188 | | PKG = os.path.join(LIB,'pkgconfig') |
| | 198 | PKG = os.path.join(LIB, 'pkgconfig') |
| 189 | 199 | for name in os.listdir(PKG): |
| 190 | | filename=os.path.join(PKG,name) |
| | 200 | filename = os.path.join(PKG,name) |
| 191 | 201 | if os.path.splitext(filename)[1]==".pc": |
| 192 | 202 | with open(filename) as file: |
| 193 | 203 | config = file.read() |
| 194 | | |
| 195 | | prefix_start=config.find('SAGE_ROOT=') |
| 196 | | prefix_end=config.find('\n', prefix_start) |
| 197 | | new_prefix='SAGE_ROOT=%s'%os.path.abspath(SAGE_ROOT) |
| 198 | | new_config=config[:prefix_start]+new_prefix+config[prefix_end:] |
| 199 | 204 | |
| | 205 | prefix_start = config.find('SAGE_ROOT=') |
| | 206 | if prefix_start==-1: |
| | 207 | # This should never happen, unless the user modified the file. |
| | 208 | sys.stderr.write( |
| | 209 | "Error: sage_location: update_pkgconfig_files():\n" + |
| | 210 | " File \"%s\" doesn't contain a definition of SAGE_ROOT.\n" % |
| | 211 | name + " Skipping it...\n") |
| | 212 | sys.stderr.flush() |
| | 213 | continue |
| | 214 | |
| | 215 | prefix_end = config.find('\n', prefix_start) |
| | 216 | new_prefix = 'SAGE_ROOT=%s' % os.path.abspath(SAGE_ROOT) |
| | 217 | new_config = config[:prefix_start] + new_prefix + config[prefix_end:] |
| 200 | 218 | with open(filename, 'w') as file: |
| 201 | 219 | file.write(new_config) |
| 202 | | |
| | 220 | |
| 203 | 221 | |
| 204 | 222 | def remove_files(path, remove_extensions): |
| 205 | 223 | """ |
| 206 | | Walk the tree starting at path and remove all files with |
| 207 | | extensions in remove_ext. The extensions in remove_extensions |
| 208 | | should start with a period, i.e., remove_files(path, ('.pyc', |
| 209 | | '.pyo')). |
| | 224 | Walk the tree starting at ``path``, and remove all files with |
| | 225 | extensions in ``remove_extensions``. |
| | 226 | The extensions in ``remove_extensions`` should start with a period, i.e., |
| | 227 | e.g. use ``remove_files(path, ('.pyc', '.pyo'))``. |
| 210 | 228 | """ |
| 211 | 229 | for root, dirs, files in os.walk(path): |
| 212 | 230 | for file in files: |
| 213 | | filename=os.path.join(root,file) |
| | 231 | filename = os.path.join(root, file) |
| 214 | 232 | if os.path.splitext(filename)[1] in remove_extensions: |
| 215 | 233 | try: |
| 216 | 234 | os.unlink(filename) |
| 217 | 235 | except OSError, msg: |
| 218 | 236 | print msg |
| 219 | | |
| | 237 | |
| | 238 | |
| 220 | 239 | def __mysig(a,b): |
| 221 | | raise KeyboardInterrupt, "computation timed out because alarm was set for %s seconds"%__alarm_time |
| | 240 | raise KeyboardInterrupt, "Computation timed out because alarm was set for %s seconds." % __alarm_time |
| 222 | 241 | |
| 223 | | if __name__ == '__main__': |
| 224 | | |
| | 242 | |
| | 243 | if __name__ == '__main__': |
| | 244 | |
| 225 | 245 | check_processor_flags() |
| | 246 | # Note: install_moved() may also run e.g. initialize_pkgconfig_files(). |
| 226 | 247 | if install_moved(): |
| 227 | | print "The Sage install tree may have moved" |
| 228 | | print "(from %s to %s)"%(OLD_SAGE_ROOT, SAGE_ROOT) |
| 229 | | print "Changing various hardcoded paths" |
| 230 | | print "(please wait at most a few minutes)..." |
| 231 | | print "Do not interrupt this." |
| 232 | | update_library_files() |
| | 248 | print "The Sage installation tree may have moved" |
| | 249 | print "(from %s to %s)." % (OLD_SAGE_ROOT, SAGE_ROOT) |
| | 250 | print "Changing various hardcoded paths..." |
| | 251 | print "(Please wait at most a few minutes.)" |
| | 252 | print "DO NOT INTERRUPT THIS." |
| | 253 | sys.stdout.flush() # One never knows... |
| | 254 | update_library_files() |
| 233 | 255 | update_pkgconfig_files() |
| 234 | | # Compiled python files need to be regenerated, so we remove them |
| | 256 | # Compiled python files need to be regenerated, so we remove them: |
| 235 | 257 | remove_files(os.path.join(SAGE_ROOT, 'local', 'lib', 'python'), |
| 236 | 258 | remove_extensions=('.pyc', '.pyo')) |
| 237 | | print "Done resetting paths" |
| | 259 | print "Done resetting paths." |