├── .gitignore ├── LICENSE ├── Makefile.am ├── README.md ├── autogen.sh ├── benchmarks └── cw_nim.nim ├── configure.ac ├── golib.nimble ├── nim.cfg.in ├── src └── golib.nim └── tests ├── bug234.go ├── bug234.nim ├── gc ├── bintrees.nim ├── closureleak.nim ├── cyclecollector.nim ├── cycleleak.nim ├── gcbench.nim ├── gcleak.nim ├── gcleak2.nim ├── gcleak3.nim ├── gcleak4.nim ├── gcleak5.nim ├── gctest.nim ├── growobjcrash.nim ├── refarrayleak.nim └── stackrefleak.nim ├── peano.go ├── peano.nim ├── perm.go ├── perm.nim ├── select.go ├── select.nim ├── select7.go ├── select7.nim ├── select_custom.go ├── select_custom.nim ├── sieve1.go ├── sieve1.nim ├── test1.nim ├── test2.nim └── tnew.nim /.gitignore: -------------------------------------------------------------------------------- 1 | # Object files 2 | *.o 3 | *.ko 4 | *.obj 5 | *.elf 6 | build 7 | 8 | # Libraries 9 | *.lib 10 | *.a 11 | *.lo 12 | *.la 13 | 14 | # Shared objects (inc. Windows DLLs) 15 | *.dll 16 | *.so 17 | *.so.* 18 | *.dylib 19 | 20 | # Executables 21 | *.exe 22 | *.out 23 | *.app 24 | *.i*86 25 | *.x86_64 26 | *.hex 27 | 28 | # autotools 29 | Makefile 30 | Makefile.in 31 | /autom4te.cache 32 | /aclocal.m4 33 | /compile 34 | /configure 35 | /depcomp 36 | /install-sh 37 | /missing 38 | /stamp-h1 39 | /config.* 40 | /libtool 41 | /ltmain.sh 42 | /test-driver 43 | /test-suite.log 44 | .libs 45 | .deps 46 | .dirstamp 47 | *.log 48 | *.trs 49 | *.tar.gz 50 | 51 | # editors 52 | *.*~ 53 | *.swp 54 | *.swo 55 | 56 | vim.session 57 | nimcache 58 | /benchmarks/cw_nim 59 | /tests/test1 60 | /tests/test2 61 | /tests/bug234 62 | /tests/select 63 | /tests/select7 64 | /tests/select_custom 65 | /tests/sieve1 66 | /tests/peano 67 | /tests/gc/gcbench 68 | /tests/gc/gctest 69 | /tests/gc/bintrees 70 | /tests/gc/closureleak 71 | /tests/gc/cyclecollector 72 | /tests/gc/cycleleak 73 | /tests/gc/gcleak 74 | /tests/gc/gcleak2 75 | /tests/gc/gcleak3 76 | /tests/gc/gcleak4 77 | /tests/gc/gcleak5 78 | /tests/gc/growobjcrash 79 | /tests/gc/refarrayleak 80 | /tests/gc/stackrefleak 81 | /tests/tnew 82 | /tests/perm 83 | /nim.cfg 84 | 85 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2015, Ștefan Talpalaru 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | 1. Redistributions of source code must retain the above copyright notice, this 8 | list of conditions and the following disclaimer. 9 | 2. Redistributions in binary form must reproduce the above copyright notice, 10 | this list of conditions and the following disclaimer in the documentation 11 | and/or other materials provided with the distribution. 12 | 13 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 14 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 15 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 16 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 17 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 18 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 19 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 20 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 21 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 22 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23 | 24 | -------------------------------------------------------------------------------- /Makefile.am: -------------------------------------------------------------------------------- 1 | %: %.nim nim.cfg src/golib.nim 2 | $(NIM) c --verbosity:2 -d:release $< 3 | 4 | check_DATA = tests/bug234 \ 5 | tests/tnew \ 6 | tests/select \ 7 | tests/select7 \ 8 | tests/peano \ 9 | tests/select_custom \ 10 | tests/sieve1 \ 11 | tests/gc/gcbench \ 12 | tests/gc/gctest \ 13 | tests/gc/bintrees \ 14 | tests/gc/closureleak \ 15 | tests/gc/cyclecollector \ 16 | tests/gc/cycleleak \ 17 | tests/gc/gcleak \ 18 | tests/gc/gcleak2 \ 19 | tests/gc/gcleak3 \ 20 | tests/gc/refarrayleak \ 21 | tests/gc/stackrefleak \ 22 | tests/gc/gcleak4 \ 23 | tests/gc/gcleak5 \ 24 | tests/gc/growobjcrash 25 | 26 | TESTS = $(check_DATA) 27 | 28 | noinst_DATA = benchmarks/cw_nim 29 | 30 | MOSTLYCLEANFILES := $(check_DATA) $(noinst_DATA) 31 | 32 | clean-local: 33 | -rm -rf benchmarks/nimcache tests/nimcache tests/*/nimcache 34 | 35 | EXTRA_DIST := tests/*.go \ 36 | tests/*.nim \ 37 | tests/*/*.nim \ 38 | benchmarks/*.nim \ 39 | benchmarks/*.nim.cfg \ 40 | autogen.sh \ 41 | nim.cfg.in \ 42 | LICENSE \ 43 | README.md 44 | 45 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## description 2 | 3 | [Nim language][1] bindings for [golib][2] - a library that (ab)uses gccgo to bring Go's channels and goroutines to the rest of the world. 4 | 5 | ## syntax comparison 6 | 7 | | feature | Go | Nim | 8 | |---------|----|-----| 9 | | channel type
(here in a
function
parameter) | ``` func S(a, b chan uint) int { ``` | ``` proc S(a, b: chan[uint]): int = ``` | 10 | | restricted
channel types | ``` chan<- float64 ```
``` <-chan int ``` | ```send_chan[float64]```
```recv_chan[int]``` | 11 | | create channel | ``` c := make(chan int) ```
``` c2 := make(chan int, 1) ``` | ``` var c = make_chan(int) ```
``` var c2 = make_chan(int, 1) ``` | 12 | | send value
to channel | ``` c <- 1 ``` | ``` c <- 1 ``` | 13 | | receive value
from channel | ``` av := <-a ```
``` av, bv := <-a, <-b ```
``` cv, ok := <-c ``` | ``` var av = <-a ```
``` var (av, bv) = (<-a, <-b) ```
``` var (cv, ok) = <--c ``` | 14 | | iterate
over channel | ``` for av := range a ``` | ``` for av in a ``` | 15 | | channel select | ``` select { ```
``` case c0 <- 0: ```
``` case <-c1: ```
``` case i2 = <-c2: ```
``` case i3, ok3 = <-c3: ```
``` case li[0] = <-c4: ```
``` case li[f()] = <-c5: ```
``` default: ```
``` break LOOP ```
``` } ``` | ``` select: ```
``` scase c0 <- 0: discard ```
``` scase <-c1: discard ```
``` scase (i2 = <-c2): discard ```
``` scase ((i3, ok3) = <--c3): discard ```
``` scase (li[0] = <-c4): discard ```
``` scase (li[f()] = <-c5): discard ```
``` default: ```
``` break LOOP ``` | 16 | | declare goroutine | ``` func f(x, y int) { ```
``` println(x, y) ```
``` } ``` | ``` proc f(x, y: int) {.goroutine.} = ```
``` echo(x, " ", y) ``` | 17 | | launch goroutine | ``` go f(1, 2) ``` | ``` go f(1, 2) ``` | 18 | | lambda goroutine | ``` go func(c chan int) { c <- 1 }(r) ``` | Unsupported. Workaround:
```proc f(c: chan[int]) {.goroutine.} = c <- 1```
```go f(r)``` | 19 | | non-blocking
sleep | ``` time.Sleep(100 * time.Millisecond) ``` | ``` go_sleep_ms(100) ``` | 20 | | yield to another
goroutine | runtime.Gosched() | ``` go_yield() ``` | 21 | | run the goroutines
on all the available
CPU cores | ``` runtime.GOMAXPROCS(runtime.NumCPU()) ``` | ``` runtime_gomaxprocsfunc(getproccount()) ``` | 22 | | special code
layout | | ``` import golib ```

``` proc go_main() {.gomain.} = ```
``` # main code here ```

``` golib_main() ```
``` # not reached ``` | 23 | | compiler
parameters | | ``` # nim.cfg ```
``` --threads:on ```
``` --stackTrace:off ```
``` --passC:"--std=gnu99 -fsplit-stack" ```
``` --dynlibOverride:"go" ```
``` --passL:"-lgolib -lgo" ```
``` --gc:go ```
``` # or --gc:none ``` | 24 | 25 | The initialization of the Go runtime (including the GC) is done in golib_main(), after which go_main() is launched as a goroutine and the main event loop is started. That's why you can't do heap memory allocation before go_main() runs and also why anything after golib_main() won't be reached - the event loop ends the program when go_main() exits. 26 | 27 | ## API stability 28 | 29 | The API is subject to change until the first version (0.0.1) is released. After this, backwards compatibility will become a priority. 30 | 31 | ## requirements 32 | 33 | - [golib][2] 34 | - Nim > 0.19.0 35 | - gcc >= 7.3 36 | 37 | ## build the benchmark and run tests 38 | 39 | ```sh 40 | ./autogen.sh 41 | ``` 42 | If you have a Nim repo in ../Nim/: 43 | ```sh 44 | ./configure NIM=../Nim/bin/nim 45 | ``` 46 | Or with the system-wide Nim and no GC (some tests and benchmarks will fail without the Go GC): 47 | ```sh 48 | ./configure --disable-gogc 49 | ``` 50 | Run tests: 51 | ```sh 52 | make check 53 | ``` 54 | Benchmark (compare it with the ones from golib): 55 | ```sh 56 | make 57 | /usr/bin/time -v ./benchmarks/cw_nim 58 | ``` 59 | 60 | ## install 61 | 62 | ```sh 63 | nimble install 64 | ``` 65 | 66 | ## license 67 | 68 | BSD-2 69 | 70 | ## credits 71 | 72 | - author: Ștefan Talpalaru 73 | 74 | - homepage: https://github.com/stefantalpalaru/golib-nim 75 | 76 | 77 | [1]: http://nim-lang.org/ 78 | [2]: https://github.com/stefantalpalaru/golib 79 | 80 | -------------------------------------------------------------------------------- /autogen.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # a u t o g e n . s h 3 | # 4 | # Copyright (c) 2005-2009 United States Government as represented by 5 | # the U.S. Army Research Laboratory. 6 | # 7 | # Redistribution and use in source and binary forms, with or without 8 | # modification, are permitted provided that the following conditions 9 | # are met: 10 | # 11 | # 1. Redistributions of source code must retain the above copyright 12 | # notice, this list of conditions and the following disclaimer. 13 | # 14 | # 2. Redistributions in binary form must reproduce the above 15 | # copyright notice, this list of conditions and the following 16 | # disclaimer in the documentation and/or other materials provided 17 | # with the distribution. 18 | # 19 | # 3. The name of the author may not be used to endorse or promote 20 | # products derived from this software without specific prior written 21 | # permission. 22 | # 23 | # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 24 | # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 | # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 27 | # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 | # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 29 | # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30 | # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 31 | # WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 32 | # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 33 | # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | # 35 | ### 36 | # 37 | # Script for automatically preparing the sources for compilation by 38 | # performing the myriad of necessary steps. The script attempts to 39 | # detect proper version support, and outputs warnings about particular 40 | # systems that have autotool peculiarities. 41 | # 42 | # Basically, if everything is set up and installed correctly, the 43 | # script will validate that minimum versions of the GNU Build System 44 | # tools are installed, account for several common configuration 45 | # issues, and then simply run autoreconf for you. 46 | # 47 | # If autoreconf fails, which can happen for many valid configurations, 48 | # this script proceeds to run manual preparation steps effectively 49 | # providing a POSIX shell script (mostly complete) reimplementation of 50 | # autoreconf. 51 | # 52 | # The AUTORECONF, AUTOCONF, AUTOMAKE, LIBTOOLIZE, ACLOCAL, AUTOHEADER 53 | # environment variables and corresponding _OPTIONS variables (e.g. 54 | # AUTORECONF_OPTIONS) may be used to override the default automatic 55 | # detection behaviors. Similarly the _VERSION variables will override 56 | # the minimum required version numbers. 57 | # 58 | # Examples: 59 | # 60 | # To obtain help on usage: 61 | # ./autogen.sh --help 62 | # 63 | # To obtain verbose output: 64 | # ./autogen.sh --verbose 65 | # 66 | # To skip autoreconf and prepare manually: 67 | # AUTORECONF=false ./autogen.sh 68 | # 69 | # To verbosely try running with an older (unsupported) autoconf: 70 | # AUTOCONF_VERSION=2.50 ./autogen.sh --verbose 71 | # 72 | # Author: 73 | # Christopher Sean Morrison 74 | # 75 | # Patches: 76 | # Sebastian Pipping 77 | # 78 | ###################################################################### 79 | 80 | # set to minimum acceptable version of autoconf 81 | if [ "x$AUTOCONF_VERSION" = "x" ] ; then 82 | AUTOCONF_VERSION=2.52 83 | fi 84 | # set to minimum acceptable version of automake 85 | if [ "x$AUTOMAKE_VERSION" = "x" ] ; then 86 | AUTOMAKE_VERSION=1.11 87 | fi 88 | # set to minimum acceptable version of libtool 89 | if [ "x$LIBTOOL_VERSION" = "x" ] ; then 90 | LIBTOOL_VERSION=1.4.2 91 | fi 92 | 93 | ################## 94 | # ident function # 95 | ################## 96 | ident ( ) { 97 | # extract copyright from header 98 | __copyright="`grep Copyright $AUTOGEN_SH | head -${HEAD_N}1 | awk '{print $4}'`" 99 | if [ "x$__copyright" = "x" ] ; then 100 | __copyright="`date +%Y`" 101 | fi 102 | 103 | # extract version from CVS Id string 104 | __id="$Id: autogen.sh 33925 2009-03-01 23:27:06Z brlcad $" 105 | __version="`echo $__id | sed 's/.*\([0-9][0-9][0-9][0-9]\)[-\/]\([0-9][0-9]\)[-\/]\([0-9][0-9]\).*/\1\2\3/'`" 106 | if [ "x$__version" = "x" ] ; then 107 | __version="" 108 | fi 109 | 110 | echo "autogen.sh build preparation script by Christopher Sean Morrison" 111 | echo " + config.guess download patch by Sebastian Pipping (2008-12-03)" 112 | echo "revised 3-clause BSD-style license, copyright (c) $__copyright" 113 | echo "script version $__version, ISO/IEC 9945 POSIX shell script" 114 | } 115 | 116 | 117 | ################## 118 | # USAGE FUNCTION # 119 | ################## 120 | usage ( ) { 121 | echo "Usage: $AUTOGEN_SH [-h|--help] [-v|--verbose] [-q|--quiet] [-d|--download] [--version]" 122 | echo " --help Help on $NAME_OF_AUTOGEN usage" 123 | echo " --verbose Verbose progress output" 124 | echo " --quiet Quiet suppressed progress output" 125 | echo " --download Download the latest config.guess from gnulib" 126 | echo " --version Only perform GNU Build System version checks" 127 | echo 128 | echo "Description: This script will validate that minimum versions of the" 129 | echo "GNU Build System tools are installed and then run autoreconf for you." 130 | echo "Should autoreconf fail, manual preparation steps will be run" 131 | echo "potentially accounting for several common preparation issues. The" 132 | 133 | echo "AUTORECONF, AUTOCONF, AUTOMAKE, LIBTOOLIZE, ACLOCAL, AUTOHEADER," 134 | echo "PROJECT, & CONFIGURE environment variables and corresponding _OPTIONS" 135 | echo "variables (e.g. AUTORECONF_OPTIONS) may be used to override the" 136 | echo "default automatic detection behavior." 137 | echo 138 | 139 | ident 140 | 141 | return 0 142 | } 143 | 144 | 145 | ########################## 146 | # VERSION_ERROR FUNCTION # 147 | ########################## 148 | version_error ( ) { 149 | if [ "x$1" = "x" ] ; then 150 | echo "INTERNAL ERROR: version_error was not provided a version" 151 | exit 1 152 | fi 153 | if [ "x$2" = "x" ] ; then 154 | echo "INTERNAL ERROR: version_error was not provided an application name" 155 | exit 1 156 | fi 157 | $ECHO 158 | $ECHO "ERROR: To prepare the ${PROJECT} build system from scratch," 159 | $ECHO " at least version $1 of $2 must be installed." 160 | $ECHO 161 | $ECHO "$NAME_OF_AUTOGEN does not need to be run on the same machine that will" 162 | $ECHO "run configure or make. Either the GNU Autotools will need to be installed" 163 | $ECHO "or upgraded on this system, or $NAME_OF_AUTOGEN must be run on the source" 164 | $ECHO "code on another system and then transferred to here. -- Cheers!" 165 | $ECHO 166 | } 167 | 168 | ########################## 169 | # VERSION_CHECK FUNCTION # 170 | ########################## 171 | version_check ( ) { 172 | if [ "x$1" = "x" ] ; then 173 | echo "INTERNAL ERROR: version_check was not provided a minimum version" 174 | exit 1 175 | fi 176 | _min="$1" 177 | if [ "x$2" = "x" ] ; then 178 | echo "INTERNAL ERROR: version check was not provided a comparison version" 179 | exit 1 180 | fi 181 | _cur="$2" 182 | 183 | # needed to handle versions like 1.10 and 1.4-p6 184 | _min="`echo ${_min}. | sed 's/[^0-9]/./g' | sed 's/\.\././g'`" 185 | _cur="`echo ${_cur}. | sed 's/[^0-9]/./g' | sed 's/\.\././g'`" 186 | 187 | _min_major="`echo $_min | cut -d. -f1`" 188 | _min_minor="`echo $_min | cut -d. -f2`" 189 | _min_patch="`echo $_min | cut -d. -f3`" 190 | 191 | _cur_major="`echo $_cur | cut -d. -f1`" 192 | _cur_minor="`echo $_cur | cut -d. -f2`" 193 | _cur_patch="`echo $_cur | cut -d. -f3`" 194 | 195 | if [ "x$_min_major" = "x" ] ; then 196 | _min_major=0 197 | fi 198 | if [ "x$_min_minor" = "x" ] ; then 199 | _min_minor=0 200 | fi 201 | if [ "x$_min_patch" = "x" ] ; then 202 | _min_patch=0 203 | fi 204 | if [ "x$_cur_minor" = "x" ] ; then 205 | _cur_major=0 206 | fi 207 | if [ "x$_cur_minor" = "x" ] ; then 208 | _cur_minor=0 209 | fi 210 | if [ "x$_cur_patch" = "x" ] ; then 211 | _cur_patch=0 212 | fi 213 | 214 | $VERBOSE_ECHO "Checking if ${_cur_major}.${_cur_minor}.${_cur_patch} is greater than ${_min_major}.${_min_minor}.${_min_patch}" 215 | 216 | if [ $_min_major -lt $_cur_major ] ; then 217 | return 0 218 | elif [ $_min_major -eq $_cur_major ] ; then 219 | if [ $_min_minor -lt $_cur_minor ] ; then 220 | return 0 221 | elif [ $_min_minor -eq $_cur_minor ] ; then 222 | if [ $_min_patch -lt $_cur_patch ] ; then 223 | return 0 224 | elif [ $_min_patch -eq $_cur_patch ] ; then 225 | return 0 226 | fi 227 | fi 228 | fi 229 | return 1 230 | } 231 | 232 | 233 | ###################################### 234 | # LOCATE_CONFIGURE_TEMPLATE FUNCTION # 235 | ###################################### 236 | locate_configure_template ( ) { 237 | _pwd="`pwd`" 238 | if test -f "./configure.ac" ; then 239 | echo "./configure.ac" 240 | elif test -f "./configure.in" ; then 241 | echo "./configure.in" 242 | elif test -f "$_pwd/configure.ac" ; then 243 | echo "$_pwd/configure.ac" 244 | elif test -f "$_pwd/configure.in" ; then 245 | echo "$_pwd/configure.in" 246 | elif test -f "$PATH_TO_AUTOGEN/configure.ac" ; then 247 | echo "$PATH_TO_AUTOGEN/configure.ac" 248 | elif test -f "$PATH_TO_AUTOGEN/configure.in" ; then 249 | echo "$PATH_TO_AUTOGEN/configure.in" 250 | fi 251 | } 252 | 253 | 254 | ################## 255 | # argument check # 256 | ################## 257 | ARGS="$*" 258 | PATH_TO_AUTOGEN="`dirname $0`" 259 | NAME_OF_AUTOGEN="`basename $0`" 260 | AUTOGEN_SH="$PATH_TO_AUTOGEN/$NAME_OF_AUTOGEN" 261 | 262 | LIBTOOL_M4="${PATH_TO_AUTOGEN}/misc/libtool.m4" 263 | 264 | if [ "x$HELP" = "x" ] ; then 265 | HELP=no 266 | fi 267 | if [ "x$QUIET" = "x" ] ; then 268 | QUIET=no 269 | fi 270 | if [ "x$VERBOSE" = "x" ] ; then 271 | VERBOSE=no 272 | fi 273 | if [ "x$VERSION_ONLY" = "x" ] ; then 274 | VERSION_ONLY=no 275 | fi 276 | if [ "x$DOWNLOAD" = "x" ] ; then 277 | DOWNLOAD=no 278 | fi 279 | if [ "x$AUTORECONF_OPTIONS" = "x" ] ; then 280 | AUTORECONF_OPTIONS="-i -f" 281 | fi 282 | if [ "x$AUTOCONF_OPTIONS" = "x" ] ; then 283 | AUTOCONF_OPTIONS="-f" 284 | fi 285 | if [ "x$AUTOMAKE_OPTIONS" = "x" ] ; then 286 | AUTOMAKE_OPTIONS="-a -c -f" 287 | fi 288 | ALT_AUTOMAKE_OPTIONS="-a -c" 289 | if [ "x$LIBTOOLIZE_OPTIONS" = "x" ] ; then 290 | LIBTOOLIZE_OPTIONS="--automake -c -f" 291 | fi 292 | ALT_LIBTOOLIZE_OPTIONS="--automake --copy --force" 293 | if [ "x$ACLOCAL_OPTIONS" = "x" ] ; then 294 | ACLOCAL_OPTIONS="" 295 | fi 296 | if [ "x$AUTOHEADER_OPTIONS" = "x" ] ; then 297 | AUTOHEADER_OPTIONS="" 298 | fi 299 | if [ "x$CONFIG_GUESS_URL" = "x" ] ; then 300 | CONFIG_GUESS_URL="http://git.savannah.gnu.org/gitweb/?p=gnulib.git;a=blob_plain;f=build-aux/config.guess;hb=HEAD" 301 | fi 302 | for arg in $ARGS ; do 303 | case "x$arg" in 304 | x--help) HELP=yes ;; 305 | x-[hH]) HELP=yes ;; 306 | x--quiet) QUIET=yes ;; 307 | x-[qQ]) QUIET=yes ;; 308 | x--verbose) VERBOSE=yes ;; 309 | x-[dD]) DOWNLOAD=yes ;; 310 | x--download) DOWNLOAD=yes ;; 311 | x-[vV]) VERBOSE=yes ;; 312 | x--version) VERSION_ONLY=yes ;; 313 | *) 314 | echo "Unknown option: $arg" 315 | echo 316 | usage 317 | exit 1 318 | ;; 319 | esac 320 | done 321 | 322 | 323 | ##################### 324 | # environment check # 325 | ##################### 326 | 327 | # sanity check before recursions potentially begin 328 | if [ ! -f "$AUTOGEN_SH" ] ; then 329 | echo "INTERNAL ERROR: $AUTOGEN_SH does not exist" 330 | if [ ! "x$0" = "x$AUTOGEN_SH" ] ; then 331 | echo "INTERNAL ERROR: dirname/basename inconsistency: $0 != $AUTOGEN_SH" 332 | fi 333 | exit 1 334 | fi 335 | 336 | # force locale setting to C so things like date output as expected 337 | LC_ALL=C 338 | 339 | # commands that this script expects 340 | for __cmd in echo head tail pwd ; do 341 | echo "test" | $__cmd > /dev/null 2>&1 342 | if [ $? != 0 ] ; then 343 | echo "INTERNAL ERROR: '${__cmd}' command is required" 344 | exit 2 345 | fi 346 | done 347 | echo "test" | grep "test" > /dev/null 2>&1 348 | if test ! x$? = x0 ; then 349 | echo "INTERNAL ERROR: grep command is required" 350 | exit 1 351 | fi 352 | echo "test" | sed "s/test/test/" > /dev/null 2>&1 353 | if test ! x$? = x0 ; then 354 | echo "INTERNAL ERROR: sed command is required" 355 | exit 1 356 | fi 357 | 358 | 359 | # determine the behavior of echo 360 | case `echo "testing\c"; echo 1,2,3`,`echo -n testing; echo 1,2,3` in 361 | *c*,-n*) ECHO_N= ECHO_C=' 362 | ' ECHO_T=' ' ;; 363 | *c*,* ) ECHO_N=-n ECHO_C= ECHO_T= ;; 364 | *) ECHO_N= ECHO_C='\c' ECHO_T= ;; 365 | esac 366 | 367 | # determine the behavior of head 368 | case "x`echo 'head' | head -n 1 2>&1`" in 369 | *xhead*) HEAD_N="n " ;; 370 | *) HEAD_N="" ;; 371 | esac 372 | 373 | # determine the behavior of tail 374 | case "x`echo 'tail' | tail -n 1 2>&1`" in 375 | *xtail*) TAIL_N="n " ;; 376 | *) TAIL_N="" ;; 377 | esac 378 | 379 | VERBOSE_ECHO=: 380 | ECHO=: 381 | if [ "x$QUIET" = "xyes" ] ; then 382 | if [ "x$VERBOSE" = "xyes" ] ; then 383 | echo "Verbose output quelled by quiet option. Further output disabled." 384 | fi 385 | else 386 | ECHO=echo 387 | if [ "x$VERBOSE" = "xyes" ] ; then 388 | echo "Verbose output enabled" 389 | VERBOSE_ECHO=echo 390 | fi 391 | fi 392 | 393 | 394 | # allow a recursive run to disable further recursions 395 | if [ "x$RUN_RECURSIVE" = "x" ] ; then 396 | RUN_RECURSIVE=yes 397 | fi 398 | 399 | 400 | ################################################ 401 | # check for help arg and bypass version checks # 402 | ################################################ 403 | if [ "x`echo $ARGS | sed 's/.*[hH][eE][lL][pP].*/help/'`" = "xhelp" ] ; then 404 | HELP=yes 405 | fi 406 | if [ "x$HELP" = "xyes" ] ; then 407 | usage 408 | $ECHO "---" 409 | $ECHO "Help was requested. No preparation or configuration will be performed." 410 | exit 0 411 | fi 412 | 413 | 414 | ####################### 415 | # set up signal traps # 416 | ####################### 417 | untrap_abnormal ( ) { 418 | for sig in 1 2 13 15; do 419 | trap - $sig 420 | done 421 | } 422 | 423 | # do this cleanup whenever we exit. 424 | trap ' 425 | # start from the root 426 | if test -d "$START_PATH" ; then 427 | cd "$START_PATH" 428 | fi 429 | 430 | # restore/delete backup files 431 | if test "x$PFC_INIT" = "x1" ; then 432 | recursive_restore 433 | fi 434 | ' 0 435 | 436 | # trap SIGHUP (1), SIGINT (2), SIGPIPE (13), SIGTERM (15) 437 | for sig in 1 2 13 15; do 438 | trap ' 439 | $ECHO "" 440 | $ECHO "Aborting $NAME_OF_AUTOGEN: caught signal '$sig'" 441 | 442 | # start from the root 443 | if test -d "$START_PATH" ; then 444 | cd "$START_PATH" 445 | fi 446 | 447 | # clean up on abnormal exit 448 | $VERBOSE_ECHO "rm -rf autom4te.cache" 449 | rm -rf autom4te.cache 450 | 451 | if test -f "acinclude.m4.$$.backup" ; then 452 | $VERBOSE_ECHO "cat acinclude.m4.$$.backup > acinclude.m4" 453 | chmod u+w acinclude.m4 454 | cat acinclude.m4.$$.backup > acinclude.m4 455 | 456 | $VERBOSE_ECHO "rm -f acinclude.m4.$$.backup" 457 | rm -f acinclude.m4.$$.backup 458 | fi 459 | 460 | { (exit 1); exit 1; } 461 | ' $sig 462 | done 463 | 464 | 465 | ############################# 466 | # look for a configure file # 467 | ############################# 468 | if [ "x$CONFIGURE" = "x" ] ; then 469 | CONFIGURE="`locate_configure_template`" 470 | if [ ! "x$CONFIGURE" = "x" ] ; then 471 | $VERBOSE_ECHO "Found a configure template: $CONFIGURE" 472 | fi 473 | else 474 | $ECHO "Using CONFIGURE environment variable override: $CONFIGURE" 475 | fi 476 | if [ "x$CONFIGURE" = "x" ] ; then 477 | if [ "x$VERSION_ONLY" = "xyes" ] ; then 478 | CONFIGURE=/dev/null 479 | else 480 | $ECHO 481 | $ECHO "A configure.ac or configure.in file could not be located implying" 482 | $ECHO "that the GNU Build System is at least not used in this directory. In" 483 | $ECHO "any case, there is nothing to do here without one of those files." 484 | $ECHO 485 | $ECHO "ERROR: No configure.in or configure.ac file found in `pwd`" 486 | exit 1 487 | fi 488 | fi 489 | 490 | #################### 491 | # get project name # 492 | #################### 493 | if [ "x$PROJECT" = "x" ] ; then 494 | PROJECT="`grep AC_INIT $CONFIGURE | grep -v '.*#.*AC_INIT' | tail -${TAIL_N}1 | sed 's/^[ ]*AC_INIT(\([^,)]*\).*/\1/' | sed 's/.*\[\(.*\)\].*/\1/'`" 495 | if [ "x$PROJECT" = "xAC_INIT" ] ; then 496 | # projects might be using the older/deprecated arg-less AC_INIT .. look for AM_INIT_AUTOMAKE instead 497 | PROJECT="`grep AM_INIT_AUTOMAKE $CONFIGURE | grep -v '.*#.*AM_INIT_AUTOMAKE' | tail -${TAIL_N}1 | sed 's/^[ ]*AM_INIT_AUTOMAKE(\([^,)]*\).*/\1/' | sed 's/.*\[\(.*\)\].*/\1/'`" 498 | fi 499 | if [ "x$PROJECT" = "xAM_INIT_AUTOMAKE" ] ; then 500 | PROJECT="project" 501 | fi 502 | if [ "x$PROJECT" = "x" ] ; then 503 | PROJECT="project" 504 | fi 505 | else 506 | $ECHO "Using PROJECT environment variable override: $PROJECT" 507 | fi 508 | $ECHO "Preparing the $PROJECT build system...please wait" 509 | $ECHO 510 | 511 | 512 | ######################## 513 | # check for autoreconf # 514 | ######################## 515 | HAVE_AUTORECONF=no 516 | if [ "x$AUTORECONF" = "x" ] ; then 517 | for AUTORECONF in autoreconf ; do 518 | $VERBOSE_ECHO "Checking autoreconf version: $AUTORECONF --version" 519 | $AUTORECONF --version > /dev/null 2>&1 520 | if [ $? = 0 ] ; then 521 | HAVE_AUTORECONF=yes 522 | break 523 | fi 524 | done 525 | else 526 | HAVE_AUTORECONF=yes 527 | $ECHO "Using AUTORECONF environment variable override: $AUTORECONF" 528 | fi 529 | 530 | 531 | ########################## 532 | # autoconf version check # 533 | ########################## 534 | _acfound=no 535 | if [ "x$AUTOCONF" = "x" ] ; then 536 | for AUTOCONF in autoconf ; do 537 | $VERBOSE_ECHO "Checking autoconf version: $AUTOCONF --version" 538 | $AUTOCONF --version > /dev/null 2>&1 539 | if [ $? = 0 ] ; then 540 | _acfound=yes 541 | break 542 | fi 543 | done 544 | else 545 | _acfound=yes 546 | $ECHO "Using AUTOCONF environment variable override: $AUTOCONF" 547 | fi 548 | 549 | _report_error=no 550 | if [ ! "x$_acfound" = "xyes" ] ; then 551 | $ECHO "ERROR: Unable to locate GNU Autoconf." 552 | _report_error=yes 553 | else 554 | _version="`$AUTOCONF --version | head -${HEAD_N}1 | sed 's/[^0-9]*\([0-9\.][0-9\.]*\)/\1/'`" 555 | if [ "x$_version" = "x" ] ; then 556 | _version="0.0.0" 557 | fi 558 | $ECHO "Found GNU Autoconf version $_version" 559 | version_check "$AUTOCONF_VERSION" "$_version" 560 | if [ $? -ne 0 ] ; then 561 | _report_error=yes 562 | fi 563 | fi 564 | if [ "x$_report_error" = "xyes" ] ; then 565 | version_error "$AUTOCONF_VERSION" "GNU Autoconf" 566 | exit 1 567 | fi 568 | 569 | 570 | ########################## 571 | # automake version check # 572 | ########################## 573 | _amfound=no 574 | if [ "x$AUTOMAKE" = "x" ] ; then 575 | for AUTOMAKE in automake ; do 576 | $VERBOSE_ECHO "Checking automake version: $AUTOMAKE --version" 577 | $AUTOMAKE --version > /dev/null 2>&1 578 | if [ $? = 0 ] ; then 579 | _amfound=yes 580 | break 581 | fi 582 | done 583 | else 584 | _amfound=yes 585 | $ECHO "Using AUTOMAKE environment variable override: $AUTOMAKE" 586 | fi 587 | 588 | 589 | _report_error=no 590 | if [ ! "x$_amfound" = "xyes" ] ; then 591 | $ECHO 592 | $ECHO "ERROR: Unable to locate GNU Automake." 593 | _report_error=yes 594 | else 595 | _version="`$AUTOMAKE --version | head -${HEAD_N}1 | sed 's/[^0-9]*\([0-9\.][0-9\.]*\)/\1/'`" 596 | if [ "x$_version" = "x" ] ; then 597 | _version="0.0.0" 598 | fi 599 | $ECHO "Found GNU Automake version $_version" 600 | version_check "$AUTOMAKE_VERSION" "$_version" 601 | if [ $? -ne 0 ] ; then 602 | _report_error=yes 603 | fi 604 | fi 605 | if [ "x$_report_error" = "xyes" ] ; then 606 | version_error "$AUTOMAKE_VERSION" "GNU Automake" 607 | exit 1 608 | fi 609 | 610 | 611 | ######################## 612 | # check for libtoolize # 613 | ######################## 614 | HAVE_LIBTOOLIZE=yes 615 | HAVE_ALT_LIBTOOLIZE=no 616 | _ltfound=no 617 | if [ "x$LIBTOOLIZE" = "x" ] ; then 618 | LIBTOOLIZE=libtoolize 619 | $VERBOSE_ECHO "Checking libtoolize version: $LIBTOOLIZE --version" 620 | $LIBTOOLIZE --version > /dev/null 2>&1 621 | if [ ! $? = 0 ] ; then 622 | HAVE_LIBTOOLIZE=no 623 | $ECHO 624 | if [ "x$HAVE_AUTORECONF" = "xno" ] ; then 625 | $ECHO "Warning: libtoolize does not appear to be available." 626 | else 627 | $ECHO "Warning: libtoolize does not appear to be available. This means that" 628 | $ECHO "the automatic build preparation via autoreconf will probably not work." 629 | $ECHO "Preparing the build by running each step individually, however, should" 630 | $ECHO "work and will be done automatically for you if autoreconf fails." 631 | fi 632 | 633 | # look for some alternates 634 | for tool in glibtoolize libtoolize15 libtoolize14 libtoolize13 ; do 635 | $VERBOSE_ECHO "Checking libtoolize alternate: $tool --version" 636 | _glibtoolize="`$tool --version > /dev/null 2>&1`" 637 | if [ $? = 0 ] ; then 638 | $VERBOSE_ECHO "Found $tool --version" 639 | _glti="`which $tool`" 640 | if [ "x$_glti" = "x" ] ; then 641 | $VERBOSE_ECHO "Cannot find $tool with which" 642 | continue; 643 | fi 644 | if test ! -f "$_glti" ; then 645 | $VERBOSE_ECHO "Cannot use $tool, $_glti is not a file" 646 | continue; 647 | fi 648 | _gltidir="`dirname $_glti`" 649 | if [ "x$_gltidir" = "x" ] ; then 650 | $VERBOSE_ECHO "Cannot find $tool path with dirname of $_glti" 651 | continue; 652 | fi 653 | if test ! -d "$_gltidir" ; then 654 | $VERBOSE_ECHO "Cannot use $tool, $_gltidir is not a directory" 655 | continue; 656 | fi 657 | HAVE_ALT_LIBTOOLIZE=yes 658 | LIBTOOLIZE="$tool" 659 | $ECHO 660 | $ECHO "Fortunately, $tool was found which means that your system may simply" 661 | $ECHO "have a non-standard or incomplete GNU Autotools install. If you have" 662 | $ECHO "sufficient system access, it may be possible to quell this warning by" 663 | $ECHO "running:" 664 | $ECHO 665 | sudo -V > /dev/null 2>&1 666 | if [ $? = 0 ] ; then 667 | $ECHO " sudo ln -s $_glti $_gltidir/libtoolize" 668 | $ECHO 669 | else 670 | $ECHO " ln -s $_glti $_gltidir/libtoolize" 671 | $ECHO 672 | $ECHO "Run that as root or with proper permissions to the $_gltidir directory" 673 | $ECHO 674 | fi 675 | _ltfound=yes 676 | break 677 | fi 678 | done 679 | else 680 | _ltfound=yes 681 | fi 682 | else 683 | _ltfound=yes 684 | $ECHO "Using LIBTOOLIZE environment variable override: $LIBTOOLIZE" 685 | fi 686 | 687 | 688 | ############################ 689 | # libtoolize version check # 690 | ############################ 691 | _report_error=no 692 | if [ ! "x$_ltfound" = "xyes" ] ; then 693 | $ECHO 694 | $ECHO "ERROR: Unable to locate GNU Libtool." 695 | _report_error=yes 696 | else 697 | _version="`$LIBTOOLIZE --version | head -${HEAD_N}1 | sed 's/[^0-9]*\([0-9\.][0-9\.]*\)/\1/'`" 698 | if [ "x$_version" = "x" ] ; then 699 | _version="0.0.0" 700 | fi 701 | $ECHO "Found GNU Libtool version $_version" 702 | version_check "$LIBTOOL_VERSION" "$_version" 703 | if [ $? -ne 0 ] ; then 704 | _report_error=yes 705 | fi 706 | fi 707 | if [ "x$_report_error" = "xyes" ] ; then 708 | version_error "$LIBTOOL_VERSION" "GNU Libtool" 709 | exit 1 710 | fi 711 | 712 | 713 | ##################### 714 | # check for aclocal # 715 | ##################### 716 | if [ "x$ACLOCAL" = "x" ] ; then 717 | for ACLOCAL in aclocal ; do 718 | $VERBOSE_ECHO "Checking aclocal version: $ACLOCAL --version" 719 | $ACLOCAL --version > /dev/null 2>&1 720 | if [ $? = 0 ] ; then 721 | break 722 | fi 723 | done 724 | else 725 | $ECHO "Using ACLOCAL environment variable override: $ACLOCAL" 726 | fi 727 | 728 | 729 | ######################## 730 | # check for autoheader # 731 | ######################## 732 | if [ "x$AUTOHEADER" = "x" ] ; then 733 | for AUTOHEADER in autoheader ; do 734 | $VERBOSE_ECHO "Checking autoheader version: $AUTOHEADER --version" 735 | $AUTOHEADER --version > /dev/null 2>&1 736 | if [ $? = 0 ] ; then 737 | break 738 | fi 739 | done 740 | else 741 | $ECHO "Using AUTOHEADER environment variable override: $AUTOHEADER" 742 | fi 743 | 744 | 745 | ######################### 746 | # check if version only # 747 | ######################### 748 | $VERBOSE_ECHO "Checking whether to only output version information" 749 | if [ "x$VERSION_ONLY" = "xyes" ] ; then 750 | $ECHO 751 | ident 752 | $ECHO "---" 753 | $ECHO "Version requested. No preparation or configuration will be performed." 754 | exit 0 755 | fi 756 | 757 | 758 | ################################# 759 | # PROTECT_FROM_CLOBBER FUNCTION # 760 | ################################# 761 | protect_from_clobber ( ) { 762 | PFC_INIT=1 763 | 764 | # protect COPYING & INSTALL from overwrite by automake. the 765 | # automake force option will (inappropriately) ignore the existing 766 | # contents of a COPYING and/or INSTALL files (depending on the 767 | # version) instead of just forcing *missing* files like it does 768 | # for AUTHORS, NEWS, and README. this is broken but extremely 769 | # prevalent behavior, so we protect against it by keeping a backup 770 | # of the file that can later be restored. 771 | 772 | for file in COPYING INSTALL ; do 773 | if test -f ${file} ; then 774 | if test -f ${file}.$$.protect_from_automake.backup ; then 775 | $VERBOSE_ECHO "Already backed up ${file} in `pwd`" 776 | else 777 | $VERBOSE_ECHO "Backing up ${file} in `pwd`" 778 | $VERBOSE_ECHO "cp -p ${file} ${file}.$$.protect_from_automake.backup" 779 | cp -p ${file} ${file}.$$.protect_from_automake.backup 780 | fi 781 | fi 782 | done 783 | } 784 | 785 | 786 | ############################## 787 | # RECURSIVE_PROTECT FUNCTION # 788 | ############################## 789 | recursive_protect ( ) { 790 | 791 | # for projects using recursive configure, run the build 792 | # preparation steps for the subdirectories. this function assumes 793 | # START_PATH was set to pwd before recursion begins so that 794 | # relative paths work. 795 | 796 | # git 'r done, protect COPYING and INSTALL from being clobbered 797 | protect_from_clobber 798 | 799 | if test -d autom4te.cache ; then 800 | $VERBOSE_ECHO "Found an autom4te.cache directory, deleting it" 801 | $VERBOSE_ECHO "rm -rf autom4te.cache" 802 | rm -rf autom4te.cache 803 | fi 804 | 805 | # find configure template 806 | _configure="`locate_configure_template`" 807 | if [ "x$_configure" = "x" ] ; then 808 | return 809 | fi 810 | # $VERBOSE_ECHO "Looking for configure template found `pwd`/$_configure" 811 | 812 | # look for subdirs 813 | # $VERBOSE_ECHO "Looking for subdirs in `pwd`" 814 | _det_config_subdirs="`grep AC_CONFIG_SUBDIRS $_configure | grep -v '.*#.*AC_CONFIG_SUBDIRS' | sed 's/^[ ]*AC_CONFIG_SUBDIRS(\(.*\)).*/\1/' | sed 's/.*\[\(.*\)\].*/\1/'`" 815 | CHECK_DIRS="" 816 | for dir in $_det_config_subdirs ; do 817 | if test -d "`pwd`/$dir" ; then 818 | CHECK_DIRS="$CHECK_DIRS \"`pwd`/$dir\"" 819 | fi 820 | done 821 | 822 | # process subdirs 823 | if [ ! "x$CHECK_DIRS" = "x" ] ; then 824 | $VERBOSE_ECHO "Recursively scanning the following directories:" 825 | $VERBOSE_ECHO " $CHECK_DIRS" 826 | for dir in $CHECK_DIRS ; do 827 | $VERBOSE_ECHO "Protecting files from automake in $dir" 828 | cd "$START_PATH" 829 | eval "cd $dir" 830 | 831 | # recursively git 'r done 832 | recursive_protect 833 | done 834 | fi 835 | } # end of recursive_protect 836 | 837 | 838 | ############################# 839 | # RESTORE_CLOBBERED FUNCION # 840 | ############################# 841 | restore_clobbered ( ) { 842 | 843 | # The automake (and autoreconf by extension) -f/--force-missing 844 | # option may overwrite COPYING and INSTALL even if they do exist. 845 | # Here we restore the files if necessary. 846 | 847 | spacer=no 848 | 849 | for file in COPYING INSTALL ; do 850 | if test -f ${file}.$$.protect_from_automake.backup ; then 851 | if test -f ${file} ; then 852 | # compare entire content, restore if needed 853 | if test "x`cat ${file}`" != "x`cat ${file}.$$.protect_from_automake.backup`" ; then 854 | if test "x$spacer" = "xno" ; then 855 | $VERBOSE_ECHO 856 | spacer=yes 857 | fi 858 | # restore the backup 859 | $VERBOSE_ECHO "Restoring ${file} from backup (automake -f likely clobbered it)" 860 | $VERBOSE_ECHO "rm -f ${file}" 861 | rm -f ${file} 862 | $VERBOSE_ECHO "mv ${file}.$$.protect_from_automake.backup ${file}" 863 | mv ${file}.$$.protect_from_automake.backup ${file} 864 | fi # check contents 865 | elif test -f ${file}.$$.protect_from_automake.backup ; then 866 | $VERBOSE_ECHO "mv ${file}.$$.protect_from_automake.backup ${file}" 867 | mv ${file}.$$.protect_from_automake.backup ${file} 868 | fi # -f ${file} 869 | 870 | # just in case 871 | $VERBOSE_ECHO "rm -f ${file}.$$.protect_from_automake.backup" 872 | rm -f ${file}.$$.protect_from_automake.backup 873 | fi # -f ${file}.$$.protect_from_automake.backup 874 | done 875 | 876 | CONFIGURE="`locate_configure_template`" 877 | if [ "x$CONFIGURE" = "x" ] ; then 878 | return 879 | fi 880 | 881 | _aux_dir="`grep AC_CONFIG_AUX_DIR $CONFIGURE | grep -v '.*#.*AC_CONFIG_AUX_DIR' | tail -${TAIL_N}1 | sed 's/^[ ]*AC_CONFIG_AUX_DIR(\(.*\)).*/\1/' | sed 's/.*\[\(.*\)\].*/\1/'`" 882 | if test ! -d "$_aux_dir" ; then 883 | _aux_dir=. 884 | fi 885 | 886 | for file in config.guess config.sub ltmain.sh ; do 887 | if test -f "${_aux_dir}/${file}" ; then 888 | $VERBOSE_ECHO "rm -f \"${_aux_dir}/${file}.backup\"" 889 | rm -f "${_aux_dir}/${file}.backup" 890 | fi 891 | done 892 | } # end of restore_clobbered 893 | 894 | 895 | ############################## 896 | # RECURSIVE_RESTORE FUNCTION # 897 | ############################## 898 | recursive_restore ( ) { 899 | 900 | # restore COPYING and INSTALL from backup if they were clobbered 901 | # for each directory recursively. 902 | 903 | # git 'r undone 904 | restore_clobbered 905 | 906 | # find configure template 907 | _configure="`locate_configure_template`" 908 | if [ "x$_configure" = "x" ] ; then 909 | return 910 | fi 911 | 912 | # look for subdirs 913 | _det_config_subdirs="`grep AC_CONFIG_SUBDIRS $_configure | grep -v '.*#.*AC_CONFIG_SUBDIRS' | sed 's/^[ ]*AC_CONFIG_SUBDIRS(\(.*\)).*/\1/' | sed 's/.*\[\(.*\)\].*/\1/'`" 914 | CHECK_DIRS="" 915 | for dir in $_det_config_subdirs ; do 916 | if test -d "`pwd`/$dir" ; then 917 | CHECK_DIRS="$CHECK_DIRS \"`pwd`/$dir\"" 918 | fi 919 | done 920 | 921 | # process subdirs 922 | if [ ! "x$CHECK_DIRS" = "x" ] ; then 923 | $VERBOSE_ECHO "Recursively scanning the following directories:" 924 | $VERBOSE_ECHO " $CHECK_DIRS" 925 | for dir in $CHECK_DIRS ; do 926 | $VERBOSE_ECHO "Checking files for automake damage in $dir" 927 | cd "$START_PATH" 928 | eval "cd $dir" 929 | 930 | # recursively git 'r undone 931 | recursive_restore 932 | done 933 | fi 934 | } # end of recursive_restore 935 | 936 | 937 | ####################### 938 | # INITIALIZE FUNCTION # 939 | ####################### 940 | initialize ( ) { 941 | 942 | # this routine performs a variety of directory-specific 943 | # initializations. some are sanity checks, some are preventive, 944 | # and some are necessary setup detection. 945 | # 946 | # this function sets: 947 | # CONFIGURE 948 | # SEARCH_DIRS 949 | # CONFIG_SUBDIRS 950 | 951 | ################################## 952 | # check for a configure template # 953 | ################################## 954 | CONFIGURE="`locate_configure_template`" 955 | if [ "x$CONFIGURE" = "x" ] ; then 956 | $ECHO 957 | $ECHO "A configure.ac or configure.in file could not be located implying" 958 | $ECHO "that the GNU Build System is at least not used in this directory. In" 959 | $ECHO "any case, there is nothing to do here without one of those files." 960 | $ECHO 961 | $ECHO "ERROR: No configure.in or configure.ac file found in `pwd`" 962 | exit 1 963 | fi 964 | 965 | ##################### 966 | # detect an aux dir # 967 | ##################### 968 | _aux_dir="`grep AC_CONFIG_AUX_DIR $CONFIGURE | grep -v '.*#.*AC_CONFIG_AUX_DIR' | tail -${TAIL_N}1 | sed 's/^[ ]*AC_CONFIG_AUX_DIR(\(.*\)).*/\1/' | sed 's/.*\[\(.*\)\].*/\1/'`" 969 | if test ! -d "$_aux_dir" ; then 970 | _aux_dir=. 971 | else 972 | $VERBOSE_ECHO "Detected auxillary directory: $_aux_dir" 973 | fi 974 | 975 | ################################ 976 | # detect a recursive configure # 977 | ################################ 978 | CONFIG_SUBDIRS="" 979 | _det_config_subdirs="`grep AC_CONFIG_SUBDIRS $CONFIGURE | grep -v '.*#.*AC_CONFIG_SUBDIRS' | sed 's/^[ ]*AC_CONFIG_SUBDIRS(\(.*\)).*/\1/' | sed 's/.*\[\(.*\)\].*/\1/'`" 980 | for dir in $_det_config_subdirs ; do 981 | if test -d "`pwd`/$dir" ; then 982 | $VERBOSE_ECHO "Detected recursive configure directory: `pwd`/$dir" 983 | CONFIG_SUBDIRS="$CONFIG_SUBDIRS `pwd`/$dir" 984 | fi 985 | done 986 | 987 | ########################################################### 988 | # make sure certain required files exist for GNU projects # 989 | ########################################################### 990 | _marker_found="" 991 | _marker_found_message_intro='Detected non-GNU marker "' 992 | _marker_found_message_mid='" in ' 993 | for marker in foreign cygnus ; do 994 | _marker_found_message=${_marker_found_message_intro}${marker}${_marker_found_message_mid} 995 | _marker_found="`grep 'AM_INIT_AUTOMAKE.*'${marker} $CONFIGURE`" 996 | if [ ! "x$_marker_found" = "x" ] ; then 997 | $VERBOSE_ECHO "${_marker_found_message}`basename \"$CONFIGURE\"`" 998 | break 999 | fi 1000 | if test -f "`dirname \"$CONFIGURE\"/Makefile.am`" ; then 1001 | _marker_found="`grep 'AUTOMAKE_OPTIONS.*'${marker} Makefile.am`" 1002 | if [ ! "x$_marker_found" = "x" ] ; then 1003 | $VERBOSE_ECHO "${_marker_found_message}Makefile.am" 1004 | break 1005 | fi 1006 | fi 1007 | done 1008 | if [ "x${_marker_found}" = "x" ] ; then 1009 | _suggest_foreign=no 1010 | for file in AUTHORS COPYING ChangeLog INSTALL NEWS README ; do 1011 | if [ ! -f $file ] ; then 1012 | $VERBOSE_ECHO "Touching ${file} since it does not exist" 1013 | _suggest_foreign=yes 1014 | touch $file 1015 | fi 1016 | done 1017 | 1018 | if [ "x${_suggest_foreign}" = "xyes" ] ; then 1019 | $ECHO 1020 | $ECHO "Warning: Several files expected of projects that conform to the GNU" 1021 | $ECHO "coding standards were not found. The files were automatically added" 1022 | $ECHO "for you since you do not have a 'foreign' declaration specified." 1023 | $ECHO 1024 | $ECHO "Considered adding 'foreign' to AM_INIT_AUTOMAKE in `basename \"$CONFIGURE\"`" 1025 | if test -f "`dirname \"$CONFIGURE\"/Makefile.am`" ; then 1026 | $ECHO "or to AUTOMAKE_OPTIONS in your top-level Makefile.am file." 1027 | fi 1028 | $ECHO 1029 | fi 1030 | fi 1031 | 1032 | ################################################## 1033 | # make sure certain generated files do not exist # 1034 | ################################################## 1035 | for file in config.guess config.sub ltmain.sh ; do 1036 | if test -f "${_aux_dir}/${file}" ; then 1037 | $VERBOSE_ECHO "mv -f \"${_aux_dir}/${file}\" \"${_aux_dir}/${file}.backup\"" 1038 | mv -f "${_aux_dir}/${file}" "${_aux_dir}/${file}.backup" 1039 | fi 1040 | done 1041 | 1042 | ############################ 1043 | # search alternate m4 dirs # 1044 | ############################ 1045 | SEARCH_DIRS="" 1046 | for dir in m4 ; do 1047 | if [ -d $dir ] ; then 1048 | $VERBOSE_ECHO "Found extra aclocal search directory: $dir" 1049 | SEARCH_DIRS="$SEARCH_DIRS -I $dir" 1050 | fi 1051 | done 1052 | 1053 | ###################################### 1054 | # remove any previous build products # 1055 | ###################################### 1056 | if test -d autom4te.cache ; then 1057 | $VERBOSE_ECHO "Found an autom4te.cache directory, deleting it" 1058 | $VERBOSE_ECHO "rm -rf autom4te.cache" 1059 | rm -rf autom4te.cache 1060 | fi 1061 | # tcl/tk (and probably others) have a customized aclocal.m4, so can't delete it 1062 | # if test -f aclocal.m4 ; then 1063 | # $VERBOSE_ECHO "Found an aclocal.m4 file, deleting it" 1064 | # $VERBOSE_ECHO "rm -f aclocal.m4" 1065 | # rm -f aclocal.m4 1066 | # fi 1067 | 1068 | } # end of initialize() 1069 | 1070 | 1071 | ############## 1072 | # initialize # 1073 | ############## 1074 | 1075 | # stash path 1076 | START_PATH="`pwd`" 1077 | 1078 | # Before running autoreconf or manual steps, some prep detection work 1079 | # is necessary or useful. Only needs to occur once per directory, but 1080 | # does need to traverse the entire subconfigure hierarchy to protect 1081 | # files from being clobbered even by autoreconf. 1082 | recursive_protect 1083 | 1084 | # start from where we started 1085 | cd "$START_PATH" 1086 | 1087 | # get ready to process 1088 | initialize 1089 | 1090 | 1091 | ######################################### 1092 | # DOWNLOAD_GNULIB_CONFIG_GUESS FUNCTION # 1093 | ######################################### 1094 | 1095 | # TODO - should make sure wget/curl exist and/or work before trying to 1096 | # use them. 1097 | 1098 | download_gnulib_config_guess () { 1099 | # abuse gitweb to download gnulib's latest config.guess via HTTP 1100 | config_guess_temp="config.guess.$$.download" 1101 | ret=1 1102 | for __cmd in wget curl fetch ; do 1103 | $VERBOSE_ECHO "Checking for command ${__cmd}" 1104 | ${__cmd} --version > /dev/null 2>&1 1105 | ret=$? 1106 | if [ ! $ret = 0 ] ; then 1107 | continue 1108 | fi 1109 | 1110 | __cmd_version=`${__cmd} --version | head -n 1 | sed -e 's/^[^0-9]\+//' -e 's/ .*//'` 1111 | $VERBOSE_ECHO "Found ${__cmd} ${__cmd_version}" 1112 | 1113 | opts="" 1114 | case ${__cmd} in 1115 | wget) 1116 | opts="-O" 1117 | ;; 1118 | curl) 1119 | opts="-o" 1120 | ;; 1121 | fetch) 1122 | opts="-t 5 -f" 1123 | ;; 1124 | esac 1125 | 1126 | $VERBOSE_ECHO "Running $__cmd \"${CONFIG_GUESS_URL}\" $opts \"${config_guess_temp}\"" 1127 | eval "$__cmd \"${CONFIG_GUESS_URL}\" $opts \"${config_guess_temp}\"" > /dev/null 2>&1 1128 | if [ $? = 0 ] ; then 1129 | mv -f "${config_guess_temp}" ${_aux_dir}/config.guess 1130 | ret=0 1131 | break 1132 | fi 1133 | done 1134 | 1135 | if [ ! $ret = 0 ] ; then 1136 | $ECHO "Warning: config.guess download failed from: $CONFIG_GUESS_URL" 1137 | rm -f "${config_guess_temp}" 1138 | fi 1139 | } 1140 | 1141 | 1142 | ############################## 1143 | # LIBTOOLIZE_NEEDED FUNCTION # 1144 | ############################## 1145 | libtoolize_needed () { 1146 | ret=1 # means no, don't need libtoolize 1147 | for feature in AC_PROG_LIBTOOL AM_PROG_LIBTOOL LT_INIT ; do 1148 | $VERBOSE_ECHO "Searching for $feature in $CONFIGURE" 1149 | found="`grep \"^$feature.*\" $CONFIGURE`" 1150 | if [ ! "x$found" = "x" ] ; then 1151 | ret=0 # means yes, need to run libtoolize 1152 | break 1153 | fi 1154 | done 1155 | return ${ret} 1156 | } 1157 | 1158 | 1159 | 1160 | ############################################ 1161 | # prepare build via autoreconf or manually # 1162 | ############################################ 1163 | reconfigure_manually=no 1164 | if [ "x$HAVE_AUTORECONF" = "xyes" ] ; then 1165 | $ECHO 1166 | $ECHO $ECHO_N "Automatically preparing build ... $ECHO_C" 1167 | 1168 | $VERBOSE_ECHO "$AUTORECONF $SEARCH_DIRS $AUTORECONF_OPTIONS" 1169 | autoreconf_output="`$AUTORECONF $SEARCH_DIRS $AUTORECONF_OPTIONS 2>&1`" 1170 | ret=$? 1171 | $VERBOSE_ECHO "$autoreconf_output" 1172 | 1173 | if [ ! $ret = 0 ] ; then 1174 | if [ "x$HAVE_ALT_LIBTOOLIZE" = "xyes" ] ; then 1175 | if [ ! "x`echo \"$autoreconf_output\" | grep libtoolize | grep \"No such file or directory\"`" = "x" ] ; then 1176 | $ECHO 1177 | $ECHO "Warning: autoreconf failed but due to what is usually a common libtool" 1178 | $ECHO "misconfiguration issue. This problem is encountered on systems that" 1179 | $ECHO "have installed libtoolize under a different name without providing a" 1180 | $ECHO "symbolic link or without setting the LIBTOOLIZE environment variable." 1181 | $ECHO 1182 | $ECHO "Restarting the preparation steps with LIBTOOLIZE set to $LIBTOOLIZE" 1183 | 1184 | export LIBTOOLIZE 1185 | RUN_RECURSIVE=no 1186 | export RUN_RECURSIVE 1187 | untrap_abnormal 1188 | 1189 | $VERBOSE_ECHO sh $AUTOGEN_SH "$1" "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9" 1190 | sh "$AUTOGEN_SH" "$1" "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9" 1191 | exit $? 1192 | fi 1193 | fi 1194 | 1195 | $ECHO "Warning: $AUTORECONF failed" 1196 | 1197 | if test -f ltmain.sh ; then 1198 | $ECHO "libtoolize being run by autoreconf is not creating ltmain.sh in the auxillary directory like it should" 1199 | fi 1200 | 1201 | $ECHO "Attempting to run the preparation steps individually" 1202 | reconfigure_manually=yes 1203 | else 1204 | if [ "x$DOWNLOAD" = "xyes" ] ; then 1205 | if libtoolize_needed ; then 1206 | download_gnulib_config_guess 1207 | fi 1208 | fi 1209 | fi 1210 | else 1211 | reconfigure_manually=yes 1212 | fi 1213 | 1214 | 1215 | ############################ 1216 | # LIBTOOL_FAILURE FUNCTION # 1217 | ############################ 1218 | libtool_failure ( ) { 1219 | 1220 | # libtool is rather error-prone in comparison to the other 1221 | # autotools and this routine attempts to compensate for some 1222 | # common failures. the output after a libtoolize failure is 1223 | # parsed for an error related to AC_PROG_LIBTOOL and if found, we 1224 | # attempt to inject a project-provided libtool.m4 file. 1225 | 1226 | _autoconf_output="$1" 1227 | 1228 | if [ "x$RUN_RECURSIVE" = "xno" ] ; then 1229 | # we already tried the libtool.m4, don't try again 1230 | return 1 1231 | fi 1232 | 1233 | if test -f "$LIBTOOL_M4" ; then 1234 | found_libtool="`$ECHO $_autoconf_output | grep AC_PROG_LIBTOOL`" 1235 | if test ! "x$found_libtool" = "x" ; then 1236 | if test -f acinclude.m4 ; then 1237 | rm -f acinclude.m4.$$.backup 1238 | $VERBOSE_ECHO "cat acinclude.m4 > acinclude.m4.$$.backup" 1239 | cat acinclude.m4 > acinclude.m4.$$.backup 1240 | fi 1241 | $VERBOSE_ECHO "cat \"$LIBTOOL_M4\" >> acinclude.m4" 1242 | chmod u+w acinclude.m4 1243 | cat "$LIBTOOL_M4" >> acinclude.m4 1244 | 1245 | # don't keep doing this 1246 | RUN_RECURSIVE=no 1247 | export RUN_RECURSIVE 1248 | untrap_abnormal 1249 | 1250 | $ECHO 1251 | $ECHO "Restarting the preparation steps with libtool macros in acinclude.m4" 1252 | $VERBOSE_ECHO sh $AUTOGEN_SH "$1" "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9" 1253 | sh "$AUTOGEN_SH" "$1" "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9" 1254 | exit $? 1255 | fi 1256 | fi 1257 | } 1258 | 1259 | 1260 | ########################### 1261 | # MANUAL_AUTOGEN FUNCTION # 1262 | ########################### 1263 | manual_autogen ( ) { 1264 | 1265 | ################################################## 1266 | # Manual preparation steps taken are as follows: # 1267 | # aclocal [-I m4] # 1268 | # libtoolize --automake -c -f # 1269 | # aclocal [-I m4] # 1270 | # autoconf -f # 1271 | # autoheader # 1272 | # automake -a -c -f # 1273 | ################################################## 1274 | 1275 | ########### 1276 | # aclocal # 1277 | ########### 1278 | $VERBOSE_ECHO "$ACLOCAL $SEARCH_DIRS $ACLOCAL_OPTIONS" 1279 | aclocal_output="`$ACLOCAL $SEARCH_DIRS $ACLOCAL_OPTIONS 2>&1`" 1280 | ret=$? 1281 | $VERBOSE_ECHO "$aclocal_output" 1282 | if [ ! $ret = 0 ] ; then $ECHO "ERROR: $ACLOCAL failed" && exit 2 ; fi 1283 | 1284 | ############## 1285 | # libtoolize # 1286 | ############## 1287 | if libtoolize_needed ; then 1288 | if [ "x$HAVE_LIBTOOLIZE" = "xyes" ] ; then 1289 | $VERBOSE_ECHO "$LIBTOOLIZE $LIBTOOLIZE_OPTIONS" 1290 | libtoolize_output="`$LIBTOOLIZE $LIBTOOLIZE_OPTIONS 2>&1`" 1291 | ret=$? 1292 | $VERBOSE_ECHO "$libtoolize_output" 1293 | 1294 | if [ ! $ret = 0 ] ; then $ECHO "ERROR: $LIBTOOLIZE failed" && exit 2 ; fi 1295 | else 1296 | if [ "x$HAVE_ALT_LIBTOOLIZE" = "xyes" ] ; then 1297 | $VERBOSE_ECHO "$LIBTOOLIZE $ALT_LIBTOOLIZE_OPTIONS" 1298 | libtoolize_output="`$LIBTOOLIZE $ALT_LIBTOOLIZE_OPTIONS 2>&1`" 1299 | ret=$? 1300 | $VERBOSE_ECHO "$libtoolize_output" 1301 | 1302 | if [ ! $ret = 0 ] ; then $ECHO "ERROR: $LIBTOOLIZE failed" && exit 2 ; fi 1303 | fi 1304 | fi 1305 | 1306 | ########### 1307 | # aclocal # 1308 | ########### 1309 | # re-run again as instructed by libtoolize 1310 | $VERBOSE_ECHO "$ACLOCAL $SEARCH_DIRS $ACLOCAL_OPTIONS" 1311 | aclocal_output="`$ACLOCAL $SEARCH_DIRS $ACLOCAL_OPTIONS 2>&1`" 1312 | ret=$? 1313 | $VERBOSE_ECHO "$aclocal_output" 1314 | 1315 | # libtoolize might put ltmain.sh in the wrong place 1316 | if test -f ltmain.sh ; then 1317 | if test ! -f "${_aux_dir}/ltmain.sh" ; then 1318 | $ECHO 1319 | $ECHO "Warning: $LIBTOOLIZE is creating ltmain.sh in the wrong directory" 1320 | $ECHO 1321 | $ECHO "Fortunately, the problem can be worked around by simply copying the" 1322 | $ECHO "file to the appropriate location (${_aux_dir}/). This has been done for you." 1323 | $ECHO 1324 | $VERBOSE_ECHO "cp -p ltmain.sh \"${_aux_dir}/ltmain.sh\"" 1325 | cp -p ltmain.sh "${_aux_dir}/ltmain.sh" 1326 | $ECHO $ECHO_N "Continuing build preparation ... $ECHO_C" 1327 | fi 1328 | fi # ltmain.sh 1329 | 1330 | if [ "x$DOWNLOAD" = "xyes" ] ; then 1331 | download_gnulib_config_guess 1332 | fi 1333 | fi # libtoolize_needed 1334 | 1335 | ############ 1336 | # autoconf # 1337 | ############ 1338 | $VERBOSE_ECHO 1339 | $VERBOSE_ECHO "$AUTOCONF $AUTOCONF_OPTIONS" 1340 | autoconf_output="`$AUTOCONF $AUTOCONF_OPTIONS 2>&1`" 1341 | ret=$? 1342 | $VERBOSE_ECHO "$autoconf_output" 1343 | 1344 | if [ ! $ret = 0 ] ; then 1345 | # retry without the -f and check for usage of macros that are too new 1346 | ac2_59_macros="AC_C_RESTRICT AC_INCLUDES_DEFAULT AC_LANG_ASSERT AC_LANG_WERROR AS_SET_CATFILE" 1347 | ac2_55_macros="AC_COMPILER_IFELSE AC_FUNC_MBRTOWC AC_HEADER_STDBOOL AC_LANG_CONFTEST AC_LANG_SOURCE AC_LANG_PROGRAM AC_LANG_CALL AC_LANG_FUNC_TRY_LINK AC_MSG_FAILURE AC_PREPROC_IFELSE" 1348 | ac2_54_macros="AC_C_BACKSLASH_A AC_CONFIG_LIBOBJ_DIR AC_GNU_SOURCE AC_PROG_EGREP AC_PROG_FGREP AC_REPLACE_FNMATCH AC_FUNC_FNMATCH_GNU AC_FUNC_REALLOC AC_TYPE_MBSTATE_T" 1349 | 1350 | macros_to_search="" 1351 | ac_major="`echo ${AUTOCONF_VERSION}. | cut -d. -f1 | sed 's/[^0-9]//g'`" 1352 | ac_minor="`echo ${AUTOCONF_VERSION}. | cut -d. -f2 | sed 's/[^0-9]//g'`" 1353 | 1354 | if [ $ac_major -lt 2 ] ; then 1355 | macros_to_search="$ac2_59_macros $ac2_55_macros $ac2_54_macros" 1356 | else 1357 | if [ $ac_minor -lt 54 ] ; then 1358 | macros_to_search="$ac2_59_macros $ac2_55_macros $ac2_54_macros" 1359 | elif [ $ac_minor -lt 55 ] ; then 1360 | macros_to_search="$ac2_59_macros $ac2_55_macros" 1361 | elif [ $ac_minor -lt 59 ] ; then 1362 | macros_to_search="$ac2_59_macros" 1363 | fi 1364 | fi 1365 | 1366 | configure_ac_macros=__none__ 1367 | for feature in $macros_to_search ; do 1368 | $VERBOSE_ECHO "Searching for $feature in $CONFIGURE" 1369 | found="`grep \"^$feature.*\" $CONFIGURE`" 1370 | if [ ! "x$found" = "x" ] ; then 1371 | if [ "x$configure_ac_macros" = "x__none__" ] ; then 1372 | configure_ac_macros="$feature" 1373 | else 1374 | configure_ac_macros="$feature $configure_ac_macros" 1375 | fi 1376 | fi 1377 | done 1378 | if [ ! "x$configure_ac_macros" = "x__none__" ] ; then 1379 | $ECHO 1380 | $ECHO "Warning: Unsupported macros were found in $CONFIGURE" 1381 | $ECHO 1382 | $ECHO "The `basename \"$CONFIGURE\"` file was scanned in order to determine if any" 1383 | $ECHO "unsupported macros are used that exceed the minimum version" 1384 | $ECHO "settings specified within this file. As such, the following macros" 1385 | $ECHO "should be removed from configure.ac or the version numbers in this" 1386 | $ECHO "file should be increased:" 1387 | $ECHO 1388 | $ECHO "$configure_ac_macros" 1389 | $ECHO 1390 | $ECHO $ECHO_N "Ignorantly continuing build preparation ... $ECHO_C" 1391 | fi 1392 | 1393 | ################### 1394 | # autoconf, retry # 1395 | ################### 1396 | $VERBOSE_ECHO 1397 | $VERBOSE_ECHO "$AUTOCONF" 1398 | autoconf_output="`$AUTOCONF 2>&1`" 1399 | ret=$? 1400 | $VERBOSE_ECHO "$autoconf_output" 1401 | 1402 | if [ ! $ret = 0 ] ; then 1403 | # test if libtool is busted 1404 | libtool_failure "$autoconf_output" 1405 | 1406 | # let the user know what went wrong 1407 | cat <" 5 | description: "Bindings for golib - a library that (ab)uses gccgo to bring Go's channels and goroutines to the rest of the world" 6 | license: "BSD" 7 | srcDir: "src" 8 | 9 | [Deps] 10 | Requires: "nim > 0.19.0" 11 | 12 | -------------------------------------------------------------------------------- /nim.cfg.in: -------------------------------------------------------------------------------- 1 | --threads:on 2 | --stackTrace:off 3 | -p:"./src" 4 | #-p:"$lib/packages/golib" 5 | --passC:"--std=gnu99 -fsplit-stack" 6 | --dynlibOverride:"go" 7 | --passL:"-fuse-ld=gold -Wl,--no-as-needed -lgolib -lgo" 8 | --parallelBuild:0 9 | --gc:@NIMGC@ 10 | 11 | -------------------------------------------------------------------------------- /src/golib.nim: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2015-2018, Ștefan Talpalaru 2 | # All rights reserved. 3 | # 4 | # Redistribution and use in source and binary forms, with or without 5 | # modification, are permitted provided that the following conditions are met: 6 | # 7 | # 1. Redistributions of source code must retain the above copyright notice, this 8 | # list of conditions and the following disclaimer. 9 | # 2. Redistributions in binary form must reproduce the above copyright notice, 10 | # this list of conditions and the following disclaimer in the documentation 11 | # and/or other materials provided with the distribution. 12 | # 13 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 14 | # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 15 | # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 16 | # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 17 | # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 18 | # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 19 | # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 20 | # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 21 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 22 | 23 | {.deadCodeElim: on.} 24 | 25 | import macros, strutils, typetraits 26 | 27 | const 28 | SELECT_DIR_SEND* = 1 29 | SELECT_DIR_RECV* = 2 30 | SELECT_DIR_DEFAULT* = 3 31 | 32 | type 33 | ## work around nimbase.h not looking for _Bool when defining NIM_BOOL 34 | ## this may require --passC:"--std=gnu99" (or c99, or a more recent standard) in the .cfg file 35 | cbool {.importc: "_Bool", nodecl.} = bool 36 | 37 | goroutine_type* = proc (x: pointer) {.cdecl.} 38 | 39 | chan_select_case* {.importc: "chan_select_case".} = object 40 | dir*: cuint 41 | chan*: pointer 42 | send*: pointer 43 | 44 | chan_select_cases* = UncheckedArray[chan_select_case] 45 | 46 | chan_select_result* {.importc: "chan_select_result".} = object 47 | chosen*: cint 48 | recv*: pointer 49 | recv_ok*: cbool 50 | 51 | chan_recv2_result* {.importc: "chan_recv2_result".} = object 52 | recv*: pointer 53 | ok*: cbool 54 | 55 | ## libgo and golib symbols 56 | proc go_go*(f: goroutine_type; a3: pointer): pointer {.cdecl, importc: "__go_go", header: "", discardable.} 57 | proc runtime_gomaxprocsfunc*(n: int32): int32 {.cdecl, importc: "runtime_gomaxprocsfunc", header: "", discardable.} 58 | proc getproccount*(): int32 {.cdecl, importc: "getproccount", header: "".} 59 | proc go_yield*() {.cdecl, importc: "go_yield", header: "".} 60 | proc golib_main_proc*(argc: cint; argv: cstringArray) {.cdecl, importc: "golib_main", header: "".} 61 | proc chan_make*(a2: cint): pointer {.cdecl, importc: "chan_make", header: "".} 62 | proc chan_send*(a2: pointer; a3: pointer) {.cdecl, importc: "chan_send", header: "".} 63 | proc chan_recv*(a2: pointer): pointer {.cdecl, importc: "chan_recv", header: "".} 64 | proc chan_recv2*(a2: pointer): chan_recv2_result {.cdecl, importc: "chan_recv2", header: "".} 65 | proc chan_close*(a2: pointer) {.cdecl, importc: "chan_close", header: "".} 66 | proc chan_select*(a2: ptr chan_select_case; a3: cint): chan_select_result {.cdecl, importc: "chan_select", header: "".} 67 | proc go_sleep_ms*(a2: int64) {.cdecl, importc: "go_sleep_ms", header: "".} 68 | 69 | ## macros 70 | {.push hint[GlobalVar]: off.} 71 | var 72 | gomain_proc {.compileTime.}: string 73 | 74 | proc get_proc_args_tuple_ref(prc: NimNode): NimNode {.compileTime.} = 75 | result = ident(prc.lineinfo.replace('.', '_').replace('(', '_') 76 | .replace(',', '_').replace(')', '_') & $prc[0] & "_args_tuple_ref_for_goroutine") 77 | 78 | proc count_params(prc: NimNode): int {.compileTime.} = 79 | for i in prc[3].children: 80 | if i.kind == nnkIdentDefs: 81 | result += i.len - 2 82 | 83 | macro goroutine*(prc: untyped): untyped = 84 | # echo treeRepr(prc) 85 | result = prc 86 | ## checks 87 | if prc.kind != nnkProcDef: 88 | error(prc.lineinfo & ": Cannot transform this node kind into a goroutine. Proc definition expected.") 89 | if prc[3][0].kind != nnkEmpty: 90 | error(prc.lineinfo & ": Goroutines should not return anything.") 91 | if prc[3].len > 1: 92 | ## we have parameters, move them in a tuple definition before the proc definition 93 | var 94 | num_params = count_params(prc) 95 | if num_params == 1: 96 | ## there's a single param and we can't assign (val) to a tuple without using the form (name: val) 97 | ## which we can't do in the 'go' macro, so add a second param 98 | prc[3].add( 99 | newIdentDefs( 100 | ident("bogus_param_for_goroutine"), 101 | ident("pointer") 102 | ) 103 | ) 104 | ## var (arg1, arg2, ...) = cast[ref tuple[arg1: type1, arg2: type2, ...]](args_for_goroutine)[] 105 | prc[6].insert(0, newNimNode(nnkVarSection).add( 106 | newNimNode(nnkVarTuple) 107 | )) 108 | for i in 1..(len(prc[3]) - 1): 109 | for j in 0..(prc[3][i].len - 3): 110 | prc[6][0][0].add(copyNimNode(prc[3][i][j])) 111 | prc[6][0][0].add( 112 | newEmptyNode(), 113 | newNimNode(nnkBracketExpr).add( 114 | newNimNode(nnkCast).add( 115 | newNimNode(nnkRefTy).add( 116 | newNimNode(nnkTupleTy) 117 | ), 118 | ident("args_for_goroutine") 119 | ) 120 | ) 121 | ) 122 | for i in 1..(len(prc[3]) - 1): 123 | prc[6][0][0].last()[0][0][0].add(copyNimTree(prc[3][i])) 124 | ## delete the params from the proc 125 | del(prc[3], 1, len(prc[3]) - 1) 126 | 127 | if num_params == 1: 128 | var push_pragma = parseStmt("{.push hint[XDeclaredButNotUsed]: off.}") 129 | var pop_pragma = parseStmt("{.pop.}") 130 | result = newStmtList(push_pragma, prc, pop_pragma) 131 | 132 | prc[3].add( 133 | newIdentDefs( 134 | ident("args_for_goroutine"), 135 | ident("pointer") 136 | ) 137 | ) 138 | 139 | ## pragma 140 | if prc[4].kind == nnkEmpty: 141 | prc[4] = newNimNode(nnkPragma) 142 | addIdentIfAbsent(prc[4], "cdecl") 143 | # echo treeRepr(result) 144 | # echo repr(result) 145 | 146 | macro gomain*(prc: untyped): untyped = 147 | result = prc 148 | ## save it in the global var so we can insert it in golib_main 149 | gomain_proc = $prc[0] 150 | ## pragma 151 | if prc[4].kind == nnkEmpty: 152 | prc[4] = newNimNode(nnkPragma) 153 | addIdentIfAbsent(prc[4], "cdecl") 154 | ## set assembler name 155 | prc[4].add( 156 | newNimNode(nnkExprColonExpr).add( 157 | ident("codegenDecl"), 158 | newStrLitNode("$1 $2$3 __asm__ (\"main.main\");\n$1 $2$3") 159 | ) 160 | ) 161 | 162 | macro call_gomain_proc(): untyped = newCall(ident(gomain_proc)) 163 | 164 | template golib_main*(): untyped = 165 | {.push hint[GlobalVar]: off.} 166 | var 167 | cmdCount {.importc.}: cint 168 | cmdLine {.importc.}: cstringArray 169 | golib_main_proc(cmdCount, cmdLine) 170 | ## avoid the XDeclaredButNotUsed hint and the need for --deadCodeElim:off 171 | ## (not reached during execution) 172 | call_gomain_proc() 173 | 174 | macro go*(c: untyped): untyped = 175 | # echo treeRepr(c) 176 | result = newStmtList() 177 | if len(c) == 1: 178 | c.insert(0, ident("go_go")) 179 | c.add(newNilLit()) 180 | result.add(c) 181 | else: 182 | ## we have parameters that need to be moved into a tuple reference, 183 | ## cast as a pointer and passed to the proc 184 | var 185 | proc_args_tuple_ref = get_proc_args_tuple_ref(c) 186 | ## var procname_args_tuple_ref_for_goroutine: ref type((arg1, arg2, ...)) 187 | result.add( 188 | newNimNode(nnkVarSection).add( 189 | newIdentDefs( 190 | proc_args_tuple_ref, 191 | newNimNode(nnkRefTy).add( 192 | newCall( 193 | "type", 194 | newNimNode(nnkPar) 195 | ) 196 | ) 197 | ) 198 | ) 199 | ) 200 | for i in 1..(len(c) - 1): 201 | result.last()[0][1][0][1].add(c[i]) 202 | if len(c) == 2: 203 | ## there's a single param and we can't assign (val) to a tuple without using the form (name: val) 204 | ## which we can't do in the 'go' macro, so add a second param 205 | result.last()[0][1][0][1].add(parseStmt("nil.pointer")) 206 | ## new(procname_args_tuple_ref_for_goroutine) 207 | result.add( 208 | newCall( 209 | ident("new"), 210 | proc_args_tuple_ref 211 | ) 212 | ) 213 | ## procname_args_tuple_ref_for_goroutine[] = (arg1, arg2, ...) 214 | result.add( 215 | newNimNode(nnkAsgn).add( 216 | newNimNode(nnkBracketExpr).add( 217 | proc_args_tuple_ref 218 | ) 219 | ).add( 220 | newNimNode(nnkPar) 221 | ) 222 | ) 223 | for i in 1..(len(c) - 1): 224 | result.last()[1].add(c[i]) 225 | if len(c) == 2: 226 | ## there's a single param and we can't assign (val) to a tuple without using the form (name: val) 227 | ## which we can't do in the 'go' macro, so add a second param 228 | result.last()[1].add(newNilLit()) 229 | ## go_go(procname, procname_args_tuple_ref_for_goroutine) 230 | result.add( 231 | newCall( 232 | ident("go_go"), 233 | c[0], 234 | proc_args_tuple_ref 235 | ) 236 | ) 237 | # echo treeRepr(result) 238 | # echo repr(result) 239 | 240 | {.push hint[XDeclaredButNotUsed]: off.} 241 | type 242 | chan_internal[T] = object 243 | real_chan: pointer 244 | capacity: cint 245 | chan*[T] = ref chan_internal[T] 246 | send_chan*[T] = distinct chan[T] 247 | recv_chan*[T] = distinct chan[T] 248 | chan_recv2_result_typed*[T] = object 249 | recv*: T 250 | ok*: bool 251 | {.pop.} 252 | 253 | ## getters 254 | proc get_chan*[T](c: chan[T]): pointer = 255 | if c == nil: 256 | result = nil 257 | else: 258 | result = c.real_chan 259 | 260 | proc get_chan*[T](c: send_chan[T]): pointer = 261 | result = chan[T](c).get_chan() 262 | 263 | proc get_chan*[T](c: recv_chan[T]): pointer = 264 | result = cast[chan[T]](c).get_chan() 265 | 266 | ## converters 267 | 268 | converter int_to_cint*(i: int): cint = 269 | result = cint(i) 270 | 271 | converter ref_to_pointer*[T](x: ref T): pointer = 272 | result = cast[pointer](x) 273 | 274 | converter chan_to_send_chan*[T](c: chan[T]): send_chan[T] = 275 | result = send_chan[T](c) 276 | 277 | converter chan_to_recv_chan*[T](c: chan[T]): recv_chan[T] = 278 | result = recv_chan[T](c) 279 | 280 | ## debugging helpers 281 | 282 | proc `$`*[T](c: chan[T]): string = 283 | if c == nil: 284 | result = "chan($#): nil" % T.name 285 | else: 286 | result = "chan($#, $#): $#" % [T.name, $(c.capacity), $(cast[uint](c.get_chan))] 287 | 288 | proc `$`*[T](c: send_chan[T]): string = 289 | result = "send_" & $(chan[T](c)) 290 | 291 | proc `$`*[T](c: recv_chan[T]): string = 292 | result = "recv_" & $(chan[T](c)) 293 | 294 | proc `$`*(x: chan_select_case): string = 295 | var direction: array[1..3, string] = ["SEND", "RECV", "DEFAULT"] 296 | result = "chan_select_case(dir: $#, chan: $#, send: $#)" % [direction[x.dir], $(cast[uint](x.chan)), $(cast[uint](x.send))] 297 | 298 | proc to_string*(x: chan_select_case, T: typedesc): string = 299 | var 300 | direction: array[1..3, string] = ["SEND", "RECV", "DEFAULT"] 301 | send = "nil" 302 | if x.send != nil: 303 | send = $((cast[ptr T](x.send))[]) 304 | result = "chan_select_case(dir: $#, chan: $#, send: $#)" % [direction[x.dir], $(cast[uint](x.chan)), send] 305 | 306 | ## public chan API 307 | 308 | proc make_chan*(T: typedesc, n: cint = 0): chan[T] = 309 | new(result) 310 | result.real_chan = chan_make(n) 311 | result.capacity = n 312 | 313 | proc send*[T](c: chan[T], m: T) = 314 | var m_copy: ref T 315 | new(m_copy) 316 | deepCopy(m_copy[], m) 317 | chan_send(c.get_chan, cast[pointer](m_copy)) 318 | 319 | proc send*[T](c: send_chan[T], m: T) = 320 | cast[chan[T]](c).send(m) 321 | 322 | proc `<-`*[T](c: chan[T], m: T) = 323 | c.send(m) 324 | 325 | proc `<-`*[T](c: send_chan[T], m: T) = 326 | cast[chan[T]](c) <- m 327 | 328 | proc recv*[T](c: chan[T]): ref T {.discardable.} = 329 | result = cast[ref T](chan_recv(c.get_chan)) 330 | 331 | proc recv*[T](c: recv_chan[T]): ref T {.discardable.} = 332 | result = cast[chan[T]](c).recv() 333 | 334 | proc `<-`*[T](c: chan[T]): T {.discardable.} = 335 | result = c.recv()[] 336 | 337 | proc `<-`*[T](c: recv_chan[T]): T {.discardable.} = 338 | result = <- cast[chan[T]](c) 339 | 340 | proc recv2*[T](c: chan[T]): chan_recv2_result_typed[T] = 341 | var 342 | res: chan_recv2_result = chan_recv2(c.get_chan) 343 | recv = cast[ref T](res.recv) 344 | if unlikely(recv == nil): 345 | reset(result.recv) 346 | else: 347 | result.recv = recv[] 348 | result.ok = res.ok 349 | 350 | proc recv2*[T](c: recv_chan[T]): chan_recv2_result_typed[T] = 351 | result = cast[chan[T]](c).recv2() 352 | 353 | proc `<--`*[T](c: chan[T]): (T, bool) = 354 | var res = c.recv2() 355 | result = (res.recv, res.ok) 356 | 357 | proc `<--`*[T](c: recv_chan[T]): (T, bool) = 358 | result = <-- cast[chan[T]](c) 359 | 360 | proc close*[T](c: chan[T]) = 361 | chan_close(c.get_chan) 362 | 363 | proc close*[T](c: send_chan[T]) = 364 | cast[chan[T]](c).close() 365 | 366 | iterator items*[T](c: chan[T]): T = 367 | var m: T 368 | var ok = true 369 | while ok: 370 | (m, ok) = <--c 371 | if ok: 372 | yield m 373 | 374 | iterator items*[T](c: recv_chan[T]): T = 375 | for m in cast[chan[T]](c): 376 | yield m 377 | 378 | macro select*(s: untyped): untyped = 379 | # echo treeRepr(s) 380 | result = newStmtList() 381 | var rw_scases, default_scases: int 382 | for scase in s.children: 383 | if (scase.kind in {nnkCommand, nnkCall} and $(scase[0]) == "scase") or (scase.kind == nnkInfix and $(scase[1]) == "scase"): 384 | inc rw_scases 385 | elif scase.kind == nnkCall and $(scase[0]) == "default": 386 | inc default_scases 387 | else: 388 | error(scase.lineinfo & ": Unsupported scase in select.") 389 | if rw_scases == 0: 390 | error(s.lineinfo & ": No send or receive scases.") 391 | if default_scases > 1: 392 | error(s.lineinfo & ": More than one 'default' scase.") 393 | var 394 | var_prefix = s.lineinfo.replace('.', '_').replace('(', '_').replace(',', '_').replace(')', '_') 395 | select_cases_var = ident(var_prefix & "select_cases") 396 | select_result_var = ident(var_prefix & "select_result") 397 | scase_no = -1 398 | send_var_statements: seq[NimNode] = @[] 399 | select_cases: seq[NimNode] = @[] 400 | case_branches: seq[NimNode] = @[] 401 | 402 | for scase in s.children: 403 | inc scase_no 404 | if (scase.kind in {nnkCommand, nnkCall} and $(scase[0]) == "scase") or (scase.kind == nnkInfix and $(scase[1]) == "scase"): 405 | ## a send_chan would skirt type checking by passing a pointer to the C function selecting it for receiving 406 | ## let's make sure that everything in an 'scase' statement is compilable, or fail at compile time otherwise 407 | ## static: doAssert(compiles(...), "message") 408 | var test_expr = scase[1] 409 | if scase[1].kind == nnkExprEqExpr: 410 | # it seems compiles() fails with assignments, so just check the right hand side 411 | test_expr = scase[1][1] 412 | result.add( 413 | newNimNode(nnkStaticStmt).add( 414 | newNimNode(nnkStmtList).add( 415 | newCall( 416 | "doAssert", 417 | newCall( 418 | "compiles", 419 | test_expr 420 | ), 421 | newStrLitNode("- scase does not compile - $1: '$2'" % [scase.lineinfo, repr(test_expr)]) 422 | ) 423 | ) 424 | ) 425 | ) 426 | if scase[1].kind == nnkInfix and $(scase[1][0]) == "<-": 427 | ## send to channel 428 | var 429 | send_var = ident(var_prefix & "send" & $scase_no) 430 | send_var_ref = ident(var_prefix & "send" & $scase_no & "_ref") 431 | ## var 432 | ## send1 = GetValue() 433 | ## send1_ref: ref type(send1) 434 | send_var_statements.add( 435 | newNimNode(nnkVarSection).add( 436 | newIdentDefs( 437 | send_var, 438 | newEmptyNode(), 439 | scase[1][2] 440 | ), 441 | newIdentDefs( 442 | send_var_ref, 443 | newNimNode(nnkRefTy).add(newCall(ident("type"), send_var)) 444 | ) 445 | ) 446 | ) 447 | ## new(send1_ref) 448 | send_var_statements.add( 449 | newCall(ident("new"), send_var_ref) 450 | ) 451 | ## send1_ref[] = send1 452 | send_var_statements.add( 453 | newNimNode(nnkAsgn).add( 454 | newNimNode(nnkBracketExpr).add(send_var_ref), 455 | send_var 456 | ) 457 | ) 458 | 459 | ## chan_select_case(dir: SELECT_DIR_SEND, chan: a.get_chan, send: send1_ref), 460 | select_cases.add( 461 | newNimNode(nnkObjConstr).add( 462 | ident("chan_select_case"), 463 | newNimNode(nnkExprColonExpr).add( 464 | ident("dir"), 465 | ident("SELECT_DIR_SEND") 466 | ), 467 | newNimNode(nnkExprColonExpr).add( 468 | ident("chan"), 469 | newNimNode(nnkDotExpr).add( 470 | scase[1][1], 471 | ident("get_chan") 472 | ) 473 | ), 474 | newNimNode(nnkExprColonExpr).add( 475 | ident("send"), 476 | send_var_ref 477 | ), 478 | ) 479 | ) 480 | 481 | ## case branch 482 | case_branches.add( 483 | newNimNode(nnkOfBranch).add( 484 | newIntLitNode(scase_no), 485 | scase[2] 486 | ) 487 | ) 488 | else: 489 | ## receive from channel 490 | var 491 | channel: NimNode 492 | statements: NimNode 493 | if scase.kind == nnkInfix: 494 | channel = scase[2] 495 | statements = scase[3] 496 | elif scase[1].kind == nnkPrefix: 497 | channel = scase[1][1] 498 | statements = scase[2] 499 | else: 500 | channel = scase[1][1][1] 501 | statements = scase[2] 502 | if $(scase[1][1][0]) == "<-": 503 | ## i2 = cast[ptr type(i2)](res.recv)[] 504 | statements.insert(0, newNimNode(nnkAsgn).add( 505 | scase[1][0], 506 | newNimNode(nnkBracketExpr).add( 507 | newNimNode(nnkCast).add( 508 | newNimNode(nnkPtrTy).add( 509 | newCall("type", scase[1][0]) 510 | ), 511 | newNimNode(nnkDotExpr).add( 512 | select_result_var, 513 | ident("recv") 514 | ) 515 | ) 516 | ) 517 | )) 518 | elif $(scase[1][1][0]) == "<--": 519 | ## i3 = cast[ptr type(i3)](res.recv)[] 520 | ## ok3 = res.recv_ok 521 | statements.insert(0, newNimNode(nnkAsgn).add( 522 | scase[1][0][0], 523 | newNimNode(nnkBracketExpr).add( 524 | newNimNode(nnkCast).add( 525 | newNimNode(nnkPtrTy).add( 526 | newCall("type", scase[1][0][0]) 527 | ), 528 | newNimNode(nnkDotExpr).add( 529 | select_result_var, 530 | ident("recv") 531 | ) 532 | ) 533 | ) 534 | )) 535 | statements.insert(1, newNimNode(nnkAsgn).add( 536 | scase[1][0][1], 537 | newNimNode(nnkDotExpr).add( 538 | select_result_var, 539 | ident("recv_ok") 540 | ) 541 | )) 542 | ## chan_select_case(dir: SELECT_DIR_RECV, chan: c1.get_chan, send: nil), 543 | select_cases.add( 544 | newNimNode(nnkObjConstr).add( 545 | ident("chan_select_case"), 546 | newNimNode(nnkExprColonExpr).add( 547 | ident("dir"), 548 | ident("SELECT_DIR_RECV") 549 | ), 550 | newNimNode(nnkExprColonExpr).add( 551 | ident("chan"), 552 | newNimNode(nnkDotExpr).add( 553 | channel, 554 | ident("get_chan") 555 | ) 556 | ), 557 | newNimNode(nnkExprColonExpr).add( 558 | ident("send"), 559 | newNilLit() 560 | ), 561 | ) 562 | ) 563 | 564 | ## case branch 565 | case_branches.add( 566 | newNimNode(nnkOfBranch).add( 567 | newIntLitNode(scase_no), 568 | statements 569 | ) 570 | ) 571 | elif scase.kind == nnkCall and $(scase[0]) == "default": 572 | ## chan_select_case(dir: SELECT_DIR_DEFAULT, chan: nil, send: nil), 573 | select_cases.add( 574 | newNimNode(nnkObjConstr).add( 575 | ident("chan_select_case"), 576 | newNimNode(nnkExprColonExpr).add( 577 | ident("dir"), 578 | ident("SELECT_DIR_DEFAULT") 579 | ), 580 | newNimNode(nnkExprColonExpr).add( 581 | ident("chan"), 582 | newNilLit() 583 | ), 584 | newNimNode(nnkExprColonExpr).add( 585 | ident("send"), 586 | newNilLit() 587 | ), 588 | ) 589 | ) 590 | ## case branch 591 | case_branches.add( 592 | newNimNode(nnkOfBranch).add( 593 | newIntLitNode(scase_no), 594 | scase[1] 595 | ) 596 | ) 597 | 598 | result.add(send_var_statements) 599 | ## var 600 | ## select_cases = [ 601 | ## chan_select_case(dir: SELECT_DIR_SEND, chan: a.get_chan, send: send1_ref), 602 | ## chan_select_case(dir: SELECT_DIR_SEND, chan: b.get_chan, send: send2_ref), 603 | ## chan_select_case(dir: SELECT_DIR_DEFAULT, chan: nil, send: nil), 604 | ## ] 605 | ## res = chan_select(addr select_cases[0], len(select_cases)) 606 | result.add(newNimNode(nnkVarSection).add( 607 | newIdentDefs( 608 | select_cases_var, 609 | newEmptyNode(), 610 | newNimNode(nnkBracket).add(select_cases) 611 | ), 612 | newIdentDefs( 613 | select_result_var, 614 | newEmptyNode(), 615 | newCall( 616 | "chan_select", 617 | newNimNode(nnkCommand).add( 618 | ident("addr"), 619 | newNimNode(nnkBracketExpr).add( 620 | select_cases_var, 621 | newIntLitNode(0) 622 | ) 623 | ), 624 | newCall("len", select_cases_var) 625 | ) 626 | ) 627 | )) 628 | ## case res.chosen: 629 | ## of 0: 630 | ## ... 631 | ## else: 632 | ## discard 633 | result.add( 634 | newNimNode(nnkCaseStmt).add( 635 | newNimNode(nnkDotExpr).add( 636 | select_result_var, 637 | ident("chosen") 638 | ) 639 | ).add( 640 | case_branches 641 | ).add( 642 | newNimNode(nnkElse).add( 643 | newNimNode(nnkStmtList).add( 644 | newNimNode(nnkDiscardStmt).add( 645 | newEmptyNode() 646 | ) 647 | ) 648 | ) 649 | ) 650 | ) 651 | 652 | # echo treeRepr(result) 653 | 654 | -------------------------------------------------------------------------------- /tests/bug234.go: -------------------------------------------------------------------------------- 1 | // run 2 | 3 | // Copyright 2009 The Go Authors. All rights reserved. 4 | // Use of this source code is governed by a BSD-style 5 | // license that can be found in the LICENSE file. 6 | 7 | package main 8 | 9 | func main() { 10 | c := make(chan int, 1) 11 | c <- 100 12 | x, ok := <-c 13 | if x != 100 || !ok { 14 | println("x=", x, " ok=", ok, " want 100, true") 15 | panic("fail") 16 | } 17 | close(c) 18 | x, ok = <-c 19 | if x != 0 || ok { 20 | println("x=", x, " ok=", ok, " want 0, false") 21 | panic("fail") 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /tests/bug234.nim: -------------------------------------------------------------------------------- 1 | import golib 2 | 3 | proc go_main() {.gomain.} = 4 | var c = make_chan(int, 1) 5 | c <- 100 6 | var (x, ok) = <--c 7 | if x != 100 or not ok: 8 | echo("x=", x, " ok=", ok, " want 100, true") 9 | quit("fail") 10 | close(c) 11 | var (x2, ok2) = <--c 12 | if x2 != 0 or ok2: 13 | echo("x=", x2, " ok=", ok2, " want 0, false") 14 | quit("fail") 15 | 16 | golib_main() 17 | # not reached 18 | 19 | -------------------------------------------------------------------------------- /tests/gc/bintrees.nim: -------------------------------------------------------------------------------- 1 | # -*- nim -*- 2 | 3 | import os, strutils, golib 4 | 5 | type 6 | PNode = ref TNode 7 | TNode {.final, acyclic.} = object 8 | left, right: PNode 9 | item: int 10 | 11 | proc checkTree(node: PNode): int = 12 | result = node.item 13 | if node.left != nil: 14 | inc result, checkTree(node.left) - checkTree(node.right) 15 | 16 | proc makeTreeAux(item, depth: int): PNode = 17 | new(result) 18 | result.item = item 19 | if depth > 0: 20 | result.left = makeTreeAux(2 * item - 1, depth - 1) 21 | result.right = makeTreeAux(2 * item, depth - 1) 22 | 23 | proc makeTree(item, depth: int): PNode = 24 | #GC_disable() 25 | result = makeTreeAux(item, depth) 26 | #GC_enable() 27 | 28 | proc go_main() {.gomain.} = 29 | # var n = parseInt(paramStr(1)) 30 | var n = 10 31 | const minDepth = 4 32 | var maxDepth = if minDepth+2 > n: minDepth+2 else: n 33 | 34 | var stretchDepth = maxDepth + 1 35 | 36 | echo("stretch tree of depth ", stretchDepth, "\t check: ", checkTree( 37 | makeTree(0, stretchDepth))) 38 | 39 | var longLivedTree = makeTree(0, maxDepth) 40 | 41 | var iterations = 1 shl maxDepth 42 | for depth in countup(minDepth, stretchDepth-1, 2): 43 | var check = 0 44 | for i in 1..iterations: 45 | check += checkTree(makeTree(i, depth)) + checkTree(makeTree(-i, depth)) 46 | 47 | echo(iterations*2, "\t trees of depth ", depth, "\t check: ", check) 48 | iterations = iterations div 4 49 | 50 | echo("long lived tree of depth ", maxDepth, "\t check: ", 51 | longLivedTree.checkTree) 52 | echo GC_getstatistics() 53 | 54 | 55 | golib_main() 56 | # not reached 57 | 58 | -------------------------------------------------------------------------------- /tests/gc/closureleak.nim: -------------------------------------------------------------------------------- 1 | discard """ 2 | outputsub: "true" 3 | """ 4 | 5 | import golib 6 | from strutils import join 7 | 8 | type 9 | TFoo * = object 10 | id: int 11 | fn: proc(){.closure.} 12 | var foo_counter = 0 13 | # var alive_foos = newseq[int](0) 14 | var alive_foos: seq[int] 15 | 16 | proc free*(some: ref TFoo) = 17 | #echo "Tfoo #", some.id, " freed" 18 | alive_foos.del alive_foos.find(some.id) 19 | proc newFoo*(): ref TFoo = 20 | new result, free 21 | 22 | result.id = foo_counter 23 | alive_foos.add result.id 24 | inc foo_counter 25 | 26 | proc go_main() {.gomain.} = 27 | alive_foos = @[] 28 | 29 | for i in 0 ..< 10: 30 | discard newFoo() 31 | 32 | for i in 0 ..< 10: 33 | let f = newFoo() 34 | f.fn = proc = 35 | echo f.id 36 | 37 | GC_fullcollect() 38 | echo alive_foos.len <= 3 39 | 40 | golib_main() 41 | # not reached 42 | 43 | -------------------------------------------------------------------------------- /tests/gc/cyclecollector.nim: -------------------------------------------------------------------------------- 1 | 2 | # Program to detect bug #1796 reliably 3 | 4 | import golib 5 | 6 | type 7 | Node = ref object 8 | a, b: Node 9 | leaf: string 10 | 11 | proc createCycle(leaf: string): Node = 12 | new result 13 | result.a = result 14 | shallowCopy result.leaf, leaf 15 | 16 | proc go_main() {.gomain.} = 17 | for i in 0 .. 100_000: 18 | var leaf = "this is the leaf. it allocates" 19 | let x = createCycle(leaf) 20 | let y = createCycle(leaf) 21 | echo "done ", getOccupiedMem() 22 | 23 | golib_main() 24 | # not reached 25 | 26 | -------------------------------------------------------------------------------- /tests/gc/cycleleak.nim: -------------------------------------------------------------------------------- 1 | discard """ 2 | outputsub: "no leak: " 3 | """ 4 | 5 | import golib 6 | 7 | type 8 | Module = object 9 | nodes*: seq[PNode] 10 | id: int 11 | 12 | PModule = ref Module 13 | 14 | Node = object 15 | owner*: PModule 16 | data*: array[0..200, char] # some fat to drain memory faster 17 | id: int 18 | 19 | PNode = ref Node 20 | 21 | var 22 | gid: int 23 | 24 | when false: 25 | proc finalizeNode(x: PNode) = 26 | echo "node id: ", x.id 27 | proc finalizeModule(x: PModule) = 28 | echo "module id: ", x.id 29 | 30 | proc newNode(owner: PModule): PNode = 31 | new(result) 32 | result.owner = owner 33 | inc gid 34 | result.id = gid 35 | 36 | proc compileModule: PModule = 37 | new(result) 38 | result.nodes = @[] 39 | for i in 0..100: 40 | result.nodes.add newNode(result) 41 | inc gid 42 | result.id = gid 43 | 44 | var gModuleCache: PModule 45 | 46 | proc go_main() {.gomain.} = 47 | for i in 0..1000: 48 | gModuleCache = compileModule() 49 | gModuleCache = nil 50 | GC_fullCollect() 51 | 52 | if getOccupiedMem() > 9_000_000: 53 | echo "still a leak! ", getOccupiedMem() 54 | quit(1) 55 | echo "no leak: ", getOccupiedMem() 56 | 57 | 58 | golib_main() 59 | # not reached 60 | 61 | -------------------------------------------------------------------------------- /tests/gc/gcbench.nim: -------------------------------------------------------------------------------- 1 | discard """ 2 | outputsub: "Success!" 3 | """ 4 | 5 | # This is adapted from a benchmark written by John Ellis and Pete Kovac 6 | # of Post Communications. 7 | # It was modified by Hans Boehm of Silicon Graphics. 8 | # 9 | # This is no substitute for real applications. No actual application 10 | # is likely to behave in exactly this way. However, this benchmark was 11 | # designed to be more representative of real applications than other 12 | # Java GC benchmarks of which we are aware. 13 | # It attempts to model those properties of allocation requests that 14 | # are important to current GC techniques. 15 | # It is designed to be used either to obtain a single overall performance 16 | # number, or to give a more detailed estimate of how collector 17 | # performance varies with object lifetimes. It prints the time 18 | # required to allocate and collect balanced binary trees of various 19 | # sizes. Smaller trees result in shorter object lifetimes. Each cycle 20 | # allocates roughly the same amount of memory. 21 | # Two data structures are kept around during the entire process, so 22 | # that the measured performance is representative of applications 23 | # that maintain some live in-memory data. One of these is a tree 24 | # containing many pointers. The other is a large array containing 25 | # double precision floating point numbers. Both should be of comparable 26 | # size. 27 | # 28 | # The results are only really meaningful together with a specification 29 | # of how much memory was used. It is possible to trade memory for 30 | # better time performance. This benchmark should be run in a 32 MB 31 | # heap, though we don't currently know how to enforce that uniformly. 32 | # 33 | # Unlike the original Ellis and Kovac benchmark, we do not attempt 34 | # measure pause times. This facility should eventually be added back 35 | # in. There are several reasons for omitting it for now. The original 36 | # implementation depended on assumptions about the thread scheduler 37 | # that don't hold uniformly. The results really measure both the 38 | # scheduler and GC. Pause time measurements tend to not fit well with 39 | # current benchmark suites. As far as we know, none of the current 40 | # commercial Java implementations seriously attempt to minimize GC pause 41 | # times. 42 | # 43 | # Known deficiencies: 44 | # - No way to check on memory use 45 | # - No cyclic data structures 46 | # - No attempt to measure variation with object size 47 | # - Results are sensitive to locking cost, but we dont 48 | # check for proper locking 49 | # 50 | 51 | import 52 | strutils, times, golib 53 | 54 | type 55 | PNode = ref TNode 56 | TNode {.final.} = object 57 | left, right: PNode 58 | i, j: int 59 | 60 | proc newNode(L, r: PNode): PNode = 61 | new(result) 62 | result.left = L 63 | result.right = r 64 | 65 | const 66 | kStretchTreeDepth = 18 # about 16Mb 67 | kLongLivedTreeDepth = 16 # about 4Mb 68 | kArraySize = 500000 # about 4Mb 69 | kMinTreeDepth = 4 70 | kMaxTreeDepth = 16 71 | 72 | # Nodes used by a tree of a given size 73 | proc TreeSize(i: int): int = return ((1 shl (i + 1)) - 1) 74 | 75 | # Number of iterations to use for a given tree depth 76 | proc NumIters(i: int): int = 77 | return 2 * TreeSize(kStretchTreeDepth) div TreeSize(i) 78 | 79 | # Build tree top down, assigning to older objects. 80 | proc Populate(iDepth: int, thisNode: PNode) = 81 | if iDepth <= 0: 82 | return 83 | else: 84 | new(thisNode.left) 85 | new(thisNode.right) 86 | Populate(iDepth-1, thisNode.left) 87 | Populate(iDepth-1, thisNode.right) 88 | 89 | # Build tree bottom-up 90 | proc MakeTree(iDepth: int): PNode = 91 | if iDepth <= 0: 92 | new(result) 93 | else: 94 | return newNode(MakeTree(iDepth-1), MakeTree(iDepth-1)) 95 | 96 | proc PrintDiagnostics() = 97 | echo("Total memory available: " & $getTotalMem() & " bytes") 98 | echo("Free memory: " & $getFreeMem() & " bytes") 99 | 100 | proc TimeConstruction(depth: int) = 101 | var 102 | tempTree: PNode 103 | iNumIters: int 104 | 105 | iNumIters = NumIters(depth) 106 | 107 | echo("Creating " & $iNumIters & " trees of depth " & $depth) 108 | var t = epochTime() 109 | for i in 0..iNumIters-1: 110 | new(tempTree) 111 | Populate(depth, tempTree) 112 | tempTree = nil 113 | echo("\tTop down construction took " & $(epochTime() - t) & "msecs") 114 | t = epochTime() 115 | for i in 0..iNumIters-1: 116 | tempTree = MakeTree(depth) 117 | tempTree = nil 118 | echo("\tBottom up construction took " & $(epochTime() - t) & "msecs") 119 | 120 | type 121 | tMyArray = seq[float] 122 | 123 | proc go_main() {.gomain.} = 124 | when defined(GC_setMaxPause): 125 | GC_setMaxPause 2_000 126 | 127 | var 128 | longLivedTree, tempTree: PNode 129 | myarray: tMyArray 130 | 131 | echo("Garbage Collector Test") 132 | echo(" Stretching memory with a binary tree of depth " & $kStretchTreeDepth) 133 | PrintDiagnostics() 134 | var t = epochTime() 135 | 136 | # Stretch the memory space quickly 137 | tempTree = MakeTree(kStretchTreeDepth) 138 | tempTree = nil 139 | 140 | # Create a long lived object 141 | echo(" Creating a long-lived binary tree of depth " & 142 | $kLongLivedTreeDepth) 143 | new(longLivedTree) 144 | Populate(kLongLivedTreeDepth, longLivedTree) 145 | 146 | # Create long-lived array, filling half of it 147 | echo(" Creating a long-lived array of " & $kArraySize & " doubles") 148 | newSeq(myarray, kArraySize) 149 | for i in 0..kArraySize div 2 - 1: 150 | myarray[i] = 1.0 / toFloat(i) 151 | 152 | PrintDiagnostics() 153 | 154 | var d = kMinTreeDepth 155 | while d <= kMaxTreeDepth: 156 | TimeConstruction(d) 157 | inc(d, 2) 158 | 159 | if longLivedTree == nil or myarray[1000] != 1.0/1000.0: 160 | echo("Failed") 161 | # fake reference to LongLivedTree 162 | # and array to keep them from being optimized away 163 | 164 | var elapsed = epochTime() - t 165 | PrintDiagnostics() 166 | echo("Completed in " & $elapsed & "ms. Success!") 167 | 168 | golib_main() 169 | # not reached 170 | 171 | -------------------------------------------------------------------------------- /tests/gc/gcleak.nim: -------------------------------------------------------------------------------- 1 | discard """ 2 | outputsub: "no leak: " 3 | """ 4 | 5 | import golib 6 | 7 | when defined(GC_setMaxPause): 8 | GC_setMaxPause 2_000 9 | 10 | type 11 | TTestObj = object of RootObj 12 | x: string 13 | 14 | proc MakeObj(): TTestObj = 15 | result.x = "Hello" 16 | 17 | proc go_main() {.gomain.} = 18 | # var max_mem = 0 19 | for i in 1 .. 1_000_000: 20 | when defined(gcMarkAndSweep) or defined(boehmgc) or defined(gogc): 21 | if i mod 3000 == 0: 22 | GC_fullcollect() 23 | var obj = MakeObj() 24 | if getOccupiedMem() > 400_000: quit("still a leak!") 25 | # if getOccupiedMem() > max_mem: max_mem = getOccupiedMem() 26 | # echo max_mem 27 | 28 | echo "no leak: ", getOccupiedMem() 29 | 30 | 31 | golib_main() 32 | # not reached 33 | 34 | -------------------------------------------------------------------------------- /tests/gc/gcleak2.nim: -------------------------------------------------------------------------------- 1 | discard """ 2 | outputsub: "no leak: " 3 | """ 4 | 5 | import golib 6 | 7 | when defined(GC_setMaxPause): 8 | GC_setMaxPause 2_000 9 | 10 | type 11 | TTestObj = object of RootObj 12 | x: string 13 | s: seq[int] 14 | 15 | proc MakeObj(): TTestObj = 16 | result.x = "Hello" 17 | result.s = @[1,2,3] 18 | 19 | proc inProc() = 20 | for i in 1 .. 1_000_000: 21 | when defined(gcMarkAndSweep) or defined(boehmgc) or defined(gogc): 22 | if i mod 2000 == 0: 23 | GC_fullcollect() 24 | var obj: TTestObj 25 | obj = MakeObj() 26 | if getOccupiedMem() > 500_000: quit("still a leak!") 27 | 28 | proc go_main() {.gomain.} = 29 | inProc() 30 | echo "no leak: ", getOccupiedMem() 31 | 32 | 33 | golib_main() 34 | # not reached 35 | 36 | -------------------------------------------------------------------------------- /tests/gc/gcleak3.nim: -------------------------------------------------------------------------------- 1 | discard """ 2 | outputsub: "no leak: " 3 | """ 4 | 5 | import golib 6 | 7 | when defined(GC_setMaxPause): 8 | GC_setMaxPause 2_000 9 | 10 | type 11 | TSomething = object 12 | s: string 13 | s1: string 14 | # var s: seq[TSomething] = @[] 15 | var s: seq[TSomething] 16 | 17 | proc limit*[t](a: var seq[t]) = 18 | var loop = s.len() - 512 19 | for i in 0..loop: 20 | #echo i 21 | #GC_fullCollect() 22 | if getOccupiedMem() > 3000_000: quit("still a leak!") 23 | s.delete(i) 24 | 25 | proc go_main() {.gomain.} = 26 | s = @[] 27 | for i in 0..1024: 28 | var obj: TSomething 29 | obj.s = "blah" 30 | obj.s1 = "asd" 31 | s.add(obj) 32 | 33 | s.limit() 34 | 35 | echo "no leak: ", getOccupiedMem() 36 | 37 | golib_main() 38 | # not reached 39 | 40 | -------------------------------------------------------------------------------- /tests/gc/gcleak4.nim: -------------------------------------------------------------------------------- 1 | discard """ 2 | outputsub: "no leak: " 3 | """ 4 | 5 | import golib 6 | 7 | when defined(GC_setMaxPause): 8 | GC_setMaxPause 2_000 9 | 10 | type 11 | TExpr = object {.inheritable.} ## abstract base class for an expression 12 | TLiteral = object of TExpr 13 | x: int 14 | op1: string 15 | TPlusExpr = object of TExpr 16 | a, b: ref TExpr 17 | op2: string 18 | 19 | method eval(e: ref TExpr): int {.base.} = 20 | # override this base method 21 | quit "to override!" 22 | 23 | method eval(e: ref TLiteral): int = return e.x 24 | 25 | method eval(e: ref TPlusExpr): int = 26 | # watch out: relies on dynamic binding 27 | return eval(e.a) + eval(e.b) 28 | 29 | proc newLit(x: int): ref TLiteral = 30 | new(result) 31 | {.watchpoint: result.} 32 | result.x = x 33 | result.op1 = $getOccupiedMem() 34 | 35 | proc newPlus(a, b: ref TExpr): ref TPlusExpr = 36 | new(result) 37 | {.watchpoint: result.} 38 | result.a = a 39 | result.b = b 40 | result.op2 = $getOccupiedMem() 41 | 42 | const Limit = when compileOption("gc", "markAndSweep") or compileOption("gc", "boehm") or compileOption("gc", "go"): 5*1024*1024 else: 500_000 43 | 44 | proc go_main() {.gomain.} = 45 | for i in 0..100_000: 46 | var s: array[0..11, ref TExpr] 47 | for j in 0..high(s): 48 | s[j] = newPlus(newPlus(newLit(j), newLit(2)), newLit(4)) 49 | if eval(s[j]) != j+6: 50 | quit "error: wrong result" 51 | if getOccupiedMem() > Limit: quit("still a leak!") 52 | 53 | echo "no leak: ", getOccupiedMem() 54 | 55 | golib_main() 56 | # not reached 57 | 58 | -------------------------------------------------------------------------------- /tests/gc/gcleak5.nim: -------------------------------------------------------------------------------- 1 | discard """ 2 | output: "success" 3 | """ 4 | 5 | import golib 6 | 7 | import os, times 8 | 9 | proc go_main() {.gomain.} = 10 | var i = 0 11 | for ii in 0..50_000: 12 | #while true: 13 | var t = getTime() 14 | var g = t.getGMTime() 15 | #echo isOnStack(addr g) 16 | 17 | if i mod 1000 == 0: 18 | GC_fullcollect() 19 | let om = getOccupiedMem() 20 | # echo "memory: ", om 21 | if om > 300_000: quit "leak" 22 | 23 | inc(i) 24 | sleep(1) 25 | 26 | echo "success" 27 | 28 | 29 | golib_main() 30 | # not reached 31 | 32 | -------------------------------------------------------------------------------- /tests/gc/gctest.nim: -------------------------------------------------------------------------------- 1 | discard """ 2 | outputsub: "finished" 3 | """ 4 | 5 | # Test the garbage collector. 6 | 7 | import 8 | strutils, golib 9 | 10 | type 11 | PNode = ref TNode 12 | TNode {.final.} = object 13 | le, ri: PNode 14 | data: string 15 | 16 | TTable {.final.} = object 17 | counter, max: int 18 | data: seq[string] 19 | 20 | TBNode {.final.} = object 21 | other: PNode # a completely different tree 22 | data: string 23 | sons: seq[TBNode] # directly embedded! 24 | t: TTable 25 | 26 | TCaseKind = enum nkStr, nkWhole, nkList 27 | PCaseNode = ref TCaseNode 28 | TCaseNode {.final.} = object 29 | case kind: TCaseKind 30 | of nkStr: data: string 31 | of nkList: sons: seq[PCaseNode] 32 | else: unused: seq[string] 33 | 34 | TIdObj* = object of RootObj 35 | id*: int # unique id; use this for comparisons and not the pointers 36 | 37 | PIdObj* = ref TIdObj 38 | PIdent* = ref TIdent 39 | TIdent*{.acyclic.} = object of TIdObj 40 | s*: string 41 | next*: PIdent # for hash-table chaining 42 | h*: int # hash value of s 43 | 44 | var 45 | flip: int 46 | 47 | proc newCaseNode(data: string): PCaseNode = 48 | new(result) 49 | if flip == 0: 50 | result.kind = nkStr 51 | result.data = data 52 | else: 53 | result.kind = nkWhole 54 | result.unused = @["", "abc", "abdc"] 55 | flip = 1 - flip 56 | 57 | proc newCaseNode(a, b: PCaseNode): PCaseNode = 58 | new(result) 59 | result.kind = nkList 60 | result.sons = @[a, b] 61 | 62 | proc caseTree(lvl: int = 0): PCaseNode = 63 | if lvl == 3: result = newCaseNode("data item") 64 | else: result = newCaseNode(caseTree(lvl+1), caseTree(lvl+1)) 65 | 66 | proc finalizeNode(n: PNode) = 67 | assert(n != nil) 68 | write(stdout, "finalizing: ") 69 | writeline(stdout, "not nil") 70 | 71 | var 72 | id: int = 1 73 | 74 | proc buildTree(depth = 1): PNode = 75 | if depth == 7: return nil 76 | new(result, finalizeNode) 77 | result.le = buildTree(depth+1) 78 | result.ri = buildTree(depth+1) 79 | result.data = $id 80 | inc(id) 81 | 82 | proc returnTree(): PNode = 83 | writeline(stdout, "creating id: " & $id) 84 | new(result, finalizeNode) 85 | result.data = $id 86 | new(result.le, finalizeNode) 87 | result.le.data = $id & ".1" 88 | new(result.ri, finalizeNode) 89 | result.ri.data = $id & ".2" 90 | inc(id) 91 | 92 | # now create a cycle: 93 | writeline(stdout, "creating id (cyclic): " & $id) 94 | var cycle: PNode 95 | new(cycle, finalizeNode) 96 | cycle.data = $id 97 | cycle.le = cycle 98 | cycle.ri = cycle 99 | inc(id) 100 | #writeline(stdout, "refcount: " & $refcount(cycle)) 101 | #writeline(stdout, "refcount le: " & $refcount(cycle.le)) 102 | #writeline(stdout, "refcount ri: " & $refcount(cycle.ri)) 103 | 104 | proc printTree(t: PNode) = 105 | if t == nil: return 106 | writeline(stdout, "printing") 107 | writeline(stdout, t.data) 108 | printTree(t.le) 109 | printTree(t.ri) 110 | 111 | proc unsureNew(result: var PNode) = 112 | writeline(stdout, "creating unsure id: " & $id) 113 | new(result, finalizeNode) 114 | result.data = $id 115 | new(result.le, finalizeNode) 116 | result.le.data = $id & ".a" 117 | new(result.ri, finalizeNode) 118 | result.ri.data = $id & ".b" 119 | inc(id) 120 | 121 | proc setSons(n: var TBNode) = 122 | n.sons = @[] # free memory of the sons 123 | n.t.data = @[] 124 | var 125 | m: seq[string] 126 | m = @[] 127 | setLen(m, len(n.t.data) * 2) 128 | for i in 0..high(m): 129 | m[i] = "..." 130 | n.t.data = m 131 | 132 | proc buildBTree(father: var TBNode) = 133 | father.data = "father" 134 | father.other = nil 135 | father.sons = @[] 136 | for i in 1..10: 137 | write(stdout, "next iteration!\n") 138 | var n: TBNode 139 | n.other = returnTree() 140 | n.data = "B node: " & $i 141 | if i mod 2 == 0: n.sons = @[] # nil and [] need to be handled correctly! 142 | add father.sons, n 143 | father.t.counter = 0 144 | father.t.max = 3 145 | father.t.data = @["ha", "lets", "stress", "it"] 146 | setSons(father) 147 | 148 | proc getIdent(identifier: cstring, length: int, h: int): PIdent = 149 | new(result) 150 | result.h = h 151 | result.s = newString(length) 152 | 153 | proc main() = 154 | discard getIdent("addr", 4, 0) 155 | discard getIdent("hall", 4, 0) 156 | discard getIdent("echo", 4, 0) 157 | discard getIdent("huch", 4, 0) 158 | 159 | var 160 | father: TBNode 161 | for i in 1..1_00: 162 | buildBTree(father) 163 | 164 | for i in 1..1_00: 165 | var t = returnTree() 166 | var t2: PNode 167 | unsureNew(t2) 168 | write(stdout, "now building bigger trees: ") 169 | var t2: PNode 170 | for i in 1..100: 171 | t2 = buildTree() 172 | printTree(t2) 173 | write(stdout, "now test sequences of strings:") 174 | var s: seq[string] = @[] 175 | for i in 1..100: 176 | add s, "hohoho" # test reallocation 177 | writeline(stdout, s[89]) 178 | write(stdout, "done!\n") 179 | 180 | proc go_main() {.gomain.} = 181 | var 182 | father: TBNode 183 | s: string 184 | s = "" 185 | s = "" 186 | writeline(stdout, repr(caseTree())) 187 | father.t.data = @["ha", "lets", "stress", "it"] 188 | father.t.data = @["ha", "lets", "stress", "it"] 189 | var t = buildTree() 190 | write(stdout, repr(t[])) 191 | buildBTree(father) 192 | write(stdout, repr(father)) 193 | 194 | write(stdout, "starting main...\n") 195 | main() 196 | 197 | GC_fullCollect() 198 | # the M&S GC fails with this call and it's unclear why. Definitely something 199 | # we need to fix! 200 | GC_fullCollect() 201 | writeline(stdout, GC_getStatistics()) 202 | write(stdout, "finished\n") 203 | 204 | golib_main() 205 | # not reached 206 | 207 | -------------------------------------------------------------------------------- /tests/gc/growobjcrash.nim: -------------------------------------------------------------------------------- 1 | discard """ 2 | output: "works" 3 | """ 4 | 5 | import cgi, strtabs, golib 6 | 7 | proc handleRequest(query: string): StringTableRef = 8 | iterator foo(): StringTableRef {.closure.} = 9 | var params = {:}.newStringTable() 10 | for key, val in cgi.decodeData(query): 11 | params[key] = val 12 | yield params 13 | 14 | let x = foo 15 | result = x() 16 | 17 | const Limit = 5*1024*1024 18 | 19 | proc go_main() {.gomain.} = 20 | var counter = 0 21 | for i in 0 .. 100_000: 22 | for k, v in handleRequest("nick=Elina2&type=activate"): 23 | inc counter 24 | if counter mod 100 == 0: 25 | # echo getOccupiedMem() 26 | # echo GC_getStatistics() 27 | if getOccupiedMem() > Limit: 28 | quit "but now a leak" 29 | 30 | echo "works" 31 | 32 | golib_main() 33 | # not reached 34 | 35 | -------------------------------------------------------------------------------- /tests/gc/refarrayleak.nim: -------------------------------------------------------------------------------- 1 | discard """ 2 | outputsub: "no leak: " 3 | """ 4 | 5 | import golib 6 | 7 | type 8 | TNode = object 9 | data: array[0..300, char] 10 | 11 | PNode = ref TNode 12 | 13 | TNodeArray = array[0..10, PNode] 14 | 15 | TArrayHolder = object 16 | sons: TNodeArray 17 | 18 | proc nullify(a: var TNodeArray) = 19 | for i in 0..high(a): 20 | a[i] = nil 21 | 22 | proc newArrayHolder: ref TArrayHolder = 23 | new result 24 | 25 | for i in 0..high(result.sons): 26 | new result.sons[i] 27 | 28 | nullify result.sons 29 | 30 | proc go_main() {.gomain.} = 31 | for i in 0..10000: 32 | if i mod 2000 == 0: 33 | GC_fullcollect() 34 | discard newArrayHolder() 35 | 36 | if getOccupiedMem() > 300_000: 37 | echo "still a leak! ", getOccupiedMem() 38 | quit 1 39 | else: 40 | echo "no leak: ", getOccupiedMem() 41 | 42 | 43 | golib_main() 44 | # not reached 45 | 46 | -------------------------------------------------------------------------------- /tests/gc/stackrefleak.nim: -------------------------------------------------------------------------------- 1 | discard """ 2 | outputsub: "no leak: " 3 | """ 4 | 5 | import golib 6 | 7 | type 8 | Cyclic = object 9 | sibling: PCyclic 10 | data: array[0..200, char] 11 | 12 | PCyclic = ref Cyclic 13 | 14 | proc makePair: PCyclic = 15 | new(result) 16 | new(result.sibling) 17 | result.sibling.sibling = result 18 | 19 | proc go_main() {.gomain.} = 20 | for i in 0..10000: 21 | var x = makePair() 22 | GC_fullCollect() 23 | x = nil 24 | GC_fullCollect() 25 | 26 | if getOccupiedMem() > 400_000: 27 | echo "still a leak! ", getOccupiedMem() 28 | quit(1) 29 | else: 30 | echo "no leak: ", getOccupiedMem() 31 | 32 | 33 | golib_main() 34 | # not reached 35 | 36 | -------------------------------------------------------------------------------- /tests/peano.go: -------------------------------------------------------------------------------- 1 | // run 2 | 3 | // Copyright 2009 The Go Authors. All rights reserved. 4 | // Use of this source code is governed by a BSD-style 5 | // license that can be found in the LICENSE file. 6 | 7 | // Test that heavy recursion works. Simple torture test for 8 | // segmented stacks: do math in unary by recursion. 9 | 10 | package main 11 | 12 | type Number *Number 13 | 14 | // ------------------------------------- 15 | // Peano primitives 16 | 17 | func zero() *Number { 18 | return nil 19 | } 20 | 21 | func is_zero(x *Number) bool { 22 | return x == nil 23 | } 24 | 25 | func add1(x *Number) *Number { 26 | e := new(Number) 27 | *e = x 28 | return e 29 | } 30 | 31 | func sub1(x *Number) *Number { 32 | return *x 33 | } 34 | 35 | func add(x, y *Number) *Number { 36 | if is_zero(y) { 37 | return x 38 | } 39 | 40 | return add(add1(x), sub1(y)) 41 | } 42 | 43 | func mul(x, y *Number) *Number { 44 | if is_zero(x) || is_zero(y) { 45 | return zero() 46 | } 47 | 48 | return add(mul(x, sub1(y)), x) 49 | } 50 | 51 | func fact(n *Number) *Number { 52 | if is_zero(n) { 53 | return add1(zero()) 54 | } 55 | 56 | return mul(fact(sub1(n)), n) 57 | } 58 | 59 | // ------------------------------------- 60 | // Helpers to generate/count Peano integers 61 | 62 | func gen(n int) *Number { 63 | if n > 0 { 64 | return add1(gen(n - 1)) 65 | } 66 | 67 | return zero() 68 | } 69 | 70 | func count(x *Number) int { 71 | if is_zero(x) { 72 | return 0 73 | } 74 | 75 | return count(sub1(x)) + 1 76 | } 77 | 78 | func check(x *Number, expected int) { 79 | var c = count(x) 80 | if c != expected { 81 | print("error: found ", c, "; expected ", expected, "\n") 82 | panic("fail") 83 | } 84 | } 85 | 86 | // ------------------------------------- 87 | // Test basic functionality 88 | 89 | func init() { 90 | check(zero(), 0) 91 | check(add1(zero()), 1) 92 | check(gen(10), 10) 93 | 94 | check(add(gen(3), zero()), 3) 95 | check(add(zero(), gen(4)), 4) 96 | check(add(gen(3), gen(4)), 7) 97 | 98 | check(mul(zero(), zero()), 0) 99 | check(mul(gen(3), zero()), 0) 100 | check(mul(zero(), gen(4)), 0) 101 | check(mul(gen(3), add1(zero())), 3) 102 | check(mul(add1(zero()), gen(4)), 4) 103 | check(mul(gen(3), gen(4)), 12) 104 | 105 | check(fact(zero()), 1) 106 | check(fact(add1(zero())), 1) 107 | check(fact(gen(5)), 120) 108 | } 109 | 110 | // ------------------------------------- 111 | // Factorial 112 | 113 | var results = [...]int{ 114 | 1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800, 115 | 39916800, 479001600, 116 | } 117 | 118 | func main() { 119 | for i := 0; i <= 9; i++ { 120 | if f := count(fact(gen(i))); f != results[i] { 121 | println("FAIL:", i, "!:", f, "!=", results[i]) 122 | panic(0) 123 | } 124 | } 125 | } 126 | -------------------------------------------------------------------------------- /tests/peano.nim: -------------------------------------------------------------------------------- 1 | # Test that heavy recursion works. Simple torture test for 2 | # segmented stacks: do math in unary by recursion. 3 | 4 | import golib 5 | 6 | type 7 | Number = ref object 8 | n: Number 9 | 10 | # ------------------------------------- 11 | # Peano primitives 12 | 13 | proc zero(): Number = 14 | return nil 15 | 16 | proc is_zero(x: Number): bool = 17 | return x == nil 18 | 19 | proc add1(x: Number): Number = 20 | var e: Number 21 | new(e) 22 | e.n = x 23 | return e 24 | 25 | proc sub1(x: Number): Number = 26 | return x.n 27 | 28 | proc add(x, y: Number): Number = 29 | if is_zero(y): 30 | return x 31 | 32 | return add(add1(x), sub1(y)) 33 | 34 | proc mul(x, y: Number): Number = 35 | if is_zero(x) or is_zero(y): 36 | return zero() 37 | 38 | return add(mul(x, sub1(y)), x) 39 | 40 | proc fact(n: Number): Number = 41 | if is_zero(n): 42 | return add1(zero()) 43 | 44 | return mul(fact(sub1(n)), n) 45 | 46 | # ------------------------------------- 47 | # Helpers to generate/count Peano integers 48 | 49 | proc gen(n: int): Number = 50 | if n > 0: 51 | return add1(gen(n - 1)) 52 | 53 | return zero() 54 | 55 | proc count(x: Number): int = 56 | if is_zero(x): 57 | return 0 58 | 59 | return count(sub1(x)) + 1 60 | 61 | proc check(x: Number, expected: int) = 62 | var c = count(x) 63 | if c != expected: 64 | echo("error: found ", c, "; expected ", expected) 65 | quit("fail") 66 | 67 | # ------------------------------------- 68 | # Test basic functionality 69 | 70 | proc init() = 71 | check(zero(), 0) 72 | check(add1(zero()), 1) 73 | check(gen(10), 10) 74 | 75 | check(add(gen(3), zero()), 3) 76 | check(add(zero(), gen(4)), 4) 77 | check(add(gen(3), gen(4)), 7) 78 | 79 | check(mul(zero(), zero()), 0) 80 | check(mul(gen(3), zero()), 0) 81 | check(mul(zero(), gen(4)), 0) 82 | check(mul(gen(3), add1(zero())), 3) 83 | check(mul(add1(zero()), gen(4)), 4) 84 | check(mul(gen(3), gen(4)), 12) 85 | 86 | check(fact(zero()), 1) 87 | check(fact(add1(zero())), 1) 88 | check(fact(gen(5)), 120) 89 | 90 | # ------------------------------------- 91 | # Factorial 92 | 93 | var results = [ 94 | 1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800, 95 | 39916800, 479001600, 96 | ] 97 | 98 | proc go_main() {.gomain.} = 99 | init() 100 | for i in 0..9: 101 | if (var f = count(fact(gen(i))); f != results[i]): 102 | echo("FAIL: ", i, " !: ", f, " != ", results[i]) 103 | quit(0) 104 | 105 | 106 | golib_main() 107 | # not reached 108 | 109 | -------------------------------------------------------------------------------- /tests/perm.go: -------------------------------------------------------------------------------- 1 | // errorcheck 2 | 3 | // Copyright 2009 The Go Authors. All rights reserved. 4 | // Use of this source code is governed by a BSD-style 5 | // license that can be found in the LICENSE file. 6 | 7 | // Test various correct and incorrect permutations of send-only, 8 | // receive-only, and bidirectional channels. 9 | // Does not compile. 10 | 11 | package main 12 | 13 | var ( 14 | cr <-chan int 15 | cs chan<- int 16 | c chan int 17 | ) 18 | 19 | func main() { 20 | cr = c // ok 21 | cs = c // ok 22 | c = cr // ERROR "illegal types|incompatible|cannot" 23 | c = cs // ERROR "illegal types|incompatible|cannot" 24 | cr = cs // ERROR "illegal types|incompatible|cannot" 25 | cs = cr // ERROR "illegal types|incompatible|cannot" 26 | 27 | c <- 0 // ok 28 | <-c // ok 29 | x, ok := <-c // ok 30 | _, _ = x, ok 31 | 32 | cr <- 0 // ERROR "send" 33 | <-cr // ok 34 | x, ok = <-cr // ok 35 | _, _ = x, ok 36 | 37 | cs <- 0 // ok 38 | <-cs // ERROR "receive" 39 | x, ok = <-cs // ERROR "receive" 40 | _, _ = x, ok 41 | 42 | select { 43 | case c <- 0: // ok 44 | case x := <-c: // ok 45 | _ = x 46 | 47 | case cr <- 0: // ERROR "send" 48 | case x := <-cr: // ok 49 | _ = x 50 | 51 | case cs <- 0: // ok 52 | case x := <-cs: // ERROR "receive" 53 | _ = x 54 | } 55 | 56 | for _ = range cs { // ERROR "receive" 57 | } 58 | 59 | close(c) 60 | close(cs) 61 | close(cr) // ERROR "receive" 62 | } 63 | -------------------------------------------------------------------------------- /tests/perm.nim: -------------------------------------------------------------------------------- 1 | import golib 2 | 3 | 4 | var 5 | c: chan[int] 6 | cr: recv_chan[int] 7 | cs: send_chan[int] 8 | 9 | proc go_main() {.gomain.} = 10 | c = make_chan(int) 11 | cr = make_chan(int) 12 | cs = make_chan(int) 13 | cr = c # ok 14 | cs = c # ok 15 | static: doAssert compiles(<-cr) 16 | # static: doAssert(compiles(<-cs), "- some error") 17 | # static: doAssert(type(c) is chan or type(c) is recv_chan, "- the type must be 'chan' or 'recv_chan'") 18 | # c = cr # ERROR "illegal types|incompatible|cannot" 19 | # c = cs # ERROR "illegal types|incompatible|cannot" 20 | # cr = cs # ERROR "illegal types|incompatible|cannot" 21 | # cs = cr # ERROR "illegal types|incompatible|cannot" 22 | 23 | c <- 0 # ok 24 | <-c # ok 25 | var (x, ok) = <--c # ok 26 | var (a, b) = (x, ok) 27 | 28 | # cr <- 0 # ERROR "send" 29 | <-cr # ok 30 | (x, ok) = <--cr # ok 31 | (a, b) = (x, ok) 32 | 33 | cs <- 0 # ok 34 | # <-cs # ERROR "receive" 35 | # (x, ok) = <--cs # ERROR "receive" 36 | (a, b) = (x, ok) 37 | 38 | # static: doAssert(compiles(cs <- a), "- some error") 39 | select: 40 | scase c <- 0: discard # ok 41 | scase (x = <-c): # ok 42 | a = x 43 | 44 | # scase cr <- 0: discard # ERROR "send" 45 | scase (x = <-cr): # ok 46 | a = x 47 | 48 | scase cs <- 0: discard # ok 49 | # scase (x = <-cs): # ERROR "receive" 50 | # a = x 51 | 52 | for a in cr: discard 53 | # for a in cs: discard # ERROR "receive" 54 | 55 | close(c) 56 | close(cs) 57 | # close(cr) # ERROR "receive" 58 | 59 | golib_main() 60 | # not reached 61 | 62 | -------------------------------------------------------------------------------- /tests/select.go: -------------------------------------------------------------------------------- 1 | // run 2 | 3 | // Copyright 2009 The Go Authors. All rights reserved. 4 | // Use of this source code is governed by a BSD-style 5 | // license that can be found in the LICENSE file. 6 | 7 | // Test simple select. 8 | 9 | package main 10 | 11 | var counter uint 12 | var shift uint 13 | 14 | func GetValue() uint { 15 | counter++ 16 | return 1 << shift 17 | } 18 | 19 | func Send(a, b chan uint) int { 20 | var i int 21 | 22 | LOOP: 23 | for { 24 | select { 25 | case a <- GetValue(): 26 | i++ 27 | a = nil 28 | case b <- GetValue(): 29 | i++ 30 | b = nil 31 | default: 32 | break LOOP 33 | } 34 | shift++ 35 | } 36 | return i 37 | } 38 | 39 | func main() { 40 | a := make(chan uint, 1) 41 | b := make(chan uint, 1) 42 | if v := Send(a, b); v != 2 { 43 | println("Send returned", v, "!= 2") 44 | panic("fail") 45 | } 46 | if av, bv := <-a, <-b; av|bv != 3 { 47 | println("bad values", av, bv) 48 | panic("fail") 49 | } 50 | if v := Send(a, nil); v != 1 { 51 | println("Send returned", v, "!= 1") 52 | panic("fail") 53 | } 54 | if counter != 10 { 55 | println("counter is", counter, "!= 10") 56 | panic("fail") 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /tests/select.nim: -------------------------------------------------------------------------------- 1 | import golib 2 | 3 | var 4 | counter: uint 5 | shift: uint 6 | 7 | proc GetValue(): uint = 8 | inc counter 9 | return 1.uint shl shift 10 | 11 | proc Send(aa, bb: chan[uint]): int = 12 | var 13 | (a, b) = (aa, bb) 14 | i: int 15 | 16 | block LOOP: 17 | while true: 18 | select: 19 | scase a <- GetValue(): 20 | inc i 21 | a = nil 22 | scase b <- GetValue(): 23 | inc i 24 | b = nil 25 | default: 26 | break LOOP 27 | inc shift 28 | return i 29 | 30 | proc go_main() {.gomain.} = 31 | var 32 | a = make_chan(uint, 1) 33 | b = make_chan(uint, 1) 34 | if (var v = Send(a, b); v != 2): 35 | echo("Send returned ", v, " != 2") 36 | quit("fail") 37 | if (var (av, bv) = (<-a, <-b); (av or bv) != 3): 38 | echo("bad values ", av, " ", bv) 39 | quit("fail") 40 | if (var v = Send(a, nil); v != 1): 41 | echo("Send returned ", v, " != 1") 42 | quit("fail") 43 | if counter != 10: 44 | echo("counter is ", counter, " != 10") 45 | quit("fail") 46 | 47 | golib_main() 48 | # not reached 49 | 50 | -------------------------------------------------------------------------------- /tests/select7.go: -------------------------------------------------------------------------------- 1 | // run 2 | 3 | // Copyright 2011 The Go Authors. All rights reserved. 4 | // Use of this source code is governed by a BSD-style 5 | // license that can be found in the LICENSE file. 6 | 7 | // Test select when discarding a value. 8 | 9 | package main 10 | 11 | import "runtime" 12 | 13 | func recv1(c <-chan int) { 14 | <-c 15 | } 16 | 17 | func recv2(c <-chan int) { 18 | select { 19 | case <-c: 20 | } 21 | } 22 | 23 | func recv3(c <-chan int) { 24 | c2 := make(chan int) 25 | select { 26 | case <-c: 27 | case <-c2: 28 | } 29 | } 30 | 31 | func send1(recv func(<-chan int)) { 32 | c := make(chan int) 33 | go recv(c) 34 | runtime.Gosched() 35 | c <- 1 36 | } 37 | 38 | func send2(recv func(<-chan int)) { 39 | c := make(chan int) 40 | go recv(c) 41 | runtime.Gosched() 42 | select { 43 | case c <- 1: 44 | } 45 | } 46 | 47 | func send3(recv func(<-chan int)) { 48 | c := make(chan int) 49 | go recv(c) 50 | runtime.Gosched() 51 | c2 := make(chan int) 52 | select { 53 | case c <- 1: 54 | case c2 <- 1: 55 | } 56 | } 57 | 58 | func main() { 59 | send1(recv1) 60 | send2(recv1) 61 | send3(recv1) 62 | send1(recv2) 63 | send2(recv2) 64 | send3(recv2) 65 | send1(recv3) 66 | send2(recv3) 67 | send3(recv3) 68 | } 69 | -------------------------------------------------------------------------------- /tests/select7.nim: -------------------------------------------------------------------------------- 1 | import golib 2 | 3 | 4 | proc recv1(c: recv_chan[int]) {.goroutine.} = 5 | <-c 6 | 7 | proc recv2(c: recv_chan[int]) {.goroutine.} = 8 | select: 9 | scase <-c: 10 | discard 11 | 12 | proc recv3(c: recv_chan[int]) {.goroutine.} = 13 | var c2 = make_chan(int) 14 | select: 15 | scase <-c: 16 | discard 17 | scase <-c2: 18 | discard 19 | 20 | proc send1(recv: goroutine_type) = 21 | var c = make_chan(int) 22 | go recv(c) 23 | go_yield() 24 | c <- 1 25 | 26 | proc send2(recv: goroutine_type) = 27 | var c = make_chan(int) 28 | go recv(c) 29 | go_yield() 30 | select: 31 | scase c <- 1: 32 | discard 33 | 34 | proc send3(recv: goroutine_type) = 35 | var c = make_chan(int) 36 | go recv(c) 37 | go_yield() 38 | var c2 = make_chan(int) 39 | select: 40 | scase c <- 1: 41 | discard 42 | scase c2 <- 1: 43 | discard 44 | 45 | proc go_main() {.gomain.} = 46 | send1(recv1) 47 | send2(recv1) 48 | send3(recv1) 49 | send1(recv2) 50 | send2(recv2) 51 | send3(recv2) 52 | send1(recv3) 53 | send2(recv3) 54 | send3(recv3) 55 | 56 | 57 | golib_main() 58 | # not reached 59 | 60 | -------------------------------------------------------------------------------- /tests/select_custom.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | func f() int { 4 | return 1 5 | } 6 | 7 | func main() { 8 | var c1 = make(chan int, 1) 9 | var c2 = make(chan int, 1) 10 | var c3 = make(chan int, 1) 11 | var c4 = make(chan int, 1) 12 | var c5 = make(chan int, 1) 13 | var c6 = make(chan int, 1) 14 | var i2, i3, i6 int 15 | var ok3 bool 16 | var l = make([]int, 2) 17 | 18 | c1 <- 1 19 | c2 <- 2 20 | c3 <- 3 21 | c4 <- 4 22 | c5 <- 5 23 | c6 <- 6 24 | 25 | LOOP: 26 | for { 27 | select { 28 | case <-c1: 29 | case i2 = <-c2: 30 | case i3, ok3 = <-c3: 31 | case l[0] = <-c4: 32 | case l[f()] = <-c5: 33 | case i6 = <-c6: 34 | default: 35 | break LOOP 36 | } 37 | } 38 | // println(i2, i3, ok3, l[0], l[1], i6) 39 | if i2 != 2 { 40 | println("i2:", i2, "!=", 2) 41 | panic("fail") 42 | } 43 | if i3 != 3 { 44 | println("i3:", i3, "!=", 3) 45 | panic("fail") 46 | } 47 | if ok3 != true { 48 | println("ok3:", ok3, "!=", true) 49 | panic("fail") 50 | } 51 | if l[0] != 4 { 52 | println("i4:", l[0], "!=", 4) 53 | panic("fail") 54 | } 55 | if l[1] != 5 { 56 | println("i5:", l[1], "!=", 5) 57 | panic("fail") 58 | } 59 | if i6 != 6 { 60 | println("i6:", i6, "!=", 6) 61 | panic("fail") 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /tests/select_custom.nim: -------------------------------------------------------------------------------- 1 | import golib 2 | 3 | proc f(): int = 4 | return 1 5 | 6 | proc go_main() {.gomain.} = 7 | var 8 | c1 = make_chan(int, 1) 9 | c2 = make_chan(int, 1) 10 | c3 = make_chan(int, 1) 11 | c4 = make_chan(int, 1) 12 | c5 = make_chan(int, 1) 13 | c6 = make_chan(int, 1) 14 | i2, i3, i6: int 15 | ok3: bool 16 | li: array[2, int] 17 | 18 | c1 <- 1 19 | c2 <- 2 20 | c3 <- 3 21 | c4 <- 4 22 | c5 <- 5 23 | c6 <- 6 24 | 25 | block LOOP: 26 | while true: 27 | select: 28 | scase <-c1: discard 29 | scase(i2 = <-c2): discard 30 | scase((i3, ok3) = <--c3): discard 31 | scase(li[0] = <-c4): discard 32 | scase(li[f()] = <-c5): discard 33 | scase(i6 = <-c6): discard 34 | default: 35 | break LOOP 36 | # echo ($i2, " ", $i3, " ", $ok3, " ", $li[0], " ", $li[1], " ", $i6) 37 | if i2 != 2: 38 | echo("i2: ", i2, " != ", 2) 39 | quit("fail") 40 | if i3 != 3: 41 | echo("i3: ", i3, " != ", 3) 42 | quit("fail") 43 | if ok3 != true: 44 | echo("ok3: ", ok3, " != ", true) 45 | quit("fail") 46 | if li[0] != 4: 47 | echo("i4: ", li[0], " != ", 4) 48 | quit("fail") 49 | if li[1] != 5: 50 | echo("i5: ", li[1], " != ", 5) 51 | quit("fail") 52 | if i6 != 6: 53 | echo("i6: ", i6, " != ", 6) 54 | quit("fail") 55 | 56 | golib_main() 57 | # not reached 58 | 59 | -------------------------------------------------------------------------------- /tests/sieve1.go: -------------------------------------------------------------------------------- 1 | // run 2 | 3 | // Copyright 2009 The Go Authors. All rights reserved. 4 | // Use of this source code is governed by a BSD-style 5 | // license that can be found in the LICENSE file. 6 | 7 | // Test concurrency primitives: classical inefficient concurrent prime sieve. 8 | 9 | // Generate primes up to 100 using channels, checking the results. 10 | // This sieve consists of a linear chain of divisibility filters, 11 | // equivalent to trial-dividing each n by all primes p ≤ n. 12 | 13 | package main 14 | 15 | // Send the sequence 2, 3, 4, ... to channel 'ch'. 16 | func Generate(ch chan<- int) { 17 | for i := 2; ; i++ { 18 | ch <- i // Send 'i' to channel 'ch'. 19 | } 20 | } 21 | 22 | // Copy the values from channel 'in' to channel 'out', 23 | // removing those divisible by 'prime'. 24 | func Filter(in <-chan int, out chan<- int, prime int) { 25 | for i := range in { // Loop over values received from 'in'. 26 | if i%prime != 0 { 27 | out <- i // Send 'i' to channel 'out'. 28 | } 29 | } 30 | } 31 | 32 | // The prime sieve: Daisy-chain Filter processes together. 33 | func Sieve(primes chan<- int) { 34 | ch := make(chan int) // Create a new channel. 35 | go Generate(ch) // Start Generate() as a subprocess. 36 | for { 37 | // Note that ch is different on each iteration. 38 | prime := <-ch 39 | primes <- prime 40 | ch1 := make(chan int) 41 | go Filter(ch, ch1, prime) 42 | ch = ch1 43 | } 44 | } 45 | 46 | func main() { 47 | primes := make(chan int) 48 | go Sieve(primes) 49 | a := []int{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97} 50 | for i := 0; i < len(a); i++ { 51 | if x := <-primes; x != a[i] { 52 | println(x, " != ", a[i]) 53 | panic("fail") 54 | } 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /tests/sieve1.nim: -------------------------------------------------------------------------------- 1 | import golib 2 | 3 | # Send the sequence 2, 3, 4, ... to channel 'ch'. 4 | proc Generate(ch: send_chan[int]) {.goroutine.} = 5 | var i = 2 6 | while true: 7 | ch <- i # Send 'i' to channel 'ch'. 8 | inc i 9 | 10 | # Copy the values from channel 'in_ch' to channel 'out_ch', 11 | # removing those divisible by 'prime'. 12 | proc Filter(in_ch: recv_chan[int], out_ch: send_chan[int], prime: int) {.goroutine.} = 13 | for i in in_ch: # Loop over values received from 'in_ch'. 14 | if i mod prime != 0: 15 | out_ch <- i # Send 'i' to channel 'out'. 16 | 17 | # The prime sieve: Daisy-chain Filter processes together. 18 | proc Sieve(primes: send_chan[int]) {.goroutine.} = 19 | var ch = make_chan(int) # Create a new channel. 20 | go Generate(ch) # Start Generate() as a subprocess. 21 | while true: 22 | # Note that ch is different on each iteration. 23 | var prime = <-ch 24 | primes <- prime 25 | var ch1 = make_chan(int) 26 | go Filter(ch, ch1, prime) 27 | ch = ch1 28 | 29 | proc go_main() {.gomain.} = 30 | var primes = make_chan(int) 31 | go Sieve(primes) 32 | var a = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97] 33 | for i in 0 ..< len(a): 34 | var x = <-primes 35 | if x != a[i]: 36 | echo(x, " != ", a[i]) 37 | quit("fail") 38 | 39 | golib_main() 40 | # not reached 41 | 42 | -------------------------------------------------------------------------------- /tests/test1.nim: -------------------------------------------------------------------------------- 1 | import macros, golib 2 | 3 | dumpTree: 4 | # proc f1() = discard 5 | # proc f2(args: pointer) = discard 6 | # proc f3(args: pointer) {.cdecl.} = discard 7 | # f4 = proc (args: pointer) {.cdecl.} = discard 8 | # proc f5(args: pointer) {.goroutine, noreturn.} = 9 | # discard 10 | # proc f5(args: pointer, args2: pointer) {.goroutine, noreturn.} = 11 | # discard 12 | # proc go_main() {.cdecl, codegenDecl: "$1 $2$3 __asm__ (\"main.main\");\n$1 $2$3".} = 13 | # discard 14 | # go_go(f5, 1, nil) 15 | # type f8_args_tuple = tuple[a: int32, b: int64, c: pointer] 16 | # proc f8(a: int32, b: int64, c: pointer) = discard 17 | # var (a, b, c) = cast[ref f8_args_tuple](args)[] 18 | # var f8_args: ref f8_args_tuple 19 | # var f8_args: ref type((int32(y), int64(100), pointer(nil))) 20 | # new(f8_args) 21 | # f8(int32(y), int64(100), nil) 22 | # f8_args[] = (int32(y), int64(100), nil) 23 | # f8(cast[pointer](f8_args)) 24 | # proc whisper(left, right: ref Channel[int32]) {.goroutine.} = discard 25 | # type X = tuple[left, right: ref Channel[int32]] 26 | 27 | # select: 28 | # scase a <- GetValue(): 29 | # inc i 30 | # a = nil 31 | # scase (i1 = <-c1): 32 | # echo(i1) 33 | # scase (i3, ok = <--c3): 34 | # echo i3, ok 35 | # default: 36 | # break LOOP 37 | 38 | # var 39 | # send1: ref uint 40 | # new(send1) 41 | # send1[] = GetValue() 42 | # var 43 | # select_cases = [ 44 | # chan_select_case(dir: SELECT_DIR_SEND, chan: a.get_chan, send: send1), 45 | # chan_select_case(dir: SELECT_DIR_RECV, chan: c1.get_chan, send: nil), 46 | # chan_select_case(dir: SELECT_DIR_RECV, chan: c3.get_chan, send: nil), 47 | # chan_select_case(dir: SELECT_DIR_DEFAULT, chan: nil, send: nil), 48 | # ] 49 | # res = chan_select(addr select_cases[0], len(select_cases)) 50 | 51 | # case res.chosen: 52 | # of 0: 53 | # inc i 54 | # a = nil 55 | # of 1: 56 | # i1 = cast[ptr uint](res.recv)[] 57 | # echo(i1) 58 | # of 2: 59 | # i3 = cast[ptr uint](res.recv)[] 60 | # ok = res.recv_ok 61 | # echo i3, ok 62 | # of 3: 63 | # break LOOP 64 | # else: 65 | # discard 66 | # select: 67 | # scase <-c1: 68 | # discard 69 | # scase (i2 = <-c2): 70 | # discard 71 | # scase ((i3, ok3) = <--c3): 72 | # discard 73 | # scase (li[0] = <-c4): 74 | # discard 75 | # scase (li[f()] = <-c5): 76 | # discard 77 | 78 | # i2 = cast[ptr type(i2)](res.recv)[] 79 | # ok3 = res.recv_ok 80 | # li[0] = cast[ptr type(li[0])](res.recv)[] 81 | # li[f()] = cast[ptr type(li[f()])](res.recv)[] 82 | # static: doAssert(compiles((a, b) = <--c), "- the type must be 'chan' or 'recv_chan'") 83 | go f(1) 84 | 85 | 86 | proc f6(args: pointer): int = 87 | return 1 88 | 89 | proc f7(a: int32, b: int64, c: pointer) {.goroutine.} = 90 | echo "in f7()" 91 | echo(a, " ", b, " ", cast[int](c)) 92 | 93 | # f8(a: int32, b: int64, c: pointer) 94 | proc f8(args: pointer) = 95 | var (a, b, c) = cast[ref tuple[a: int32, b: int64, c: pointer]](args)[] 96 | echo "in f8()" 97 | echo(a, " ", b, " ", cast[int](c)) 98 | 99 | proc f9() {.goroutine.} = 100 | echo "in f9()" 101 | 102 | proc go_main() {.gomain.} = 103 | var x: pointer 104 | discard f6(x) 105 | var y = 10 106 | 107 | var f8_args: ref type((int32(y), int64(100), pointer(nil))) 108 | new(f8_args) 109 | f8_args[] = (int32(y), int64(100), nil) 110 | # f8(cast[pointer](f8_args)) 111 | f8(f8_args) 112 | 113 | go f7(y.int32, 100.int64, nil.pointer) 114 | go f9() 115 | go_sleep_ms(100) 116 | 117 | var c = make_chan(int, 1) 118 | echo c 119 | echo c.capacity 120 | c <- 100 121 | var r = <-c 122 | echo r 123 | c = nil 124 | echo c 125 | 126 | proc f10(c: chan[int]) {.goroutine.} = c <- 111 127 | c = make_chan(int) 128 | go f10(c) 129 | r = <-c 130 | echo "value from f10(): ", r 131 | 132 | 133 | golib_main() 134 | # not reached 135 | 136 | -------------------------------------------------------------------------------- /tests/test2.nim: -------------------------------------------------------------------------------- 1 | import golib, strutils 2 | 3 | # proc f(c: send_chan[int]) {.goroutine.} = 4 | # go_sleep_ms(100) 5 | # c <- 1 6 | 7 | {.push, hint[XDeclaredButNotUsed]: off.} 8 | proc f(args_for_goroutine: pointer) {.cdecl.} = 9 | var (c, bogus_param_for_goroutine) = cast[ref tuple[c: chan[int], bogus_param_for_goroutine: pointer]](args_for_goroutine)[] 10 | go_sleep_ms(100) 11 | c <- 1 12 | {.pop.} 13 | 14 | proc go_main() {.gomain.} = 15 | # type 16 | # Tp1 = tuple[i1: int32, p2: pointer] 17 | # # Tp2 = tuple[i: int] 18 | # var 19 | # x: Tp1 = (1.int32, nil) 20 | # # y: Tp2 = (2) 21 | 22 | # echo(x.i1, " ", cast[int](x.p2)) 23 | # # echo(y.i) 24 | 25 | var c = make_chan(int) 26 | 27 | # go f(c) 28 | var test2_nim_19_8_f_args_tuple_ref_for_goroutine: ref type((c, nil.pointer)) 29 | new(test2_nim_19_8_f_args_tuple_ref_for_goroutine) 30 | test2_nim_19_8_f_args_tuple_ref_for_goroutine[] = (c, nil) 31 | go_go(f, test2_nim_19_8_f_args_tuple_ref_for_goroutine) 32 | 33 | echo "got $# from f()" % $(<-c) 34 | 35 | 36 | golib_main() 37 | # not reached 38 | 39 | -------------------------------------------------------------------------------- /tests/tnew.nim: -------------------------------------------------------------------------------- 1 | # Test the implementation of the new operator 2 | # and the code generation for gc walkers 3 | # (and the garbage collector): 4 | 5 | import golib 6 | 7 | type 8 | PNode = ref TNode 9 | TNode = object 10 | data: int 11 | str: string 12 | le, ri: PNode 13 | 14 | TStressTest = ref array[0..45, array[1..45, TNode]] 15 | 16 | proc finalizer(n: PNode) = 17 | write(stdout, n.data) 18 | write(stdout, " is now freed\n") 19 | 20 | proc newNode(data: int, le, ri: PNode): PNode = 21 | new(result, finalizer) 22 | result.le = le 23 | result.ri = ri 24 | result.data = data 25 | 26 | # now loop and build a tree 27 | proc go_main() {.gomain.} = 28 | var 29 | i = 0 30 | p: TStressTest 31 | while i < 1000: 32 | var n: PNode 33 | 34 | n = newNode(i, nil, newNode(i + 10000, nil, nil)) 35 | inc(i) 36 | new(p) 37 | 38 | write(stdout, "Simple tree node allocation worked!\n") 39 | i = 0 40 | while i < 1000: 41 | var m = newNode(i + 20000, nil, nil) 42 | var k = newNode(i + 30000, nil, nil) 43 | m.le = m 44 | m.ri = k 45 | k.le = m 46 | k.ri = k 47 | inc(i) 48 | 49 | write(stdout, "Simple cycle allocation worked!\n") 50 | 51 | 52 | golib_main() 53 | # not reached 54 | 55 | --------------------------------------------------------------------------------