113498266Sopenharmony_ci#!/usr/bin/env bash
213498266Sopenharmony_ci#***************************************************************************
313498266Sopenharmony_ci#                                  _   _ ____  _
413498266Sopenharmony_ci#  Project                     ___| | | |  _ \| |
513498266Sopenharmony_ci#                             / __| | | | |_) | |
613498266Sopenharmony_ci#                            | (__| |_| |  _ <| |___
713498266Sopenharmony_ci#                             \___|\___/|_| \_\_____|
813498266Sopenharmony_ci#
913498266Sopenharmony_ci# Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
1013498266Sopenharmony_ci#
1113498266Sopenharmony_ci# This software is licensed as described in the file COPYING, which
1213498266Sopenharmony_ci# you should have received as part of this distribution. The terms
1313498266Sopenharmony_ci# are also available at https://curl.se/docs/copyright.html.
1413498266Sopenharmony_ci#
1513498266Sopenharmony_ci# You may opt to use, copy, modify, merge, publish, distribute and/or sell
1613498266Sopenharmony_ci# copies of the Software, and permit persons to whom the Software is
1713498266Sopenharmony_ci# furnished to do so, under the terms of the COPYING file.
1813498266Sopenharmony_ci#
1913498266Sopenharmony_ci# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
2013498266Sopenharmony_ci# KIND, either express or implied.
2113498266Sopenharmony_ci#
2213498266Sopenharmony_ci# SPDX-License-Identifier: curl
2313498266Sopenharmony_ci#
2413498266Sopenharmony_ci###########################################################################
2513498266Sopenharmony_ci# This script performs all of the steps needed to build a
2613498266Sopenharmony_ci# universal binary libcurl.framework for Mac OS X 10.4 or greater.
2713498266Sopenharmony_ci#
2813498266Sopenharmony_ci# Hendrik Visage:
2913498266Sopenharmony_ci#  Generalizations added since  Snowleopard (10.6) do not include
3013498266Sopenharmony_ci# the 10.4u SDK.
3113498266Sopenharmony_ci#
3213498266Sopenharmony_ci# Also note:
3313498266Sopenharmony_ci# 10.5 is the *ONLY* SDK that support PPC64 :( -- 10.6 do not have ppc64 support
3413498266Sopenharmony_ci#If you need to have PPC64 support then change below to 1
3513498266Sopenharmony_ciPPC64_NEEDED=0
3613498266Sopenharmony_ci# Apple does not support building for PPC anymore in Xcode 4 and later.
3713498266Sopenharmony_ci# If you're using Xcode 3 or earlier and need PPC support, then change
3813498266Sopenharmony_ci# the setting below to 1
3913498266Sopenharmony_ciPPC_NEEDED=0
4013498266Sopenharmony_ci
4113498266Sopenharmony_ci# For me the default is to develop for the platform I am on, and if you
4213498266Sopenharmony_ci#desire compatibility with older versions then change USE_OLD to 1 :)
4313498266Sopenharmony_ciUSE_OLD=0
4413498266Sopenharmony_ci
4513498266Sopenharmony_ciVERSION=`/usr/bin/sed -ne 's/^#define LIBCURL_VERSION "\(.*\)"/\1/p' include/curl/curlver.h`
4613498266Sopenharmony_ciFRAMEWORK_VERSION=Versions/Release-$VERSION
4713498266Sopenharmony_ci
4813498266Sopenharmony_ci#I also wanted to "copy over" the system, and thus the reason I added the
4913498266Sopenharmony_ci# version to Versions/Release-7.20.1 etc.
5013498266Sopenharmony_ci# now a simple rsync -vaP libcurl.framework /Library/Frameworks will install it
5113498266Sopenharmony_ci# and setup the right paths to this version, leaving the system version
5213498266Sopenharmony_ci# "intact", so you can "fix" it later with the links to Versions/A/...
5313498266Sopenharmony_ci
5413498266Sopenharmony_ciDEVELOPER_PATH=`xcode-select --print-path`
5513498266Sopenharmony_ci# Around Xcode 4.3, SDKs were moved from the Developer folder into the
5613498266Sopenharmony_ci# MacOSX.platform folder
5713498266Sopenharmony_ciif test -d "$DEVELOPER_PATH/Platforms/MacOSX.platform/Developer/SDKs"; then
5813498266Sopenharmony_ci SDK_PATH="$DEVELOPER_PATH/Platforms/MacOSX.platform/Developer/SDKs"
5913498266Sopenharmony_cielse
6013498266Sopenharmony_ci SDK_PATH="$DEVELOPER_PATH/SDKs"
6113498266Sopenharmony_cifi
6213498266Sopenharmony_ciOLD_SDK=`ls  $SDK_PATH|head -1`
6313498266Sopenharmony_ciNEW_SDK=`ls -r $SDK_PATH|head -1`
6413498266Sopenharmony_ci
6513498266Sopenharmony_ciif test "0"$USE_OLD -gt 0
6613498266Sopenharmony_cithen
6713498266Sopenharmony_ci SDK32=$OLD_SDK
6813498266Sopenharmony_cielse
6913498266Sopenharmony_ci SDK32=$NEW_SDK
7013498266Sopenharmony_cifi
7113498266Sopenharmony_ci
7213498266Sopenharmony_ciMACVER=`echo $SDK32|sed -e s/[a-zA-Z]//g -e s/.\$//`
7313498266Sopenharmony_ci
7413498266Sopenharmony_ciSDK32_DIR=$SDK_PATH/$SDK32
7513498266Sopenharmony_ciMINVER32='-mmacosx-version-min='$MACVER
7613498266Sopenharmony_ciif test $PPC_NEEDED -gt 0; then
7713498266Sopenharmony_ci ARCHES32='-arch i386 -arch ppc'
7813498266Sopenharmony_cielse
7913498266Sopenharmony_ci ARCHES32='-arch i386'
8013498266Sopenharmony_cifi
8113498266Sopenharmony_ci
8213498266Sopenharmony_ciif test $PPC64_NEEDED -gt 0
8313498266Sopenharmony_cithen
8413498266Sopenharmony_ci  SDK64=10.5
8513498266Sopenharmony_ci  ARCHES64='-arch x86_64 -arch ppc64'
8613498266Sopenharmony_ci  SDK64=`ls  $SDK_PATH | grep "10\.5" | head -1`
8713498266Sopenharmony_cielse
8813498266Sopenharmony_ci ARCHES64='-arch x86_64'
8913498266Sopenharmony_ci #We "know" that 10.4 and earlier do not support 64bit
9013498266Sopenharmony_ci OLD_SDK64=`ls  $SDK_PATH | grep -v "10\.[0-4]" | head -1`
9113498266Sopenharmony_ci NEW_SDK64=`ls -r $SDK_PATH | grep -v "10\.[0-4][^0-9]" | head -1`
9213498266Sopenharmony_ci if test $USE_OLD -gt 0
9313498266Sopenharmony_ci  then
9413498266Sopenharmony_ci   SDK64=$OLD_SDK64
9513498266Sopenharmony_ci  else
9613498266Sopenharmony_ci   SDK64=$NEW_SDK64
9713498266Sopenharmony_ci  fi
9813498266Sopenharmony_cifi
9913498266Sopenharmony_ci
10013498266Sopenharmony_ciSDK64_DIR=$SDK_PATH/$SDK64
10113498266Sopenharmony_ciMACVER64=`echo $SDK64|sed -e s/[a-zA-Z]//g -e s/.\$//`
10213498266Sopenharmony_ci
10313498266Sopenharmony_ciMINVER64='-mmacosx-version-min='$MACVER64
10413498266Sopenharmony_ci
10513498266Sopenharmony_ciif test ! -z $SDK32; then
10613498266Sopenharmony_ci  echo "----Configuring libcurl for 32 bit universal framework..."
10713498266Sopenharmony_ci  make clean
10813498266Sopenharmony_ci  ./configure --disable-dependency-tracking --disable-static --with-gssapi --with-secure-transport \
10913498266Sopenharmony_ci    CFLAGS="-Os -isysroot $SDK32_DIR $ARCHES32" \
11013498266Sopenharmony_ci    LDFLAGS="-Wl,-syslibroot,$SDK32_DIR $ARCHES32 -Wl,-headerpad_max_install_names" \
11113498266Sopenharmony_ci    CC=$CC
11213498266Sopenharmony_ci
11313498266Sopenharmony_ci  echo "----Building 32 bit libcurl..."
11413498266Sopenharmony_ci  make -j `sysctl -n hw.logicalcpu_max`
11513498266Sopenharmony_ci
11613498266Sopenharmony_ci  echo "----Creating 32 bit framework..."
11713498266Sopenharmony_ci  rm -r libcurl.framework
11813498266Sopenharmony_ci  mkdir -p libcurl.framework/${FRAMEWORK_VERSION}/Resources
11913498266Sopenharmony_ci  cp lib/.libs/libcurl.dylib libcurl.framework/${FRAMEWORK_VERSION}/libcurl
12013498266Sopenharmony_ci  install_name_tool -id @rpath/libcurl.framework/${FRAMEWORK_VERSION}/libcurl libcurl.framework/${FRAMEWORK_VERSION}/libcurl
12113498266Sopenharmony_ci  cp lib/libcurl.plist libcurl.framework/${FRAMEWORK_VERSION}/Resources/Info.plist
12213498266Sopenharmony_ci  mkdir -p libcurl.framework/${FRAMEWORK_VERSION}/Headers/curl
12313498266Sopenharmony_ci  cp include/curl/*.h libcurl.framework/${FRAMEWORK_VERSION}/Headers/curl
12413498266Sopenharmony_ci  pushd libcurl.framework
12513498266Sopenharmony_ci  ln -fs ${FRAMEWORK_VERSION}/libcurl libcurl
12613498266Sopenharmony_ci  ln -fs ${FRAMEWORK_VERSION}/Resources Resources
12713498266Sopenharmony_ci  ln -fs ${FRAMEWORK_VERSION}/Headers Headers
12813498266Sopenharmony_ci  cd Versions
12913498266Sopenharmony_ci  ln -fs $(basename "${FRAMEWORK_VERSION}") Current
13013498266Sopenharmony_ci
13113498266Sopenharmony_ci  echo Testing for SDK64
13213498266Sopenharmony_ci  if test -d $SDK64_DIR; then
13313498266Sopenharmony_ci  echo entering...
13413498266Sopenharmony_ci    popd
13513498266Sopenharmony_ci    make clean
13613498266Sopenharmony_ci    echo "----Configuring libcurl for 64 bit universal framework..."
13713498266Sopenharmony_ci    ./configure --disable-dependency-tracking --disable-static --with-gssapi --with-secure-transport \
13813498266Sopenharmony_ci      CFLAGS="-Os -isysroot $SDK64_DIR $ARCHES64" \
13913498266Sopenharmony_ci      LDFLAGS="-Wl,-syslibroot,$SDK64_DIR $ARCHES64 -Wl,-headerpad_max_install_names" \
14013498266Sopenharmony_ci      CC=$CC
14113498266Sopenharmony_ci
14213498266Sopenharmony_ci    echo "----Building 64 bit libcurl..."
14313498266Sopenharmony_ci    make -j `sysctl -n hw.logicalcpu_max`
14413498266Sopenharmony_ci
14513498266Sopenharmony_ci    echo "----Appending 64 bit framework to 32 bit framework..."
14613498266Sopenharmony_ci    cp lib/.libs/libcurl.dylib libcurl.framework/${FRAMEWORK_VERSION}/libcurl64
14713498266Sopenharmony_ci    install_name_tool -id @rpath/libcurl.framework/${FRAMEWORK_VERSION}/libcurl libcurl.framework/${FRAMEWORK_VERSION}/libcurl64
14813498266Sopenharmony_ci    cp libcurl.framework/${FRAMEWORK_VERSION}/libcurl libcurl.framework/${FRAMEWORK_VERSION}/libcurl32
14913498266Sopenharmony_ci    pwd
15013498266Sopenharmony_ci    lipo libcurl.framework/${FRAMEWORK_VERSION}/libcurl32 libcurl.framework/${FRAMEWORK_VERSION}/libcurl64 -create -output libcurl.framework/${FRAMEWORK_VERSION}/libcurl
15113498266Sopenharmony_ci    rm libcurl.framework/${FRAMEWORK_VERSION}/libcurl32 libcurl.framework/${FRAMEWORK_VERSION}/libcurl64
15213498266Sopenharmony_ci  fi
15313498266Sopenharmony_ci
15413498266Sopenharmony_ci  pwd
15513498266Sopenharmony_ci  lipo -info libcurl.framework/${FRAMEWORK_VERSION}/libcurl
15613498266Sopenharmony_ci  echo "libcurl.framework is built and can now be included in other projects."
15713498266Sopenharmony_ci  echo "Copy libcurl.framework to your bundle's Contents/Frameworks folder, ~/Library/Frameworks or /Library/Frameworks."
15813498266Sopenharmony_cielse
15913498266Sopenharmony_ci  echo "Building libcurl.framework requires Mac OS X 10.4 or later with the MacOSX10.4/5/6 SDK installed."
16013498266Sopenharmony_cifi
161