Version 1.0.9.
[gnuk/neug.git] / src / configure
index 71fd479..dcd2ee3 100755 (executable)
@@ -1,9 +1,13 @@
 #! /bin/bash
 
+# This is bash which supports ANSI-C Quoting
+nl=$'\n'
+
 #
 # This file is *NOT* generated by GNU Autoconf, but written by NIIBE Yutaka
 #
-# Copyright (C) 2010, 2011, 2012, 2013 Free Software Initiative of Japan
+# Copyright (C) 2010, 2011, 2012, 2013, 2015, 2016, 2017, 2018
+#               Free Software Initiative of Japan
 #
 # This file is a part of NeuG, a Random Number Generator implementation.
 #
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
+#
+# Submodule check
+#
+if ! test -f ../chopstx/rules.mk; then
+  echo "Submodule 'chopstx' not found" >&2
+  echo "You might need: git submodule update --init" >&2
+  exit 1
+fi
+
 # Default settings
 help=no
+vidpid=none
 target=FST_01
 verbose=no
-vidpid=none
 with_dfu=default
 debug=no
 sys1_compat=yes
+ackbtn_support=no
 with_fraucheky=no
+with_index=none
+# For emulation
+prefix=/usr/local
+exec_prefix='${prefix}'
+libexecdir='${exec_prefix}/libexec'
+
+# Revision numbers
+if test -e ../.git; then
+  if type git >/dev/null 2>&1; then
+     REVISION=$(git describe --dirty="-modified")
+     REVISION_CHOPSTX=$(cd ../chopstx && git describe --dirty="-modified")
+     REVISION_FRAUCHEKY=$(cd ../fraucheky && git describe --dirty="-modified")
+  else
+     # echo 'No git available, please install git'
+     GIT_REVISION=$(sed -e 's/^\(.......\).*$/g\1/' "../.git/$(sed -e 's/^ref: //' ../.git/HEAD)")
+     REVISION=$(cat ../VERSION)-$GIT_REVISION
+     REVISION_CHOPSTX=$(cat ../chopstx/VERSION)-$(sed -e 's/^\(.......\).*$/g\1/' ../.git/modules/chopstx/HEAD)
+     REVISION_FRAUCHEKY=$(cat ../fraucheky/VERSION)-$(sed -e 's/^\(.......\).*$/g\1/' ../.git/modules/fraucheky/HEAD)
+  fi
+else
+  REVISION=$(cat ../VERSION)
+  REVISION_CHOPSTX=$(cat ../chopstx/VERSION)
+  REVISION_FRAUCHEKY=$(cat ../fraucheky/VERSION)
+fi
 
 # Process each option
 for option; do
   case $option in
-  *=*) optarg=`expr "X$option" : '[^=]*=\(.*\)'` ;;
+  *=*) optarg=$(expr "X$option" : '[^=]*=\(.*\)') ;;
   *)   optarg=yes ;;
   esac
 
   case $option in
   -h | --help)
     help=yes ;;
-  --target=*)
-    target=$optarg ;;
   -v | --verbose)
     verbose=yes ;;
   --vidpid=*)
     vidpid=$optarg ;;
+  --target=*)
+    target=$optarg ;;
   --enable-sys1-compat)
-    sys1_compat = yes ;;
+    sys1_compat=yes ;;
   --disable-sys1-compat)
-    sys1_compat = no ;;
+    sys1_compat=no ;;
   --with-dfu)
     with_dfu=yes ;;
   --without-dfu)
@@ -58,6 +96,17 @@ for option; do
     with_fraucheky=$optarg ;;
   --without-fraucheky)
     with_fraucheky=no ;;
+  --with-index=*)
+    with_index=$optarg ;;
+  #
+  # For emulation
+  #
+  --prefix=*)
+    prefix=optarg ;;
+  --exec-prefix=*)
+    exec_prefix=optarg ;;
+  --libexecdir=*)
+    libexecdir=optarg ;;
   *)
     echo "Unrecognized option \`$option'" >&2
     echo "Try \`$0 --help' for more information." >&2
@@ -76,35 +125,58 @@ Configuration:
   -h, --help           display this help and exit      [no]
   --vidpid=VID:PID     specify vendor/product ID       [<NONE>]
   --target=TARGET      specify target                  [FST_01]
-                       supported targes are:
+                       supported targets are:
                           FST_01
-                          FST_01_00
+                          FST_01G
+                          FST_01SZ
                           STM8S_DISCOVERY
                           OLIMEX_STM32_H103
+                          MAPLE_MINI
+                          ST_DONGLE
+                          ST_NUCLEO_F103
+                          NITROKEY_START
+                          BLUE_PILL
+                          CQ_STARM
+                          STM32_PRIMER2
+                          STBEE
+                          STBEE_MINI
+                          FST_01_00 (unreleased version with 8MHz XTAL)
   --enable-sys1-compat enable SYS 1.0 compatibility    [yes]
                           executable is target dependent
   --disable-sys1-compat        disable SYS 1.0 compatibility   [no]
                           executable is target independent
-                          but requires SYS 2.0 or newer
+                          but requires SYS 3.0 or newer
   --with-dfu           build image for DFU             [<target specific>]
 
   --with-fraucheky=VID:PID
                        include fraucheky support       [no]
-  --with-index=INDEX   specify INDEX file              [INDEX.HTM]
+  --with-index=INDEX   specify INDEX file              [none]
 EOF
   exit 0
 fi
 
-if test "$vidpid" = "none"; then
-  echo "Please specify Vendor ID and Product ID by --vidpid option." 
-  exit 1
+if test "$with_fraucheky" = "no"; then
+  enable_fraucheky=""
+  FRAUCHEKY_DEFINE="#undef FRAUCHEKY_SUPPORT"
+  FRAUCHEKY_MSC_DEFINE="#undef MSC_INTERFACE_NO"
+else
+  ../fraucheky/configure $with_fraucheky $with_index $REVISION $REVISION_CHOPSTX $REVISION_FRAUCHEKY
+  enable_fraucheky=yes
+  FRAUCHEKY_DEFINE="#define FRAUCHEKY_SUPPORT 1"
+  FRAUCHEKY_MSC_DEFINE="#define MSC_INTERFACE_NO    2"
+  if ! test -f ../fraucheky/build.mk; then
+    echo "Submodule 'fraucheky' not found" >&2
+    echo "You might need: git submodule update --init" >&2
+    exit 1
+  fi
 fi
 
-TARGET_DEFINE="#define BOARD_$target 1"
-BOARD_HEADER_FILE=board-`echo $target | tr '_[:upper:]' '-[:lower:]'`.h
-echo Header file is: $BOARD_HEADER_FILE
-ln -sf ../chopstx/board/$BOARD_HEADER_FILE board.h
+BOARD_HEADER_FILE=board-$(echo $target | tr '_[:upper:]' '-[:lower:]').h
+echo "Header file is: $BOARD_HEADER_FILE"
+ln -sf "../chopstx/board/$BOARD_HEADER_FILE" board.h
 
+# Frequency
+MHZ=72
 # Flash page size in byte
 FLASH_PAGE_SIZE=1024
 # Flash memory size in KiB
@@ -130,112 +202,246 @@ STBEE)
   if test "$with_dfu" = "default"; then
     with_dfu=yes;
   fi  ;;
-STM8S_DISCOVERY)
-  FLASH_SIZE=64
+BLUE_PILL|STM8S_DISCOVERY)
+  # It's 64KB version of STM32F103, but actually has 128KB
+  flash_override="-DSTM32F103_OVERRIDE_FLASH_SIZE_KB=128"
+  ;;
+BLUE_PILL_G)
+  MHZ=96
+  ;;
+FST_01SZ)
+  MHZ=96
+  ackbtn_support=yes
   ;;
 *)
   ;;
 esac
 
+if test "$target" = "GNU_LINUX"; then
+  ldscript=""
+  chip="gnu-linux"
+  emulation="yes"
+  cross=""
+  mcu="none"
+  def_emulation="-DGNU_LINUX_EMULATION"
+  def_mhz=""
+  enable_hexoutput=""
+  libs="-lpthread"
+else
+  ldscript="neug.ld"
+  chip="stm32f103"
+  emulation=""
+  cross="arm-none-eabi-"
+  mcu="cortex-m3"
+  def_emulation=""
+  def_mhz="-DMHZ=$MHZ"
+  enable_hexoutput=yes
+  libs=""
+fi
+
+if test "$emulation" = "yes"; then
+  if test "$vidpid" = "none"; then
+    vidpid=0000:0000
+  else
+    echo "Please don't specify VID:PID for emulation at compile time;"
+    echo "It is a user who should specify VID:PID at run time."
+    exit 1
+  fi
+else
+  if test "$vidpid" = "none"; then
+    echo "Please specify Vendor ID and Product ID by --vidpid option." >&2
+  exit 1
+  fi
+fi
+
 # --with-dfu option
 if test "$with_dfu" = "yes"; then
+  if test "$target" = "FST_01" -o "$target" = "FST_01G" \
+     -o "$target" = "FST_01_00"; then
+      echo "FST-01 doesn't have DFU loader, you should not use --with-dfu." >&2
+      exit 1
+  fi
   echo "Configured for DFU"
   ORIGIN=0x08003000
-  FLASH_SIZE=`expr $FLASH_SIZE - 12`
+  FLASH_SIZE=$((FLASH_SIZE - 12))
   DFU_DEFINE="#define DFU_SUPPORT 1"
 else
+  with_dfu=no
   echo "Configured for bare system (no-DFU)"
   ORIGIN=0x08000000
   DFU_DEFINE="#undef DFU_SUPPORT"
 fi
 
-if test -d ../.git; then
-  REVISION=`git describe --dirty="-modified"` 
+# Acknowledge button support
+if test "$ackbtn_support" = "yes"; then
+  ACKBTN_DEFINE="#define ACKBTN_SUPPORT 1"
+  echo "Acknowledge button is supported"
 else
-  REVISION=`cat ../VERSION`
+  ACKBTN_DEFINE="#undef ACKBTN_SUPPORT"
+  echo "Acknowledge button is not supported"
 fi
 
 ### !!! Replace following string of "FSIJ" to yours !!! ####
-SERIALNO="FSIJ-`cat ../VERSION | sed -e 's%^[^/]*/%%'`-"
+SERIALNO="FSIJ-$(sed -e 's%^[^/]*/%%' <../VERSION)-"
 
 SERIALNO_STR_LEN_DEFINE="#define SERIALNO_STR_LEN ${#SERIALNO}"
 
 
-CONFIG="$target:dfu=$with_dfu"
+if test "$sys1_compat" = "yes"; then
+  CONFIG="$target:dfu=$with_dfu"
+else
+  if test "$with_dfu" = "yes"; then
+    echo "Common binary can't support DFU loader, don't use --with-dfu." >&2
+    exit 1
+  fi
+  # Override settings for common binary.  Safer side.
+  FLASH_PAGE_SIZE=2048
+  FLASH_SIZE=128
+  MEMORY_SIZE=20
+  CONFIG="common"
+fi
+
+output_vid_pid_version () {
+  echo "$VIDPID" | \
+  sed -n -e "s%^\([0-9a-f][0-9a-f]\)\([0-9a-f][0-9a-f]\):\([0-9a-f][0-9a-f]\)\([0-9a-f][0-9a-f]\)$%\1\t\2\t\3\t\4%p" | \
+  while read -r FIRST SECOND THIRD FOURTH; do
+    if test $FIRST != 00; then
+      echo replace_vid_msb $FIRST
+    fi
+    if test $SECOND != 00; then
+      echo replace_vid_lsb $SECOND
+    fi
+    if test $THIRD != 00; then
+      echo replace_pid_msb $THIRD
+    fi
+    if test $FOURTH != 00; then
+      echo replace_pid_lsb $FOURTH
+    fi
+  done
+  echo "$VERSION" | \
+  sed -n -e "s%^\([0-9a-f][0-9a-f]\)\([0-9a-f][0-9a-f]\)$%\1\t\2%p" | \
+  while read -r FIRST SECOND; do
+    if test $FIRST != 00; then
+      echo replace_bcd_device_msb $FIRST
+    fi
+    if test $SECOND != 00; then
+      echo replace_bcd_device_lsb $SECOND
+    fi
+  done
+}
+
+output_vendor_product_serial_strings () {
+  name=$1
+
+  echo "static const uint8_t ${name}string_vendor[] = {"
+  echo "  ${#VENDOR}*2+2,                       /* bLength */"
+  echo "  STRING_DESCRIPTOR,            /* bDescriptorType */"
+  echo "  /* Manufacturer: \"$VENDOR\" */"
+  echo "$VENDOR" | sed -e "s/\(........\)/\1\\${nl}/g" | sed -n -e "s/\(.\)/'\1', 0, /g" -e "s/^/  /" -e "/^  ./s/ $//p"
+  echo '};'
+  echo
+  echo "static const uint8_t ${name}string_product[] = {"
+  echo "  ${#PRODUCT}*2+2,                      /* bLength */"
+  echo "  STRING_DESCRIPTOR,            /* bDescriptorType */"
+  echo "  /* Product name: \"$PRODUCT\" */"
+  echo "$PRODUCT" | sed -e "s/\(........\)/\1\\${nl}/g" | sed -n -e "s/\(.\)/'\1', 0, /g" -e "s/^/  /" -e "/^  ./s/ $//p"
+  echo '};'
+
+  if test -n "$name"; then
+  echo
+  echo "static uint8_t ${name}string_serial[] = {"
+  echo "  ${#SERIALNO}*2+2+16,                  /* bLength */"
+  echo "  STRING_DESCRIPTOR,            /* bDescriptorType */"
+  echo "  /* Serial number: \"$SERIALNO\" */"
+  echo "$SERIALNO" | sed -e "s/\(........\)/\1\\${nl}/g" | sed -n -e "s/\(.\)/'\1', 0, /g" -e "s/^/  /" -e "/^  ./s/ $//p"
+  if test "$emulation" = "yes"; then
+    echo "  'E', 0, 'M', 0, 'U', 0, 'L', 0,"
+    echo "  'A', 0, 'T', 0, 'E', 0, 'D', 0,"
+  else
+    echo "  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,"
+    echo "  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,"
+  fi
+  echo '};'
+  echo
+  echo "static const uint8_t ${name}revision_detail[] = {"
+  echo "  ${#REVISION}*2+2,                     /* bLength */"
+  echo "  STRING_DESCRIPTOR,            /* bDescriptorType */"
+  echo "  /* revision detail: \"$REVISION\" */"
+  echo "$REVISION" | sed -e "s/\(........\)/\1\\${nl}/g" | sed -n -e "s/\(.\)/'\1', 0, /g" -e "s/^/  /" -e "/^  ./s/ $//p"
+  echo '};'
+  echo
+  echo "static const uint8_t ${name}config_options[] = {"
+  echo "  ${#CONFIG}*2+2,                       /* bLength */"
+  echo "  STRING_DESCRIPTOR,            /* bDescriptorType */"
+  echo "  /* configure options: \"$CONFIG\" */"
+  echo $CONFIG | sed -e "s/\(........\)/\1\\${nl}/g" | sed -n -e "s/\(.\)/'\1', 0, /g" -e "s/^/  /" -e "/^  ./s/ $//p"
+  echo '};'
+  fi
+}
+
+(echo "#! /bin/bash"
+ echo
+ echo 'source "binary-edit.sh"') > put-vid-pid-ver.sh
 
 if !(IFS="     "
-  while read VIDPID VERSION PRODUCT VENDOR; do
+  while read -r VIDPID VERSION PRODUCT VENDOR; do
     if test "$vidpid" = "$VIDPID"; then
-      (echo $VIDPID | sed -n -e "s%^\([0-9a-f][0-9a-f]\)\([0-9a-f][0-9a-f]\):\([0-9a-f][0-9a-f]\)\([0-9a-f][0-9a-f]\)$%  0x\2, 0x\1, /* idVendor  */\n  0x\4, 0x\3, /* idProduct */%p"
-       echo $VERSION | sed -n -e "s%^\([0-9a-f][0-9a-f]\)\([0-9a-f][0-9a-f]\)$%  0x\2, 0x\1, /* bcdDevice  */%p"
-      ) > usb-vid-pid-ver.c.inc
-      (echo 'static const uint8_t neug_string_vendor[] = {'
-       echo "  ${#VENDOR}*2+2,                 /* bLength */"
-       echo "  USB_STRING_DESCRIPTOR_TYPE,     /* bDescriptorType */"
-       echo "  /* Manufacturer: \"$VENDOR\" */"
-       echo $VENDOR | sed -n -e "s/\(........\)/\1\n/gp" | sed -n -e "s/\(.\)/'\1', 0, /g" -e "s/^/  /" -e "s/ $//p"
-       echo '};'
-       echo
-       echo 'static const uint8_t neug_string_product[] = {'
-       echo "  ${#PRODUCT}*2+2,                        /* bLength */"
-       echo "  USB_STRING_DESCRIPTOR_TYPE,     /* bDescriptorType */"
-       echo "  /* Product name: \"$PRODUCT\" */"
-       echo $PRODUCT | sed -n -e "s/\(........\)/\1\n/gp" | sed -n -e "s/\(.\)/'\1', 0, /g" -e "s/^/  /" -e "s/ $//p"
-       echo '};'
-       echo
-       echo 'static uint8_t neug_string_serial[] = {'
-       echo "  ${#SERIALNO}*2+2+16,                    /* bLength */"
-       echo "  USB_STRING_DESCRIPTOR_TYPE,     /* bDescriptorType */"
-       echo "  /* Serial number: \"$SERIALNO\" */"
-       echo $SERIALNO | sed -n -e "s/\(........\)/\1\n/gp" | sed -n -e "s/\(.\)/'\1', 0, /g" -e "s/^/  /" -e "s/ $//p"
-       echo "  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,"
-       echo "  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,"
-       echo '};'
-       echo
-       echo '#ifdef USB_STRINGS_FOR_NEUG'
-       echo 'static const uint8_t neug_revision_detail[] = {'
-       echo "  ${#REVISION}*2+2,                       /* bLength */"
-       echo "  USB_STRING_DESCRIPTOR_TYPE,     /* bDescriptorType */"
-       echo "  /* revision detail: \"$REVISION\" */"
-       echo $REVISION | sed -n -e "s/\(........\)/\1\n/gp" | sed -n -e "s/\(.\)/'\1', 0, /g" -e "s/^/  /" -e "s/ $//p"
-       echo '};'
-       echo
-       echo 'static const uint8_t neug_config_options[] = {'
-       echo "  ${#CONFIG}*2+2,                 /* bLength */"
-       echo "  USB_STRING_DESCRIPTOR_TYPE,     /* bDescriptorType */"
-       echo "  /* configure options: \"$CONFIG\" */"
-       echo $CONFIG | sed -n -e "s/\(........\)/\1\n/gp" | sed -n -e "s/\(.\)/'\1', 0, /g" -e "s/^/  /" -e "s/ $//p"
-       echo '};'
-       echo '#endif'
-       ) >usb-strings.c.inc
+      echo                       >> put-vid-pid-ver.sh
+      echo 'addr=$file_off_ADDR' >> put-vid-pid-ver.sh
+      output_vid_pid_version     >> put-vid-pid-ver.sh
+      output_vendor_product_serial_strings neug_ >usb-strings.c.inc
       exit 0
+    elif test "$with_fraucheky" = "$VIDPID"; then
+      echo                                 >> put-vid-pid-ver.sh
+      echo 'addr=$file_off_fraucheky_ADDR' >> put-vid-pid-ver.sh
+      output_vid_pid_version               >> put-vid-pid-ver.sh
+      output_vendor_product_serial_strings >fraucheky-usb-strings.c.inc
     fi
   done; exit 1) < ../NEUG_USB_DEVICE_ID
 then
-  echo "Please specify valid Vendor ID and Product ID." 
-  echo "Check ../NEUG_USB_DEVICE_ID."
+  echo "Please specify valid Vendor ID and Product ID." >&2
+  echo "Check ../NEUG_USB_DEVICE_ID." >&2
   exit 1
 fi
 
 if test "$sys1_compat" = "no"; then
-   # Disable when you are sure that it's sys version 2.0.
-   # Note that Gnuk 1.0 and Neug (until 0.06) uses sys version 1.0.
-   # Disabling the compatibility, executable will be target independent,
-   # assuming the clock initialization will be done by SYS (before entry).
-   have_sys_h="-DHAVE_SYS_H"
+    # Disable when you are sure that it's sys version 3.0 or later.
+    # Note that Gnuk 1.0 and NeuG (until 0.06) uses sys version 1.0.
+    # Disabling the compatibility, executable will be target independent,
+    # assuming the clock initialization will be done by clock_init in 
+    # SYS.
+    use_sys3="-DUSE_SYS3"
 else
-   have_sys_h=""
+    use_sys3=""
 fi
 
 
-sed -e "s%@HAVE_SYS_H@%$have_sys_h%" \
-       < Makefile.in > Makefile
+(echo "CHIP=$chip";
+ echo "EMULATION=$emulation";
+ echo "CROSS=$cross";
+ echo "MCU=$mcu";
+ echo "DEFS=$use_sys3 $flash_override $def_emulation $def_mhz";
+ echo "LDSCRIPT=$ldscript";
+ echo "LIBS=$libs";
+ echo "ENABLE_FRAUCHEKY=$enable_fraucheky";
+ echo "ENABLE_OUTPUT_HEX=$enable_hexoutput"
+ if test "$ackbtn_support" = "yes"; then
+   echo "USE_ACKBTN=yes"
+ fi
+ if test "$emulation" = "yes"; then
+   echo "prefix=$prefix"
+   echo "exec_prefix=$exec_prefix"
+   echo "libexecdir=$libexecdir"
+ fi
+)      > config.mk
 sed -e "s/@ORIGIN@/$ORIGIN/" -e "s/@FLASH_SIZE@/$FLASH_SIZE/" \
     -e "s/@MEMORY_SIZE@/$MEMORY_SIZE/" \
     -e "s/@FLASH_PAGE_SIZE@/$FLASH_PAGE_SIZE/" \
        < neug.ld.in > neug.ld
 sed -e "s/@DFU_DEFINE@/$DFU_DEFINE/" \
-    -e "s/@TARGET_DEFINE@/$TARGET_DEFINE/" \
-    -e "s/@SERIALNO_STR_LEN@/$SERIALNO_STR_LEN_DEFINE/" \
+    -e "s/@SERIALNO_STR_LEN_DEFINE@/$SERIALNO_STR_LEN_DEFINE/" \
+    -e "s/@FRAUCHEKY_DEFINE@/$FRAUCHEKY_DEFINE/" \
+    -e "s/@FRAUCHEKY_MSC_DEFINE@/$FRAUCHEKY_MSC_DEFINE/" \
+    -e "s/@ACKBTN_DEFINE@/$ACKBTN_DEFINE/" \
        < config.h.in > config.h
 exit 0