# Makefile for Smart WASM JSON Project

# 配置
EMSDK_PATH = E:/emsdk
EMCC = $(EMSDK_PATH)/upstream/emscripten/em++.bat
SOURCES = src/main.cpp src/math_utils.cpp src/smart_json_wrapper.cpp
INCLUDES = -I./src
OUTPUT = public/wasm/smart_math.js
FLAGS = -std=c++17 -O3

# WASM 编译选项
WASM_FLAGS = \
	-s WASM=1 \
	-s EXPORTED_FUNCTIONS='["_smart_process_json","_smart_get_function_list","_smart_get_function_info","_smart_test_match","_smart_get_version","_smart_free_string","_malloc","_free"]' \
	-s EXPORTED_RUNTIME_METHODS='["ccall","cwrap","UTF8ToString","stringToUTF8","lengthBytesUTF8"]' \
	-s MODULARIZE=1 \
	-s EXPORT_NAME='createSmartMathModule' \
	-s ALLOW_MEMORY_GROWTH=1 \
	-s ENVIRONMENT='web' \
	-s ASSERTIONS=1

# 默认目标
all: build

# 构建 WASM
build: $(SOURCES)
	@echo "Building Smart WASM JSON Module..."
	$(EMCC) $(SOURCES) $(INCLUDES) $(FLAGS) $(WASM_FLAGS) -o $(OUTPUT)
	@echo "Build complete!"

# 清理
clean:
	@echo "Cleaning build files..."
	rm -f public/wasm/*.js public/wasm/*.wasm build/*.exe
	@echo "Clean complete!"

# 运行本地测试
test: build
	@echo "Running local tests..."
	g++ -std=c++17 $(INCLUDES) $(SOURCES) tests/test_smart_json.cpp -o build/test_smart.exe
	./build/test_smart.exe

# 启动开发服务器
serve: build
	@echo "Starting development server..."
	cd public && python -m http.server 8080

# 安装依赖
install:
	@echo "Please install:"
	@echo "1. Emscripten SDK"
	@echo "2. Python 3.7+"
	@echo "3. Node.js (optional)"

.PHONY: all build clean test serve install