Skip to content

Commit 929bdb2

Browse files
committed
Allow compiling for x64 mac; Fix a bug where win-arm64 would enable x64 features (like AVX2)
1 parent 2bf3698 commit 929bdb2

5 files changed

Lines changed: 57 additions & 17 deletions

File tree

scripts/build/__main__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,17 @@ def main():
2323
default=None,
2424
help="The variant to build for (e.g., 'sm86' for CUDA). If not specified, the default variant for the platform will be used.",
2525
)
26+
parser.add_argument(
27+
"--macos-min-version",
28+
default=None,
29+
help="Minimum supported macOS version to pass as CMAKE_OSX_DEPLOYMENT_TARGET.",
30+
)
2631
args = parser.parse_args()
2732

2833
platform = args.platform
2934
arch = args.arch
3035
variant = args.variant
36+
macos_min_version = args.macos_min_version
3137
try:
3238
module = globals()[f"platform_{platform}"]
3339
except KeyError:
@@ -52,6 +58,7 @@ def main():
5258
env_func,
5359
arch,
5460
variant,
61+
macos_min_version,
5562
)
5663

5764

scripts/build/common.py

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import re
99

1010
INTERMEDIATE_LIB_PATTERN = re.compile(r".*\.so\.\d+|.*\.\d+\.dylib$") # skip foo.so.1, foo.1.dylib etc
11+
MACOS_CMAKE_ARCH = {"x64": "x86_64", "arm64": "arm64"}
12+
DEFAULT_MACOS_DEPLOYMENT_TARGET = {"x64": "10.14", "arm64": "11.0"}
1113

1214

1315
def get_os():
@@ -34,11 +36,31 @@ def get_arch():
3436
return machine
3537

3638

37-
def configure_cmake(build_dir, source_dir, options=[], env=None):
39+
def get_target_arch(arch=None):
40+
"""Get the requested target architecture or fall back to the host arch."""
41+
return arch if arch else get_arch()
42+
43+
44+
def get_cmake_configure_args(arch=None, macos_min_version=None):
45+
"""Get target-specific CMake configure arguments."""
46+
os_name = get_os()
47+
arch_name = get_target_arch(arch)
48+
cmake_args = []
49+
50+
if os_name == "mac":
51+
cmake_args.append(f"-DCMAKE_OSX_ARCHITECTURES={MACOS_CMAKE_ARCH[arch_name]}")
52+
deployment_target = macos_min_version or DEFAULT_MACOS_DEPLOYMENT_TARGET[arch_name]
53+
cmake_args.append(f"-DCMAKE_OSX_DEPLOYMENT_TARGET={deployment_target}")
54+
55+
return cmake_args
56+
57+
58+
def configure_cmake(build_dir, source_dir, options=None, env=None):
3859
"""Configure CMake with given options."""
60+
options = options or []
3961
options = options + ["-DCMAKE_BUILD_TYPE=Release"]
4062
os.makedirs(build_dir, exist_ok=True)
41-
cmake_cmd = ["cmake", "-S", source_dir, "-B", build_dir] + options
63+
cmake_cmd = ["cmake"] + ["-S", source_dir, "-B", build_dir] + options
4264
print(f"Configuring CMake: {' '.join(cmake_cmd)}")
4365

4466
result = subprocess.run(cmake_cmd, env=env)
@@ -116,7 +138,7 @@ def get_target(get_platform_name_func, variant_name, arch=None):
116138
"""
117139
platform_name = get_platform_name_func()
118140
os_name = get_os()
119-
arch_name = arch if arch else get_arch()
141+
arch_name = get_target_arch(arch)
120142
target = f"{os_name}-{arch_name}-{platform_name}-{variant_name}"
121143
return target
122144

@@ -194,6 +216,7 @@ def build_project(
194216
get_env_func=None,
195217
arch=None,
196218
variant=None,
219+
macos_min_version=None,
197220
):
198221
"""Common build logic for all backends.
199222
@@ -206,6 +229,7 @@ def build_project(
206229
get_additional_files_func: Optional function that returns additional files
207230
get_env_func: Optional function that returns environment dict for cmake
208231
arch: Optional architecture to build for
232+
macos_min_version: Optional macOS deployment target override
209233
"""
210234
# Check if platform module has get_variants function
211235
variants = []
@@ -225,6 +249,7 @@ def build_project(
225249

226250
# Get target for "any" variant to use for additional files
227251
target_any = get_target(get_platform_name_func, "any", arch)
252+
cmake_args = get_cmake_configure_args(arch, macos_min_version)
228253

229254
# Get environment for cmake
230255
env = get_env_func(target_any) if get_env_func else None
@@ -239,7 +264,7 @@ def build_project(
239264

240265
# Combine base compile flags with variant-specific flags
241266
base_options = get_compile_flags_func(target_any)
242-
options = base_options + variant_compile_flags
267+
options = cmake_args + base_options + variant_compile_flags
243268

244269
# Resolve additional files if available
245270
additional_files = get_additional_files_func() if get_additional_files_func else []

scripts/build/platform_metal.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ def check_environment():
1919

2020
def get_compile_flags(target_any):
2121
"""Get compile flags for Metal."""
22-
# Disable GGML_NATIVE and use explicit ARM64 architecture
22+
# Disable GGML_NATIVE; target architecture is set centrally during CMake configure.
2323
# GGML_ACCELERATE enables Apple's optimized framework
24-
return ["-DSD_METAL=ON", "-DGGML_NATIVE=OFF", "-DGGML_ACCELERATE=ON", "-DCMAKE_OSX_ARCHITECTURES=arm64"]
24+
return ["-DSD_METAL=ON", "-DGGML_NATIVE=OFF", "-DGGML_ACCELERATE=ON"]
2525

2626

2727
def get_platform_name():

scripts/build/platform_vulkan.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,17 @@ def get_compile_flags(target_any):
3636
"""Get compile flags for Vulkan."""
3737
# Disable GGML_NATIVE for portable CPU code (works on both Intel and AMD)
3838
# Enable AVX2/FMA/F16C for good performance while maintaining compatibility
39-
flags = [
40-
"-DSD_VULKAN=ON",
41-
"-DGGML_NATIVE=OFF",
42-
"-DGGML_AVX2=ON",
43-
"-DGGML_FMA=ON",
44-
"-DGGML_F16C=ON",
45-
"-DGGML_BMI2=ON",
46-
]
39+
flags = ["-DSD_VULKAN=ON", "-DGGML_NATIVE=OFF"]
40+
41+
if "-x64-" in target_any:
42+
flags.extend(
43+
[
44+
"-DGGML_AVX2=ON",
45+
"-DGGML_FMA=ON",
46+
"-DGGML_F16C=ON",
47+
"-DGGML_BMI2=ON",
48+
]
49+
)
4750

4851
if target_any.startswith("win-arm64"):
4952
vulkan_sdk_path = os.environ["VULKAN_SDK"].replace("\\", "/")

scripts/build_all.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,24 @@
1717
("vulkan", "x64"),
1818
],
1919
"Darwin": [
20-
("metal", "arm64"),
20+
("metal", "arm64", "11.0"),
21+
("metal", "x64", "10.14"),
2122
],
2223
}
2324

2425
print(f"Detected OS: {OS_NAME}")
25-
platforms = BUILD_PLATFORMS.get(OS_NAME, ["cpu"])
26+
platforms = BUILD_PLATFORMS.get(OS_NAME, [("cpu", "x64")])
2627
print(f"Platforms to build for: {platforms}")
2728

2829

2930
def main():
30-
for platform_name, arch in platforms:
31+
for platform_name, arch, *rest in platforms:
32+
macos_min_version = rest[0] if rest else None
3133
cmd = [sys.executable, "-m", "scripts.build", "--platform", platform_name, "--arch", arch]
3234

35+
if macos_min_version:
36+
cmd.extend(["--macos-min-version", macos_min_version])
37+
3338
print(f"Running build script: {cmd}")
3439
result = subprocess.run(cmd)
3540
if result.returncode != 0:

0 commit comments

Comments
 (0)