aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTomasz Kramkowski <tk@the-tk.com>2021-08-05 18:30:33 +0100
committerTomasz Kramkowski <tk@the-tk.com>2021-08-05 20:17:48 +0100
commit8c58e5683cbd90d529eb1ea86fce019e553b2248 (patch)
treeb31e8754a56ab0a87170bfecffca3ef434850826
parent7e7bdc19e76a5f87bccf7a84872f8d45295ee5b7 (diff)
downloadpack-8c58e5683cbd90d529eb1ea86fce019e553b2248.tar.gz
pack-8c58e5683cbd90d529eb1ea86fce019e553b2248.tar.xz
pack-8c58e5683cbd90d529eb1ea86fce019e553b2248.zip
Cross compilation support
-rwxr-xr-x.compile.do9
-rw-r--r--.gitignore6
-rwxr-xr-x.link-executable.do9
-rwxr-xr-x.link-library.do9
-rwxr-xr-x.vars.rc.do22
-rw-r--r--README.md16
-rwxr-xr-xall.do2
-rwxr-xr-xclean5
-rwxr-xr-xdefault.o.do9
-rwxr-xr-xdefault.tool.do25
-rwxr-xr-xlibpack.do (renamed from libpack.so.do)4
-rwxr-xr-xtest.do4
-rw-r--r--test_gen.c1
-rwxr-xr-xtest_gen.do4
14 files changed, 77 insertions, 48 deletions
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<name>` 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.so.do b/libpack.do
index 68ad137..3ecb302 100755
--- a/libpack.so.do
+++ b/libpack.do
@@ -1,5 +1,5 @@
#!/usr/bin/env bash
set -e
objects=(common.o pack.o trace.o unpack.o ieee754b.o)
-redo-ifchange .link-library "${objects[@]}"
-. ./.link-library
+redo-ifchange link-library.tool "${objects[@]}"
+. ./link-library.tool
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 <tk@the-tk.com>
* SPDX-License-Identifier: MIT
*/
+// @BUILD_CC host
#include <assert.h>
#include <ctype.h>
#include <inttypes.h>
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