diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..a4c1b8d --- /dev/null +++ b/.editorconfig @@ -0,0 +1,7 @@ +root = true + +[*] +end_of_line = lf +indent_style = tab +trim_trailing_whitespace = true +insert_final_newline = true diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..6313b56 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +* text=auto eol=lf diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..254135c --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,86 @@ +name: Build + +on: + push: + branches: + - '**' + +jobs: + native-build: + name: 'Native build' + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@main + with: + submodules: true + - name: Build Ai + run: | + bash './build.sh' 'native' + - name: Generate tarball + run: | + declare tarball_filename='/tmp/x86_64-linux-gnu.tar.xz' + tar --directory='/tmp' --create --file=- 'ai' | xz --threads='0' --compress -9 > "${tarball_filename}" + sha256sum "${tarball_filename}" | sed 's|/tmp/||' > "${tarball_filename}.sha256" + - name: Upload artifact + uses: actions/upload-artifact@main + with: + name: native-toolchain + if-no-files-found: error + path: | + /tmp/x86_64-linux-gnu.tar.xz + /tmp/x86_64-linux-gnu.tar.xz.sha256 + + cross-build: + name: 'Cross build' + needs: native-build + runs-on: ubuntu-latest + continue-on-error: true + strategy: + matrix: + target: [ + 'alpha-unknown-linux-gnu', + 'x86_64-unknown-linux-gnu', + 'i386-unknown-linux-gnu', + 'arm-unknown-linux-gnueabi', + 'arm-unknown-linux-gnueabihf', + 'hppa-unknown-linux-gnu', + 'aarch64-unknown-linux-gnu', + 'mips-unknown-linux-gnu', + 'mipsel-unknown-linux-gnu', + 'powerpc-unknown-linux-gnu', + 's390-unknown-linux-gnu', + 's390x-unknown-linux-gnu', + 'sparc-unknown-linux-gnu', + 'powerpc64le-unknown-linux-gnu' + ] + steps: + - uses: actions/checkout@main + with: + submodules: true + - name: Download artifact + uses: actions/download-artifact@main + with: + name: native-toolchain + - name: Setup toolchain + run: | + tar --directory='/tmp' --extract --file='./x86_64-linux-gnu.tar.xz' + mv '/tmp/ai' '/tmp/ai-toolchain' + - name: Build Ai with OBGGCC + run: | + source './tools/setup_toolchain.sh' + source './submodules/obggcc/tools/setup_toolchain.sh' + + bash './build.sh' '${{ matrix.target }}' + - name: Generate tarball + run: | + declare tarball_filename='/tmp/${{ matrix.target }}.tar.xz' + tar --directory='/tmp' --create --file=- 'ai' | xz --threads='0' --compress -9 > "${tarball_filename}" + sha256sum "${tarball_filename}" | sed 's|/tmp/||' > "${tarball_filename}.sha256" + - name: Upload artifact + uses: actions/upload-artifact@main + with: + name: cross-toolchain + if-no-files-found: error + path: | + /tmp/${{ matrix.target }}.tar.xz + /tmp/${{ matrix.target }}.tar.xz.sha256 diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..013bb83 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "submodules/obggcc"] + path = submodules/obggcc + url = https://github.com/AmanoTeam/obggcc diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..153d416 --- /dev/null +++ b/LICENSE @@ -0,0 +1,165 @@ + GNU LESSER GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + + This version of the GNU Lesser General Public License incorporates +the terms and conditions of version 3 of the GNU General Public +License, supplemented by the additional permissions listed below. + + 0. Additional Definitions. + + As used herein, "this License" refers to version 3 of the GNU Lesser +General Public License, and the "GNU GPL" refers to version 3 of the GNU +General Public License. + + "The Library" refers to a covered work governed by this License, +other than an Application or a Combined Work as defined below. + + An "Application" is any work that makes use of an interface provided +by the Library, but which is not otherwise based on the Library. +Defining a subclass of a class defined by the Library is deemed a mode +of using an interface provided by the Library. + + A "Combined Work" is a work produced by combining or linking an +Application with the Library. The particular version of the Library +with which the Combined Work was made is also called the "Linked +Version". + + The "Minimal Corresponding Source" for a Combined Work means the +Corresponding Source for the Combined Work, excluding any source code +for portions of the Combined Work that, considered in isolation, are +based on the Application, and not on the Linked Version. + + The "Corresponding Application Code" for a Combined Work means the +object code and/or source code for the Application, including any data +and utility programs needed for reproducing the Combined Work from the +Application, but excluding the System Libraries of the Combined Work. + + 1. Exception to Section 3 of the GNU GPL. + + You may convey a covered work under sections 3 and 4 of this License +without being bound by section 3 of the GNU GPL. + + 2. Conveying Modified Versions. + + If you modify a copy of the Library, and, in your modifications, a +facility refers to a function or data to be supplied by an Application +that uses the facility (other than as an argument passed when the +facility is invoked), then you may convey a copy of the modified +version: + + a) under this License, provided that you make a good faith effort to + ensure that, in the event an Application does not supply the + function or data, the facility still operates, and performs + whatever part of its purpose remains meaningful, or + + b) under the GNU GPL, with none of the additional permissions of + this License applicable to that copy. + + 3. Object Code Incorporating Material from Library Header Files. + + The object code form of an Application may incorporate material from +a header file that is part of the Library. You may convey such object +code under terms of your choice, provided that, if the incorporated +material is not limited to numerical parameters, data structure +layouts and accessors, or small macros, inline functions and templates +(ten or fewer lines in length), you do both of the following: + + a) Give prominent notice with each copy of the object code that the + Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the object code with a copy of the GNU GPL and this license + document. + + 4. Combined Works. + + You may convey a Combined Work under terms of your choice that, +taken together, effectively do not restrict modification of the +portions of the Library contained in the Combined Work and reverse +engineering for debugging such modifications, if you also do each of +the following: + + a) Give prominent notice with each copy of the Combined Work that + the Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the Combined Work with a copy of the GNU GPL and this license + document. + + c) For a Combined Work that displays copyright notices during + execution, include the copyright notice for the Library among + these notices, as well as a reference directing the user to the + copies of the GNU GPL and this license document. + + d) Do one of the following: + + 0) Convey the Minimal Corresponding Source under the terms of this + License, and the Corresponding Application Code in a form + suitable for, and under terms that permit, the user to + recombine or relink the Application with a modified version of + the Linked Version to produce a modified Combined Work, in the + manner specified by section 6 of the GNU GPL for conveying + Corresponding Source. + + 1) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (a) uses at run time + a copy of the Library already present on the user's computer + system, and (b) will operate properly with a modified version + of the Library that is interface-compatible with the Linked + Version. + + e) Provide Installation Information, but only if you would otherwise + be required to provide such information under section 6 of the + GNU GPL, and only to the extent that such information is + necessary to install and execute a modified version of the + Combined Work produced by recombining or relinking the + Application with a modified version of the Linked Version. (If + you use option 4d0, the Installation Information must accompany + the Minimal Corresponding Source and Corresponding Application + Code. If you use option 4d1, you must provide the Installation + Information in the manner specified by section 6 of the GNU GPL + for conveying Corresponding Source.) + + 5. Combined Libraries. + + You may place library facilities that are a work based on the +Library side by side in a single library together with other library +facilities that are not Applications and are not covered by this +License, and convey such a combined library under terms of your +choice, if you do both of the following: + + a) Accompany the combined library with a copy of the same work based + on the Library, uncombined with any other library facilities, + conveyed under the terms of this License. + + b) Give prominent notice with the combined library that part of it + is a work based on the Library, and explaining where to find the + accompanying uncombined form of the same work. + + 6. Revised Versions of the GNU Lesser General Public License. + + The Free Software Foundation may publish revised and/or new versions +of the GNU Lesser General Public License from time to time. Such new +versions will be similar in spirit to the present version, but may +differ in detail to address new problems or concerns. + + Each version is given a distinguishing version number. If the +Library as you received it specifies that a certain numbered version +of the GNU Lesser General Public License "or any later version" +applies to it, you have the option of following the terms and +conditions either of that published version or of any later version +published by the Free Software Foundation. If the Library as you +received it does not specify a version number of the GNU Lesser +General Public License, you may choose any version of the GNU Lesser +General Public License ever published by the Free Software Foundation. + + If the Library as you received it specifies that a proxy can decide +whether future versions of the GNU Lesser General Public License shall +apply, that proxy's public statement of acceptance of any version is +permanent authorization for you to choose that version for the +Library. \ No newline at end of file diff --git a/arm-bookeen-linux-gnueabi.sh b/arm-bookeen-linux-gnueabi.sh new file mode 100644 index 0000000..9e80956 --- /dev/null +++ b/arm-bookeen-linux-gnueabi.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +declare extra_configure_flags='--with-cpu=cortex-a8 --with-fpu=neon --with-float=softfp --with-mode=thumb' + +declare triplet='arm-bookeen-linux-gnueabi' + +declare sysroot='https://web.archive.org/web/0if_/https://github.com/koreader/koxtoolchain/releases/latest/download/bookeen.zip' diff --git a/build.sh b/build.sh new file mode 100644 index 0000000..09b5cda --- /dev/null +++ b/build.sh @@ -0,0 +1,262 @@ +#!/bin/bash + +set -eu + +declare -r current_source_directory="${PWD}" + +declare -r revision="$(git rev-parse --short HEAD)" + +declare -r toolchain_directory='/tmp/ai' + +declare -r gmp_tarball='/tmp/gmp.tar.xz' +declare -r gmp_directory='/tmp/gmp-6.2.1' + +declare -r mpfr_tarball='/tmp/mpfr.tar.xz' +declare -r mpfr_directory='/tmp/mpfr-4.2.0' + +declare -r mpc_tarball='/tmp/mpc.tar.gz' +declare -r mpc_directory='/tmp/mpc-1.3.1' + +declare -r binutils_tarball='/tmp/binutils.tar.xz' +declare -r binutils_directory='/tmp/binutils-2.41' + +declare -r gcc_tarball='/tmp/gcc.tar.gz' +declare -r gcc_directory='/tmp/gcc-13.2.0' + +declare -r optflags='-Os' +declare -r linkflags='-Wl,-s' + +declare -r max_jobs="$(($(nproc) * 8))" + +declare build_type="${1}" + +if [ -z "${build_type}" ]; then + build_type='native' +fi + +declare is_native='0' + +if [ "${build_type}" == 'native' ]; then + is_native='1' +fi + +declare OBGGCC_TOOLCHAIN='/tmp/obggcc-toolchain' +declare CROSS_COMPILE_TRIPLET='' + +declare cross_compile_flags='' + +if ! (( is_native )); then + source "./submodules/obggcc/toolchains/${build_type}.sh" + cross_compile_flags+="--host=${CROSS_COMPILE_TRIPLET}" +fi + +if ! [ -f "${gmp_tarball}" ]; then + curl --connect-timeout '10' --retry '15' --retry-all-errors --fail --silent --url 'https://mirrors.kernel.org/gnu/gmp/gmp-6.2.1.tar.xz' --output "${gmp_tarball}" + tar --directory="$(dirname "${gmp_directory}")" --extract --file="${gmp_tarball}" +fi + +if ! [ -f "${mpfr_tarball}" ]; then + curl --connect-timeout '10' --retry '15' --retry-all-errors --fail --silent --url 'https://mirrors.kernel.org/gnu/mpfr/mpfr-4.2.0.tar.xz' --output "${mpfr_tarball}" + tar --directory="$(dirname "${mpfr_directory}")" --extract --file="${mpfr_tarball}" +fi + +if ! [ -f "${mpc_tarball}" ]; then + curl --connect-timeout '10' --retry '15' --retry-all-errors --fail --silent --url 'https://mirrors.kernel.org/gnu/mpc/mpc-1.3.1.tar.gz' --output "${mpc_tarball}" + tar --directory="$(dirname "${mpc_directory}")" --extract --file="${mpc_tarball}" +fi + +if ! [ -f "${binutils_tarball}" ]; then + curl --connect-timeout '10' --retry '15' --retry-all-errors --fail --silent --url 'https://mirrors.kernel.org/gnu/binutils/binutils-2.41.tar.xz' --output "${binutils_tarball}" + tar --directory="$(dirname "${binutils_directory}")" --extract --file="${binutils_tarball}" +fi + +if ! [ -f "${gcc_tarball}" ]; then + curl --connect-timeout '10' --retry '15' --retry-all-errors --fail --silent --url 'https://mirrors.kernel.org/gnu/gcc/gcc-13.2.0/gcc-13.2.0.tar.xz' --output "${gcc_tarball}" + tar --directory="$(dirname "${gcc_directory}")" --extract --file="${gcc_tarball}" +fi + +[ -d "${gmp_directory}/build" ] || mkdir "${gmp_directory}/build" + +cd "${gmp_directory}/build" + +../configure \ + --prefix="${toolchain_directory}" \ + --enable-shared \ + --enable-static \ + ${cross_compile_flags} \ + CFLAGS="${optflags}" \ + CXXFLAGS="${optflags}" \ + LDFLAGS="${linkflags}" + +make all --jobs +make install + +[ -d "${mpfr_directory}/build" ] || mkdir "${mpfr_directory}/build" + +cd "${mpfr_directory}/build" + +../configure \ + --prefix="${toolchain_directory}" \ + --with-gmp="${toolchain_directory}" \ + --enable-shared \ + --enable-static \ + ${cross_compile_flags} \ + CFLAGS="${optflags}" \ + CXXFLAGS="${optflags}" \ + LDFLAGS="${linkflags}" + +make all --jobs +make install + +[ -d "${mpc_directory}/build" ] || mkdir "${mpc_directory}/build" + +cd "${mpc_directory}/build" + +../configure \ + --prefix="${toolchain_directory}" \ + --with-gmp="${toolchain_directory}" \ + --enable-shared \ + --enable-static \ + ${cross_compile_flags} \ + CFLAGS="${optflags}" \ + CXXFLAGS="${optflags}" \ + LDFLAGS="${linkflags}" + +make all --jobs +make install + +declare -ra targets=( + 'arm-bookeen-linux-gnueabi' +) + +for target in "${targets[@]}"; do + source "${current_source_directory}/${target}.sh" + + declare sysroot_filename='/tmp/sysroot.zip' + declare sysroot_directory="/tmp/x-tools/${triplet}/${triplet}/sysroot" + + curl \ + --connect-timeout '10' \ + --retry '15' \ + --retry-all-errors \ + --fail \ + --silent \ + --location \ + --output "${sysroot_filename}" \ + --url "${sysroot}" + + unzip -d '/tmp' "${sysroot_filename}" + + tar --extract --directory='/tmp' --file="/tmp/$(basename "${sysroot}" '.zip').tar.gz" + + [ -d "${toolchain_directory}/${triplet}/lib" ] || mkdir --parent "${toolchain_directory}/${triplet}/lib" + + cp --recursive "${sysroot_directory}/lib/"* "${toolchain_directory}/${triplet}/lib" + cp --recursive "${sysroot_directory}/usr/lib/"* "${toolchain_directory}/${triplet}/lib" + cp --recursive "${sysroot_directory}/usr/include" "${toolchain_directory}/${triplet}" + + while read name; do + if [ -f "${name}" ]; then + chmod 644 "${name}" + elif [ -d "${name}" ]; then + chmod 755 "${name}" + fi + done <<< "$(find "${toolchain_directory}/${triplet}")" + + cd "${toolchain_directory}/${triplet}/lib" + + find "${toolchain_directory}/${triplet}/lib" -type 'l' | xargs ls -l | grep '/lib/' | awk '{print "unlink "$9" && ln --symbolic $(basename "$11") $(basename "$9")"}' | /proc/self/exe + find "${toolchain_directory}/${triplet}/lib" -maxdepth '1' -mindepth '1' -type 'd' -exec rm --recursive {} \; + + sed --in-place 's|/usr/lib|.|g; s|/lib/|./|g' "${toolchain_directory}/${triplet}/lib/libc.so" "${toolchain_directory}/${triplet}/lib/libpthread.so" + + [ -d "${binutils_directory}/build" ] || mkdir "${binutils_directory}/build" + + cd "${binutils_directory}/build" + rm --force --recursive ./* + + ../configure \ + --target="${triplet}" \ + --prefix="${toolchain_directory}" \ + --enable-gold \ + --enable-ld \ + --enable-lto \ + --disable-gprofng \ + --with-static-standard-libraries \ + --with-sysroot="${toolchain_directory}/${triplet}" \ + ${cross_compile_flags} \ + CFLAGS="${optflags}" \ + CXXFLAGS="${optflags}" \ + LDFLAGS="${linkflags}" + + make all --jobs="${max_jobs}" + make install + + [ -d "${gcc_directory}/build" ] || mkdir "${gcc_directory}/build" + + cd "${gcc_directory}/build" + + rm --force --recursive ./* + + ../configure \ + --target="${triplet}" \ + --prefix="${toolchain_directory}" \ + --with-linker-hash-style='gnu' \ + --with-gmp="${toolchain_directory}" \ + --with-mpc="${toolchain_directory}" \ + --with-mpfr="${toolchain_directory}" \ + --with-bugurl='https://github.com/AmanoTeam/Ai/issues' \ + --with-pkgversion="Ai v0.1-${revision}" \ + --with-sysroot="${toolchain_directory}/${triplet}" \ + --with-native-system-header-dir='/include' \ + --enable-__cxa_atexit \ + --enable-cet='auto' \ + --enable-checking='release' \ + --enable-clocale='gnu' \ + --enable-default-ssp \ + --enable-gnu-indirect-function \ + --enable-gnu-unique-object \ + --enable-libstdcxx-backtrace \ + --enable-link-serialization='1' \ + --enable-linker-build-id \ + --enable-lto \ + --enable-shared \ + --enable-threads='posix' \ + --enable-libssp \ + --enable-languages='c,c++' \ + --enable-ld \ + --enable-gold \ + --disable-libgomp \ + --disable-bootstrap \ + --disable-libstdcxx-pch \ + --disable-werror \ + --disable-multilib \ + --disable-plugin \ + --disable-nls \ + --without-headers \ + ${extra_configure_flags} \ + ${cross_compile_flags} \ + CFLAGS="${optflags}" \ + CXXFLAGS="${optflags}" \ + LDFLAGS="-Wl,-rpath-link,${OBGGCC_TOOLCHAIN}/${CROSS_COMPILE_TRIPLET}/lib ${linkflags}" + + LD_LIBRARY_PATH="${toolchain_directory}/lib" PATH="${PATH}:${toolchain_directory}/bin" make \ + CFLAGS_FOR_TARGET="${optflags} ${linkflags}" \ + CXXFLAGS_FOR_TARGET="${optflags} ${linkflags}" \ + all --jobs="${max_jobs}" + make install + + cd "${toolchain_directory}/${triplet}/bin" + + for name in *; do + rm "${name}" + ln -s "../../bin/${triplet}-${name}" "${name}" + done + + rm --recursive "${toolchain_directory}/share" + rm --recursive "${toolchain_directory}/lib/gcc/${triplet}/"*"/include-fixed" + + patchelf --add-rpath '$ORIGIN/../../../../lib' "${toolchain_directory}/libexec/gcc/${triplet}/"*"/cc1" + patchelf --add-rpath '$ORIGIN/../../../../lib' "${toolchain_directory}/libexec/gcc/${triplet}/"*"/cc1plus" + patchelf --add-rpath '$ORIGIN/../../../../lib' "${toolchain_directory}/libexec/gcc/${triplet}/"*"/lto1" +done \ No newline at end of file diff --git a/build_all.sh b/build_all.sh new file mode 100644 index 0000000..e63c984 --- /dev/null +++ b/build_all.sh @@ -0,0 +1,39 @@ +#!/bin/bash + +set -eu + +declare -ra targets=( + 'alpha-unknown-linux-gnu' + 'x86_64-unknown-linux-gnu' + 'i386-unknown-linux-gnu' + 'arm-unknown-linux-gnueabi' + 'arm-unknown-linux-gnueabihf' + 'hppa-unknown-linux-gnu' + 'aarch64-unknown-linux-gnu' + 'mips-unknown-linux-gnu' + 'mipsel-unknown-linux-gnu' + 'powerpc-unknown-linux-gnu' + 's390-unknown-linux-gnu' + 's390x-unknown-linux-gnu' + 'sparc-unknown-linux-gnu' + 'powerpc64le-unknown-linux-gnu' + 'mips64el-unknown-linux-gnuabi64' +) + +declare -r tarballs_directory="${PWD}/ai-tarballs" + +[ -d "${tarballs_directory}" ] || mkdir "${tarballs_directory}" + +source './tools/setup_toolchain.sh' +source './submodules/obggcc/tools/setup_toolchain.sh' + +for target in "${targets[@]}"; do + bash './build.sh' "${target}" + + declare tarball_filename="${tarballs_directory}/${target}.tar.xz" + + tar --directory='/tmp' --create --file=- 'ai' | xz --threads=0 --compress -9 > "${tarball_filename}" + sha256sum "${tarball_filename}" > "${tarball_filename}.sha256" + + rm --recursive --force '/tmp/ai' +done diff --git a/submodules/obggcc b/submodules/obggcc new file mode 160000 index 0000000..9499bd3 --- /dev/null +++ b/submodules/obggcc @@ -0,0 +1 @@ +Subproject commit 9499bd3eeb8a005cbea5c22c1c839f79e2f34e25 diff --git a/tools/setup_toolchain.sh b/tools/setup_toolchain.sh new file mode 100644 index 0000000..cf14b6c --- /dev/null +++ b/tools/setup_toolchain.sh @@ -0,0 +1,28 @@ +#!/bin/bash + +set -eu + +declare -r AI_HOME='/tmp/ai-toolchain' + +if [ -d "${AI_HOME}" ]; then + PATH+=":${AI_HOME}/bin" + export AI_HOME \ + PATH + return 0 +fi + +declare -r AI_CROSS_TAG="$(jq --raw-output '.tag_name' <<< "$(curl --retry 10 --retry-delay 3 --silent --url 'https://api.github.com/repos/AmanoTeam/Ai/releases/latest')")" +declare -r AI_CROSS_TARBALL='/tmp/ai.tar.xz' +declare -r AI_CROSS_URL="https://github.com/AmanoTeam/Ai/releases/download/${AI_CROSS_TAG}/x86_64-unknown-linux-gnu.tar.xz" + +curl --connect-timeout '10' --retry '15' --retry-all-errors --fail --silent --location --url "${AI_CROSS_URL}" --output "${AI_CROSS_TARBALL}" +tar --directory="$(dirname "${AI_CROSS_TARBALL}")" --extract --file="${AI_CROSS_TARBALL}" + +rm "${AI_CROSS_TARBALL}" + +mv '/tmp/ai' "${AI_HOME}" + +PATH+=":${AI_HOME}/bin" + +export AI_HOME \ + PATH