Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

macOS Apple Silicon support #415

Merged
merged 9 commits into from
May 19, 2021
46 changes: 42 additions & 4 deletions BUILDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ KTX consist of the following parts
Supported platforms (please to through their specific requirements first)

- [GNU/Linux](#gnulinux)
- [iOS and macOS](#ios-and-macos)
- [Apple macOS/iOS](#apple-macosios)
- [Web/Emscripten](#webemscripten)
- [Windows 64-bit](#windows-x64)

Expand Down Expand Up @@ -98,7 +98,7 @@ cmake . -G Ninja -B build -DCMAKE_BUILD_TYPE=Release -DKTX_FEATURE_LOADTEST_APPS
cmake --build build
```

### iOS and macOS
### Apple macOS/iOS

You need to install the following

Expand All @@ -110,6 +110,10 @@ For the load tests applications you need to install the [Vulkan SDK](#vulkan-sdk

Other dependencies (like zstd, SDL2 or the assimp library are included in this repository or come with Xcode).

**NOTE:** the `iphoneos` or `MacOSX` SDK version gets hardwired into the generated projects. After installing an Xcode update that has the SDK for a new version of iOS, builds will fail. The only way to remedy this is to delete the build folder and regenerate from scratch.

#### macOS

To build for macOS:

```bash
Expand All @@ -123,6 +127,37 @@ cmake -GXcode -Bbuild/mac -DKTX_FEATURE_LOADTEST_APPS=ON -DVULKAN_INSTALL_DIR="$
# Compile the project
cmake --build build/mac
```
##### Apple Silicon and Universal Binaries

Macs are either based on Intel or the newer Apple Silicon architecture. By default CMake configures to build for your host's platform, whichever it is. If you want to cross compile universal binaries (that support both platforms), add the parameter `-DCMAKE_OSX_ARCHITECTURES="\$(ARCHS_STANDARD)"` to cmake.

> **Known limitations:**
> - Load tests apps are not supported on native Apple Silicon and cannot be cross compiled for Intel either
atteneder marked this conversation as resolved.
Show resolved Hide resolved
> - Intel Macs have support for SSE, but if you're building universal binaries, you have to disable SSE or the build will fail

Example how to build universal binaries

```bash
# Configure universal binaries and disable SSE
cmake -G Xcode -B build-macos-universal -DCMAKE_OSX_ARCHITECTURES="\$(ARCHS_STANDARD)" -DBASISU_SUPPORT_SSE=OFF
# Build
cmake --build build-macos-universal
# Easy way to check if the resulting binaries are universal

file build-macos-universal/Debug/libktx.dylib
# outputs:
# build-macos-universal/Debug/libktx.dylib: Mach-O universal binary with 2 architectures: [x86_64:Mach-O 64-bit dynamically linked shared library x86_64] [arm64]
# build-macos-universal/Debug/libktx.dylib (for architecture x86_64): Mach-O 64-bit dynamically linked shared library x86_64
# build-macos-universal/Debug/libktx.dylib (for architecture arm64): Mach-O 64-bit dynamically linked shared library arm64

file build-macos-universal/Debug/toktx
# outputs:
# build-macos-universal/Debug/toktx: Mach-O universal binary with 2 architectures: [x86_64:Mach-O 64-bit executable x86_64] [arm64:Mach-O 64-bit executable arm64]
# build-macos-universal/Debug/toktx (for architecture x86_64): Mach-O 64-bit executable x86_64
# build-macos-universal/Debug/toktx (for architecture arm64): Mach-O 64-bit executable arm64
```

##### macOS signing

To sign the applications you need to set the following CMake variables:

Expand All @@ -138,6 +173,8 @@ To sign the installation package you need to set the following variables:
| PRODUCTBUILD\_IDENTITY\_NAME | Owner of the _Developer ID Installer_ certificate to use for signing |
| PRODUCTBUILD\_KEYCHAIN\_PATH | Path to the keychain file with the certificate. Blank if its in the default keychain.

#### iOS

To build for iOS:

```bash
Expand All @@ -151,8 +188,11 @@ cmake -G Xcode -B build/ios -DKTX_FEATURE_LOADTEST_APPS=ON -DVULKAN_INSTALL_DIR=
# Compile the project
cmake --build build -- -sdk iphoneos
```

If using the CMake GUI, when it asks you to specify the generator for the project, you need to check _Specify options for cross-compiling_ and on the next screen make sure _Operating System_ is set to `iOS`.

##### iOS signing

To sign the applications you need to set the following CMake variables:

| Name | Value |
Expand All @@ -161,8 +201,6 @@ To sign the applications you need to set the following CMake variables:
| XCODE\_DEVELOPMENT\_TEAM | Development team of the certificate owner
| XCODE\_PROVISIONING\_PROFILE | Name of the profile to use.

**NOTE:** the `iphoneos` SDK version gets hardwired into the generated projects. After installing an Xcode update that has the SDK for a new version of iOS, builds will fail. The only way to remedy this is to delete the build folder and regenerate from scratch.

### Web/Emscripten

There are two ways to build the Web version of the software: using Docker or using your own Emscripten installation.
Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@ if(EMSCRIPTEN)
)
endif()

add_library( objUtil OBJECT
add_library( objUtil STATIC
utils/argparser.cpp
utils/argparser.h
utils/ktxapp.h
Expand Down
7 changes: 6 additions & 1 deletion ci_scripts/build_macos.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,19 @@ echo "Configure KTX-Software (macOS)"
if [ -n "$MACOS_CERTIFICATES_P12" ]; then
cmake -GXcode -Bbuild-macos \
-DKTX_FEATURE_DOC=ON \
-DCMAKE_OSX_ARCHITECTURES="\$(ARCHS_STANDARD)" \
-DBASISU_SUPPORT_SSE=OFF \
-DKTX_FEATURE_LOADTEST_APPS=ON -DVULKAN_INSTALL_DIR="${VULKAN_INSTALL_DIR}" \
-DXCODE_CODE_SIGN_IDENTITY="${CODE_SIGN_IDENTITY}" \
-DXCODE_DEVELOPMENT_TEAM="${DEVELOPMENT_TEAM}" \
-DPRODUCTBUILD_IDENTITY_NAME="${PKG_SIGN_IDENTITY}"
else # No secure variables means a PR or fork build.
cmake -GXcode -Bbuild-macos \
-DKTX_FEATURE_DOC=ON \
-DKTX_FEATURE_LOADTEST_APPS=ON -DVULKAN_INSTALL_DIR="${VULKAN_INSTALL_DIR}"
atteneder marked this conversation as resolved.
Show resolved Hide resolved
-DCMAKE_OSX_ARCHITECTURES="\$(ARCHS_STANDARD)" \
-DBASISU_SUPPORT_SSE=OFF \
-DKTX_FEATURE_LOADTEST_APPS=ON \
-DVULKAN_INSTALL_DIR="${VULKAN_INSTALL_DIR}"
fi

echo "Configure KTX-Software (macOS) without SSE support"
Expand Down
11 changes: 11 additions & 0 deletions cmake/cputypetest.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,20 @@ armv8
#endif
#elif TARGET_OS_MAC

#if defined __x86_64__

#undef x86_64
x86_64

#elif defined __aarch64__

#undef arm64
arm64

#else
#error Unsupported platform
#endif

#else
#error Unsupported platform
#endif
Expand Down
11 changes: 1 addition & 10 deletions interface/basisu_c_binding/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
# Copyright 2017-2020 The Khronos Group Inc.
# SPDX-License-Identifier: Apache-2.0

add_library(obj_basisu_cbind OBJECT
add_library(obj_basisu_cbind STATIC
inc/basisu_c_binding.h
src/basisu_c_binding.cpp
)

if(WIN32)
# The Windows ktx.dll seem to not include needed symbols from basisu_transcoder.cpp
# This is a workaround to get it linking
target_sources(obj_basisu_cbind
PRIVATE
${PROJECT_SOURCE_DIR}/lib/basisu/transcoder/basisu_transcoder.cpp
)
endif()

target_compile_features(obj_basisu_cbind PUBLIC c_std_99 cxx_std_11)

target_include_directories(
Expand Down
6 changes: 5 additions & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@

if(KTX_FEATURE_LOADTEST_APPS)
# Test apps that load and display various KTX textures
add_subdirectory(loadtests)
if(APPLE AND CPU_ARCHITECTURE STREQUAL arm64)
message(WARNING "Building load test apps is not supported on Apple Silicon as host platform, so they are skipped")
else()
add_subdirectory(loadtests)
endif()
endif()

# gtest based unit-tests
Expand Down
4 changes: 2 additions & 2 deletions tests/loadtests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ function( ensure_runtime_dependencies_windows target )
)
endfunction()

add_library( appfwSDL OBJECT
add_library( appfwSDL STATIC
appfwSDL/AppBaseSDL.cpp
appfwSDL/AppBaseSDL.h
appfwSDL/main.cpp
Expand Down Expand Up @@ -97,7 +97,7 @@ PUBLIC
geom
)

add_library( GLAppSDL OBJECT
add_library( GLAppSDL STATIC
appfwSDL/GLAppSDL.cpp
appfwSDL/GLAppSDL.h
glloadtests/GLLoadTests.cpp
Expand Down
2 changes: 2 additions & 0 deletions tests/loadtests/glloadtests.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ function( create_gl_target target sources KTX_GL_CONTEXT_PROFILE KTX_GL_CONTEXT_
if(NOT IOS)
set_target_properties( ${target} PROPERTIES
INSTALL_RPATH "@executable_path/../Frameworks"
# No Apple silicon support yet, so restrict archs to Intel
XCODE_ATTRIBUTE_ARCHS x86_64
)
add_custom_command( TARGET ${target} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:ktx> "$<TARGET_BUNDLE_CONTENT_DIR:${target}>/Frameworks/$<TARGET_FILE_NAME:ktx>"
Expand Down
6 changes: 4 additions & 2 deletions tests/loadtests/vkloadtests.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,6 @@ add_custom_target(

add_executable( vkloadtests
${EXE_FLAG}
$<TARGET_OBJECTS:appfwSDL>
$<TARGET_OBJECTS:objUtil>
appfwSDL/VulkanAppSDL/VulkanAppSDL.cpp
appfwSDL/VulkanAppSDL/VulkanAppSDL.h
appfwSDL/VulkanAppSDL/vulkancheckres.h
Expand Down Expand Up @@ -132,6 +130,8 @@ target_link_libraries(
vkloadtests
ktx
${KTX_ZLIB_LIBRARIES}
objUtil
appfwSDL
)

if(IOS)
Expand Down Expand Up @@ -245,6 +245,8 @@ if(APPLE)
if(NOT IOS)
set_target_properties( vkloadtests PROPERTIES
INSTALL_RPATH "@executable_path/../Frameworks"
# No Apple silicon support yet, so restrict archs to Intel
XCODE_ATTRIBUTE_ARCHS x86_64
)
add_custom_command( TARGET vkloadtests POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy "${VULKAN_SDK}/lib/libMoltenVK.dylib" "$<TARGET_BUNDLE_CONTENT_DIR:vkloadtests>/Frameworks/libMoltenVK.dylib"
Expand Down
17 changes: 16 additions & 1 deletion tests/transcodetests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

add_executable( transcodetests
transcodetests.cc
$<TARGET_OBJECTS:obj_basisu_cbind>
)

target_include_directories(
Expand All @@ -19,6 +18,7 @@ target_link_libraries(
transcodetests
gtest
ktx
obj_basisu_cbind
${CMAKE_THREAD_LIBS_INIT}
)

Expand All @@ -33,3 +33,18 @@ gtest_discover_tests( transcodetests
TEST_PREFIX transcodetest
EXTRA_ARGS "${PROJECT_SOURCE_DIR}/tests/testimages/"
)

if(WIN32)
# The Windows ktx.dll seem to not include needed symbols from basisu_transcoder.cpp
# This is a workaround to get it linking
add_library(obj_tmp_basisu OBJECT
${PROJECT_SOURCE_DIR}/lib/basisu/transcoder/basisu_transcoder.cpp
${PROJECT_SOURCE_DIR}/lib/basisu/zstd/zstd.c
)
target_compile_definitions(
obj_tmp_basisu
PUBLIC # only for basisu_c_binding.
BASISU_NO_ITERATOR_DEBUG_LEVEL
)
target_link_libraries(transcodetests obj_tmp_basisu)
endif()
2 changes: 1 addition & 1 deletion tools/ktx2check/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
# SPDX-License-Identifier: Apache-2.0

add_executable( ktx2check
$<TARGET_OBJECTS:objUtil>
ktx2check.cpp
stdafx.h
)
Expand All @@ -21,6 +20,7 @@ PRIVATE
target_link_libraries(
ktx2check
ktx
objUtil
)
set_tool_properties(ktx2check)
set_xcode_code_sign(ktx2check)
2 changes: 1 addition & 1 deletion tools/ktx2ktx2/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
# SPDX-License-Identifier: Apache-2.0

add_executable( ktx2ktx2
$<TARGET_OBJECTS:objUtil>
ktx2ktx2.cpp
stdafx.h
)
Expand All @@ -21,6 +20,7 @@ PRIVATE
target_link_libraries(
ktx2ktx2
ktx
objUtil
)

set_tool_properties(ktx2ktx2)
Expand Down
2 changes: 1 addition & 1 deletion tools/ktxinfo/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
# SPDX-License-Identifier: Apache-2.0

add_executable( ktxinfo
$<TARGET_OBJECTS:objUtil>
ktxinfo.cpp
stdafx.h
)
Expand All @@ -20,6 +19,7 @@ PRIVATE
target_link_libraries(
ktxinfo
ktx
objUtil
)

set_tool_properties(ktxinfo)
Expand Down
2 changes: 1 addition & 1 deletion tools/ktxsc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
# SPDX-License-Identifier: Apache-2.0

add_executable( ktxsc
$<TARGET_OBJECTS:objUtil>
ktxsc.cpp
stdafx.h
)
Expand All @@ -21,6 +20,7 @@ PRIVATE
target_link_libraries(
ktxsc
ktx
objUtil
)

set_tool_properties(ktxsc)
Expand Down
2 changes: 1 addition & 1 deletion tools/toktx/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
# SPDX-License-Identifier: Apache-2.0

add_executable( toktx
$<TARGET_OBJECTS:objUtil>
image.cc
image.hpp
jpgimage.cc
Expand All @@ -28,6 +27,7 @@ PRIVATE
target_link_libraries(
toktx
ktx
objUtil
)

target_compile_definitions(
Expand Down