1 # functions for handling cross-compilation
3 import Utils
, Logs
, sys
, os
, Options
, re
4 from Configure
import conf
8 ANSWER_UNKNOWN
= (254, "")
9 ANSWER_FAIL
= (255, "")
12 cross_answers_incomplete
= False
15 def add_answer(ca_file
, msg
, answer
):
16 '''add an answer to a set of cross answers'''
18 f
= open(ca_file
, 'a')
20 Logs
.error("Unable to open cross-answers file %s" % ca_file
)
22 if answer
== ANSWER_OK
:
23 f
.write('%s: OK\n' % msg
)
24 elif answer
== ANSWER_UNKNOWN
:
25 f
.write('%s: UNKNOWN\n' % msg
)
26 elif answer
== ANSWER_FAIL
:
27 f
.write('%s: FAIL\n' % msg
)
29 (retcode
, retstring
) = answer
30 f
.write('%s: (%d, "%s")' % (msg
, retcode
, retstring
))
34 def cross_answer(ca_file
, msg
):
35 '''return a (retcode,retstring) tuple from a answers file'''
37 f
= open(ca_file
, 'r')
39 add_answer(ca_file
, msg
, ANSWER_UNKNOWN
)
43 if line
== '' or line
[0] == '#':
45 if line
.find(':') != -1:
47 thismsg
= a
[0].strip()
51 if ans
== "OK" or ans
== "YES":
54 elif ans
== "UNKNOWN":
57 elif ans
== "FAIL" or ans
== "NO":
61 return (0, ans
.strip('"'))
63 return (0, ans
.strip("'"))
65 m
= re
.match('\(\s*(-?\d+)\s*,\s*\"(.*)\"\s*\)', ans
)
68 return (int(m
.group(1)), m
.group(2))
70 raise Utils
.WafError("Bad answer format '%s' in %s" % (line
, ca_file
))
72 add_answer(ca_file
, msg
, ANSWER_UNKNOWN
)
76 class cross_Popen(Utils
.pproc
.Popen
):
77 '''cross-compilation wrapper for Popen'''
78 def __init__(*k
, **kw
):
81 if '--cross-execute' in args
:
82 # when --cross-execute is set, then change the arguments
83 # to use the cross emulator
84 i
= args
.index('--cross-execute')
85 newargs
= args
[i
+1].split()
86 newargs
.extend(args
[0:i
])
88 elif '--cross-answers' in args
:
89 # when --cross-answers is set, then change the arguments
90 # to use the cross answers if available
91 i
= args
.index('--cross-answers')
94 ans
= cross_answer(ca_file
, msg
)
95 if ans
== ANSWER_UNKNOWN
:
96 global cross_answers_incomplete
97 cross_answers_incomplete
= True
98 (retcode
, retstring
) = ans
99 args
= ['/bin/sh', '-c', "echo -n '%s'; exit %d" % (retstring
, retcode
)]
100 real_Popen
.__init
__(*(obj
, args
), **kw
)
104 def SAMBA_CROSS_ARGS(conf
, msg
=None):
105 '''get exec_args to pass when running cross compiled binaries'''
106 if not conf
.env
.CROSS_COMPILE
:
110 if real_Popen
is None:
111 real_Popen
= Utils
.pproc
.Popen
112 Utils
.pproc
.Popen
= cross_Popen
116 if conf
.env
.CROSS_EXECUTE
:
117 ret
.extend(['--cross-execute', conf
.env
.CROSS_EXECUTE
])
118 elif conf
.env
.CROSS_ANSWERS
:
120 raise Utils
.WafError("Cannot have NULL msg in cross-answers")
121 ret
.extend(['--cross-answers', os
.path
.join(Options
.launch_dir
, conf
.env
.CROSS_ANSWERS
), msg
])
124 raise Utils
.WafError("Cannot cross-compile without either --cross-execute or --cross-answers")
129 def SAMBA_CROSS_CHECK_COMPLETE(conf
):
130 '''check if we have some unanswered questions'''
131 global cross_answers_incomplete
132 if conf
.env
.CROSS_COMPILE
and cross_answers_incomplete
:
133 raise Utils
.WafError("Cross answers file %s is incomplete" % conf
.env
.CROSS_ANSWERS
)