-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbuild_cpp.sh
More file actions
executable file
·103 lines (84 loc) · 2.47 KB
/
build_cpp.sh
File metadata and controls
executable file
·103 lines (84 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/bin/bash
# Build script for C++ Ray Tracer Module
set -e
echo "========================================"
echo "Building C++ Ray Tracer Module"
echo "========================================"
# Detect OS
UNAME_S=$(uname -s)
echo "Platform: $UNAME_S"
# Python
PYTHON=${PYTHON:-python3}
echo "Python: $PYTHON"
# Check pybind11
if ! $PYTHON -c "import pybind11" 2>/dev/null; then
echo "Error: pybind11 not installed. Run: pip install pybind11"
exit 1
fi
echo "✓ pybind11 found"
# Detect compiler
if [ "$UNAME_S" = "Darwin" ]; then
CXX=clang++
echo "✓ Compiler: clang++ (macOS)"
else
if command -v g++ &> /dev/null; then
CXX=g++
elif command -v clang++ &> /dev/null; then
CXX=clang++
else
echo "Error: No C++ compiler found"
exit 1
fi
echo "✓ Compiler: $CXX"
fi
$CXX --version | head -1
# Base flags
CXXFLAGS="-O3 -std=c++14 -fPIC -Wall -Wextra -Wno-unused-parameter"
# Python includes
PYTHON_INCLUDES=$($PYTHON -m pybind11 --includes 2>/dev/null)
if [ -z "$PYTHON_INCLUDES" ]; then
PYBIND_INC=$($PYTHON -c "import pybind11; print(pybind11.get_include())")
PYTHON_INCLUDES="-I$PYBIND_INC"
PYTHON_INCLUDES="$PYTHON_INCLUDES $($PYTHON-config --includes 2>/dev/null || echo "")"
fi
echo "✓ Python includes configured"
# Platform-specific flags
if [ "$UNAME_S" = "Darwin" ]; then
# macOS
LDFLAGS="-shared -undefined dynamic_lookup"
# Check for OpenMP
if brew --prefix libomp &> /dev/null; then
LIBOMP_PREFIX=$(brew --prefix libomp)
CXXFLAGS="$CXXFLAGS -Xpreprocessor -fopenmp -I$LIBOMP_PREFIX/include"
LDFLAGS="$LDFLAGS -L$LIBOMP_PREFIX/lib -lomp"
echo "✓ OpenMP: $LIBOMP_PREFIX"
else
echo "⚠ OpenMP not found (optional). Install: brew install libomp"
fi
else
# Linux
CXXFLAGS="$CXXFLAGS -fopenmp"
LDFLAGS="-shared -fopenmp"
echo "✓ OpenMP enabled (Linux)"
fi
# Build
echo ""
echo "Compiling..."
cd cpp
# Clean old files
rm -f *.o *.so
echo " → raytracer.cpp"
$CXX $CXXFLAGS $PYTHON_INCLUDES -c raytracer.cpp -o raytracer.o
echo " → binding.cpp"
$CXX $CXXFLAGS $PYTHON_INCLUDES -c binding.cpp -o binding.o
echo " → Linking..."
$CXX $LDFLAGS -o raytracer_cpp.so raytracer.o binding.o
# Copy to parent
cp raytracer_cpp.so ..
echo "✓ Created: ../raytracer_cpp.so"
cd ..
echo ""
echo "========================================"
echo "Build successful!"
echo "========================================"
echo "Run: python main_cpp.py"