krzyzanowskim / OpenSSL

OpenSSL package for SwiftPM, CocoaPod, and Carthage, multiplatform
https://swift.best
Other
912 stars 337 forks source link

Add support for apple watch / watchos #192

Closed VityaSchel closed 3 months ago

VityaSchel commented 5 months ago

why

VityaSchel commented 5 months ago

why even visionos but no watchos

VityaSchel commented 5 months ago

wow even tvos

I surely think someone uses openssl on tv I literally can imagine how you open your pentium tv and generate pgp crypto keys on it for watching youtube

VityaSchel commented 5 months ago

I'll briefly describe steps for future reference:

  1. add build_watchos function to scripts/build.sh with call in the end of this script

    build_watchos() {
    local TMP_BUILD_DIR=$( mktemp -d )
    
    # Clean up whatever was left from our previous build
    rm -rf "${SCRIPT_DIR}"/../{watchsimulator/include,watchsimulator/lib}
    mkdir -p "${SCRIPT_DIR}"/../{watchsimulator/include,watchsimulator/lib}
    
    build "x86_64" "WatchSimulator" ${TMP_BUILD_DIR} "watchsimulator"
    build "arm64" "WatchSimulator" ${TMP_BUILD_DIR} "watchsimulator"
    
    rm -rf "${SCRIPT_DIR}"/../{watchos/include,watchos/lib}
    mkdir -p "${SCRIPT_DIR}"/../{watchos/include,watchos/lib}
    
    build "arm64" "WatchOS" ${TMP_BUILD_DIR} "watchos"
    
    ditto "${TMP_BUILD_DIR}/${OPENSSL_VERSION}-WatchOS-arm64/include/openssl" "${SCRIPT_DIR}/../watchos/include/${FWNAME}"
    cp -f "${SCRIPT_DIR}/../shim/shim.h" "${SCRIPT_DIR}/../watchos/include/${FWNAME}/shim.h"
    
    # Copy headers
    ditto "${TMP_BUILD_DIR}/${OPENSSL_VERSION}-WatchSimulator-arm64/include/openssl" "${SCRIPT_DIR}/../watchsimulator/include/${FWNAME}"
    cp -f "${SCRIPT_DIR}/../shim/shim.h" "${SCRIPT_DIR}/../watchsimulator/include/${FWNAME}/shim.h"
    
    # fix inttypes.h
    # find "${SCRIPT_DIR}/../appletvos/include/${FWNAME}" -type f -name "*.h" -exec sed -i "" -e "s/include <inttypes\.h>/include <sys\/types\.h>/g" {} \;
    # find "${SCRIPT_DIR}/../appletvsimulator/include/${FWNAME}" -type f -name "*.h" -exec sed -i "" -e "s/include <inttypes\.h>/include <sys\/types\.h>/g" {} \;
    
    local OPENSSLCONF_PATH="${SCRIPT_DIR}/../watchsimulator/include/${FWNAME}/opensslconf.h"
    echo "#if defined(__APPLE__) && defined (__x86_64__)" >> ${OPENSSLCONF_PATH}
    cat ${TMP_BUILD_DIR}/${OPENSSL_VERSION}-WatchSimulator-x86_64/include/openssl/opensslconf.h >> ${OPENSSLCONF_PATH}
    
    echo "#elif defined(__APPLE__) && defined (__arm64__)" >> ${OPENSSLCONF_PATH}
    cat ${TMP_BUILD_DIR}/${OPENSSL_VERSION}-WatchSimulator-arm64/include/openssl/opensslconf.h >> ${OPENSSLCONF_PATH}
    echo "#endif" >> ${OPENSSLCONF_PATH}
    
    OPENSSLCONF_PATH="${SCRIPT_DIR}/../watchos/include/${FWNAME}/opensslconf.h"
    echo "#if defined(__APPLE__) && defined (__x86_64__)" >> ${OPENSSLCONF_PATH}
    
    echo "#elif defined(__APPLE__) && defined (__arm64__)" >> ${OPENSSLCONF_PATH}
    cat ${TMP_BUILD_DIR}/${OPENSSL_VERSION}-WatchOS-arm64/include/openssl/opensslconf.h >> ${OPENSSLCONF_PATH}
    echo "#endif" >> ${OPENSSLCONF_PATH}
    
    # Update include "openssl/" to "OpenSSL/"
    grep -rl '#\s*include\s*<openssl' --include \*.h ${SCRIPT_DIR}/../watchos/include | xargs -I@ sed -i '' -e 's/#[[:space:]]*include[[:space:]]*<openssl/#include <OpenSSL/gi' @
    grep -rl '#\s*include\s*<openssl' --include \*.h ${SCRIPT_DIR}/../watchsimulator/include | xargs -I@ sed -i '' -e 's/#[[:space:]]*include[[:space:]]*<openssl/#include <OpenSSL/gi' @
    
    rm -rf ${TMP_BUILD_DIR}
    }
  2. add section to scripts/create-frameworks.sh

# watchOS
DERIVED_DATA_PATH=$( mktemp -d )
xcrun xcodebuild build \
    $COMMON_SETUP \
    -scheme "${FWNAME} (watchOS)" \
    -derivedDataPath "${DERIVED_DATA_PATH}" \
    -destination 'generic/platform=watchOS'

rm -rf "${OUTPUT_DIR}/watchos"
mkdir -p "${OUTPUT_DIR}/watchos"
ditto "${DERIVED_DATA_PATH}/Build/Products/Release-watchos/${FWNAME}.framework" "${OUTPUT_DIR}/watchos/${FWNAME}.framework"
rm -rf "${DERIVED_DATA_PATH}"

# watchOS Simulator
DERIVED_DATA_PATH=$( mktemp -d )
xcrun xcodebuild build \
    $COMMON_SETUP \
    -scheme "${FWNAME} (watchOS Simulator)" \
    -derivedDataPath "${DERIVED_DATA_PATH}" \
    -destination 'generic/platform=watchOS Simulator'

rm -rf "${OUTPUT_DIR}/watchsimulator"
mkdir -p "${OUTPUT_DIR}/watchsimulator"
ditto "${DERIVED_DATA_PATH}/Build/Products/Release-watchsimulator/${FWNAME}.framework" "${OUTPUT_DIR}/watchsimulator/${FWNAME}.framework"
rm -rf "${DERIVED_DATA_PATH}"
  1. add configurations to conf/20-apple.conf otherwise you'll get Usage: Configure [no-<cipher> ...] [enable-<cipher> ...] [-Dxxx] [-lxxx] [-Lxxx] [-fxxx] [-Kxxx] [no-hw-xxx|no-hw] [[no-]threads] [[no-]shared] [[no-]zlib|zlib-dynamic] [no-asm] [no-egd] [sctp] [386] [--prefix=DIR] [--openssldir=OPENSSLDIR] [--with-xxx[=vvv]] [--config=FILE] os/compiler[:flags] and pick os/compiler from: and Using os-specific seed configuration
'watchos-cross-base' => {
        template => 1,
        cflags => combine('-isysroot $(CROSS_TOP)/SDKs/$(CROSS_SDK) -fno-common',
            sub { defined($ENV{'WATCHOS_DEPLOYMENT_VERSION'}) ? '-mwatchos-version-min=$(WATCHOS_DEPLOYMENT_VERSION)' : '-mwatchos-version-min=4.0'; }),
        disable => [ 'engine', 'async' ],
        defines => [ "HAVE_FORK=0" ]
    },

    'watchos-sim-cross-base' => {
        inherit_from => [ 'watchos-cross-base' ],
        template => 1,
        cflags => add(sub { defined($ENV{'WATCHOS_DEPLOYMENT_VERSION'}) ? '-mwatchos-simulator-version-min=$(WATCHOS_DEPLOYMENT_VERSION)' : '-mwatchos-simulator-version-min=4.0'; }),
    },

    'watchos-sim-cross-x86_64' => {
        inherit_from => [ 'darwin-common', 'watchos-sim-cross-base' ],
        CC => 'xcrun -sdk watchsimulator cc',
        cflags => add('-arch x86_64'),
        lib_cppflags => add('-DL_ENDIAN'),
        bn_ops => 'SIXTY_FOUR_BIT_LONG',
        sys_id => 'watchOS',
        perlasm_scheme => 'watchos64',
    },

    'watchos-sim-cross-arm64' => {
        inherit_from => [ 'darwin-common', 'watchos-sim-cross-base' ],
        CC => 'xcrun -sdk watchsimulator cc',
        cflags => add('-arch arm64'),
        asm_arch => 'aarch64',
        lib_cppflags => add('-DL_ENDIAN'),
        bn_ops => 'SIXTY_FOUR_BIT_LONG RC4_CHAR',
        perlasm_scheme => 'watchos64',
        sys_id => 'watchOS',
    },

    'watchos-cross-arm64' => {
        inherit_from => [ 'darwin-common', 'watchos-cross-base' ],
        cflags => add('-arch arm64'),
        asm_arch => 'aarch64',
        bn_ops => 'SIXTY_FOUR_BIT_LONG RC4_CHAR',
        lib_cppflags => add('-DL_ENDIAN'),
        perlasm_scheme => 'watchos64',
        sys_id => 'watchOS',
    },

    'watchos-cross-armv7k' => {
        inherit_from => [ 'darwin-common', 'watchos-cross-base' ],
        cflags => add('-arch armv7k'),
        asm_arch => 'arm',
        bn_ops => 'BN_LLONG RC4_INT',
        lib_cppflags => add('-DL_ENDIAN'),
        perlasm_scheme => 'ios32',
        sys_id => 'watchOS',
    },

we're also having defines => [ "HAVE_FORK=0" ] in base config because watchos does not support fork()

then run make and it should create watchos and watchsimulator directories in repo root, after than figure out on your own how to add these to frameworks in xcode project

krzyzanowskim commented 5 months ago

There's always a way to make a PR. This may be an inspiration how to make it: https://github.com/krzyzanowskim/OpenSSL/pull/205

XITRIX commented 3 months ago

I've made it #216

VityaSchel commented 3 months ago

I've made it #216

yippeee-yippee-gif

krzyzanowskim commented 3 months ago

thx https://github.com/krzyzanowskim/OpenSSL/pull/216