-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprojectinfo.py
160 lines (128 loc) · 5.06 KB
/
projectinfo.py
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
#@ {
#@ "targets":
#@ [{
#@ "name":"projectinfo.hpp"
#@ ,"dependencies":[{"ref":"externals.json","rel":"misc"}
#@ ,{"ref":"maikeconfig.json","rel":"misc"}
#@ ,{"ref":"projectinfo.json","rel":"misc"}
#@ ,{"ref":"versioninfo.txt","rel":"misc"}]
#@ }]
#@ }
import sys
import json
import string
import time
import subprocess
import os
def write_error(*args, **kwargs):
print(*args,file=sys.stderr,**kwargs)
projinfo_template=string.Template('''/*Information about $projname
*
* This file has been generated by $srcfile on $date.
* Any changes to this file may be overwritten during compilation.
*/
#ifndef MAIKE_PROJECTINFO_HPP
#define MAIKE_PROJECTINFO_HPP
namespace Maike
{
struct ProjectInfo
{
public:
static constexpr const char* name() noexcept
{return s_projname;}
static constexpr const char* revision() noexcept
{return s_revision;}
static constexpr const char* nameAndRevision() noexcept
{return s_name_rev;}
static constexpr const char* description() noexcept
{return s_description;}
static constexpr const char* author() noexcept
{return s_author;}
static constexpr const char* years() noexcept
{return s_years;}
static constexpr const char* copyright() noexcept
{return s_copyright;}
static constexpr const char* const* acknowledgement() noexcept
{return s_acknowledgement;}
static constexpr const char* acknowledgementAll() noexcept
{return s_acknowledgement_all;}
static constexpr const char* legalBrief() noexcept
{return s_legal_brief;}
static constexpr const char* const* libraries() noexcept
{return s_libraries;}
static constexpr const char* const* tools() noexcept
{return s_tools;}
static constexpr const char* compilationDate() noexcept
{return s_compilation_date;}
static constexpr const char* compiler() noexcept
{return s_compiler;}
static constexpr const char* architecture() noexcept
{return s_architecture;}
static constexpr const char* techstring() noexcept
{return s_techstring;}
private:
static constexpr const char* s_projname="$projname";
static constexpr const char* s_revision="$revision";
static constexpr const char* s_name_rev="$projname, $revision";
static constexpr const char* s_description="$description";
static constexpr const char* s_author="$author";
static constexpr const char* s_years="$years";
static constexpr const char* s_copyright="©\xa0$years\xa0$author";
static constexpr const char* s_acknowledgement[]={"$acknowledgement",nullptr};
static constexpr const char* s_acknowledgement_all="$acknowledgement_all";
static constexpr const char* s_legal_brief="$legal_brief";
static constexpr const char* s_libraries[]={"$libraries",nullptr};
static constexpr const char* s_tools[]={"$tools",nullptr};
static constexpr const char* s_compilation_date="$date";
static constexpr const char* s_compiler="$compiler";
static constexpr const char* s_architecture="$architecture";
static constexpr const char* s_techstring="This $projname was "
"compiled for $architecture by\\n$compiler, on $date, using $libstring.";
};
}
#endif
''')
def load(filename):
with open(filename,encoding='utf-8') as input:
return json.load(input)
def compiler_name(config):
for hook in config['target_hooks']:
if hook['name']=='targetcxx_default':
return hook['config']['objcompile']['name']
def compiler_version(exename):
with subprocess.Popen([exename,'--version'] \
,stdout=subprocess.PIPE) as compiler:
for lines in compiler.stdout:
return lines.decode('utf8').rstrip()
return result
def get_revision(target_dir):
with open(target_dir + '/versioninfo.txt') as versionfile:
return versionfile.read().strip()
try:
target_dir=sys.argv[1]
in_dir=sys.argv[2]
substitutes=dict()
substitutes['srcfile']=sys.argv[0]
substitutes['date']=time.strftime('%Y-%m-%d %H:%M %Z')
projinfo=load(in_dir + '/projectinfo.json')
substitutes['projname']=projinfo['name']
substitutes['acknowledgement']='","'.join(projinfo['acknowledgement'])
substitutes['acknowledgement_all']='\\n'.join(projinfo['acknowledgement'])
substitutes['author']=projinfo['author']
substitutes['legal_brief']=projinfo['legal_brief']
substitutes['years']=str(projinfo['years']).replace(', ','–').strip('[]')
substitutes['revision']=get_revision(target_dir)
substitutes['description']=projinfo['description']
externals=load(target_dir + '/externals.json')
substitutes['libraries']='","'.join(sorted( externals['libraries']) )
substitutes['libstring']=', '.join(sorted( externals['libraries']) )
substitutes['tools']='","'.join(sorted( externals['tools']) )
config=load(target_dir + '/maikeconfig.json')
substitutes['compiler']=compiler_version(compiler_name(config)).replace(' ','\xa0')
substitutes['architecture']=config['targetinfo']['architecture']
with open(target_dir + '/' + in_dir + '/projectinfo.hpp','wb') as output:
output.write(projinfo_template.substitute(substitutes).encode('utf-8'))
sys.exit(0)
except Exception:
write_error('%s:%d: error: %s\n'%(sys.argv[0],sys.exc_info()[2].tb_lineno,sys.exc_info()[1]))
sys.exit(-1)