export PYVERSION ?= 3.12.1
export PYODIDE_EMSCRIPTEN_VERSION ?= 3.1.58
export PYODIDE_VERSION ?= 0.27.0.dev0
export PYODIDE_ABI_VERSION ?= 2024_0

export PYTHON_ARCHIVE_SHA256=d01ec6a33bc10009b09c17da95cc2759af5a580a7316b3a446eb4190e13f97b2

# Update the following variables when you want to update the version of the
# pyodide-build version used in the build process. If you want to make breaking
# changes in pyodide-build, it is also useful to change the repository URL
# to your fork to test the changes are working as expected.

# v0.27.3
export PYODIDE_BUILD_COMMIT=fac0109aa2acf14469320b049d710dd42639bf94
export PYODIDE_BUILD_REPO=https://github.com/pyodide/pyodide-build

ifdef CPYTHON_DEBUG
	export CPYTHON_ABI_FLAGS=d
endif

export PLATFORM_TRIPLET=wasm32-emscripten
export SYSCONFIG_NAME=_sysconfigdata_$(CPYTHON_ABI_FLAGS)_emscripten_$(PLATFORM_TRIPLET)

# BASH_ENV tells bash to run pyodide_env.sh on startup, which sets various
# environment variables. The next line instructs make to use bash to run each
# command.
export BASH_ENV := $(PYODIDE_ROOT)/pyodide_env.sh
SHELL := /usr/bin/env bash

version_tmp_1 := $(subst ., ,$(PYVERSION:v%=%))
# Handle alpha, beta, and release candidate versions
version_tmp_2 := $(subst a, ,$(version_tmp_1))
version_tmp_3 := $(subst b, ,$(version_tmp_2))
version_tuple := $(subst r, ,$(version_tmp_3))

export PYMAJOR=$(word 1,$(version_tuple))
export PYMINOR=$(word 2,$(version_tuple))
export PYMICRO=$(word 3,$(version_tuple))
export PYSTABLEVERSION=$(PYMAJOR).$(PYMINOR).$(PYMICRO)

export HOSTPYTHONROOT=$(shell python${PYMAJOR}.${PYMINOR} -c "import sys; print(sys.prefix)")
export HOSTPYTHON=$(HOSTPYTHONROOT)/bin/python$(PYMAJOR).$(PYMINOR)

export PYTHON_ARCHIVE_URL=https://www.python.org/ftp/python/$(PYSTABLEVERSION)/Python-$(PYVERSION).tgz

export CPYTHONROOT=$(PYODIDE_ROOT)/cpython
export CPYTHONINSTALL=$(CPYTHONROOT)/installs/python-$(PYVERSION)
export CPYTHONLIB=$(CPYTHONINSTALL)/lib/python$(PYMAJOR).$(PYMINOR)
export SYSCONFIGDATA_DIR=$(CPYTHONINSTALL)/sysconfigdata/
export CPYTHONBUILD=$(CPYTHONROOT)/build/Python-$(PYVERSION)/

export TARGETINSTALLDIR=$(PYODIDE_ROOT)/cpython/installs/python-$(PYVERSION)

export PYTHONINCLUDE=$(PYODIDE_ROOT)/cpython/installs/python-$(PYVERSION)/include/python$(PYMAJOR).$(PYMINOR)$(CPYTHON_ABI_FLAGS)

# Use env variable if defined, otherwise fallback to './'
export PYODIDE_BASE_URL?=./

# The compression level used for zip files and wheels. When distributing via a
# CDN it's more efficient to keep this value to 0, and let the CDN perform the
# Brotli compression.
export PYODIDE_ZIP_COMPRESSION_LEVEL?=6

export PIP_CONSTRAINT=$(PYODIDE_ROOT)/tools/constraints.txt

# List of modules to exclude from the zipped standard library
export PYZIP_EXCLUDE_FILES=\
	ensurepip/ \
	venv/ \
	lib2to3/ \
	_osx_support.py \
	_aix_support.py \
	curses/ \
	dbm/ \
	idlelib/ \
	tkinter/ \
	turtle.py \
	turtledemo/ \
	test/ \
	sqlite3/ \
	ssl.py \
	lzma.py \
	_pydecimal.py \
	pydoc_data/

# List of modules that we replace with a stub in the zipped standard library
export PYZIP_JS_STUBS=\
	webbrowser.py

export PYCOMPILE_EXCLUDE_FILES=\
	RobotRaconteur- \
	astropy- \
	opencv_python- \
	test-

export DBGFLAGS_NODEBUG=-g0
export DBGFLAGS_WASMDEBUG=-g2
export DBGFLAGS_SOURCEMAPDEBUG=-g3
export DBG_LDFLAGS_SOURCEMAPDEBUG=-gseparate-dwarf

export DBGFLAGS=$(DBGFLAGS_NODEBUG)

ifdef PYODIDE_DEBUG
	export PYODIDE_SYMBOLS=1
	export PYODIDE_DEBUG_JS=1
	export PYODIDE_ASSERTIONS=1
endif

ifdef PYODIDE_SOURCEMAP
	# Debug with source maps (less useful than WASMDEBUG but easier if it helps)
	export DBGFLAGS=$(DBGFLAGS_SOURCEMAPDEBUG)
	export DBG_LDFLAGS=$(DBG_LDFLAGS_SOURCEMAPDEBUG)
else
	ifdef PYODIDE_SYMBOLS
		# Include debug symbols but no source maps (most useful)
		export DBGFLAGS=$(DBGFLAGS_WASMDEBUG)
	endif
endif

ifdef PYODIDE_ASSERTIONS
	EXTRA_CFLAGS+= -DDEBUG_F
endif


export OPTFLAGS=-O2
export CFLAGS_BASE=\
	$(OPTFLAGS) \
	$(DBGFLAGS) \
	-fPIC \
	$(EXTRA_CFLAGS)


export LDFLAGS_BASE=\
	$(OPTFLAGS) \
	$(DBGFLAGS) \
	$(DBG_LDFLAGS) \
	-L$(CPYTHONROOT)/installs/python-$(PYVERSION)/lib/ \
	-s WASM_BIGINT \
	$(EXTRA_LDFLAGS)

export CXXFLAGS_BASE=

export MAIN_MODULE_LDFLAGS= $(LDFLAGS_BASE) \
	-s MAIN_MODULE=1 \
	-s MODULARIZE=1 \
	-s LZ4=1 \
	-s EXPORT_NAME="'_createPyodideModule'" \
	-s EXPORT_EXCEPTION_HANDLING_HELPERS \
	-s EXCEPTION_CATCHING_ALLOWED=['we only want to allow exception handling in side modules'] \
	-sEXPORTED_RUNTIME_METHODS='wasmTable,ERRNO_CODES' \
	-s USE_ZLIB \
	-s USE_BZIP2 \
	-s FORCE_FILESYSTEM=1 \
	-s TOTAL_MEMORY=20971520 \
	-s ALLOW_MEMORY_GROWTH=1 \
	-s EXPORT_ALL=1 \
	-s STACK_SIZE=5MB \
	-s AUTO_JS_LIBRARIES=0 \
	-s AUTO_NATIVE_LIBRARIES=0 \
	-s NODEJS_CATCH_EXIT=0 \
	-s NODEJS_CATCH_REJECTION=0 \
	-s EXPORTED_FUNCTIONS='$(EXPORTS)'\
	\
	-lpython$(PYMAJOR).$(PYMINOR)$(CPYTHON_ABI_FLAGS) \
	-lffi \
	-lstdc++ \
	-lidbfs.js \
	-lnodefs.js \
	-lproxyfs.js \
	-lworkerfs.js \
	-lwebsocket.js \
	-leventloop.js \
	-lhiwire \
	\
	-lGL \
	-legl.js \
	-lwebgl.js \
	-lhtml5.js \
	-lhtml5_webgl.js \
	-lsdl.js \
	-sGL_WORKAROUND_SAFARI_GETCONTEXT_BUG=0

EXPORTS=_main \
   ,_free \
   \
   ,_hiwire_new \
   ,_hiwire_intern \
   ,_hiwire_num_refs \
   ,_hiwire_get \
   ,_hiwire_incref \
   ,_hiwire_decref \
   ,_hiwire_pop \
   ,__hiwire_get \
   ,__hiwire_set \
   ,__hiwire_immortal_get \
   ,__hiwire_immortal_add \
   ,_jslib_init \
   ,_init_pyodide_proxy \
   \
   ,_PyBuffer_Release \
   ,_Py_DecRef \
   ,_PyDict_New \
   ,_PyDict_SetItem \
   ,__PyErr_CheckSignals \
   ,_PyErr_CheckSignals \
   ,_PyErr_Clear \
   ,_PyErr_Occurred \
   ,_PyErr_Print \
   ,_PyErr_SetRaisedException \
   ,_PyErr_SetString \
   ,_PyEval_SaveThread \
   ,_PyEval_RestoreThread \
   ,_PyFloat_FromDouble \
   ,_PyGILState_Check \
   ,_Py_IncRef \
   ,_PyList_New \
   ,_PyList_SetItem \
   ,__PyLong_FromByteArray \
   ,_PyLong_FromDouble \
   ,_PyMem_Free \
   ,_PyObject_GetAIter \
   ,_PyObject_GetIter \
   ,_PyObject_Size \
   ,_PyRun_SimpleString \
   ,_PySet_Add \
   ,_PySet_New \
   ,__PyTraceback_Add \
   ,_PyUnicode_Data \
   ,_PyUnicode_New \


ifeq ($(DISABLE_DYLINK), 1)
	export MAIN_MODULE_LDFLAGS= $(LDFLAGS_BASE) \
		-s MODULARIZE=1 \
		-s EXPORT_NAME="'_createPyodideModule'" \
		-sEXPORTED_RUNTIME_METHODS='wasmTable,ERRNO_CODES,FS,ENV,stringToNewUTF8,addFunction' \
		-s LZ4=1 \
		-s USE_ZLIB \
		-s USE_BZIP2 \
		-s FORCE_FILESYSTEM=1 \
		-s TOTAL_MEMORY=20971520 \
		-s ALLOW_MEMORY_GROWTH=1 \
		-s STACK_SIZE=5MB \
		-s ALLOW_TABLE_GROWTH \
		-sWASM_BIGINT \
		-sEXPORTED_FUNCTIONS='$(EXPORTS)'\
		-lpython$(PYMAJOR).$(PYMINOR)$(CPYTHON_ABI_FLAGS) \
		-lffi \
		-lhiwire \
		-lidbfs.js \
		-lnodefs.js \

endif

export MAIN_MODULE_CFLAGS= $(CFLAGS_BASE) \
	-Wall \
	-Wno-warn-absolute-paths \
	-Werror=unused-variable \
	-Werror=sometimes-uninitialized \
	-Werror=int-conversion \
	-Werror=incompatible-pointer-types \
	-Werror=unused-result \
	-mreference-types \
	-I$(PYTHONINCLUDE) \
	-I$(PYTHONINCLUDE)/.. \
	-s EXCEPTION_CATCHING_ALLOWED=['we only want to allow exception handling in side modules']

.output_vars:
	set