Skip to content

Commit a74cf3f

Browse files
authored
Fix build in Windows.
1 parent a4b2ae2 commit a74cf3f

File tree

2 files changed

+44
-20
lines changed

2 files changed

+44
-20
lines changed

README.rst

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
.. image:: https://travis-ci.org/VirusTotal/yara-python.svg
2-
:target: https://travis-ci.org/VirusTotal/yara-python
31
.. image:: https://ci.appveyor.com/api/projects/status/gidnb9ulj3rje5s2?svg=true
42
:target: https://ci.appveyor.com/project/plusvic/yara-python
53

setup.py

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,13 @@ def muted(*streams):
7676
devnull.close()
7777

7878

79-
def has_function(function_name, include_dirs=None, libraries=None, library_dirs=None):
79+
def has_function(function_name, includes=None, include_dirs=None, libraries=None, library_dirs=None):
8080
"""Checks if a given functions exists in the current platform."""
8181
compiler = distutils.ccompiler.new_compiler()
8282
with muted(sys.stdout, sys.stderr):
8383
result = compiler.has_function(
8484
function_name,
85+
includes=includes,
8586
include_dirs=include_dirs,
8687
libraries=libraries,
8788
library_dirs=library_dirs)
@@ -188,12 +189,6 @@ def run(self):
188189

189190
exclusions = []
190191

191-
# Needed to build tlsh
192-
module.define_macros.extend([('BUCKETS_128', 1), ('CHECKSUM_1B', 1)])
193-
194-
# Needed to build authenticode parser
195-
module.libraries.append('ssl')
196-
197192
for define in self.define or []:
198193
module.define_macros.append(define)
199194

@@ -206,6 +201,22 @@ def run(self):
206201
building_for_freebsd = 'freebsd' in self.plat_name
207202
building_for_openbsd = 'openbsd' in self.plat_name # need testing
208203

204+
if building_for_windows:
205+
arch = 'x86' if self.plat_name == 'win32' else 'x64'
206+
openssl_include_dirs = [
207+
os.path.join(base_dir, 'yara\\windows\\vs2015\\packages\\YARA.OpenSSL.{}.1.1.1\\include'.format(arch)),
208+
os.path.join(base_dir, 'yara\\windows\\vs2017\\packages\\YARA.OpenSSL.{}.1.1.1\\include'.format(arch))
209+
]
210+
openssl_library_dirs = [
211+
os.path.join(base_dir, 'yara\\windows\\vs2015\\packages\\YARA.OpenSSL.{}.1.1.1\\lib'.format(arch)),
212+
os.path.join(base_dir, 'yara\\windows\\vs2017\\packages\\YARA.OpenSSL.{}.1.1.1\\lib'.format(arch))
213+
]
214+
openssl_libraries = ['libcrypto']
215+
else:
216+
openssl_include_dirs = []
217+
openssl_library_dirs = []
218+
openssl_libraries = ['crypto']
219+
209220
if building_for_linux:
210221
module.define_macros.append(('_GNU_SOURCE', '1'))
211222
module.define_macros.append(('USE_LINUX_PROC', '1'))
@@ -264,17 +275,32 @@ def run(self):
264275
if self.dynamic_linking:
265276
module.libraries.append('yara')
266277
else:
267-
if not self.define or not ('HASH_MODULE', '1') in self.define:
268-
if (has_function('MD5_Init', include_dirs=module.include_dirs, libraries=['crypto'], library_dirs=module.library_dirs) and
269-
has_function('SHA256_Init', include_dirs=module.include_dirs, libraries=['crypto'], library_dirs=module.library_dirs)):
270-
module.define_macros.append(('HASH_MODULE', '1'))
271-
module.define_macros.append(('HAVE_LIBCRYPTO', '1'))
272-
module.libraries.append('crypto')
273-
elif building_for_windows:
274-
module.define_macros.append(('HASH_MODULE', '1'))
275-
module.define_macros.append(('HAVE_WINCRYPT_H', '1'))
276-
else:
277-
exclusions.append('yara/libyara/modules/hash/hash.c')
278+
# Is OpenSSL available?
279+
if (has_function('OpenSSL_add_all_algorithms',
280+
includes=['openssl/evp.h'],
281+
include_dirs=module.include_dirs + openssl_include_dirs,
282+
libraries=module.libraries + openssl_libraries,
283+
library_dirs=module.library_dirs + openssl_library_dirs)
284+
# In case OpenSSL is being linked statically
285+
or has_function('OpenSSL_add_all_algorithms',
286+
includes=['openssl/evp.h'],
287+
include_dirs=module.include_dirs + openssl_include_dirs,
288+
libraries=module.libraries + openssl_libraries + ['dl', 'pthread', 'z'],
289+
library_dirs=module.library_dirs + openssl_library_dirs)
290+
):
291+
module.define_macros.append(('HASH_MODULE', '1'))
292+
module.define_macros.append(('HAVE_LIBCRYPTO', '1'))
293+
module.libraries.extend(openssl_libraries)
294+
module.include_dirs.extend(openssl_include_dirs)
295+
module.library_dirs.extend(openssl_library_dirs)
296+
elif building_for_windows:
297+
# OpenSSL is not available, but in Windows we can rely on Wincrypt.
298+
module.define_macros.append(('HASH_MODULE', '1'))
299+
module.define_macros.append(('HAVE_WINCRYPT_H', '1'))
300+
else:
301+
# OpenSSL is not available, exclude hash.c, as it requires some hashing
302+
# functions.
303+
exclusions.append('yara/libyara/modules/hash/hash.c')
278304

279305
if self.enable_magic:
280306
module.define_macros.append(('MAGIC_MODULE', '1'))

0 commit comments

Comments
 (0)