Skip to content

Instantly share code, notes, and snippets.

@anderseknert
Created August 25, 2019 07:51
Show Gist options
  • Save anderseknert/e1fd91598325600455e0fd46d3a79e58 to your computer and use it in GitHub Desktop.
Save anderseknert/e1fd91598325600455e0fd46d3a79e58 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# Example of cython --embed producing a .c file for compilation with
# all dependencies statically linked. This to create a Python standalone
# application. Tested on Ubuntu 16.04. This was done as a learning exercise
# only - for real use see https://pyinstaller.readthedocs.io/
OS_ARCH=`echo $(uname -sm) | tr '[:upper:]' '[:lower:]' | tr ' ' '-'`
# ..or take from args
executable_name="application"
mkdir -p build/tmp
mkdir -p target/"$OS_ARCH"
# Can only be used with single .py file - investigate options like:
# 1. https://github.com/mwilliamson/stickytape
# 2. https://github.com/cython/cython/tree/master/Demos/freeze
cython "$executable_name".py -3 --embed -o build/tmp/
# Only replaced -dynamic with -static as we're going for a statically linked executable
cflags=$(python3-config --ldflags --cflags)
cflags_static=${cflags/-dynamic/-static}
if [[ $cflags_static != *"-static"* ]]; then
cflags_static="$cflags_static -static"
echo "$cflags_static"
fi
gcc -c build/tmp/"$executable_name".c -o build/tmp/"$executable_name".o $cflags_static -static -fPIC
gcc build/tmp/"$executable_name".o -o target/"$OS_ARCH"/"$executable_name" \
-L/usr/lib/python3.6/config-3.6m-x86_64-linux-gnu \
-L/usr/lib \
-lpython3.6m \
-ldl \
-lutil \
-lexpat \
-lz \
-lm \
-pthread \
-static
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment