| 1 |
#!/usr/bin/tclsh |
| 2 |
|
| 3 |
# |
| 4 |
# SRT - Secure, Reliable, Transport |
| 5 |
# Copyright (c) 2018 Haivision Systems Inc. |
| 6 |
# |
| 7 |
# This Source Code Form is subject to the terms of the Mozilla Public |
| 8 |
# License, v. 2.0. If a copy of the MPL was not distributed with this |
| 9 |
# file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 10 |
# |
| 11 |
|
| 12 |
# This is a general-purpose configure script, which is a user-friendly |
| 13 |
# wrapper to call the "cmake" command. |
| 14 |
|
| 15 |
# There are two options that are handled specifically: |
| 16 |
# |
| 17 |
# --help: show the list of official options |
| 18 |
# --prefix: alias to --cmake-install-prefix |
| 19 |
|
| 20 |
# The processing done automatically on all options by default is: |
| 21 |
# Every option like: |
| 22 |
# --long-c++-option |
| 23 |
# --cmake-special-option=ON |
| 24 |
# Turns into: |
| 25 |
# -DLONG_CXX_OPTION=1 |
| 26 |
# -DCMAKE_SPECIAL_OPTION=ON |
| 27 |
# |
| 28 |
# In the configuration file, "configure-data.tcl", you can add |
| 29 |
# special processing for options and define explicit options |
| 30 |
# in the "::options" dictionary. Explicit options (in contrast |
| 31 |
# to "blind" options) have additional properties: |
| 32 |
# |
| 33 |
# - only those options are mentioned with --help |
| 34 |
# - you can pass a value for this option without = character |
| 35 |
# - you can specify --disable-option instead of --enable-option=0 |
| 36 |
# |
| 37 |
# In "configure-data.tcl", beside ::options, you can define "preprocess" and |
| 38 |
# "postprocess" procedures. In "preprocess", use ::optval array to modify the |
| 39 |
# list of options to be processed further. Additionally in "postprocess" |
| 40 |
# procedure you can influence directly the options for "cmake" command in |
| 41 |
# ::cmakeopt variable (modifying ::optval in "postprocess" is useless). |
| 42 |
|
| 43 |
# The idea is that CMakeLists.txt contains things that are highly |
| 44 |
# customizable, but no system or option autodetection AWA "sensible |
| 45 |
# defaults" are provided. This is done by this script. |
| 46 |
|
| 47 |
|
| 48 |
set here [file dirname $argv0] |
| 49 |
|
| 50 |
set options "" |
| 51 |
set toolchain_changers "" |
| 52 |
|
| 53 |
source $here/configure-data.tcl |
| 54 |
|
| 55 |
# Update alias with default alias |
| 56 |
dict set alias --prefix --cmake-install-prefix= |
| 57 |
|
| 58 |
proc resolve opt { |
| 59 |
set type arg |
| 60 |
set pos [string first $opt =] |
| 61 |
if { $pos == -1 } { |
| 62 |
set type bool |
| 63 |
set mark "" |
| 64 |
} else { |
| 65 |
set type arg |
| 66 |
set mark [string range $opt $pos+1 end] |
| 67 |
set opt [string range $opt 0 $pos-1] |
| 68 |
} |
| 69 |
set var [string toupper [string map {- _ + x} $opt]] |
| 70 |
return [list --$opt $var $type $mark] |
| 71 |
} |
| 72 |
|
| 73 |
# Check if a --disable option has its --enable counterpart. If so, |
| 74 |
# then just invert the option. |
| 75 |
proc resolve_disablers {} { |
| 76 |
set enablers "" |
| 77 |
set optkeys_len [llength $::optkeys] |
| 78 |
for {set pos 0} {$pos < $optkeys_len} {incr pos} { |
| 79 |
set opt [lindex $::optkeys $pos] |
| 80 |
if { [string match --disable-* $opt] } { |
| 81 |
set inverted enable-[string range $opt 10 end] |
| 82 |
if { $inverted in [dict keys $::options] } { |
| 83 |
lset ::optkeys $pos --$inverted |
| 84 |
set val $::optval($opt) |
| 85 |
unset ::optval($opt) |
| 86 |
if { $val == "" || ![string is boolean $val] } { |
| 87 |
set ::optval(--$inverted) 0 |
| 88 |
} else { |
| 89 |
set ::optval(--$inverted) [expr {!$val}] |
| 90 |
} |
| 91 |
|
| 92 |
puts "NOTE: $opt changed into --$inverted=$::optval(--$inverted)" |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
foreach {o desc} $options { |
| 99 |
lassign [resolve $o] optname optvar opttype optmark |
| 100 |
set opt($optname) [list $optvar $opttype $optmark] |
| 101 |
set info($optname) $desc |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
if { $argv == "--help" || $argv == "-h" } { |
| 106 |
puts stderr "Usage: ./configure \[options\]" |
| 107 |
puts stderr "OPTIONS:" |
| 108 |
foreach o [lsort [array names opt]] { |
| 109 |
lassign $opt($o) unu type mark |
| 110 |
set imark "" |
| 111 |
if { $mark != "" } { |
| 112 |
set imark "=$mark" |
| 113 |
} |
| 114 |
puts stderr "\t$o$imark - $info($o)" |
| 115 |
} |
| 116 |
|
| 117 |
puts stderr "NOTE1: Option list may be incomplete. Refer to variables in CMakeLists.txt" |
| 118 |
puts stderr "NOTE2: Non-internal options turn e.g. --enable-c++11 into cmake -DENABLE_CXX11=1" |
| 119 |
puts stderr "NOTE3: You can use --disable-x instead of --enable-x=0 for the above options." |
| 120 |
|
| 121 |
exit 1 |
| 122 |
} |
| 123 |
|
| 124 |
if { [info proc init] != "" } { |
| 125 |
init |
| 126 |
} |
| 127 |
|
| 128 |
#parray opt |
| 129 |
|
| 130 |
set saveopt "" |
| 131 |
set optkeys "" |
| 132 |
|
| 133 |
set dryrun 0 |
| 134 |
set type "" |
| 135 |
|
| 136 |
foreach a $argv { |
| 137 |
if { [info exists val] } { unset val } |
| 138 |
|
| 139 |
if { $saveopt != "" } { |
| 140 |
set optval($saveopt) $a |
| 141 |
set saveopt "" |
| 142 |
continue |
| 143 |
} |
| 144 |
|
| 145 |
if { [string range $a 0 1] != "--" } { |
| 146 |
error "Unexpected argument '$a'. Options must start with --" |
| 147 |
} |
| 148 |
|
| 149 |
if { $a == "--dryrun" } { |
| 150 |
set dryrun 1 |
| 151 |
continue |
| 152 |
} |
| 153 |
|
| 154 |
set type "" |
| 155 |
|
| 156 |
if { [set a1 [string first = $a]] != -1 } { |
| 157 |
# Do not split. Options may include =. |
| 158 |
set val [string range $a $a1+1 end] |
| 159 |
set a [string range $a 0 $a1-1] |
| 160 |
} |
| 161 |
|
| 162 |
if { [dict exists $::alias $a] } { |
| 163 |
set aname [dict get $::alias $a] |
| 164 |
if { [string first = $aname] != -1 } { |
| 165 |
lassign [split $aname =] a aval |
| 166 |
set type arg |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
if { ![info exists opt($a)] } { |
| 171 |
#puts stderr "WARNING: Unknown option: $a" |
| 172 |
# But still, simply turn the option to assign-based use. |
| 173 |
lassign [resolve [string range $a 2 end]] oname var |
| 174 |
if { ![info exists val] && $type == "" } { |
| 175 |
set type bool |
| 176 |
} |
| 177 |
} else { |
| 178 |
lassign $opt($a) var type |
| 179 |
} |
| 180 |
|
| 181 |
if { $type == "bool" } { |
| 182 |
if { ![info exists val] } { |
| 183 |
set val 1 |
| 184 |
} |
| 185 |
set optval($a) $val |
| 186 |
} elseif { [info exists val] } { |
| 187 |
set optval($a) $val |
| 188 |
} else { |
| 189 |
set saveopt $a |
| 190 |
} |
| 191 |
|
| 192 |
lappend optkeys $a |
| 193 |
} |
| 194 |
|
| 195 |
if { $saveopt != "" } { |
| 196 |
error "Extra unhandled argument: $saveopt" |
| 197 |
} |
| 198 |
|
| 199 |
# Save the original call into config-status.sh |
| 200 |
|
| 201 |
set ofd [open config-status.sh w] |
| 202 |
puts $ofd "#!/bin/bash" |
| 203 |
puts -nonewline $ofd "$argv0 " |
| 204 |
foreach a $argv { |
| 205 |
set len 1 |
| 206 |
if {[catch {llength $a} len] || $len > 1 } { |
| 207 |
puts -nonewline $ofd "'$a' " |
| 208 |
} else { |
| 209 |
puts -nonewline $ofd "$a " |
| 210 |
} |
| 211 |
} |
| 212 |
puts $ofd "" |
| 213 |
close $ofd |
| 214 |
file attributes config-status.sh -permissions +x |
| 215 |
|
| 216 |
set cmakeopt "" |
| 217 |
|
| 218 |
resolve_disablers |
| 219 |
|
| 220 |
if { [info proc preprocess] != "" } { |
| 221 |
preprocess |
| 222 |
} |
| 223 |
|
| 224 |
# Check if there were new values added not added to optkeys |
| 225 |
foreach a [array names optval] { |
| 226 |
if { $a ni $optkeys } { |
| 227 |
lappend optkeys $a |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
foreach a $optkeys { |
| 233 |
|
| 234 |
if { ![info exists optval($a)] } { |
| 235 |
continue ;# user action might have removed it. |
| 236 |
} |
| 237 |
|
| 238 |
if { ![info exists opt($a)] } { |
| 239 |
#puts stderr "WARNING: Unknown option: $a" |
| 240 |
# But still, simply turn the option to assign-based use. |
| 241 |
lassign [resolve [string range $a 2 end]] oname var |
| 242 |
if { ![info exists val] && $type == "" } { |
| 243 |
set type bool |
| 244 |
} |
| 245 |
} else { |
| 246 |
lassign $opt($a) var type |
| 247 |
} |
| 248 |
|
| 249 |
set val $optval($a) |
| 250 |
lappend cmakeopt "-D$var=$val" |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
if { [info proc postprocess] != "" } { |
| 255 |
postprocess |
| 256 |
} |
| 257 |
|
| 258 |
#puts "VARSPEC: $cmakeopt" |
| 259 |
|
| 260 |
set cmd [list cmake $here {*}$cmakeopt] |
| 261 |
puts "Running: $cmd" |
| 262 |
if { !$dryrun} { |
| 263 |
if { [catch {exec 2>@stderr >@stdout {*}$cmd} result] } { |
| 264 |
puts "CONFIGURE: cmake reported error: $result" |
| 265 |
} |
| 266 |
} else { |
| 267 |
puts "(not really - dry run)" |
| 268 |
} |