Skip to content

Commit

Permalink
simplify GH action and move compiler options to the setup file. clear…
Browse files Browse the repository at this point in the history
… current compiled files
  • Loading branch information
jfranmatheu committed Feb 12, 2025
1 parent cadcec0 commit 480249c
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 112 deletions.
72 changes: 5 additions & 67 deletions .github/workflows/build_cython.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,79 +47,17 @@ jobs:
- name: Build Cython extensions
shell: bash
env:
ARCHFLAGS: ${{ matrix.target == 'intel' && '-arch x86_64' || '' }}
CC: ${{ matrix.target == 'intel' && 'gcc -arch x86_64' || 'gcc' }}
CXX: ${{ matrix.target == 'intel' && 'g++ -arch x86_64' || 'g++' }}
ARCHFLAGS: ${{ matrix.target == 'intel' && '-arch x86_64' || matrix.target == 'native' && '-arch arm64' || '' }}
run: |
if [ "${{ matrix.os }}" == "macos-latest" ] && [ "${{ matrix.target }}" == "intel" ]; then
# Install Rosetta 2 if needed
softwareupdate --install-rosetta --agree-to-license || true
# Use homebrew to install cross-compilation tools
brew install gcc
python -m pip install --upgrade pip
python -m pip install cython numpy setuptools wheel
fi
python -m pip install --upgrade pip
python -m pip install cython numpy setuptools wheel
python cy_setup.py build_ext --inplace
- name: Rename compiled extensions
- name: List compiled extensions
shell: bash
run: |
cd retopoflow/cy
echo "Files before renaming:"
ls -la *.so || ls -la *.pyd
if [ "${{ matrix.os }}" == "windows-latest" ]; then
echo "Windows build - keeping original names"
elif [ "${{ matrix.os }}" == "macos-latest" ]; then
# Set architecture based on build target
if [ "${{ matrix.target }}" == "intel" ]; then
arch="x86_64"
elif [ "${{ matrix.target }}" == "native" ]; then
arch=$(uname -m)
fi
echo "Target architecture: ${arch}"
# Process all .so files
for f in *.so; do
if [ -f "$f" ]; then
base_name="${f%.cpython*}"
target_name="${base_name}.cpython-311-darwin-${arch}.so"
# Only rename if the file doesn't already have the correct name
if [ "$f" != "$target_name" ]; then
echo "Renaming $f to $target_name"
# First remove target if it exists
rm -f "$target_name"
# Then move the file
mv "$f" "$target_name"
else
echo "File $f already has correct name"
fi
fi
done
elif [ "${{ matrix.os }}" == "ubuntu-latest" ]; then
arch=$(uname -m)
echo "Linux architecture: ${arch}"
# Process all .so files
for f in *.so; do
if [ -f "$f" ]; then
base_name="${f%.cpython*}"
target_name="${base_name}.cpython-311-linux-${arch}.so"
# Only rename if the file doesn't already have the correct name
if [ "$f" != "$target_name" ]; then
echo "Renaming $f to $target_name"
# First remove target if it exists
rm -f "$target_name"
# Then move the file
mv "$f" "$target_name"
else
echo "File $f already has correct name"
fi
fi
done
fi
echo "Files after renaming:"
echo "Compiled files:"
ls -la *.so || ls -la *.pyd
- name: Upload compiled extensions
Expand Down
126 changes: 81 additions & 45 deletions cy_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,52 +4,60 @@
import sys
import platform
import os
import subprocess

# Platform-specific compiler flags
compiler_flags = {
'Windows': ['/O2'],
'Darwin': ['-O3'], # macOS
'Linux': ['-O3'],
}

# Get the current platform
current_platform = platform.system()
def build_for_architecture(arch):
"""Build extensions for a specific architecture"""
print(f"Building for architecture: {arch}")

# Base compiler flags for all platforms
compiler_flags = {
'Windows': ['/O2'],
'Darwin': ['-O3'], # macOS
'Linux': ['-O3']
}

# Get base optimization flag for current platform
extra_compile_args = compiler_flags.get(platform.system(), ['-O3'])
extra_link_args = []

# Add architecture flags only for macOS
if platform.system() == 'Darwin' and arch:
if 'ARCHFLAGS' not in os.environ:
os.environ['ARCHFLAGS'] = f'-arch {arch}'
if 'MACOSX_DEPLOYMENT_TARGET' not in os.environ:
os.environ['MACOSX_DEPLOYMENT_TARGET'] = '10.15' # minimum macOS version
extra_compile_args.append(f'-arch {arch}')
extra_link_args.append(f'-arch {arch}')

# Base compiler flags
extra_compile_args = compiler_flags.get(current_platform, ['-O3'])

# Handle macOS cross-compilation
if current_platform == 'Darwin' and os.environ.get('ARCHFLAGS'):
extra_compile_args.extend(os.environ['ARCHFLAGS'].split())

shared_ext_kwargs = {
'extra_compile_args': extra_compile_args,
'language': 'c++'
}

if current_platform == 'Darwin':
shared_ext_kwargs['extra_link_args'] = extra_compile_args

np_ext_kwargs = {
"include_dirs": [np.get_include()],
"define_macros": [('NPY_NO_DEPRECATED_API', 'NPY_1_7_API_VERSION')],
}

ext_modules = [
Extension(
"retopoflow.cy.rfmesh_visibility",
sources=["retopoflow/cy/rfmesh_visibility.pyx"],
**shared_ext_kwargs,
**np_ext_kwargs
),
Extension(
"retopoflow.cy.bmesh_utils",
sources=["retopoflow/cy/bmesh_utils.pyx"],
**shared_ext_kwargs
)
]

try:
shared_ext_kwargs = {
'extra_compile_args': extra_compile_args,
'extra_link_args': extra_link_args,
'language': 'c++'
}

np_ext_kwargs = {
"include_dirs": [np.get_include()],
"define_macros": [('NPY_NO_DEPRECATED_API', 'NPY_1_7_API_VERSION')],
}

# Define extension modules
ext_modules = [
Extension(
f"retopoflow.cy.rfmesh_visibility",
sources=["retopoflow/cy/rfmesh_visibility.pyx"],
**shared_ext_kwargs,
**np_ext_kwargs
),
Extension(
f"retopoflow.cy.bmesh_utils",
sources=["retopoflow/cy/bmesh_utils.pyx"],
**shared_ext_kwargs
)
]

# Build extensions
setup(
name="retopoflow",
packages=find_packages(),
Expand All @@ -62,5 +70,33 @@
}),
zip_safe=False,
)
except Exception as e:
print(f"Error: {e}")

def main():
system = platform.system()

if system == 'Darwin': # macOS
# Check if specific architecture is requested
target_arch = os.environ.get('TARGET_ARCH')

if target_arch:
# Build for specific architecture
build_for_architecture(target_arch)
else:
# Build for both architectures on Apple Silicon Mac
if platform.machine() == 'arm64':
print("Building universal binaries (arm64 + x86_64)")
build_for_architecture('arm64')
build_for_architecture('x86_64')
else:
print("Building only x86_64 binary (Intel Mac)")
build_for_architecture('x86_64')
else:
# For non-macOS platforms, use regular build without arch flags
build_for_architecture(None)

if __name__ == '__main__':
try:
main()
except Exception as e:
print(f"Error: {e}")
sys.exit(1)
Binary file removed retopoflow/cy/bmesh_utils.cp311-win_amd64.pyd
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file removed retopoflow/cy/rfmesh_visibility.cp311-win_amd64.pyd
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 comments on commit 480249c

Please # to comment.