winexe: add configure option to control whether to build it (default: auto)
[bbaumbach/samba-autobuild/.git] / examples / winexe / wscript_build
1 #!/usr/bin/env python
2
3 import samba_utils
4
5 def generate_winexesvc_c_from_exe(t):
6     '''generate a C source file with the contents of the given binary'''
7     src = t.inputs[0].bldpath(t.env)
8     tgt = t.outputs[0].bldpath(t.env)
9     fn = t.env.SAMBA_GENERATOR_VARS['WINEXE_FN']
10
11     try:
12         with open(src, 'rb') as f:
13             src_blob = f.read()
14             f.close()
15     except:
16         print('Failed to read %s to convert to C array' % (src))
17         return -1
18
19     def c_array(src):
20         N = 0
21         result = ''
22         while src:
23             l = src[:8]
24             src = src[8:]
25             # Even files opened in binary mode are read as type "str" in
26             # Python 2, so we need to get the integer ordinal of each
27             # character in the string before we try to convert it to hex.
28             if isinstance(l, str):
29                 h = ' '.join(["0x%02X," % ord(x) for x in l])
30             # Files opened in binary mode are read as type "bytes" in
31             # Python 3, so we can convert each individual integer in the
32             # array of bytes to hex directly.
33             else:
34                 h = ' '.join(["0x%02X," % x for x in l])
35             result += "\t\t%s\n" % (h)
36         return result
37
38     src_array = c_array(src_blob)
39     if len(src_array) <= 0:
40         print('Failed to convert %s to C array' % (src))
41         return -1
42
43     contents = '''
44 #include "replace.h"
45 #include "lib/util/data_blob.h"
46
47 const DATA_BLOB *%s(void);
48 const DATA_BLOB *%s(void)
49 {
50 \tstatic const uint8_t array[] = {
51 %s
52 \t};
53 \tstatic const DATA_BLOB blob = {
54 \t\t.data = discard_const_p(uint8_t, array),
55 \t\t.length = ARRAY_SIZE(array),
56 \t};
57 \treturn &blob;
58 }
59 ''' % (fn, fn, src_array)
60
61     if not samba_utils.save_file(tgt, contents):
62         print('Failed to write C source file %s' % (tgt))
63         return -1
64     return 0
65
66 winexesvc_binaries = ''
67
68 if bld.env.WINEXE_CC_WIN32:
69     bld.SAMBA_GENERATOR(
70         'winexesvc32_exe',
71         source='winexesvc.c',
72         target='winexesvc32.exe',
73         rule='${WINEXE_CC_WIN32} ${SRC} -o ${TGT} ${WINEXE_LDFLAGS}')
74     vars = {"WINEXE_FN": "winexesvc32_exe_binary"}
75     bld.SAMBA_GENERATOR(
76         'winexesvc32_exe_binary',
77         source='winexesvc32.exe',
78         target='winexesvc32_exe_binary.c',
79         group='build_source',
80         vars=vars,
81         rule=generate_winexesvc_c_from_exe)
82     winexesvc_binaries += ' winexesvc32_exe_binary.c'
83
84 if bld.env.WINEXE_CC_WIN64:
85     bld.SAMBA_GENERATOR(
86         'winexesvc64_exe',
87         source='winexesvc.c',
88         target='winexesvc64.exe',
89         rule='${WINEXE_CC_WIN64} ${SRC} -o ${TGT} ${WINEXE_LDFLAGS}')
90     vars = {"WINEXE_FN": "winexesvc64_exe_binary"}
91     bld.SAMBA_GENERATOR(
92         'winexesvc64_exe_binary',
93         source='winexesvc64.exe',
94         target='winexesvc64_exe_binary.c',
95         group='build_source',
96         vars=vars,
97         rule=generate_winexesvc_c_from_exe)
98     winexesvc_binaries += ' winexesvc64_exe_binary.c'
99
100 if winexesvc_binaries != '':
101     bld.SAMBA3_BINARY('winexe',
102                       source='winexe.c ' + winexesvc_binaries,
103                       deps='''
104                           popt
105                           samba-credentials
106                           LOADPARM_CTX
107                           libsmb
108                           msrpc3
109                       ''',
110                       enabled=bld.env.build_winexe)