Skip to content

Commit

Permalink
Enable building Synlig as python module
Browse files Browse the repository at this point in the history
  • Loading branch information
Szelwiga committed Sep 27, 2024
1 parent e5e2dab commit 4a54665
Show file tree
Hide file tree
Showing 6 changed files with 2,526 additions and 21 deletions.
11 changes: 10 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ endif
ifneq ($(filter plugin,${MAKECMDGOALS}),)
CFG_BUILD_TYPE := plugin
endif
ifneq ($(filter install@pysynlig build@pysynlig,${MAKECMDGOALS}),)
CFG_BUILD_TYPE := pysynlig
endif

#--------------------------------------------------------------------------------
# Load config.
Expand Down Expand Up @@ -94,7 +97,7 @@ override undefine command_line_illegal_vars
# Validate configuration variable values and assign them to variables without
# `CFG_` prefix used internally.

override ALL_ALLOWED_BUILD_TYPES := release asan plugin
override ALL_ALLOWED_BUILD_TYPES := release asan plugin pysynlig
$(if $(filter-out 1,$(words $(filter ${ALL_ALLOWED_BUILD_TYPES},${CFG_BUILD_TYPE}))),\
$(error CFG_BUILD_TYPE: invalid value (${CFG_BUILD_TYPE}). Must be one of: ${ALL_ALLOWED_BUILD_TYPES})\
)
Expand Down Expand Up @@ -330,6 +333,12 @@ build@asan : build
.PHONY: install@asan
install@asan : install

.PHONY: build@pysynlig
build@pysynlig : build

.PHONY: install@pysynlig
install@pysynlig : install

install@eqy: ${OUT_DIR}/bin/synlig-config ${TOP_DIR}/third_party/eqy/.git
install@sby: ${OUT_DIR}/bin/synlig-config ${TOP_DIR}/third_party/sby/.git
install@sv2v: ${TOP_DIR}/third_party/sv2v/.git
Expand Down
7 changes: 7 additions & 0 deletions buildconfig.mk
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ PREFIX ?= /usr/local
DESTDIR ?=
CFG_OUT_DIR := $(DESTDIR)$(PREFIX)/

# Install directory for python
PYTHON_PREFIX := $(shell /usr/bin/env python3 -c "import site; print(site.getsitepackages()[-1]);")

ifneq ($(PREFIX),/usr/local)
PYTHON_PREFIX := $(PREFIX)
endif

# Basic build tools
CC ?= cc
CXX ?= c++
Expand Down
7 changes: 3 additions & 4 deletions src/Build.synlig.mk
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ ${ts}.input_files = \

${ts}.make_args := \
PREFIX:=$(call ToAbsPaths,${PREFIX}) \
PYTHON_PREFIX:=$(call ToAbsPaths,${PYTHON_PREFIX}) \
DESTDIR:=$(if $(DESTDIR),$(call ToAbsPaths,$(DESTDIR)),) \
CONFIG:=$(if cxx_is_clang,clang,gcc) \
CC:=${CC} \
CXX:=${CXX} \
LD:=${CXX} \
$(if ${LD},LDFLAGS:=$(call ShQuote,${LDFLAGS} ${USE_LD_FLAG})) \
SYNLIG_BUILD_TYPE:=$(CFG_BUILD_TYPE)
SYNLIG_BUILD_TYPE:=$(BUILD_TYPE) \
SYNLIG_BUILD_DIR:=$(call ToAbsPaths,$(BUILD_DIR))

cxxflags := ${CXXFLAGS}

Expand All @@ -31,9 +33,6 @@ ${ts}.make_args += SANITIZER:=address
ifdef cxx_is_clang
cxxflags += -fsanitize-address-use-after-return=always
endif
endif

ifneq (${BUILD_TYPE},release)
${ts}.make_args += \
ENABLE_DEBUG:=1 \
STRIP:=/bin/true
Expand Down
92 changes: 76 additions & 16 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,8 @@ REPO_DIR := $(abspath $(THIS_MK_DIR)/..)
YOSYS_SRC := $(REPO_DIR)/third_party/yosys
SYNLIG_SRC := $(REPO_DIR)/src

ifeq ($(SYNLIG_BUILD_TYPE),asan)
BUILD_DIR := $(REPO_DIR)/build/asan/synlig
SURELOG_PKG_DIR := $(REPO_DIR)/build/asan/surelog
else
BUILD_DIR := $(REPO_DIR)/build/release/synlig
SURELOG_PKG_DIR := $(REPO_DIR)/build/release/surelog
endif

BUILD_DIR := $(SYNLIG_BUILD_DIR)synlig
SURELOG_PKG_DIR := $(SYNLIG_BUILD_DIR)surelog

# --- set compiler ---#
CONFIG := none
Expand Down Expand Up @@ -60,8 +54,13 @@ OBJS =
EXTRA_OBJS =
GENFILES =
EXTRA_TARGETS =
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
OUT_TARGETS = $(BUILD_DIR)/libsynlig.so $(BUILD_DIR)/python/__init__.py
endif
all: top-all

# --- setup linking surelog --- #
Expand Down Expand Up @@ -126,10 +125,45 @@ OBJS = kernel/version_$(GIT_REV).o
# --- set abc flags --- #
ABCMKARGS = CC="$(CXX)" CXX="$(CXX)" ABC_USE_LIBSTDCXX=1 ABC_USE_NAMESPACE=abc VERBOSE=$(Q)

# --- detect python --- #
PYTHON_EXECUTABLE := $(shell if python3 -c ""; then echo "python3"; else echo "python"; fi)
PYTHON_CONFIG := $(PYTHON_EXECUTABLE)-config
PYTHON_DESTDIR := $(shell $(PYTHON_EXECUTABLE) -c "import site; print(site.getsitepackages()[-1]);")
# --- 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
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))
endif

# --- gcc/clang --- #
LTOFLAGS := $(GCC_LTO)
Expand Down Expand Up @@ -193,6 +227,7 @@ CXXFLAGS := -Og -DDEBUG $(filter-out $(OPT_LEVEL),$(CXXFLAGS))
endif

# --- link abc --- #
ABC_ENV := CFLAGS=-fPIC
CXXFLAGS += -DYOSYS_ENABLE_ABC
CXXFLAGS += -DYOSYS_LINK_ABC
LIBS += -lpthread
Expand Down Expand Up @@ -220,6 +255,7 @@ ENABLE_VERIFIC_VHDL := 1
ENABLE_ZLIB := 1
DISABLE_SPAWN := 0
SMALL = 0
PYTHON_EXECUTABLE := /usr/bin/env python3

# --- prepare defines for adding include files --- #
define add_share_file
Expand Down Expand Up @@ -370,6 +406,25 @@ compile-only: $(YOSYS_OBJS) $(SYNLIG_OBJS) $(GENFILES) $(EXTRA_TARGETS)
@echo " Compile successful."
@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))
$(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
$(Q) mkdir -p $(dir $@)
$(P) $(CXX) -o $@ -c $(CPPFLAGS) $(CXXFLAGS) $(PYFLAG) -I ./ $<
endif

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

Expand All @@ -388,15 +443,15 @@ $(BUILD_DIR)/frontends/systemverilog/%.o: frontends/systemverilog/%.cc

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

$(BUILD_DIR)/%.pyh: %.h
$(Q) mkdir -p $(dir $@)
$(P) cat $< | grep -E -v "#[ ]*(include|error)" | $(CXX) $(CXXFLAGS) -x c++ -o $@ -E -P -
$(P) cat $< | grep -E -v "#[ ]*(include|error)" | $(CXX) $(CXXFLAGS) $(PYFLAG) -x c++ -o $@ -E -P -

$(BUILD_DIR)/%.o: %.cpp
$(Q) mkdir -p $(dir $@)
$(P) $(CXX) -o $@ -c $(CPPFLAGS) $(CXXFLAGS) $<
$(P) $(CXX) -o $@ -c $(CPPFLAGS) $(CXXFLAGS) $(PYFLAG) $<

VERSION_STR := Synlig (git sha1 $(GIT_REV), $(notdir $(CXX)) $(shell \
$(CXX) --version | tr ' ()' '\n' | grep '^[0-9]' | head -n1) $(filter -f% -m% -O% -DNDEBUG,$(CXXFLAGS)))
Expand All @@ -422,15 +477,20 @@ check-git-abc:

abc/libabc.a: | check-git-abc
$(P)
$(Q) mkdir -p abc && $(MAKE) -C abc -f "$(realpath $(YOSYS_SRC)/abc/Makefile)" ABCSRC="$(realpath $(YOSYS_SRC)/abc/)" $(S) $(ABCMKARGS) $(if $(filter %.a,$@),PROG="abc",PROG="abc") MSG_PREFIX="$(eval P_OFFSET = 10)$(call P_SHOW)$(eval P_OFFSET = 15) ABC: " $(if $(filter %.a,$@),libabc.a)
$(Q) mkdir -p abc && $(ABC_ENV) $(MAKE) -C abc -f "$(realpath $(YOSYS_SRC)/abc/Makefile)" ABCSRC="$(realpath $(YOSYS_SRC)/abc/)" $(S) $(ABCMKARGS) $(if $(filter %.a,$@),PROG="abc",PROG="abc") MSG_PREFIX="$(eval P_OFFSET = 10)$(call P_SHOW)$(eval P_OFFSET = 15) ABC: " $(if $(filter %.a,$@),libabc.a)

$(BUILD_DIR)/yosys-libabc.a: abc/libabc.a
$(P) cp $< $(BUILD_DIR)/yosys-libabc.a

install: $(TARGETS) $(EXTRA_TARGETS)
ifeq ($(SYNLIG_BUILD_TYPE),pysynlig)
mkdir -p $(DESTDIR)$(PYTHON_PREFIX)/PySynlig
cp $(OUT_TARGETS) $(DESTDIR)$(PYTHON_PREFIX)/PySynlig
else
mkdir -p $(DESTDIR)$(BINDIR)
cp $(OUT_TARGETS) $(DESTDIR)$(BINDIR)
$(STRIP) -S $(DESTDIR)$(BINDIR)/synlig
endif
mkdir -p $(DESTDIR)$(DATDIR)
cp -r $(BUILD_DIR)/share/. $(DESTDIR)$(DATDIR)/.

Expand Down
5 changes: 5 additions & 0 deletions src/python/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import os
import sys
sys.setdlopenflags(os.RTLD_NOW | os.RTLD_GLOBAL)

__all__ = ["libsynlig"]
Loading

0 comments on commit 4a54665

Please sign in to comment.