How to enable Bitcode for WebRTC iOS framework? - ios

How can I compile WebRTC iOS framework with Bitcode enabled. Currently I have to disable the Bitcode of my project due to WebRTC framework.

You will need to build it yourself.
Something like:
# Clone the depot tools
git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git
# Add the tools to the path
export PATH=$PATH:"`pwd`/depot_tools"
# Download the WebRTC source code
mkdir webrtc_ios
cd webrtc_ios
# This will take some time
fetch --nohooks webrtc_ios
gclient sync
# Let's start building
cd src
# Build the framework, remove --arch "arm64 x64" to build ALL architectures, including 32 bit
tools_webrtc/ios/build_ios_libs.py --bitcode --arch arm64 x64
# The framework is at out_ios_libs/WebRTC.framework
Documentation: https://webrtc.github.io/webrtc-org/native-code/ios/
Update:
Starting with Xcode 14, bitcode is no longer required (see release notes), and no longer supported in submissions.
The google WebRTC neglected to fix an ARM build issue with bitcode for so long, they just waited it out...
the --bitcode option is no longer supported by the build script.
To continue building using my instructions, remove it:
tools_webrtc/ios/build_ios_libs.py --arch arm64 x64

according to the official doc, you have to compile manually. More details there:
main page: https://webrtc.org/native-code/development/
iOS page: https://webrtc.org/native-code/ios/
bottom of the page (last paragraph) includes instructions to build with bitcode support:
To build the framework with bitcode support, pass the --bitcode flag to the script like so
python build_ios_libs.py --bitcode

Related

Bitcode disabled on Carthage dependencies

Scenario
My project (codebase shared for iOS/watchOS/tvOS ) has build settings with ENABLE_BITCODE = YES and takes advantage of fundamental libraries which are not yet supporting bitcode, although quoting Apple documentation in App Thinning
Bitcode is the default, but optional. For watchOS and tvOS apps,
bitcode is required. If you provide bitcode, all apps and frameworks
in the app bundle (all targets in the project) need to include bitcode
I am currently integrating those fundamental libraries with Carthage.
Problem
In order to have a clean build process, I tried to submit some pull requests to the respective owners for enabling the bitcode but, due to the complexity of their codebases, which are working for multiple operating systems/architectures, my pull requests are still in pending: so, to be able to build my own project, I still have to change manually their build settings.
Question
How can I short-circuit the Carthage process for injecting specific build settings (in this case ENABLE_BITCODE = YES) into the relevant libraries?
I found a solution by making a shell script able to erase the disabling of bitcode, in case someone is facing or curious to solve a similar problem, the script is this:
carthage update --platform ios
for D in ./Carthage/Checkouts/*; do
if [ -d "${D}" ]; then
find $D -type d -name \*.xcodeproj -print0 |
while IFS= read -r -d $'\0' folder; do
sed -i '' 's/ENABLE_BITCODE = NO;//g' $folder/project.pbxproj
done
fi
done
carthage build --platform ios
so basically the script's mechanism is:
downloading all the dependencies
for each dependency, find thepbxproj living inside xcodeproj and cut off the string ENABLE_BITCODE = NO
finally building the dependencies to make the .framework
To add to Andrea's answer, make sure you carthage build with --no-use-binaries as the dependency may have a bitcode disabled in its released framework which will be used without --no-use-binaries.

How to build iOS application with supported Architectures 64bit only in Cordova?

This article guide to you How to customize building process of Cordova.
My sample for this article focus to building iOS application and How to setting for application to supported Architectures 64bit only in Corodva.
You can add more extended configuration parameters for xcodebuild command via build-release.xcconfig, build-debug.xcconfig files which is Cordova's build core file.
This article guide you to use the Corodova Hook also, and how to use "before_build" hook action to update -xcconfig parameter of xcodebuild command.
You can catch up an idea of Cordova Hook and deploy your ideas with other hook actions of Cordova.
I hope this article will help many guys Who work with Cordova framework and want to customize the building processing of Cordova.
[INVESTIGATION]
There are configurations in XCode which allow we restrict supported architectures of application. Modify below all settings in Build Settings of Xcode:
Architectures
Build Actives Architectures Only
Valid Architectures
The configuration values of supported architecture 64bit of settings as below:
Architectures = arm64
Build Actives Architectures Only = YES
Valid Architectures = arm64
Cordova building command have not supported setting of extra configuration. That mean we can not build app with extra configuration of application.
Cordova have supported HOOKs mechanism which allow hook to some execution like: build, run, prepare, compile, platform add, platform rm, … So that, we can use hook mechanism to inject our extended process to building process of Cordova.Reference link to http://cordova.apache.org/docs/en/latest/guide/appdev/hooks/index.html
Cordova building mechanism use configuration files build-release.xcconfig, build-debug.xcconfig in platforms/ios/cordova/ folder to setting up extra configuration for xcodebuild command. So that, we can use this files for our customization.
Reference Cordova building core source: platforms/ios/cordova/lib/build.js
The setting of architecture parameters which use for -xcconfig parameter of xcodebuild command:
ARCHS stand for Architectures parameter.
VALID_ARCHS stand for Valid Architectures parameter.
ONLY_ACTIVE_ARCH stand for Build Actives Architectures Only parameter.
[SOLUTION]
By use HOOK mechanism:
We use "before_build" hook action to inject our extended process before building process started.
We create executing job file as *.sh file which use as running script for before_build hook action.
We need to add configuration for hook action to config.xml. Add this configuration to /hooks folder:
<hook src="hooks/before_build.sh" type="before_build" />
By use Corodova's build configuration files:
We add our new configuration as we wanted to configuration files(We should keep original setting of genereated content from Cordova).
Add below settings for supported architectures 64 bits in block of #include "build.xcconfig" and #include "build-extras.xcconfig".
The sample content of build-release.xcconfig file:
#include "build.xcconfig"
CODE_SIGN_IDENTITY = iPhone Distribution
CODE_SIGN_IDENTITY[sdk=iphoneos*] = iPhone Distribution
ONLY_ACTIVE_ARCH = YES
ARCHS = arm64
VALID_ARCHS = arm64
#include "build-extras.xcconfig"
Please note that, we add below configuration for Architectures 64bit supported:
ONLY_ACTIVE_ARCH = YES
ARCHS = arm64
VALID_ARCHS = arm64
Note: The setting of CODE_SIGN_IDENTITY above depend your signing provision profile. In my case is iPhone Distribution.
Create executing job file in shell script *.sh
Main idea is overwriting our customize build configuration files to original build configuration files of Corodva in platforms/ios/cordova/ folder.
You could store your new build configuration files anywhere- it should be store in hooks for easy maintain. I store the build configuration files in hooks/build/ios/ folder.
The content of shell script before_build.sh:
#!/usr/bin/env bash
#Overwrite the configurations files of Cordova
cp "hooks/build/ios/build-debug.xcconfig" "platforms/ios/cordova/";
cp "hooks/build/ios/build-release.xcconfig" "platforms/ios/cordova/";
Execute build app via Cordova's build command:
cordova build ios -device
===
Thank you,
DQ.

Ios - Wrong architecture building WebRTC

I'm building WebRTC for iOS. To do that, I'm using the following bash script:
# Set flags to compile in arm64
export GYP_GENERATORS="ninja"
export GYP_DEFINES="build_with_libjingle=1 build_with_chromium=1 libjingle_objc=1 OS=ios target_arch=arm64"
export GYP_GENERATOR_FLAGS="output_dir=out_ios_arm64"
export GYP_CROSSCOMPILE=1
# Generate metadata for compile
gclient runhooks
# Compile webRTC in Release and verbose mode
ninja -v -C src/out_ios_arm64/Release-iphoneos AppRTCDemo
In the same machine and the same dir, I have compiled for armv7 and now when I try to build with the code above, the library is compiled for armv7. Maybe I have some metadata info in my dir that I'm not removing or something but I understand that if I set target_arch=arm64 in my GYP_DEFINES I should see my library compiled for this arch. Any idea what could be happening?
P.D: I need to build for both (armv7 and arm64) in order to generate with lipo an universal lib.

How to compile OpenCV iOS with ENABLE_BITCODE

When I tried to compile my XCode project with OpenCV 2.4 iOS using XCode 7 + iOS SDK 9, XCode complained that
ld:
'opencv2.framework/opencv2(alloc.o)'
does not contain bitcode. You must rebuild it with bitcode enabled
(Xcode setting ENABLE_BITCODE), obtain an updated library from the
vendor, or disable bitcode for this target. for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see
invocation)
and refused to link. After some googling, it turns out to be because Apple added a new feature named Bitcode for app optimization within App Store. While OpenCV iOS binary hasn't been updated to include Bitcode, it cannot pass the link stage.
Some reference pointed out a temporary solution to disable ENABLE_BITCODE so the linking could be done without Bitcode. This will prevent the app being compiled for Apple Watches because Bitcode is mandatory for Watch Apps. Therefore my question is, are there some (best easy) ways to compile iOS OpenCV with Bitcode enabled? (better with a download link for compiled framework)
After some search and trial, I figured out a way to compile OpenCV iOS from the source with Bitcode. A compiled binary is also provided here: [v3.0] [v2.4]. [Disclaimer: I am not responsible for the integrity of the compiled binary. Use at your own risk.]
The steps of compilation is basically the same as the official document, with only one extra step.
Download the code with git:
cd ~/<my_working_directory>
git clone https://github.com/Itseez/opencv.git
Make symbolic link for Xcode to let OpenCV build scripts find the compiler, header files etc.
cd /
sudo ln -s /Applications/Xcode.app/Contents/Developer Developer
[Key Step] Change the compilation script to add the extra option for Bitcode: edit ~/<my_working_directory>/opencv/platform/ios/build_framework.py, and locate the line containing -DCMAKE_C_FLAGS. Add a flag of -fembed-bitcode. For example, in the source I got, it's line 55, and will look like
"-DCMAKE_C_FLAGS=\"-Wno-implicit-function-declaration -fembed-bitcode\" " +
after the change. [ref]
Build OpenCV framework:
cd ~/<my_working_directory>
python opencv/platforms/ios/build_framework.py ios
If everything’s fine, a few minutes later you will get ~/<my_working_directory>/ios/opencv2.framework. You can add this framework to your Xcode projects.
P.S. Ask a question, even when you already know the answer is encouraged according to this post on Meta Stackchange.
OpenCV is precisely the kind of software (along with audio and video Codecs) that is likely to have hand-rolled ARM NEON optimisations. The documentation suggests that ~40 functions have had this treatment in OpenCV3.0.
If compiling for LLVM bit-code you'll get the generic (less optimised, implemented in C or C++) versions instead.
Use of Bitcode is optional - except when compiling for Apple watch, where it's hard to imagine you'd run computationally complex image processing anyway. If you're bundling a watch app, override the build setting for bitcode on it only.

Linker errors after upgrading Xcode to 4.5.2 and OpenCV to 2.4.3

My project was working just fine until this morning. I was using xcode 4.3, and an older version of OpenCV (I'm not sure about the exact version). OSX was already 10.7.x, but not 10.7.5
Today, after upgrading OSX to 10.7.5, xcode to 4.5.2, and downloading OpenCV 2.4.3, I am getting the following linker errors when trying to build the project:
Undefined symbols for architecture armv7:
"_OBJC_CLASS_$_ALAssetsLibrary", referenced from:
objc-class-ref in opencv2(cap_ios_video_camera.o)
"cv::FeatureDetector::create(std::string const&)", referenced from:
-[ImageAnalyzer detectBlobs:] in ImageAnalyzer.o
"cv::FeatureDetector::detect(cv::Mat const&, std::vector >&, cv::Mat const&) const", referenced from:
-[ImageAnalyzer detectBlobs:] in ImageAnalyzer.o
ld: symbol(s) not found for architecture armv7
clang: error: linker command failed with exit code 1 (use -v to see invocation)
In order to use the new opencv framework I removed the reference to the old framework, and referenced the project to the official prebuilt opencv2.framework downloaded from here.
I also removed the reference to libz.dylib, and added a reference to libc++.dylib instead.
Last step was to update the prefix file to the new framework. The relevant part in the prefix file now looks like this:
#ifdef __cplusplus
#import <opencv2/opencv.hpp>
#endif
It all narrowed down to these 4 linker errors I can't seem to get rid of. I tried using libstdc++.dylib, but I am getting even more errors. I also tried building OpenCV myself as explained here, but I am still getting the same errors as the prebuilt framework.
What did I miss? Is there anything else I need to change in my project?
UPDATE:
As seen here, setting the "C++ Standard Library" to "libc++ (LLVM C++ standard libray with C++ 11 support" yielded only one error:
clang: error: invalid deployment target for -stdlib=libc++ (requires iOS 5.0 or later)
Changing the deployment target to iOS 5 finally got my project to run again.
Does this mean OpenCV 2.4.3 doesn't work on iOS versions older than 5?
steps to compile and run c++ opencv 2.4.4 on mac os x lion 10.7.5 with cmake 2.8.10 and xcode 4.6.1
Having the right tools
download opencv-unix from http://sourceforge.net/projects/opencvlibrary/files/ and untar it wherever
download cmake .dmg from http://www.cmake.org/cmake/resources/software.html and install it
i am assuming you have xcode 4.6 on os x lion which includes the ios sdk 6.1
go to xcode preferences to download and install the Command Line Tools so you have g++ etc.
Use cmake to compile opencv
go to the extracted opencv folder
create a build directory
mkdir build
cd build
cmake -D WITH_TBB=OFF -D BUILD_NEW_PYTHON_SUPPORT=OFF -D BUILD_FAT_JAVA_LIB=OFF -D BUILD_TBB=OFF -D BUILD_EXAMPLES=ON -D CMAKE_CXX_COMPILER=g++ CMAKE_CC_COMPILER=gcc -D CMAKE_OSX_ARCHITECTURES=x86_64 -D BUILD_opencv_java=OFF -G "Unix Makefiles" ..
make -j8
sudo make install
from the build folder, go to bin/ and run one of the tests
./opencv_test_stitching
Create your own c++ opencv xcode project
fire up xcode and create a new xcode project
select Command Line Tool for the type of project under os x
open your project's build settings
under Architectures, set Architecture to 64-bit intel. also set Valid Architectures to x86_64
under Build Options, set Compiler for C/C++ to Default Compiler
under Search Paths, set Header Search Paths to /usr/local/include
also under Search Paths, set Library Search Paths to /usr/local/lib
under Apple LLVM compiler 4.2 - Language set C++ Standard Library to libstd++ (For OpenCV 2.4.6, Xcode 5, LLVM 5.0, and 10.8.5, set both language dialect and std library to "Compiler Default" instead of "libstd++")
Add the compiled opencv libraries to your project
go the the Build Phases tab next to Build Settings tab you were in
inside Link Binary With Libraries, click on the + sign and choose Add Other
hit the front slash / on your keyboard and enter /usr/local/lib
hit enter and select the libraries you want to use in your project
make sure you always select libopencv_core.2.4.4.dylib
hit enter and you will see the selected dylibs under your project
write some code
first lets organize the files, right click on your project blueprint icon and select New Group
name the new group opencv or whatever
drag the dylibs and drop them in that group
open main.cpp
copy code from any of the sample tests that came with opencv and paste it here
make sure all the required dylibs are added, for example, if you copied the opencv_test_stitching.cpp code into main.cpp, you will need to add the following libraries in the previous steps
libopencv_core.2.4.4.dylib
libopencv_highgui.2.4.4.dylib
libopencv_stitching.2.4.4.dylib
Cheers.
It seems that your project is missing the framework AssetsLibrary.
Select the top node in the project navigator. (The project and targets page with the build settings appears.)
Select the target.
Select Summary.
Scroll down to Linked Frameworks and Libraries.
Click the Plus icon at the end of the table and select AssetsLibrary.framework.
Click Add.
Then try to build it again.
Since I can't seem to get an answer regarding the versions (neither here nor at the OpenCV Q&A site), I'm going to post this as an answer, as it at least solved the issue. This is described here.
In your project's Build Settings, go down to the section Apple LLVM compiler 4.1-Language.
There:
Set C++ Language Dialect to Compiler Default
Set C++ Standard Libray to libc++ (LLVM C++ standard libray with C++ 11 support
After doing the above, I stopped getting those linker errors, and only got one error instead, which stated that only iOS 5 and above is supported. Changing the Deployment Target to 5.0 in the project summery did the trick.
On a final note, I'm still not sure what it means, regarding OpenCV 2.4.3's compatibility with iOS versions older than 5.
clang: error: invalid deployment target for -stdlib=libc++ (requires iOS 5.0 or later) to remove this error.
GoTo BuildSettings. Set c++ standard library to compiler default. The
error will get removed surely.
Just to get this into the postings somewhere in case someone else runs into the same thing. If you follow all the great advice aboutsetting the proper c++ library to link against for building an iOS app BUT still get the link errors for undefined symbols make sure your code files are set to compile as c++! That is rename your .m to .mm and .h to .hpp. It's the little things...
Instead of using terminal commands given in the opencv installation guide in official website, use the following commands to build opencv from terminal. Worked for me.
cd OpenCV-2.3.1
mkdir build
cd build
cmake -G "Unix Makefiles" ..
make
sudo make install
Go Xcode/General/Linked Frameworks and Libraries
Press "+" button
type: AssetsLibrary
Select "AssetsLibrary.framework" and import it
Done
Good luck!

Resources