Debian Codegraph @

debian/debcrossgen

raw
1 #!/usr/bin/env python3
2
3 # Copyright 2017-2023 Jussi Pakkanen
4
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8
9 # http://www.apache.org/licenses/LICENSE-2.0
10
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 import sys, os, subprocess
18
19 import argparse
20
21 parser = argparse.ArgumentParser(description='''Generate cross compilation definition file for the Meson build system.
22
23 If you do not specify the --arch argument, Meson assumes that running
24 plain 'dpkg-architecture' will return correct information for the
25 host system.
26
27 This script must be run in an environment where CPPFLAGS et al are set to the
28 same values used in the actual compilation.
29 '''
30 )
31
32 parser.add_argument('--arch', default=None,
33 help='The dpkg architecture to generate.')
34 parser.add_argument('--gccsuffix', default="",
35 help='A particular gcc version suffix if necessary.')
36 parser.add_argument('-o', required=True, dest='outfile',
37 help='The output file.')
38
39 def locate_path(program):
40 if os.path.isabs(program):
41 return program
42 for d in os.get_exec_path():
43 f = os.path.join(d, program)
44 if os.access(f, os.X_OK):
45 return f
46 raise ValueError("%s not found on $PATH" % program)
47
48 def write_args_line(ofile, name, args):
49 if len(args) == 0:
50 return
51 ostr = name + ' = ['
52 ostr += ', '.join("'" + i + "'" for i in args)
53 ostr += ']\n'
54 ofile.write(ostr)
55
56 def write_args_from_envvars(ofile):
57 import shlex
58 cppflags = shlex.split(os.environ.get('CPPFLAGS', ''))
59 cflags = shlex.split(os.environ.get('CFLAGS', ''))
60 cxxflags = shlex.split(os.environ.get('CXXFLAGS', ''))
61 ldflags = shlex.split(os.environ.get('LDFLAGS', ''))
62
63 c_args = cppflags + cflags
64 cpp_args = cppflags + cxxflags
65 c_link_args = cflags + ldflags
66 cpp_link_args = cxxflags + ldflags
67
68 write_args_line(ofile, 'c_args', c_args)
69 write_args_line(ofile, 'cpp_args', cpp_args)
70 write_args_line(ofile, 'c_link_args', c_link_args)
71 write_args_line(ofile, 'cpp_link_args', cpp_link_args)
72
73 cpu_family_map = dict(mips64el="mips64",
74 i686='x86',
75 powerpc64le='ppc64',
76 )
77 cpu_map = dict(armhf="arm7hlf",
78 mips64el="mips64",
79 powerpc64le="ppc64",
80 )
81
82 def run(options):
83 if options.arch is None:
84 cmd = ['dpkg-architecture']
85 else:
86 cmd = ['dpkg-architecture', '-a' + options.arch]
87 output = subprocess.check_output(cmd, universal_newlines=True,
88 stderr=subprocess.DEVNULL)
89 data = {}
90 for line in output.split('\n'):
91 line = line.strip()
92 if line == '':
93 continue
94 k, v = line.split('=', 1)
95 data[k] = v
96 host_arch = data['DEB_HOST_GNU_TYPE']
97 host_os = data['DEB_HOST_ARCH_OS']
98 host_cpu_family = cpu_family_map.get(data['DEB_HOST_GNU_CPU'],
99 data['DEB_HOST_GNU_CPU'])
100 host_cpu = cpu_map.get(data['DEB_HOST_ARCH'],
101 data['DEB_HOST_ARCH'])
102 host_endian = data['DEB_HOST_ARCH_ENDIAN']
103 with open(options.outfile, "w") as ofile:
104 ofile.write('[binaries]\n')
105 c = "%s-gcc%s" % (host_arch, options.gccsuffix)
106 ofile.write("c = '%s'\n" % locate_path(c))
107 cpp = "%s-g++%s" % (host_arch, options.gccsuffix)
108 ofile.write("cpp = '%s'\n" % locate_path(cpp))
109 ofile.write("ar = '%s'\n" % locate_path("%s-ar" % host_arch))
110 ofile.write("strip = '%s'\n" % locate_path("%s-strip" % host_arch))
111 ofile.write("objcopy = '%s'\n" % locate_path("%s-objcopy" % host_arch))
112 ofile.write("ld= '%s'\n" % locate_path("%s-ld" % host_arch))
113 try:
114 ofile.write("pkgconfig = '%s'\n" % locate_path("%s-pkg-config" % host_arch))
115 except ValueError:
116 pass # pkg-config is optional
117 try:
118 ofile.write("cups-config = '%s'\n" % locate_path("cups-config"))
119 except ValueError:
120 pass # cups-config is optional
121 ofile.write('\n[properties]\n')
122 write_args_from_envvars(ofile)
123 ofile.write('\n[host_machine]\n')
124 ofile.write("system = '%s'\n" % host_os)
125 ofile.write("cpu_family = '%s'\n" % host_cpu_family)
126 ofile.write("cpu = '%s'\n" % host_cpu)
127 ofile.write("endian = '%s'\n" % host_endian)
128
129 def internal_impl():
130 print('WARNING: this tool is deprecated, use "meson env2mfile" instead.')
131 options = parser.parse_args()
132 run(options)
133 print('Remember to add the correct --libdir arg to Meson invocation.')
134
135 def external_impl():
136 print('NOTE: Using Meson env2mfile, you should update build scripts to use that instead.')
137 cmd = ['meson', 'env2mfile', '--cross']
138 had_arch = False
139 for c in sys.argv[1:]:
140 if c.startswith('--arch'):
141 c = c.replace('--arch', '--debarch')
142 had_arch = True
143 cmd.append(c)
144 if not had_arch:
145 cmd.append('--debarch=auto')
146 try:
147 subprocess.check_call(cmd, shell=False)
148 except Exception as e:
149 sys.exit(str(e))
150
151 if __name__ == '__main__':
152 revert_to_internal = 'MESON_USE_NEW_CROSSGEN' not in os.environ
153 if revert_to_internal:
154 internal_impl()
155 else:
156 external_impl()