#   Critcl configuration file
#
#   $Id: Config,v 1.12 2006/08/25 09:05:28 steve Exp $

# some defaults - you can override these in the platform specific section
# but shouldn't need to for typical *nix toolchains using gcc
#
#   platform        sets the platform (defaults to platform::generic)
#   compile         compile a C source file to an object file
#   version         print the compiler version number
#   link            link one or more object files and create a shared library
#   preproc_define  preprocess C source file (for critcl::cdefines)
#   preproc_enum    ditto
#   tclstubs        cflag to set USE_TCL_STUBS
#   tkstubs         cflag to set USE_TK_STUBS
#   debug_memory    cflag to enable memory debugging
#   debug_symbols   cflag to add symbols to resulting library
#   object          file extension for object files
#   output          flag to set output file
#   strip           cflag to tell linker to strip symbols
#   optimize        cflag to specify optimization level
#   include         cflag to add an include directory
#   noassert        cflag to turn off assertions in Tcl code
#   threadflags     cflags to enable threaded build
#   target          indicates that this is a cross-compile target
#   sharedlibext    the platform shared library extension
#
#   (Support for Fortran)
#   fcompile        compile a Fortran source file to an object file
#   fversion        print the Fortran compiler version number
#   flink           link one or more object files and create a shared library,
#                   if at least one object file comes from Fortran
#   foutput         Fortran flag to set output file
#   finclude        Fortran flag to add an include directory
#   fextra_cflags   Extra C flags for indicating type of Fortran compiler
#
# Any other config options are assumed to refer to Tcl variables and
# these are set when building so they can be used in the Critcl script.
# Typically this is used when defining cross-compile environments to set
# various tcl_platform() values.
#
# You can also set Tcl variables to use in "when" options (see the MacOSX
# section for an example - "set universal ..."). These commands and the
# "when" commands are run in a separate interpreter.
#
# The following will be needed/used once MS VC++ is working again
#   link_debug      not yet used
#   link_release    not yet used
#   ldoutput        not yet used

compile         gcc -c -fPIC
version         gcc -v
link            gcc -shared
include         -I
preproc_define  gcc -E -dM
preproc_enum    gcc -E
tclstubs        -DUSE_TCL_STUBS
tkstubs         -DUSE_TK_STUBS
debug_memory    -DTCL_MEM_DEBUG
debug_symbols   -g
object          .o
output          -o $outfile
ldoutput
link_debug
link_release
link_preload    --unresolved-symbols=ignore-in-shared-libs
strip           -Wl,-s
optimize        -O2
noassert        -DNDEBUG
threadflags     -DUSE_THREAD_ALLOC=1 -D_REENTRANT=1 -D_THREAD_SAFE=1    \
                -DHAVE_PTHREAD_ATTR_SETSTACKSIZE=1 -DHAVE_READDIR_R=1   \
                -DTCL_THREADS=1

# Fortran-specific flags
#
fcompile        g95 -c
fversion        g95 -v
flink           g95 -shared
finclude        -I
foutput         -o $outfile
foptimize       -O2
fextra_cflags   -DFTN_UNDERSCORE -D__unix

# platform specific stuff follows

# OSX - check if universal binaries supported by the installed toolchain

if {[string match macosx-* $platform]} {
    if {[info exists ::env(SDKROOT)] && $::env(SDKROOT) ne ""} {
        set SDKROOT $::env(SDKROOT)
    } else {
        # look for an SDK supporting universal binaries
        set pos [string length MacOSX]
	set sdklist {}
	set base /Developer
	catch {set base [exec xcode-select -print-path]}
        foreach dir [glob -nocomplain -tails \
			    -directory $base/SDKs MacOSX*] {
            set ver [string trimright \
                        [file rootname [string range $dir $pos end]] u]
            if {[package vcompare $ver 10.4] >= 0}  {
                # 10.4 is earliest supporting universal that we support
                set path($ver) $dir
                lappend sdklist $ver
            }
        }
        if {[llength $sdklist]} {
            set ver [lindex [lsort -command "package vcompare" $sdklist] 0]
            set SDKROOT $base/SDKs/$path($ver)
        } else {
            set SDKROOT  ""
        }
    }
    if {$SDKROOT ne ""} {
        # get the SDKsettings for the min OSX value supported
        if [catch {
                set info [exec defaults read $SDKROOT/SDKSettings buildSettings]
        }] {
            catch {
                set info [exec defaults read $SDKROOT/SDKSettings DefaultProperties -dict MACOSX_DEPLOYMENT_TARGET]
            }
        }
        set info [string trim $info]
        regsub -all {[\n"\;=\{\}]+} $info "" info
        regsub -all {\s+} $info " " info
        set osxmin [lindex $info 1]
    } else {
        # use the current platform
        foreach {v1 v2 v3} [split $::tcl_platform(osVersion) .] break
        incr v1 -4
        set osxmin 10.$v1.$v2
        # do we actually need to check if universal is supported, given the
        # gcc check below will do it for us?
        # set info [exec lipo -info /usr/lib/libSystem.dylib]
        # set plats [split [string trim [lindex [split $info :] 2]]]
    }
    set universal 0
    catch {
        set universal [expr {[exec gcc -v -arch ppc > /dev/null 2>@stdout] eq "" \
                && [exec gcc -v -arch i386 > /dev/null 2>@stdout] eq ""}]
    }
}

# default to universal binary if available
macosx-* when               $universal == 1
macosx-* platform           macosx-universal $osxmin macosx-powerpc macosx-ix86
macosx-* compile            gcc -c -arch i386 -arch ppc -isysroot $SDKROOT \
                                    -mmacosx-version-min=$osxmin
macosx-* link               gcc -bundle -arch i386 -arch ppc -isysroot $SDKROOT \
                                    -mmacosx-version-min=$osxmin
macosx-* link_preload       -undefined dynamic_lookup -mmacosx-version-min=10.3
macosx-* strip

# OSX ppc - when not universal or selected via -target
macosx-powerpc when         $universal != 1
macosx-powerpc compile      gcc -c -arch ppc
macosx-powerpc link         gcc -bundle -arch ppc
macosx-powerpc link_preload -undefined dynamic_lookup -mmacosx-version-min=10.3
macosx-powerpc strip

# OSX ix86
macosx-ix86 when            $universal != 1
macosx-ix86 compile         gcc -c -arch i386
macosx-ix86 link            gcc -bundle -arch i386
macosx-ix86 link_preload    -undefined dynamic_lookup -mmacosx-version-min=10.3
macosx-ix86 strip

# OSX ppc 64 bit
macosx-ppc64 when           $universal != 1
macosx-ppc64 compile        gcc -c -arch ppc64
macosx-ppc64 link           gcc -bundle -arch ppc64
macosx-ppc64 link_preload   -undefined dynamic_lookup
macosx-ppc64 strip

# OSX x86 64 bit
macosx-x86_64 when          $universal != 1
macosx-x86_64 compile       gcc -c -arch x86_64
macosx-x86_64 link          gcc -bundle -arch x86_64
macosx-x86_64 link_preload  -undefined dynamic_lookup
macosx-x86_64 strip

# Linux - 32 bit or 64 bit build - select using "-target" if you don't want
#         the platform default (32 on 32, 64 on 64)
linux-32-*  compile   gcc -c -m32
linux-64-*  compile   gcc -c -m64

# Windows - using MSVC++ 6.0
#
# Note: the language option for cl is -TC for c and -TP for c++ or
#       it can treat single files -Tc<filename>
#
win32-ix86-cl  when            [auto_execok cl] ne ""
win32-ix86-cl  compile         cl -nologo -c
win32-ix86-cl  link            link -nologo
win32-ix86-cl  preproc_define  cl -nologo -E
win32-ix86-cl  preproc_enum    cl -nologo -E
win32-ix86-cl  object          .obj
win32-ix86-cl  debug_symbols   -W3 -Od -Zi -GZ -MDd -D_DEBUG
win32-ix86-cl  optimize        -W3 -O2 -Op -Gs -MD
win32-ix86-cl  output          -Fo$outfile
win32-ix86-cl  ldoutput        -dll -out:$outfile
win32-ix86-cl  link_debug      -debug:full -debugtype:cv
win32-ix86-cl  link_release    -release -opt:ref -opt:icf,3 -ws:aggressive
win32-ix86-cl  link_preload
win32-ix86-cl  strip
win32-ix86-cl  version         cl

# Cross-compile for Windows using Xmingwin
mingw32     target         win32-ix86
mingw32     compile        gcc -c -nostdlib
mingw32     link           gcc -shared
mingw32     link_preload
mingw32     sharedlibext  .dll
mingw32     tcl_platform(byteOrder)  littleEndian
mingw32     tcl_platform(machine)    intel
mingw32     tcl_platform(os)         Windows NT
mingw32     tcl_platform(osVersion)  5.0
mingw32     tcl_platform(platform)   windows
mingw32     tcl_platform(wordSize)   4

# Cross-compile for ARM (n770/Zaurus/etc) using Scratchbox et al
linux-arm   target
linux-arm   sharedlibext .so
linux-arm   tcl_platform(byteOrder)  littleEndian
linux-arm   tcl_platform(machine)    arm
linux-arm   tcl_platform(os)         Linux
linux-arm   tcl_platform(osVersion)  2.6
linux-arm   tcl_platform(platform)   unix
linux-arm   tcl_platform(wordSize)   4
