Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Windows compatibility #1

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 29 additions & 10 deletions bob/extension/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,10 @@ def __init__(self, name, sources, **kwargs):

# add the -isystem to all system include dirs
for k in system_includes:
parameters['extra_compile_args'].extend(['-isystem', k])
if os.name == 'nt':
parameters['extra_compile_args'].extend(['-I' + k])
else:
parameters['extra_compile_args'].extend(['-isystem', k])

# Filter and make unique
for key in parameters.keys():
Expand Down Expand Up @@ -465,6 +468,9 @@ def __init__(self, name, sources, **kwargs):
# Uniq'fy library directories
kwargs['library_dirs'] = uniq_paths(kwargs['library_dirs'])

if os.name == 'nt':
kwargs['define_macros'] = [(x, y.replace('"', '\\"')) for x,y in kwargs['define_macros']]

# Run the constructor for the base class
DistutilsExtension.__init__(self, name, sources, **kwargs)

Expand Down Expand Up @@ -576,6 +582,7 @@ def compile(self, build_directory, compiler = None, stdout=None):
self.c_target_directory = os.path.join(os.path.realpath(build_directory), self.c_sub_directory)
if not os.path.exists(self.c_target_directory):
os.makedirs(self.c_target_directory)

# generate CMakeLists.txt makefile
generator = CMakeListsGenerator(
name = self.c_name,
Expand Down Expand Up @@ -603,11 +610,22 @@ def compile(self, build_directory, compiler = None, stdout=None):
env['CXX'] = compiler
# configure cmake
command = [self.c_cmake, final_build_dir]

# if on windows and 64-bit add 64bit macro
if os.name == 'nt' and platform.architecture()[0] == '64bit':
command.extend(['-G', 'Visual Studio 14 2015 Win64'])

if subprocess.call(command, cwd=final_build_dir, env=env, stdout=stdout) != 0:
raise OSError("Could not generate makefiles with CMake")
# run make
make_call = ['make']
if "BOB_BUILD_PARALLEL" in os.environ: make_call += ['-j%s' % os.environ["BOB_BUILD_PARALLEL"]]
# run make or cmake depending on the platform
if os.name == 'nt':
make_call = 'cmake --build . --target ALL_BUILD --config Release'.split()
# make_call = 'cmake --build .'.split()
else:
make_call = ['make']
if "BOB_BUILD_PARALLEL" in os.environ: make_call += ['-j%s' % os.environ["BOB_BUILD_PARALLEL"]]

print("Calling the command: {}".format(make_call))
if subprocess.call(make_call, cwd=final_build_dir, env=env, stdout=stdout) != 0:
raise OSError("CMake compilation stopped with an error; stopping ...")

Expand Down Expand Up @@ -646,12 +664,13 @@ def build_extension(self, ext):
"""

# HACK: remove the "-Wstrict-prototypes" option keyword
self.compiler.compiler = [c for c in self.compiler.compiler if c != "-Wstrict-prototypes"]
self.compiler.compiler_so = [c for c in self.compiler.compiler_so if c != "-Wstrict-prototypes"]
if "-Wno-strict-aliasing" not in self.compiler.compiler:
self.compiler.compiler.append("-Wno-strict-aliasing")
if "-Wno-strict-aliasing" not in self.compiler.compiler_so:
self.compiler.compiler_so.append("-Wno-strict-aliasing")
if os.name != 'nt':
self.compiler.compiler = [c for c in self.compiler.compiler if c != "-Wstrict-prototypes"]
self.compiler.compiler_so = [c for c in self.compiler.compiler_so if c != "-Wstrict-prototypes"]
if "-Wno-strict-aliasing" not in self.compiler.compiler:
self.compiler.compiler.append("-Wno-strict-aliasing")
if "-Wno-strict-aliasing" not in self.compiler.compiler_so:
self.compiler.compiler_so.append("-Wno-strict-aliasing")

# check if it is our type of extension
if isinstance(ext, Library):
Expand Down
4 changes: 2 additions & 2 deletions bob/extension/boost.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def __init__ (self, requirement=''):


def libconfig(self, modules, only_static=False,
templates=['boost_%(name)s-mt-%(py)s', 'boost_%(name)s-%(py)s', 'boost_%(name)s-mt', 'boost_%(name)s']):
templates=['boost_%(name)s-mt-%(py)s', 'boost_%(name)s-%(py)s', 'boost_%(name)s-mt', 'boost_%(name)s', 'boost_%(name)s-vc140-mt-1_65_1']):
"""Returns a tuple containing the library configuration for requested
modules.

Expand Down Expand Up @@ -184,7 +184,7 @@ def libconfig(self, modules, only_static=False,
libraries = []
for f in filenames:
name, ext = os.path.splitext(os.path.basename(f))
if ext in ['.so', '.a', '.dylib', '.dll']:
if ext in ['.so', '.a', '.dylib', '.dll', '.lib']:
libraries.append(name[3:]) #strip 'lib' from the name
else: #link against the whole thing
libraries.append(':' + os.path.basename(f))
Expand Down
23 changes: 15 additions & 8 deletions bob/extension/cmake.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
' set(COMMON_CXX_FLAGS "-std=c++0x")\n'
' set(COMMON_C_FLAGS "-std=c99")\n'
'elseif(WIN32)\n'
' set(COMMON_CXX_FLAGS "-std=gnu++0x")\n'
' set(COMMON_C_FLAGS "-std=gnu99")\n'
' set(COMMON_CXX_FLAGS "/EHsc")\n'
' set(COMMON_C_FLAGS "")\n'
'else()\n'
' set(COMMON_CXX_FLAGS "-std=c++0x")\n'
' set(COMMON_C_FLAGS "-std=c99")\n'
Expand Down Expand Up @@ -113,20 +113,27 @@ def generate(self, source_directory, build_directory):
f.write(HEADER)
# add include directories
for directory in self.includes:
f.write('include_directories(%s)\n' % directory)
f.write('include_directories(%s)\n' % directory.replace('\\','\\\\'))
for directory in self.system_includes:
f.write('include_directories(SYSTEM %s)\n' % directory)
f.write('include_directories(SYSTEM %s)\n' % directory.replace('\\','\\\\'))
# add link directories
# TODO: handle RPATH and Non-RPATH differently (don't know, how, though)
for directory in self.library_directories:
f.write('link_directories(%s)\n' % directory)
f.write('link_directories(%s)\n' % directory.replace('\\','\\\\'))
# add defines
for macro in self.macros:
f.write('add_definitions(-D%s=%s)\n' % macro)
if os.name == 'nt':
f.write('add_definitions(/D%s=%s)\n' % macro)
else:
f.write('add_definitions(-D%s=%s)\n' % macro)
# compile this library
f.write('\nadd_library(${PROJECT_NAME} \n\t' + "\n\t".join(source_files) + '\n)\n')
if os.name == 'nt':
static = 'STATIC'
else:
static = ''
f.write('\nadd_library(${PROJECT_NAME} {} \n\t'.format(static) + "\n\t".join(source_files).replace('\\','/') + '\n)\n')
f.write('set_target_properties(${PROJECT_NAME} PROPERTIES POSITION_INDEPENDENT_CODE TRUE)\n')
f.write('set_target_properties(${PROJECT_NAME} PROPERTIES LIBRARY_OUTPUT_DIRECTORY %s)\n\n' % self.target_directory)
f.write('set_target_properties(${PROJECT_NAME} PROPERTIES LIBRARY_OUTPUT_DIRECTORY %s)\n\n' % self.target_directory.replace('\\','/'))
# link libraries
if self.libraries:
f.write('target_link_libraries(${PROJECT_NAME} %s)\n\n' % " ".join(self.libraries))
5 changes: 5 additions & 0 deletions bob/extension/pkgconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,13 @@ def call_pkgconfig(cmd, paths=None):
old = env.get('PKG_CONFIG_PATH', False)
env['PKG_CONFIG_PATH'] = os.pathsep.join([var, old]) if old else var

# windows
if os.name == 'nt':
env['PKG_CONFIG_PATH'] = os.path.join(env.get("CONDA_PREFIX", False),"Library","lib","pkgconfig")

# calls the program
cmd = pkg_config[:1] + [str(k) for k in cmd]

subproc = subprocess.Popen(
cmd,
env=env,
Expand Down
24 changes: 20 additions & 4 deletions bob/extension/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ def construct_search_paths(prefixes=None, subpaths=None, suffix=None):
# Priority 4: the conda prefix
conda_prefix = os.environ.get('CONDA_PREFIX')
if conda_prefix:
search.append(conda_prefix + suffix)
if os.name == 'nt':
search.append(os.path.join(conda_prefix,'Library') + suffix)
else:
search.append(conda_prefix + suffix)

# Priority 5: the default search prefixes
search += [p + suffix for p in DEFAULT_PREFIXES]
Expand Down Expand Up @@ -225,6 +228,9 @@ def find_library(name, version=None, subpaths=None, prefixes=None,

libpaths += ['lib']

if os.name == 'nt':
libpaths += ['bin']

# Exhaustive combination of paths and subpaths
if subpaths:
my_subpaths = []
Expand All @@ -235,12 +241,15 @@ def find_library(name, version=None, subpaths=None, prefixes=None,

# Extensions to consider
if only_static:
extensions = ['.a']
if os.name == 'nt':
extensions = ['.lib']
else:
extensions = ['.a']
else:
if sys.platform == 'darwin':
extensions = ['.dylib', '.a']
elif sys.platform == 'win32':
extensions = ['.dll', '.a']
elif os.name == 'nt':
extensions = ['.lib', '.dll']
else: # linux like
extensions = ['.so', '.a']

Expand All @@ -250,6 +259,8 @@ def find_library(name, version=None, subpaths=None, prefixes=None,
for ext in extensions:
if sys.platform == 'darwin': # version in the middle
libname = 'lib' + name + '.' + version + ext
elif os.name == 'nt':
libname = 'lib' + name + '-' + '_'.join(version.split('.')[:2]) + ext
else: # version at the end
libname = 'lib' + name + ext + '.' + version

Expand Down Expand Up @@ -305,6 +316,11 @@ def find_executable(name, subpaths=None, prefixes=None):

binpaths += ['bin']

# Windows
if os.name == 'nt':
binpaths += [os.path.join('mingw-w64','bin')]
name = name + '.exe'

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You probably want to remove this too. We are trying to add support for msvc not mingw 64 which are incompatible

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think incompatibility applies to executables. Many executables are compiled on MingW and can be used by passing input arguments regardless of how they are compiled. Removing this will cause problems in the future, if not now. The "name = name + '.exe'" is also required as in windows executables need the extension in special cases.

# Exhaustive combination of paths and subpaths
if subpaths:
my_subpaths = []
Expand Down