From 08569de4b805aff2c65986b282d3870f610f32c4 Mon Sep 17 00:00:00 2001 From: Dmitry Yashunin Date: Fri, 11 Aug 2023 18:11:09 +0400 Subject: [PATCH 1/3] Bring back HNSWLIB_NO_NATIVE --- setup.py | 40 ++++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/setup.py b/setup.py index c630e876..6669339d 100644 --- a/setup.py +++ b/setup.py @@ -73,15 +73,19 @@ def cpp_flag(compiler): class BuildExt(build_ext): """A custom build extension for adding compiler-specific options.""" + native_flag = '-march=native' c_opts = { 'msvc': ['/EHsc', '/openmp', '/O2'], - 'unix': ['-O3', '-march=native'], # , '-w' + 'unix': ['-O3', native_flag], # , '-w' } link_opts = { 'unix': [], 'msvc': [], } + if os.environ.get("HNSWLIB_NO_NATIVE"): + c_opts['unix'].remove(native_flag) + if sys.platform == 'darwin': c_opts['unix'] += ['-stdlib=libc++', '-mmacosx-version-min=10.7'] link_opts['unix'] += ['-stdlib=libc++', '-mmacosx-version-min=10.7'] @@ -97,23 +101,23 @@ def build_extensions(self): 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:', 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') elif ct == 'msvc': opts.append('/DVERSION_INFO=\\"%s\\"' % self.distribution.get_version()) From e023f7e1bae1c297d9eb20525eccc920bacd13c8 Mon Sep 17 00:00:00 2001 From: Dmitry Yashunin Date: Fri, 11 Aug 2023 18:20:50 +0400 Subject: [PATCH 2/3] Fix --- setup.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/setup.py b/setup.py index 6669339d..17ef179d 100644 --- a/setup.py +++ b/setup.py @@ -103,10 +103,10 @@ def build_extensions(self): opts.append('-fvisibility=hidden') if not os.environ.get("HNSWLIB_NO_NATIVE"): # check that native flag is available - 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) + print('checking avalability of flag:', self.native_flag) + if not has_flag(self.compiler, self.native_flag): + print('removing unsupported compiler flag:', self.native_flag) + opts.remove(self.native_flag) # for macos add apple-m1 flag if it's available if sys.platform == 'darwin': m1_flag = '-mcpu=apple-m1' @@ -117,7 +117,7 @@ def build_extensions(self): else: print(f'flag: {m1_flag} is not available') else: - print(f'flag: {native_flag} is available') + print(f'flag: {self.native_flag} is available') elif ct == 'msvc': opts.append('/DVERSION_INFO=\\"%s\\"' % self.distribution.get_version()) From eecd540eb1be6c93a21f3d211fa00be33acf3764 Mon Sep 17 00:00:00 2001 From: Dmitry Yashunin Date: Sun, 13 Aug 2023 21:05:59 +0400 Subject: [PATCH 3/3] Rename flag, use class name to access static variables --- setup.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/setup.py b/setup.py index 17ef179d..062b66ce 100644 --- a/setup.py +++ b/setup.py @@ -73,10 +73,10 @@ def cpp_flag(compiler): class BuildExt(build_ext): """A custom build extension for adding compiler-specific options.""" - native_flag = '-march=native' + compiler_flag_native = '-march=native' c_opts = { 'msvc': ['/EHsc', '/openmp', '/O2'], - 'unix': ['-O3', native_flag], # , '-w' + 'unix': ['-O3', compiler_flag_native], # , '-w' } link_opts = { 'unix': [], @@ -84,7 +84,7 @@ class BuildExt(build_ext): } if os.environ.get("HNSWLIB_NO_NATIVE"): - c_opts['unix'].remove(native_flag) + c_opts['unix'].remove(compiler_flag_native) if sys.platform == 'darwin': c_opts['unix'] += ['-stdlib=libc++', '-mmacosx-version-min=10.7'] @@ -95,7 +95,7 @@ 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)) @@ -103,10 +103,10 @@ def build_extensions(self): opts.append('-fvisibility=hidden') if not os.environ.get("HNSWLIB_NO_NATIVE"): # check that native flag is available - print('checking avalability of flag:', self.native_flag) - if not has_flag(self.compiler, self.native_flag): - print('removing unsupported compiler flag:', self.native_flag) - opts.remove(self.native_flag) + 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' @@ -117,13 +117,13 @@ def build_extensions(self): else: print(f'flag: {m1_flag} is not available') else: - print(f'flag: {self.native_flag} is available') + 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)