From 8c58e5683cbd90d529eb1ea86fce019e553b2248 Mon Sep 17 00:00:00 2001 From: Tomasz Kramkowski Date: Thu, 5 Aug 2021 18:30:33 +0100 Subject: Cross compilation support --- .compile.do | 9 --------- .gitignore | 6 ++---- .link-executable.do | 9 --------- .link-library.do | 9 --------- .vars.rc.do | 22 ++++++++++++++++------ README.md | 16 ++++++++++++++++ all.do | 2 +- clean | 5 +++-- default.o.do | 9 +++++++-- default.tool.do | 25 +++++++++++++++++++++++++ libpack.do | 5 +++++ libpack.so.do | 5 ----- test.do | 4 ++-- test_gen.c | 1 + test_gen.do | 4 ++-- 15 files changed, 80 insertions(+), 51 deletions(-) delete mode 100755 .compile.do delete mode 100755 .link-executable.do delete mode 100755 .link-library.do create mode 100755 default.tool.do create mode 100755 libpack.do delete mode 100755 libpack.so.do diff --git a/.compile.do b/.compile.do deleted file mode 100755 index 238c93b..0000000 --- a/.compile.do +++ /dev/null @@ -1,9 +0,0 @@ -#!/usr/bin/env bash -set -e -redo-ifchange .vars.rc -. ./.vars.rc -exec >"$3" -echo "# generated by $0" -declare -p CC CFLAGS CPPFLAGS -echo '"$CC" -MMD -MF - "${CFLAGS[@]}" "${CPPFLAGS[@]}" -c -o "$3" "${1%.o}.c"' -if command -v redo-stamp &>/dev/null; then redo-stamp <"$3"; fi diff --git a/.gitignore b/.gitignore index 187e67e..63763aa 100644 --- a/.gitignore +++ b/.gitignore @@ -1,10 +1,7 @@ *.d *.inc *.o -*.so -.compile -.link-executable -.link-library +*.tool .redo/ .vars.rc all @@ -12,3 +9,4 @@ compile_flags.txt config.rc test test_gen +libpack diff --git a/.link-executable.do b/.link-executable.do deleted file mode 100755 index 6d9b89b..0000000 --- a/.link-executable.do +++ /dev/null @@ -1,9 +0,0 @@ -#!/usr/bin/env bash -set -e -redo-ifchange .vars.rc -. ./.vars.rc -exec >"$3" -echo "# generated by $0" -declare -p CC LDFLAGS LDLIBS -echo '"$CC" "${LDFLAGS[@]}" "${objects[@]}" "${LDLIBS[@]}" -o "$3"' -if command -v redo-stamp &>/dev/null; then redo-stamp <"$3"; fi diff --git a/.link-library.do b/.link-library.do deleted file mode 100755 index a8f4675..0000000 --- a/.link-library.do +++ /dev/null @@ -1,9 +0,0 @@ -#!/usr/bin/env bash -set -e -redo-ifchange .vars.rc -. ./.vars.rc -exec >"$3" -echo "# generated by $0" -declare -p CC LDFLAGS LDLIBS -echo '"$CC" "${LDFLAGS[@]}" -shared "${objects[@]}" "${LDLIBS[@]}" -o "$3"' -if command -v redo-stamp &>/dev/null; then redo-stamp <"$3"; fi diff --git a/.vars.rc.do b/.vars.rc.do index a5521e6..8a31fe7 100755 --- a/.vars.rc.do +++ b/.vars.rc.do @@ -3,9 +3,11 @@ set -e # Ensure variables from the config aren't set before the config is read to # avoid capturing transient state -unset CC CFLAGS CPPFLAGS LDFLAGS LDLIBS colour debug optimise verbose warn werror +unset {,T,H}{CC,CFLAGS,CPPFLAGS,LDFLAGS,LDLIBS} \ + colour debug optimise verbose warn werror -CFLAGS=(-std=c11 -fPIC) +CFLAGS=(-std=c11) +TCFLAGS=(-fPIC) warnings=( -Wall -Wcast-align -Wcast-qual -Wextra -Wpedantic -Wformat=2 @@ -13,7 +15,7 @@ warnings=( -Wstrict-prototypes ) -declare -a CFLAGS CPPFLAGS LDFLAGS LDLIBS +declare -a {,T,H}{CFLAGS,CPPFLAGS,LDFLAGS,LDLIBS} if [ -e config.rc ]; then redo-ifchange config.rc @@ -47,8 +49,8 @@ if [[ $debug ]]; then LDFLAGS+=(-Og -g) fi if [[ $optimise ]]; then - CFLAGS+=(-O2 -flto) - LDFLAGS+=(-O2 -flto) + TCFLAGS+=(-O2 -flto) + TLDFLAGS+=(-O2 -flto) fi if [[ $warn ]]; then CFLAGS+=("${warnings[@]}") @@ -60,9 +62,17 @@ if [[ $werror ]]; then CFLAGS+=(-Werror) fi +if [[ ! $TCC ]]; then TCC=$CC; fi +if [[ ! $HCC ]]; then HCC=$CC; fi +for p in T H; do + for v in CFLAGS CPPFLAGS LDFLAGS LDLIBS; do + eval "$p$v+=(\"\${$v[@]}\")" + done +done + { echo "# generated by $0" - declare -p CC CFLAGS CPPFLAGS LDFLAGS LDLIBS + declare -p {T,H}{CC,CFLAGS,CPPFLAGS,LDFLAGS,LDLIBS} } >"$3" if [[ $verbose ]]; then diff --git a/README.md b/README.md index b408e55..1bc870b 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,22 @@ linker for certain math.h functions. Run `redo all` to build the library and tests. +### Cross Compilation + +To cross compile, run `./configure` as normal and edit the resulting `config.rc` +to set `TCC` to the target compiler. Additional flags can be passed via the +arrays `TCFLAGS`, `TCPPFLAGS`, `TLDFLAGS`, and `TLDLIBS`. Host specific flags +can be passed via the corresponding `H` arrays. + +For example, to cross compile for windows using `i686-w64-mingw32-gcc` as the +target compiler and `clang` as the host compiler: + +``` +$ CC=clang ./configure +$ echo TCC=i686-w64-mingw32-gcc >>config.rc +$ redo all +``` + Testing ------- diff --git a/all.do b/all.do index 321cf2c..e8dccf3 100755 --- a/all.do +++ b/all.do @@ -1,2 +1,2 @@ #!/bin/sh -redo-ifchange libpack.so test compile_flags.txt +redo-ifchange libpack test compile_flags.txt diff --git a/clean b/clean index ae420c3..5d6fc0d 100755 --- a/clean +++ b/clean @@ -2,6 +2,7 @@ if command -v redo-targets >/dev/null 2>&1; then redo-targets | tr '\n' '\0' | xargs -r0 rm -f else - find . -type f \( -o -name '*.o' -o -name '*.inc' \) -exec rm -f {} + - rm -f .compile .link-executable .link-library .vars.rc all compile_flags.txt libpack.so test test_gen + find . -type f \( -o -name '*.o' -o -name '*.inc' -o '*.tool' \) \ + -exec rm -f {} + + rm -f .vars.rc all compile_flags.txt libpack.so test test_gen fi diff --git a/default.o.do b/default.o.do index 75a77d8..4ab2ffa 100755 --- a/default.o.do +++ b/default.o.do @@ -2,6 +2,11 @@ set -e redo-ifchg-pipe() { tr '\n' '\0' | xargs -r0 redo-ifchange; } src=${1%.o}.c -redo-ifchange .compile .parse-deps.sed "$src" +if grep -q '@BUILD_CC host' "$src"; then + compiler=compile-host.tool +else + compiler=compile.tool +fi +redo-ifchange "$compiler" .parse-deps.sed "$src" sed -n 's|.*@BUILD_DEP \(.*\)|\1|p' "$src" | redo-ifchg-pipe -. ./.compile | sed -f .parse-deps.sed | redo-ifchg-pipe +. "./$compiler" | sed -f .parse-deps.sed | redo-ifchg-pipe diff --git a/default.tool.do b/default.tool.do new file mode 100755 index 0000000..a4e18e7 --- /dev/null +++ b/default.tool.do @@ -0,0 +1,25 @@ +#!/usr/bin/env bash +set -e +redo-ifchange .vars.rc +if [[ $1 = *-host.tool ]]; then prefix=H; else prefix=T; fi +case "${1##*/}" in +compile*.tool) + vars=(CC CFLAGS CPPFLAGS) + cmd='"$CC" -MMD -MF - "${CFLAGS[@]}" "${CPPFLAGS[@]}" -c -o "$3" "${1%.o}.c"' + ;; +link-*.tool) + vars=(CC LDFLAGS LDLIBS) + if [[ $1 = *-library* ]]; then shared='-shared '; else shared=''; fi + cmd='"$CC" "${LDFLAGS[@]}" '"$shared"'"${objects[@]}" "${LDLIBS[@]}" -o "$3"' + ;; +*) + echo "Invalid tool name $1" >&2 + exit 1 ;; +esac +for v in "${vars[@]}"; do cmd=${cmd//$v/$prefix$v}; done +. ./.vars.rc +exec >"$3" +echo "#generated by $0 $1" +declare -p "${vars[@]/#/$prefix}" +echo "$cmd" +if command -v redo-stamp &>/dev/null; then redo-stamp <"$3"; fi diff --git a/libpack.do b/libpack.do new file mode 100755 index 0000000..3ecb302 --- /dev/null +++ b/libpack.do @@ -0,0 +1,5 @@ +#!/usr/bin/env bash +set -e +objects=(common.o pack.o trace.o unpack.o ieee754b.o) +redo-ifchange link-library.tool "${objects[@]}" +. ./link-library.tool diff --git a/libpack.so.do b/libpack.so.do deleted file mode 100755 index 68ad137..0000000 --- a/libpack.so.do +++ /dev/null @@ -1,5 +0,0 @@ -#!/usr/bin/env bash -set -e -objects=(common.o pack.o trace.o unpack.o ieee754b.o) -redo-ifchange .link-library "${objects[@]}" -. ./.link-library diff --git a/test.do b/test.do index eb36809..82cbd07 100755 --- a/test.do +++ b/test.do @@ -1,5 +1,5 @@ #!/usr/bin/env bash set -e objects=(common.o pack.o test.o trace.o unpack.o ieee754b.o) -redo-ifchange .link-executable "${objects[@]}" -. ./.link-executable +redo-ifchange link-executable.tool "${objects[@]}" +. ./link-executable.tool diff --git a/test_gen.c b/test_gen.c index 809b1ba..549d925 100644 --- a/test_gen.c +++ b/test_gen.c @@ -2,6 +2,7 @@ * Copyright (C) 2020-2021 Tomasz Kramkowski * SPDX-License-Identifier: MIT */ +// @BUILD_CC host #include #include #include diff --git a/test_gen.do b/test_gen.do index 044e1c2..1eab838 100755 --- a/test_gen.do +++ b/test_gen.do @@ -1,5 +1,5 @@ #!/usr/bin/env bash set -e objects=(test_gen.o) -redo-ifchange .link-executable "${objects[@]}" -. ./.link-executable +redo-ifchange link-executable-host.tool "${objects[@]}" +. ./link-executable-host.tool -- cgit v1.2.3-54-g00ecf