1 # Copyright (c) 2010 WorkWare Systems http://www.workware.net.au/
6 # This module supports common system interrogation and options
7 # such as --host, --build, --prefix, and setting srcdir, builddir, and EXEXT.
9 # It also support the 'feature' naming convention, where searching
10 # for a feature such as sys/type.h defines HAVE_SYS_TYPES_H
13 host
:host-alias
=> {a complete or partial cpu-vendor-opsys
for the system where
14 the application will run
(defaults to the same value as
--build)}
15 build
:build-alias
=> {a complete or partial cpu-vendor-opsys
for the system
16 where the application will be built
(defaults to the
17 result of running config.guess
)}
18 prefix
:dir
=> {the target directory
for the build
(defaults to
/usr
/local
)}
20 # These (hidden) options are supported for autoconf/automake compatibility
37 # Returns 1 if exists, or 0 if not
39 proc check-feature
{name code
} {
40 msg-checking
"Checking for $name..."
41 set r
[uplevel 1 $code]
42 define-feature
$name $r
46 msg-result
"not found"
51 # @have-feature name ?default=0?
53 # Returns the value of the feature if defined, or $default if not.
54 # See 'feature-define-name' for how the feature name
55 # is translated into the define name.
57 proc have-feature
{name
{default 0}} {
58 get-define
[feature-define-name
$name] $default
61 # @define-feature name ?value=1?
63 # Sets the feature 'define' to the given value.
64 # See 'feature-define-name' for how the feature name
65 # is translated into the define name.
67 proc define-feature
{name
{value
1}} {
68 define
[feature-define-name
$name] $value
71 # @feature-checked name
73 # Returns 1 if the feature has been checked, whether true or not
75 proc feature-checked
{name
} {
76 is-defined
[feature-define-name
$name]
79 # @feature-define-name name ?prefix=HAVE_?
81 # Converts a name to the corresponding define,
82 # e.g. sys/stat.h becomes HAVE_SYS_STAT_H.
84 # Converts * to P and all non-alphanumeric to underscore.
86 proc feature-define-name
{name
{prefix HAVE_
}} {
87 string toupper
$prefix[regsub -all {[^a-zA-Z0-9
]} [regsub -all {[*]} $name p
] _
]
90 # If $file doesn't exist, or it's contents are different than $buf,
91 # the file is written and $script is executed.
92 # Otherwise a "file is unchanged" message is displayed.
93 proc write-if-changed
{file buf
{script
{}}} {
94 set old
[readfile
$file ""]
95 if {$old eq
$buf && [file exists
$file]} {
96 msg-result
"$file is unchanged"
98 writefile
$file $buf\n
103 # @make-template template ?outfile?
105 # Reads the input file <srcdir>/$template and writes the output file $outfile.
106 # If $outfile is blank/omitted, $template should end with ".in" which
107 # is removed to create the output file name.
109 # Each pattern of the form @define@ is replaced the the corresponding
110 # define, if it exists, or left unchanged if not.
112 # The special value @srcdir@ is subsituted with the relative
113 # path to the source directory from the directory where the output
114 # file is created. Use @top_srcdir@ for the absolute path.
116 # Conditional sections may be specified as follows:
123 # Where 'name' is a defined variable name and @else is optional.
124 # If the expression does not match, all lines through '@endif' are ignored.
126 # The alternative forms may also be used:
130 # Where the first form is true if the variable is defined, but not empty or 0
132 # Currently these expressions can't be nested.
134 proc make-template
{template
{out
{}}} {
135 set infile
[file join $::autosetup(srcdir
) $template]
137 if {![file exists
$infile]} {
138 user-error
"Template $template is missing"
141 # Define this as late as possible
142 define AUTODEPS
$::autosetup(deps
)
145 if {[file ext
$template] ne
".in"} {
146 autosetup-error
"make_template $template has no target file and can't guess"
148 set out
[file rootname
$template]
151 set outdir
[file dirname
$out]
153 # Make sure the directory exists
156 # Set up srcdir to be relative to the target dir
157 define srcdir
[relative-path
[file join $::autosetup(srcdir
) $outdir] $outdir]
160 foreach {n v
} [array get
::define] {
161 lappend mapping
@$n@ $v
164 foreach line
[split [readfile
$infile] \n] {
165 if {[info exists cond
]} {
166 set l
[string trimright
$line]
167 if {$l eq
"@endif"} {
172 set cond
[expr {!$cond}]
180 if {[regexp {^
@if\s
+(\w
+)(.
*)} $line -> name expression
]} {
181 lassign
$expression equal value
182 set varval
[get-define
$name ""]
184 set cond
[expr {$varval ni
{"" 0}}]
186 set cond
[expr {$varval eq
$value}]
187 if {$equal ne
"=="} {
188 set cond
[expr {!$cond}]
195 writefile
$out [string map
$mapping [join $result \n]]\n
197 msg-result
"Created [relative-path $out] from [relative-path $template]"
200 # build/host tuples and cross-compilation prefix
201 set build
[opt-val build
]
202 define build_alias
$build
204 define build
[config_guess
]
206 define build
[config_sub
$build]
209 set host
[opt-val host
]
210 define host_alias
$host
212 define host
[get-define build
]
215 define host
[config_sub
$host]
218 define cross
[get-env CROSS
$cross]
220 # Do "define defaultprefix myvalue" to set the default prefix *before* the first "use"
221 set prefix
[opt-val prefix
[get-define defaultprefix
/usr
/local
]]
223 # These are for compatibility with autoconf
224 define target
[get-define host
]
225 define prefix
$prefix
226 define builddir
$autosetup(builddir
)
227 define srcdir
$autosetup(srcdir
)
228 # Allow this to come from the environment
229 define top_srcdir
[get-env top_srcdir
[get-define srcdir
]]
231 # autoconf supports all of these
232 set exec_prefix
[opt-val exec-prefix
$prefix]
233 define exec_prefix
$exec_prefix
234 foreach {name defpath
} {
240 define
$name [opt-val
$name $exec_prefix$defpath]
242 foreach {name defpath
} {
251 define
$name [opt-val
$name $prefix$defpath]
254 define SHELL
[get-env SHELL
[find-an-executable sh bash ksh
]]
256 # Windows vs. non-Windows
257 switch -glob -- [get-define host
] {
258 *-*-ming* - *-*-cygwin - *-*-msys {
259 define-feature windows
268 msg-result
"Host System...[get-define host]"
269 msg-result
"Build System...[get-define build]"