Skip to content

Commit fd027d4

Browse files
authored
Merge pull request #493 from dyashuni/no_native
Bring back HNSWLIB_NO_NATIVE
2 parents 39b210d + eecd540 commit fd027d4

File tree

1 file changed

+24
-20
lines changed

1 file changed

+24
-20
lines changed

setup.py

+24-20
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,19 @@ def cpp_flag(compiler):
7373

7474
class BuildExt(build_ext):
7575
"""A custom build extension for adding compiler-specific options."""
76+
compiler_flag_native = '-march=native'
7677
c_opts = {
7778
'msvc': ['/EHsc', '/openmp', '/O2'],
78-
'unix': ['-O3', '-march=native'], # , '-w'
79+
'unix': ['-O3', compiler_flag_native], # , '-w'
7980
}
8081
link_opts = {
8182
'unix': [],
8283
'msvc': [],
8384
}
8485

86+
if os.environ.get("HNSWLIB_NO_NATIVE"):
87+
c_opts['unix'].remove(compiler_flag_native)
88+
8589
if sys.platform == 'darwin':
8690
c_opts['unix'] += ['-stdlib=libc++', '-mmacosx-version-min=10.7']
8791
link_opts['unix'] += ['-stdlib=libc++', '-mmacosx-version-min=10.7']
@@ -91,35 +95,35 @@ class BuildExt(build_ext):
9195

9296
def build_extensions(self):
9397
ct = self.compiler.compiler_type
94-
opts = self.c_opts.get(ct, [])
98+
opts = BuildExt.c_opts.get(ct, [])
9599
if ct == 'unix':
96100
opts.append('-DVERSION_INFO="%s"' % self.distribution.get_version())
97101
opts.append(cpp_flag(self.compiler))
98102
if has_flag(self.compiler, '-fvisibility=hidden'):
99103
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')
104+
if not os.environ.get("HNSWLIB_NO_NATIVE"):
105+
# check that native flag is available
106+
print('checking avalability of flag:', BuildExt.compiler_flag_native)
107+
if not has_flag(self.compiler, BuildExt.compiler_flag_native):
108+
print('removing unsupported compiler flag:', BuildExt.compiler_flag_native)
109+
opts.remove(BuildExt.compiler_flag_native)
110+
# for macos add apple-m1 flag if it's available
111+
if sys.platform == 'darwin':
112+
m1_flag = '-mcpu=apple-m1'
113+
print('checking avalability of flag:', m1_flag)
114+
if has_flag(self.compiler, m1_flag):
115+
print('adding flag:', m1_flag)
116+
opts.append(m1_flag)
117+
else:
118+
print(f'flag: {m1_flag} is not available')
119+
else:
120+
print(f'flag: {BuildExt.compiler_flag_native} is available')
117121
elif ct == 'msvc':
118122
opts.append('/DVERSION_INFO=\\"%s\\"' % self.distribution.get_version())
119123

120124
for ext in self.extensions:
121125
ext.extra_compile_args.extend(opts)
122-
ext.extra_link_args.extend(self.link_opts.get(ct, []))
126+
ext.extra_link_args.extend(BuildExt.link_opts.get(ct, []))
123127

124128
build_ext.build_extensions(self)
125129

0 commit comments

Comments
 (0)