1 |
2010/12/13 Ryan Hill <dirtyepic@g.o> |
2 |
|
3 |
> On Sun, 12 Dec 2010 09:01:13 -0400 |
4 |
> "Sergio D. Rodríguez Inclan" <srinclan@×××××.com> wrote: |
5 |
> |
6 |
> > El 12/12/2010 02:46 a.m., Ryan Hill escribió: |
7 |
> > > I think the fewer sources of magic USE flags the better. Maybe we |
8 |
> could |
9 |
> > > document how to figure out what instruction sets a processor supports |
10 |
> in the |
11 |
> > > handbook instead. |
12 |
> |
13 |
> > A good manual would be greatly appreciated :) |
14 |
> |
15 |
> I wrote a guide a couple weeks ago that might be a good starting point. |
16 |
> |
17 |
> http://en.gentoo-wiki.com/wiki/Hardware_CFLAGS |
18 |
> |
19 |
> |
20 |
if I read correctly the article on the wiki it does circa what the script |
21 |
reported below does. |
22 |
would be possible to adopt something similar for automatic C*FLAGS selection |
23 |
if someone step in willing to take the pain to mantain it. |
24 |
|
25 |
#!/usr/bin/python |
26 |
# Copyright 2010 Gentoo Foundation |
27 |
# Distributed under the terms of the GNU General Public License v2 |
28 |
# Author: Francesco Riosa |
29 |
# extrapolated from http://en.gentoo-wiki.com/wiki/Hardware_CFLAGS, errors |
30 |
are mine |
31 |
|
32 |
# kate: encoding utf-8; eol unix |
33 |
# kate: indent-width 4; mixedindent off; replace-tabs on; |
34 |
# kate: remove-trailing-space on; space-indent on |
35 |
|
36 |
# echo "int main() { return 0; }" | gcc -march=native -v -E - 2>&1 | grep |
37 |
march |
38 |
# echo "int main() { return 0; }" | gcc -march=core2 -v -Q -x c - 2>&1 |
39 |
|
40 |
""" |
41 |
example output: |
42 |
./hw-cflags.py |
43 |
extrapolating flags for gcc-4.4.5 |
44 |
useful flags: -march=core2 -msse4.1 --param l1-cache-size=32 --param |
45 |
l1-cache-line-size=64 --param l2-cache-size=2048 -mtune=generic |
46 |
redundant: -mcx16 -msahf |
47 |
|
48 |
extrapolating flags for gcc-4.5.1 |
49 |
useful flags: -march=core2 -msse4.1 --param l1-cache-size=32 --param |
50 |
l1-cache-line-size=64 --param l2-cache-size=2048 -mtune=core2 |
51 |
redundant: -mcx16 -msahf |
52 |
""" |
53 |
|
54 |
import os |
55 |
import time |
56 |
import fnmatch |
57 |
from subprocess import Popen, PIPE |
58 |
|
59 |
GCC_PATH = '/usr/bin/' |
60 |
GCC_LIST = fnmatch.filter(os.listdir(GCC_PATH), 'gcc-[0-9].*') |
61 |
GCC_LIST.sort() |
62 |
|
63 |
def extract_flags(gcccmd, header): |
64 |
# get output from gcc |
65 |
buf = '' |
66 |
devnul = open('/dev/null', 'w') |
67 |
p = Popen(gcccmd, stdin=PIPE, stdout=devnul, stderr=PIPE) |
68 |
p.stdin.write("""int main() { return 0; }""") |
69 |
p.stdin.close() |
70 |
while p.poll() is None: |
71 |
t = p.stderr.read() |
72 |
buf = "buf%s" % t |
73 |
time.sleep(0.01) |
74 |
p.stderr.close() |
75 |
devnul.close() |
76 |
|
77 |
# parse it |
78 |
flags = [] |
79 |
add = False |
80 |
for line in buf.split('\n'): |
81 |
if line.startswith(header): |
82 |
add = True |
83 |
flags += line.strip().split(' ') |
84 |
continue |
85 |
if add: |
86 |
if line.startswith(' '): |
87 |
flags += line.strip().split(' ') |
88 |
else: |
89 |
break |
90 |
|
91 |
# extract flags we are interested in |
92 |
t = [] |
93 |
march = '' |
94 |
mtune = '-mtune=generic' |
95 |
for i in xrange(len(flags)): |
96 |
if flags[i].startswith('-m'): |
97 |
if flags[i].startswith('-mtune'): |
98 |
mtune = flags[i] |
99 |
elif flags[i].startswith('-march'): |
100 |
march = flags[i] |
101 |
else: |
102 |
t.append(flags[i]) |
103 |
elif flags[i] == '--param': |
104 |
t.append("%s %s" % (flags[i], flags[i+1])) |
105 |
flags = t |
106 |
|
107 |
return march, mtune, flags |
108 |
|
109 |
|
110 |
for gcc in GCC_LIST: |
111 |
|
112 |
print "extrapolating flags for %s" % gcc |
113 |
|
114 |
gcccmd = [ GCC_PATH + gcc, '-march=native', '-v', '-E', '-', ] |
115 |
header='COLLECT_GCC_OPTIONS' |
116 |
march, mtune, flags_native = extract_flags(gcccmd, header) |
117 |
|
118 |
gcccmd = [ GCC_PATH + gcc, march, '-v', '-Q', '-x', 'c', '-', ] |
119 |
header='options enabled:' |
120 |
t, t, flags_enabled = extract_flags(gcccmd, header) |
121 |
|
122 |
redundant_flags = [] |
123 |
useful_flags = [] |
124 |
|
125 |
for x in flags_native: |
126 |
if x in flags_enabled: |
127 |
redundant_flags.append(x) |
128 |
else: |
129 |
useful_flags.append(x) |
130 |
|
131 |
if gcc < "gcc-4.5.0": |
132 |
mtune = '-mtune=generic' |
133 |
|
134 |
print " useful flags: %s %s %s " % (march, " ".join(useful_flags), |
135 |
mtune) |
136 |
print " redundant: %s" % " ".join(redundant_flags) |
137 |
print |