|
1 | | -# |
2 | | -# Copyright (c) 2016-present, Facebook, Inc. |
3 | | -# All rights reserved. |
4 | | -# |
5 | | -# This source code is licensed under the MIT license found in the |
6 | | -# LICENSE file in the root directory of this source tree. |
7 | | -# |
8 | | -CXX = c++ |
| 1 | +# hnswlib-to-go Makefile |
| 2 | +# Builds the C++ static library and Go bindings for HNSW vector search. |
| 3 | + |
| 4 | +CXX ?= c++ |
| 5 | +AR ?= ar |
9 | 6 | INCLUDES = -I. |
10 | | -CXXFLAGS = -pthread -std=c++0x -march=native -std=c++11 $(INCLUDES) |
| 7 | +CXXFLAGS = -pthread -std=c++11 $(INCLUDES) |
11 | 8 | OBJS = hnsw_wrapper.o |
12 | 9 |
|
13 | | -opt: CXXFLAGS += -O3 -funroll-loops |
| 10 | +# Platform detection |
| 11 | +ifeq ($(OS),Windows_NT) |
| 12 | + UNAME_S := Windows |
| 13 | + UNAME_M := $(PROCESSOR_ARCHITECTURE) |
| 14 | + # Windows: MinGW environment |
| 15 | + CXX ?= g++ |
| 16 | + CXXFLAGS = -std=c++11 $(INCLUDES) |
| 17 | + LDFLAGS_EXTRA = -lstdc++ |
| 18 | + RM = del /Q |
| 19 | + LIB_EXT = .a |
| 20 | +else |
| 21 | + UNAME_S := $(shell uname -s) |
| 22 | + UNAME_M := $(shell uname -m) |
| 23 | + RM = rm -rf |
| 24 | + LIB_EXT = .a |
| 25 | + ifeq ($(UNAME_S),Darwin) |
| 26 | + CXXFLAGS += -stdlib=libc++ |
| 27 | + LDFLAGS_EXTRA = -lc++ |
| 28 | + else ifeq ($(UNAME_S),Linux) |
| 29 | + LDFLAGS_EXTRA = -lstdc++ |
| 30 | + endif |
| 31 | +endif |
| 32 | + |
| 33 | +# Default: optimized build with native arch |
| 34 | +.PHONY: all opt build clean test bench lint help |
| 35 | + |
| 36 | +all: build |
| 37 | + |
| 38 | +opt: CXXFLAGS += -O3 -funroll-loops -march=native |
14 | 39 | opt: build |
15 | 40 |
|
| 41 | +# Portable build without -march=native (for CI / cross-compile) |
| 42 | +portable: CXXFLAGS += -O2 |
| 43 | +portable: build |
| 44 | + |
16 | 45 | coverage: CXXFLAGS += -O0 -fno-inline -fprofile-arcs --coverage |
| 46 | +coverage: build |
| 47 | + |
| 48 | +# ---------- C++ compilation ---------- |
17 | 49 |
|
18 | 50 | hnsw_wrapper.o: hnsw_wrapper.h hnsw_wrapper.cc hnswlib/*.h |
19 | 51 | $(CXX) $(CXXFLAGS) -c hnsw_wrapper.cc |
20 | 52 |
|
21 | 53 | libhnsw.a: $(OBJS) |
22 | 54 | $(AR) rcs libhnsw.a $(OBJS) |
23 | 55 |
|
24 | | -clean: |
25 | | - rm -rf *.o libhnsw.a *.o *.gcno *.gcda hnsw |
| 56 | +# ---------- Go targets ---------- |
26 | 57 |
|
27 | 58 | build: libhnsw.a |
28 | 59 | env CGO_CXXFLAGS="$(INCLUDES) -std=c++11" go build |
29 | 60 |
|
30 | 61 | test: build |
31 | | - go test |
| 62 | + env CGO_CXXFLAGS="$(INCLUDES) -std=c++11" go test -v -count=1 -timeout 120s ./... |
| 63 | + |
| 64 | +bench: build |
| 65 | + env CGO_CXXFLAGS="$(INCLUDES) -std=c++11" go test -bench=. -benchmem -benchtime=2s -timeout 300s ./... |
| 66 | + |
| 67 | +lint: |
| 68 | + @command -v golangci-lint >/dev/null 2>&1 && golangci-lint run ./... || echo "golangci-lint not installed, skipping" |
| 69 | + |
| 70 | +# ---------- Cross-platform builds ---------- |
| 71 | +# These targets build the static library for specific OS/arch combinations. |
| 72 | +# Requires appropriate cross-compilation toolchains to be installed. |
| 73 | + |
| 74 | +.PHONY: build-linux-amd64 build-linux-arm64 build-darwin-amd64 build-darwin-arm64 build-windows-amd64 |
| 75 | + |
| 76 | +build-linux-amd64: |
| 77 | + GOOS=linux GOARCH=amd64 CGO_ENABLED=1 \ |
| 78 | + CC=x86_64-linux-gnu-gcc CXX=x86_64-linux-gnu-g++ \ |
| 79 | + $(MAKE) CXX=x86_64-linux-gnu-g++ AR=x86_64-linux-gnu-ar build |
| 80 | + |
| 81 | +build-linux-arm64: |
| 82 | + GOOS=linux GOARCH=arm64 CGO_ENABLED=1 \ |
| 83 | + CC=aarch64-linux-gnu-gcc CXX=aarch64-linux-gnu-g++ \ |
| 84 | + $(MAKE) CXX=aarch64-linux-gnu-g++ AR=aarch64-linux-gnu-ar build |
| 85 | + |
| 86 | +build-darwin-amd64: |
| 87 | + GOOS=darwin GOARCH=amd64 CGO_ENABLED=1 \ |
| 88 | + $(MAKE) CXXFLAGS="-pthread -std=c++11 $(INCLUDES) -target x86_64-apple-macos11" build |
| 89 | + |
| 90 | +build-darwin-arm64: |
| 91 | + GOOS=darwin GOARCH=arm64 CGO_ENABLED=1 \ |
| 92 | + $(MAKE) CXXFLAGS="-pthread -std=c++11 $(INCLUDES) -target arm64-apple-macos11" build |
| 93 | + |
| 94 | +build-windows-amd64: |
| 95 | + GOOS=windows GOARCH=amd64 CGO_ENABLED=1 \ |
| 96 | + CC=x86_64-w64-mingw32-gcc CXX=x86_64-w64-mingw32-g++ \ |
| 97 | + $(MAKE) CXX=x86_64-w64-mingw32-g++ AR=x86_64-w64-mingw32-ar \ |
| 98 | + CXXFLAGS="-std=c++11 $(INCLUDES)" build |
| 99 | + |
| 100 | +# ---------- Cleanup ---------- |
| 101 | + |
| 102 | +clean: |
| 103 | +ifeq ($(OS),Windows_NT) |
| 104 | + -del /Q *.o libhnsw.a *.gcno *.gcda hnsw.exe 2>nul |
| 105 | +else |
| 106 | + rm -rf *.o libhnsw.a *.gcno *.gcda hnsw |
| 107 | +endif |
| 108 | + |
| 109 | +# ---------- Help ---------- |
| 110 | + |
| 111 | +help: |
| 112 | + @echo "hnswlib-to-go build targets:" |
| 113 | + @echo "" |
| 114 | + @echo " make build - Build C++ library and Go package (default)" |
| 115 | + @echo " make opt - Build with -O3 and -march=native optimizations" |
| 116 | + @echo " make portable - Build without -march=native (CI-friendly)" |
| 117 | + @echo " make test - Run unit tests" |
| 118 | + @echo " make bench - Run benchmarks" |
| 119 | + @echo " make lint - Run golangci-lint (if installed)" |
| 120 | + @echo " make clean - Remove build artifacts" |
| 121 | + @echo "" |
| 122 | + @echo "Cross-platform targets (require cross-compilation toolchains):" |
| 123 | + @echo " make build-linux-amd64 - Build for Linux x86_64" |
| 124 | + @echo " make build-linux-arm64 - Build for Linux aarch64" |
| 125 | + @echo " make build-darwin-amd64 - Build for macOS x86_64" |
| 126 | + @echo " make build-darwin-arm64 - Build for macOS ARM64" |
| 127 | + @echo " make build-windows-amd64 - Build for Windows x86_64 (MinGW)" |
| 128 | + @echo "" |
| 129 | + @echo "Detected platform: $(UNAME_S) / $(UNAME_M)" |
0 commit comments