Skip to content

Commit

Permalink
Set shared library suffixes, prefixes and folders for windows
Browse files Browse the repository at this point in the history
  • Loading branch information
Caspar Gruijthuijsen committed Sep 18, 2023
1 parent 55a75f7 commit 834893c
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 24 deletions.
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ if(CMAKE_CXX_COMPILER_ID MATCHES "GNU" AND NOT CMAKE_SYSTEM_NAME MATCHES "Window
endif()

if(CMAKE_CXX_COMPILER_ID MATCHES "GNU" AND CMAKE_SYSTEM_NAME MATCHES "Windows")
# MinGW, change to .lib such that mex recognizes it
# MinGW: remove prefix and change suffix to match MSVC
# (such that Matlab mex recognizes the libraries)
set(CMAKE_SHARED_LIBRARY_PREFIX "")
set(CMAKE_STATIC_LIBRARY_SUFFIX ".lib")
set(CMAKE_STATIC_LIBRARY_PREFIX "")
if(BUILD_SHARED_LIBS MATCHES ON)
Expand Down
4 changes: 2 additions & 2 deletions interfaces/acados_template/acados_template/acados_ocp.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import numpy as np
import os
from .acados_model import AcadosModel
from .utils import get_acados_path, J_to_idx, J_to_idx_slack, get_lib_ext
from .utils import get_acados_path, J_to_idx, J_to_idx_slack, get_shared_lib_ext

class AcadosOcpDims:
"""
Expand Down Expand Up @@ -3183,7 +3183,7 @@ def __init__(self, acados_path=''):
"""Path to acados include directory (set automatically), type: `string`"""
self.acados_lib_path = os.path.join(acados_path, 'lib').replace(os.sep, '/') # the replace part is important on Windows for CMake
"""Path to where acados library is located, type: `string`"""
self.shared_lib_ext = get_lib_ext()
self.shared_lib_ext = get_shared_lib_ext()

# get cython paths
from sysconfig import get_paths
Expand Down
12 changes: 6 additions & 6 deletions interfaces/acados_template/acados_template/acados_ocp_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@
generate_c_code_nls_cost)
from .gnsf.detect_gnsf_structure import detect_gnsf_structure
from .utils import (casadi_length, check_casadi_version, format_class_dict,
get_lib_ext, get_ocp_nlp_layout, get_python_interface_path,
get_shared_lib_ext, get_shared_lib_prefix, get_shared_lib_dir,
get_ocp_nlp_layout, get_python_interface_path,
is_column, is_empty, make_model_consistent,
make_object_json_dumpable, render_template,
set_up_imported_gnsf_model, verbose_system_call)
Expand Down Expand Up @@ -969,18 +970,17 @@ def __init__(self, acados_ocp: AcadosOcp, json_file='acados_ocp_nlp.json', simul
self.build(code_export_directory, with_cython=False, cmake_builder=cmake_builder, verbose=verbose)

# prepare library loading
lib_prefix = 'lib'
lib_ext = get_lib_ext()
if os.name == 'nt':
lib_prefix = ''
lib_ext = get_shared_lib_ext()
lib_prefix = get_shared_lib_prefix()
lib_dir = get_shared_lib_dir()

# Load acados library to avoid unloading the library.
# This is necessary if acados was compiled with OpenMP, since the OpenMP threads can't be destroyed.
# Unloading a library which uses OpenMP results in a segfault (on any platform?).
# see [https://stackoverflow.com/questions/34439956/vc-crash-when-freeing-a-dll-built-with-openmp]
# or [https://python.hotexamples.com/examples/_ctypes/-/dlclose/python-dlclose-function-examples.html]
libacados_name = f'{lib_prefix}acados{lib_ext}'
libacados_filepath = os.path.join(acados_lib_path, libacados_name)
libacados_filepath = os.path.join(acados_lib_path, '..', lib_dir, libacados_name)
self.__acados_lib = DllLoader(libacados_filepath, winmode=self.winmode)

# find out if acados was compiled with OpenMP
Expand Down
4 changes: 2 additions & 2 deletions interfaces/acados_template/acados_template/acados_sim.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import numpy as np
import os
from .acados_model import AcadosModel
from .utils import get_acados_path, get_lib_ext
from .utils import get_acados_path, get_shared_lib_ext

class AcadosSimDims:
"""
Expand Down Expand Up @@ -332,7 +332,7 @@ def __init__(self, acados_path=''):

self.code_export_directory = 'c_generated_code'
"""Path to where code will be exported. Default: `c_generated_code`."""
self.shared_lib_ext = get_lib_ext()
self.shared_lib_ext = get_shared_lib_ext()

# get cython paths
from sysconfig import get_paths
Expand Down
12 changes: 6 additions & 6 deletions interfaces/acados_template/acados_template/acados_sim_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@
generate_c_code_implicit_ode)
from .gnsf.detect_gnsf_structure import detect_gnsf_structure
from .utils import (casadi_length, check_casadi_version, format_class_dict,
get_lib_ext, get_python_interface_path, is_column,
get_shared_lib_ext, get_shared_lib_prefix, get_shared_lib_dir,
get_python_interface_path, is_column,
is_empty, make_model_consistent, make_object_json_dumpable,
render_template, set_up_imported_gnsf_model,
verbose_system_call)
Expand Down Expand Up @@ -297,18 +298,17 @@ def __init__(self, acados_sim: AcadosSim, json_file='acados_sim.json', generate=
self.build(code_export_dir, cmake_builder=cmake_builder, verbose=verbose)

# prepare library loading
lib_prefix = 'lib'
lib_ext = get_lib_ext()
if os.name == 'nt':
lib_prefix = ''
lib_ext = get_shared_lib_ext()
lib_prefix = get_shared_lib_prefix()
lib_dir = get_shared_lib_dir()

# Load acados library to avoid unloading the library.
# This is necessary if acados was compiled with OpenMP, since the OpenMP threads can't be destroyed.
# Unloading a library which uses OpenMP results in a segfault (on any platform?).
# see [https://stackoverflow.com/questions/34439956/vc-crash-when-freeing-a-dll-built-with-openmp]
# or [https://python.hotexamples.com/examples/_ctypes/-/dlclose/python-dlclose-function-examples.html]
libacados_name = f'{lib_prefix}acados{lib_ext}'
libacados_filepath = os.path.join(acados_sim.acados_lib_path, libacados_name)
libacados_filepath = os.path.join(acados_sim.acados_lib_path, '..', lib_dir, libacados_name)
self.__acados_lib = DllLoader(libacados_filepath, winmode=self.winmode)

# find out if acados was compiled with OpenMP
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,11 @@ option(BUILD_ACADOS_SIM_SOLVER_LIB "Should the simulation solver library acados_


if(CMAKE_CXX_COMPILER_ID MATCHES "GNU" AND CMAKE_SYSTEM_NAME MATCHES "Windows")
# MinGW, change to .lib such that mex recognizes it
set(CMAKE_SHARED_LIBRARY_SUFFIX ".lib")
# MinGW: remove prefix and change suffix to match MSVC
# (such that Matlab mex recognizes the libraries)
set(CMAKE_SHARED_LIBRARY_PREFIX "")
set(CMAKE_STATIC_LIBRARY_SUFFIX ".lib")
set(CMAKE_STATIC_LIBRARY_PREFIX "")
endif()


Expand Down
21 changes: 16 additions & 5 deletions interfaces/acados_template/acados_template/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,14 +165,25 @@ def make_model_consistent(model):

return model

def get_lib_ext():
lib_ext = '.so'
def get_shared_lib_ext():
if sys.platform == 'darwin':
lib_ext = '.dylib'
return '.dylib'
elif os.name == 'nt':
lib_ext = ''
return '.dll'
else:
return '.so'

def get_shared_lib_dir():
if os.name == 'nt':
return 'bin'
else:
return 'lib'

return lib_ext
def get_shared_lib_prefix():
if os.name == 'nt':
return ''
else:
return 'lib'

def get_tera():
tera_path = get_tera_exec_path()
Expand Down

0 comments on commit 834893c

Please sign in to comment.