Hi,

 

As you may know, macOS can build a single file universal binary that is able
to run on different processors.

By default, it selects the CPU associated to the running machine but, with
standard arch command, you can select another CPU (like x86_64 when you're
running on an Apple Silicon - M1, M2...)

 

I would like to push this script named tcc-universal.sh that builds a
universal binary.

The main benefit is that it allows you to generate x86_64 from a Silicon
machine.

Other benefit is that the same tcc file can run on different architectures.

 

Running this script gives:

jullien@mobley:~/tinycc $ ../tcc-universal.sh

Building universal binary for x86_64 processor

./configure --cpu=x86_64 --config-codesign

Building universal binary for arm64 processor

./configure --cpu=arm64 --config-codesign

 

tcc universal binary has been built for x86_64 arm64

On Silicon CPU use:

  $ arch -x86_64 tcc ...   ## to generate code for x86_64

  $ arch -arm64  tcc ...   ## to generate code for arm64

  $ tcc ...                ## to generate code for default processor

 

Without arch prefix, it selects the CPU you're running on

tcc version 0.9.27 mob:676755f 2022-12-26T01:48:54+01:00 (x86_64 Darwin)

tcc version 0.9.27 mob:676755f 2022-12-26T01:48:54+01:00 (AArch64 Darwin)

 

Here is the proposed script, is it Ok to push?

 

#! /bin/sh

 

set -e

 

if test "`uname`-`uname -m`" != "Darwin-arm64"

then

  echo Making a universal binary is macOS feature only available on Silicon
processor>&2

  exit 1

fi

 

ARGS=$*

 

build() {

  echo Building universal binary for $1 processor

  make clean 2>&1

  echo ./configure --cpu=$1 --config-codesign $ARGS

  ./configure --cpu=$1 --config-codesign $ARGS > tcc-universal.log

 

  make -j $(sysctl -n hw.ncpu) >> tcc-universal.log 2>&1

  if test $? != 0

  then

    cat tcc-universal.log

    exit $?

  fi

  mv tcc tcc-universal-$1

}

 

build x86_64

build arm64

 

# Create universal build.

lipo -create -output tcc tcc-universal-x86_64 tcc-universal-arm64

rm -rf tcc-universal-x86_64 tcc-universal-arm64 tcc-universal.log

# verify

echo

echo tcc universal binary has been built for `lipo -archs tcc` CPU

echo "On Silicon CPU use:"

echo "  $ arch -x86_64 tcc ...   ## to generate code for x86_64"

echo "  $ arch -arm64  tcc ...   ## to generate code for arm64"

echo "  $ tcc ...                ## to generate code for default processor"

echo

echo "Without arch prefix, it selects the CPU you're running on"

 

arch -x86_64 ./tcc -v

arch -arm64 ./tcc -v

 

C.

_______________________________________________
Tinycc-devel mailing list
Tinycc-devel@nongnu.org
https://lists.nongnu.org/mailman/listinfo/tinycc-devel

Reply via email to