build_toolchain.sh 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #!/bin/sh
  2. #
  3. # FPGA toolchain install script.
  4. # This script installs the full FPGA toolchain into the
  5. # directory specified as INSTALLDIR.
  6. #
  7. # The toolchain will be installed into this directory:
  8. INSTALLDIR="$HOME/fpga-toolchain"
  9. # The following directory will be used as temporary build directory:
  10. BUILDDIR="./toolchain-build"
  11. # Run at most this many build processes in parallel:
  12. PARALLEL="$(getconf _NPROCESSORS_ONLN)"
  13. # The following tools are needed to build the toolchain:
  14. # - gcc
  15. # - clang
  16. # - python3
  17. # - cmake
  18. # - git
  19. #
  20. # Please ensure that all these tools are installed in the system.
  21. #####################################################################
  22. #####################################################################
  23. #####################################################################
  24. # Source repositories:
  25. REPO_ICESTORM="https://github.com/cliffordwolf/icestorm.git"
  26. REPO_NEXTPNR="https://github.com/YosysHQ/nextpnr.git"
  27. REPO_YOSYS="https://github.com/YosysHQ/yosys.git"
  28. REPO_TINYPROG="https://github.com/tinyfpga/TinyFPGA-Bootloader.git"
  29. BUILD_ICESTORM=1
  30. BUILD_NEXTPNR=1
  31. BUILD_YOSYS=1
  32. BUILD_TINYPROG=1
  33. die()
  34. {
  35. echo "$*" >&2
  36. exit 1
  37. }
  38. checkprog()
  39. {
  40. local prog="$1"
  41. which "$prog" >/dev/null ||\
  42. die "$prog is not installed. Please install it by use of the distribution package manager (apt, apt-get, rpm, etc...)"
  43. }
  44. [ "$(id -u)" = "0" ] && die "Do not run this as root!"
  45. checkprog gcc
  46. checkprog clang
  47. checkprog python3
  48. checkprog cmake
  49. checkprog git
  50. BUILDDIR="$(realpath -m -s "$BUILDDIR")"
  51. INSTALLDIR="$(realpath -m -s "$INSTALLDIR")"
  52. echo "BUILDDIR=$BUILDDIR"
  53. echo "INSTALLDIR=$INSTALLDIR"
  54. echo "PARALLEL=$PARALLEL"
  55. [ -n "$BUILDDIR" -a -n "$INSTALLDIR" ] || die "Failed to resolve directories"
  56. echo
  57. # Cleanup
  58. rm -rf "$BUILDDIR" || die "Failed to cleanup BUILDDIR"
  59. mkdir -p "$BUILDDIR" || die "Failed to create BUILDDIR"
  60. mkdir -p "$INSTALLDIR" || die "Failed to create INSTALLDIR"
  61. newpath="\$PATH"
  62. # Project Icestorm
  63. if [ $BUILD_ICESTORM -ne 0 ]; then
  64. echo "Building icestorm..."
  65. cd "$BUILDDIR" || die "Failed to cd to builddir."
  66. git clone "$REPO_ICESTORM" "$BUILDDIR/icestorm" || die "Failed to clone icestorm"
  67. cd "$BUILDDIR/icestorm" || die "Failed to cd to icestorm."
  68. export PREFIX="$INSTALLDIR/icestorm"
  69. make -j "$PARALLEL" PREFIX="$PREFIX" || die "Failed to build icestorm"
  70. rm -rf "$PREFIX" || die "Failed to clean install icestorm"
  71. make install PREFIX="$PREFIX" || die "Failed to install icestorm"
  72. newpath="$PREFIX/bin:$newpath"
  73. fi
  74. # nextpnr
  75. if [ $BUILD_NEXTPNR -ne 0 ]; then
  76. echo "Building nextpnr..."
  77. cd "$BUILDDIR" || die "Failed to cd to builddir."
  78. git clone "$REPO_NEXTPNR" "$BUILDDIR/nextpnr" || die "Failed to clone nextpnr"
  79. mkdir "$BUILDDIR/nextpnr/builddir" || die "Failed to create nextpnr builddir"
  80. cd "$BUILDDIR/nextpnr/builddir" || die "Failed to cd to nextpnr."
  81. export PREFIX="$INSTALLDIR/nextpnr"
  82. cmake -DARCH=ice40 -DICEBOX_ROOT="$INSTALLDIR/icestorm/share/icebox" -DCMAKE_INSTALL_PREFIX="$PREFIX" .. || die "Failed to build nextpnr"
  83. make -j "$PARALLEL" || die "Failed to build nextpnr"
  84. rm -rf "$PREFIX" || die "Failed to clean install nextpnr"
  85. make install || die "Failed to install nextpnr"
  86. newpath="$PREFIX/bin:$newpath"
  87. fi
  88. # yosys
  89. if [ $BUILD_YOSYS -ne 0 ]; then
  90. echo "Building yosys..."
  91. cd "$BUILDDIR" || die "Failed to cd to builddir."
  92. git clone "$REPO_YOSYS" "$BUILDDIR/yosys" || die "Failed to clone yosys"
  93. cd "$BUILDDIR/yosys" || die "Failed to cd to yosys."
  94. export PREFIX="$INSTALLDIR/yosys"
  95. make config-clang PREFIX="$PREFIX" || die "Failed to configure yosys"
  96. make -j "$PARALLEL" PREFIX="$PREFIX" || die "Failed to build yosys"
  97. rm -rf "$PREFIX" || die "Failed to clean install yosys"
  98. make install PREFIX="$PREFIX" || die "Failed to install yosys"
  99. newpath="$PREFIX/bin:$newpath"
  100. fi
  101. # tinyprog
  102. if [ $BUILD_TINYPROG -ne 0 ]; then
  103. echo "Building tinyprog..."
  104. cd "$BUILDDIR" || die "Failed to cd to builddir."
  105. git clone "$REPO_TINYPROG" "$BUILDDIR/TinyFPGA-Bootloader" || die "Failed to clone tinyprog"
  106. cd "$BUILDDIR/TinyFPGA-Bootloader/programmer" || die "Failed to cd to tinyprog."
  107. export PREFIX="$INSTALLDIR/tinyprog"
  108. rm -rf "$PREFIX" || die "Failed to clean install tinyprog"
  109. mkdir -p "$PREFIX/lib" || die "Failed to create tinyprog lib"
  110. mkdir -p "$PREFIX/bin" || die "Failed to create tinyprog bin"
  111. cp -r "$BUILDDIR/TinyFPGA-Bootloader/programmer/tinyprog" "$PREFIX/lib/" || die "Failed to install tinyprog"
  112. cat > "$PREFIX/bin/tinyprog" <<EOF
  113. #!/bin/sh
  114. export PYTHONPATH="$PREFIX/lib/:\$PYTHONPATH"
  115. exec python3 "$PREFIX/lib/tinyprog" "\$@"
  116. EOF
  117. [ -f "$PREFIX/bin/tinyprog" ] || die "Failed to install tinyprog wrapper"
  118. chmod 755 "$PREFIX/bin/tinyprog" || die "Failed to chmod tinyprog"
  119. newpath="$PREFIX/bin:$newpath"
  120. fi
  121. echo
  122. echo
  123. echo
  124. echo "Successfully built and installed all FPGA tools to: $INSTALLDIR"
  125. echo "Please add the following line to your $HOME/.bashrc file:"
  126. echo
  127. echo "export PATH=\"$newpath\""
  128. echo