Debian Codegraph @

library/platform/platform.tcl

raw
1 # -*- tcl -*-
2 # ### ### ### ######### ######### #########
3 ## Overview
4
5 # Heuristics to assemble a platform identifier from publicly available
6 # information. The identifier describes the platform of the currently
7 # running tcl shell. This is a mixture of the runtime environment and
8 # of build-time properties of the executable itself.
9 #
10 # Examples:
11 # <1> A tcl shell executing on a x86_64 processor, but having a
12 # wordsize of 4 was compiled for the x86 environment, i.e. 32
13 # bit, and loaded packages have to match that, and not the
14 # actual cpu.
15 #
16 # <2> The hp/solaris 32/64 bit builds of the core cannot be
17 # distinguished by looking at tcl_platform. As packages have to
18 # match the 32/64 information we have to look in more places. In
19 # this case we inspect the executable itself (magic numbers,
20 # i.e. fileutil::magic::filetype).
21 #
22 # The basic information used comes out of the 'os' and 'machine'
23 # entries of the 'tcl_platform' array. A number of general and
24 # os/machine specific transformation are applied to get a canonical
25 # result.
26 #
27 # General
28 # Only the first element of 'os' is used - we don't care whether we
29 # are on "Windows NT" or "Windows XP" or whatever.
30 #
31 # Machine specific
32 # % amd64 -> x86_64
33 # % arm* -> arm
34 # % sun4* -> sparc
35 # % ia32* -> ix86
36 # % intel -> ix86
37 # % i*86* -> ix86
38 # % Power* -> powerpc
39 # % x86_64 + wordSize 4 => x86 code
40 #
41 # OS specific
42 # % AIX are always powerpc machines
43 # % HP-UX 9000/800 etc means parisc
44 # % linux has to take glibc version into account
45 # % sunos -> solaris, and keep version number
46 #
47 # NOTE: A platform like linux glibc 2.3, which can use glibc 2.2 stuff
48 # has to provide all possible allowed platform identifiers when
49 # searching search. Ditto a solaris 2.8 platform can use solaris 2.6
50 # packages. Etc. This is handled by the other procedure, see below.
51
52 # ### ### ### ######### ######### #########
53 ## Requirements
54
55 namespace eval ::platform {}
56
57 # ### ### ### ######### ######### #########
58 ## Implementation
59
60 # -- platform::generic
61 #
62 # Assembles an identifier for the generic platform. It leaves out
63 # details like kernel version, libc version, etc.
64
65 proc ::platform::generic {} {
66 global tcl_platform
67
68 set plat [string tolower [lindex $tcl_platform(os) 0]]
69 set cpu $tcl_platform(machine)
70
71 switch -glob -- $cpu {
72 sun4* {
73 set cpu sparc
74 }
75 intel -
76 ia32* -
77 i*86* {
78 set cpu ix86
79 }
80 x86_64 {
81 if {$tcl_platform(wordSize) == 4} {
82 # See Example <1> at the top of this file.
83 set cpu ix86
84 }
85 }
86 ppc -
87 "Power*" {
88 set cpu powerpc
89 }
90 "arm*" {
91 set cpu arm
92 }
93 ia64 {
94 if {$tcl_platform(wordSize) == 4} {
95 append cpu _32
96 }
97 }
98 }
99
100 switch -glob -- $plat {
101 windows {
102 if {$tcl_platform(platform) eq "unix"} {
103 set plat cygwin
104 } else {
105 set plat win32
106 }
107 if {$cpu eq "amd64"} {
108 # Do not check wordSize, win32-x64 is an IL32P64 platform.
109 set cpu x86_64
110 }
111 }
112 sunos {
113 set plat solaris
114 if {[string match "ix86" $cpu]} {
115 if {$tcl_platform(wordSize) == 8} {
116 set cpu x86_64
117 }
118 } elseif {![string match "ia64*" $cpu]} {
119 # sparc
120 if {$tcl_platform(wordSize) == 8} {
121 append cpu 64
122 }
123 }
124 }
125 darwin {
126 set major [lindex [split $tcl_platform(osVersion) .] 0]
127 if {$major > 19} {
128 set plat macos
129 } else {
130 set plat macosx
131 }
132 # Correctly identify the cpu when running as a 64bit
133 # process on a machine with a 32bit kernel
134 if {$cpu eq "ix86"} {
135 if {$tcl_platform(wordSize) == 8} {
136 set cpu x86_64
137 }
138 }
139 }
140 aix {
141 set cpu powerpc
142 if {$tcl_platform(wordSize) == 8} {
143 append cpu 64
144 }
145 }
146 hp-ux {
147 set plat hpux
148 if {![string match "ia64*" $cpu]} {
149 set cpu parisc
150 if {$tcl_platform(wordSize) == 8} {
151 append cpu 64
152 }
153 }
154 }
155 osf1 {
156 set plat tru64
157 }
158 default {
159 set plat [lindex [split $plat _-] 0]
160 }
161 }
162
163 return "${plat}-${cpu}"
164 }
165
166 # -- platform::identify
167 #
168 # Assembles an identifier for the exact platform, by extending the
169 # generic identifier. I.e. it adds in details like kernel version,
170 # libc version, etc., if they are relevant for the loading of
171 # packages on the platform.
172
173 proc ::platform::identify {} {
174 global tcl_platform
175
176 set id [generic]
177 regexp {^([^-]+)-([^-]+)$} $id -> plat cpu
178
179 switch -glob -- $plat {
180 solaris {
181 regsub {^5} $tcl_platform(osVersion) 2 text
182 append plat $text
183 return "${plat}-${cpu}"
184 }
185 macos* {
186 set major [lindex [split $tcl_platform(osVersion) .] 0]
187 if {$major > 19} {
188 incr major
189 if {$major < 26} {
190 incr major -10
191 }
192 append plat $major
193 } else {
194 incr major -4
195 append plat 10.$major
196 }
197 return "${plat}-${cpu}"
198 }
199 linux {
200 # Look for the libc*.so and determine its version
201 # (libc5/6, libc6 further glibc 2.X)
202
203 set v unknown
204
205 # Determine in which directory to look. /lib, or /lib64.
206 # For that we use the tcl_platform(wordSize).
207 #
208 # We could use the 'cpu' info, per the equivalence below,
209 # that however would be restricted to intel. And this may
210 # be a arm, mips, etc. system. The wordsize is more
211 # fundamental.
212 #
213 # ix86 <=> (wordSize == 4) <=> 32 bit ==> /lib
214 # x86_64 <=> (wordSize == 8) <=> 64 bit ==> /lib64
215 #
216 # Do not look into /lib64 even if present, if the cpu
217 # doesn't fit.
218
219 # TODO: Determine the prefixes (i386, x86_64, ...) for
220 # other cpus. The path after the generic one is utterly
221 # specific to intel right now. Ok, on Ubuntu, possibly
222 # other Debian systems we may apparently be able to query
223 # the necessary CPU code. If we can't we simply use the
224 # hardwired fallback.
225
226 switch -- $tcl_platform(wordSize) {
227 4 {
228 lappend bases /lib
229 try {
230 set res [exec dpkg-architecture -qDEB_HOST_MULTIARCH]
231 # dpkg-arch returns the full triple, not just cpu.
232 lappend bases /lib/$res
233 } on error {} {
234 lappend bases /lib/i386-linux-gnu
235 }
236 }
237 8 {
238 lappend bases /lib64
239 try {
240 set res [exec dpkg-architecture -qDEB_HOST_MULTIARCH]
241 # dpkg-arch returns the full triple, not just cpu.
242 lappend bases /lib/$res
243 } on error {} {
244 lappend bases /lib/x86_64-linux-gnu
245 }
246 }
247 default {
248 return -code error "Bad wordSize $tcl_platform(wordSize), expected 4 or 8"
249 }
250 }
251
252 foreach base $bases {
253 if {[LibcVersion $base -> v]} break
254 }
255
256 append plat -$v
257 return "${plat}-${cpu}"
258 }
259 }
260
261 return $id
262 }
263
264 proc ::platform::LibcVersion {base _->_ vv} {
265 upvar 1 $vv v
266 set libclist [lsort [glob -nocomplain -directory $base libc*]]
267
268 if {![llength $libclist]} { return 0 }
269
270 set libc [lindex $libclist 0]
271
272 # Try executing the library first. This should succeed
273 # for a glibc library, and return the version information.
274
275 try {
276 set vdata [lindex [split [exec $libc] \n] 0]
277 } on ok {} {
278 regexp {version ([0-9]+(\.[0-9]+)*)} $vdata -> v
279 lassign [split $v .] major minor
280 set v glibc${major}.${minor}
281 return 1
282 } on error {} {
283 # We had trouble executing the library. We are now
284 # inspecting its name to determine the version
285 # number. This code by Larry McVoy.
286
287 if {[regexp -- {libc-([0-9]+)\.([0-9]+)} $libc -> major minor]} {
288 set v glibc${major}.${minor}
289 return 1
290 }
291 }
292 return 0
293 }
294
295 # -- platform::patterns
296 #
297 # Given an exact platform identifier, i.e. _not_ the generic
298 # identifier it assembles a list of exact platform identifier
299 # describing platform which should be compatible with the
300 # input.
301 #
302 # I.e. packages for all platforms in the result list should be
303 # loadable on the specified platform.
304
305 # << Should we add the generic identifier to the list as well ? In
306 # general it is not compatible I believe. So better not. In many
307 # cases the exact identifier is identical to the generic one
308 # anyway.
309 # >>
310
311 proc ::platform::patterns {id} {
312 set res [list $id]
313 if {$id eq "tcl"} {return $res}
314
315 switch -glob -- $id {
316 solaris*-* {
317 if {[regexp {solaris([^-]*)-(.*)} $id -> v cpu]} {
318 if {$v eq ""} {return $id}
319 lassign [split $v .] major minor
320 incr minor -1
321 for {set j $minor} {$j >= 6} {incr j -1} {
322 lappend res solaris${major}.${j}-${cpu}
323 }
324 }
325 }
326 linux*-* {
327 if {[regexp {linux-glibc([^-]*)-(.*)} $id -> v cpu]} {
328 lassign [split $v .] major minor
329 incr minor -1
330 for {set j $minor} {$j >= 0} {incr j -1} {
331 lappend res linux-glibc${major}.${j}-${cpu}
332 }
333 }
334 }
335 macosx-powerpc {
336 lappend res macosx-universal
337 }
338 macosx-x86_64 {
339 lappend res macosx-i386-x86_64
340 }
341 macosx-ix86 {
342 lappend res macosx-universal macosx-i386-x86_64
343 }
344 macos*-* {
345 # 10.5+,11.0+
346 if {[regexp {macosx?([^-]*)-(.*)} $id -> v cpu]} {
347
348 lassign [split $v.15 .] major minor
349 switch -exact -- $cpu {
350 ix86 {
351 lappend alt i386-x86_64
352 lappend alt universal
353 }
354 x86_64 {
355 if {$major < 11 && $minor < 15} {
356 set alt i386-x86_64
357 } else {
358 set alt {}
359 }
360 }
361 arm {
362 lappend alt x86_64
363 }
364 default { set alt {} }
365 }
366
367 if {$v ne ""} {
368 set res {}
369 while {$major > 10} {
370 # Add $major to patterns.
371 lappend res macos${major}-${cpu}
372 foreach a $alt {
373 lappend res macos${major}-$a
374 }
375 incr major -1
376 if {$major == 25} {
377 set major 15
378 }
379 }
380 # Add 10.9 to 10.minor to patterns.
381 for {set j $minor} {$j >= 9} {incr j -1} {
382 if {$cpu ne "arm"} {
383 lappend res macosx${major}.${j}-${cpu}
384 }
385 if {($cpu eq "x86_64") && ($j == 14) && ![package vsatisfies [package provide Tcl] 9.0-]} {
386 set alt i386-x86_64
387 }
388 foreach a $alt {
389 lappend res macosx${major}.${j}-$a
390 }
391 }
392 if {![package vsatisfies [package provide Tcl] 9.0-]} {
393 # Continue up to 10.5.
394 for {} {$j >= 5} {incr j -1} {
395 if {$cpu ne "arm"} {
396 lappend res macosx${major}.${j}-${cpu}
397 }
398 foreach a $alt {
399 lappend res macosx${major}.${j}-$a
400 }
401 }
402 # Add unversioned patterns for 10.3/10.4 builds.
403 lappend res macosx-${cpu}
404 foreach a $alt {
405 lappend res macosx-$a
406 }
407 }
408 } else {
409 # No version, just do unversioned patterns.
410 foreach a $alt {
411 lappend res macosx-$a
412 }
413 }
414 } else {
415 # no v, no cpu ... nothing
416 }
417 }
418 }
419 lappend res tcl ; # Pure tcl packages are always compatible.
420 return $res
421 }
422
423
424 # ### ### ### ######### ######### #########
425 ## Ready
426
427 package provide platform 1.1.0
428
429 # ### ### ### ######### ######### #########
430 ## Demo application
431
432 if {[info exists argv0] && ($argv0 eq [info script])} {
433 puts ====================================
434 parray tcl_platform
435 puts ====================================
436 puts Generic\ identification:\ [::platform::generic]
437 puts Exact\ identification:\ \ \ [::platform::identify]
438 puts ====================================
439 puts Search\ patterns:
440 puts *\ [join [::platform::patterns [::platform::identify]] \n*\ ]
441 puts ====================================
442 exit 0
443 }