@@ -73,15 +73,19 @@ def cpp_flag(compiler):
73
73
74
74
class BuildExt (build_ext ):
75
75
"""A custom build extension for adding compiler-specific options."""
76
+ compiler_flag_native = '-march=native'
76
77
c_opts = {
77
78
'msvc' : ['/EHsc' , '/openmp' , '/O2' ],
78
- 'unix' : ['-O3' , '-march=native' ], # , '-w'
79
+ 'unix' : ['-O3' , compiler_flag_native ], # , '-w'
79
80
}
80
81
link_opts = {
81
82
'unix' : [],
82
83
'msvc' : [],
83
84
}
84
85
86
+ if os .environ .get ("HNSWLIB_NO_NATIVE" ):
87
+ c_opts ['unix' ].remove (compiler_flag_native )
88
+
85
89
if sys .platform == 'darwin' :
86
90
c_opts ['unix' ] += ['-stdlib=libc++' , '-mmacosx-version-min=10.7' ]
87
91
link_opts ['unix' ] += ['-stdlib=libc++' , '-mmacosx-version-min=10.7' ]
@@ -91,35 +95,35 @@ class BuildExt(build_ext):
91
95
92
96
def build_extensions (self ):
93
97
ct = self .compiler .compiler_type
94
- opts = self .c_opts .get (ct , [])
98
+ opts = BuildExt .c_opts .get (ct , [])
95
99
if ct == 'unix' :
96
100
opts .append ('-DVERSION_INFO="%s"' % self .distribution .get_version ())
97
101
opts .append (cpp_flag (self .compiler ))
98
102
if has_flag (self .compiler , '-fvisibility=hidden' ):
99
103
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' )
117
121
elif ct == 'msvc' :
118
122
opts .append ('/DVERSION_INFO=\\ "%s\\ "' % self .distribution .get_version ())
119
123
120
124
for ext in self .extensions :
121
125
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 , []))
123
127
124
128
build_ext .build_extensions (self )
125
129
0 commit comments