Skip to content

Commit 85b0309

Browse files
committed
cmake: set openmp strategy for Python on Windows
1 parent bc0eff3 commit 85b0309

File tree

1 file changed

+36
-5
lines changed

1 file changed

+36
-5
lines changed

bindings/python/CMakeLists.txt

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,13 +130,44 @@ add_header_group(PYWRAP_HEADERS)
130130
add_source_group(PYWRAP_SOURCES)
131131

132132
# --- INSTALL SCRIPTS
133-
set(PYTHON_FILES __init__.py)
134133

135-
foreach(python ${PYTHON_FILES})
136-
python_build(${PROJECT_NAME} ${python})
137-
install(FILES "${CMAKE_CURRENT_SOURCE_DIR}/proxsuite/${python}"
134+
# On Windows, we need to enforce the environment variable KMP_DUPLICATE_LIB_OK
135+
# to True to to allow the program to continue to execute with OpenMP support
136+
if(WIN32
137+
AND CMAKE_CXX_COMPILER_ID STREQUAL "Clang"
138+
AND BUILD_WITH_OPENMP_SUPPORT)
139+
set(OPENMP_KMP_DUPLICATE_LIB_OK_SCRIPT
140+
"import os\n" "os.environ[\"KMP_DUPLICATE_LIB_OK\"] = \"1\"\n\n")
141+
142+
if(CMAKE_GENERATOR MATCHES "Visual Studio|Xcode")
143+
set(PYTHON_MODULE_DIR "${CMAKE_CURRENT_BINARY_DIR}/proxsuite/$<CONFIG>")
144+
else()
145+
set(PYTHON_MODULE_DIR "${CMAKE_CURRENT_BINARY_DIR}/proxsuite")
146+
endif()
147+
148+
set(original_init_dot_py_file ${CMAKE_CURRENT_LIST_DIR}/proxsuite/__init__.py)
149+
set(generated_init_dot_py_file
150+
${CMAKE_CURRENT_BINARY_DIR}/proxsuite/__init__.py)
151+
set(generated_init_dot_pyc_file ${PYTHON_MODULE_DIR}/__init__.pyc)
152+
153+
# Copy content of the __init__.py file
154+
file(READ ${original_init_dot_py_file} INIT_CONTENT)
155+
# Create a new __init__.py file containing both the content of __init__.py
156+
# prepended with the OPENMP_KMP_DUPLICATE_LIB_OK_SCRIPT content
157+
file(WRITE ${generated_init_dot_py_file}
158+
${OPENMP_KMP_DUPLICATE_LIB_OK_SCRIPT})
159+
file(APPEND ${generated_init_dot_py_file} ${INIT_CONTENT})
160+
161+
python_build_file(${generated_init_dot_py_file}
162+
${generated_init_dot_pyc_file})
163+
install(FILES "${generated_init_dot_py_file}"
164+
DESTINATION ${${PYWRAP}_INSTALL_DIR})
165+
166+
else()
167+
python_build(${PROJECT_NAME} __init__.py)
168+
install(FILES "${CMAKE_CURRENT_SOURCE_DIR}/proxsuite/__init__.py"
138169
DESTINATION ${${PYWRAP}_INSTALL_DIR})
139-
endforeach(python)
170+
endif()
140171

141172
python_build_get_target(compile_pyc)
142173
add_dependencies(python ${compile_pyc})

0 commit comments

Comments
 (0)