Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding LUA bindings #860

Open
wants to merge 37 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
49425c7
Lua bindings
subvertnormality Dec 17, 2024
cd1b3d3
One test now passes
subvertnormality Dec 17, 2024
156c086
Addressing more test errors
subvertnormality Dec 17, 2024
b606d17
More tests fixed
subvertnormality Dec 17, 2024
64337d3
Even more tests fixed
subvertnormality Dec 17, 2024
70473d8
Eliminated lua binding test errors, only failures remaining
subvertnormality Dec 17, 2024
6a2b585
More tests pass
subvertnormality Dec 17, 2024
1c162f3
More tests pass
subvertnormality Dec 18, 2024
1a8034b
More tests pass
subvertnormality Dec 18, 2024
98795e2
More tests for lua bindings passing
subvertnormality Dec 18, 2024
0f4ec8d
More tests for lua bindings passing
subvertnormality Dec 18, 2024
f0a2dcf
More tests for lua bindings passing
subvertnormality Dec 18, 2024
538928f
More tests for lua bindings passing
subvertnormality Dec 18, 2024
a7fb8e4
More tests for lua bindings passing
subvertnormality Dec 18, 2024
824d900
More tests for lua bindings passing
subvertnormality Dec 18, 2024
fec0669
Tests passing
subvertnormality Dec 18, 2024
60a9916
All lua bindings test now pass
subvertnormality Dec 18, 2024
2ec467e
Adding documentation
subvertnormality Dec 19, 2024
36a1999
Added build steps to the readme
subvertnormality Dec 19, 2024
665d344
Updating docs
subvertnormality Dec 19, 2024
3c615fc
Lua bindings now compile with 5.1
subvertnormality Dec 20, 2024
587e5e3
Now uses luajit correctly
subvertnormality Dec 20, 2024
1b2bac8
Adding find best move to lua bindings
subvertnormality Dec 21, 2024
18f9e7f
Updating tests
subvertnormality Jan 12, 2025
745fa5c
Keep GitHub Actions up to date with GitHub's Dependabot
cclauss Dec 11, 2024
2a72b29
Bump github-actions dependencies (#850)
dependabot[bot] Dec 27, 2024
c711d02
Upload artifacts for ffish.js builds in CI (#834)
yjf2002ghty Dec 29, 2024
cf24d55
Implement fog_fen() (#840)
gbtami Dec 29, 2024
21e29e1
Update variants.ini (+ Leaperhouse) (#836)
Snowmoondaphne Dec 29, 2024
0a034e7
Update workflow action version (#851)
yjf2002ghty Dec 30, 2024
eca5dbe
Handle invalid castling FEN
ianfab Dec 30, 2024
fd99d63
Fix pyffish License classifier
gbtami Dec 31, 2024
dc25dc6
Fix FEN parsing for S-Chess
ianfab Jan 9, 2025
dd7a887
Update cibuildwheel, bump pyffish version
gbtami Jan 9, 2025
318f21d
Merge branch 'fairy-stockfish:master' into master
subvertnormality Jan 12, 2025
8747056
Bringing makefile into alignment with origin
subvertnormality Jan 12, 2025
9567c91
Exclude lua bindings from default build
subvertnormality Jan 12, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[submodule "build/ThirdParty/LuaBridge3"]
path = build/ThirdParty/LuaBridge3
url = https://github.com/kunitoki/LuaBridge3.git
[submodule "ThirdParty/LuaBridge3"]
path = ThirdParty/LuaBridge3
url = https://github.com/kunitoki/LuaBridge3.git
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ Besides the C++ engine, this project also includes bindings for other programmin

The python binding [pyffish](https://pypi.org/project/pyffish/) contributed by [@gbtami](https://github.com/gbtami) is implemented in [pyffish.cpp](https://github.com/fairy-stockfish/Fairy-Stockfish/blob/master/src/pyffish.cpp). It is e.g. used in the backend for the [pychess server](https://github.com/gbtami/pychess-variants).

### Lua

The Lua bindings provide a powerful interface to the chess variant engine. They require Lua 5.1 for LuaJIT compatibility. See [tests/lua/README.md](tests/lua/README.md) for installation and usage instructions.

### Javascript

The javascript binding [ffish.js](https://www.npmjs.com/package/ffish) contributed by [@QueensGambit](https://github.com/QueensGambit) is implemented in [ffishjs.cpp](https://github.com/fairy-stockfish/Fairy-Stockfish/blob/master/src/ffishjs.cpp). The compilation/binding to javascript is done using emscripten, see the [readme](https://github.com/fairy-stockfish/Fairy-Stockfish/tree/master/tests/js).
Expand Down
1 change: 1 addition & 0 deletions ThirdParty/LuaBridge3
Submodule LuaBridge3 added at 67c109
4 changes: 4 additions & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ before_build:
'set(CMAKE_CXX_STANDARD_REQUIRED ON)',
'set (CMAKE_CXX_EXTENSIONS OFF)',
'set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/src)',
'option(BUILD_WITH_LUA "Build with Lua support" OFF)',
'set(source_files', $src, ')',
'if(NOT BUILD_WITH_LUA)',
' list(REMOVE_ITEM source_files "${CMAKE_SOURCE_DIR}/src/luabindings.cpp")',
'endif()',
'add_compile_definitions(NNUE_EMBEDDING_OFF)',
'add_executable(stockfish ${source_files})'

Expand Down
1 change: 1 addition & 0 deletions build/ThirdParty/LuaBridge3
Submodule LuaBridge3 added at 67c109
67 changes: 66 additions & 1 deletion src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ else
EXE = stockfish
endif

### Lua module name (if Lua is enabled)
ifeq ($(KERNEL),Darwin)
LUA_MODULE = fairystockfish.so
else
LUA_MODULE = fairystockfish.so
endif

### Installation dir definitions
PREFIX = /usr/local
BINDIR = $(PREFIX)/bin
Expand All @@ -45,6 +52,11 @@ SRCS = benchmark.cpp bitbase.cpp bitboard.cpp endgame.cpp evaluate.cpp main.cpp
partner.cpp parser.cpp piece.cpp variant.cpp xboard.cpp \
nnue/features/half_ka_v2_variants.cpp

# Add Lua bindings if enabled
ifeq ($(lua),yes)
SRCS += luabindings.cpp
endif

OBJS = $(notdir $(SRCS:.cpp=.o))

VPATH = syzygy:nnue:nnue/features
Expand Down Expand Up @@ -92,12 +104,47 @@ endif
# Example of use for these flags:
# make build ARCH=x86-64-avx512 debug=on sanitize="address undefined"

### Add Lua configuration
ifeq ($(lua),yes)
# Try to detect Lua using pkg-config first
LUA_VERSION = $(shell pkg-config --exists lua5.1 && echo 5.1 || (pkg-config --exists lua-5.1 && echo 5.1 || (pkg-config --exists luajit && echo 5.1)))

ifeq ($(LUA_VERSION),5.1)
LUA_CFLAGS = $(shell pkg-config --cflags luajit 2>/dev/null || pkg-config --cflags lua5.1 || pkg-config --cflags lua-5.1)
LUA_LDFLAGS = $(shell pkg-config --libs luajit 2>/dev/null || pkg-config --libs lua5.1 || pkg-config --libs lua-5.1)
else
# Fallback paths for common Lua/LuaJIT installations
ifeq ($(KERNEL),Darwin)
LUA_CFLAGS = -I/usr/local/include/luajit-2.1
LUA_LDFLAGS = -L/usr/local/lib -lluajit-5.1
else
LUA_CFLAGS = -I/usr/include/luajit-2.1
LUA_LDFLAGS = -lluajit-5.1
endif
endif

# Add LuaBridge3 include path
LUABRIDGE_PATH = ../ThirdParty/LuaBridge3/Source
LUABRIDGE_CFLAGS = -I$(LUABRIDGE_PATH)

# Add combined flags for shared library
ifeq ($(KERNEL),Darwin)
CXXFLAGS += $(LUA_CFLAGS) $(LUABRIDGE_CFLAGS) -DUSE_LUA -std=c++17 -fPIC
LDFLAGS += $(LUA_LDFLAGS)
LUA_MODULE_FLAGS = -dynamiclib -undefined dynamic_lookup
else
CXXFLAGS += $(LUA_CFLAGS) $(LUABRIDGE_CFLAGS) -DUSE_LUA -std=c++17 -fPIC
LDFLAGS += $(LUA_LDFLAGS)
LUA_MODULE_FLAGS = -shared
endif
endif

### 2.1. General and architecture defaults
largeboards = no
all = no
precomputedmagics = yes
nnue = no
lua = no
load_net = $(if $(filter $(nnue),yes),net)

ifeq ($(ARCH),)
Expand Down Expand Up @@ -718,6 +765,13 @@ help:
@echo "install > Install executable"
@echo "clean > Clean up"
@echo ""
@echo "Optional features:"
@echo ""
@echo "make build ARCH=x86-64 largeboards=yes > Version for large board variants"
@echo "make build ARCH=x86-64 all=yes > Include all variants"
@echo "make build ARCH=x86-64 nnue=yes > Enable NNUE"
@echo "make build ARCH=x86-64 lua=yes > Enable Lua bindings (requires Lua 5.1 for LuaJIT compatibility)"
@echo ""
@echo "Supported archs:"
@echo ""
@echo "x86-64-vnni512 > x86 64-bit with vnni support 512bit wide"
Expand Down Expand Up @@ -874,7 +928,7 @@ default:
### Section 5. Private Targets
### ==========================================================================

all: $(EXE) .depend
all: $(EXE) $(if $(filter $(lua),yes),$(LUA_MODULE)) .depend

config-sanity: $(load_net)
@echo ""
Expand Down Expand Up @@ -905,6 +959,7 @@ config-sanity: $(load_net)
@echo "all: '$(all)'"
@echo "precomputedmagics: '$(precomputedmagics)'"
@echo "nnue: '$(nnue)'"
@echo "lua: '$(lua)'"
@echo ""
@echo "Flags:"
@echo "CXX: $(CXX)"
Expand Down Expand Up @@ -939,6 +994,12 @@ config-sanity: $(load_net)
$(EXE): $(OBJS)
+$(CXX) -o $@ $(OBJS) $(LDFLAGS)

$(LUA_MODULE): luabindings.o bitboard.o position.o piece.o variant.o parser.o benchmark.o endgame.o evaluate.o \
material.o misc.o movegen.o movepick.o pawns.o psqt.o search.o thread.o timeman.o tt.o uci.o ucioption.o tune.o \
syzygy/tbprobe.o nnue/evaluate_nnue.o nnue/features/half_ka_v2.o nnue/features/half_ka_v2_variants.o \
bitbase.o partner.o xboard.o
$(CXX) $(LUA_MODULE_FLAGS) -o $@ $^ $(LDFLAGS)

clang-profile-make:
$(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
EXTRACXXFLAGS='-fprofile-instr-generate ' \
Expand Down Expand Up @@ -980,3 +1041,7 @@ icc-profile-use:
-@$(CXX) $(DEPENDFLAGS) -MM $(SRCS) > $@ 2> /dev/null

-include .depend

# Special case for Lua bindings to enable exceptions
luabindings.o: luabindings.cpp
$(CXX) $(CXXFLAGS:-fno-exceptions=) $(CPPFLAGS) $(LUA_CFLAGS) $(LUABRIDGE_CFLAGS) -c $< -o $@
Loading