2 # -*- coding: utf-8 vi:ts=4:noexpandtab
3 # Tool to provide dedicated variables for cross-compilation
5 __author__ = __maintainer__ = "Jérôme Carretero <cJ-waf@zougloub.eu>"
6 __copyright__ = "Jérôme Carretero, 2014"
9 This tool allows to use environment variables to define cross-compilation
10 variables intended for build variants.
12 The variables are obtained from the environment in 3 ways:
14 1. By defining CHOST, they can be derived as ${CHOST}-${TOOL}
16 3. By defining ${CHOST//-/_}_x
18 else one can set ``cfg.env.CHOST`` in ``wscript`` before loading ``cross_gnu``.
22 - In your build script::
26 for variant in x_variants:
28 conf.load('cross_gnu')
29 conf.xcheck_host_var('POUET')
35 CHOST=arm-hardfloat-linux-gnueabi waf configure
36 env arm-hardfloat-linux-gnueabi-CC="clang -..." waf configure
37 CFLAGS=... CHOST=arm-hardfloat-linux-gnueabi HOST_CFLAGS=-g waf configure
38 HOST_CC="clang -..." waf configure
40 This example ``wscript`` compiles to Microchip PIC (xc16-gcc-xyz must be in PATH):
44 from waflib import Configure
46 #from https://gist.github.com/rpuntaie/2bddfb5d7b77db26415ee14371289971
49 variants='pc fw/variant1 fw/variant2'.split()
54 PIC = '33FJ128GP804' #dsPICxxx
57 def gcc_modifier_xc16(cfg):
59 v.cprogram_PATTERN = '%s.elf'
60 v.LINKFLAGS_cprogram = ','.join(['-Wl','','','--defsym=__MPLAB_BUILD=0','','--script=p'+PIC+'.gld',
61 '--stack=16','--check-sections','--data-init','--pack-data','--handles','--isr','--no-gc-sections',
62 '--fill-upper=0','--stackguard=16','--no-force-link','--smart-io']) #,'--report-mem'])
63 v.CFLAGS_cprogram=['-mcpu='+PIC,'-omf=elf','-mlarge-code','-msmart-io=1',
64 '-msfr-warn=off','-mno-override-inline','-finline','-Winline']
67 if 'fw' in cfg.variant: #firmware
68 cfg.env.DEST_OS = 'xc16' #cfg.env.CHOST = 'xc16' #works too
69 cfg.load('c cross_gnu') #cfg.env.CHOST becomes ['xc16']
71 else: #configure for pc SW
75 if 'fw' in bld.variant: #firmware
76 bld.program(source='maintst.c', target='maintst');
77 bld(source='maintst.elf', target='maintst.hex', rule="xc16-bin2hex ${SRC} -a -omf=elf")
78 else: #build for pc SW
84 from waflib import Utils, Configure
85 from waflib.Tools import ccroot, gcc
88 from shlex import quote
90 from pipes import quote
92 def get_chost_stuff(conf):
94 Get the CHOST environment variable contents
99 chost = conf.env.CHOST[0]
100 chost_envar = chost.replace('-', '_')
101 return chost, chost_envar
105 def xcheck_var(conf, name, wafname=None, cross=False):
106 wafname = wafname or name
108 if wafname in conf.env:
109 value = conf.env[wafname]
110 if isinstance(value, str):
113 envar = os.environ.get(name)
116 value = Utils.to_list(envar) if envar != '' else [envar]
118 conf.env[wafname] = value
120 pretty = 'cross-compilation %s' % wafname
123 conf.msg('Will use %s' % pretty, " ".join(quote(x) for x in value))
126 def xcheck_host_prog(conf, name, tool, wafname=None):
127 wafname = wafname or name
129 chost, chost_envar = get_chost_stuff(conf)
133 specific = os.environ.get('%s_%s' % (chost_envar, name))
136 value = Utils.to_list(specific)
137 conf.env[wafname] += value
138 conf.msg('Will use cross-compilation %s from %s_%s' % (name, chost_envar, name),
139 " ".join(quote(x) for x in value))
142 envar = os.environ.get('HOST_%s' % name)
143 if envar is not None:
144 value = Utils.to_list(envar)
145 conf.env[wafname] = value
146 conf.msg('Will use cross-compilation %s from HOST_%s' % (name, name),
147 " ".join(quote(x) for x in value))
150 if conf.env[wafname]:
155 value = '%s-%s' % (chost, tool)
158 conf.env[wafname] = value
159 conf.msg('Will use cross-compilation %s from CHOST' % wafname, value)
162 def xcheck_host_envar(conf, name, wafname=None):
163 wafname = wafname or name
165 chost, chost_envar = get_chost_stuff(conf)
169 specific = os.environ.get('%s_%s' % (chost_envar, name))
172 value = Utils.to_list(specific)
173 conf.env[wafname] += value
174 conf.msg('Will use cross-compilation %s from %s_%s' \
175 % (name, chost_envar, name),
176 " ".join(quote(x) for x in value))
180 envar = os.environ.get('HOST_%s' % name)
184 value = Utils.to_list(envar) if envar != '' else [envar]
186 conf.env[wafname] = value
187 conf.msg('Will use cross-compilation %s from HOST_%s' % (name, name),
188 " ".join(quote(x) for x in value))
192 def xcheck_host(conf):
193 conf.xcheck_var('CHOST', cross=True)
194 conf.env.CHOST = conf.env.CHOST or [conf.env.DEST_OS]
195 conf.env.DEST_OS = conf.env.CHOST[0].replace('-','_')
196 conf.xcheck_host_prog('CC', 'gcc')
197 conf.xcheck_host_prog('CXX', 'g++')
198 conf.xcheck_host_prog('LINK_CC', 'gcc')
199 conf.xcheck_host_prog('LINK_CXX', 'g++')
200 conf.xcheck_host_prog('AR', 'ar')
201 conf.xcheck_host_prog('AS', 'as')
202 conf.xcheck_host_prog('LD', 'ld')
203 conf.xcheck_host_envar('CFLAGS')
204 conf.xcheck_host_envar('CXXFLAGS')
205 conf.xcheck_host_envar('LDFLAGS', 'LINKFLAGS')
206 conf.xcheck_host_envar('LIB')
207 conf.xcheck_host_envar('PKG_CONFIG_LIBDIR')
208 conf.xcheck_host_envar('PKG_CONFIG_PATH')
212 conf.env.env.update(os.environ)
213 if conf.env.PKG_CONFIG_LIBDIR:
214 conf.env.env['PKG_CONFIG_LIBDIR'] = conf.env.PKG_CONFIG_LIBDIR[0]
215 if conf.env.PKG_CONFIG_PATH:
216 conf.env.env['PKG_CONFIG_PATH'] = conf.env.PKG_CONFIG_PATH[0]
220 Configuration example for gcc, it will not work for g++/clang/clang++
223 conf.gcc_common_flags()
224 conf.gcc_modifier_platform()
227 conf.link_add_flags()