Skip to content

Commit

Permalink
Build PySynlig with pybind11
Browse files Browse the repository at this point in the history
  • Loading branch information
Szelwiga committed Sep 27, 2024
1 parent 732ed30 commit cb88b28
Show file tree
Hide file tree
Showing 11 changed files with 121 additions and 2,486 deletions.
16 changes: 8 additions & 8 deletions .ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ check_format:

variables:
BUILD_DEPENDENCIES: >
tclsh g++-11 default-jre swig clang gcc-11 git
python3-dev python3-pip tcl-dev cmake uuid ant
pkg-config libreadline-dev bison python3 flex
build-essential libboost-python-dev libfl-dev
google-perftools libgoogle-perftools-dev wget
libffi-dev python3-orderedmultidict uuid-dev
libboost-filesystem-dev libboost-system-dev
google-perftools libgoogle-perftools-dev
libffi-dev python3-orderedmultidict git
tclsh ant default-jre swig clang gcc-11
pkg-config libreadline-dev bison g++-11
python3 python3-dev python3-pip uuid
uuid-dev tcl-dev flex libfl-dev wget
build-essential cmake
.job_template: &build_binary
stage: "Build binaries"
Expand Down Expand Up @@ -86,7 +86,7 @@ build_pysynlig:
- echo "##/ Install dependencies and build \##"
- apt update && apt install -y $BUILD_DEPENDENCIES
- echo "##/ Build yosys, surelog and synlig binaries \##"
- git submodule sync && git submodule update --init --recursive third_party/{surelog,yosys}
- git submodule sync && git submodule update --init --recursive third_party/{surelog,yosys,pybind11}
- make install@surelog install@pysynlig -j $(nproc) PREFIX=out

build_package:
Expand Down
9 changes: 7 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,30 +74,35 @@ jobs:
cc: 'gcc'
cxx: 'g++'
build-binaries-args: 'install install@surelog PREFIX=out'
submodules: 'third_party/yosys third_party/surelog'
- name: 'Build Plugin'
artifact-name: 'binaries-plugin'
cc: 'gcc'
cxx: 'g++'
apt-extra-deps: 'gcc g++'
build-binaries-args: 'install-plugin install@surelog PREFIX=out'
submodules: 'third_party/yosys third_party/surelog'
- name: 'Build Synlig (ASAN)'
artifact-name: 'binaries-asan'
cc: 'clang-15'
cxx: 'clang++-15'
apt-extra-deps: 'clang-15'
build-binaries-args: 'install@surelog install@asan PREFIX=out'
submodules: 'third_party/yosys third_party/surelog'
- name: 'Build Package'
artifact-name: 'binaries-package'
apt-extra-deps: 'gcc g++'
cc: 'gcc'
cxx: 'g++'
build-binaries-args: 'install PREFIX=out'
submodules: 'third_party/yosys third_party/surelog'
- name: 'Build PySynlig'
artifact-name: 'binaries-pysynlig'
apt-extra-deps: 'gcc g++ libboost-system-dev libboost-filesystem-dev libboost-python-dev'
apt-extra-deps: 'gcc g++'
cc: 'gcc'
cxx: 'g++'
build-binaries-args: 'install@surelog install@pysynlig PREFIX=out'
submodules: 'third_party/yosys third_party/surelog third_party/pybind11'
name: ${{ matrix.name }}
runs-on: [self-hosted, Linux, X64, gcp-custom-runners]
container: debian:bookworm
Expand Down Expand Up @@ -155,7 +160,7 @@ jobs:
run: |
git submodule sync
git submodule update --depth 1 --init --recursive --checkout \
third_party/{surelog,yosys} \
${{ matrix.submodules }} \
;
- name: Setup cache
Expand Down
4 changes: 4 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,7 @@
path = third_party/eqy
url = https://github.com/YosysHQ/eqy
update = none
[submodule "third_party/pybind11"]
path = third_party/pybind11
url = https://github.com/pybind/pybind11
branch = stable
59 changes: 13 additions & 46 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ REPO_DIR := $(abspath $(THIS_MK_DIR)/..)

YOSYS_SRC := $(REPO_DIR)/third_party/yosys
SYNLIG_SRC := $(REPO_DIR)/src
PYBIND_SRC := $(REPO_DIR)/third_party/pybind11

BUILD_DIR := $(SYNLIG_BUILD_DIR)synlig
SURELOG_PKG_DIR := $(SYNLIG_BUILD_DIR)surelog
Expand All @@ -17,7 +18,7 @@ CONFIG := none
# CONFIG := gcc

# --- add synlig cxxflags --- #
CXXFLAGS += -DSYNLIG_STANDALONE_BINARY -I $(SYNLIG_SRC)/frontends/*/ -I $(SYNLIG_SRC)/mods/*
CXXFLAGS += -DSYNLIG_STANDALONE_BINARY -I $(SYNLIG_SRC)/frontends/*/ -I $(SYNLIG_SRC)/mods/* -I $(SYNLIG_SRC)/utils

# --- add static option --- #
CXXFLAGS += -static
Expand Down Expand Up @@ -58,7 +59,7 @@ ifneq ($(SYNLIG_BUILD_TYPE),pysynlig)
TARGETS = $(BUILD_DIR)/synlig $(BUILD_DIR)/synlig-config
OUT_TARGETS = $(BUILD_DIR)/synlig $(BUILD_DIR)/synlig-config
else
TARGETS = $(BUILD_DIR)/libsynlig.so
TARGETS = $(BUILD_DIR)/libsynlig.so $(BUILD_DIR)/python/__init__.py
OUT_TARGETS = $(BUILD_DIR)/libsynlig.so $(BUILD_DIR)/python/__init__.py
endif
all: top-all
Expand Down Expand Up @@ -127,42 +128,12 @@ ABCMKARGS = CC="$(CXX)" CXX="$(CXX)" ABC_USE_LIBSTDCXX=1 ABC_USE_NAMESPACE=abc V

# --- setup python --- #
ifeq ($(SYNLIG_BUILD_TYPE),pysynlig)
PYTHON_VERSION_TESTCODE := "import sys;t='{v[0]}.{v[1]}'.format(v=list(sys.version_info[:2]));print(t)"
PYTHON_VERSION := $(shell /usr/bin/env python3 -c ""$(PYTHON_VERSION_TESTCODE)"")
PYTHON_MAJOR_VERSION := $(shell echo $(PYTHON_VERSION) | cut -f1 -d.)

ENABLE_PYTHON_CONFIG_EMBED ?= $(shell /usr/bin/env python3-config --embed --libs > /dev/null && echo 1)
ifeq ($(ENABLE_PYTHON_CONFIG_EMBED),1)
PYTHON_CONFIG := /usr/bin/env python3-config --embed
else
PYTHON_CONFIG := /usr/bin/env python3-config
endif
endif

ifeq ($(SYNLIG_BUILD_TYPE),pysynlig)
# Detect name of boost_python library. Some distros use boost_python-py<version>, other boost_python<version>, some only use the major version number, some a concatenation of major and minor version numbers
CHECK_BOOST_PYTHON = (echo "int main(int argc, char ** argv) {return 0;}" | $(CXX) -xc -o /dev/null $(shell /usr/bin/env python3-config --ldflags) -l$(1) - > /dev/null 2>&1 && echo "-l$(1)")
BOOST_PYTHON_LIB ?= $(shell \
$(call CHECK_BOOST_PYTHON,boost_python-py$(subst .,,$(PYTHON_VERSION))) || \
$(call CHECK_BOOST_PYTHON,boost_python-py$(PYTHON_MAJOR_VERSION)) || \
$(call CHECK_BOOST_PYTHON,boost_python$(subst .,,$(PYTHON_VERSION))) || \
$(call CHECK_BOOST_PYTHON,boost_python$(PYTHON_MAJOR_VERSION)) \
)

ifeq ($(BOOST_PYTHON_LIB),)
$(error BOOST_PYTHON_LIB could not be detected. Please define manually)
endif

LIBS += $(shell /usr/bin/env python3-config --libs) $(BOOST_PYTHON_LIB) -lboost_system -lboost_filesystem
# python-config --ldflags includes LIBS for some reason
LIBS += $(shell /usr/bin/env python3-config --libs)
LINKFLAGS += $(filter-out -l%,$(shell /usr/bin/env python3-config --ldflags))
CXXFLAGS += $(shell /usr/bin/env python3-config --includes)
PYFLAG := -DWITH_PYTHON
PY_WRAPPER_FILE = kernel/python_wrappers
OBJS += $(PY_WRAPPER_FILE).o
PY_GEN_SCRIPT= py_wrap_generator
PY_WRAP_INCLUDES := $(shell cd $(SYNLIG_SRC) && python$(PYTHON_VERSION) -c "from python import $(PY_GEN_SCRIPT); $(PY_GEN_SCRIPT).print_includes()")
PY_WRAP_INCLUDES := $(addprefix $(BUILD_DIR)/, $(PY_WRAP_INCLUDES))
SYNLIG_OBJS += python/wrapper.o
CXXFLAGS += $(shell /usr/bin/env python3-config --includes)
CXXFLAGS += -I $(PYBIND_SRC)/include
endif

# --- gcc/clang --- #
Expand Down Expand Up @@ -371,7 +342,8 @@ include $(YOSYS_SRC)/backends/aiger/Makefile.inc
include $(YOSYS_SRC)/backends/intersynth/Makefile.inc
include $(YOSYS_SRC)/backends/spice/Makefile.inc

SYNLIG_OBJS += main.o
SYNLIG_OBJS += utils/main.o
SYNLIG_OBJS += utils/setup.o
SYNLIG_OBJS += yosys-libabc.a

include $(SYNLIG_SRC)/frontends/*/Makefile.inc
Expand Down Expand Up @@ -407,26 +379,21 @@ compile-only: $(YOSYS_OBJS) $(SYNLIG_OBJS) $(GENFILES) $(EXTRA_TARGETS)
@echo ""

ifeq ($(SYNLIG_BUILD_TYPE),pysynlig)

$(BUILD_DIR)/$(PY_WRAPPER_FILE).cc: python/$(PY_GEN_SCRIPT).py $(PY_WRAP_INCLUDES)
$(Q) cp -r $(SYNLIG_SRC)/python .
$(Q) mkdir -p $(dir $@)
$(P) python$(PYTHON_VERSION) -c "from python import $(PY_GEN_SCRIPT); $(PY_GEN_SCRIPT).gen_wrappers(\"$(PY_WRAPPER_FILE).cc\")"

$(BUILD_DIR)/libsynlig.so: $(filter-out main.o,$(YOSYS_OBJS) $(SYNLIG_OBJS))
$(BUILD_DIR)/libsynlig.so: $(filter-out utils/main.o,$(YOSYS_OBJS) $(SYNLIG_OBJS))
$(P) $(CXX) -o libsynlig.so -shared -Wl,-soname,libsynlig.so $(LINKFLAGS) $^ $(LIBS) $(SURELOG_LDFLAGS) $(SURELOG_LDLIBS)

$(BUILD_DIR)/kernel/yosys.o: kernel/yosys.cc
$(Q) mkdir -p $(dir $@)
$(P) $(CXX) -o $@ -c $(CPPFLAGS) $(CXXFLAGS) -I ./ $<

$(BUILD_DIR)/$(PY_WRAPPER_FILE).o: $(BUILD_DIR)/$(PY_WRAPPER_FILE).cc
$(BUILD_DIR)/python/%.py: $(SYNLIG_SRC)/python/%.py
$(Q) mkdir -p $(dir $@)
$(P) $(CXX) -o $@ -c $(CPPFLAGS) $(CXXFLAGS) $(PYFLAG) -I ./ $<
$(Q) cp $< $@
endif

$(BUILD_DIR)/kernel/log.o: CXXFLAGS += -DYOSYS_SRC='"$(YOSYS_SRC)"'
$(BUILD_DIR)/kernel/yosys.o: CXXFLAGS += -DYOSYS_DATDIR='"$(DATDIR)"' -DYOSYS_PROGRAM_PREFIX='""'
$(BUILD_DIR)/utils/setup.o: CXXFLAGS += -DSYNLIG_DATDIR='"$(DATDIR)"'

$(BUILD_DIR)/synlig: $(YOSYS_OBJS) $(SYNLIG_OBJS)
$(CXX) -o synlig $(LINKFLAGS) $(YOSYS_OBJS) $(SYNLIG_OBJS) $(LIBS) $(SURELOG_LDFLAGS) $(SURELOG_LDLIBS)
Expand Down
Loading

0 comments on commit cb88b28

Please sign in to comment.