forked from tycho/cpuid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SConstruct
184 lines (159 loc) · 4.77 KB
/
SConstruct
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import os
import platform
import re
import subprocess
def subprocess_output(cmdline):
p = subprocess.Popen(cmdline.split(" "), stdout=subprocess.PIPE)
stdout, stderr = p.communicate()
stdout = stdout.decode('utf-8')
return stdout.rstrip()
def read_whole_file(path):
f = open(path)
r = f.read().rstrip()
f.close()
return r
def describe_revision():
if os.path.isdir('.git'):
#
# I'd like to use Dulwich or Git-Python to do this, but
# I concluded that it's highly unlikely that people would
# have the git repo checked out, but not have git.
#
hash = subprocess_output("git rev-list HEAD~1..HEAD")
tag = subprocess_output("git describe --abbrev=0")
revs = subprocess_output("git rev-list %s..HEAD" % (tag,)).split('\n')
if len(revs) and revs[0] == '':
revs = []
n = len(revs)
return ('git', tag, n, hash)
elif os.path.isdir('.hg'):
#
# I considered using the Mercurial API directly, but the
# Mercurial wiki highly discourages it.
#
hash = subprocess_output("hg parent --template {node}")
tag = subprocess_output("hg parent --template {latesttag}")
n = int(subprocess_output("hg parent --template {latesttagdistance}"))
return ('hg', tag, n, hash)
else:
tag = read_whole_file('tools/release_ver')
n = 0
hash = 'no-hash'
return ('rel', tag, n, hash)
def generate_build_header(**kwargs):
env = kwargs['env']
targets = kwargs['target']
source = kwargs['source']
scm, tag, n, hash = describe_revision()
extra = tag.split('-')
version = list(map(int, extra[0].split('.')))
while len(version) < 3:
version.append(0)
if n < 1:
long = "%s" % (tag)
else:
long = "%s+%d-%s-%s" % (tag, n, scm, hash[0:8])
lines = [
"#ifndef __included_cpuid_build_h\n",
"#define __included_cpuid_build_h\n",
"\n",
"#define CPUID_VERSION_MAJOR %d\n" % version[0],
"#define CPUID_VERSION_MINOR %d\n" % version[1],
"#define CPUID_VERSION_REVISION %d\n" % version[2],
"#define CPUID_VERSION_BUILD %d\n" % n,
"#define CPUID_VERSION_TAG \"%s\"\n" % tag,
"#define CPUID_VERSION_LONG \"%s\"\n" % long,
"\n",
"#define CPUID_RESOURCE_VERSION %d,%d,%d,%d\n" % (version[0], version[1], version[2], n),
"#define CPUID_RESOURCE_VERSION_STRING \"%d, %d, %d, %d\"\n" % (version[0], version[1], version[2], n),
"\n",
"#endif\n"
]
for target in targets:
f = open(str(target), "wt")
f.writelines(lines)
f.close()
return 0
def generate_license_header(**kwargs):
env = kwargs['env']
targets = kwargs['target']
source = kwargs['source']
source = str(source[0])
source = open(source, 'rb')
license = source.read().decode('utf-8')
source.close()
license = re.sub('\t', r'\\t', license)
license = re.sub('\n', r'\\n', license)
license = re.sub('"', r'\\"', license)
lines = [
"#ifndef __included_cpuid_license_h\n",
"#define __included_cpuid_license_h\n",
"\n",
"#define CPUID_LICENSE \"%s\"\n" % (license),
"\n",
"#endif\n"
]
for target in targets:
f = open(str(target), 'wt')
f.writelines(lines)
f.close()
env = Environment(
tools=['default'],
ENV=os.environ,
**ARGUMENTS)
env.Decider('MD5-timestamp')
debug = ARGUMENTS.get('debug', 0)
if env['CC'] == 'gcc' or env['CC'] == 'clang':
# Optimization levels
if int(debug):
env.Append(CFLAGS='-O0 -ggdb')
else:
env.Append(CFLAGS='-Os')
# Basic CFLAGS for correctness
env.Append(CFLAGS='-std=gnu89 -fno-strict-aliasing')
# Standard search path
env.Append(CFLAGS='-I.')
# Warning flags
env.Append(CFLAGS='-Wall -Wextra -Wdeclaration-after-statement -Wimplicit-function-declaration -Wmissing-declarations -Wmissing-prototypes -Wno-long-long -Wno-overlength-strings -Wold-style-definition -Wstrict-prototypes')
# pthreads
env.Append(CFLAGS='-pthread')
env.Append(LINKFLAGS='-pthread')
# needed for sqrt
env.Append(LIBS=['-lm'])
if platform.system() == 'Linux':
# needed on older glibc for clock_gettime
env.Append(LIBS=['-lrt'])
elif env['CC'] == 'cl':
# Optimization levels
if int(debug):
env.Append(CFLAGS=['/Od', '/MTd'])
else:
env.Append(CFLAGS=['/O2', '/MT'])
# Standard search path
env.Append(CFLAGS=['/I.', '/Imsvc'])
# For high resolution timers
env.Append(LINKFLAGS='winmm.lib')
sources = [
'cache.c',
'clock.c',
'cpuid.c',
'feature.c',
'handlers.c',
'main.c',
'sanity.c',
'threads.c',
'util.c',
'version.c',
]
if not env.GetOption('clean'):
conf = Configure(env)
if not conf.CheckHeader('getopt.h') or not conf.CheckFunc('getopt_long'):
conf.env.Append(CFLAGS='-Igetopt')
sources.append('getopt/getopt_long.c')
env = conf.Finish()
else:
sources.append('getopt/getopt_long.c')
env.Command('build.h', [Value(describe_revision())], env.Action(generate_build_header, 'Generating build.h'))
env.Command('license.h', '#COPYING', env.Action(generate_license_header, 'Generating license.h'))
env.Program(target='cpuid', source=sources)
# vim: set ts=4 sts=4 sw=4 noet: