aboutsummaryrefslogtreecommitdiff
path: root/tools/configure.py
blob: b29d0cfe603d8c97b67c2fc24da26146c48e92d9 (plain)
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
#!/usr/bin/env python3
""" This tool serves for user configuration of the libcarrot.
	Basically, it takes the user parameters (architecture, platform,
	programming languages, additional modules, build tools), and makes
	a configuration that the actual tool, 'make.py', can read to know
	what he is supposed to do.

	It is responsible for finding the modules corresponding to what the
	user asks, finding a configuration of the build tools that work and
	produce the expected result, and warn if what the user asks for is
	incorrect or if there are any mistakes in the involved
	global, platform or module configurations.
"""

import os, sys, platform, shlex
from subprocess import call

import yaml
from Internals import *

def __getplatform():
	""" Internal function to get the current platform. """
	return platform.system().lower()

def __getarch():
	""" Internal function to get the current architecture. """
	return platform.machine().lower()

def __getendian():
	""" Internal function to get the current endianness. """
	return sys.byteorder
#*****************************************************************************#
# Set up the arguments parser                                                 #
#*****************************************************************************#
argparser = CarrotArgumentParser()

# Main arguments.
argparser.add_argument('-v', '/V', '--version', dest='version',
	action='store_true', help=loc["args"]["version"])
argparser.add_argument('-q', '--quiet', '/-silent', dest='silent',
	action='store_true', help=loc["args"]["quiet"])
argparser.add_argument('-o', '--output', '/C', '/-config-cache', dest='out',
	default=os.path.join(os.getcwd(), '.config.yml'),
	help=loc["args"]["out"])

# Build-related configuration elements.
argparser.add_argument('-t', '--target', dest='target', default=None,
	help=loc["args"]["target"])
argparser.add_argument('-l', '--languages', dest='lang', default='c',
	help=loc["args"]["languages"])
argparser.add_argument('-a', '--add', dest='modules',
	default=[], action='append',
	help=loc["args"]["add"])
argparser.add_argument('-T', '--tooldir', dest='tooldir', default=None,
	help=loc["args"]["tooldir"])

# Installation-related elements (TODO).

argparser.add_argument('--root', help=argparse.SUPPRESS)
argparser.add_argument('--prefix', help=argparse.SUPPRESS)
argparser.add_argument('--libdir', help=argparse.SUPPRESS)
argparser.add_argument('--includedir', help=argparse.SUPPRESS)

# Deprecated and hidden arguments.

argparser.add_argument('--arch', dest='arch',
	default=__getarch(), help=argparse.SUPPRESS)
argparser.add_argument('--endian', dest='endian',
	default=__getendian(), help=argparse.SUPPRESS)
argparser.add_argument('--platform', dest='platform',
	default=__getplatform(), help=argparse.SUPPRESS)

# Reserved arguments (from the glibc configure script).
argparser.add_argument('--maintainer', action='store_true',
	help=argparse.SUPPRESS)
argparser.add_argument('--cache-file', help=argparse.SUPPRESS)

argparser.add_argument('--exec-prefix', help=argparse.SUPPRESS)
argparser.add_argument('--bindir', help=argparse.SUPPRESS)
argparser.add_argument('--sbindir', help=argparse.SUPPRESS)
argparser.add_argument('--libexecdir', help=argparse.SUPPRESS)
argparser.add_argument('--sysconfdir', help=argparse.SUPPRESS)
argparser.add_argument('--sharedstatedir', help=argparse.SUPPRESS)
argparser.add_argument('--localstatedir', help=argparse.SUPPRESS)
argparser.add_argument('--oldincludedir', help=argparse.SUPPRESS)
argparser.add_argument('--datarootdir', help=argparse.SUPPRESS)
argparser.add_argument('--datadir', help=argparse.SUPPRESS)
argparser.add_argument('--infodir', help=argparse.SUPPRESS)
argparser.add_argument('--localedir', help=argparse.SUPPRESS)
argparser.add_argument('--mandir', help=argparse.SUPPRESS)
argparser.add_argument('--docdir', help=argparse.SUPPRESS)
argparser.add_argument('--htmldir', help=argparse.SUPPRESS)
argparser.add_argument('--dvidir', help=argparse.SUPPRESS)
argparser.add_argument('--pdfdir', help=argparse.SUPPRESS)
argparser.add_argument('--psdir', help=argparse.SUPPRESS)

argparser.add_argument('--disable-option-checking', action='store_true',
	help=argparse.SUPPRESS)
argparser.add_argument('--disable-sanity-checks', action='store_true',
	help=argparse.SUPPRESS)
argparser.add_argument('--enable-shared', action='store_true',
	help=argparse.SUPPRESS)
argparser.add_argument('--enable-profile', action='store_true',
	help=argparse.SUPPRESS)
argparser.add_argument('--disable-timezone-tools', action='store_true',
	help=argparse.SUPPRESS)
argparser.add_argument('--enable-hardcoded-path-in-tests', action='store_true',
	help=argparse.SUPPRESS)
argparser.add_argument('--enable-stackguard-randomization',
	action='store_true', help=argparse.SUPPRESS)
argparser.add_argument('--enable-lock-elision', choices=('yes', 'no'),
	default='no', help=argparse.SUPPRESS)
argparser.add_argument('--disable-hidden-plt', action='store_true',
	help=argparse.SUPPRESS)
#*****************************************************************************#
# Write the configuration                                                     #
#*****************************************************************************#
def main():
	# Parse the arguments.
	args = argparser.parse_args()

	# Get the arguments.
	if args.target:
		args.arch = args.target.split('-')[0]
		args.platform = '-'.join(args.target.split('-')[1:])

	# Get the languages.
	args.lang = set(args.lang.split(','))
	for lang in args.lang:
		if not lang in ['c', 'c++']:
			Raise(UnsupportedLanguageException(lang))

	# Get the additional modules.
	addm = []
	for arg in args.modules:
		addm += arg.split(',')
	args.modules = addm

	# Get the global source.
	g = SourceGlobal('arch')

	# Check the main configuration.
	if args.version:
		print(g.version)
		return

	# Clean the repository.
	ret = call(['python3', os.path.join('tools', 'make.py'), 'mrproper'])
	if ret: Raise(SilentException)

	# Get the roots.
	root = os.path.normpath(os.path.join(os.path.dirname(__file__), '..'))
	mroot = os.path.join(root, 'arch')

	# Get the rest.
	if not args.arch in g.arch:
		raise UnsupportedArchException(args.arch)
		exit(1)

	# Find the tools.
	found_tools = tools.find(args.lang, args.arch)
	tools.setup(found_tools)

	# List the modules.
	modules = list(g.findmodules(args.arch, args.platform,
		tools.get('compiler'), args.lang, args.modules).keys())

	# Write the configuration.
	with open(args.out, "w") as out:
		print(yaml.dump({"magic": "potatosdk-1.0", "tools": found_tools,
			"modules": modules, "orig": ' '.join(map(shlex.quote, sys.argv))},
			version=(1, 2), width=60), file=out, end='')

	# Tell the user we're done.
	if not args.silent:
		print(loc["messages"]["configured"])

if __name__ == "__main__":
	do_main(main)

# End of file.