3 # WARNING! Do not edit! https://waf.io/book/index.html#_obtaining_the_waf_file
6 # -*- coding: utf-8 vi:ts=4:noexpandtab
7 # Tool to provide dedicated variables for cross-compilation
9 __author__ = __maintainer__ = "Jérôme Carretero <cJ-waf@zougloub.eu>"
10 __copyright__ = "Jérôme Carretero, 2014"
13 This tool allows to use environment variables to define cross-compilation
14 variables intended for build variants.
16 The variables are obtained from the environment in 3 ways:
18 1. By defining CHOST, they can be derived as ${CHOST}-${TOOL}
20 3. By defining ${CHOST//-/_}_x
22 else one can set ``cfg.env.CHOST`` in ``wscript`` before loading ``cross_gnu``.
26 - In your build script::
30 for variant in x_variants:
32 conf.load('cross_gnu')
33 conf.xcheck_host_var('POUET')
39 CHOST=arm-hardfloat-linux-gnueabi waf configure
40 env arm-hardfloat-linux-gnueabi-CC="clang -..." waf configure
41 CFLAGS=... CHOST=arm-hardfloat-linux-gnueabi HOST_CFLAGS=-g waf configure
42 HOST_CC="clang -..." waf configure
44 This example ``wscript`` compiles to Microchip PIC (xc16-gcc-xyz must be in PATH):
48 from waflib import Configure
50 #from https://gist.github.com/rpuntaie/2bddfb5d7b77db26415ee14371289971
53 variants='pc fw/variant1 fw/variant2'.split()
58 PIC = '33FJ128GP804' #dsPICxxx
61 def gcc_modifier_xc16(cfg):
63 v.cprogram_PATTERN = '%s.elf'
64 v.LINKFLAGS_cprogram = ','.join(['-Wl','','','--defsym=__MPLAB_BUILD=0','','--script=p'+PIC+'.gld',
65 '--stack=16','--check-sections','--data-init','--pack-data','--handles','--isr','--no-gc-sections',
66 '--fill-upper=0','--stackguard=16','--no-force-link','--smart-io']) #,'--report-mem'])
67 v.CFLAGS_cprogram=['-mcpu='+PIC,'-omf=elf','-mlarge-code','-msmart-io=1',
68 '-msfr-warn=off','-mno-override-inline','-finline','-Winline']
71 if 'fw' in cfg.variant: #firmware
72 cfg.env.DEST_OS = 'xc16' #cfg.env.CHOST = 'xc16' #works too
73 cfg.load('c cross_gnu') #cfg.env.CHOST becomes ['xc16']
75 else: #configure for pc SW
79 if 'fw' in bld.variant: #firmware
80 bld.program(source='maintst.c', target='maintst');
81 bld(source='maintst.elf', target='maintst.hex', rule="xc16-bin2hex ${SRC} -a -omf=elf")
82 else: #build for pc SW
88 from waflib import Utils, Configure
89 from waflib.Tools import ccroot, gcc
92 from shlex import quote
94 from pipes import quote
96 def get_chost_stuff(conf):
98 Get the CHOST environment variable contents
103 chost = conf.env.CHOST[0]
104 chost_envar = chost.replace('-', '_')
105 return chost, chost_envar
109 def xcheck_var(conf, name, wafname=None, cross=False):
110 wafname = wafname or name
112 if wafname in conf.env:
113 value = conf.env[wafname]
114 if isinstance(value, str):
117 envar = os.environ.get(name)
120 value = Utils.to_list(envar) if envar != '' else [envar]
122 conf.env[wafname] = value
124 pretty = 'cross-compilation %s' % wafname
127 conf.msg('Will use %s' % pretty, " ".join(quote(x) for x in value))
130 def xcheck_host_prog(conf, name, tool, wafname=None):
131 wafname = wafname or name
133 chost, chost_envar = get_chost_stuff(conf)
137 specific = os.environ.get('%s_%s' % (chost_envar, name))
140 value = Utils.to_list(specific)
141 conf.env[wafname] += value
142 conf.msg('Will use cross-compilation %s from %s_%s' % (name, chost_envar, name),
143 " ".join(quote(x) for x in value))
146 envar = os.environ.get('HOST_%s' % name)
147 if envar is not None:
148 value = Utils.to_list(envar)
149 conf.env[wafname] = value
150 conf.msg('Will use cross-compilation %s from HOST_%s' % (name, name),
151 " ".join(quote(x) for x in value))
154 if conf.env[wafname]:
159 value = '%s-%s' % (chost, tool)
162 conf.env[wafname] = value
163 conf.msg('Will use cross-compilation %s from CHOST' % wafname, value)
166 def xcheck_host_envar(conf, name, wafname=None):
167 wafname = wafname or name
169 chost, chost_envar = get_chost_stuff(conf)
173 specific = os.environ.get('%s_%s' % (chost_envar, name))
176 value = Utils.to_list(specific)
177 conf.env[wafname] += value
178 conf.msg('Will use cross-compilation %s from %s_%s' \
179 % (name, chost_envar, name),
180 " ".join(quote(x) for x in value))
184 envar = os.environ.get('HOST_%s' % name)
188 value = Utils.to_list(envar) if envar != '' else [envar]
190 conf.env[wafname] = value
191 conf.msg('Will use cross-compilation %s from HOST_%s' % (name, name),
192 " ".join(quote(x) for x in value))
196 def xcheck_host(conf):
197 conf.xcheck_var('CHOST', cross=True)
198 conf.env.CHOST = conf.env.CHOST or [conf.env.DEST_OS]
199 conf.env.DEST_OS = conf.env.CHOST[0].replace('-','_')
200 conf.xcheck_host_prog('CC', 'gcc')
201 conf.xcheck_host_prog('CXX', 'g++')
202 conf.xcheck_host_prog('LINK_CC', 'gcc')
203 conf.xcheck_host_prog('LINK_CXX', 'g++')
204 conf.xcheck_host_prog('AR', 'ar')
205 conf.xcheck_host_prog('AS', 'as')
206 conf.xcheck_host_prog('LD', 'ld')
207 conf.xcheck_host_envar('CFLAGS')
208 conf.xcheck_host_envar('CXXFLAGS')
209 conf.xcheck_host_envar('LDFLAGS', 'LINKFLAGS')
210 conf.xcheck_host_envar('LIB')
211 conf.xcheck_host_envar('PKG_CONFIG_LIBDIR')
212 conf.xcheck_host_envar('PKG_CONFIG_PATH')
216 conf.env.env.update(os.environ)
217 if conf.env.PKG_CONFIG_LIBDIR:
218 conf.env.env['PKG_CONFIG_LIBDIR'] = conf.env.PKG_CONFIG_LIBDIR[0]
219 if conf.env.PKG_CONFIG_PATH:
220 conf.env.env['PKG_CONFIG_PATH'] = conf.env.PKG_CONFIG_PATH[0]
224 Configuration example for gcc, it will not work for g++/clang/clang++
227 conf.gcc_common_flags()
228 conf.gcc_modifier_platform()
231 conf.link_add_flags()