Skip to content

Commit

Permalink
Merge pull request #493 from dyashuni/no_native
Browse files Browse the repository at this point in the history
Bring back HNSWLIB_NO_NATIVE
  • Loading branch information
yurymalkov authored Aug 19, 2023
2 parents 39b210d + eecd540 commit fd027d4
Showing 1 changed file with 24 additions and 20 deletions.
44 changes: 24 additions & 20 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,19 @@ def cpp_flag(compiler):

class BuildExt(build_ext):
"""A custom build extension for adding compiler-specific options."""
compiler_flag_native = '-march=native'
c_opts = {
'msvc': ['/EHsc', '/openmp', '/O2'],
'unix': ['-O3', '-march=native'], # , '-w'
'unix': ['-O3', compiler_flag_native], # , '-w'
}
link_opts = {
'unix': [],
'msvc': [],
}

if os.environ.get("HNSWLIB_NO_NATIVE"):
c_opts['unix'].remove(compiler_flag_native)

if sys.platform == 'darwin':
c_opts['unix'] += ['-stdlib=libc++', '-mmacosx-version-min=10.7']
link_opts['unix'] += ['-stdlib=libc++', '-mmacosx-version-min=10.7']
Expand All @@ -91,35 +95,35 @@ class BuildExt(build_ext):

def build_extensions(self):
ct = self.compiler.compiler_type
opts = self.c_opts.get(ct, [])
opts = BuildExt.c_opts.get(ct, [])
if ct == 'unix':
opts.append('-DVERSION_INFO="%s"' % self.distribution.get_version())
opts.append(cpp_flag(self.compiler))
if has_flag(self.compiler, '-fvisibility=hidden'):
opts.append('-fvisibility=hidden')
# check that native flag is available
native_flag = '-march=native'
print('checking avalability of flag:', native_flag)
if not has_flag(self.compiler, native_flag):
print('removing unsupported compiler flag:', native_flag)
opts.remove(native_flag)
# for macos add apple-m1 flag if it's available
if sys.platform == 'darwin':
m1_flag = '-mcpu=apple-m1'
print('checking avalability of flag:', m1_flag)
if has_flag(self.compiler, m1_flag):
print('adding flag:', m1_flag)
opts.append(m1_flag)
else:
print(f'flag: {m1_flag} is not available')
else:
print(f'flag: {native_flag} is available')
if not os.environ.get("HNSWLIB_NO_NATIVE"):
# check that native flag is available
print('checking avalability of flag:', BuildExt.compiler_flag_native)
if not has_flag(self.compiler, BuildExt.compiler_flag_native):
print('removing unsupported compiler flag:', BuildExt.compiler_flag_native)
opts.remove(BuildExt.compiler_flag_native)
# for macos add apple-m1 flag if it's available
if sys.platform == 'darwin':
m1_flag = '-mcpu=apple-m1'
print('checking avalability of flag:', m1_flag)
if has_flag(self.compiler, m1_flag):
print('adding flag:', m1_flag)
opts.append(m1_flag)
else:
print(f'flag: {m1_flag} is not available')
else:
print(f'flag: {BuildExt.compiler_flag_native} is available')
elif ct == 'msvc':
opts.append('/DVERSION_INFO=\\"%s\\"' % self.distribution.get_version())

for ext in self.extensions:
ext.extra_compile_args.extend(opts)
ext.extra_link_args.extend(self.link_opts.get(ct, []))
ext.extra_link_args.extend(BuildExt.link_opts.get(ct, []))

build_ext.build_extensions(self)

Expand Down

0 comments on commit fd027d4

Please sign in to comment.