Skip to content

Commit

Permalink
Make cosmocc capable of cross compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
jart committed Aug 13, 2023
1 parent 504a4bb commit 4816e28
Show file tree
Hide file tree
Showing 21 changed files with 435 additions and 128 deletions.
13 changes: 13 additions & 0 deletions bin/aarch64-unknown-cosmo-addr2line
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/sh

MODE=${MODE:-${m:-aarch64}}
COSMO=${COSMO:-/opt/cosmo}
COSMOS=${COSMOS:-/opt/cosmos/aarch64}
TOOL="$COSMO/o/third_party/gcc/bin/aarch64-linux-musl-addr2line"

if [ ! -x "$TOOL" ]; then
echo "$0: you need to run: aarch64-unknown-cosmo-cc --update" >&2
exit 1
fi

exec "$TOOL" "$@"
1 change: 1 addition & 0 deletions bin/aarch64-unknown-cosmo-apecopy
29 changes: 29 additions & 0 deletions bin/aarch64-unknown-cosmo-ar
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/sh

PROG=${0##*/}
MODE=${MODE:-${m:-aarch64}}
COSMO=${COSMO:-/opt/cosmo}

fatal_error() {
echo "$PROG: $1" >&2
exit 1
}

if [ ! -x "$COSMO/o//tool/build/ar.com" ]; then
fatal_error "you need to run: aarch64-unknown-cosmo-cc --update"
fi

if [ x"$1" = x"rc" ] ||
[ x"$1" = x"rcD" ] ||
[ x"$1" = x"rcu" ] |
[ x"$1" = x"rcuD" ] ||
[ x"$1" = x"rcs" ] ||
[ x"$1" = x"rcsD" ] ||
[ x"$1" = x"--help" ] ||
[ x"$1" = x"--version" ]; then
AR="$COSMO/o//tool/build/ar.com"
else
AR="$COSMO/o/third_party/gcc/bin/aarch64-linux-musl-ar"
fi

exec "$AR" "$@"
13 changes: 13 additions & 0 deletions bin/aarch64-unknown-cosmo-as
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/sh

MODE=${MODE:-${m:-aarch64}}
COSMO=${COSMO:-/opt/cosmo}
COSMOS=${COSMOS:-/opt/cosmos/aarch64}
TOOL="$COSMO/o/third_party/gcc/bin/aarch64-linux-musl-as"

if [ ! -x "$TOOL" ]; then
echo "$0: you need to run: aarch64-unknown-cosmo-cc --update" >&2
exit 1
fi

exec "$TOOL" "$@"
1 change: 1 addition & 0 deletions bin/aarch64-unknown-cosmo-c++
1 change: 1 addition & 0 deletions bin/aarch64-unknown-cosmo-cc
1 change: 1 addition & 0 deletions bin/aarch64-unknown-cosmo-g++
1 change: 1 addition & 0 deletions bin/aarch64-unknown-cosmo-gcc
13 changes: 13 additions & 0 deletions bin/aarch64-unknown-cosmo-ld
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/sh

MODE=${MODE:-${m:-aarch64}}
COSMO=${COSMO:-/opt/cosmo}
COSMOS=${COSMOS:-/opt/cosmos/aarch64}
TOOL="$COSMO/o/third_party/gcc/bin/aarch64-linux-musl-ld"

if [ ! -x "$TOOL" ]; then
echo "$0: you need to run: aarch64-unknown-cosmo-cc --update" >&2
exit 1
fi

exec "$TOOL" "$@"
13 changes: 13 additions & 0 deletions bin/aarch64-unknown-cosmo-objcopy
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/sh

MODE=${MODE:-${m:-aarch64}}
COSMO=${COSMO:-/opt/cosmo}
COSMOS=${COSMOS:-/opt/cosmos/aarch64}
TOOL="$COSMO/o/third_party/gcc/bin/aarch64-linux-musl-objcopy"

if [ ! -x "$TOOL" ]; then
echo "$0: you need to run: aarch64-unknown-cosmo-cc --update" >&2
exit 1
fi

exec "$TOOL" "$@"
13 changes: 13 additions & 0 deletions bin/aarch64-unknown-cosmo-objdump
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/sh

MODE=${MODE:-${m:-aarch64}}
COSMO=${COSMO:-/opt/cosmo}
COSMOS=${COSMOS:-/opt/cosmos/aarch64}
TOOL="$COSMO/o/third_party/gcc/bin/aarch64-linux-musl-objdump"

if [ ! -x "$TOOL" ]; then
echo "$0: you need to run: aarch64-unknown-cosmo-cc --update" >&2
exit 1
fi

exec "$TOOL" "$@"
13 changes: 13 additions & 0 deletions bin/aarch64-unknown-cosmo-strip
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/sh

MODE=${MODE:-${m:-aarch64}}
COSMO=${COSMO:-/opt/cosmo}
COSMOS=${COSMOS:-/opt/cosmos/aarch64}
TOOL="$COSMO/o/third_party/gcc/bin/aarch64-linux-musl-strip"

if [ ! -x "$TOOL" ]; then
echo "$0: you need to run: aarch64-unknown-cosmo-cc --update" >&2
exit 1
fi

exec "$TOOL" "$@"
95 changes: 95 additions & 0 deletions bin/apecopy
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#!/bin/sh

PROG=${0##*/}
COSMO=${COSMO:-/opt/cosmo}

if [ x"$1" = x"--version" ]; then
cat <<EOF
$PROG 0.1
Copyright (c) 2023 Justine Alexandra Roberts Tunney
Cosmopolitan Libc and LLVM libcxx/compiler-rt are subject to non-GPL
notice licenses, e.g. ISC, MIT, etc. Your compiled programs must embed
our copyright notices. This toolchain is configured to do so default.
Cosmopolitan comes with absolutely NO WARRANTY of any kind.
For more information, see the Cosmopolitan LICENSE files.
Copyright (C) 2019 Free Software Foundation, Inc.
This launches GNU GCC/Binutils subprocesses, which is free software; see
Cosmopolitan's third_party/gcc/ for source code and copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.
EOF
fi

if [ x"$1" = x"--help" ]; then
cat <<EOF
Usage: $PROG INPUT OUTPUT
Converts an executable linked by cosmocc to the Actually Portable
Executable file format. See $PROG --version for other details.
EOF
fi

fatal_error() {
echo "$PROG: fatal error: $1" >&2
echo "operation terminated." >&2
exit 1
}

CROSS=1
ARCH=${PROG%%-*} # split(prog, '-')[0]
if [ x"$ARCH" = x"$PROG" ]; then
ARCH=x86_64
CROSS=0
fi

if [ x"$ARCH" = x"x86_64" ]; then
MODE=${MODE:-$m}
else
MODE=${MODE:-${m:-$ARCH}}
fi

if [ $CROSS -eq 0 ]; then
COSMOS=${COSMOS:-/opt/cosmos}
else
COSMOS=${COSMOS:-/opt/cosmos/$ARCH}
fi

if [ x"$ARCH" = x"x86_64" ]; then
OBJCOPYFLAGS="-S -O binary"
elif [ x"$ARCH" = x"aarch64" ]; then
OBJCOPYFLAGS="-S"
else
fatal_error "$ARCH: unsupported architecture"
fi

if [ ! -d "$COSMO" ]; then
fatal_error "you need to clone cosmopolitan to your $COSMO directory"
fi

if [ ! -f "$COSMO/o//tool/build/zipcopy.com" ] ||
[ ! -f "$COSMO/o/third_party/gcc/bin/$ARCH-linux-musl-objcopy" ]; then
if [ $CROSS -eq 0 ]; then
fatal_error "you need to run: cosmocc --update"
else
fatal_error "you need to run: $ARCH-unknown-cosmo-cosmocc --update"
fi
fi

INPUT=$1
OUTPUT=$2
if [ ! -f "$INPUT" ]; then
fatal_error "$INPUT: missing input and output arguments"
elif [ ! -f "$INPUT" ]; then
fatal_error "$INPUT: no such file"
elif [ ! -f "$INPUT" ]; then
fatal_error "$INPUT: missing output argument"
elif [ x"$INPUT" = x"$OUTPUT" ]; then
fatal_error "$INPUT: input and output file can't be the same"
fi

"$COSMO/o/third_party/gcc/bin/$ARCH-linux-musl-objcopy" \
$OBJCOPYFLAGS \
"$INPUT" \
"$OUTPUT" || exit
"$COSMO/o//tool/build/zipcopy.com" \
"$INPUT" \
"$OUTPUT" || exit
Loading

0 comments on commit 4816e28

Please sign in to comment.