Commit d88a6ae8 authored by Xavier Thompson's avatar Xavier Thompson

Improve Makefile and update README.md

parent b9e7d56f
...@@ -45,42 +45,36 @@ $ cd cython/ ...@@ -45,42 +45,36 @@ $ cd cython/
To build: To build:
``` ```
$ python3 setup.py build_ext --inplace $ make
``` ```
or for convenience a Makefile recipe is provided to do the same: Or equivalently: `python3 setup.py build_ext --inplace`
```
$ make python
```
To run: To run:
``` ```
$ make pythonrun $ make run
``` ```
### Building without the Python runtime ### Building without the Python runtime
The Cython compiler systematically links compiled programs against the python runtime, however the program actually never calls into the Python runtime. The Cython compiler systematically links compiled programs against the python runtime, however this program never actually calls into the Python runtime.
There currently is no support in the Cython+ language to express independance from the Python runtime and generate code which does not need to include the Python headers. So in the meantime we introduce a hack: we only use the cython compiler to generate a C++ file which we then compile and link manually, but instead of providing the python runtime to link against we tell the linker to ignore linking errors. We also provide a `main` function as an entry point to bypass the normal entry point for the python interpreter. This only works because the program happens not to require the python runtime for anything, but the Cython+ compiler does not check for this (yet).
To build: There is no support yet in the Cython+ language to express independance from the Python runtime and generate code which does not need to include the Python headers. However in the meantime we introduce a hack: we only use the cython compiler to generate a C++ file which we then compile and link manually, but instead of providing the python runtime to link against we tell the linker to ignore linking errors. We also provide a `main` function as an entry point to bypass the entry point for the python interpreter.
``` This hack can only work because this program happens not to require the python runtime for anything.
$ make No guarantees whatsoever are made that the program produced will behave as expected.
``` It has been tested and shown to work with g++.
or equivalently To build:
``` ```
$ make cython $ make nopython
``` ```
To run: To run:
``` ```
$ make run $ make runnopython
``` ```
ifeq (,) ifeq (,)
INCLUDE_PATHS = -I/usr/include/python3.8 INCLUDE_DIRS = -I/usr/include/python3.8
LIBRARIES = -lcrypto -lfmt
else else
...@@ -17,43 +17,58 @@ INCLUDE_FMTLIB = -I$(FMTLIB_PATH)/include ...@@ -17,43 +17,58 @@ INCLUDE_FMTLIB = -I$(FMTLIB_PATH)/include
LIBRARY_FMTLIB = -L$(FMTLIB_PATH)/lib LIBRARY_FMTLIB = -L$(FMTLIB_PATH)/lib
RUNPATH_FMTLIB = -Wl,-rpath=$(FMTLIB_PATH)/lib RUNPATH_FMTLIB = -Wl,-rpath=$(FMTLIB_PATH)/lib
INCLUDE_PATHS = $(INCLUDE_PYTHON) $(INCLUDE_OPENSSL) $(INCLUDE_FMTLIB) INCLUDE_DIRS = $(INCLUDE_PYTHON) $(INCLUDE_OPENSSL) $(INCLUDE_FMTLIB)
LIBRARY_PATHS = $(LIBRARY_OPENSSL) $(LIBRARY_FMTLIB) LIBPATHS = $(LIBRARY_OPENSSL) $(LIBRARY_FMTLIB)
RUNPATHS = $(RUNPATH_OPENSSL) $(RUNPATH_FMTLIB) RUNPATHS = $(RUNPATH_OPENSSL) $(RUNPATH_FMTLIB)
LIBRARIES = $(LIBRARY_PATHS) -lcrypto -lfmt $(RUNPATHS)
LDFLAGS = $(LIBPATHS) $(RUNPATHS)
endif endif
CC_FLAGS = -O2 -g -Wno-unused-result -Wsign-compare EXE = main
CXX = g++
CPPFLAGS = -O2 -g -Wno-unused-result -Wsign-compare -pthread $(INCLUDE_DIRS)
LDFLAGS += -Wl,--unresolved-symbols=ignore-all
LDLIBS = -lcrypto -lfmt
EXT_SUFFIX := $(shell python3 -c "import sysconfig; print(sysconfig.get_config_var('EXT_SUFFIX'))")
EXT = $(EXE)$(EXT_SUFFIX)
# Build with Python runtime
all: $(EXT)
cython: main.exe $(EXT): setup.py
@echo "[Cython Compiling $^ -> $@]"
python3 setup.py build_ext --inplace
# Run with Python runtime
run: $(EXT)
python3 -c "import $(EXE); $(EXE).python_main()" 2>/dev/null
run: main.exe # Build without Python runtime
./main.exe 2>/dev/null nopython: $(EXE)
%.cpp: %.pyx %.cpp: %.pyx
@echo "[Cython Compiling $^ -> $@]" @echo "[Cython Compiling $^ -> $@]"
-@python3 -c "from Cython.Compiler.Main import main; main(command_line=1)" $^ --cplus -3 python3 -c "from Cython.Compiler.Main import main; main(command_line=1)" $^ --cplus -3
@rm -f $(subst .cpp,.h,$@)
%.exe: %.cpp %: %.cpp
@echo "[C++ Compiling $^ -> $@]" @echo "[C++ Compiling $^ -> $@]"
g++ $^ $(CC_FLAGS) -pthread $(INCLUDE_PATHS) $(LIBRARIES) -Wl,--unresolved-symbols=ignore-all -o $@ $(LINK.cpp) $^ $(LOADLIBES) $(LDLIBS) -o $@
python: setup.py
-python3 setup.py build_ext --inplace
pythonrun: # Run without Python runtime
python3 -c "import main; main.python_main()" 2>/dev/null runnopython: $(EXE)
./$(EXE) 2>/dev/null
clean: clean:
rm -f *.c *.cpp *.html -rm -f *.c *.cpp *.html
rm -f *.h -rm -f *.h
rm -f *.so -rm -f *.so
rm -f *.exe -rm -f $(EXE)
rm -f *.o -rm -f *.o
rm -f -r build -rm -f -r build
rm -f *.json -rm -f *.json
.PHONY: cython run python pythonrun clean .PHONY: all run nopython runnopython clean
.PRECIOUS: %.cpp .PRECIOUS: %.cpp
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment