Compile the libevent library on iOS

Source: Internet
Author: User
Tags openssl library openssl version

Yesterday, libevent ran through on Android, using the process solution mentioned in the previous article. Today, the libevent on iOS is also compiled. Cross-compilation on iOS is much smoother than that of ndk, probably by finding a good article for reference.

The preparation work includes the OS x system. I have installed 10.8 64-bit here. Xcode is 4.5.2, and the corresponding SDK version is 6.0. You can download the libevent version, but it is the latest official version 2.0.21 or the corresponding version for Android on git. In git, I remember it was 2.0.20, and the gap should be small. Download the OpenSSL library. The latest version 1.0.1e is used to compile and generate the configure file. If it is the official version 2.0.21-stable, the configure file is generated in the file. skip this step. If no, execute the autogen. Sh script in the directory to generate configure. In the meantime, you may need to install tools such as Autoconf, automake, and libtool, directly download the corresponding source code, and compile and install it. As for how to install it, I will give you a command list. You can check it as needed:
./configure && makesudo make install

After the above steps, the libevent source code Directory should have been generated by configure and other files. Compile the build_libevent.sh script and then compile the script. In fact, you can use the configure tool to specify the corresponding directory to generate. Here we can provide an open source script for the onion browser, which is very powerful and helps you download and compile the libevent library directly on Mac OS X. Bytes. It is worth mentioning that on Mac, multiple static databases can be integrated directly using the lipo command. For example, the static databases of the three CPU architectures i386, armv7, and armv7s can be integrated into one copy, this is really exciting. It would be nice if I could do the same on Android. Of course I haven't checked the materials and I don't know whether Linux supports lipo. Note that the libevent library has been compiled for a long time. If OpenSSL support is required, compile the OpenSSL library first, otherwise, the static library libevent_openssl.a is not included. Below I simply put the two bash scripts, the first is the libevent

#!/bin/bash#  Builds libevent for all three current iPhone targets: iPhoneSimulator-i386,#  iPhoneOS-armv6, iPhoneOS-armv7.##  Copyright 2012 Mike Tigas <mike@tig.as>##  Based on work by Felix Schulze on 16.12.10.#  Copyright 2010 Felix Schulze. All rights reserved.##  Licensed under the Apache License, Version 2.0 (the "License");#  you may not use this file except in compliance with the License.#  You may obtain a copy of the License at##  http://www.apache.org/licenses/LICENSE-2.0##  Unless required by applicable law or agreed to in writing, software#  distributed under the License is distributed on an "AS IS" BASIS,#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.#  See the License for the specific language governing permissions and#  limitations under the License.#############################################################################  Choose your libevent version and your currently-installed iOS SDK version:#VERSION="2.0.21-stable"SDKVERSION="6.1"############################################################################### Don't change anything under this line!############################################################################# No need to change this since xcode build will only compile in the# necessary bits from the libraries we createARCHS="i386 armv7 armv7s"DEVELOPER=`xcode-select -print-path`cd "`dirname \"$0\"`"REPOROOT=$(pwd)# Where we'll end up storing things in the endOUTPUTDIR="${REPOROOT}/dependencies"mkdir -p ${OUTPUTDIR}/includemkdir -p ${OUTPUTDIR}/libBUILDDIR="${REPOROOT}/build"# where we will keep our sources and build from.SRCDIR="${BUILDDIR}/src"mkdir -p $SRCDIR# where we will store intermediary buildsINTERDIR="${BUILDDIR}/built"mkdir -p $INTERDIR########################################cd $SRCDIR# Exit the script if an error happensset -eif [ ! -e "${SRCDIR}/libevent-${VERSION}.tar.gz" ]; thenecho "Downloading libevent-${VERSION}.tar.gz"    curl -LO https://github.com/downloads/libevent/libevent/libevent-${VERSION}.tar.gzelseecho "Using libevent-${VERSION}.tar.gz"fitar zxf libevent-${VERSION}.tar.gz -C $SRCDIRcd "${SRCDIR}/libevent-${VERSION}"set +e # don't bail out of bash script if ccache doesn't existCCACHE=`which ccache`if [ $? == "0" ]; then    echo "Building with ccache: $CCACHE"    CCACHE="${CCACHE} "else    echo "Building without ccache"    CCACHE=""fiset -e # back to regular "bail out on error" modefor ARCH in ${ARCHS}doif [ "${ARCH}" == "i386" ];thenPLATFORM="iPhoneSimulator"        EXTRA_CONFIG=""elsePLATFORM="iPhoneOS"        EXTRA_CONFIG="--host=arm-apple-darwin11"fimkdir -p "${INTERDIR}/${PLATFORM}${SDKVERSION}-${ARCH}.sdk"./configure --disable-shared --enable-static --disable-debug-mode ${EXTRA_CONFIG} \    --prefix="${INTERDIR}/${PLATFORM}${SDKVERSION}-${ARCH}.sdk" \    CC="${CCACHE}${DEVELOPER}/Platforms/${PLATFORM}.platform/Developer/usr/bin/gcc -arch ${ARCH}" \    LDFLAGS="$LDFLAGS -L${OUTPUTDIR}/lib" \    CFLAGS="$CFLAGS -I${OUTPUTDIR}/include -isysroot ${DEVELOPER}/Platforms/${PLATFORM}.platform/Developer/SDKs/${PLATFORM}${SDKVERSION}.sdk" \    CPPFLAGS="$CPPFLAGS -I${OUTPUTDIR}/include -isysroot ${DEVELOPER}/Platforms/${PLATFORM}.platform/Developer/SDKs/${PLATFORM}${SDKVERSION}.sdk"    # Build the application and install it to the fake SDK intermediary dir    # we have set up. Make sure to clean up afterward because we will re-use    # this source tree to cross-compile other targets.make -j4make installmake cleandone########################################echo "Build library..."# These are the libs that comprise libevent. `libevent_openssl` and `libevent_pthreads`# may not be compiled if those dependencies aren't available.OUTPUT_LIBS="libevent.a libevent_core.a libevent_extra.a libevent_openssl.a libevent_pthreads.a"for OUTPUT_LIB in ${OUTPUT_LIBS}; do    INPUT_LIBS=""    for ARCH in ${ARCHS}; do        if [ "${ARCH}" == "i386" ];        then            PLATFORM="iPhoneSimulator"        else            PLATFORM="iPhoneOS"        fi        INPUT_ARCH_LIB="${INTERDIR}/${PLATFORM}${SDKVERSION}-${ARCH}.sdk/lib/${OUTPUT_LIB}"        if [ -e $INPUT_ARCH_LIB ]; then            INPUT_LIBS="${INPUT_LIBS} ${INPUT_ARCH_LIB}"        fi    done    # Combine the three architectures into a universal library.    if [ -n "$INPUT_LIBS"  ]; then        lipo -create $INPUT_LIBS \        -output "${OUTPUTDIR}/lib/${OUTPUT_LIB}"    else        echo "$OUTPUT_LIB does not exist, skipping (are the dependencies installed?)"    fidonefor ARCH in ${ARCHS}; do    if [ "${ARCH}" == "i386" ];    then        PLATFORM="iPhoneSimulator"    else        PLATFORM="iPhoneOS"    fi    cp -R ${INTERDIR}/${PLATFORM}${SDKVERSION}-${ARCH}.sdk/include/* ${OUTPUTDIR}/include/    if [ $? == "0" ]; then        # We only need to copy the headers over once. (So break out of forloop        # once we get first success.)        break    fidone####################echo "Building done."echo "Cleaning up..."rm -fr ${INTERDIR}rm -fr "${SRCDIR}/libevent-${VERSION}"echo "Done."

Note the following two points:

  1. The version of the above Library and the corresponding SDK version (the same is true when OpenSSL is compiled). Set the version according to your needs, especially the SDK version. The default value is 6.1, I used xcode4.5.2. I looked at the Directory and found that there is only 6.0 SDK. Therefore, we need to set it according to the SDK version in your own xcode. Otherwise, the compiler will not be found.
  2. When setting developer variables, note that the path obtained through xcode-select-print-path on my machine is/volumne/xcode. APP/contents/developer, But I am actually on the desktop. If this directory is incorrect, the compiler may not be found. You can write an absolute path here (do not use a relative path, and an error will be reported)
Run bash directly in your directory. If there is no OpenSSL Library and the Library libevent_openssl.a does not exist after compilation, OpenSSL is invalid during the configure process and the corresponding code is commented out in makefile. Therefore, if OpenSSL is required, run the build_ssl.sh script first. Similarly, the precautions are the same as those of build_libevent.sh. In fact, these two scripts can be placed in the same libevent directory.
#!/bin/bash#  Builds openssl for all three current iPhone targets: iPhoneSimulator-i386,#  iPhoneOS-armv6, iPhoneOS-armv7.##  Copyright 2012 Mike Tigas <mike@tig.as>##  Based on work by Felix Schulze on 16.12.10.#  Copyright 2010 Felix Schulze. All rights reserved.##  Licensed under the Apache License, Version 2.0 (the "License");#  you may not use this file except in compliance with the License.#  You may obtain a copy of the License at##  http://www.apache.org/licenses/LICENSE-2.0##  Unless required by applicable law or agreed to in writing, software#  distributed under the License is distributed on an "AS IS" BASIS,#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.#  See the License for the specific language governing permissions and#  limitations under the License.#############################################################################  Choose your openssl version and your currently-installed iOS SDK version:#VERSION="1.0.1e"SDKVERSION="6.1"############################################################################### Don't change anything under this line!############################################################################# No need to change this since xcode build will only compile in the# necessary bits from the libraries we createARCHS="i386 armv7 armv7s"DEVELOPER=`xcode-select -print-path`cd "`dirname \"$0\"`"REPOROOT=$(pwd)# Where we'll end up storing things in the endOUTPUTDIR="${REPOROOT}/dependencies"mkdir -p ${OUTPUTDIR}/includemkdir -p ${OUTPUTDIR}/libBUILDDIR="${REPOROOT}/build"# where we will keep our sources and build from.SRCDIR="${BUILDDIR}/src"mkdir -p $SRCDIR# where we will store intermediary buildsINTERDIR="${BUILDDIR}/built"mkdir -p $INTERDIR########################################cd $SRCDIR# Exit the script if an error happensset -eif [ ! -e "${SRCDIR}/openssl-${VERSION}.tar.gz" ]; thenecho "Downloading openssl-${VERSION}.tar.gz"    curl -O http://www.openssl.org/source/openssl-${VERSION}.tar.gzelseecho "Using openssl-${VERSION}.tar.gz"fitar zxf openssl-${VERSION}.tar.gz -C $SRCDIRcd "${SRCDIR}/openssl-${VERSION}"set +e # don't bail out of bash script if ccache doesn't existCCACHE=`which ccache`if [ $? == "0" ]; then    echo "Building with ccache: $CCACHE"    CCACHE="${CCACHE} "else    echo "Building without ccache"    CCACHE=""fiset -e # back to regular "bail out on error" modefor ARCH in ${ARCHS}doif [ "${ARCH}" == "i386" ];thenPLATFORM="iPhoneSimulator"elsesed -ie "s!static volatile sig_atomic_t intr_signal;!static volatile intr_signal;!" "crypto/ui/ui_openssl.c"PLATFORM="iPhoneOS"fiecho "Building openssl-${VERSION} for ${PLATFORM} ${SDKVERSION} ${ARCH}"echo "Please stand by..."mkdir -p "${INTERDIR}/${PLATFORM}${SDKVERSION}-${ARCH}.sdk"#LOG="${INTERDIR}/${PLATFORM}${SDKVERSION}-${ARCH}.sdk/build-openssl-${VERSION}.log"    export CC="${CCACHE}${DEVELOPER}/Platforms/${PLATFORM}.platform/Developer/usr/bin/gcc -arch ${ARCH}"./configure BSD-generic32 \    --openssldir="${INTERDIR}/${PLATFORM}${SDKVERSION}-${ARCH}.sdk" #> "${LOG}" 2>&1# add -isysroot to configure-generated CFLAGSsed -ie "s!^CFLAG=!CFLAG=-isysroot ${DEVELOPER}/Platforms/${PLATFORM}.platform/Developer/SDKs/${PLATFORM}${SDKVERSION}.sdk !" "Makefile"    # Build the application and install it to the fake SDK intermediary dir    # we have set up. Make sure to clean up afterward because we will re-use    # this source tree to cross-compile other targets.make #>> "${LOG}" 2>&1make install #>> "${LOG}" 2>&1make clean #>> "${LOG}" 2>&1done########################################echo "Build library..."OUTPUT_LIBS="libssl.a libcrypto.a"for OUTPUT_LIB in ${OUTPUT_LIBS}; do    INPUT_LIBS=""    for ARCH in ${ARCHS}; do        if [ "${ARCH}" == "i386" ];        then            PLATFORM="iPhoneSimulator"        else            PLATFORM="iPhoneOS"        fi        INPUT_ARCH_LIB="${INTERDIR}/${PLATFORM}${SDKVERSION}-${ARCH}.sdk/lib/${OUTPUT_LIB}"        if [ -e $INPUT_ARCH_LIB ]; then            INPUT_LIBS="${INPUT_LIBS} ${INPUT_ARCH_LIB}"        fi    done    # Combine the three architectures into a universal library.    if [ -n "$INPUT_LIBS"  ]; then        lipo -create $INPUT_LIBS \        -output "${OUTPUTDIR}/lib/${OUTPUT_LIB}"    else        echo "$OUTPUT_LIB does not exist, skipping (are the dependencies installed?)"    fidonefor ARCH in ${ARCHS}; do    if [ "${ARCH}" == "i386" ];    then        PLATFORM="iPhoneSimulator"    else        PLATFORM="iPhoneOS"    fi    cp -R ${INTERDIR}/${PLATFORM}${SDKVERSION}-${ARCH}.sdk/include/* ${OUTPUTDIR}/include/    if [ $? == "0" ]; then        # We only need to copy the headers over once. (So break out of forloop        # once we get first success.)        break    fidoneecho "Building done."echo "Cleaning up..."rm -fr ${INTERDIR}rm -fr "${SRCDIR}/openssl-${VERSION}"echo "Done."

Thanks again to the author of the onion browser for the powerful script. After compilation, the corresponding library files are stored in the lib directory of the dependencies directory. The outer include file is the header file required for the corresponding Library call. Well, the libevent library on iOS is done in this way. By the way, a compiled library file is lost: Download

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.