Skip to content

Commit

Permalink
fix: fix cross-compiling for ARM64 on x64 Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
XuehaiPan committed Jan 15, 2025
1 parent 9d3ba8b commit 6b78c6a
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 4 deletions.
42 changes: 41 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ message(STATUS "Use C++ standard: C++${CMAKE_CXX_STANDARD}")
set(CMAKE_POSITION_INDEPENDENT_CODE ON) # -fPIC
set(CMAKE_CXX_VISIBILITY_PRESET hidden) # -fvisibility=hidden

string(STRIP "${CMAKE_CXX_FLAGS}" CMAKE_CXX_FLAGS)
string(STRIP "${CMAKE_CXX_FLAGS_DEBUG}" CMAKE_CXX_FLAGS_DEBUG)
string(STRIP "${CMAKE_CXX_FLAGS_RELEASE}" CMAKE_CXX_FLAGS_RELEASE)

if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
if(NOT DEFINED _GLIBCXX_USE_CXX11_ABI AND NOT "$ENV{_GLIBCXX_USE_CXX11_ABI}" STREQUAL "")
set(_GLIBCXX_USE_CXX11_ABI "$ENV{_GLIBCXX_USE_CXX11_ABI}")
Expand Down Expand Up @@ -103,6 +107,20 @@ if(OPTREE_CXX_WERROR)
endif()
endif()

string(STRIP "${CMAKE_CXX_FLAGS}" CMAKE_CXX_FLAGS)
string(STRIP "${CMAKE_CXX_FLAGS_DEBUG}" CMAKE_CXX_FLAGS_DEBUG)
string(STRIP "${CMAKE_CXX_FLAGS_RELEASE}" CMAKE_CXX_FLAGS_RELEASE)
message(STATUS "CXX flags: \"${CMAKE_CXX_FLAGS}\"")
message(STATUS "CXX flags (Debug): \"${CMAKE_CXX_FLAGS_DEBUG}\"")
message(STATUS "CXX flags (Release): \"${CMAKE_CXX_FLAGS_RELEASE}\"")

if(MSVC AND NOT "$ENV{VSCMD_ARG_TGT_ARCH}" STREQUAL "")
message(STATUS "Use VSCMD_ARG_TGT_ARCH: \"$ENV{VSCMD_ARG_TGT_ARCH}\"")
endif()
if(NOT "$ENV{SETUPTOOLS_EXT_SUFFIX}" STREQUAL "")
message(STATUS "Use SETUPTOOLS_EXT_SUFFIX: \"$ENV{SETUPTOOLS_EXT_SUFFIX}\"")
endif()

string(LENGTH "${CMAKE_SOURCE_DIR}/" SOURCE_PATH_PREFIX_SIZE)
add_definitions("-DSOURCE_PATH_PREFIX_SIZE=${SOURCE_PATH_PREFIX_SIZE}")

Expand Down Expand Up @@ -164,7 +182,6 @@ system(
message(STATUS "Use Python version: ${Python_VERSION}")
message(STATUS "Use Python executable: \"${Python_EXECUTABLE}\"")

unset(Python_INCLUDE_DIR)
if(NOT DEFINED Python_INCLUDE_DIR)
message(STATUS "Auto detecting Python include directory...")
system(
Expand All @@ -180,9 +197,27 @@ else()
include_directories("${Python_INCLUDE_DIR}")
endif()

if(DEFINED Python_EXTRA_INCLUDE_DIRS)
message(STATUS "Use Python_EXTRA_INCLUDE_DIRS: \"${Python_EXTRA_INCLUDE_DIRS}\"")
foreach(Python_EXTRA_INCLUDE_DIR IN LISTS Python_EXTRA_INCLUDE_DIRS)
include_directories("${Python_EXTRA_INCLUDE_DIR}")
endforeach()
endif()
if(DEFINED Python_EXTRA_LIBRARY_DIRS)
message(STATUS "Use Python_EXTRA_LIBRARY_DIRS: \"${Python_EXTRA_LIBRARY_DIRS}\"")
list(PREPEND CMAKE_PREFIX_PATH "${Python_EXTRA_LIBRARY_DIRS}")
foreach(Python_EXTRA_LIBRARY_DIR IN LISTS Python_EXTRA_LIBRARY_DIRS)
link_directories("${Python_EXTRA_LIBRARY_DIR}")
endforeach()
endif()
if(DEFINED Python_EXTRA_LIBRARIES)
message(STATUS "Use Python_EXTRA_LIBRARIES: \"${Python_EXTRA_LIBRARIES}\"")
endif()

# Include pybind11
set(PYBIND11_PYTHON_VERSION "${Python_VERSION}")
set(PYBIND11_FINDPYTHON ON)
set(PYBIND11_PYTHONLIBS_OVERWRITE OFF)

if(NOT DEFINED pybind11_DIR)
message(STATUS "Auto detecting pybind11 CMake directory...")
Expand Down Expand Up @@ -219,5 +254,10 @@ else()
find_package(pybind11 "${pybind11_MINIMUM_VERSION}" CONFIG REQUIRED)
endif()

if(NOT "$ENV{SETUPTOOLS_EXT_SUFFIX}" STREQUAL "")
set(PYTHON_MODULE_EXTENSION "$ENV{SETUPTOOLS_EXT_SUFFIX}")
endif()
message(STATUS "Use PYTHON_MODULE_EXTENSION: \"${PYTHON_MODULE_EXTENSION}\"")

include_directories("${CMAKE_SOURCE_DIR}/include")
add_subdirectory(src)
19 changes: 16 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def __init__(self, name, source_dir='.', target=None, **kwargs):


class cmake_build_ext(build_ext): # noqa: N801
def build_extension(self, ext):
def build_extension(self, ext): # noqa: C901
if not isinstance(ext, CMakeExtension):
super().build_extension(ext)
return
Expand All @@ -44,14 +44,27 @@ def build_extension(self, ext):
f'-DPython_EXECUTABLE={sys.executable}',
f'-DPython_INCLUDE_DIR={sysconfig.get_path("platinclude")}',
]
if self.include_dirs:
cmake_args.append(f'-DPython_EXTRA_INCLUDE_DIRS={";".join(self.include_dirs)}')
if self.library_dirs:
cmake_args.append(f'-DPython_EXTRA_LIBRARY_DIRS={";".join(self.library_dirs)}')
if self.libraries:
cmake_args.append(f'-DPython_EXTRA_LIBRARIES={";".join(self.libraries)}')

if platform.system() == 'Darwin':
# Cross-compile support for macOS - respect ARCHFLAGS if set
archs = re.findall(r'-arch (\S+)', os.getenv('ARCHFLAGS', ''))
if archs:
cmake_args.append(f'-DCMAKE_OSX_ARCHITECTURES={";".join(archs)}')
elif platform.system() == 'Windows' and platform.architecture()[0] == '32bit':
cmake_args.append('-A=Win32')
elif platform.system() == 'Windows':
plat_name_to_cmake_generator = {
'win32': 'Win32',
'win-amd64': 'x64',
'win-arm32': 'ARM',
'win-arm64': 'ARM64',
}
if self.plat_name in plat_name_to_cmake_generator:
cmake_args.append(f'-A={plat_name_to_cmake_generator[self.plat_name]}')

pybind11_dir = os.getenv('pybind11_DIR', '') # noqa: SIM112
if pybind11_dir:
Expand Down
4 changes: 4 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,7 @@ set(
)

pybind11_add_module(_C MODULE THIN_LTO "${optree_csrc}")

if(DEFINED Python_EXTRA_LIBRARIES)
target_link_libraries(_C PRIVATE ${Python_EXTRA_LIBRARIES})
endif()

0 comments on commit 6b78c6a

Please sign in to comment.