Skip to content

Commit

Permalink
Support universal libraries on macOS (#11)
Browse files Browse the repository at this point in the history
* Pass through multiarch
* Patch boost
* Build PNG from source
* (opencv) Disable DNN
* Don't make PNG_SHARED/STATIC exclusive
* (libpng) Patch SSE fix into universal PNG code
  • Loading branch information
csparker247 authored Aug 16, 2024
1 parent a0f3122 commit 478979b
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 4 deletions.
14 changes: 13 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.24 FATAL_ERROR)

project(vc-deps VERSION 1.9.1)
project(vc-deps VERSION 1.9.2)

set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/)

Expand All @@ -11,6 +11,9 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)

#### Options ####
option(BUILD_SHARED_LIBS "Build libraries as shared" OFF)
if(APPLE)
option(BUILD_MACOS_MULTIARCH "Build macOS universal libraries (Intel and Apple Silicon)" OFF)
endif()

#### Setup output directory if not specified ####
if (CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
Expand Down Expand Up @@ -42,6 +45,12 @@ if(CMAKE_POSITION_INDEPENDENT_CODE)
)
endif()

if(BUILD_MACOS_MULTIARCH)
list(APPEND GLOBAL_CMAKE_ARGS
"-DCMAKE_OSX_ARCHITECTURES:STRING=x86_64;arm64"
)
endif()

#### Setup a legacy macOS build using an SDK ####
# include(InstallSDK-macOS)

Expand All @@ -52,6 +61,9 @@ include(BuildZLIB)
# libtiff
include(BuildTIFF)

# libpng
include(BuildPNG)

# Boost
include(BuildBoost)

Expand Down
8 changes: 7 additions & 1 deletion cmake/BuildBoost.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ else()
set(BOOST_LINK_TYPE static)
endif()

if(BUILD_MACOS_MULTIARCH)
set(BOOST_ARCH "-arch x86_64 -arch arm64")
else()
set(BOOST_ARCH "-arch ${CMAKE_HOST_SYSTEM_PROCESSOR}")
endif()

# Set Boost toolset type based on the OS
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "AppleClang")
set(BOOST_TOOLSET_AUTO clang)
Expand All @@ -38,7 +44,7 @@ if(VCDEPS_BUILD_BOOST)
string(REPLACE ";" "," BOOST_BUILD_LIBS "${VCDEPS_BOOST_COMPONENTS}")

# Compiler flags
set(BOOST_CXX_FLAGS "cxxflags=-std=c++${CMAKE_CXX_STANDARD}")
set(BOOST_CXX_FLAGS "cxxflags=-std=c++${CMAKE_CXX_STANDARD} ${BOOST_ARCH}")

# Mimic project PIC flag
if(CMAKE_POSITION_INDEPENDENT_CODE)
Expand Down
20 changes: 19 additions & 1 deletion cmake/BuildOpenCV.cmake
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
option(VCDEPS_BUILD_OPENCV "Build OpenCV" ON)

if(VCDEPS_BUILD_PNG)
set(OPENCV_BUILD_PNG OFF)
else()
set(OPENCV_BUILD_PNG ON)
endif()

set(OPENCV_BUILD_DNN ON)
if(BUILD_MACOS_MULTIARCH)
set(OPENCV_BUILD_DNN OFF)
endif()

if(VCDEPS_BUILD_OPENCV)
externalproject_add(
opencv
DEPENDS zlib libtiff ${GLOBAL_DEPENDS}
DEPENDS zlib libtiff libpng ${GLOBAL_DEPENDS}
URL https://github.com/opencv/opencv/archive/4.10.0.tar.gz
URL_HASH SHA512=b4f7248f89f1cd146dbbae7860a17131cd29bd3cb81db1e678abfcfbf2d8fa4a7633bfd0edbf50afae7b838c8700e8c0d0bb05828139d5cb5662df6bbf3eb92c
DOWNLOAD_NO_PROGRESS true
Expand All @@ -12,13 +24,19 @@ externalproject_add(
-DWITH_VTK:BOOL=OFF
-DWITH_CUDA:BOOL=OFF
-DBUILD_TIFF:BOOL=OFF
-DBUILD_PNG:BOOL=${OPENCV_BUILD_PNG}
-DBUILD_ZLIB:BOOL=OFF
-DBUILD_TESTS:BOOL=OFF
-DBUILD_PERF_TESTS:BOOL=OFF
-DBUILD_EXAMPLES:BOOL=OFF
-DWITH_FFMPEG:BOOL=OFF
-DWITH_EIGEN:BOOL=OFF
-DWITH_OPENEXR:BOOL=OFF
-DOBSENSOR_USE_ORBBEC_SDK:BOOL=OFF
-DWITH_OBSENSOR:BOOL=OFF
-DBUILD_opencv_dnn:BOOL=${OPENCV_BUILD_DNN}
-DWITH_PROTOBUF:BOOL=${OPENCV_BUILD_DNN}
-DBUILD_PROTOBUF:BOOL=${OPENCV_BUILD_DNN}
)
else()
find_package(OpenCV 3 QUIET)
Expand Down
33 changes: 33 additions & 0 deletions cmake/BuildPNG.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
option(VCDEPS_BUILD_PNG "Build libpng" ${BUILD_MACOS_MULTIARCH})

if(VCDEPS_BUILD_PNG)

set(PNG_SHARED ${BUILD_SHARED_LIBS})
if(NOT PNG_SHARED)
set(PNG_STATIC ON)
else()
set(PNG_STATIC OFF)
endif()

set(PNG_PATCH_CMD patch -p1 -i ${CMAKE_SOURCE_DIR}/patches/libpng-auto-sse.diff)

externalproject_add(
libpng
DEPENDS zlib ${GLOBAL_DEPENDS}
GIT_REPOSITORY https://github.com/csparker247/libpng.git
GIT_TAG cmake-fix-macos-universal
DOWNLOAD_NO_PROGRESS true
DOWNLOAD_EXTRACT_TIMESTAMP ON
PATCH_COMMAND ${PNG_PATCH_CMD}
CMAKE_CACHE_ARGS
${GLOBAL_CMAKE_ARGS}
-DPNG_TESTS:BOOL=OFF
-DPNG_TOOLS:BOOL=ON
-DPNG_FRAMEWORK:BOOL=OFF
-DPNG_SHARED:BOOL=${PNG_SHARED}
-DPNG_STATIC:BOOL=${PNG_STATIC}
)
else()
# Nothing to do. OpenCV will build it.
add_custom_target(libpng)
endif()
2 changes: 1 addition & 1 deletion patches/boost-macOS-user-config.jam.in
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using clang : ${CMAKE_OSX_DEPLOYMENT_TARGET} : clang++ :
<striper> <root>"${OSX_SDK_SYSROOT}/"
<target-os>"darwin"
<compileflags>"-arch ${CMAKE_HOST_SYSTEM_PROCESSOR} ${BOOST_OSX_SDK}"
<compileflags>"${BOOST_ARCH} ${BOOST_OSX_SDK}"
<cxxflags>"-std=c++${CMAKE_CXX_STANDARD}"
;
26 changes: 26 additions & 0 deletions patches/libpng-auto-sse.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
diff --git a/pngpriv.h b/pngpriv.h
index b59084e7e..ec27ed974 100644
--- a/pngpriv.h
+++ b/pngpriv.h
@@ -187,11 +187,6 @@
#endif

#ifndef PNG_INTEL_SSE_OPT
-# ifdef PNG_INTEL_SSE
- /* Only check for SSE if the build configuration has been modified to
- * enable SSE optimizations. This means that these optimizations will
- * be off by default. See contrib/intel for more details.
- */
# if defined(__SSE4_1__) || defined(__AVX__) || defined(__SSSE3__) || \
defined(__SSE2__) || defined(_M_X64) || defined(_M_AMD64) || \
(defined(_M_IX86_FP) && _M_IX86_FP >= 2)
@@ -199,9 +194,6 @@
# else
# define PNG_INTEL_SSE_OPT 0
# endif
-# else
-# define PNG_INTEL_SSE_OPT 0
-# endif
#endif

#if PNG_INTEL_SSE_OPT > 0

0 comments on commit 478979b

Please sign in to comment.