Skip to content

Commit a4c8b0b

Browse files
authored
Fix mac setup (#461)
* Add macos into CI * Fix mac setup * Use macos-13 in CI * Allow asserts, fix checkIntegrity * Revert macos to latest in CI * Fix CI * Windows build warnings
1 parent 1925428 commit a4c8b0b

File tree

4 files changed

+39
-17
lines changed

4 files changed

+39
-17
lines changed

.github/workflows/build.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ jobs:
77
runs-on: ${{matrix.os}}
88
strategy:
99
matrix:
10-
os: [ubuntu-latest, windows-latest]
10+
os: [ubuntu-latest, windows-latest, macos-latest]
1111
python-version: ["3.7", "3.8", "3.9", "3.10"]
1212
steps:
1313
- uses: actions/checkout@v3
@@ -28,7 +28,7 @@ jobs:
2828
runs-on: ${{matrix.os}}
2929
strategy:
3030
matrix:
31-
os: [ubuntu-latest, windows-latest]
31+
os: [ubuntu-latest, windows-latest, macos-latest]
3232
steps:
3333
- uses: actions/checkout@v3
3434
- uses: actions/setup-python@v4
@@ -40,10 +40,10 @@ jobs:
4040
mkdir build
4141
cd build
4242
cmake ..
43-
if [ "$RUNNER_OS" == "Linux" ]; then
44-
make
45-
elif [ "$RUNNER_OS" == "Windows" ]; then
43+
if [ "$RUNNER_OS" == "Windows" ]; then
4644
cmake --build ./ --config Release
45+
else
46+
make
4747
fi
4848
shell: bash
4949

CMakeLists.txt

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ project(hnswlib
44
LANGUAGES CXX)
55

66
include(GNUInstallDirs)
7+
include(CheckCXXCompilerFlag)
78

89
add_library(hnswlib INTERFACE)
910
add_library(hnswlib::hnswlib ALIAS hnswlib)
@@ -33,12 +34,23 @@ endif()
3334
if(HNSWLIB_EXAMPLES)
3435
set(CMAKE_CXX_STANDARD 11)
3536

36-
if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
37-
SET( CMAKE_CXX_FLAGS "-Ofast -DNDEBUG -std=c++11 -DHAVE_CXX0X -openmp -march=native -fpic -ftree-vectorize")
37+
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
38+
SET( CMAKE_CXX_FLAGS "-Ofast -std=c++11 -DHAVE_CXX0X -openmp -fpic -ftree-vectorize" )
39+
check_cxx_compiler_flag("-march=native" COMPILER_SUPPORT_NATIVE_FLAG)
40+
if(COMPILER_SUPPORT_NATIVE_FLAG)
41+
SET( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=native" )
42+
message("set -march=native flag")
43+
else()
44+
check_cxx_compiler_flag("-mcpu=apple-m1" COMPILER_SUPPORT_M1_FLAG)
45+
if(COMPILER_SUPPORT_M1_FLAG)
46+
SET( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mcpu=apple-m1" )
47+
message("set -mcpu=apple-m1 flag")
48+
endif()
49+
endif()
3850
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
39-
SET( CMAKE_CXX_FLAGS "-Ofast -lrt -DNDEBUG -std=c++11 -DHAVE_CXX0X -march=native -fpic -w -fopenmp -ftree-vectorize -ftree-vectorizer-verbose=0" )
51+
SET( CMAKE_CXX_FLAGS "-Ofast -lrt -std=c++11 -DHAVE_CXX0X -march=native -fpic -w -fopenmp -ftree-vectorize -ftree-vectorizer-verbose=0" )
4052
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
41-
SET( CMAKE_CXX_FLAGS "-Ofast -lrt -DNDEBUG -std=c++11 -DHAVE_CXX0X -openmp -march=native -fpic -w -fopenmp -ftree-vectorize" )
53+
SET( CMAKE_CXX_FLAGS "/O2 -DHAVE_CXX0X /W1 /openmp /EHsc" )
4254
endif()
4355

4456
# examples

hnswlib/hnswalg.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1272,7 +1272,6 @@ class HierarchicalNSW : public AlgorithmInterface<dist_t> {
12721272
tableint *data = (tableint *) (ll_cur + 1);
12731273
std::unordered_set<tableint> s;
12741274
for (int j = 0; j < size; j++) {
1275-
assert(data[j] > 0);
12761275
assert(data[j] < cur_element_count);
12771276
assert(data[j] != i);
12781277
inbound_connections_num[data[j]]++;

setup.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,20 +75,14 @@ class BuildExt(build_ext):
7575
"""A custom build extension for adding compiler-specific options."""
7676
c_opts = {
7777
'msvc': ['/EHsc', '/openmp', '/O2'],
78-
#'unix': ['-O3', '-march=native'], # , '-w'
79-
'unix': ['-O3'], # , '-w'
78+
'unix': ['-O3', '-march=native'], # , '-w'
8079
}
81-
if not os.environ.get("HNSWLIB_NO_NATIVE"):
82-
c_opts['unix'].append('-march=native')
83-
8480
link_opts = {
8581
'unix': [],
8682
'msvc': [],
8783
}
8884

8985
if sys.platform == 'darwin':
90-
if platform.machine() == 'arm64':
91-
c_opts['unix'].remove('-march=native')
9286
c_opts['unix'] += ['-stdlib=libc++', '-mmacosx-version-min=10.7']
9387
link_opts['unix'] += ['-stdlib=libc++', '-mmacosx-version-min=10.7']
9488
else:
@@ -103,6 +97,23 @@ def build_extensions(self):
10397
opts.append(cpp_flag(self.compiler))
10498
if has_flag(self.compiler, '-fvisibility=hidden'):
10599
opts.append('-fvisibility=hidden')
100+
# check that native flag is available
101+
native_flag = '-march=native'
102+
print('checking avalability of flag:', native_flag)
103+
if not has_flag(self.compiler, native_flag):
104+
print('removing unsupported compiler flag:', native_flag)
105+
opts.remove(native_flag)
106+
# for macos add apple-m1 flag if it's available
107+
if sys.platform == 'darwin':
108+
m1_flag = '-mcpu=apple-m1'
109+
print('checking avalability of flag:', m1_flag)
110+
if has_flag(self.compiler, m1_flag):
111+
print('adding flag:', m1_flag)
112+
opts.append(m1_flag)
113+
else:
114+
print(f'flag: {m1_flag} is not available')
115+
else:
116+
print(f'flag: {native_flag} is available')
106117
elif ct == 'msvc':
107118
opts.append('/DVERSION_INFO=\\"%s\\"' % self.distribution.get_version())
108119

0 commit comments

Comments
 (0)