summary refs log tree commit diff stats
path: root/tools/niminst/buildsh.nimf
diff options
context:
space:
mode:
Diffstat (limited to 'tools/niminst/buildsh.nimf')
-rw-r--r--tools/niminst/buildsh.nimf220
1 files changed, 220 insertions, 0 deletions
diff --git a/tools/niminst/buildsh.nimf b/tools/niminst/buildsh.nimf
new file mode 100644
index 000000000..04ef35653
--- /dev/null
+++ b/tools/niminst/buildsh.nimf
@@ -0,0 +1,220 @@
+#? stdtmpl(subsChar='?') | standard
+#proc generateBuildShellScript(c: ConfigData): string =
+#  result = "#! /bin/sh\n# Generated from niminst\n" &
+#           "# Template is in tools/niminst/buildsh.nimf\n" &
+#           "# To regenerate run ``niminst csource`` or ``koch csource``\n"
+
+set -e
+
+while :
+do
+  case "$1" in
+    --os)
+      optos=$2
+      shift 2
+      ;;
+    --cpu)
+      optcpu=$2
+      shift 2
+      ;;
+    --osname)
+      optosname=$2
+      shift 2
+      ;;
+    --extraBuildArgs)
+      extraBuildArgs=" $2"
+      shift 2
+      ;;
+    --) # End of all options
+      shift
+      break;
+      ;;
+    -*)
+      echo 2>&1 "Error: Unknown option: $1" >&2
+      exit 1
+      ;;
+    *)  # No more options
+      break
+      ;;
+  esac
+done
+
+CC="${CC:-gcc}"
+LINKER="${LD:-gcc}"
+COMP_FLAGS="${CPPFLAGS:-} ${CFLAGS:-} ?{c.ccompiler.flags}$extraBuildArgs"
+LINK_FLAGS="${LDFLAGS:-} ?{c.linker.flags}"
+PS4=""
+#  add(result, "# platform detection\n")
+ucpu=`uname -m`
+uos=`uname`
+uosname=
+#  add(result, "# bin dir detection\n")
+binDir=?{firstBinPath(c).toUnix}
+
+if [ -s ../koch.nim ]; then
+  binDir="../bin"
+fi
+
+if [ ! -d $binDir ]; then
+  mkdir $binDir
+fi
+
+#  add(result, "# override OS, CPU and OS Name with command-line arguments\n")
+if [ -n "$optos" ]; then 
+  uos="$optos"
+fi
+if [ -n "$optcpu" ]; then
+  ucpu="$optcpu"
+fi
+if [ -n "$optcpu" ]; then
+  uosname="$optosname"
+fi
+
+#  add(result, "# convert to lower case:\n")
+ucpu=`echo $ucpu | tr "[:upper:]" "[:lower:]"`
+uos=`echo $uos | tr "[:upper:]" "[:lower:]"`
+uosname=`echo $uosname | tr "[:upper:]" "[:lower:]"`
+
+case $uos in
+  *linux* )
+    myos="linux"
+    LINK_FLAGS="$LINK_FLAGS -ldl -lm -lrt"
+    ;;
+  *dragonfly* )
+    myos="dragonfly"
+    LINK_FLAGS="$LINK_FLAGS -lm"
+    ;;
+  *freebsd* )
+    myos="freebsd"
+    CC="clang"
+    LINKER="clang"
+    LINK_FLAGS="$LINK_FLAGS -lm"
+    ;;
+  *openbsd* )
+    myos="openbsd"
+    LINK_FLAGS="$LINK_FLAGS -lm"
+    ;;
+  *netbsd* )
+    myos="netbsd"
+    LINK_FLAGS="$LINK_FLAGS -lm"
+    ;;
+  *darwin* )
+    myos="macosx"
+    CC="clang"
+    LINKER="clang"
+    LINK_FLAGS="$LINK_FLAGS -ldl -lm"
+    if [ "$HOSTTYPE" = "x86_64" ] ; then
+      ucpu="amd64"
+    fi
+    ;;
+  *aix* )
+    myos="aix"
+    LINK_FLAGS="$LINK_FLAGS -ldl -lm"
+    ;;
+  *solaris* | *sun* )
+    myos="solaris"
+    LINK_FLAGS="$LINK_FLAGS -ldl -lm -lsocket -lnsl"
+    ;;
+  *haiku* )
+    myos="haiku"
+    LINK_FLAGS="$LINK_FLAGS -lroot -lnetwork"
+    ;;
+  *mingw* | *msys* )
+    myos="windows"
+    ;;
+  *android* )
+    myos="android"
+    LINK_FLAGS="$LINK_FLAGS -ldl -lm -lrt"
+    LINK_FLAGS="$LINK_FLAGS -landroid-glob"
+    ;;
+  *)
+    echo 2>&1 "Error: unknown operating system: $uos"
+    exit 1
+    ;;
+esac
+
+case $ucpu in
+  *i386* | *i486* | *i586* | *i686* | *bepc* | *i86pc* )
+    mycpu="i386" ;;
+  *amd*64* | *x86-64* | *x86_64* )
+    mycpu="amd64" ;;
+  *sparc*|*sun* )
+    mycpu="sparc"
+    if [ "$(isainfo -b)" = "64" ]; then
+      mycpu="sparc64"
+    fi
+    ;;
+  *ppc64le* )
+    mycpu="powerpc64el" ;;
+  *ppc64* )
+    if [ "$myos" = "linux" ] ; then
+      COMP_FLAGS="$COMP_FLAGS -m64"
+      LINK_FLAGS="$LINK_FLAGS -m64"
+    fi
+    mycpu="powerpc64" ;;
+  *power*|*ppc* )
+    mycpu="powerpc" ;;
+  *mips* )
+    mycpu="$("$CC" -dumpmachine | sed 's/-.*//')"
+    case $mycpu in
+      mips|mipsel|mips64|mips64el)
+        ;;
+      *)
+        echo 2>&1 "Error: unknown MIPS target: $mycpu"
+        exit 1
+    esac
+    ;;
+  *arm*|*armv6l*|*armv71* )
+    mycpu="arm" ;;
+  *aarch64* )
+    mycpu="arm64" ;;
+  *riscv64* )
+    mycpu="riscv64" ;;
+  *)
+    echo 2>&1 "Error: unknown processor: $ucpu"
+    exit 1
+    ;;
+esac
+
+case $uosname in
+  *android* )
+    LINK_FLAGS="$LINK_FLAGS -landroid-glob"
+    myosname="android"
+    myos="android"
+    ;;
+esac
+
+#  add(result, "# call the compiler:\n")
+echo \# OS:  $myos
+echo \# CPU: $mycpu
+
+case $myos in
+#  for osA in 1..c.oses.len:
+?{c.oses[osA-1]})
+  case $mycpu in
+#    for cpuA in 1..c.cpus.len:
+  ?{c.cpus[cpuA-1]})
+    set -x
+#      var linkCmd = ""
+#      for ff in items(c.cfiles[osA][cpuA]):
+#        let f = ff.toUnix
+    $CC $COMP_FLAGS -Ic_code -c ?{f} -o ?{changeFileExt(f, "o")}
+#        add(linkCmd, " \\\n" & changeFileExt(f, "o"))
+#      end for
+    $LINKER -o ?{"$binDir/" & toLowerAscii(c.name)} ?linkCmd $LINK_FLAGS
+    ;;
+#    end for
+  *)
+    echo 2>&1 "Error: no C code generated for: [$myos: $mycpu]"
+    exit 1
+    ;;
+  esac
+  ;;
+#  end for
+*)
+  echo 2>&1 "Error: no C code generated for: [$myos: $mycpu]"
+  exit 1
+  ;;
+esac
+
+: SUCCESS