Skip to content

Commit

Permalink
build: make flatter static
Browse files Browse the repository at this point in the history
  • Loading branch information
rot256 committed Feb 16, 2025
1 parent a63fc1b commit e1b446d
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 55 deletions.
15 changes: 13 additions & 2 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ jobs:
sudo apt update
sudo apt install -y \
build-essential wget m4 xz-utils \
make cmake libeigen3-dev \
liblapack-dev libopenblas-dev
make cmake libeigen3-dev
- name: Build wheel
run: |
Expand All @@ -39,6 +38,12 @@ jobs:
name: wheel-${{ matrix.arch }}-linux
path: dist/*.whl

- name: Upload flatter binary
uses: actions/upload-artifact@v4
with:
name: flatter-${{ matrix.arch }}-linux
path: flatter-linux

build-darwin:
runs-on: macos-latest
strategy:
Expand Down Expand Up @@ -66,6 +71,12 @@ jobs:
name: wheel-${{ matrix.arch }}-darwin
path: dist/*.whl

- name: Upload flatter binary
uses: actions/upload-artifact@v4
with:
name: flatter-${{ matrix.arch }}-darwin
path: flatter-darwin

test-darwin:
name: Fresh Install on macOS
runs-on: macos-latest
Expand Down
124 changes: 88 additions & 36 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ $(LIBS)/omp:
-DOPENMP_ENABLE_LIBOMPTARGET=OFF \
-DCMAKE_INSTALL_PREFIX=$(LIBS_PATH)/omp \
-DCMAKE_BINARY_DIR=$(LIBS_PATH)/omp \
-DCMAKE_POSITION_INDEPENDENT_CODE=ON \
-DCMAKE_CXX_FLAGS="-fPIC" \
-DCMAKE_C_FLAGS="-fPIC" \
&& make -j \
&& make install
rm -rf _omp_build
Expand All @@ -29,6 +32,8 @@ $(LIBS)/gmp:
mkdir -p $(LIBS_PATH)
cd gmp-6.3.0 \
&& ./configure --enable-static --disable-shared --prefix $(LIBS_PATH)/gmp \
CFLAGS="-fPIC" \
ABI=64 \
&& make -j \
&& make -j check \
&& make install
Expand All @@ -39,7 +44,7 @@ $(LIBS)/mpfr: $(LIBS)/gmp
tar -xf deps/mpfr-4.2.1.tar.gz
mkdir -p $(LIBS_PATH)
cd mpfr-4.2.1 \
&& ./configure --enable-static --disable-shared --with-gmp=$(LIBS_PATH)/gmp --prefix $(LIBS_PATH)/mpfr \
&& ./configure --enable-static --disable-shared --with-gmp=$(LIBS_PATH)/gmp --prefix $(LIBS_PATH)/mpfr CFLAGS="-fPIC" \
&& make -j \
&& make -j check \
&& make install
Expand All @@ -50,8 +55,8 @@ $(LIBS)/fplll: $(LIBS)/gmp $(LIBS)/mpfr
tar -xf deps/fplll-5.5.0.tar.gz
mkdir -p $(LIBS_PATH)
cd fplll-5.5.0 \
&& CXXFLAGS="-w -Wno-overloaded-virtual" ./configure --enable-static --disable-shared --with-gmp=$(LIBS_PATH)/gmp --with-mpfr=$(LIBS_PATH)/mpfr --prefix $(LIBS_PATH)/fplll \
&& make -j CXXFLAGS="-w -Wno-overloaded-virtual" \
&& CXXFLAGS="-w -Wno-overloaded-virtual -fPIC" ./configure --enable-static --disable-shared --with-gmp=$(LIBS_PATH)/gmp --with-mpfr=$(LIBS_PATH)/mpfr --prefix $(LIBS_PATH)/fplll \
&& make -j CXXFLAGS="-w -Wno-overloaded-virtual -static -fPIC" \
&& make -j check \
&& make install
rm -rf fplll-5.5.0
Expand All @@ -61,63 +66,110 @@ $(LIBS)/openblas:
tar -xf deps/OpenBLAS-0.3.29.tar.gz
mkdir -p $(LIBS_PATH)
cd OpenBLAS-0.3.29 \
&& make DYNAMIC_ARCH=1 NO_SHARED=1 USE_THREAD=1 PREFIX=$(LIBS_PATH)/openblas -j \
&& make install PREFIX=$(LIBS_PATH)/openblas
&& make DYNAMIC_ARCH=1 USE_THREAD=1 NO_SHARED=1 FC=gfortran BINARY=64 PREFIX=$(LIBS_PATH)/openblas -j \
&& make PREFIX=$(LIBS_PATH)/openblas install
rm -rf OpenBLAS-0.3.29

flatter-darwin libflatter.dylib: $(LIBS)/fplll $(LIBS)/gmp $(LIBS)/mpfr $(LIBS)/omp
flatter-darwin: $(LIBS)/fplll $(LIBS)/gmp $(LIBS)/mpfr $(LIBS)/omp
# untar the flatter source code
rm -rf flatter
mkdir flatter
tar -xf deps/flatter.tar.gz --strip-components=1 -C flatter

# build the flatter library
cd flatter \
&& mkdir build \
&& cd build \
&& CMAKE_INCLUDE_PATH=$(LIBS_PATH)/gmp/include:$(LIBS_PATH)/mpfr/include:$(LIBS_PATH)/fplll/include:$(LIBS_PATH)/omp/include \
CMAKE_LIBRARY_PATH=$(LIBS_PATH)/gmp/lib:$(LIBS_PATH)/mpfr/lib:$(LIBS_PATH)/fplll/lib:$(LIBS_PATH)/omp/lib \
cmake .. \
-DCMAKE_CXX_COMPILER=/usr/bin/clang++ \
-DOpenMP_CXX_FLAGS="-Xpreprocessor -fopenmp" \
-DOpenMP_CXX_LIB_NAMES="omp" \
-DOpenMP_omp_LIBRARY="$(LIBS_PATH)/omp/lib/libomp.a" \
-DCMAKE_PREFIX_PATH=$(LIBS_PATH)/gmp:$(LIBS_PATH)/mpfr:$(LIBS_PATH)/fplll:$(LIBS_PATH)/omp \
-DCMAKE_CXX_FLAGS="-I$(LIBS_PATH)/gmp/include -I$(LIBS_PATH)/mpfr/include -I$(LIBS_PATH)/fplll/include -I$(LIBS_PATH)/omp/include -Wno-overloaded-virtual -Wno-error=overloaded-virtual" \
-DBUILD_SHARED_LIBS=OFF \
-DCMAKE_FIND_LIBRARY_SUFFIXES=".a;.dylib" \
&& make -j

# copy the library and the executable to the root directory
cp flatter/build/lib/libflatter.dylib .
cp flatter/build/bin/flatter flatter-darwin
&& mkdir build \
&& cd build \
&& CMAKE_INCLUDE_PATH=$(LIBS_PATH)/gmp/include:$(LIBS_PATH)/mpfr/include:$(LIBS_PATH)/fplll/include:$(LIBS_PATH)/omp/include \
CMAKE_LIBRARY_PATH=$(LIBS_PATH)/gmp/lib:$(LIBS_PATH)/mpfr/lib:$(LIBS_PATH)/fplll/lib:$(LIBS_PATH)/omp/lib \
cmake .. \
-DCMAKE_CXX_COMPILER=/usr/bin/clang++ \
-DOpenMP_CXX_FLAGS="-Xpreprocessor -fopenmp" \
-DOpenMP_CXX_LIB_NAMES="omp" \
-DOpenMP_omp_LIBRARY="$(LIBS_PATH)/omp/lib/libomp.a" \
-DCMAKE_PREFIX_PATH=$(LIBS_PATH)/gmp:$(LIBS_PATH)/mpfr:$(LIBS_PATH)/fplll:$(LIBS_PATH)/omp \
-DCMAKE_CXX_FLAGS="-I$(LIBS_PATH)/gmp/include -I$(LIBS_PATH)/mpfr/include -I$(LIBS_PATH)/fplll/include -I$(LIBS_PATH)/omp/include -Wno-overloaded-virtual -Wno-error=overloaded-virtual -fPIC" \
-DCMAKE_POSITION_INDEPENDENT_CODE=ON \
-DBUILD_SHARED_LIBS=OFF \
-DGMP_LIBRARIES="$(LIBS_PATH)/gmp/lib/libgmp.a" \
-DMPFR_LIBRARIES="$(LIBS_PATH)/mpfr/lib/libmpfr.a" \
-DFPLLL_LIBRARIES="$(LIBS_PATH)/fplll/lib/libfplll.a" \
&& make VERBOSE=1

# Create a list of unique object files with full paths
cd flatter/build && \
find `pwd`/src/CMakeFiles/flatter.dir -name "*.o" | sort -u > obj_files.txt

# Manual linking step with unique object files
/usr/bin/clang++ \
-o flatter-darwin \
flatter/build/apps/CMakeFiles/flatter_bin.dir/flatter.cpp.o \
`cat flatter/build/obj_files.txt` \
$(LIBS_PATH)/fplll/lib/libfplll.a \
$(LIBS_PATH)/mpfr/lib/libmpfr.a \
$(LIBS_PATH)/gmp/lib/libgmp.a \
$(LIBS_PATH)/omp/lib/libomp.a \
-framework Accelerate \
-lpthread -ldl -lm \
-Xpreprocessor -fopenmp

# a quick test
echo "[[1 0 331 303]\n[0 1 456 225]\n[0 0 628 0]\n[0 0 0 628]]" | DYLD_LIBRARY_PATH=. ./flatter-darwin
echo "[[1 0 331 303]\n[0 1 456 225]\n[0 0 628 0]\n[0 0 0 628]]" | ./flatter-darwin

flatter-linux libflatter.so: $(LIBS)/fplll $(LIBS)/gmp $(LIBS)/mpfr $(LIBS)/omp $(LIBS)/openblas
flatter-linux: $(LIBS)/fplll $(LIBS)/gmp $(LIBS)/mpfr $(LIBS)/omp $(LIBS)/openblas
rm -rf flatter
mkdir flatter
tar -xf deps/flatter.tar.gz --strip-components=1 -C flatter

# First build flatter as a static library
cd flatter \
&& rm -rf build \
&& mkdir build \
&& cd build \
&& cmake .. \
-DCMAKE_PREFIX_PATH=$(LIBS_PATH)/gmp:$(LIBS_PATH)/mpfr:$(LIBS_PATH)/fplll:$(LIBS_PATH)/omp:$(LIBS_PATH)/openblas \
-DCMAKE_CXX_FLAGS="-I$(LIBS_PATH)/gmp/include -I$(LIBS_PATH)/mpfr/include -I$(LIBS_PATH)/fplll/include -I$(LIBS_PATH)/omp/include" \
&& CXXFLAGS="-fPIC" CFLAGS="-fPIC" cmake .. \
-DCMAKE_BUILD_TYPE=Release \
-DOpenMP_CXX_FLAGS="-fopenmp" \
-DOpenMP_CXX_LIB_NAMES="omp" \
-DBLA_VENDOR=OpenBLAS \
-DBLAS_LIBRARIES=$(LIBS_PATH)/openblas/lib/libopenblas.a \
&& make -j

cp flatter/build/lib/libflatter.so .
cp flatter/build/bin/flatter flatter-linux
-DLAPACK_LIBRARIES=$(LIBS_PATH)/openblas/lib/libopenblas.a \
-DOpenMP_omp_LIBRARY="$(LIBS_PATH)/omp/lib/libomp.a" \
-DCMAKE_PREFIX_PATH=$(LIBS_PATH)/gmp:$(LIBS_PATH)/mpfr:$(LIBS_PATH)/fplll:$(LIBS_PATH)/omp:$(LIBS_PATH)/openblas \
-DCMAKE_CXX_FLAGS="-I$(LIBS_PATH)/gmp/include -I$(LIBS_PATH)/mpfr/include -I$(LIBS_PATH)/fplll/include -I$(LIBS_PATH)/omp/include -I$(LIBS_PATH)/openblas/include -Wno-overloaded-virtual -Wno-error=overloaded-virtual -fPIC" \
-DCMAKE_POSITION_INDEPENDENT_CODE=ON \
-DBUILD_SHARED_LIBS=OFF \
-DGMP_LIBRARIES="$(LIBS_PATH)/gmp/lib/libgmp.a" \
-DMPFR_LIBRARIES="$(LIBS_PATH)/mpfr/lib/libmpfr.a" \
-DFPLLL_LIBRARIES="$(LIBS_PATH)/fplll/lib/libfplll.a" \
-DCMAKE_CXX_STANDARD_LIBRARIES="-static-libgcc -static-libstdc++" \
-DBLAS_ROOT=$(LIBS_PATH)/openblas \
-DLAPACK_ROOT=$(LIBS_PATH)/openblas \
&& make VERBOSE=1

# Create a list of unique object files with full paths
cd flatter/build && \
find `pwd`/src/CMakeFiles/flatter.dir -name "*.o" | sort -u > obj_files.txt

# Manual linking step with unique object files
$(CXX) \
-o flatter-linux \
flatter/build/apps/CMakeFiles/flatter_bin.dir/flatter.cpp.o \
`cat flatter/build/obj_files.txt` \
$(LIBS_PATH)/openblas/lib/libopenblas.a \
$(LIBS_PATH)/fplll/lib/libfplll.a \
$(LIBS_PATH)/mpfr/lib/libmpfr.a \
$(LIBS_PATH)/gmp/lib/libgmp.a \
$(LIBS_PATH)/omp/lib/libomp.a \
-static-libgcc -static-libstdc++ \
-lpthread -ldl -lm -fopenmp \
$(LIBS_PATH)/gmp/lib/libgmp.a

echo "[[1 0 331 303]\n[0 1 456 225]\n[0 0 628 0]\n[0 0 0 628]]" | LD_LIBRARY_PATH=. ./flatter-linux
# a quick test
echo "[[1 0 331 303]\n[0 1 456 225]\n[0 0 628 0]\n[0 0 0 628]]" | ./flatter-linux

darwin: flatter-darwin libflatter.dylib
darwin: flatter-darwin

linux: flatter-linux libflatter.so
linux: flatter-linux

clean:
rm -rf libs
Expand Down
2 changes: 0 additions & 2 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ set -e
# build the flatter binary and the shared library
if [ "$(uname)" = "Linux" ]; then
make linux
cp libflatter.so flatn
cp flatter-linux flatn/flatter
echo "Done"
elif [ "$(uname)" = "Darwin" ]; then
make darwin
cp libflatter.dylib flatn
cp flatter-darwin flatn/flatter
echo "Done"
else
Expand Down
17 changes: 2 additions & 15 deletions flatn/_flat.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@

import os
import sys
import subprocess
import importlib.resources as pkg_resources

Expand All @@ -10,13 +9,11 @@
# get file path
with pkg_resources.path(PACKAGE, '') as path_bin:
assert os.path.exists(path_bin), f"Path {path_bin} does not exist"
__path_flatter = path_bin / 'flatter'
__path_dylib = path_bin
__path_flatter = path_bin / BIN_NAME

# sanity: check if the file exists
assert os.path.exists(path_bin), f"Path {path_bin} does not exist"
assert os.path.exists(__path_flatter), f"Path {__path_flatter} does not exist"
assert os.path.exists(__path_dylib), f"Path {__path_dylib} does not exist"

def run_flatter_raw(
lattice_str: str,
Expand Down Expand Up @@ -57,18 +54,8 @@ def run_flatter_raw(
if logcond:
args += ['-l', f'{logcond}']

env = {}
if sys.platform == 'darwin':
# if we are on MacOS add libflatter.dylib to DYLD_LIBRARY_PATH
env['DYLD_LIBRARY_PATH'] = str(__path_dylib)
elif sys.platform == 'linux':
# if we are on Linux add libflatter.so using LD_PRELOAD
env['LD_PRELOAD'] = str(__path_dylib)
else:
raise NotImplementedError("This build script is only supported on MacOS and Linux")

# Run flatter command and return proc directly
return subprocess.run(args, input=lattice_str, text=True, capture_output=True, env=env)
return subprocess.run(args, input=lattice_str, text=True, capture_output=True)

def flatter(
lattice: list[list[int]],
Expand Down

0 comments on commit e1b446d

Please sign in to comment.