zlib: Don't use PASTE for INTMAX error messages
[jimtcl.git] / autosetup / system.tcl
blob9d9cb390072318bb92ef31d8aa61388cf9ce1968
1 # Copyright (c) 2010 WorkWare Systems http://www.workware.net.au/
2 # All rights reserved
4 # @synopsis:
6 # This module supports common system interrogation and options
7 # such as --host, --build, --prefix, and setting srcdir, builddir, and EXEEXT
9 # It also support the 'feature' naming convention, where searching
10 # for a feature such as sys/type.h defines HAVE_SYS_TYPES_H
12 module-options {
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
21 exec-prefix:
22 bindir:
23 sbindir:
24 includedir:
25 mandir:
26 infodir:
27 libexecdir:
28 datadir:
29 libdir:
30 sysconfdir:
31 sharedstatedir:
32 localstatedir:
33 maintainer-mode=0
34 dependency-tracking=0
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
43 if {$r} {
44 msg-result "ok"
45 } else {
46 msg-result "not found"
48 return $r
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"
97 } else {
98 writefile $file $buf\n
99 uplevel 1 $script
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 with the corresponding
110 # define, if it exists, or left unchanged if not.
112 # The special value @srcdir@ is substituted with the relative
113 # path to the source directory from the directory where the output
114 # file is created, while the special value @top_srcdir@ is substituted
115 # with the relative path to the top level source directory.
117 # Conditional sections may be specified as follows:
118 ## @if name == value
119 ## lines
120 ## @else
121 ## lines
122 ## @endif
124 # Where 'name' is a defined variable name and @else is optional.
125 # If the expression does not match, all lines through '@endif' are ignored.
127 # The alternative forms may also be used:
128 ## @if name
129 ## @if name != value
131 # Where the first form is true if the variable is defined, but not empty or 0
133 # Currently these expressions can't be nested.
135 proc make-template {template {out {}}} {
136 set infile [file join $::autosetup(srcdir) $template]
138 if {![file exists $infile]} {
139 user-error "Template $template is missing"
142 # Define this as late as possible
143 define AUTODEPS $::autosetup(deps)
145 if {$out eq ""} {
146 if {[file ext $template] ne ".in"} {
147 autosetup-error "make_template $template has no target file and can't guess"
149 set out [file rootname $template]
152 set outdir [file dirname $out]
154 # Make sure the directory exists
155 file mkdir $outdir
157 # Set up srcdir and top_srcdir to be relative to the target dir
158 define srcdir [relative-path [file join $::autosetup(srcdir) $outdir] $outdir]
159 define top_srcdir [relative-path $::autosetup(srcdir) $outdir]
161 set mapping {}
162 foreach {n v} [array get ::define] {
163 lappend mapping @$n@ $v
165 set result {}
166 foreach line [split [readfile $infile] \n] {
167 if {[info exists cond]} {
168 set l [string trimright $line]
169 if {$l eq "@endif"} {
170 unset cond
171 continue
173 if {$l eq "@else"} {
174 set cond [expr {!$cond}]
175 continue
177 if {$cond} {
178 lappend result $line
180 continue
182 if {[regexp {^@if\s+(\w+)(.*)} $line -> name expression]} {
183 lassign $expression equal value
184 set varval [get-define $name ""]
185 if {$equal eq ""} {
186 set cond [expr {$varval ni {"" 0}}]
187 } else {
188 set cond [expr {$varval eq $value}]
189 if {$equal ne "=="} {
190 set cond [expr {!$cond}]
193 continue
195 lappend result $line
197 writefile $out [string map $mapping [join $result \n]]\n
199 msg-result "Created [relative-path $out] from [relative-path $template]"
202 # build/host tuples and cross-compilation prefix
203 set build [opt-val build]
204 define build_alias $build
205 if {$build eq ""} {
206 define build [config_guess]
207 } else {
208 define build [config_sub $build]
211 set host [opt-val host]
212 define host_alias $host
213 if {$host eq ""} {
214 define host [get-define build]
215 set cross ""
216 } else {
217 define host [config_sub $host]
218 set cross $host-
220 define cross [get-env CROSS $cross]
222 # Do "define defaultprefix myvalue" to set the default prefix *before* the first "use"
223 set prefix [opt-val prefix [get-define defaultprefix /usr/local]]
225 # These are for compatibility with autoconf
226 define target [get-define host]
227 define prefix $prefix
228 define builddir $autosetup(builddir)
229 define srcdir $autosetup(srcdir)
230 # Allow this to come from the environment
231 define top_srcdir [get-env top_srcdir [get-define srcdir]]
233 # autoconf supports all of these
234 set exec_prefix [opt-val exec-prefix $prefix]
235 define exec_prefix $exec_prefix
236 foreach {name defpath} {
237 bindir /bin
238 sbindir /sbin
239 libexecdir /libexec
240 libdir /lib
242 define $name [opt-val $name $exec_prefix$defpath]
244 foreach {name defpath} {
245 datadir /share
246 sysconfdir /etc
247 sharedstatedir /com
248 localstatedir /var
249 infodir /share/info
250 mandir /share/man
251 includedir /include
253 define $name [opt-val $name $prefix$defpath]
256 define SHELL [get-env SHELL [find-an-executable sh bash ksh]]
258 # Windows vs. non-Windows
259 switch -glob -- [get-define host] {
260 *-*-ming* - *-*-cygwin - *-*-msys {
261 define-feature windows
262 define EXEEXT .exe
264 default {
265 define EXEEXT ""
269 # Display
270 msg-result "Host System...[get-define host]"
271 msg-result "Build System...[get-define build]"